int addon_event_process_begin(struct event *evt, void *obj, struct proto_process_stack *stack, unsigned int stack_index) { struct addon_instance_priv *p = obj; // Fetch the instance table lua_State *L = addon_get_instance_and_thread(p); // Stack : self // Fetch the table associated with that event lua_pushlightuserdata(L, evt->reg); // Stack : self, evt_reg lua_gettable(L, -2); // Stack : self, evt_table if (!lua_istable(L, -1)) { pomlog(POMLOG_ERR "Listener not registered for event %s", evt->reg->info->name); return POM_ERR; } // Get the open function lua_getfield(L, -1, "begin"); // Stack : self, evt_table, open_func if (lua_isnil(L, -1)) { lua_pop(L, 3); // Stack : empty return POM_OK; } // Push self lua_pushvalue(L, -3); // Stack : self, evt_table, open_func, self // Push event if (addon_event_push(L, evt) != POM_OK) // Stack : self, evt_table, process_func, self, evt return POM_ERR; int res = addon_pcall(L, 2, 0); // Stack : self, evt_table lua_pop(L, 2); // Stack : empty return res; }
int addon_event_process_end(struct event *evt, void *obj) { struct addon_instance_priv *p = obj; lua_State *L = addon_get_instance_and_thread(p); // Stack : self // Fetch the table associated with that event lua_pushlightuserdata(L, evt->reg); lua_gettable(L, -2); // Stack : self, evt_table if (!lua_istable(L, -1)) { pomlog(POMLOG_ERR "Listener not registered for event %s", evt->reg->info->name); return POM_ERR; } // Get the open function lua_getfield(L, -1, "end"); // Stack : self, evt_table, close_func // Check if there is an end function if (lua_isnil(L, -1)) { lua_pop(L, 3); // Stack : empty return POM_OK; } // Push self lua_pushvalue(L, -3); // Stack : self, evt_table, close_func, self // Push event if (addon_event_push(L, evt) != POM_OK) // Stack : self, evt_table, process_func, self, evt return POM_ERR; int res = addon_pcall(L, 2, 0); // Stack : self, evt_table lua_pop(L, 2); // Stack : empty return res; }
// Called from lua to get the metatable static int addon_pload_metatable(lua_State *L) { const char *key = luaL_checkstring(L, 2); struct analyzer_pload_instance **i = luaL_checkudata(L, 1, ADDON_PLOAD_METATABLE); struct analyzer_pload_buffer *p = analyzer_pload_instance_get_buffer(*i); if (!strcmp(key, "event")) { // Return the corresponding event addon_event_push(L, p->rel_event); } else if (!strcmp(key, "data")) { addon_data_push(L, p->data, p->type->analyzer->data_reg); } else if (!strcmp(key, "type") && p->type) { // Return the type table if any lua_newtable(L); // Add type name lua_pushliteral(L, "name"); lua_pushstring(L, p->type->name); lua_settable(L, -3); // Add type description lua_pushliteral(L, "description"); lua_pushstring(L, p->type->description); lua_settable(L, -3); // Add type extension lua_pushliteral(L, "extension"); lua_pushstring(L, p->type->extension); lua_settable(L, -3); // Add the class lua_pushliteral(L, "class"); switch (p->type->cls) { case analyzer_pload_class_unknown: lua_pushliteral(L, "unknown"); break; case analyzer_pload_class_application: lua_pushliteral(L, "application"); break; case analyzer_pload_class_audio: lua_pushliteral(L, "audio"); break; case analyzer_pload_class_image: lua_pushliteral(L, "image"); break; case analyzer_pload_class_video: lua_pushliteral(L, "video"); break; case analyzer_pload_class_document: lua_pushliteral(L, "document"); break; } lua_settable(L, -3); } else { return 0; } return 1; }