LuPPC/src/lua/core/component.lua

178 lines
4.9 KiB
Lua
Raw Normal View History

2016-01-05 04:20:40 +11:00
local component = {}
local api = {}
2016-01-06 08:29:49 +11:00
local components = {}
2016-01-05 04:20:40 +11:00
component.api = api
2016-01-06 08:29:49 +11:00
component.components = components
local componentCallback = {
__call = function(self, ...) return components[self.address].rawproxy[self.name](...) end,
__tostring = function(self) return (components[self.address] ~= nil and components[self.address].doc[self.name] ~= nil) and components[self.address].doc[self.name] or "function" end
}
2016-01-05 04:20:40 +11:00
2016-02-12 07:45:46 +11:00
--Debug only
local start = native.uptime()
local last = native.uptime()
2016-02-10 03:40:51 +11:00
if native.debug then
componentCallback.__call = function(self, ...)
local t = {}
for k,v in pairs({...}) do
if type(v) == "string" then
v = "\"" .. v .. "\""
end
t[k] = tostring(v)
end
local caller = debug.getinfo(2)
local msg = tostring((native.uptime() - start) / 1000) .. " [+" .. native.uptime() - last .. "] " .. caller.short_src .. ":".. caller.currentline .. " > invoke(" .. self.address .. "): "
.. components[self.address].type .. "." .. self.name
.. "(" .. table.concat(t, ", ") .. ")"
native.log(msg)
last = native.uptime()
return components[self.address].rawproxy[self.name](...)
end
end
2016-01-05 04:20:40 +11:00
function component.prepare()
2016-01-06 08:29:49 +11:00
print("Assembling initial component tree")
end
function api.register(address, ctype, proxy, doc)
checkArg(2, ctype, "string")
checkArg(3, proxy, "table")
if type(address) ~= "string" then
2016-01-07 02:58:05 +11:00
address = modules.random.uuid()
2016-01-06 08:29:49 +11:00
end
if components[address] ~= nil then
return nil, "component already at address"
end
components[address] = {address = address, type = ctype, doc = doc or {}}
components[address].rawproxy = proxy
components[address].proxy = {}
for k,v in pairs(proxy) do
if type(v) == "function" then
2016-01-07 02:58:05 +11:00
components[address].proxy[k] = setmetatable({name=k,address=address}, componentCallback)
2016-01-06 08:29:49 +11:00
else
2016-01-07 02:58:05 +11:00
components[address].proxy[k] = v
2016-01-06 08:29:49 +11:00
end
end
2016-01-07 02:58:05 +11:00
modules.computer.api.pushSignal("component_added", address, ctype)
2016-01-20 04:04:12 +11:00
if native.debug then
local caller = debug.getinfo(2)
local msg = caller.short_src .. ":".. caller.currentline .. " > component.register(" .. address .. ", " .. ctype .. ")"
native.log(msg)
end
2016-01-10 05:39:48 +11:00
return address
2016-01-06 08:29:49 +11:00
end
function api.unregister(address)
checkArg(1, address, "string")
if components[address] == nil then
return nil, "no component at address"
end
local ctype = components[address].type
components[address] = nil
2016-01-07 02:58:05 +11:00
modules.computer.api.pushSignal("component_removed", address, ctype)
2016-01-06 08:29:49 +11:00
return true
end
function api.doc(address, method)
checkArg(1, address, "string")
checkArg(2, method, "string")
if not components[address] then
error("No such component")
end
return components[address].doc[method]
end
function api.invoke(address, method, ...)
checkArg(1, address, "string")
checkArg(2, method, "string")
if not components[address] then
error("No such component")
end
if not components[address].rawproxy[method] then
2016-01-10 05:39:48 +11:00
error("No such method: " .. tostring(components[address].type) .. "." .. tostring(method))
2016-01-06 08:29:49 +11:00
end
2016-02-12 07:45:46 +11:00
if native.debug then --TODO: This may generate little performance hit
local t = {}
for k,v in pairs({...}) do
if type(v) == "string" then
v = "\"" .. v .. "\""
end
t[k] = tostring(v)
end
local caller = debug.getinfo(2)
local msg = tostring((native.uptime() - start) / 1000) .. " [+" .. native.uptime() - last .. "] " .. caller.short_src .. ":".. caller.currentline .. " > c.invoke(" .. address .. "): "
.. components[address].type .. "." .. method
.. "(" .. table.concat(t, ", ") .. ")"
2016-01-20 04:04:12 +11:00
native.log(msg)
2016-02-12 07:45:46 +11:00
end
2016-01-06 08:29:49 +11:00
return components[address].rawproxy[method](...)
end
function api.list(filter, exact)
checkArg(1, filter, "string", "nil")
local list = {}
for addr, c in pairs(components) do
if not filter or ((exact and c.type or c.type:sub(1, #filter)) == filter) then
list[addr] = c.type
end
end
local key = nil
return setmetatable(list, {__call=function()
key = next(list, key)
if key then
return key, list[key]
end
end})
end
function api.methods(address)
checkArg(1, address, "string")
if not components[address] then
return nil, "No such component"
end
local list = {}
for k, v in pairs(components[address].rawproxy) do
if type(v) == "function" then
list[k] = true
end
end
return list
end
function api.fields(address)
checkArg(1, address, "string")
if not components[address] then
return nil, "No such component"
end
local list = {}
for k, v in pairs(components[address].rawproxy) do
if type(v) ~= "function" then
list[k] = true
end
end
return list
end
function api.proxy(address)
if not components[address] then
return nil, "No such component"
end
return components[address].proxy
2016-01-05 04:20:40 +11:00
end
2016-01-14 12:13:00 +11:00
function api.slot() --legacy
return 0
end
2016-01-16 22:42:09 +11:00
function api.type(address)
return components[address].type
end
2016-01-05 04:20:40 +11:00
return component