int luaopen_justenoughicu (lua_State *L) { lua_newtable(L); luaL_setfuncs(L, lib_table, 0); return 1; }
// Ctor, initialization lua_kernel_base::lua_kernel_base(CVideo * video) : mState(luaL_newstate()) , video_(video) , cmd_log_() { get_lua_kernel_base_ptr(mState) = this; lua_State *L = mState; cmd_log_ << "Initializing " << my_name() << "...\n"; // Open safe libraries. // Debug and OS are not, but most of their functions will be disabled below. cmd_log_ << "Adding standard libs...\n"; static const luaL_Reg safe_libs[] = { { "", luaopen_base }, { "table", luaopen_table }, { "string", luaopen_string }, { "math", luaopen_math }, { "coroutine", luaopen_coroutine }, { "debug", luaopen_debug }, { "os", luaopen_os }, { "bit32", luaopen_bit32 }, // added in Lua 5.2 { NULL, NULL } }; for (luaL_Reg const *lib = safe_libs; lib->func; ++lib) { luaL_requiref(L, lib->name, lib->func, 1); lua_pop(L, 1); /* remove lib */ } // Disable functions from os which we don't want. lua_getglobal(L, "os"); lua_pushnil(L); while(lua_next(L, -2) != 0) { lua_pop(L, 1); char const* function = lua_tostring(L, -1); if(strcmp(function, "clock") == 0 || strcmp(function, "date") == 0 || strcmp(function, "time") == 0 || strcmp(function, "difftime") == 0) continue; lua_pushnil(L); lua_setfield(L, -3, function); } lua_pop(L, 1); // Disable functions from debug which we don't want. lua_getglobal(L, "debug"); lua_pushnil(L); while(lua_next(L, -2) != 0) { lua_pop(L, 1); char const* function = lua_tostring(L, -1); if(strcmp(function, "traceback") == 0 || strcmp(function, "getinfo") == 0) continue; //traceback is needed for our error handler lua_pushnil(L); //getinfo is needed for ilua strict mode lua_setfield(L, -3, function); } lua_pop(L, 1); // Delete dofile and loadfile. lua_pushnil(L); lua_setglobal(L, "dofile"); lua_pushnil(L); lua_setglobal(L, "loadfile"); // Store the error handler. cmd_log_ << "Adding error handler...\n"; lua_pushlightuserdata(L , executeKey); lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); lua_remove(L, -2); lua_rawset(L, LUA_REGISTRYINDEX); lua_pop(L, 1); // Create the gettext metatable. cmd_log_ << lua_common::register_gettext_metatable(L); // Create the tstring metatable. cmd_log_ << lua_common::register_tstring_metatable(L); lua_settop(L, 0); // Define the CPP_function metatable ( so we can override print to point to a C++ member function, add "show_dialog" for this kernel, etc. ) cmd_log_ << "Adding boost function proxy...\n"; lua_cpp::register_metatable(L); // Add some callback from the wesnoth lib cmd_log_ << "Registering basic wesnoth API...\n"; static luaL_Reg const callbacks[] = { { "compare_versions", &intf_compare_versions }, { "have_file", &lua_fileops::intf_have_file }, { "textdomain", &lua_common::intf_textdomain }, { "tovconfig", &lua_common::intf_tovconfig }, { "get_dialog_value", &lua_gui2::intf_get_dialog_value }, { "set_dialog_active", &lua_gui2::intf_set_dialog_active }, { "set_dialog_visible", &lua_gui2::intf_set_dialog_visible }, { "add_dialog_tree_node", &lua_gui2::intf_add_dialog_tree_node }, { "set_dialog_callback", &lua_gui2::intf_set_dialog_callback }, { "set_dialog_canvas", &lua_gui2::intf_set_dialog_canvas }, { "set_dialog_focus", &lua_gui2::intf_set_dialog_focus }, { "set_dialog_markup", &lua_gui2::intf_set_dialog_markup }, { "set_dialog_value", &lua_gui2::intf_set_dialog_value }, { "remove_dialog_item", &lua_gui2::intf_remove_dialog_item }, { "dofile", &dispatch<&lua_kernel_base::intf_dofile> }, { "require", &dispatch<&lua_kernel_base::intf_require> }, { "show_dialog", &dispatch<&lua_kernel_base::intf_show_dialog> }, { "show_message_dialog", &dispatch<&lua_kernel_base::intf_show_message_dialog> }, { "show_popup_dialog", &dispatch<&lua_kernel_base::intf_show_popup_dialog> }, { "show_lua_console", &dispatch<&lua_kernel_base::intf_show_lua_console> }, { NULL, NULL } }; lua_cpp::Reg const cpp_callbacks[] = { /* { "dofile", boost::bind(&lua_kernel_base::intf_dofile, this, _1)}, { "require", boost::bind(&lua_kernel_base::intf_require, this, _1)}, { "show_dialog", boost::bind(&lua_kernel_base::intf_show_dialog, this, _1)}, { "show_lua_console", boost::bind(&lua_kernel_base::intf_show_lua_console, this, _1)}, */ { NULL, NULL } }; lua_getglobal(L, "wesnoth"); if (!lua_istable(L,-1)) { lua_newtable(L); } luaL_setfuncs(L, callbacks, 0); lua_cpp::set_functions(L, cpp_callbacks, 0); lua_setglobal(L, "wesnoth"); // Override the print function cmd_log_ << "Redirecting print function...\n"; lua_getglobal(L, "print"); lua_setglobal(L, "std_print"); //storing original impl as 'std_print' lua_settop(L, 0); //clear stack, just to be sure lua_pushcfunction(L, &dispatch<&lua_kernel_base::intf_print>); lua_setglobal(L, "print"); // Create the package table. lua_getglobal(L, "wesnoth"); lua_newtable(L); lua_setfield(L, -2, "package"); lua_pop(L, 1); // Get some callbacks for map locations cmd_log_ << "Adding map_location table...\n"; static luaL_Reg const map_callbacks[] = { { "get_direction", &lua_map_location::intf_get_direction }, { "vector_sum", &lua_map_location::intf_vector_sum }, { "vector_negation", &lua_map_location::intf_vector_negation }, { "zero", &lua_map_location::intf_vector_zero }, { "rotate_right_around_center", &lua_map_location::intf_rotate_right_around_center }, { "tiles_adjacent", &lua_map_location::intf_tiles_adjacent }, { "get_adjacent_tiles", &lua_map_location::intf_get_adjacent_tiles }, { "distance_between", &lua_map_location::intf_distance_between }, { "get_in_basis_N_NE", &lua_map_location::intf_get_in_basis_N_NE }, { "get_relative_dir", &lua_map_location::intf_get_relative_dir }, { "parse_direction", &lua_map_location::intf_parse_direction }, { "write_direction", &lua_map_location::intf_write_direction }, { NULL, NULL } }; // Create the map_location table. lua_getglobal(L, "wesnoth"); lua_newtable(L); luaL_setfuncs(L, map_callbacks, 0); lua_setfield(L, -2, "map_location"); lua_pop(L,1); // Add mersenne twister rng wrapper cmd_log_ << "Adding rng tables...\n"; lua_rng::load_tables(L); // Loading ilua: cmd_log_ << "Loading ilua...\n"; lua_pushstring(L, "lua/ilua.lua"); int result = intf_require(L); if (result == 1) { //run "ilua.set_strict()" lua_pushstring(L, "set_strict"); lua_gettable(L, -2); if (!protected_call(0,0, boost::bind(&lua_kernel_base::log_error, this, _1, _2))) { cmd_log_ << "Failed to activate strict mode.\n"; } else { cmd_log_ << "Activated strict mode.\n"; } lua_setglobal(L, "ilua"); //save ilua table as a global } else { cmd_log_ << "Error: failed to load ilua.\n"; } lua_settop(L, 0); }
/* * Returns: processors_load_average (number), is_per_cpu (boolean) */ static int sys_loadavg (lua_State *L) { double loadavg; #ifndef _WIN32 const int res = 0; if (getloadavg(&loadavg, 1) == 1) { const int is_per_cpu = 1; #else const int res = getloadavg(&loadavg); if (!res) { const int is_per_cpu = 0; #endif lua_pushnumber(L, (lua_Number) loadavg); lua_pushboolean(L, is_per_cpu); return 2; } return sys_seterror(L, res); } /* * Arguments: [number_of_files (number)] * Returns: number_of_files (number) */ static int sys_limit_nfiles (lua_State *L) { #ifndef _WIN32 const int narg = lua_gettop(L); struct rlimit rlim; rlim.rlim_max = 0; getrlimit(RLIMIT_NOFILE, &rlim); lua_pushinteger(L, rlim.rlim_max); if (narg != 0) { const int n = lua_tointeger(L, 1); rlim.rlim_cur = rlim.rlim_max = n; if (setrlimit(RLIMIT_NOFILE, &rlim)) return sys_seterror(L, 0); } return 1; #else (void) L; lua_pushinteger(L, -1); return 1; #endif } /* * Arguments: string * Returns: number */ static int sys_toint (lua_State *L) { const char *s = lua_tostring(L, 1); int num = 0, sign = 1; if (s) { switch (*s) { case '-': sign = -1; case '+': ++s; } while (*s >= '0' && *s <= '9') num = (num << 3) + (num << 1) + (*s++ & ~'0'); } lua_pushinteger(L, sign * num); return 1; } /* * Arguments: error_handler (function), function, any ... * Returns: status (boolean), any ... */ static int sys_xpcall (lua_State *L) { const int status = lua_pcall(L, lua_gettop(L) - 2, LUA_MULTRET, 1); lua_pushboolean(L, !status); lua_insert(L, 2); return lua_gettop(L) - 1; } #include "isa/fcgi/sys_fcgi.c" #include "mem/sys_mem.c" #include "thread/sys_thread.c" #ifndef _WIN32 #include "sys_unix.c" #else #include "win32/sys_win32.c" #endif #include "sys_file.c" #include "sys_date.c" #include "sys_env.c" #include "sys_evq.c" #include "sys_fs.c" #include "sys_log.c" #include "sys_proc.c" #include "sys_rand.c" static luaL_Reg sys_lib[] = { {"strerror", sys_strerror}, {"nprocs", sys_nprocs}, {"loadavg", sys_loadavg}, {"limit_nfiles", sys_limit_nfiles}, {"toint", sys_toint}, {"xpcall", sys_xpcall}, DATE_METHODS, ENV_METHODS, EVQ_METHODS, FCGI_METHODS, FD_METHODS, FS_METHODS, LOG_METHODS, PROC_METHODS, RAND_METHODS, #ifndef _WIN32 UNIX_METHODS, #endif {NULL, NULL} }; /* * Arguments: ..., sys_lib (table) */ static void createmeta (lua_State *L) { const int top = lua_gettop(L); const struct meta_s { const char *tname; luaL_Reg *meth; int is_index; } meta[] = { {DIR_TYPENAME, dir_meth, 0}, {EVQ_TYPENAME, evq_meth, 1}, {FD_TYPENAME, fd_meth, 1}, {PERIOD_TYPENAME, period_meth, 1}, {PID_TYPENAME, pid_meth, 1}, {LOG_TYPENAME, log_meth, 0}, {RAND_TYPENAME, rand_meth, 0}, }; int i; for (i = 0; i < (int) (sizeof(meta) / sizeof(struct meta_s)); ++i) { luaL_newmetatable(L, meta[i].tname); if (meta[i].is_index) { lua_pushvalue(L, -1); /* push metatable */ lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ } luaL_setfuncs(L, meta[i].meth, 0); lua_pop(L, 1); } /* Predefined file handles */ luaL_getmetatable(L, FD_TYPENAME); { const char *std[] = {"stdin", "stdout", "stderr"}; for (i = 3; i--; ) { #ifndef _WIN32 const fd_t fd = i; #else const fd_t fd = GetStdHandle(i == 0 ? STD_INPUT_HANDLE : (i == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE)); #endif lua_pushstring(L, std[i]); lua_boxinteger(L, fd); lua_pushvalue(L, -3); /* metatable */ lua_pushboolean(L, 1); lua_rawseti(L, -2, (int) ((lua_Integer) fd)); /* don't close std. handles */ lua_setmetatable(L, -2); lua_rawset(L, top); } } lua_settop(L, top); } LUALIB_API int luaopen_sys (lua_State *L) { luaL_register(L, LUA_SYSLIBNAME, sys_lib); createmeta(L); signal_init(); #ifdef _WIN32 luaopen_sys_win32(L); #endif luaopen_sys_mem(L); luaopen_sys_thread(L); return 1; }
static int _newmetatable(lua_State* L, const char* mod, const luaL_Reg* funcs, int func_count) { luaL_newmetatable(L, mod); luaL_setfuncs(L, funcs, 0); return 0; }
int luaopen_tek_lib_display_x11(lua_State *L) { TAPTR exec; TEKDisplay *display; /* require "tek.lib.exec": */ lua_getglobal(L, "require"); /* s: "require" */ lua_pushliteral(L, "tek.lib.exec"); /* s: "require", "tek.lib.exec" */ lua_call(L, 1, 1); /* s: exectab */ lua_getfield(L, -1, "base"); /* s: exectab, execbase */ exec = *(TAPTR *) lua_touserdata(L, -1); /* register functions: */ #if LUA_VERSION_NUM < 502 luaL_register(L, "tek.lib.display.x11", tek_lib_display_x11_funcs); #else luaL_newlib(L, tek_lib_display_x11_funcs); #endif /* s: exectab, execbase, libtab */ lua_pushvalue(L, -1); /* s: exectab, execbase, libtab, libtab */ lua_insert(L, -4); /* s: libtab, exectab, execbase, libtab */ /* create userdata: */ display = lua_newuserdata(L, sizeof(TEKDisplay)); /* s: exectab, execbase, libtab, libbase */ display->Base = TNULL; display->ExecBase = exec; display->IsBase = TTRUE; /* register base: */ lua_pushvalue(L, -1); /* s: exectab, execbase, libtab, libbase, libbase */ lua_setfield(L, LUA_REGISTRYINDEX, TEK_LIB_DISPLAY_X11_BASECLASSNAME); /* s: exectab, execbase, libtab, libbase */ /* create metatable for userdata, register methods: */ luaL_newmetatable(L, TEK_LIB_DISPLAY_X11_CLASSNAME); /* s: exectab, execbase, libtab, libbase, libmeta */ lua_pushvalue(L, -1); /* s: exectab, execbase, libtab, libbase, libmeta, libmeta */ lua_setfield(L, -2, "__index"); /* s: exectab, execbase, libtab, libbase, libmeta */ #if LUA_VERSION_NUM < 502 luaL_register(L, NULL, tek_lib_display_x11_methods); #else luaL_setfuncs(L, tek_lib_display_x11_methods, 0); #endif /* s: exectab, execbase, libtab, libbase, libmeta */ lua_setmetatable(L, -2); /* s: exectab, execbase, libtab, libbase */ /* place exec reference in metatable: */ lua_getmetatable(L, -1); /* s: exectab, execbase, libtab, libbase, libmeta */ lua_pushvalue(L, -4); /* s: exectab, execbase, libtab, libbase, libmeta, execbase */ luaL_ref(L, -2); /* index returned is always 1 */ /* s: exectab, execbase, libtab, libbase, libmeta */ lua_pop(L, 5); /* Add visual module to TEKlib's internal module list: */ TExecAddModules(exec, (struct TModInitNode *) &im_display, 0); return 1; }
int luaopen_myecho(lua_State *L) { lua_newtable(L); luaL_setfuncs(L, Functions, 0); return 1; }
void luaMic_init(lua_State *L) { lua_newtable(L); if (csndAccess) luaL_setfuncs(L, Mic_CSND_functions, 0); else luaL_setfuncs(L, Mic_DSP_functions, 0); lua_setglobal(L, "Mic"); }
void moonglut_open_cursor(lua_State *L) { luaL_setfuncs(L, Functions, 0); }
static int luaopen_hexchat(lua_State *L) { lua_newtable(L); luaL_setfuncs(L, api_hexchat, 0); lua_pushinteger(L, HEXCHAT_PRI_HIGHEST); lua_setfield(L, -2, "PRI_HIGHEST"); lua_pushinteger(L, HEXCHAT_PRI_HIGH); lua_setfield(L, -2, "PRI_HIGH"); lua_pushinteger(L, HEXCHAT_PRI_NORM); lua_setfield(L, -2, "PRI_NORM"); lua_pushinteger(L, HEXCHAT_PRI_LOW); lua_setfield(L, -2, "PRI_LOW"); lua_pushinteger(L, HEXCHAT_PRI_LOWEST); lua_setfield(L, -2, "PRI_LOWEST"); lua_pushinteger(L, HEXCHAT_EAT_NONE); lua_setfield(L, -2, "EAT_NONE"); lua_pushinteger(L, HEXCHAT_EAT_HEXCHAT); lua_setfield(L, -2, "EAT_HEXCHAT"); lua_pushinteger(L, HEXCHAT_EAT_PLUGIN); lua_setfield(L, -2, "EAT_PLUGIN"); lua_pushinteger(L, HEXCHAT_EAT_ALL); lua_setfield(L, -2, "EAT_ALL"); lua_newtable(L); lua_newtable(L); luaL_setfuncs(L, api_hexchat_prefs_meta, 0); lua_setmetatable(L, -2); lua_setfield(L, -2, "prefs"); lua_newtable(L); lua_newtable(L); luaL_setfuncs(L, api_hexchat_props_meta, 0); lua_setmetatable(L, -2); lua_setfield(L, -2, "props"); lua_newtable(L); lua_newtable(L); luaL_setfuncs(L, api_hexchat_pluginprefs_meta, 0); lua_setmetatable(L, -2); lua_setfield(L, -2, "pluginprefs"); luaL_newmetatable(L, "hook"); lua_newtable(L); luaL_setfuncs(L, api_hook_meta_index, 0); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, "context"); lua_newtable(L); lua_pushcfunction(L, api_hexchat_set_context); lua_setfield(L, -2, "set"); wrap_context(L, "find_context", api_hexchat_find_context); wrap_context(L, "print", api_hexchat_print); wrap_context(L, "emit_print", api_hexchat_emit_print); wrap_context(L, "emit_print_attrs", api_hexchat_emit_print_attrs); wrap_context(L, "command", api_hexchat_command); wrap_context(L, "nickcmp", api_hexchat_nickcmp); wrap_context(L, "get_info", api_hexchat_get_info); wrap_context(L, "iterate", api_hexchat_iterate); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, api_hexchat_context_meta_eq); lua_setfield(L, -2, "__eq"); lua_pop(L, 1); luaL_newmetatable(L, "attrs"); luaL_setfuncs(L, api_attrs_meta, 0); lua_pop(L, 1); luaL_newmetatable(L, "list"); luaL_setfuncs(L, api_list_meta, 0); lua_pop(L, 1); return 1; }
void State::loadContentBindings(Console::Context& context) { lua_pushglobaltable(mImpl); luaL_setfuncs(mImpl, Content::lua_functions, 0); Content::set_context(mImpl, context); }
LUA_API int luaopen_openssl(lua_State*L) { char * config_filename; CRYPTO_thread_setup(); OpenSSL_add_all_ciphers(); OpenSSL_add_all_digests(); SSL_library_init(); ERR_load_ERR_strings(); ERR_load_crypto_strings(); ERR_load_EVP_strings(); ERR_load_SSL_strings(); ENGINE_load_dynamic(); ENGINE_load_openssl(); /* Determine default SSL configuration file */ config_filename = getenv("OPENSSL_CONF"); if (config_filename == NULL) { config_filename = getenv("SSLEAY_CONF"); } /* default to 'openssl.cnf' if no environment variable is set */ if (config_filename == NULL) { snprintf(default_ssl_conf_filename, sizeof(default_ssl_conf_filename), "%s/%s", X509_get_default_cert_area(), "openssl.cnf"); } else { strncpy(default_ssl_conf_filename, config_filename, sizeof(default_ssl_conf_filename)); } openssl_register_pkey(L); openssl_register_x509(L); openssl_register_csr(L); openssl_register_digest(L); openssl_register_cipher(L); openssl_register_sk_x509(L); openssl_register_bio(L); openssl_register_crl(L); #ifdef OPENSSL_HAVE_TS openssl_register_ts(L); #endif openssl_register_conf(L); openssl_register_pkcs7(L); openssl_register_misc(L); openssl_register_engine(L); openssl_register_ssl(L); openssl_register_ocsp(L); #if LUA_VERSION_NUM==501 luaL_register(L,"openssl",eay_functions); #elif LUA_VERSION_NUM==502 lua_newtable(L); luaL_setfuncs(L, eay_functions, 0); #endif setNamedIntegers(L, consts); /* third part */ luaopen_bn(L); lua_setfield(L, -2, "bn"); return 1; }
void RegLuaPacket(lua_State *L) { luaL_Reg packet_mt[] = { {"__gc", destroy_luapacket}, {NULL, NULL} }; luaL_Reg rpacket_methods[] = { {"ReadU8", ReadUint8}, {"ReadU16", ReadUint16}, {"ReadU32", ReadUint32}, {"ReadI8", ReadInt8}, {"ReadI16", ReadInt16}, {"ReadI32", ReadInt32}, {"ReadNum", ReadDouble}, {"ReadStr", ReadString}, {"ReadTable", ReadTable}, {NULL, NULL} }; luaL_Reg wpacket_methods[] = { {"WriteU8", WriteUint8}, {"WriteU16",WriteUint16}, {"WriteU32",WriteUint32}, {"WriteNum",WriteDouble}, {"WriteStr",WriteString}, {"WriteTable",WriteTable}, {"RewriteU8",RewriteUint8}, {"RewriteU16",RewriteUint16}, {"RewriteU32",RewriteUint32}, {"RewriteNum",RewriteDouble}, {"GetWritePos",GetWritePos}, {NULL, NULL} }; luaL_Reg rawpacket_methods[] = { {"ReadBinary", ReadBinary}, {NULL, NULL} }; luaL_Reg cmdrpacket_methods[] = { {"ReadU8", ReadUint8}, {"ReadU16", ReadUint16}, {"ReadU32", ReadUint32}, {"ReadI8", ReadInt8}, {"ReadI16", ReadInt16}, {"ReadI32", ReadInt32}, {"ReadNum", ReadDouble}, {"ReadStr", ReadString}, {"ReadTable", ReadTable}, {"ReadCmd",ReadCmd}, {NULL, NULL} }; luaL_Reg cmdwpacket_methods[] = { {"WriteU8", WriteUint8}, {"WriteU16",WriteUint16}, {"WriteU32",WriteUint32}, {"WriteNum",WriteDouble}, {"WriteStr",WriteString}, {"WriteTable",WriteTable}, {"RewriteU8",RewriteUint8}, {"RewriteU16",RewriteUint16}, {"RewriteU32",RewriteUint32}, {"RewriteNum",RewriteDouble}, {"WriteCmd",WriteCmd}, {"GetWritePos",GetWritePos}, {NULL, NULL} }; luaL_Reg httppacket_methods[] = { {"GetUrl", GetUrl}, {"GetStatus",GetStatus}, {"GetBody",GetBody}, {"GetHeaders",GetHeaders}, {"GetMethod",GetMethod}, {NULL, NULL} }; luaL_newmetatable(L, LUARPACKET_METATABLE); luaL_setfuncs(L, packet_mt, 0); luaL_newlib(L, rpacket_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, LUAWPACKET_METATABLE); luaL_setfuncs(L, packet_mt, 0); luaL_newlib(L, wpacket_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, LUARAWPACKET_METATABLE); luaL_setfuncs(L, packet_mt, 0); luaL_newlib(L, rawpacket_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, LUACMDRPACKET_METATABLE); luaL_setfuncs(L, packet_mt, 0); luaL_newlib(L, cmdrpacket_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, LUACMDWPACKET_METATABLE); luaL_setfuncs(L, packet_mt, 0); luaL_newlib(L, cmdwpacket_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, LUAHTTPPACKET_METATABLE); luaL_setfuncs(L, packet_mt, 0); luaL_newlib(L, httppacket_methods); lua_setfield(L, -2, "__index"); lua_pop(L, 1); SET_FUNCTION(L,"NewWPacket",NewWPacket); SET_FUNCTION(L,"NewRPacket",NewRPacket); SET_FUNCTION(L,"NewCmdWPacket",NewCmdWPacket); SET_FUNCTION(L,"NewCmdRPacket",NewCmdRPacket); SET_FUNCTION(L,"NewRawPacket",NewRawPacket); }
int luaopen_marshal(lua_State *L) { lua_newtable(L); luaL_setfuncs(L, R, 0); return 1; }
int luaopen_testlib_profile(lua_State *L) { // #ifdef DEBUG_LOG // printf("%s\n", __FUNCTION__); // #endif luaL_checkversion(L); luaL_Reg l[] = { { "start", lstart }, { "stop", lstop }, { "resume", lresume }, { "yield", lyield }, { "resume_co", lresume_co }, { "yield_co", lyield_co }, { NULL, NULL }, }; luaL_newlibtable(L,l); lua_newtable(L); // table thread->start time lua_newtable(L); // table thread->total time lua_newtable(L); // weak table lua_pushliteral(L, "kv"); // 同lua_pushstring,复制一个string lua_setfield(L, -2, "__mode"); // -2指向的weak table,key:__mode, value:栈顶, weak_table[__mode] = kv, pop出value lua_pushvalue(L, -1); // 复制一个weak table引用在栈顶 lua_setmetatable(L, -3); // pop出上面复制的weak table,设置为total time table的metatable lua_setmetatable(L, -3); // pop出第一个weak table,设置为start time table的metatable lua_pushnil(L); // cfunction (coroutine.resume or coroutine.yield), 留出位置给下面设置lua自己的协程函数 luaL_setfuncs(L,l,3); // 所有函数共享3个upvalues,这里调用完毕会pop出上面三个value int libtable = lua_gettop(L); // libtable的index栈位置 lua_getglobal(L, "coroutine"); lua_getfield(L, -1, "resume"); // coroutine table里的resume key对应的value压顶 lua_CFunction co_resume = lua_tocfunction(L, -1); // 取出lua自己的resume的c函数 if (co_resume == NULL) return luaL_error(L, "Can't get coroutine.resume"); lua_pop(L,1); lua_getfield(L, libtable, "resume"); // libtable里的resume key对应的函数压顶 lua_pushcfunction(L, co_resume); // lua自己的resume函数压顶 lua_setupvalue(L, -2, 3); // -2指向的闭包,该闭包的第3个upvalue,设置为栈顶的lua自己的resume函数,pop出栈顶 lua_pop(L,1); // pop出resume闭包 lua_getfield(L, libtable, "resume_co"); lua_pushcfunction(L, co_resume); lua_setupvalue(L, -2, 3); lua_pop(L,1); lua_getfield(L, -1, "yield"); lua_CFunction co_yield = lua_tocfunction(L, -1); // 取出lua自己的yield c函数 if (co_yield == NULL) return luaL_error(L, "Can't get coroutine.yield"); lua_pop(L,1); lua_getfield(L, libtable, "yield"); lua_pushcfunction(L, co_yield); lua_setupvalue(L, -2, 3); lua_pop(L,1); lua_getfield(L, libtable, "yield_co"); lua_pushcfunction(L, co_yield); lua_setupvalue(L, -2, 3); lua_pop(L,1); lua_settop(L, libtable); return 1; }
void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l){ if(libname) lua_newtable(L); luaL_setfuncs(L, l, 0); }
void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction, lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath) { ScriptController* sc = Game::getInstance()->getScriptController(); // If the type is an inner type, get the correct parent // table on the stack before creating the table for the class. if (!scopePath.empty()) { std::string tablename = name; // Strip off the scope path part of the name. lua_getglobal(sc->_lua, scopePath[0].c_str()); std::size_t index = tablename.find(scopePath[0]); if (index != std::string::npos) tablename = tablename.substr(index + scopePath[0].size()); for (unsigned int i = 1; i < scopePath.size(); i++) { lua_pushstring(sc->_lua, scopePath[i].c_str()); lua_gettable(sc->_lua, -2); index = tablename.find(scopePath[i]); if (index != std::string::npos) tablename = tablename.substr(index + scopePath[i].size()); } lua_pushstring(sc->_lua, tablename.c_str()); lua_newtable(sc->_lua); } else { // If the type is not an inner type, set it as a global table. lua_newtable(sc->_lua); lua_pushvalue(sc->_lua, -1); lua_setglobal(sc->_lua, name); } // Create the metatable and populate it with the member functions. lua_pushliteral(sc->_lua, "__metatable"); luaL_newmetatable(sc->_lua, name); if (members) luaL_setfuncs(sc->_lua, members, 0); lua_pushstring(sc->_lua, "__index"); lua_pushvalue(sc->_lua, -2); lua_settable(sc->_lua, -3); // Add the delete function if it was specified. if (deleteFunction) { lua_pushstring(sc->_lua, "__gc"); lua_pushcfunction(sc->_lua, deleteFunction); lua_settable(sc->_lua, -3); } // Set the metatable on the main table. lua_settable(sc->_lua, -3); // Populate the main table with the static functions. if (statics) luaL_setfuncs(sc->_lua, statics, 0); // Set the new function(s) for the class. if (newFunction) { lua_pushliteral(sc->_lua, "new"); lua_pushcfunction(sc->_lua, newFunction); lua_settable(sc->_lua, -3); } // Set the table we just created within the correct parent table. if (!scopePath.empty()) { lua_settable(sc->_lua, -3); // Pop all the parent tables off the stack. int size = (int)scopePath.size(); lua_pop(sc->_lua, size); } else { // Pop the main table off the stack. lua_pop(sc->_lua, 1); } }
void moonglmath_open_tracing(lua_State *L) { luaL_setfuncs(L, Functions, 0); }
void moongl_open_perfragment(lua_State *L) { luaL_setfuncs(L, Functions, 0); }