[OpenWrt-Devel] Support for callable metatable for ubus-lua
Anton D. Kachalov
mouse at yandex-team.ru
Fri Jun 17 11:09:39 EDT 2016
Hello.
I'm newbie in Lua and tries to expose several objects' methods to ubus. In current implementation only function is allowed as methods, but I need to pass objects to such functions. Here is the snippet:
============
local uci = require "uci"
local ubus = require "ubus"
local uloop = require "uloop"
local x = uci.cursor()
uloop.init()
local conn = ubus.connect()
if not conn then
error("Failed to connect to ubus")
end
local SDRs = { }
for s,e in pairs(x:get_all('SDR')) do
print('-------- '..s..' --------')
local t = e['.type']
local klass = require ("sdr."..t)
local obj = klass:new(e, true)
SDRs['SDR.'..e['.name']] = obj:ubus_methods(conn)
end
conn:add(SDRs)
uloop.run()
=================
The klass method ubus_methods returns:
=================
function _M.ubus_methods(self, conn)
local _g = setmetatable({_o = self, _conn = conn}, {
__call = function(tbl, req, msg)
tbl._conn:reply(req, { rpm = tbl._o:get() })
end})
local _s = setmetatable({_o = self, _conn = conn}, {
__call = function(tbl, req, msg)
tbl._conn:reply(req, { status = tbl._o:set(tonumber(msg['duty'])) })
end})
local _f = setmetatable({_o = self, _conn = conn}, {
__call = function(tbl, req, msg)
tbl._conn:reply(req, { status=tbl._o:fanmap() })
end})
return {
get = { _g, { } },
set = { _s, { duty = ubus.INT32 } },
fanmap = { _f, { } }
}
end
=================
So, I wrote a little patch to add additional check for "__call" metamethod to make it possible.
BTW. There is no code to check signatures (policy) of methods for Lua code.
--
Anton D. Kachalov
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 01-ubus-allow-table-as-method.patch
Type: text/x-diff
Size: 1167 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20160617/f83757d9/attachment.bin>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list