allow luaget/luaset/luacall to access tables and methods, improve type conversion, add bl.string, begin adding matrix

This commit is contained in:
Redo
2025-10-06 10:30:25 -05:00
parent 5f98dc017b
commit 76c758a47b
12 changed files with 223 additions and 94 deletions

View File

@@ -2,8 +2,6 @@
-- Main lua-side functionality of bllua,
-- provided through the global table 'bl.'
-- todo: set
local _bllua_ts = ts
bl = bl or {}
@@ -18,7 +16,7 @@ local function map(t,f)
return u
end
local function isValidFuncName(name)
return type(name)=='string' and name:find('^[a-zA-Z0-9_]+$')
return type(name)=='string' and name:find('^[a-zA-Z_][a-zA-Z0-9_]*$')
end
local function isValidFuncNameNs(name)
return type(name)=='string' and (
@@ -76,7 +74,7 @@ for k,v in pairs(tsTypesByName) do
tsTypesByNum[v] = k
end
-- Type conversion
-- Type conversion from Lua to TS
local toTsObject
-- Convert a string from TS into a boolean
-- Note: Nonempty nonnumeric strings evaluate to 1, unlike in TS
@@ -100,21 +98,61 @@ local function valToTs(val)
-- box - > 6 numbers
return table.concat(val[1], ' ')..' '..table.concat(val[2], ' ')
else
error('valToTs: could not convert table', 3)
error('valToTs: cannot pass Lua tables to TorqueScript.', 3)
end
else
error('valToTs: could not convert '..type(val), 3)
error('valToTs: could not convert value to TorqueScript: '..tostring(val), 3)
end
end
-- Type conversion from TS to Lua
local fromTsForceTypes = {
['boolean'] = tsBool,
['object'] = function(val) toTsObject(val) end, -- toTsObject not defined yet
['string'] = tostring,
['object'] = function(val) toTsObject(val) end, -- wrap because toTsObject not defined yet
['string'] = function(val) return val end,
}
local function convertValFromTs(val, typ)
local function forceValFromTs(val, typ)
return fromTsForceTypes[typ](val) or
error('valFromTs: invalid force type '..typ, 4)
end
local function vectorFromTs(val)
local xS,yS,zS = val:match('^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
if xS then return vector{tonumber(xS),tonumber(yS),tonumber(zS)}
else return nil end
end
local function boxFromTs(val)
local x1S,y1S,z1S,x2S,y2S,z2S = val:match(
'^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+) '..
'(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
if x1S then return {
vector{tonumber(x1S),tonumber(y1S),tonumber(z1S)},
vector{tonumber(x2S),tonumber(y2S),tonumber(z2S)} }
else return nil end
end
local function multinumericFromTs(val)
local tsNumPat = '%-?[0-9]+%.?[0-9]*e?[0-9]*'
if val:find('^'..tsNumPat) then
local nums = {}
for _,part in ipairs(val:split(' ')) do
if part:find('^'..tsNumPat..'$') then
table.insert(nums, tonumber(part))
else
return nil
end
end
if #nums==2 or #nums==3 then
return vector(nums)
elseif #nums==6 then -- box
return {
vector{nums[1], nums[2], nums[3]},
vector{nums[4], nums[5], nums[6]} }
elseif #nums==7 then -- axis
return nil --return matrix(nums)
else
return nil
end
end
end
bl._forceType = bl._forceType or {}
local function valFromTs(val, name, name2) -- todo: ensure name and name2 are already lowercase
if type(val)~='string' then
@@ -122,13 +160,13 @@ local function valFromTs(val, name, name2) -- todo: ensure name and name2 are al
if name then
name = name:lower()
if bl._forceType[name] then
return convertValFromTs(val, bl._forceType[name])
return forceValFromTs(val, bl._forceType[name])
end
end
if name2 then
name2 = name2:lower()
if bl._forceType[name2] then
return convertValFromTs(val, bl._forceType[name2])
return forceValFromTs(val, bl._forceType[name2])
end
end
-- '' -> nil
@@ -136,16 +174,9 @@ local function valFromTs(val, name, name2) -- todo: ensure name and name2 are al
-- number
local num = tonumber(val)
if num then return num end
-- vector
local xS,yS,zS = val:match('^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
if xS then return vector{tonumber(xS),tonumber(yS),tonumber(zS)} end
local x1S,y1S,z1S,x2S,y2S,z2S = val:match(
'^(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+) '..
'(%-?[0-9%.e]+) (%-?[0-9%.e]+) (%-?[0-9%.e]+)$')
-- box (2 vectors)
if x1S then return {
vector{tonumber(x1S),tonumber(y1S),tonumber(z1S)},
vector{tonumber(x2S),tonumber(y2S),tonumber(z2S)} } end
-- vector, box, or axis->matrix
local vec = multinumericFromTs(val)
if vec then return vec end
-- string
return val
end
@@ -189,13 +220,33 @@ setForceType = function(ftname, typ)
end
bl.type = setForceType
-- Type conversion TS->Lua backwards, convert back to string
local function numFromTsTostring(val)
-- todo: as-good-as-possible scientific notation for numbers
return tostring(val)
end
local function valFromTsTostring(val)
if type(val)=='string' then
return val
elseif type(val)=='number' then
return numFromTsTostring(val)
elseif type(val)=='table' then
if val.__is_vector then
local strs = {}
for i=1,#val do strs[i] = numFromTsTostring(val[i]) end
return table.concat(strs, ' ')
elseif val.__is_matrix then
-- todo: matrix back to axis-angle string
error('bl.string: matrix not yet supported', 3)
end
end
error('bl.string: cannot convert \''..type(val)..'\'', 3)
end
-- Value detection
-- Getting info from TS about functions and objects
local function isTsObject(t)
return type(t)=='table' and t._tsObjectId~=nil
end
local function tsIsObject(name) return tsBool(_bllua_ts.call('isObject', name)) end
local function tsIsFunction(name) return tsBool(_bllua_ts.call('isFunction', name)) end
local function tsIsFunctionNs(ns, name) return tsBool(_bllua_ts.call('isFunction', ns, name)) end
@@ -204,27 +255,31 @@ local function tsIsFunctionNsname(nsname)
if ns then return tsIsFunctionNs(ns, name)
else return tsIsFunction(nsname) end
end
-- sanity check to make sure objects that don't isObject are always marked as ._deleted
-- can be removed later
local function assertTsObjectExists(obj)
local is = tsIsObject(obj._tsObjectId)
if not is then
print('Warning: TS object :exists or isobject from lua but no longer exists') end
return is
end
function bl.isObject(obj)
if isTsObject(obj) then
obj = obj._tsObjectId
elseif type(obj)=='number' then
obj = tostring(obj)
elseif type(obj)~='string' then
if type(obj)=='number' then -- object id
return tsIsObject(tostring(obj))
elseif type(obj)=='string' then -- object name
return tsIsObject(obj)
elseif isTsObject(obj) then -- lua object
if obj._deleted then
return false
else
return assertTsObjectExists(obj)
end
else
error('bl.isObject: argument #1: expected torque object, number, or string', 2)
end
return tsIsObject(obj)
end
function bl.isFunction(a1, a2)
if type(a1)~='string' then
error('bl.isFunction: argument #1: expected string', 2) end
if a2 then
if type(a2)~='string' then
error('bl.isFunction: argument #2: expected string', 2) end
return tsIsFunctionNs(a1, a2)
else
return tsIsFunction(a1)
end
function bl.isFunction(name)
return tsIsFunctionNsname(name)
end
-- Torque object pseudo-class
@@ -350,7 +405,8 @@ local tsObjectMeta = {
-- Display a nice info string
__tostring = function(t)
return 'torque:'..t._tsNamespace..':'..t._tsObjectId..
(t._tsName~='' and ('('..t._tsName..')') or '')
(t._tsName~='' and ('('..t._tsName..')') or '')..
(t._deleted and '(deleted)' or '')
end,
-- #object
-- If the object has a getCount method, return its count
@@ -430,8 +486,10 @@ local tsObjectMeta = {
if t==nil then
error('ts object method: be sure to use :func() not .func()', 2) end
if t._deleted then
return false end
return tsIsObject(t._tsObjectId)
return false
else
return assertTsObjectExists(t)
end
end,
}
-- Weak-values table for caching Torque object references
@@ -464,7 +522,7 @@ toTsObject = function(idiS)
idiS = idiS:lower()
if bl._objectsWeak[idiS] then return bl._objectsWeak[idiS] end
if not tsBool(_bllua_ts.call('isObject', idiS)) then
if not tsIsObject(idiS) then
--error('toTsObject: object \''..idiS..'\' does not exist', 2) end
return nil end
@@ -560,6 +618,10 @@ end
function bl.exec(file)
return valFromTs(_bllua_ts.call('exec', file))
end
function bl.array(name, ...)
local rest = {...}
return name..table.concat(rest, '_')
end
function bl.boolean(val)
return val~=nil and
val~=false and
@@ -576,18 +638,62 @@ function bl.object(id)
error('bl.object: id must be a ts object, number, or string', 2)
end
end
function bl.array(name, ...)
local rest = {...}
return name..table.concat(rest, '_')
function bl.string(val)
return valFromTsTostring(val)
end
function _bllua_call(fnameS, ...)
local args = arglistFromTs('lua:'..fnameS:lower(), {...}) -- todo: allow this though bl.type
if not _G[fnameS] then
error('luacall: no global lua function named \''..fnameS..'\'') end
-- todo: library fields and object methods
local res = _G[fnameS](unpack(args))
-- Lua calling from TS
local luaLookup
luaLookup = function(tbl, name, set, val)
print('lookup', tbl, name, set, val)
if name:find('%.') then
local first, rest = name:match('^([^%.:]+)%.(.+)$')
if not isValidFuncName(first) then
error('luaLookup: invalid name \''..tostring(first)..'\'', 3) end
if not tbl[first] then
if set then tbl[first] = {}
else return nil end
end
return luaLookup(tbl[first], rest, set, val)
elseif name:find(':') then
local first, rest = name:match('^([^%.:]+):(.*)$')
if rest:find('[%.:]') then
error('luacall: cannot have : or . after :', 3) end
if not isValidFuncName(first) then
error('luacall: invalid name \''..tostring(first)..'\'', 3) end
if not tbl[first] then
error('luacall: no object named \''..rest..'\'', 3) end
if not tbl[first][rest] then
error('luacall: no method named \''..rest..'\'', 3) end
return function(...)
tbl[first][rest](tbl[first], ...)
end
else
if set then
tbl[name] = val
else
return tbl[name]
end
end
end
-- Todo: similar deep access for luaget and luaset
function _bllua_call(fname, ...)
local args = arglistFromTs(fname:lower(), {...}) -- todo: separate lua from ts func names?
local func = luaLookup(_G, fname)
if not func then
error('luacall: no global in lua named \''..name..'\'', 2) end
local res = func(unpack(args))
return valToTs(res)
end
function _bllua_getvar(vname)
return valToTs(luaLookup(_G, vname))
end
function _bllua_setvar(vname, valS)
return valToTs(luaLookup(_G, vname, true, valFromTs(valS, vname))) -- todo: separate lua from ts var names?
end
function _bllua_eval(code) return loadstring(code)() end
function _bllua_exec(fn) return dofile(fn, 2) end
-- bl.schedule: Use TS's schedule function to schedule lua calls
-- bl.schedule(time, function, args...)