Przeglądaj źródła

add lua chunk for counting lua obj

mikymod 12 lat temu
rodzic
commit
5437cff4e2
1 zmienionych plików z 23 dodań i 3 usunięć
  1. 23 3
      engine/lua/LuaEnvironment.cpp

+ 23 - 3
engine/lua/LuaEnvironment.cpp

@@ -69,6 +69,8 @@ void LuaEnvironment::init()
 	execute(0, 0);
 	execute(0, 0);
 	load_buffer(tmp_print_table, string::strlen(tmp_print_table));
 	load_buffer(tmp_print_table, string::strlen(tmp_print_table));
 	execute(0, 0);
 	execute(0, 0);
+	load_buffer(count_all, string::strlen(count_all));
+	execute(0, 0);
 
 
 	m_is_used = true;
 	m_is_used = true;
 }
 }
@@ -248,7 +250,7 @@ CE_EXPORT int32_t luaopen_libcrown(lua_State* L)
 	return 1;
 	return 1;
 }
 }
 
 
-const char* LuaEnvironment::class_system = "function class(klass, super) "
+const char* LuaEnvironment::class_system =  "function class(klass, super) "
     										"	if not klass then "
     										"	if not klass then "
         									"		klass = {} "
         									"		klass = {} "
                 							"		local meta = {} "
                 							"		local meta = {} "
@@ -301,6 +303,24 @@ const char* LuaEnvironment::tmp_print_table =	"function print_table(table) "
 												"	for k,v in pairs(table) do "
 												"	for k,v in pairs(table) do "
 												"		print(v) "
 												"		print(v) "
 												"	end "
 												"	end "
-												"end"
-;
+												"end";
+
+const char* LuaEnvironment::count_all = 	"function count_all(f) "
+											"	local seen = {} "
+											"	local count_table "
+											"	count_table = function(t) "
+											"		if seen[t] then return end "
+											"		f(t) "
+											"		seen[t] = true "
+											"		for k,v in pairs(t) do "
+											"			if type(v) == 'table' then "
+											"				count_table(v) "
+											"			elseif type(v) == 'userdata' then "
+											"				f(v) "
+											"			end "
+											"		end "
+											"	end "
+											"	count_table(_G) "
+											"end";
+
 } // namespace crown
 } // namespace crown