static int compat53_pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ lua_pushvalue(L, LUA_GLOBALSINDEX); if (compat53_findfield(L, top + 1, 2)) { lua_copy(L, -1, top + 1); /* move name to proper place */ lua_pop(L, 2); /* remove pushed values */ return 1; } else { lua_settop(L, top); /* remove function and global table */ return 0; } }
/* ** Search for a name for a function in all loaded modules ** (registry._LOADED). */ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ lua_pushstring(L, name + 3); /* push name without prefix */ lua_remove(L, -2); /* remove original name */ } lua_copy(L, -1, top + 1); /* move name to proper place */ lua_pop(L, 2); /* remove pushed values */ return 1; } else { lua_settop(L, top); /* remove function and global table */ return 0; } }
void LuaRoot::changeCursor(Root * sender, Cursor cursor) { bool stop = false; notify_ ? notify_->changeCursor(this, cursor, stop) : 0; if (stop) return; //(self, cursor) auto l = LuaContext::State(); LUA_STACK_AUTO_CHECK(l); LuaContext::GetLOFromCO(l, this); lua_getfield(l, -1, kNotifyOrder); if (lua_istable(l, -1)) { lua_getfield(l, -1, kMethodChangeCursor); lua_pushnil(l); lua_copy(l, -4, -1); lua_pushinteger(l, (lua_Integer)cursor); LuaContext::SafeLOPCall(l, 2, 0); } lua_pop(l, 2); }
static void buffer_push_metapairs(lua_State *L, struct buffer *b, int idx, int depth) { uint8_t type = TYPE_TABLE; lua_Integer v = 0; buffer_push(b, (char *)&type, 1); buffer_push(b, (char *)&v, sizeof v); lua_pushvalue(L, idx); lua_call(L, 1, 3); for (;;) { lua_pushvalue(L, -2); lua_pushvalue(L, -2); lua_copy(L, -5, -3); lua_call(L, 2, 2); if (lua_type(L, -2) == LUA_TNIL) { lua_pop(L, 4); break; } lpack_one(L, b, -2, depth); lpack_one(L, b, -1, depth); lua_pop(L, 1); } buffer_push_nil(b); }
static void wb_table_metapairs(lua_State *L, struct write_block *wb, int index, int depth) { uint8_t n = COMBINE_TYPE(TYPE_TABLE, 0); wb_push(wb, &n, 1); lua_pushvalue(L, index); lua_call(L, 1, 3); for(;;) { lua_pushvalue(L, -2); lua_pushvalue(L, -2); lua_copy(L, -5, -3); lua_call(L, 2, 2); int type = lua_type(L, -2); if (type == LUA_TNIL) { lua_pop(L, 4); break; } pack_one(L, wb, -2, depth); pack_one(L, wb, -1, depth); lua_pop(L, 1); } wb_nil(wb); }
void LuaRoot::requestFocus(Root * sender) { bool stop = false; notify_ ? notify_->requestFocus(this, stop) : 0; if (stop) return; //post to script //(self) auto l = LuaContext::State(); LUA_STACK_AUTO_CHECK(l); LuaContext::GetLOFromCO(l, this); lua_getfield(l, -1, kNotifyOrder); if (lua_istable(l, -1)) { lua_getfield(l, -1, kMethodRequestFocus); lua_pushnil(l); lua_copy(l, -4, -1); LuaContext::SafeLOPCall(l, 1, 0); } lua_pop(l, 2); }
// Called in protected mode. static int _ActorObjectCreate(lua_State *L) { // Stack is lightuserdata lua_newtable(L); lua_pushstring(L, "__cstate"); lua_pushvalue(L, -3); lua_settable(L, -3); // Stack is lightuserdata, newtable lua_copy(L, -1, -2); lua_pop(L, 1); // Stack is newtable // Create associated mailbox lua_pushstring(L, "__mbox"); lua_pushcfunction(L, MBoxCreateInternal); lua_pcall(L, 0, 1, 0); lua_settable(L, -3); // Set its metatable lua_pushstring(L, RIDX_ACTORLIB_METATABLE); lua_gettable(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); return 1; }
int main(void) { char buf[BUFSIZ]; lua_State *L = luaL_newstate(); lua_pushstring(L, "hello"); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "hello ") == 0); lua_pushstring(L, "world"); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "hello world ") == 0); lua_pushnumber(L, 42); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "hello world 42 ") == 0); lua_pushnumber(L, 1327); lua_pushnumber(L, 42); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "hello world 42 1327 42 ") == 0); lua_insert(L, 1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello world 42 1327 ") == 0); lua_insert(L, 2); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 1327 hello world 42 ") == 0); lua_remove(L, 1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "1327 hello world 42 ") == 0); lua_replace(L, 1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello world ") == 0); lua_copy(L, 1, 3); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello 42 ") == 0); lua_settop(L, -1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello 42 ") == 0); lua_settop(L, -2); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello ") == 0); lua_insert(L, -1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello ") == 0); lua_pushvalue(L, 1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello 42 ") == 0); lua_pushvalue(L, -1); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello 42 42 ") == 0); lua_pushvalue(L, 2); memset(buf, 0, BUFSIZ); assert(strcmp(dumpstack(L, buf), "42 hello 42 42 hello ") == 0); return 0; }
static int ComposeQueryString(lua_State *L) { int length; int i; const char *ss; // [query_string_table] lua_pushstring(L, ""); // [query_string_table, 'query_string'] lua_pushnil(L); // [query_string_table, 'query_string', nil] while (lua_next(L, 1) != 0) { // [query_string_table, 'query_string', name, value_array_or_nil] if (!lua_istable(L, -1)) { // [query_string_table, 'query_string', name, nil] lua_pop(L, 1); // [query_string_table, 'query_string', name] if (luax_len(L, 2) > 0) { lua_pushvalue(L, 2); // [query_string_table, 'query_string', name, 'query_string'] lua_pushstring(L, "&"); // [query_string_table, 'query_string', name, 'query_string', '&'] lua_pushvalue(L, 3); // [query_string_table, 'query_string', name, 'query_string', '&', name] lua_concat(L, 3); // [query_string_table, 'query_string', name, 'query_string+name'] } else { // [query_string_table, '', name] lua_pushvalue(L, -1); // [query_string_table, '', name, 'name'] } } else { // Expect name and value_varray on stack. asplite_ArrayToQueryStringPairs(L, lua_gettop(L) - 2); // [query_string_table, 'query_string', name, value_array, 'name=value&name=value...'] lua_copy(L, 2, -2); // [query_string_table, 'query_string', name, 'query_string', 'name=value&name=value...'] if (luax_len(L, -2) > 0) { lua_pushstring(L, "&"); // [query_string_table, 'query_string', name, // 'query_string', 'name=value&name=value...', '&'] lua_insert(L, -2); // [query_string_table, 'query_string', name, // 'query_string', '&', 'name=value&name=value...'] lua_concat(L, 3); } else { // [query_string_table, '', name, '', 'name=value&name=value...'] lua_concat(L, 2); } } // [query_string_table, '', name, 'name=value&name=value...'] lua_replace(L, 2); } // [query_string_table, 'query_string'] return 1; }