LUAMOD_API int luaopen_extlib_snowflake(lua_State* l) { spinlock_init(&sync_policy); luaL_checkversion(l); luaL_Reg lib[] = { { "init", linit }, { "next_id", lnextid }, { NULL, NULL } }; luaL_newlib(l, lib); return 1; }
LUALIB_API void luaL_openlib (lua_State *L, const char *libname, const luaL_Reg *l, int nup) { luaL_checkversion(L); if (libname) { luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ } if (l) luaL_setfuncs(L, l, nup); else lua_pop(L, nup); /* remove upvalues */ }
int luaopen_memory_c(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { {"used", lused}, {"stat", lstat}, { NULL, NULL }, }; luaL_newlib(L, l); return 1; }
DDCLLUA int luaopen_lddcl_core(lua_State * L){ luaL_checkversion(L); DDLUA_NEWLIB(L, "lddcl.core", _reg); openlib_file(L); openlib_thread(L); openlib_service(L); openlib_socket(L); return 1; }
//将l构成的函数列表设置到top-nup指定的数组,将栈顶的nup个元素设置为每个函数的upvalues. //返回时栈顶下的第一个元素为top-nup指定的数组 LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkversion(L); luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -nup); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ lua_setfield(L, -(nup + 2), l->name); } lua_pop(L, nup); /* remove upvalues */ }
int luaopen_otc(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "name", lname }, { NULL, NULL }, }; luaL_newlib(L,l); return 1; }
int luaopen_lcrab(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { {"open", dict_open}, {"filter", dict_filter}, {NULL, NULL} }; luaL_newlib(L, l); return 1; }
LUA_API_EXPORT int luaopen_aes (lua_State *L) { luaL_checkversion (L); luaL_Reg l[] = { { "encrypt", lencrypt }, { "decrypt", ldecrypt }, { NULL, NULL }, }; //luaL_newlib (L,l); lua_newtable(L); luaL_openlib(L, "aes", l, 0); return 1; }
static int pmain (lua_State *L) { /* open standard libraries */ luaL_checkversion(L); lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ luaL_openlibs(L); /* open libraries */ lua_gc(L, LUA_GCRESTART, 0); print_version(); dotty(L); lua_pushboolean(L, 1); /* signal no errors */ return 1; }
int LUA::init() { lock(); printf("LUA\n"); L = lua_open(); luaL_checkversion(L); /* check that interpreter has correct version */ luaopen_base(L); luaL_openlibs(L); /* open standard libraries */ hulua::def(L, "_ALERT", LUA::show_error); hulua::def(L, "jump", Jump); hulua::def(L, "import_xml",import_xml); unlock(); }
/* ** Main body of stand-alone interpreter (to be called in protected mode). ** Reads the options and handles them all. */ static int pmain (lua_State *L) { luaL_checkversion(L); /* check that interpreter has correct version */ lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); luaL_openlibs(L); /* open standard libraries */ print_version(); doREPL(L); /* do read-eval-print loop */ lua_pushboolean(L, 1); /* signal no errors */ return 1; }
int luaopen_csystem(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "get", lget }, { "set", lset }, { NULL, NULL }, }; luaL_newlib(L, l); lua_pushstring(L, "SOCKET"); lua_pushinteger(L, SERVICE_SOCKET); lua_settable(L, -3); return 1; }
LUALIB_API int luaopen_levent_socket_c(lua_State *L) { luaL_checkversion(L); if(luaL_newmetatable(L, SOCKET_METATABLE)) { luaL_setfuncs(L, socket_mt, 0); luaL_newlib(L, socket_methods); lua_setfield(L, -2, "__index"); } lua_pop(L, 1); // +end luaL_newlib(L, socket_module_methods); // address family ADD_CONSTANT(L, AF_INET); ADD_CONSTANT(L, AF_INET6); // socket type ADD_CONSTANT(L, SOCK_STREAM); ADD_CONSTANT(L, SOCK_DGRAM); // protocal type ADD_CONSTANT(L, IPPROTO_TCP); ADD_CONSTANT(L, IPPROTO_UDP); // sock opt ADD_CONSTANT(L, SOL_SOCKET); ADD_CONSTANT(L, SO_REUSEADDR); ADD_CONSTANT(L, SO_LINGER); ADD_CONSTANT(L, SO_KEEPALIVE); ADD_CONSTANT(L, SO_SNDBUF); ADD_CONSTANT(L, SO_RCVBUF); #ifdef SO_REUSEPORT ADD_CONSTANT(L, SO_REUSEPORT); #endif #ifdef SO_NOSIGPIPE ADD_CONSTANT(L, SO_NOSIGPIPE); #endif #ifdef SO_NREAD ADD_CONSTANT(L, SO_NREAD); #endif #ifdef SO_NWRITE ADD_CONSTANT(L, SO_NWRITE); #endif #ifdef SO_LINGER_SEC ADD_CONSTANT(L, SO_LINGER_SEC); #endif return 1; }
int luaopen_luna_webclient(lua_State * L) { luaL_checkversion(L); if (luaL_newmetatable(L, LUA_WEB_CLIENT_MT)) { lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); luaL_setfuncs(L, webclient_funs, 0); lua_pop(L, 1); } luaL_newlib(L, webclient_createfuns); return 1; }
int luaopen_monitor(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "depth", ldepth }, { "report", lreport }, { "detailreport", ldetailreport }, { NULL, NULL }, }; luaL_newlib(L,l); struct status * s = malloc(sizeof(*G)); G = s; return 1; }
int luaopen_server_core(lua_State *L) { luaL_checkversion(L); /* typedef struct luaL_Reg { const char *name; lua_CFunction func; } luaL_Reg; 用于 luaL_setfuncs 注册函数的数组类型。 name 指函数名,func 是函数指针。 任何 luaL_Reg 数组必须以一对 name 与 func 皆为 NULL 结束。 */ luaL_Reg l[] = { { "send" , _send }, { "genid", _genid }, { "redirect", _redirect }, { "command" , _command }, { "error", _error }, { "callback", _callback }, { "getsignal", _getsignal }, { "getpid", _getpid }, { "tostring", _tostring }, { "pack", _luaseri_pack }, { "packstring", lpackstring }, { "unpack", _luaseri_unpack }, { "trash" , ltrash }, { NULL, NULL }, }; //创建一张新的表,并预分配足够保存下数组 l 内容的空间(但不填充),这是给 luaL_setfuncs 一起用的 luaL_newlibtable(L, l); //提取ctx压入栈顶,用于下面luaL_setfuncs设置上值 lua_getfield(L, LUA_REGISTRYINDEX, "server_context"); struct server_context *ctx = lua_touserdata(L,-1); if (ctx == NULL) { return luaL_error(L, "Init server context first"); } /* void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); 把数组 l 中的所有函数注册到栈顶的表中 若nup不为零,所有的函数都共享nup个上值。这些值必须在调用之前,压在表之上。这些值在注册完毕后都会从栈弹出。 上值在函数调用时可以使用 lua_upvalueindex 获取到 */ luaL_setfuncs(L,l,1); return 1; }
int luaopen_csocket(lua_State *L){ luaL_checkversion(L); luaL_Reg l[] = { {"open", lopen}, {"close", lclose}, {"write", lwrite}, {"read", lread}, {NULL, NULL}, }; luaL_newlib(L, l); return 1; }
int luaopen_pipe(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { {"connect", lconnect }, {"close", lclose }, {"read", lread }, {"write", lwrite }, { NULL, NULL }, }; luaL_newlib(L,l); return 1; }
int luaopen_memory(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "total", _total }, { "block", _block }, { "dump", _dump }, { NULL, NULL }, }; luaL_newlib(L,l); return 1; }
int luaopen_seal_core(lua_State* L) { #ifdef luaL_checkversion luaL_checkversion(L); #endif luaL_Reg lib[] = { { "inject", lsealinject}, { "reload_script", lseal_reload_script}, { NULL, NULL }, }; luaL_newlib(L, lib); return 1; }
int luaopen_liekkas_decode(lua_State* L) { luaL_checkversion(L); luaL_Reg l[] = { {"decode_tools", adl_decode_tools}, {"decode_mp3", adl_decode_mp3}, {"decode_wav", adl_decode_wav}, {"decode_ogg", adl_decode_ogg}, {"info_dump", _info_dump}, {NULL, NULL}, }; luaL_newlib(L, l); return 1; }
// require "socketdriver" int luaopen_socketdriver(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "buffer", lnewbuffer }, { "push", lpushbuffer }, { "pop", lpopbuffer }, { "drop", ldrop }, { "readall", lreadall }, { "clear", lclearbuffer }, { "readline", lreadline }, { "str2p", lstr2p }, { "header", lheader }, { "unpack", lunpack }, { NULL, NULL }, }; // 创建一张新的表,并把列表 l 中的函数注册进去 luaL_newlib(L,l); luaL_Reg l2[] = { { "connect", lconnect }, { "close", lclose }, { "shutdown", lshutdown }, { "listen", llisten }, { "send", lsend }, { "lsend", lsendlow }, { "bind", lbind }, { "start", lstart }, { "nodelay", lnodelay }, { "udp", ludp }, { "udp_connect", ludp_connect }, { "udp_send", ludp_send }, { "udp_address", ludp_address }, { NULL, NULL }, }; // 把注册表变量skynet_context压入栈中 lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context"); struct skynet_context *ctx = lua_touserdata(L,-1); // 获取并判断非空 if (ctx == NULL) { return luaL_error(L, "Init skynet context first"); } // 把列表 l2 中的函数注册进去,并设置一个上值skynet_context // 设置完上值从栈中弹出,只对l2中的函数有效 luaL_setfuncs(L,l2,1); return 1; }
int luaopen_protobuf_c(lua_State *L) { luaL_Reg reg[] = { {"_env_new" , _env_new }, {"_env_register" , _env_register }, {"_env_type", _env_type }, {"_rmessage_new" , _rmessage_new }, {"_rmessage_delete" , _rmessage_delete }, {"_rmessage_integer" , _rmessage_integer }, {"_rmessage_int32", _rmessage_int32 }, {"_rmessage_int64", _rmessage_int64 }, {"_rmessage_int52", _rmessage_int52 }, {"_rmessage_uint52", _rmessage_uint52 }, {"_rmessage_real" , _rmessage_real }, {"_rmessage_string" , _rmessage_string }, {"_rmessage_message" , _rmessage_message }, {"_rmessage_size" , _rmessage_size }, {"_wmessage_new", _wmessage_new }, {"_wmessage_delete", _wmessage_delete }, {"_wmessage_integer", _wmessage_integer }, {"_wmessage_real", _wmessage_real }, {"_wmessage_string", _wmessage_string }, {"_wmessage_message", _wmessage_message }, {"_wmessage_int32", _wmessage_int32 }, {"_wmessage_int64", _wmessage_int64 }, {"_wmessage_int52", _wmessage_int52 }, {"_wmessage_uint52", _wmessage_uint52 }, {"_wmessage_buffer", _wmessage_buffer }, {"_wmessage_buffer_string", _wmessage_buffer_string }, {"_pattern_new", _pattern_new }, {"_pattern_delete", _pattern_delete }, {"_pattern_size", _pattern_size }, {"_pattern_unpack", _pattern_unpack }, {"_pattern_pack", _pattern_pack }, {"_last_error", _last_error }, {"_decode", _decode }, {"_gc", _gc }, {"_add_pattern", _add_pattern }, {"_add_rmessage", _add_rmessage }, {"_env_enum_id", _env_enum_id}, {NULL,NULL}, }; luaL_checkversion(L); luaL_newlib(L, reg); return 1; }
LUAMOD_API int luaopen_skynet_multicast_core(lua_State *L) { luaL_Reg l[] = { { "pack", mc_packlocal }, { "unpack", mc_unpacklocal }, { "bind", mc_bindrefer }, { "close", mc_closelocal }, { "remote", mc_remote }, { "packremote", mc_packremote }, { "nextid", mc_nextid }, { NULL, NULL }, }; luaL_checkversion(L); luaL_newlib(L,l); return 1; }
int luaopen_httppack(lua_State *L) { luaL_checkversion(L); // the order is same with macros : TYPE_* (defined top) lua_pushliteral(L, "data"); lua_pushliteral(L, "more"); lua_pushliteral(L, "error"); lua_pushliteral(L, "open"); lua_pushliteral(L, "close"); lua_pushcclosure(L, lfilter, 5); lua_setfield(L, -2, "filter"); return 1; }
LUAMOD_API int luaopen_skynet_datasheet_core(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "new", lnew }, { "update", lupdate }, { NULL, NULL }, }; luaL_newlibtable(L,l); gen_metatable(L); luaL_setfuncs(L, l, 1); lua_pushcfunction(L, lstringpointer); lua_setfield(L, -2, "stringpointer"); return 1; }
void set_functions( lua_State* L, const std::vector<lua_cpp::Reg>& functions, int nup ) { luaL_checkversion(L); luaL_checkstack(L, nup+1, "too many upvalues"); for (const lua_cpp::Reg& l : functions) { /* fill the table with given functions */ if (l.name == nullptr) { continue; } int i; for (i = 0; i < nup; ++i) /* copy upvalues to the top */ lua_pushvalue(L, -nup); push_closure(L, l.func, nup); /* closure with those upvalues */ lua_setfield(L, -(nup + 2), l.name); } lua_pop(L, nup); /* remove upvalues */ }
int luaopen_silly(lua_State *L) { luaL_Reg tbl[] = { //core {"workid", _lworkid}, {"getenv", _lgetenv}, {"setenv", _lsetenv}, {"exit", _lexit}, //timer {"timerentry", _ltimer_entry}, {"timeradd", _ltimer_add}, {"timernow", _ltimer_now}, //socket {"socketentry", _lsocket_entry}, {"socketconnect", _lsocket_connect}, {"socketclose", _lsocket_close}, {"socketsend", _lsocket_send}, {"dropmessage", _ldrop}, //end {NULL, NULL}, }; luaL_checkversion(L); luaL_newmetatable(L, "silly_socket_data"); luaL_newmetatable(L, "silly_socket_packet"); lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *m = lua_tothread(L, -1); lua_pop(L, 1); lua_pushlightuserdata(L, (void *)m); lua_gettable(L, LUA_REGISTRYINDEX); struct silly_worker *w = lua_touserdata(L, -1); assert(w); silly_worker_message(w, _process_msg); silly_worker_exit(w, _exit); luaL_newlibtable(L, tbl); lua_pushlightuserdata(L, (void *)w); luaL_setfuncs(L, tbl, 1); return 1; }
bool Core::init() { lua = luaL_newstate(); assert(lua != 0); luaL_checkversion(lua); // Stop garbage collector. lua_gc(lua, LUA_GCSTOP, 0); // Load Lua core libraries. // \todo maybe don't load lua libraries to save memory (and omit from build). luaL_openlibs(lua); // Register core function with Lua. lua_register(lua, "quit", Script::quit); lua_register(lua, "backgroundColor", Script::backgroundColor); lua_register(lua, "create", Script::create); lua_register(lua, "pos", Script::pos); lua_register(lua, "move", Script::move); lua_register(lua, "destroy", Script::destroy); lua_register(lua, "pc", Script::pc); lua_register(lua, "tone", Script::tone); // Restart garbage collector. lua_gc(lua, LUA_GCRESTART, 0); if (!Graphics::init()) { return false; } if (!assetContainer.setContainerFilename("content.seng")) { Platform::error << "Failed to load content file: content.seng" << std::endl << std::endl; Platform::error << "AssetContainer error: " << assetContainer.getLastError() << std::endl << std::endl; return false; } char initScript[1024]; if (!assetContainer.getString("scripts/init.lua", initScript, 1024)) { Platform::error << "AssetContainer error: " << assetContainer.getLastError(); return false; } doString(initScript); return true; }
int luaopen_csocketepl(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { {"bind", lbind}, {"listen", llisten}, {"accept", laccept}, {"write", lwrite}, {"read", lread}, {"close", lclose}, {NULL, NULL}, }; luaL_newlib(L, l); return 1; }