[OpenWrt-Devel] Support for callable metatable for ubus-lua
Anton D. Kachalov
mouse at yandex-team.ru
Wed Jun 29 17:38:40 EDT 2016
Hi.
Who is the right person to address such question? Felix, Luka?
17.06.2016, 18:10, "Anton D. Kachalov" <mouse at yandex-team.ru>:
> 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
> ,
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel at lists.openwrt.org
> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
_______________________________________________
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