int install_sexe_userdata(sexe_t *S, char *tag) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; int is_new; k = shkey_str(tag); sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); memcpy(&S->pname, k, sizeof(S->pname)); shkey_free(&k); buff = shbuf_init(); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); is_new = shfs_read(fl, buff); udata = shjson_init(shbuf_size(buff) ? (char *)shbuf_data(buff) : NULL); shbuf_free(&buff); if (is_new) shjson_num_add(udata, "birth", shtimef(shtime())); sexe_table_set(S, udata); lua_setglobal(S, "userdata"); shjson_free(&udata); shfs_free(&fs); return (0); }
/** * Call a lua pre-stored global Lua function with one argument. */ int sexe_event_call(lua_State *L, const char *f_name, int e_type, shjson_t *json) { lua_getglobal(L, f_name); /* push global func ref to stack */ lua_pushnumber(L, e_type); if (json) sexe_table_set(L, json); return (lua_pcall(L, json ? 2 : 1, 0, 0)); }
int sexe_exec_pcall(sexe_t *S, char *func, shjson_t *json) { int err; lua_getglobal(S, func); /* push global func ref to stack */ if (!json) { err = lua_pcall(S, 0, 0, 0); } else { sexe_table_set(S, json); err = lua_pcall(S, 1, 0, 0); } if (err) return (err); return (0); }
/** * The "os.trigger(<string>[,<table>])" function will trigger all callbacks for the specifid event name to be called with an optional object argument. */ int lfunc_trigger_event(sexe_t *L) { const char *e_name; shjson_t *json; int t_reg = 0; int ret_bool; int err; /* first argument: event name */ e_name = (int)luaL_checkstring(L, 1); /* second optional arg; table of data. */ json = NULL; if (lua_istable(L, 2)) { lua_pushvalue(L, 2); json = sexe_table_get(L); } ret_bool = 1; err = sexe_event_handle(L, e_name, json); if (err) ret_bool = 0; #if 0 /* iterate through registered functions for event */ { shkey_t *key = sexe_event_key(e_name); char *e_hex = shkey_hex(key); lua_getglobal(L, EVENT_ENV); if (lua_isnil(L, -1)) { return (0); /* error */ } lua_getfield(L, -1, e_hex); if (lua_isnil(L, -1)) { return (0); /* error */ } lua_pushnil(L); while (lua_next(L, -2)) { int t = lua_type(L, -1); if (t == LUA_TFUNCTION) { /* copy function call onto stack lua_pushvalue(L, 1); */ /* 1. user args */ if (json) sexe_table_set(L, json); else lua_pushnil(L); /* 2. event name */ lua_pushstring(L, e_name); /* exec */ lua_pcall(L, 2, 0, 0); } else { lua_pop(L, 1); /* value */ } //lua_pop(L, 1); /* key */ } } #endif if (json) shjson_free(&json); /* return single boolean */ lua_pushboolean(L, ret_bool); return (1); }
int sexe_exec_pset(sexe_t *S, char *name, shjson_t *arg) { sexe_table_set(S, arg); lua_setglobal(S, name); }
static void _api_push_json(lua_State *L, shjson_t *arg) { sexe_table_set(L, arg); lua_setglobal(L, "arg"); }