Implement callback registration in Lua instead of C
authorPerttu Ahola <celeron55@gmail.com>
Sun, 27 Nov 2011 23:11:17 +0000 (01:11 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:56 +0000 (19:13 +0200)
data/builtin.lua
src/scriptapi.cpp

index b1abeb364d7903a690e0078f8cc265fd7c991e53..a3276192190013eb1f59ab8cd06e8ed22dee59f6 100644 (file)
@@ -151,11 +151,24 @@ minetest.register_node("ignore", {
 -- Chat message processing
 --
 
-minetest.registered_on_chat_messages = {}
+function make_registration()
+       local t = {}
+       local registerfunc = function(func) table.insert(t, func) end
+       return t, registerfunc
+end
+
+minetest.registered_on_chat_messages, minetest.register_on_chat_message = make_registration()
+minetest.registered_globalsteps, minetest.register_globalstep = make_registration()
+minetest.registered_on_placenodes, minetest.register_on_placenode = make_registration()
+minetest.registered_on_dignodes, minetest.register_on_dignode = make_registration()
+minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
+minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
+minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
+minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
 
 minetest.on_chat_message = function(name, message)
        for i,func in ipairs(minetest.registered_on_chat_messages) do
-               ate = func(name, message)
+               local ate = func(name, message)
                if ate then
                        return true
                end
@@ -163,8 +176,4 @@ minetest.on_chat_message = function(name, message)
        return false
 end
 
-minetest.register_on_chat_message = function(func)
-       table.insert(minetest.registered_on_chat_messages, func)
-end
-
 -- END
index 1872085dcf68a0109121efdd50717f5e4fb8280c..fb1b58f5d76e077477e4f68e2071bf954729e7ed 100644 (file)
@@ -837,79 +837,6 @@ static int l_register_craft(lua_State *L)
        return 0; /* number of results */
 }
 
-static int register_lua_callback(lua_State *L, const char *tablename)
-{
-       luaL_checktype(L, 1, LUA_TFUNCTION);
-
-       lua_getglobal(L, "table");
-       lua_getfield(L, -1, "insert");
-       int table_insert = lua_gettop(L);
-       // Get minetest.registered_globalsteps
-       lua_getglobal(L, "minetest");
-       lua_getfield(L, -1, tablename);
-       luaL_checktype(L, -1, LUA_TTABLE);
-       int registered = lua_gettop(L);
-       // table.insert(registered_globalsteps, func)
-       lua_pushvalue(L, table_insert);
-       lua_pushvalue(L, registered);
-       lua_pushvalue(L, 1); // push function from argument 1
-       // Call insert
-       if(lua_pcall(L, 2, 0, 0))
-               script_error(L, "error: %s\n", lua_tostring(L, -1));
-
-       return 0; /* number of results */
-}
-
-// Register a global step function
-// register_globalstep(function)
-static int l_register_globalstep(lua_State *L)
-{
-       infostream<<"register_globalstep"<<std::endl;
-       return register_lua_callback(L, "registered_globalsteps");
-}
-
-// register_on_placenode(function)
-static int l_register_on_placenode(lua_State *L)
-{
-       infostream<<"register_on_placenode"<<std::endl;
-       return register_lua_callback(L, "registered_on_placenodes");
-}
-
-// register_on_dignode(function)
-static int l_register_on_dignode(lua_State *L)
-{
-       infostream<<"register_on_dignode"<<std::endl;
-       return register_lua_callback(L, "registered_on_dignodes");
-}
-
-// register_on_punchnode(function)
-static int l_register_on_punchnode(lua_State *L)
-{
-       infostream<<"register_on_punchnode"<<std::endl;
-       return register_lua_callback(L, "registered_on_punchnodes");
-}
-
-// register_on_generated(function)
-static int l_register_on_generated(lua_State *L)
-{
-       infostream<<"register_on_generated"<<std::endl;
-       return register_lua_callback(L, "registered_on_generateds");
-}
-
-// register_on_newplayer(function)
-static int l_register_on_newplayer(lua_State *L)
-{
-       infostream<<"register_on_newplayer"<<std::endl;
-       return register_lua_callback(L, "registered_on_newplayers");
-}
-
-// register_on_respawnplayer(function)
-static int l_register_on_respawnplayer(lua_State *L)
-{
-       infostream<<"register_on_respawnplayer"<<std::endl;
-       return register_lua_callback(L, "registered_on_respawnplayers");
-}
-
 // setting_get(name)
 static int l_setting_get(lua_State *L)
 {
@@ -967,13 +894,6 @@ static const struct luaL_Reg minetest_f [] = {
        {"register_tool", l_register_tool},
        {"register_node", l_register_node},
        {"register_craft", l_register_craft},
-       {"register_globalstep", l_register_globalstep},
-       {"register_on_placenode", l_register_on_placenode},
-       {"register_on_dignode", l_register_on_dignode},
-       {"register_on_punchnode", l_register_on_punchnode},
-       {"register_on_generated", l_register_on_generated},
-       {"register_on_newplayer", l_register_on_newplayer},
-       {"register_on_respawnplayer", l_register_on_respawnplayer},
        {"setting_get", l_setting_get},
        {"setting_getbool", l_setting_getbool},
        {"chat_send_all", l_chat_send_all},