-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathscriptClientBridge.lua
44 lines (40 loc) · 1.09 KB
/
scriptClientBridge.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
scriptClientBridge = {}
function scriptClientBridge.require(n)
_G[n] = require(n)
end
function scriptClientBridge.call(b)
cbor = require 'org.conman.cbor'
i = cbor.decode(tostring(b))
require 'var'
f = getvar(i.func)
local ok, r = pcall(function() return {f(table.unpack(i.args))} end)
if ok then
r = {success = true, result = r}
else
r = {success = false, error = r}
end
return cbor.encode(r)
end
function scriptClientBridge.info(obj)
if type(obj) == 'string' then obj = scriptClientBridge.getField(obj) end
if type(obj) ~= 'table' then return obj end
local ret = {}
for k, v in pairs(obj) do
if type(v) == 'table' then
ret[k] = scriptClientBridge.info(v)
elseif type(v) == 'function' then
ret[k] = {func = {}}
elseif type(v) ~= 'function' then
ret[k] = {const = v}
end
end
return ret
end
function scriptClientBridge.getField(f)
local v = _G
for w in string.gmatch(f, '[%w_]+') do
v = v[w]
if not v then return nil end
end
return v
end