bool ScriptApiEntity::luaentity_Add(u16 id, const char *name, bool force_usage) { SCRIPTAPI_PRECHECKHEADER /* verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\"" <<name<<"\""<<std::endl; */ // Get core.registered_entities[name] lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_entities"); luaL_checktype(L, -1, LUA_TTABLE); lua_pushstring(L, name); lua_gettable(L, -2); // Should be a table, which we will use as a prototype //luaL_checktype(L, -1, LUA_TTABLE); if (lua_type(L, -1) != LUA_TTABLE){ infostream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl; return false; } int prototype_table = lua_gettop(L); //dump2(L, "prototype_table"); // Create entity object lua_newtable(L); int object = lua_gettop(L); // Set object metatable lua_pushvalue(L, prototype_table); lua_setmetatable(L, -2); // Add object reference // This should be userdata with metatable ObjectRef objectrefGet(L, id); luaL_checktype(L, -1, LUA_TUSERDATA); if (!luaL_checkudata(L, -1, "ObjectRef")) luaL_typerror(L, -1, "ObjectRef"); lua_setfield(L, -2, "object"); // core.luaentities[id] = object lua_getglobal(L, "core"); lua_getfield(L, -1, "luaentities"); luaL_checktype(L, -1, LUA_TTABLE); lua_pushnumber(L, id); // Push id lua_pushvalue(L, object); // Copy object to top of stack lua_settable(L, -3); return true; }
static uid_t mygetuid(lua_State *L, int i) { if (lua_isnone(L, i)) return -1; else if (lua_isnumber(L, i)) return (uid_t) lua_tonumber(L, i); else if (lua_isstring(L, i)) { struct passwd *p=getpwnam(lua_tostring(L, i)); return (p==NULL) ? -1 : p->pw_uid; } else return luaL_typerror(L, i, "string or number"); }
/** * \brief Ensures that a value in the stack is a host and returns it. * \param L a Lua state * \param index an index in the Lua stack * \return the C host corresponding to this Lua host */ msg_host_t sglua_check_host(lua_State * L, int index) { msg_host_t *pi, ht; luaL_checktype(L, index, LUA_TTABLE); lua_getfield(L, index, "__simgrid_host"); pi = (msg_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME); if (pi == NULL) luaL_typerror(L, index, HOST_MODULE_NAME); ht = *pi; if (!ht) luaL_error(L, "null Host"); lua_pop(L, 1); return ht; }
static gid_t mygetgid(lua_State *L, int i) { if (lua_isnone(L, i)) return -1; else if (lua_isnumber(L, i)) return (gid_t) lua_tonumber(L, i); else if (lua_isstring(L, i)) { struct group *g=getgrnam(lua_tostring(L, i)); return (g==NULL) ? -1 : g->gr_gid; } else return luaL_typerror(L, i, "string or number"); }
/** * Counts the attacks of a unit (__len metamethod). * - Arg 1: table containing the userdata containing the unit id. * - Ret 1: size of unit attacks vector. */ static int impl_unit_attacks_len(lua_State *L) { if(!lua_istable(L, 1)) { return luaL_typerror(L, 1, "unit attacks"); } lua_rawgeti(L, 1, 0); const unit* u = luaW_tounit(L, -1); const unit_type* ut = luaW_tounittype(L, -1); if(!u && !ut) { return luaL_argerror(L, 1, "unknown unit"); } lua_pushinteger(L, (u ? u->attacks() : ut->attacks()).size()); return 1; }
void *lutil_checkudatap (lua_State *L, int ud, const void *p) { void *up = lua_touserdata(L, ud); if (up != NULL) { /* value is a userdata? */ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ lutil_getmetatablep(L,p); /* get correct metatable */ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ lua_pop(L, 2); /* remove both metatables */ return up; } } } luaL_typerror(L, ud, p); /* else error */ return NULL; /* to avoid warnings */ }
LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { void *p = lua_touserdata(L, ud); if (p != NULL) { /* value is a userdata? */ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ lua_pop(L, 2); /* remove both metatables */ return p; } } } luaL_typerror(L, ud, tname); /* else error */ return NULL; /* to avoid warnings */ }
mrp_funcarray_t *mrp_funcarray_check(lua_State *L, int t) { mrp_funcarray_t *fa; size_t i, len; if (t < 0 && t > LUA_REGISTRYINDEX) t = lua_gettop(L) + t + 1; switch (lua_type(L, t)) { case LUA_TFUNCTION: fa = mrp_funcarray_create(L); fa->funcs = calloc(1, sizeof(mrp_funcbridge_t *)); fa->nfunc = 1; fa->funcs[0] = mrp_funcbridge_create_luafunc(L, t); break; case LUA_TTABLE: if (!(fa = to_funcarray(L, t))) { fa = mrp_funcarray_create(L); len = luaL_getn(L, t); fa->funcs = calloc(len, sizeof(mrp_funcbridge_t *)); fa->nfunc = len; for (i = 0; i < len; i++) { lua_pushnumber(L, (int)(i + 1)); lua_gettable(L, t); fa->funcs[i] = mrp_funcbridge_create_luafunc(L, -1); lua_pop(L, 1); } lua_replace(L, t); } break; default: luaL_typerror(L, t, "function array"); fa = NULL; break; } return fa; }
static int vmatrix_MatrixBuildScale (lua_State *L) { switch(lua_type(L, 2)) { case LUA_TNUMBER: default: MatrixBuildScale(luaL_checkvmatrix(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4)); break; case LUA_TUSERDATA: if (luaL_checkudata(L, 2, "Vector")) MatrixBuildScale(luaL_checkvmatrix(L, 1), luaL_checkvector(L, 2)); else luaL_typerror(L, 2, "Vector"); break; } return 0; }
/*static ALLUA_audio_stream toaudio_stream (lua_State *L, int index)//, int *gc_allowed) { struct ALLUA_audio_stream_s *pi = (struct ALLUA_audio_stream_s*)lua_touserdata(L, index); if (pi == NULL) luaL_typerror(L, index, AUDIO_STREAM_STRING); // if(gc_allowed) // *gc_allowed = pi->gc_allowed; return pi->audio_stream; } */ ALLUA_audio_stream allua_check_audio_stream(lua_State * L, int index /* int *gc_allowed */ ) { struct ALLUA_audio_stream_s *pi; ALLUA_audio_stream im; luaL_checktype(L, index, LUA_TUSERDATA); pi = (struct ALLUA_audio_stream_s *)luaL_checkudata(L, index, AUDIO_STREAM_STRING); if (pi == NULL) luaL_typerror(L, index, AUDIO_STREAM_STRING); im = pi->audio_stream; if (!im) luaL_error(L, "null audio_stream"); return im; }
static int ai_stopunit_select(lua_State *L, bool exec, bool remove_movement, bool remove_attacks) { int index = 1; if (false) { error_call_destructors: return luaL_typerror(L, index, "location (unit/integers)"); } int side = get_readonly_context(L).get_side(); map_location loc; if (!to_map_location(L, index, loc)) goto error_call_destructors; ai::stopunit_result_ptr stopunit_result = ai::actions::execute_stopunit_action(side,exec,loc,remove_movement,remove_attacks); return transform_ai_action(L,stopunit_result); }
int lua_Sprite_SetPosition(lua_State* state) { Sprite* sprite = (Sprite*)lua_touserdata(state, 1); if (sprite == NULL) { return luaL_typerror(state, 1, "Sprite"); } if(lua_isnumber(state, 2)) { double x = (double) luaL_optnumber(state, 2, sprite->GetPosition().x); double y = (double) luaL_optnumber(state, 3, sprite->GetPosition().y); sprite->SetPosition(x, y); } else { Vector* vector = (Vector*)lua_touserdata(state, 2); if (vector == NULL) { return luaL_typerror(state, 2, "Vector"); } sprite->SetPosition(*vector); } return 0; }
/*---------------------------------------------------------------------*/ static Linker_Intf * check_linker(lua_State *L, int index) { TRACE_LUA_FUNC_START(); Linker_Intf *linker; /* Retrieve linker */ luaL_checktype(L, index, LUA_TUSERDATA); linker = (Linker_Intf *)luaL_optudata(L, index); if (linker == NULL) luaL_typerror(L, index, "Brick"); TRACE_LUA_FUNC_END(); return linker; }
static int lb_rep(lua_State *L) { buffer *b = lb_checkbuffer(L, 1); size_t len = b->len; const char *str = NULL; int rep = 0; if (lua_type(L, 2) == LUA_TNUMBER) rep = lua_tointeger(L, 2); else if ((str = lb_tolstring(L, 2, &len)) != NULL) rep = luaL_checkint(L, 3); else luaL_typerror(L, 2, "number, buffer or string"); if (lb_realloc(L, b, len * (rep >= 0 ? rep : 0))) fill_str(b, 0, b->len, str != NULL ? str : b->str, len); lua_settop(L, 1); return 1; }
bool LuaUtil::ToBoolean(lua_State *L, int n, bool isOptional, bool defaultValue) { if (isOptional) { if (!lua_isboolean(L, n)) { return defaultValue; } else { return lua_toboolean(L, n); } } else { if (!lua_isboolean(L, n)) { luaL_typerror(L, n, lua_typename(L, LUA_TBOOLEAN)); return false; } return lua_toboolean(L, n); } }
static struct timer_list* lua_checktimer(lua_State *L, int index) { struct timer_list *timer = 0; luaL_checktype(L, index, LUA_TTABLE); if (!lua_getmetatable(L, index)) luaL_typerror(L, index, CRON_TIMER_METATABLE); lua_getfield(L, LUA_REGISTRYINDEX, CRON_TIMER_METATABLE); /* get correct metatable */ if( !lua_rawequal(L, -1, -2) ) luaL_typerror(L, index, CRON_TIMER_METATABLE); lua_pop(L, 2); lua_pushstring(L, "timer"); lua_rawget(L, index); if( lua_isnil(L, -1) ) luaL_typerror(L, index, CRON_TIMER_METATABLE); timer = (struct timer_list*)lua_touserdata(L, -1); lua_pop(L, 1); return timer; }
centity_t *lua_toentityraw(lua_State *L, int i) { centity_t *luaentity; luaL_checktype(L,i,LUA_TUSERDATA); luaentity = (centity_t *)luaL_checkudata(L, i, "Entity"); if (luaentity == NULL) luaL_typerror(L, i, "Entity"); if (luaentity->currentState.number == cg.predictedPlayerEntity.currentState.number) { return &cg.predictedPlayerEntity; } //CG_CalcEntityLerpPositions( luaentity ); return luaentity; }
/** * Checks that we have a watcher at watcher_i index by validating the * metatable has the is_watcher__ field set to the watcher magic light * userdata that is simply used to mark a metatable as being a * "watcher". * * [-0, +0, ?] */ static struct ev_watcher* check_watcher(lua_State *L, int watcher_i) { void *watcher = lua_touserdata(L, watcher_i); if ( watcher != NULL ) { /* Got a userdata? */ if ( lua_getmetatable(L, watcher_i) ) { /* got a metatable? */ lua_getfield(L, -1, "is_watcher__"); lua_pushlightuserdata(L, (void*)watcher_magic); if ( lua_rawequal(L, -1, -2) ) { lua_pop(L, 3); return (struct ev_watcher*)watcher; } } } luaL_typerror(L, watcher_i, "ev{io,timer,signal,idle}"); return NULL; }
/*static ALLUA_sample_instance tosample_instance (lua_State *L, int index)//, int *gc_allowed) { struct ALLUA_sample_instance_s *pi = (struct ALLUA_sample_instance_s*)lua_touserdata(L, index); if (pi == NULL) luaL_typerror(L, index, SAMPLE_INSTANCE_STRING); // if(gc_allowed) // *gc_allowed = pi->gc_allowed; return pi->sample_instance; } */ ALLUA_sample_instance allua_check_sample_instance(lua_State * L, int index /* int *gc_allowed */ ) { struct ALLUA_sample_instance_s *pi; ALLUA_sample_instance im; luaL_checktype(L, index, LUA_TUSERDATA); pi = (struct ALLUA_sample_instance_s *)luaL_checkudata(L, index, SAMPLE_INSTANCE_STRING); if (pi == NULL) luaL_typerror(L, index, SAMPLE_INSTANCE_STRING); im = pi->sample_instance; if (!im) luaL_error(L, "null sample_instance"); return im; }
static tcpsock *lua_checksock(lua_State *L, int index) { tcpsock **data, *sock; luaL_checktype(L, index, LUA_TUSERDATA); data = (tcpsock **)luaL_checkudata(L, index, "sock"); if(!data) { luaL_typerror(L, index, "sock"); } sock = *data; if(!sock) { luaL_error(L, "Null sock object"); } return sock; }
/** * @brief Implementation of sol.menu.start(). * @param l The Lua context that is calling this function. * @return Number of values to return to Lua. */ int LuaContext::menu_api_start(lua_State *l) { // Parameters: context table. if (lua_type(l, 1) != LUA_TTABLE && lua_type(l, 1) != LUA_TUSERDATA) { luaL_typerror(l, 1, "table or userdata"); } luaL_checktype(l, 2, LUA_TTABLE); lua_settop(l, 2); LuaContext& lua_context = get_lua_context(l); int menu_ref = lua_context.create_ref(); lua_context.add_menu(menu_ref, 1); return 0; }
int opt_linger(lua_State *L, p_socket ps) { struct linger li; /* obj, name, table */ if (!lua_istable(L, 3)) luaL_typerror(L, 3, lua_typename(L, LUA_TTABLE)); lua_pushstring(L, "on"); lua_gettable(L, 3); if (!lua_isboolean(L, -1)) luaL_argerror(L, 3, "boolean 'on' field expected"); li.l_onoff = lua_toboolean(L, -1); lua_pushstring(L, "timeout"); lua_gettable(L, 3); if (!lua_isnumber(L, -1)) luaL_argerror(L, 3, "number 'timeout' field expected"); li.l_linger = lua_tonumber(L, -1); return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); }
/** * @brief Implementation of sol.timer.start(). * @param l the Lua context that is calling this function * @return number of values to return to Lua */ int LuaContext::timer_api_start(lua_State *l) { // Parameters: [context] delay callback. LuaContext& lua_context = get_lua_context(l); if (lua_type(l, 1) != LUA_TNUMBER) { // The first parameter is the context. if (lua_type(l, 1) != LUA_TTABLE && lua_type(l, 1) != LUA_TUSERDATA) { luaL_typerror(l, 1, "table or userdata"); } } else { // No context specified: set a default context: // - during a game: the current map, // - outside a game: sol.main. Game* game = lua_context.get_main_loop().get_game(); if (game != NULL) { push_map(l, game->get_current_map()); } else { LuaContext::push_main(l); } lua_insert(l, 1); } // Now the first parameter is the context. uint32_t delay = uint32_t(luaL_checkint(l, 2)); luaL_checktype(l, 3, LUA_TFUNCTION); if (delay == 0) { // The delay is zero: call the function right now. lua_settop(l, 3); lua_context.call_function(0, 0, "callback"); lua_pushnil(l); } else { // Create the timer. Timer* timer = new Timer(delay); lua_context.add_timer(timer, 1, 3); push_timer(l, *timer); } return 1; }
int write_output(lua_State* lua) { static const char* default_type = "txt"; // static const char* default_name = ""; void* luserdata = lua_touserdata(lua, lua_upvalueindex(1)); if (NULL == luserdata) { luaL_error(lua, "write() invalid lightuserdata"); } lua_sandbox* lsb = (lua_sandbox*)luserdata; // void* ud = NULL; const char* type = default_type; // const char* name = default_name; switch (lua_gettop(lua)) { case 0: break; case 2: // name = luaL_checkstring(lua, 2); // fallthru case 1: switch (lua_type(lua, 1)) { case LUA_TSTRING: type = lua_tostring(lua, 1); if (strlen(type) == 0) type = default_type; break; case LUA_TTABLE: type = ""; if (lsb_output_protobuf(lsb, 1, 0) != 0) { luaL_error(lua, "write() cound not encode protobuf - %s", lsb_get_error(lsb)); } break; case LUA_TUSERDATA: type = lsb_output_userdata(lsb, 1, 0); break; default: luaL_typerror(lua, 1, "string, table, or circular_buffer"); break; } break; default: luaL_error(lua, "write() takes a maximum of 2 arguments"); break; } written_data = lsb_get_output(lsb, &written_data_len); return 0; }
/** * @brief Back-end for luaL_validjump. * * @param L Lua state to get jump from. * @param ind Index to check. * @param[out] offset How many Lua arguments were passed. * @param[out] sys System the jump exists in. * @return Jump found at the index in the state. * * @sa luaL_validjump */ static JumpPoint* luaL_validjumpSystem( lua_State *L, int ind, int *offset, StarSystem **outsys ) { LuaJump *lj; JumpPoint *jp; StarSystem *a, *b; /* Defaults. */ jp = NULL; a = NULL; b = NULL; if (lua_isjump(L, ind)) { lj = luaL_checkjump(L, ind); a = system_getIndex( lj->srcid ); b = system_getIndex( lj->destid ); if (offset != NULL) *offset = 1; } else if (lua_gettop(L) > 1) { if (lua_isstring(L, ind)) a = system_get( lua_tostring( L, ind )); else if (lua_issystem(L, ind)) a = system_getIndex( lua_tosystem(L, ind) ); if (lua_isstring(L, ind+1)) b = system_get( lua_tostring( L, ind+1 )); else if (lua_issystem(L, ind+1)) b = system_getIndex( lua_tosystem(L, ind+1) ); if (offset != NULL) *offset = 2; } else { luaL_typerror(L, ind, JUMP_METATABLE); return NULL; } if (b != NULL && a != NULL) jp = jump_getTarget( b, a ); if (jp == NULL) NLUA_ERROR(L, "Jump is invalid"); if (outsys != NULL) *outsys = a; return jp; }
static FUNC_UNUSED obj_udata *obj_udata_toobj(lua_State *L, int _index) { obj_udata *ud; size_t len; /* make sure it's a userdata value. */ ud = (obj_udata *)lua_touserdata(L, _index); if(ud == NULL) { luaL_typerror(L, _index, "userdata"); /* is not a userdata value. */ } /* verify userdata size. */ len = lua_objlen(L, _index); if(len != sizeof(obj_udata)) { /* This shouldn't be possible */ luaL_error(L, "invalid userdata size: size=%d, expected=%d", len, sizeof(obj_udata)); } return ud; }
/* Common handlers * */ shared_ptr<Shader> check_shader (lua_State *L, int index) { Shader_ud** pi; shared_ptr<Shader> im; luaL_checktype(L, index, LUA_TUSERDATA); if(luaL_getmetafield (L, index, "is_shader")) { pi = (Shader_ud**)lua_touserdata(L, index); return (*pi)->shader; } pi = (Shader_ud**)luaL_checkudata(L, index, SHADER_STRING); if (*pi == NULL) luaL_typerror(L, index, SHADER_STRING); return (*pi)->shader; }
static int lua_Sprite_SetRotation(lua_State* state) { Sprite* sprite = LuaState::GetFuncParam<Sprite>(state, 1); if(sprite == NULL) { return 0; } if(!lua_isnumber(state, 2)) { return luaL_typerror(state, 2, "number"); } double rotation = lua_tonumber(state, 2); sprite->SetRotation(rotation); return 0; }
static int Pgetpasswd(lua_State *L) /** getpasswd(name or id) */ { struct passwd *p=NULL; if (lua_isnoneornil(L, 1)) p = getpwuid(geteuid()); else if (lua_isnumber(L, 1)) p = getpwuid((uid_t)lua_tonumber(L, 1)); else if (lua_isstring(L, 1)) p = getpwnam(lua_tostring(L, 1)); else luaL_typerror(L, 1, "string or number"); if (p==NULL) lua_pushnil(L); else doselection(L, 2, Sgetpasswd, Fgetpasswd, p); return 1; }
/* Common handlers * */ shared_ptr<Scenenode> check_scenenode (lua_State *L, int index) { Scenenode_ud** pi; shared_ptr<Scenenode> im; luaL_checktype(L, index, LUA_TUSERDATA); if(luaL_getmetafield (L, index, "is_scenenode")) { pi = (Scenenode_ud**)lua_touserdata(L, index); return (*pi)->scenenode; } pi = (Scenenode_ud**)luaL_checkudata(L, index, SCENENODE_STRING); if (*pi == NULL) luaL_typerror(L, index, SCENENODE_STRING); return (*pi)->scenenode; }