bool LuaInstance::NewMetatable(const String& str) { return luaL_newmetatable(m_state, str.GetConstBuffer()) != 0; }
DLL_EXPORT int luaopen_libluazmq(lua_State *L) { /* context metatable. */ luaL_newmetatable(L, MT_ZMQ_CONTEXT); luaL_register(L, NULL, ctxmethods); lua_pushvalue(L, -1); lua_setfield(L, -1, "__index"); /* socket metatable. */ luaL_newmetatable(L, MT_ZMQ_SOCKET); luaL_register(L, NULL, sockmethods); lua_pushvalue(L, -1); lua_setfield(L, -1, "__index"); luaL_register(L, "zmq", zmqlib); /* Socket types. */ set_zmq_const(PAIR); set_zmq_const(PUB); set_zmq_const(SUB); set_zmq_const(REQ); set_zmq_const(REP); set_zmq_const(XREQ); set_zmq_const(XREP); set_zmq_const(PULL); set_zmq_const(PUSH); /* Socket options. */ set_zmq_const(HWM); set_zmq_const(SWAP); set_zmq_const(AFFINITY); set_zmq_const(IDENTITY); set_zmq_const(SUBSCRIBE); set_zmq_const(UNSUBSCRIBE); set_zmq_const(RATE); set_zmq_const(RECOVERY_IVL); set_zmq_const(MCAST_LOOP); set_zmq_const(SNDBUF); set_zmq_const(RCVBUF); set_zmq_const(RCVMORE); #if VERSION_2_1 set_zmq_const(FD); set_zmq_const(EVENTS); set_zmq_const(TYPE); set_zmq_const(LINGER); set_zmq_const(RECONNECT_IVL); set_zmq_const(BACKLOG); /* POLL events */ set_zmq_const(POLLIN); set_zmq_const(POLLOUT); set_zmq_const(POLLERR); #endif /* Send/recv options. */ set_zmq_const(NOBLOCK); set_zmq_const(SNDMORE); Lzmq_CharInit(L); Lzmq_ByteInit(L); Lzmq_ShortInit(L); Lzmq_IntInit(L); Lzmq_LongInit(L); Lzmq_FloatInit(L); Lzmq_DoubleInit(L); return 1; }
//使用原始方式注册函数 int luaopen_array(lua_State *L) { luaL_newmetatable(L, "LuaBook.array"); luaL_openlib(L, "array", arraylib, 0); return 1; }
bool LuaInstance::NewMetatable(const char* str) { return luaL_newmetatable(m_state, str) != 0; }
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); }
LUALIB_API int luaopen_zlib(lua_State *L) { const luaL_reg lzstream_meta[] = { {"write", lzstream_compress }, {"read", lzstream_decompress }, {"lines", lzstream_lines }, {"flush", lzstream_flush }, {"close", lzstream_close }, {"adler", lzstream_adler }, {"__tostring", lzstream_tostring }, {"__gc", lzstream_gc }, {NULL, NULL} }; const luaL_reg zlib[] = { {"version", lzlib_version }, {"adler32", lzlib_adler32 }, {"crc32", lzlib_crc32 }, {"deflate", lzlib_deflate }, {"inflate", lzlib_inflate }, {"compress", lzlib_compress }, {"decompress", lzlib_decompress }, {NULL, NULL} }; /* ====================================================================== */ /* create new metatable for zlib compression structures */ luaL_newmetatable(L, ZSTREAMMETA); lua_pushliteral(L, "__index"); lua_pushvalue(L, -2); /* push metatable */ lua_rawset(L, -3); /* metatable.__index = metatable */ /* ** Stack: metatable */ luaL_register(L, NULL, lzstream_meta); lua_pop(L, 1); /* remove metatable from stack */ /* ** Stack: */ lua_newtable(L); lua_pushliteral (L, "_COPYRIGHT"); lua_pushliteral (L, "Copyright (C) 2003-2008 Tiago Dionizio"); lua_settable (L, -3); lua_pushliteral (L, "_DESCRIPTION"); lua_pushliteral (L, "Lua 5 interface to access zlib library functions"); lua_settable (L, -3); lua_pushliteral (L, "_VERSION"); lua_pushliteral (L, "lzlib 0.4-work2"); lua_settable (L, -3); luaL_register(L, NULL, zlib); /* ** Stack: zlib table */ return 1; }
int main(int argc, char **argv) { int ch; const char *scriptpath = "/tmp/test.lua"; log_init(-1); log_debug("debug: filter-lua: args: %s", argv[1]); while ((ch = getopt(argc, argv, "")) != -1) { switch (ch) { default: log_warnx("warn: filter-lua: bad option"); return (1); /* NOTREACHED */ } } argc -= optind; argv += optind; log_debug("debug: filter-lua: starting..."); if ((L = luaL_newstate()) == NULL) { log_warnx("warn: filter-lua: can't create lua state"); return (1); } luaL_openlibs(L); luaL_newlib(L, l_filter); luaL_newmetatable(L, "filter"); lua_setmetatable(L, -2); lua_pushnumber(L, FILTER_OK); lua_setfield(L, -2, "FILTER_OK"); lua_pushnumber(L, FILTER_FAIL); lua_setfield(L, -2, "FILTER_FAIL"); lua_pushnumber(L, FILTER_CLOSE); lua_setfield(L, -2, "FILTER_CLOSE"); lua_setglobal(L, "filter"); if (luaL_loadfile(L, scriptpath) != 0) { log_warnx("warn: filter-lua: error loading script: %s", scriptpath); return (1); } if (lua_pcall(L, 0, 0, 0)) { log_warnx("warn: filter-lua: error running script: %s", scriptpath); return (1); } lua_getglobal(L, "on_connect"); if (lua_isfunction(L, 1)) { log_debug("debug: filter-lua: on_connect is present"); filter_api_on_connect(on_connect); } lua_getglobal(L, "on_helo"); if (lua_isfunction(L, 1)) { log_debug("debug: filter-lua: on_helo is present"); filter_api_on_helo(on_helo); } lua_getglobal(L, "on_mail"); if (lua_isfunction(L, 1)) { log_debug("debug: filter-lua: on_mail is present"); filter_api_on_mail(on_mail); } lua_getglobal(L, "on_rcpt"); if (lua_isfunction(L, 1)) { log_debug("debug: filter-lua: on_rcpt is present"); filter_api_on_rcpt(on_rcpt); } lua_getglobal(L, "on_data"); if (lua_isfunction(L, 1)) { log_debug("debug: filter-lua: on_data is present"); filter_api_on_data(on_data); } lua_getglobal(L, "on_eom"); if (lua_isfunction(L, 1)) { log_debug("debug: filter-lua: on_eom is present"); filter_api_on_eom(on_eom); } filter_api_loop(); log_debug("debug: filter-lua: exiting"); return (0); }
int luaopen_ui(lua_State *L) { static const struct luaL_Reg font_lib[] = { { NULL, NULL } }; static const struct luaL_Reg image_lib[] = { { "Draw", lua_image_draw }, { "Height", lua_image_height }, { "Width", lua_image_width }, { NULL, NULL } }; static const struct luaL_Reg ui_lib[] = { { "DrawString", lua_ui_draw_string }, { "FillRectangle", lua_ui_fill_rectangle }, { "Font", lua_ui_font }, { "Height", lua_ui_height }, { "HideProgress", lua_ui_hide_progress }, { "Image", lua_ui_image }, { "SetColor", lua_ui_set_color }, { "SetFont", lua_ui_set_font }, { "ShowProgress", lua_ui_show_progress }, { "Width", lua_ui_width }, { NULL, NULL } }; state = L; /* Create the "ui" library */ luaL_newlib(L, ui_lib); /* Add constants to the UI object */ lua_pushnumber(L, UI_TEXT_LEFT); lua_setfield(L, -2, "TEXT_LEFT"); lua_pushnumber(L, UI_TEXT_CENTER); lua_setfield(L, -2, "TEXT_CENTER"); lua_pushnumber(L, UI_TEXT_RIGHT); lua_setfield(L, -2, "TEXT_RIGHT"); /* Setup meta data for the "Font" object */ luaL_newmetatable(L, FontTypeName); luaL_newlib(L, font_lib); lua_setfield(L, -2, "__index"); /* GC callback for the "Font" object */ lua_pushstring(L, "__gc"); lua_pushcfunction(L, lua_font_gc); lua_settable(L, -3); /* Remove the Font library object from the stack */ lua_pop(L, 1); /* Setup meta data for the "Image" object */ luaL_newmetatable(L, ImageTypeName); luaL_newlib(L, image_lib); lua_setfield(L, -2, "__index"); /* GC callback for the "Image" object */ lua_pushstring(L, "__gc"); lua_pushcfunction(L, lua_image_gc); lua_settable(L, -3); /* Remove the Image library object from the stack */ lua_pop(L, 1); return 1; }
static int vlclua_sd_add_item( lua_State *L ) { services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L ); if( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "path" ); if( lua_isstring( L, -1 ) ) { const char *psz_path = lua_tostring( L, -1 ); lua_getfield( L, -2, "title" ); const char *psz_title = luaL_checkstring( L, -1 ) ? luaL_checkstring( L, -1 ) : psz_path; /* The table must be at the top of the stack when calling * vlclua_read_options() */ char **ppsz_options = NULL; int i_options = 0; lua_pushvalue( L, -3 ); vlclua_read_options( p_sd, L, &i_options, &ppsz_options ); input_item_t *p_input = input_item_NewExt( psz_path, psz_title, i_options, (const char **)ppsz_options, VLC_INPUT_OPTION_TRUSTED, -1 ); lua_pop( L, 3 ); if( p_input ) { vlclua_read_meta_data( p_sd, L, p_input ); /* This one is to be tested... */ vlclua_read_custom_meta_data( p_sd, L, p_input ); /* The duration is given in seconds, convert to microseconds */ lua_getfield( L, -1, "duration" ); if( lua_isnumber( L, -1 ) ) input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) ); else if( !lua_isnil( L, -1 ) ) msg_Warn( p_sd, "Item duration should be a number (in seconds)." ); lua_pop( L, 1 ); lua_getfield( L, -1, "category" ); if( lua_isstring( L, -1 ) ) services_discovery_AddItem( p_sd, p_input, luaL_checkstring( L, -1 ) ); else services_discovery_AddItem( p_sd, p_input, NULL ); lua_pop( L, 1 ); /* string to build the input item uid */ lua_getfield( L, -1, "uiddata" ); if( lua_isstring( L, -1 ) ) { char *s = strdup( luaL_checkstring( L, -1 ) ); if ( s ) { struct md5_s md5; InitMD5( &md5 ); AddMD5( &md5, s, strlen( s ) ); EndMD5( &md5 ); free( s ); s = psz_md5_hash( &md5 ); if ( s ) input_item_AddInfo( p_input, "uid", "md5", "%s", s ); free( s ); } } lua_pop( L, 1 ); input_item_t **udata = (input_item_t **) lua_newuserdata( L, sizeof( input_item_t * ) ); *udata = p_input; if( luaL_newmetatable( L, "input_item_t" ) ) { lua_newtable( L ); luaL_register( L, NULL, vlclua_item_reg ); lua_setfield( L, -2, "__index" ); lua_pushliteral( L, "none of your business" ); lua_setfield( L, -2, "__metatable" ); } lua_setmetatable( L, -2 ); vlc_gc_decref( p_input ); } while( i_options > 0 ) free( ppsz_options[--i_options] ); free( ppsz_options ); } else msg_Err( p_sd, "vlc.sd.add_item: the \"path\" parameter can't be empty" ); } else msg_Err( p_sd, "Error parsing add_item arguments" ); return 1; }
int luaopen_cgroup(lua_State *L) { /* create metatable */ luaL_newmetatable(L, CGROUP_META); /* metatable.__index = metatable */ lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); /* register module */ luaL_register(L, "cgroups", R); /* register metatable as socket_meta */ lua_pushvalue(L, -2); lua_setfield(L, -2, "meta_cgroups"); /* module version */ // PUSH_INT(version, VERSION) PUSH_INT(CGFLAG_DELETE_IGNORE_MIGRATION, CGFLAG_DELETE_IGNORE_MIGRATION) PUSH_INT(CGFLAG_DELETE_RECURSIVE, CGFLAG_DELETE_RECURSIVE) // error ints PUSH_INT(ECGMOUNTNAMESPACE, ECGMOUNTNAMESPACE) PUSH_INT(ECGNAMESPACECONTROLLER, ECGNAMESPACECONTROLLER) PUSH_INT(ECGNAMESPACEPATHS, ECGNAMESPACEPATHS) PUSH_INT(ECGCONFIGPARSEFAIL, ECGCONFIGPARSEFAIL) PUSH_INT(ECGEOF, ECGEOF) PUSH_INT(ECGSENTINEL, ECGSENTINEL) PUSH_INT(ECGMOUNTFAIL, ECGMOUNTFAIL) PUSH_INT(ECGROUPNORULES, ECGROUPNORULES) PUSH_INT(ECGROUPPARSEFAIL, ECGROUPPARSEFAIL) PUSH_INT(ECGCONTROLLERNOTEQUAL, ECGCONTROLLERNOTEQUAL) PUSH_INT(ECGROUPNOTEQUAL, ECGROUPNOTEQUAL) PUSH_INT(ECGOTHER, ECGOTHER) PUSH_INT(ECGROUPVALUENOTEXIST, ECGROUPVALUENOTEXIST) PUSH_INT(ECGROUPNOTINITIALIZED, ECGROUPNOTINITIALIZED) PUSH_INT(ECGFAIL, ECGFAIL) PUSH_INT(ECGCONTROLLERCREATEFAILED, ECGCONTROLLERCREATEFAILED) PUSH_INT(ECGINVAL, ECGINVAL) PUSH_INT(ECGVALUEEXISTS, ECGVALUEEXISTS) PUSH_INT(ECGCONTROLLEREXISTS, ECGCONTROLLEREXISTS) PUSH_INT(ECGMAXVALUESEXCEEDED, ECGMAXVALUESEXCEEDED) PUSH_INT(ECGROUPNOTALLOWED, ECGROUPNOTALLOWED) PUSH_INT(ECGROUPMULTIMOUNTED, ECGROUPMULTIMOUNTED) PUSH_INT(ECGROUPNOTOWNER, ECGROUPNOTOWNER) PUSH_INT(ECGROUPSUBSYSNOTMOUNTED, ECGROUPSUBSYSNOTMOUNTED) PUSH_INT(ECGROUPNOTCREATED, ECGROUPNOTCREATED) PUSH_INT(ECGROUPNOTEXIST, ECGROUPNOTEXIST) PUSH_INT(ECGROUPNOTMOUNTED, ECGROUPNOTMOUNTED) PUSH_INT(ECGROUPNOTCOMPILED, ECGROUPNOTCOMPILED) /* remove meta table */ lua_remove(L, -2); // map cgroup luaL_register(L, CGROUP, cgroup_methods); luaL_newmetatable(L, CGROUP); luaL_register(L, NULL, cgroup_meta); lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); lua_rawset(L, -3); /* metatable.__index = methods */ lua_pushliteral(L, "__metatable"); lua_pushvalue(L, -3); /* dup methods table*/ lua_rawset(L, -3); lua_pop(L, 1); // map cgroup_controller luaL_register(L, CGROUP_CONTROLLER, cgroup_controller_methods); luaL_newmetatable(L, CGROUP_CONTROLLER); luaL_register(L, NULL, cgroup_controller_meta); lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); lua_rawset(L, -3); /* metatable.__index = methods */ lua_pushliteral(L, "__metatable"); lua_pushvalue(L, -3); /* dup methods table*/ lua_rawset(L, -3); lua_pop(L, 1); 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 { nullptr, nullptr } }; 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"; push_error_handler(L); // 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 }, { "read_file", &lua_fileops::intf_read_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", &video_dispatch<lua_gui2::show_dialog> }, { "show_menu", &video_dispatch<lua_gui2::show_menu> }, { "show_message_dialog", &video_dispatch<lua_gui2::show_message_dialog> }, { "show_popup_dialog", &video_dispatch<lua_gui2::show_popup_dialog> }, { "show_lua_console", &dispatch<&lua_kernel_base::intf_show_lua_console> }, { "compile_formula", &lua_formula_bridge::intf_compile_formula}, { "eval_formula", &lua_formula_bridge::intf_eval_formula}, { "name_generator", &intf_name_generator }, { nullptr, nullptr } }; 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 }, { nullptr, nullptr } }; // 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); cmd_log_ << "Adding name generator metatable...\n"; luaL_newmetatable(L, Gen); static luaL_Reg const generator[] = { { "__call", &impl_name_generator_call}, { "__gc", &impl_name_generator_collect}, { nullptr, nullptr} }; luaL_setfuncs(L, generator, 0); // Create formula bridge metatables cmd_log_ << lua_formula_bridge::register_metatables(L); // Loading ilua: cmd_log_ << "Loading ilua...\n"; lua_settop(L, 0); 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, std::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); }
////////////////////////////// // // Now push all these functions into the Lua state! // // int LUA_InfoLib(lua_State *L) { // index of A_Lua actions to run for each state lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, LREG_STATEACTION); // index of globally available Lua actions by function name lua_newtable(L); lua_setfield(L, LUA_REGISTRYINDEX, LREG_ACTIONS); luaL_newmetatable(L, META_STATE); lua_pushcfunction(L, state_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, state_set); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, state_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_MOBJINFO); lua_pushcfunction(L, mobjinfo_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, mobjinfo_set); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, mobjinfo_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_SFXINFO); lua_pushcfunction(L, sfxinfo_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, sfxinfo_set); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, sfxinfo_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSprname); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_sprnamelen); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "sprnames"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getState); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_setState); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, lib_statelen); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "states"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getMobjInfo); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_setMobjInfo); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, lib_mobjinfolen); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "mobjinfo"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSfxInfo); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_setSfxInfo); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, lib_sfxlen); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_pushvalue(L, -1); lua_setglobal(L, "S_sfx"); lua_setglobal(L, "sfxinfo"); return 0; }
static void createmeta (lua_State *L) { luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ lua_pushvalue(L, -1); /* push metatable */ lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ luaL_register(L, NULL, flib); /* file methods */ }
int LUA_MapLib(lua_State *L) { luaL_newmetatable(L, META_SECTORLINES); lua_pushcfunction(L, sectorlines_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, sectorlines_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_SECTOR); lua_pushcfunction(L, sector_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, sector_set); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, sector_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_SUBSECTOR); lua_pushcfunction(L, subsector_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, subsector_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_LINE); lua_pushcfunction(L, line_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, line_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_SIDENUM); lua_pushcfunction(L, sidenum_get); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, META_SIDE); lua_pushcfunction(L, side_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, side_set); lua_setfield(L, -2, "__newindex"); lua_pushcfunction(L, side_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_VERTEX); lua_pushcfunction(L, vertex_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, vertex_num); lua_setfield(L, -2, "__len"); lua_pop(L, 1); luaL_newmetatable(L, META_FFLOOR); lua_pushcfunction(L, ffloor_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, ffloor_set); lua_setfield(L, -2, "__newindex"); lua_pop(L, 1); luaL_newmetatable(L, META_MAPHEADER); lua_pushcfunction(L, mapheaderinfo_get); lua_setfield(L, -2, "__index"); //lua_pushcfunction(L, mapheaderinfo_num); //lua_setfield(L, -2, "__len"); lua_pop(L, 1); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSector); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_numsectors); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "sectors"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSubsector); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_numsubsectors); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "subsectors"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getLine); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_numlines); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "lines"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getSide); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_numsides); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "sides"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getVertex); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_numvertexes); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "vertexes"); lua_newuserdata(L, 0); lua_createtable(L, 0, 2); lua_pushcfunction(L, lib_getMapheaderinfo); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lib_nummapheaders); lua_setfield(L, -2, "__len"); lua_setmetatable(L, -2); lua_setglobal(L, "mapheaderinfo"); 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; }
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_lunum(lua_State *L) { lua_settop(L, 0); // start with an empty stack // Create the 'array' metatable // --------------------------------------------------------------------------- luaL_newmetatable(L, "array"); LUA_NEW_METAMETHOD(L, array, tostring); LUA_NEW_METAMETHOD(L, array, call); LUA_NEW_METAMETHOD(L, array, index); LUA_NEW_METAMETHOD(L, array, newindex); LUA_NEW_METAMETHOD(L, array, add); LUA_NEW_METAMETHOD(L, array, sub); LUA_NEW_METAMETHOD(L, array, mul); LUA_NEW_METAMETHOD(L, array, div); LUA_NEW_METAMETHOD(L, array, pow); LUA_NEW_METAMETHOD(L, array, unm); LUA_NEW_METAMETHOD(L, array, gc); lua_pop(L, 1); // Create the 'complex' metatable // --------------------------------------------------------------------------- luaL_newmetatable(L, "complex"); LUA_NEW_METAMETHOD(L, complex, tostring); LUA_NEW_METAMETHOD(L, complex, add); LUA_NEW_METAMETHOD(L, complex, sub); LUA_NEW_METAMETHOD(L, complex, mul); LUA_NEW_METAMETHOD(L, complex, div); LUA_NEW_METAMETHOD(L, complex, pow); LUA_NEW_METAMETHOD(L, complex, unm); LUA_NEW_METAMETHOD(L, complex, eq); LUA_NEW_METAMETHOD(L, complex, lt); LUA_NEW_METAMETHOD(L, complex, le); lua_pop(L, 1); // Create the 'lunum' table // --------------------------------------------------------------------------- lua_newtable(L); LUA_NEW_MODULEMETHOD(L, lunum, array); LUA_NEW_MODULEMETHOD(L, lunum, zeros); LUA_NEW_MODULEMETHOD(L, lunum, range); LUA_NEW_MODULEMETHOD(L, lunum, resize); LUA_NEW_MODULEMETHOD(L, lunum, sin); LUA_NEW_MODULEMETHOD(L, lunum, cos); LUA_NEW_MODULEMETHOD(L, lunum, tan); LUA_NEW_MODULEMETHOD(L, lunum, asin); LUA_NEW_MODULEMETHOD(L, lunum, acos); LUA_NEW_MODULEMETHOD(L, lunum, atan); LUA_NEW_MODULEMETHOD(L, lunum, sinh); LUA_NEW_MODULEMETHOD(L, lunum, cosh); LUA_NEW_MODULEMETHOD(L, lunum, tanh); LUA_NEW_MODULEMETHOD(L, lunum, asinh); LUA_NEW_MODULEMETHOD(L, lunum, acosh); LUA_NEW_MODULEMETHOD(L, lunum, atanh); LUA_NEW_MODULEMETHOD(L, lunum, exp); LUA_NEW_MODULEMETHOD(L, lunum, log); LUA_NEW_MODULEMETHOD(L, lunum, log10); LUA_NEW_MODULEMETHOD(L, lunum, conjugate); LUA_NEW_MODULEMETHOD(L, lunum, loadtxt); LUA_NEW_MODULEMETHOD(L, lunum, fromfile); LUA_NEW_MODULEMETHOD(L, lunum, slice); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_BOOL , bool); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_CHAR , char); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_SHORT , short); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_INT , int); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_LONG , long); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_FLOAT , float); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_DOUBLE , double); LUA_NEW_MODULEDATA(L, ARRAY_TYPE_COMPLEX, complex); // Register the purely imaginary number 'I' lunum_pushcomplex(L, I); lua_setfield(L, 1, "I"); lua_setglobal(L, "lunum"); #include "array_class.lc" // sets the lunum.__array_methods table lua_getglobal(L, "lunum"); return 1; }
static int bindAll(lua_State* L) { Application* application = static_cast<Application*>(lua_touserdata(L, 1)); lua_pop(L, 1); StackChecker checker(L, "bindAll", 0); setEnvironTable(L); { lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_touches); lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_eventClosures); lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_events); luaL_newweaktable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_b2); lua_newtable(L); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_timers); } static const luaL_Reg functionList[] = { {"isInstanceOf", instance_of}, {"getClass", get_class}, {"getBaseClass", get_base}, {NULL, NULL}, }; luaL_newmetatable(L, "Object"); luaL_register(L, NULL, functionList); lua_setglobal(L, "Object"); EventBinder eventBinder(L); EventDispatcherBinder eventDispatcherBinder(L); TimerBinder timerBinder(L); MatrixBinder matrixBinder(L); SpriteBinder spriteBinder(L); TextureBaseBinder textureBaseBinder(L); TextureBinder textureBinder(L); TexturePackBinder texturePackBinder(L); BitmapDataBinder bitmapDataBinder(L); BitmapBinder bitmapBinder(L); StageBinder stageBinder(L, application); FontBaseBinder fontBaseBinder(L); FontBinder fontBinder(L); TTFontBinder ttfontBinder(L); TextFieldBinder textFieldBinder(L); #ifndef TARGET_OS_TV AccelerometerBinder accelerometerBinder(L); #endif Box2DBinder2 box2DBinder2(L); DibBinder dibBinder(L); TileMapBinder tileMapBinder(L); ApplicationBinder applicationBinder(L); ShapeBinder shapeBinder(L); MovieClipBinder movieClipBinder(L); UrlLoaderBinder urlLoaderBinder(L); #ifndef TARGET_OS_TV GeolocationBinder geolocationBinder(L); GyroscopeBinder gyroscopeBinder(L); #endif AlertDialogBinder alertDialogBinder(L); TextInputDialogBinder textInputDialogBinder(L); MeshBinder meshBinder(L); AudioBinder audioBinder(L); RenderTargetBinder renderTargetBinder(L); ShaderBinder shaderBinder(L); Path2DBinder path2DBinder(L); PluginManager& pluginManager = PluginManager::instance(); for (size_t i = 0; i < pluginManager.plugins.size(); ++i) pluginManager.plugins[i].main(L, 0); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_Event); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_EnterFrameEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_MouseEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_TouchEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_TimerEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_KeyboardEvent); lua_getglobal(L, "Event"); lua_getfield(L, -1, "new"); lua_pushlightuserdata(L, NULL); lua_call(L, 1, 1); lua_remove(L, -2); luaL_rawsetptr(L, LUA_REGISTRYINDEX, &key_CompleteEvent); #include "property.c.in" #include "texturepack.c.in" #include "sprite.c.in" #include "compatibility.c.in" lua_newtable(L); lua_pushinteger(L, GINPUT_KEY_BACK); lua_setfield(L, -2, "BACK"); lua_pushinteger(L, GINPUT_KEY_SEARCH); lua_setfield(L, -2, "SEARCH"); lua_pushinteger(L, GINPUT_KEY_MENU); lua_setfield(L, -2, "MENU"); lua_pushinteger(L, GINPUT_KEY_CENTER); lua_setfield(L, -2, "CENTER"); lua_pushinteger(L, GINPUT_KEY_SELECT); lua_setfield(L, -2, "SELECT"); lua_pushinteger(L, GINPUT_KEY_START); lua_setfield(L, -2, "START"); lua_pushinteger(L, GINPUT_KEY_L1); lua_setfield(L, -2, "L1"); lua_pushinteger(L, GINPUT_KEY_R1); lua_setfield(L, -2, "R1"); lua_pushinteger(L, GINPUT_KEY_LEFT); lua_setfield(L, -2, "LEFT"); lua_pushinteger(L, GINPUT_KEY_UP); lua_setfield(L, -2, "UP"); lua_pushinteger(L, GINPUT_KEY_RIGHT); lua_setfield(L, -2, "RIGHT"); lua_pushinteger(L, GINPUT_KEY_DOWN); lua_setfield(L, -2, "DOWN"); lua_pushinteger(L, GINPUT_KEY_A); lua_setfield(L, -2, "A"); lua_pushinteger(L, GINPUT_KEY_B); lua_setfield(L, -2, "B"); lua_pushinteger(L, GINPUT_KEY_C); lua_setfield(L, -2, "C"); lua_pushinteger(L, GINPUT_KEY_D); lua_setfield(L, -2, "D"); lua_pushinteger(L, GINPUT_KEY_E); lua_setfield(L, -2, "E"); lua_pushinteger(L, GINPUT_KEY_F); lua_setfield(L, -2, "F"); lua_pushinteger(L, GINPUT_KEY_G); lua_setfield(L, -2, "G"); lua_pushinteger(L, GINPUT_KEY_H); lua_setfield(L, -2, "H"); lua_pushinteger(L, GINPUT_KEY_I); lua_setfield(L, -2, "I"); lua_pushinteger(L, GINPUT_KEY_J); lua_setfield(L, -2, "J"); lua_pushinteger(L, GINPUT_KEY_K); lua_setfield(L, -2, "K"); lua_pushinteger(L, GINPUT_KEY_L); lua_setfield(L, -2, "L"); lua_pushinteger(L, GINPUT_KEY_M); lua_setfield(L, -2, "M"); lua_pushinteger(L, GINPUT_KEY_N); lua_setfield(L, -2, "N"); lua_pushinteger(L, GINPUT_KEY_O); lua_setfield(L, -2, "O"); lua_pushinteger(L, GINPUT_KEY_P); lua_setfield(L, -2, "P"); lua_pushinteger(L, GINPUT_KEY_Q); lua_setfield(L, -2, "Q"); lua_pushinteger(L, GINPUT_KEY_R); lua_setfield(L, -2, "R"); lua_pushinteger(L, GINPUT_KEY_S); lua_setfield(L, -2, "S"); lua_pushinteger(L, GINPUT_KEY_T); lua_setfield(L, -2, "T"); lua_pushinteger(L, GINPUT_KEY_U); lua_setfield(L, -2, "U"); lua_pushinteger(L, GINPUT_KEY_V); lua_setfield(L, -2, "V"); lua_pushinteger(L, GINPUT_KEY_W); lua_setfield(L, -2, "W"); lua_pushinteger(L, GINPUT_KEY_X); lua_setfield(L, -2, "X"); lua_pushinteger(L, GINPUT_KEY_Y); lua_setfield(L, -2, "Y"); lua_pushinteger(L, GINPUT_KEY_Z); lua_setfield(L, -2, "Z"); lua_pushinteger(L, GINPUT_KEY_0); lua_setfield(L, -2, "NUM_0"); lua_pushinteger(L, GINPUT_KEY_1); lua_setfield(L, -2, "NUM_1"); lua_pushinteger(L, GINPUT_KEY_2); lua_setfield(L, -2, "NUM_2"); lua_pushinteger(L, GINPUT_KEY_3); lua_setfield(L, -2, "NUM_3"); lua_pushinteger(L, GINPUT_KEY_4); lua_setfield(L, -2, "NUM_4"); lua_pushinteger(L, GINPUT_KEY_5); lua_setfield(L, -2, "NUM_5"); lua_pushinteger(L, GINPUT_KEY_6); lua_setfield(L, -2, "NUM_6"); lua_pushinteger(L, GINPUT_KEY_7); lua_setfield(L, -2, "NUM_7"); lua_pushinteger(L, GINPUT_KEY_8); lua_setfield(L, -2, "NUM_8"); lua_pushinteger(L, GINPUT_KEY_9); lua_setfield(L, -2, "NUM_9"); lua_pushinteger(L, GINPUT_KEY_SHIFT); lua_setfield(L, -2, "SHIFT"); lua_pushinteger(L, GINPUT_KEY_SPACE); lua_setfield(L, -2, "SPACE"); lua_pushinteger(L, GINPUT_KEY_BACKSPACE); lua_setfield(L, -2, "BACKSPACE"); lua_pushinteger(L, GINPUT_KEY_CTRL); lua_setfield(L, -2, "CTRL"); lua_pushinteger(L, GINPUT_KEY_ALT); lua_setfield(L, -2, "ALT"); lua_pushinteger(L, GINPUT_KEY_ESC); lua_setfield(L, -2, "ESC"); lua_pushinteger(L, GINPUT_KEY_TAB); lua_setfield(L, -2, "TAB"); lua_pushinteger(L, GINPUT_NO_BUTTON); lua_setfield(L, -2, "MOUSE_NONE"); lua_pushinteger(L, GINPUT_LEFT_BUTTON); lua_setfield(L, -2, "MOUSE_LEFT"); lua_pushinteger(L, GINPUT_RIGHT_BUTTON); lua_setfield(L, -2, "MOUSE_RIGHT"); lua_pushinteger(L, GINPUT_MIDDLE_BUTTON); lua_setfield(L, -2, "MOUSE_MIDDLE"); lua_setglobal(L, "KeyCode"); // correct clock function which is wrong in iphone lua_getglobal(L, "os"); lua_pushcfunction(L, os_timer); lua_setfield(L, -2, "timer"); lua_pop(L, 1); // register collectgarbagelater // lua_pushcfunction(L, ::collectgarbagelater); // lua_setglobal(L, "collectgarbagelater"); registerModules(L); return 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 register_zlib(lua_State *L) { const luaL_Reg lzstream_meta[] = { {"write", lzstream_compress }, {"read", lzstream_decompress }, {"lines", lzstream_lines }, {"flush", lzstream_flush }, {"close", lzstream_close }, {"adler", lzstream_adler }, {"__tostring", lzstream_tostring }, {"__gc", lzstream_gc }, {NULL, NULL} }; const luaL_Reg zlib[] = { {"version", lzlib_version }, {"adler32", lzlib_adler32 }, {"crc32", lzlib_crc32 }, {"deflate", lzlib_deflate }, {"inflate", lzlib_inflate }, {"compress", lzlib_compress }, {"decompress", lzlib_decompress }, {NULL, NULL} }; /* ====================================================================== */ /* create new metatable for zlib compression structures */ luaL_newmetatable(L, ZSTREAMMETA); lua_pushliteral(L, "__index"); lua_pushvalue(L, -2); /* push metatable */ lua_rawset(L, -3); /* metatable.__index = metatable */ /* ** Stack: metatable */ luaL_register(L, NULL, lzstream_meta); lua_pop(L, 1); /* remove metatable from stack */ /* ** Stack: */ lua_newtable(L); lua_pushliteral (L, "_COPYRIGHT"); lua_pushliteral (L, "Copyright (C) 2003-2010 Tiago Dionizio"); lua_settable (L, -3); lua_pushliteral (L, "_DESCRIPTION"); lua_pushliteral (L, "Lua 5 interface to access zlib library functions"); lua_settable (L, -3); lua_pushliteral (L, "_VERSION"); lua_pushliteral (L, "lzlib 0.4-work3"); lua_settable (L, -3); #define PUSH_LITERAL(name) \ lua_pushliteral (L, #name); \ lua_pushinteger (L, Z_##name); \ lua_settable (L, -3); #define PUSH_NUMBER(name, value) \ lua_pushliteral (L, #name); \ lua_pushinteger (L, value); \ lua_settable (L, -3); PUSH_LITERAL(NO_COMPRESSION) PUSH_LITERAL(BEST_SPEED) PUSH_LITERAL(BEST_COMPRESSION) PUSH_LITERAL(DEFAULT_COMPRESSION) PUSH_LITERAL(FILTERED) PUSH_LITERAL(HUFFMAN_ONLY) PUSH_LITERAL(RLE) PUSH_LITERAL(FIXED) PUSH_LITERAL(DEFAULT_STRATEGY) PUSH_NUMBER(MINIMUM_MEMLEVEL, 1) PUSH_NUMBER(MAXIMUM_MEMLEVEL, 9) PUSH_NUMBER(DEFAULT_MEMLEVEL, 8) PUSH_NUMBER(DEFAULT_WINDOWBITS, 15) PUSH_NUMBER(MINIMUM_WINDOWBITS, 8) PUSH_NUMBER(MAXIMUM_WINDOWBITS, 15) PUSH_NUMBER(GZIP_WINDOWBITS, 16) PUSH_NUMBER(RAW_WINDOWBITS, -1) luaL_register(L, NULL, zlib); lua_setglobal(L, "zlib"); }
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; }
static int vlclua_node_add_subitem( lua_State *L ) { services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L ); input_item_t **pp_node = (input_item_t **)luaL_checkudata( L, 1, "node" ); if( *pp_node ) { if( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "path" ); if( lua_isstring( L, -1 ) ) { const char *psz_path = lua_tostring( L, -1 ); /* The table must be at the top of the stack when calling * vlclua_read_options() */ char **ppsz_options = NULL; int i_options = 0; lua_pushvalue( L, -2 ); vlclua_read_options( p_sd, L, &i_options, &ppsz_options ); input_item_t *p_input = input_item_NewExt( psz_path, psz_path, i_options, (const char **)ppsz_options, VLC_INPUT_OPTION_TRUSTED, -1 ); lua_pop( L, 2 ); if( p_input ) { input_item_node_t *p_input_node = input_item_node_Create( *pp_node ); vlclua_read_meta_data( p_sd, L, p_input ); /* This one is to be tested... */ vlclua_read_custom_meta_data( p_sd, L, p_input ); lua_getfield( L, -1, "duration" ); if( lua_isnumber( L, -1 ) ) input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) ); else if( !lua_isnil( L, -1 ) ) msg_Warn( p_sd, "Item duration should be a number (in seconds)." ); lua_pop( L, 1 ); input_item_node_AppendItem( p_input_node, p_input ); input_item_node_PostAndDelete( p_input_node ); input_item_t **udata = (input_item_t **) lua_newuserdata( L, sizeof( input_item_t * ) ); *udata = p_input; if( luaL_newmetatable( L, "input_item_t" ) ) { lua_newtable( L ); luaL_register( L, NULL, vlclua_item_reg ); lua_setfield( L, -2, "__index" ); lua_pushliteral( L, "none of your business" ); lua_setfield( L, -2, "__metatable" ); } lua_setmetatable( L, -2 ); vlc_gc_decref( p_input ); } while( i_options > 0 ) free( ppsz_options[--i_options] ); free( ppsz_options ); } else msg_Err( p_sd, "node:add_subitem: the \"path\" parameter can't be empty" ); } else msg_Err( p_sd, "Error parsing add_subitem arguments" ); } return 1; }
int luaopen_coevent(lua_State *L) { luaopen_uthread(L); LM = L; _loop_fd = -1; swop_top = malloc(sizeof(cosocket_swop_t)); swop_top->next = NULL; pid = getpid(); update_time(); SSL_library_init(); OpenSSL_add_all_algorithms(); lua_pushlightuserdata(L, NULL); lua_setglobal(L, "null"); lua_register(L, "startloop", lua_f_startloop); lua_register(L, "swop", lua_f_coroutine_swop); lua_register(L, "sleep", lua_f_sleep); lua_register(L, "md5", lua_f_md5); lua_register(L, "sha1bin", lua_f_sha1bin); lua_register(L, "hmac_sha1", lua_f_hmac_sha1); lua_register(L, "base64_encode", lua_f_base64_encode); lua_register(L, "base64_decode", lua_f_base64_decode); lua_register(L, "base64_encode_url", lua_f_base64_encode_url); lua_register(L, "base64_decode_url", lua_f_base64_decode_url); lua_register(L, "escape", cosocket_lua_f_escape); lua_register(L, "escape_uri", lua_f_escape_uri); lua_register(L, "unescape_uri", lua_f_unescape_uri); lua_register(L, "time", lua_f_time); lua_register(L, "longtime", lua_f_longtime); lua_register(L, "build_dns_query_packet", lua_co_build_dns_query_packet); lua_register(L, "parse_dns_result", lua_co_parse_dns_result); lua_register(L, "LOG", lua_f_log); lua_register(L, "open_log", lua_f_open_log); luaopen_eio(L); luaL_loadstring(L, " \ DEBUG,INFO,NOTICE,WARN,ALERT,ERR = 1,2,3,4,5,6 \ coroutine_resume=coroutine.resume \ coroutine_create=coroutine.create \ coroutine_wait=coroutine.wait \ wait=coroutine.wait \ newthread=coroutine.spawn \ newco = newthread "); lua_pcall(L, 0, 0, 0); luaL_newmetatable(L, "cosocket:tcp"); lua_pushvalue(L, lua_upvalueindex(1)); setfuncs(L, M, 1); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaL_newmetatable(L, "cosocket:udp"); lua_pushvalue(L, lua_upvalueindex(1)); setfuncs(L, M_UDP, 1); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); lua_pop(L, 1); static const struct luaL_reg _MT[] = {{NULL, NULL}}; luaL_openlib(L, "cosocket", cosocket_methods, 0); luaL_newmetatable(L, "cosocket*"); luaL_register(L, NULL, _MT); lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); /* dup methods table*/ lua_rawset(L, -3); /* metatable.__index = methods */ lua_pushliteral(L, "__metatable"); lua_pushvalue(L, -3); /* dup methods table*/ lua_rawset(L, -3); /* hide metatable: metatable.__metatable = methods */ lua_pop(L, 1); /* drop metatable */ lua_pushcfunction(L, lua_f_startloop); return 1; }
void Player::registerMetatable(lua_State* l) { if (luaL_newmetatable(l, metatableName.c_str()) == 1) { } }
int Particle2DSystem::Init(lua_State*L) { luaL_Reg api_m[] = { { "AddChild",AliceParticle2DSystem_AddChild }, { "ChangeMode",AliceParticle2DSystem_ChangeMode }, //重力模式 { "SetGravity",AliceParticle2DSystem_SetGravity }, { "SetSpeed",AliceParticle2DSystem_SetSpeed }, { "SetSpeedVar",AliceParticle2DSystem_SetSpeedVar }, { "SetTangencialAccel",AliceParticle2DSystem_SetTangencialAccel }, { "SetTangencialAccelVar",AliceParticle2DSystem_SetTangencialAccelVar }, { "SetRadialAccel",AliceParticle2DSystem_SetRadialAccel }, { "SetRadialAccelVar",AliceParticle2DSystem_SetRadialAccelVar }, //角度模式 { "SetStartRadius",AliceParticle2DSystem_SetStartRadius }, { "SetStartRadiusVar",AliceParticle2DSystem_SetStartRadiusVar }, { "SetEndRadius",AliceParticle2DSystem_SetEndRadius }, { "SetEndRadiusVar",AliceParticle2DSystem_SetEndRadiusVar }, { "SetRotatePerSecond",AliceParticle2DSystem_SetRotatePerSecond }, { "SetRotatePerSecondVar",AliceParticle2DSystem_SetRotatePerSecondVar }, //公用属性 { "SetStartSize",AliceParticle2DSystem_SetStartSize }, { "SetStartSizeVar",AliceParticle2DSystem_SetStartSizeVar }, { "SetEndSize",AliceParticle2DSystem_SetEndSize }, { "SetEndSizeVar",AliceParticle2DSystem_SetEndSizeVar }, { "SetStartColor",AliceParticle2DSystem_SetStartColor }, { "SetStartColorVar",AliceParticle2DSystem_SetStartColorVar }, { "SetEndColor",AliceParticle2DSystem_SetEndColor }, { "SetEndColorVar",AliceParticle2DSystem_SetEndColorVar }, { "SetStartSpin",AliceParticle2DSystem_SetStartSpin }, { "SetStartSpinVar",AliceParticle2DSystem_SetStartSpinVar }, { "SetEndSpin",AliceParticle2DSystem_SetEndSpin }, { "SetEndSpinVar",AliceParticle2DSystem_SetEndSpinVar }, { "SetLife",AliceParticle2DSystem_SetLife }, { "SetLifeVar",AliceParticle2DSystem_SetLifeVar }, { "SetAngle",AliceParticle2DSystem_SetAngle }, { "SetAngleVar",AliceParticle2DSystem_SetAngleVar }, { "SetPosition",AliceParticle2DSystem_SetPosition }, { "SetPositionVar",AliceParticle2DSystem_SetPositionVar }, { "SetCenterOfGravity",AliceParticle2DSystem_SetCenterOfGravity }, //继承自ParticleSystem的公有属性 { "SetEmissionRate",AliceParticle2DSystem_SetEmissionRate }, { "SetDuration",AliceParticle2DSystem_SetDuration }, { "SetBlendFunc",AliceParticle2DSystem_SetBlendFunc }, { "SetPositionType",AliceParticle2DSystem_SetPositionType }, { "SetTexture",AliceParticle2DSystem_SetTexture }, { NULL,NULL } }; luaL_newmetatable(L, "Particle2DSystemT"); lua_pushstring(L, "__index"); lua_pushvalue(L, -2); lua_settable(L, -3); lua_pushstring(L, "__newindex"); lua_pushvalue(L, -2); lua_settable(L, -3); luaL_openlib(L, NULL, api_m, 0); //register entry luaL_Reg api_c[] = { { "New",NewParticle2DSystemObject }, { NULL,NULL } }; luaL_openlib(L, "Particle2DSystem", api_c, 0); printf("init Particle2DSystem\n"); return 0; }
static void luv_req_init(lua_State* L) { luaL_newmetatable (L, "uv_req"); lua_pushcfunction(L, luv_req_tostring); lua_setfield(L, -2, "__tostring"); lua_pop(L, 1); }