uv_stop(uv_default_loop());
}
+/** Register module properties in Lua environment */
+static int register_properties(struct engine *engine, struct kr_module *module)
+{
+ lua_newtable(engine->L);
+ if (module->config != NULL) {
+ REGISTER_MODULE_CALL(engine->L, module, module->config, "config");
+ }
+ for (struct kr_prop *p = module->props; p->name; ++p) {
+ if (p->cb != NULL && p->name != NULL) {
+ REGISTER_MODULE_CALL(engine->L, module, p->cb, p->name);
+ }
+ }
+ lua_setglobal(engine->L, module->name);
+
+ /* Register module in Lua env */
+ lua_getglobal(engine->L, "modules_register");
+ lua_getglobal(engine->L, module->name);
+ if (l_sandboxcall(engine->L, 1) != 0) {
+ lua_pop(engine->L, 1);
+ }
+
+ return kr_ok();
+}
+
int engine_register(struct engine *engine, const char *name)
{
if (engine == NULL || name == NULL) {
/* Register properties */
if (module->props) {
- lua_newtable(engine->L);
- if (module->config != NULL) {
- REGISTER_MODULE_CALL(engine->L, module, module->config, "config");
- }
- for (struct kr_prop *p = module->props; p->name; ++p) {
- if (p->cb != NULL && p->name != NULL) {
- REGISTER_MODULE_CALL(engine->L, module, p->cb, p->name);
- }
- }
- lua_setglobal(engine->L, module->name);
+ return register_properties(engine, module);
}
return kr_ok();
end
})
+-- Register module in Lua environment
+function modules_register(module)
+ -- Syntactic sugar for get() and set() properties
+ setmetatable(module, {
+ __index = function (t, k)
+ local v = rawget(t, k)
+ if v then return v
+ elseif t.get then return t.get(k)
+ end
+ end,
+ __newindex = function (t, k, v)
+ local old_v = rawget(t, k)
+ if not old_v and t.set then
+ t.set(k..' '..v)
+ end
+ end
+ })
+end
+
-- Make sandboxed environment
local function make_sandbox(defined)
local __protected = { modules = true, cache = true, net = true }
*Note* |---| this relies on function pointers, so the same ``static inline`` trick as for the ``Layer()`` is required for C/Go.
+Special properties
+------------------
+
+If the module declares properties ``get`` or ``set``, they can be used in the Lua interpreter as
+regular tables.
+
+.. warning: This is not yet completely implemented, as the module I/O format may change to map_t a/o
+ embedded JSON tokenizer.
+
.. _`not present in Go`: http://blog.golang.org/gos-declaration-syntax
.. _CGO: http://golang.org/cmd/cgo/
.. _GCCGO: https://golang.org/doc/install/gccgo