//----------------------------------------------------------------// void MOAILuaObject::SetMemberTable ( MOAILuaState& state, int idx ) { UNUSED ( state ); UNUSED ( idx ); // TODO: what if object is a singleton? assert ( !this->GetLuaClass ()->IsSingleton ()); // TODO: should actually set the member table, not just crash idx = state.AbsIndex ( idx ); this->PushLuaUserdata ( state ); // userdata lua_getmetatable ( state, - 1 ); // ref table lua_getmetatable ( state, - 1 ); // member table lua_getmetatable ( state, - 1 ); // interface table lua_pushvalue ( state, idx ); lua_replace ( state, -3 ); this->MakeLuaBinding ( state ); state.Pop ( 1 ); }
static struct sprite * lookup(lua_State *L, struct sprite * spr) { int i; struct sprite * root = (struct sprite *)lua_touserdata(L, -1); lua_getuservalue(L,-1); for (i=0;sprite_component(root, i)>=0;i++) { struct sprite * child = root->data.children[i]; if (child) { lua_rawgeti(L, -1, i+1); if (child == spr) { lua_replace(L,-2); return child; } else { lua_pop(L,1); } } } lua_pop(L,1); return NULL; }
LUALIB_API int luaopen_io(lua_State* L) { createmeta(L); /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ newfenv(L, io_fclose); lua_replace(L, LUA_ENVIRONINDEX); /* open library */ luaL_register(L, LUA_IOLIBNAME, iolib); /* create (and set) default files */ newfenv(L, io_noclose); /* close function for default files */ createstdfile(L, stdin, IO_INPUT, "stdin"); createstdfile(L, stdout, IO_OUTPUT, "stdout"); createstdfile(L, stderr, 0, "stderr"); lua_pop(L, 1); /* pop environment for default files */ lua_getfield(L, -1, "popen"); newfenv(L, io_pclose); /* create environment for 'popen' */ lua_setfenv(L, -2); /* set fenv for 'popen' */ lua_pop(L, 1); /* pop 'popen' */ return 1; }
static int class_call_event(lua_State* L) { if (lua_istable(L, 1)) { //class is not a metatable now, so must get it's metatable to access ".call" function. 2014.6.5 by SunLightJuly if (lua_getmetatable(L, 1)) { lua_replace(L, 1); lua_pushstring(L, ".call"); lua_rawget(L, 1); if (lua_isfunction(L, -1)) { lua_insert(L, 1); lua_call(L, lua_gettop(L)-1, 1); return 1; } } } tolua_error(L,"Attempt to call a non-callable object.",NULL); return 0; };
int Xml_load (lua_State *L) { const char * filename = luaL_checkstring(L,1); FILE * file=fopen(filename,"r"); char* buffer; size_t sz; if(!file) return luaL_error(L,"LuaXml ERROR: \"%s\" file error or file not found!",filename); fseek (file , 0 , SEEK_END); sz = ftell (file); rewind (file); buffer = (char*)malloc(sz+1); sz = fread (buffer,1,sz,file); fclose(file); buffer[sz]=0; lua_pushlightuserdata(L,buffer); lua_replace(L,1); return Xml_eval(L); };
int luaopen_sdl_audio(lua_State *L) { lua_newtable(L); luaL_register(L, NULL, sdl_audiolib); lua_pushboolean(L, 1); lua_setfield(L, -2, "loaded"); lua_createtable(L, 0, 2); lua_pushvalue(L, -1); lua_replace(L, luaT_environindex); lua_pushvalue(L, luaT_environindex); lua_pushcclosure(L, luaT_stdgc<music_t, luaT_environindex>, 1); lua_setfield(L, -2, "__gc"); lua_pushvalue(L, 1); lua_setfield(L, -2, "__index"); lua_pop(L, 1); luaT_register(L, NULL, sdl_musiclib); return 1; }
static int lua_member_lookup(lua_State* L) { #define IFLUA_NUM_MEMB_LOOKUP(n, m) \ if (strncmp(cstr, n, sizeof(n))==0){\ lua_pushnumber(L, m );\ } #define IFLUA_STR_MEMB_LOOKUP(n, m) \ if (strncmp(cstr, n, sizeof(n))==0){\ lua_pushlstring(L, m.c_str(), m.size() );\ } bind_t* state = lunar_t::check(L, 1); const char* cstr = lua_tostring(L, 2); CombatStats* stats = state->get_stats(L); if (!stats) { lua_pushnil(L); return 1; } IFLUA_NUM_MEMB_LOOKUP("hp", stats->core.hp) else IFLUA_NUM_MEMB_LOOKUP("mp", stats->core.mp) else IFLUA_NUM_MEMB_LOOKUP("max_hp", stats->core.max_hp) else IFLUA_NUM_MEMB_LOOKUP("max_mp", stats->core.max_mp) else IFLUA_NUM_MEMB_LOOKUP("strength", stats->core.strength) else IFLUA_NUM_MEMB_LOOKUP("magic", stats->core.magic) else IFLUA_NUM_MEMB_LOOKUP("defence", stats->core.defence) else IFLUA_NUM_MEMB_LOOKUP("willpower", stats->core.willpower) else IFLUA_NUM_MEMB_LOOKUP("speed", stats->movespeed) else IFLUA_NUM_MEMB_LOOKUP("xp", stats->class_stats.xp) else IFLUA_NUM_MEMB_LOOKUP("xpneeded", stats->class_stats.xpneeded) else IFLUA_NUM_MEMB_LOOKUP("level", stats->class_stats.xplevel) else IFLUA_STR_MEMB_LOOKUP("weapon_type", stats->equipment.weapon.weapon_entry().weapon_class) else { lua_getglobal(L, bind_t::className); int tableind = lua_gettop(L); lua_pushvalue(L, 2); lua_gettable(L, tableind); lua_replace(L, tableind); } return 1; }
int jiveL_update_screen(lua_State *L) { JiveSurface *screen; /* stack is: * 1: framework */ /* ping watchdog */ // FIXME 30 seconds watchdog_keepalive(ui_watchdog, 3); if (!update_screen) { return 0; } lua_pushcfunction(L, jive_traceback); /* push traceback function */ lua_pushcfunction(L, _draw_screen); lua_pushvalue(L, 1); lua_getfield(L, 1, "screen"); lua_getfield(L, -1, "surface"); lua_replace(L, -2); screen = tolua_tousertype(L, -1, 0); lua_pushboolean(L, 0); if (lua_pcall(L, 3, 1, 2) != 0) { LOG_WARN(log_ui_draw, "error in update_screen:\n\t%s\n", lua_tostring(L, -1)); return 0; } /* flip screen */ if (lua_toboolean(L, -1)) { jive_surface_flip(screen); } lua_pop(L, 2); return 0; }
/*-------------------------------------------------------------------------*\ * object:receive() interface \*-------------------------------------------------------------------------*/ int buffer_meth_receive(lua_State *L, p_buffer buf) { int err = IO_DONE, top = lua_gettop(L); luaL_Buffer b; size_t size; const char *part = luaL_optlstring(L, 3, "", &size); p_timeout tm = timeout_markstart(buf->tm); /* initialize buffer with optional extra prefix * (useful for concatenating previous partial results) */ luaL_buffinit(L, &b); luaL_addlstring(&b, part, size); /* receive new patterns */ if (!lua_isnumber(L, 2)) { const char *p= luaL_optstring(L, 2, "*l"); if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); else luaL_argcheck(L, 0, 2, "invalid receive pattern"); /* get a fixed number of bytes (minus what was already partially * received) */ } else err = recvraw(buf, (size_t) lua_tonumber(L, 2)-size, &b); /* check if there was an error */ if (err != IO_DONE) { /* we can't push anyting in the stack before pushing the * contents of the buffer. this is the reason for the complication */ luaL_pushresult(&b); lua_pushstring(L, buf->io->error(buf->io->ctx, err)); lua_pushvalue(L, -2); lua_pushnil(L); lua_replace(L, -4); } else { luaL_pushresult(&b); lua_pushnil(L); lua_pushnil(L); } #ifdef LUASOCKET_DEBUG /* push time elapsed during operation as the last return value */ lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); #else (void)tm; #endif return lua_gettop(L) - top; }
/// @brief __newindex closure /// @note upvalue 1: Member descriptor, non-permissions, and lookup data /// @note upvalue 2: Member setter table /// @note upvalue 3: Propagation boolean /// @note object: Object being accessed /// @note key: Lookup key /// @note value: Value to assign static int NewIndex (lua_State * L) { Lookup(L); // object, key, value, data // lua_pushvalue(L, 2);// object, key, value, data, key lua_gettable(L, lua_upvalueindex(2)); // object, key, value, data, setter if (!lua_isnil(L, 5)) { lua_insert(L, 1); // setter, object, key, value, data lua_call(L, 4, 1); // result if (!lua_isnil(L, 1) && lua_toboolean(L, lua_upvalueindex(3))) return 1; } // else { lua_pop(L, 1); // object, key, value, data lua_replace(L, 1); // data, key, value lua_rawgeti(L, lua_upvalueindex(1), eNoWrite); // data, key, value, nowrite lua_pushvalue(L, 2);// data, key, value, nowrite, key lua_gettable(L, 4); // data, key, value, nowrite, bNoWrite if (!lua_toboolean(L, 5)) { // lua_pop(L, 2); // data, key, value lua_rawgeti(L, lua_upvalueindex(1), eDescriptors); // data, key, value, D lua_pushvalue(L, 2);// data, key, value, D, key lua_gettable(L, 4); // data, key, value, D, D[key] if (!lua_isnil(L, 5)) NewIndexMember(L); else if (lua_toboolean(L, lua_upvalueindex(3))) return 1; } } return 0; }
static int lua_SettingIterator(lua_State *L) { int i = lua_tointeger(L, lua_upvalueindex(1)); enumDBSettingsParam* param = (enumDBSettingsParam*)lua_touserdata(L, lua_upvalueindex(2)); if (i < param->count) { lua_pushinteger(L, (i + 1)); lua_replace(L, lua_upvalueindex(1)); lua_pushstring(L, ptrA(mir_utf8encode(param->pszSettingName[i]))); mir_free(param->pszSettingName[i]); } else { lua_pushnil(L); mir_free(param->pszSettingName); mir_free(param); } return 1; }
int push_table_instance(lua_State* L, int lo) { if (lua_istable(L, lo)) { lua_pushstring(L, ".c_instance"); lua_gettable(L, lo); if (lua_isuserdata(L, -1)) { lua_replace(L, lo); return 1; } else { lua_pop(L, 1); return 0; }; } else { return 0; }; return 0; };
static void *proxy_ThFunc(void *__ ) { int ret_= 0, i; TH_STATE *th= __th_state[(int )__ ]; //在 L 中执行, 进行数据处理 ... lua_State *L= th->L; //ret_= lua_resume(L, 0 ); ret_= lua_pcall(L, 0, 1, 0 ); //如果执行正确, L 栈高度 <= 1, 否则 > 1 i= lua_gettop(L ); if(1< i ) //清理不需要的返回值 { lua_replace(L, 1 ); lua_settop(L, 1 ); } if(0!= ret_ ) { lua_pushinteger(L, ret_ ); //错误号 lua_pushvalue(L, -2 ); lua_remove(L, 1 ); //进行位置交换 } pthread_mutex_lock(&MUTEX_S_ ); __th_state[(int )__ ]= NULL; pthread_mutex_unlock(&MUTEX_S_ ); lua_gc(th->L, LUA_GCCOLLECT, 0 ); lua_close(th->L ); pthread_detach(th->hTh ); if(th ) free(th ); return NULL; }
int luaopen_libmodbus(lua_State *L) { #ifdef LUA_ENVIRONINDEX /* set private environment for this module */ lua_newtable(L); lua_replace(L, LUA_ENVIRONINDEX); #endif /* metatable.__index = metatable */ luaL_newmetatable(L, MODBUS_META_CTX); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); luaL_setfuncs(L, ctx_M, 0); luaL_newlib(L, R); modbus_register_defs(L, D, S); return 1; }
int L_Container<Lua_MonsterClasses_Name, Lua_MonsterClass>::_iterator(lua_State *L) { int32 index = static_cast<int32>(lua_tonumber(L, lua_upvalueindex(1))); while (index < Length()) { if (Lua_MonsterClass::Valid(1 << index)) { Lua_MonsterClass::Push(L, 1 << index); lua_pushnumber(L, ++index); lua_replace(L, lua_upvalueindex(1)); return 1; } else { ++index; } } lua_pushnil(L); return 1; }
int frmMain::_l_init(lua_State *L) { frmMain *pThis = reinterpret_cast<frmMain*>(lua_touserdata(L, 1)); THMapWrapper::wrap(L); // Create a new environment table: { // [1] = <light userdata pThis>, // } lua_newtable(L); lua_insert(L, 1); lua_rawseti(L, 1, 1); lua_replace(L, LUA_ENVIRONINDEX); // NB: Following functions registered with above environment table luaT_execute(L, "MapEditorSetBlocks = ...", _l_set_blocks); luaT_execute(L, "MapEditorSetBlockBrush = ...", _l_set_block_brush); luaT_execute(L, "MapEditorInitWithLuaApp = ...", _l_init_with_lua_app); return 0; }
static int call_funcarray_from_lua(lua_State *L) { mrp_funcarray_t *fa; int narg, top; size_t i; int j; int success; top = lua_gettop(L); narg = top - 1; if (!(fa = to_funcarray(L, 1))) luaL_typerror(L, 1, "function array"); if (fa->nfunc > 0 && !fa->funcs) luaL_error(L, "attempt to call a corruptfunction array"); success = true; for (i = 0; i < fa->nfunc; i++) { mrp_funcbridge_push(L, fa->funcs[i]); for (j = 0; j < narg; j++) lua_pushvalue(L, j+2); make_lua_call(L, fa->funcs[i], top+1); if (!lua_isboolean(L, -1) || !lua_toboolean(L, -1)) success = false; lua_settop(L, top); } lua_pushboolean(L, success); lua_replace(L, 1); lua_settop(L, 1); return 1; }
int jiveL_style_path(lua_State *L) { int numStrings = 0; lua_pushvalue(L, 1); while (!lua_isnil(L, -1)) { lua_getfield(L, -1, "style"); if (!lua_isnil(L, -1)) { lua_insert(L, 2); lua_pushstring(L, "."); lua_insert(L, 2); numStrings += 2; } else { lua_pop(L, 1); } lua_getfield(L, -1, "styleModifier"); if (!lua_isnil(L, -1)) { lua_insert(L, 2); lua_pushstring(L, "."); lua_insert(L, 2); numStrings += 2; } else { lua_pop(L, 1); } lua_getfield(L, -1, "parent"); lua_replace(L, -2); } lua_pop(L, 1); lua_concat(L, numStrings - 1); lua_remove(L, -2); lua_pushvalue(L, -1); lua_setfield(L, 1, "_stylePath"); return 1; }
// __len metamethod handler static int l_str_len(lua_State *L) { luaL_checktype(L, 1, LUA_TUSERDATA); // Fetch proxied value aux_push_weak_table(L, 0); lua_pushvalue(L, 1); lua_gettable(L, -2); // String tables are proxied in Lua, and Lua tables do not honour __len // so use ipairs to get the unproxied table to call __len on. if(lua_type(L, -1) == LUA_TTABLE) { lua_getglobal(L, "ipairs"); lua_insert(L, -2); lua_call(L, 1, 2); lua_replace(L, -2); } lua_pushinteger(L, (lua_Integer)lua_objlen(L, -1)); return 1; }
//----------------------------------------------------------------// bool MOAILuaObject::PushRefTable ( MOAILuaState& state ) { MOAILuaClass* luaClass = this->GetLuaClass (); if ( !luaClass ) { lua_pushnil ( state ); return false; } if ( luaClass->IsSingleton ()) { luaClass->PushRefTable ( state ); return true; } if ( this->PushLuaUserdata ( state )) { int result = lua_getmetatable ( state, -1 ); if ( result ) { lua_replace ( state, -2 ); return true; } } return false; }
/* See STRATEGY section below */ static int got_array_value(lua_State* L) { /* ..., Table, Integer, Func, Value */ lua_pushliteral(L, "length"); /* ..., Table, Integer, Func, Value, "length" */ lua_pushvalue(L, -1); /* ..., Table, Integer, Func, Value, "length", "length" */ lua_rawget(L, -6); /* ..., Table, Integer, Func, Value, "length", len */ long len = lua_tonumber(L, -1); lua_pop(L, 1); /* ..., Table, Integer, Func, Value, "length" */ lua_pushnumber(L, len + 1); /* ..., Table, Integer, Func, Value, "length", len */ lua_rawset(L, -6); /* ..., Table, Integer, Func, Value */ lua_rawseti(L, -4, lua_tointeger(L, -3)); lua_pushinteger(L, lua_tointeger(L, -2)+1); lua_replace(L, -3); return 0; /* Ignored. */ }
static int search_path(lua_State *L, int widget, char *path, const char *key) { char *tok = strtok(path, "."); while (tok) { lua_pushstring(L, tok); lua_gettable(L, -2); if (lua_isnil(L, -1)) { lua_pop(L, 1); return 0; } luaL_checktype(L, -1, LUA_TTABLE); lua_replace(L, -2); tok = strtok(NULL, "."); } lua_pushstring(L, key); lua_gettable(L, -2); return 1; }
void *lualoom_getnativepointer(lua_State *L, int index, bool replaceIndex, const char *typecheck) { index = lua_absindex(L, index); lmAssert(!lua_isnil(L, index), "Interal Error: lua_getnativepointer() passes null value for type %s", typecheck ? typecheck : "Undefined"); lmAssert(lua_istable(L, index), "Interal Error: lua_getnativepointer() received non-table for type %s", typecheck ? typecheck : "Undefined"); if (typecheck) { lua_rawgeti(L, index, LSINDEXTYPE); Type *type = (Type *)lua_topointer(L, -1); lmAssert(type, "Interal Error: Unable to get valid Type* from native instance table"); lmAssert(type->getFullName() == typecheck, "Interal Error: Type mismatch in lua_getnativepointer(). Expected %s, received %s", typecheck, type->getFullName().c_str()); lua_pop(L, 1); } lua_rawgeti(L, index, LSINDEXNATIVE); lmAssert(lua_isuserdata(L, -1), "Interal Error: lua_getpointer() native index is not a userdata"); int _index = -1; if (replaceIndex) { lua_replace(L, index); _index = index; } LS::Detail::Userdata *p = (Detail::Userdata *)lua_topointer(L, _index); lmAssert(p, "Interal Error: lua_getpointer() invalid pointer on lua stack"); void *pointer = p->getPointer(); lmAssert(pointer, "Interal Error: lua_getpointer() unable to get pointer from Userdata"); if (!replaceIndex) { lua_pop(L, 1); } return pointer; }
static inline int get_callback(int win, const char* name) { // get callback registry for active window lua_pushstring(LUA, CALLBACKS_NAME); lua_rawget(LUA, LUA_REGISTRYINDEX); lua_pushinteger(LUA, win); lua_rawget(LUA, -2); // get requested callback lua_pushstring(LUA, name); lua_rawget(LUA, -2); int is_function = lua_isfunction(LUA, -1); if (is_function) { // leave only function on stack lua_replace(LUA, -3); lua_pop(LUA, 1); } else { lua_pop(LUA, 3); } return is_function; }
LUA_API int luaS_rawnetobj(lua_State *L, int index) { int *ud; if (lua_istable(L, index)) { lua_pushvalue(L, index); while (lua_istable(L, -1)) { lua_pushstring(L, "__base"); lua_rawget(L, -2); lua_remove(L, -2); } if (lua_isuserdata(L, -1) > 0) lua_replace(L, index); else luaL_error(L, "arg %d expect object, but get a table", index); } ud = lua_touserdata(L, index); return (ud != NULL)?*ud:-1; }
void luaconf_freeElt(luaconf_elt *elt) { int top; lua_State *L; L = elt->L; top = lua_gettop(L); if (top == elt->pos) { // at top lua_pop(L, 1); while (lua_gettop(L) > 0 && lua_isnil(L, -1)) lua_pop(L, 1); } else { lua_pushnil(L); lua_replace(L, elt->pos); } free(elt); }
LUALIB_API int luaopen_package (lua_State *L) { int i; /* create new type _LOADLIB */ luaL_newmetatable(L, "_LOADLIB"); lua_pushcfunction(L, gctm); lua_setfield(L, -2, "__gc"); /* create `package' table */ luaL_register(L, LUA_LOADLIBNAME, pk_funcs); #if defined(LUA_COMPAT_LOADLIB) lua_getfield(L, -1, "loadlib"); lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); #endif lua_pushvalue(L, -1); lua_replace(L, LUA_ENVIRONINDEX); /* create `loaders' table */ lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); /* fill it with pre-defined loaders */ for (i=0; loaders[i] != nullptr; i++) { lua_pushcfunction(L, loaders[i]); lua_rawseti(L, -2, i+1); } lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ /* store config information */ lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" LUA_EXECDIR "\n" LUA_IGMARK); lua_setfield(L, -2, "config"); /* set field `loaded' */ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); lua_setfield(L, -2, "loaded"); /* set field `preload' */ lua_newtable(L); lua_setfield(L, -2, "preload"); lua_pushvalue(L, LUA_GLOBALSINDEX); luaL_register(L, nullptr, ll_funcs); /* open lib into global table */ lua_pop(L, 1); return 1; /* return 'package' table */ }
int mlua_index_with_properties (lua_State *L, const struct luaL_Reg *properties, bool use_cache) { char const * key; const struct luaL_Reg *reg; key = lua_tostring (L, 2); if (key == NULL) return 0; reg = mlua_find_method (properties, key); if (reg) { return mlua_get_property (L, reg, use_cache); } lua_getmetatable (L, 1); lua_replace (L, 1); lua_gettable (L, 1); return 1; }
static int fluent_Do(lua_State *L) { luaL_checktype(L, 1, LUA_TFUNCTION); lua_getfield(L, lua_upvalueindex(1), "Interval"); int interval = luaL_optinteger(L, -1, 0); lua_pop(L, 1); lua_getfield(L, lua_upvalueindex(1), "Timestamp"); time_t timestamp = lua_tointeger(L, -1); lua_pop(L, 1); time_t now = time(NULL); if (timestamp < now) { if (interval == 0) return 0; timestamp += (((now - timestamp) / interval) + 1) * interval; } lua_pushnil(L); lua_replace(L, lua_upvalueindex(1)); ScheduleTask *task = new ScheduleTask(); task->timestamp = timestamp; task->interval = interval; task->L = lua_newthread(L); task->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 1); task->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); { mir_cslock lock(threadLock); tasks.insert(task); } SetEvent(hScheduleEvent); return 0; }
static int magnet_env_next(lua_State *L) { server *srv; connection *con; int pos = lua_tointeger(L, lua_upvalueindex(1)); buffer *dest; lua_pushstring(L, "lighty.srv"); lua_gettable(L, LUA_REGISTRYINDEX); srv = lua_touserdata(L, -1); lua_pop(L, 1); lua_pushstring(L, "lighty.con"); lua_gettable(L, LUA_REGISTRYINDEX); con = lua_touserdata(L, -1); lua_pop(L, 1); lua_settop(L, 0); if (NULL == magnet_env[pos].name) return 0; /* end of list */ lua_pushstring(L, magnet_env[pos].name); dest = magnet_env_get_buffer_by_id(srv, con, magnet_env[pos].type); if (dest && dest->used) { lua_pushlstring(L, dest->ptr, dest->used - 1); } else { lua_pushnil(L); } /* Update our positional upval to reflect our new current position */ pos++; lua_pushinteger(L, pos); lua_replace(L, lua_upvalueindex(1)); /* Returning 2 items on the stack (key, value) */ return 2; }