int LuaGenerator::mesh(lua_State *L) { const char* interp = luaL_checkstring(L, 1); luaL_checktype(L, 2, LUA_TTABLE); luaL_checktype(L, 3, LUA_TTABLE); int nfaces = luaL_getn(L, 2); std::vector<int> nverts(nfaces); for (int i = 0; i < nfaces; ++i) { lua_rawgeti(L, 2, i + 1); nverts[i] = luaL_checkinteger(L, -1); lua_pop(L, 1); } std::vector<int> verts(luaL_getn(L, 3)); for (int i = 0; i < (int)verts.size(); ++i) { lua_rawgeti(L, 3, i + 1); verts[i] = luaL_checkinteger(L, -1); lua_pop(L, 1); } for (int i = 4; i < lua_gettop(L); i += 2) parameter(L, i); api_->mesh(interp, nfaces, &nverts[0], &verts[0]); clear(); return 0; }
int gr_joint_cmd(lua_State* L) { GRLUA_DEBUG_CALL; gr_node_ud* data = (gr_node_ud*)lua_newuserdata(L, sizeof(gr_node_ud)); data->node = 0; const char* name = luaL_checkstring(L, 1); JointNode* node = new JointNode(name); luaL_checktype(L, 2, LUA_TTABLE); luaL_argcheck(L, luaL_getn(L, 2) == 3, 2, "Three-tuple expected"); luaL_checktype(L, 3, LUA_TTABLE); luaL_argcheck(L, luaL_getn(L, 3) == 3, 3, "Three-tuple expected"); double x[3], y[3]; for (int i = 1; i <= 3; i++) { lua_rawgeti(L, 2, i); x[i - 1] = luaL_checknumber(L, -1); lua_rawgeti(L, 3, i); y[i - 1] = luaL_checknumber(L, -1); lua_pop(L, 2); } node->set_joint_x(x[0], x[1], x[2]); node->set_joint_y(y[0], y[1], y[2]); data->node = node; luaL_getmetatable(L, "gr.node"); lua_setmetatable(L, -2); return 1; }
int gr_mesh_cmd(lua_State* L) { GRLUA_DEBUG_CALL; gr_node_ud* data = (gr_node_ud*)lua_newuserdata(L, sizeof(gr_node_ud)); data->node = 0; const char* name = luaL_checkstring(L, 1); std::vector<Point3D> verts; std::vector< std::vector<int> > faces; luaL_checktype(L, 2, LUA_TTABLE); int vert_count = luaL_getn(L, 2); luaL_argcheck(L, vert_count >= 1, 2, "Tuple of vertices expected"); for (int i = 1; i <= vert_count; i++) { lua_rawgeti(L, 2, i); Point3D vertex; get_tuple(L, -1, &vertex[0], 3); verts.push_back(vertex); lua_pop(L, 1); } luaL_checktype(L, 3, LUA_TTABLE); int face_count = luaL_getn(L, 3); luaL_argcheck(L, face_count >= 1, 3, "Tuple of faces expected"); faces.resize(face_count); for (int i = 1; i <= face_count; i++) { lua_rawgeti(L, 3, i); luaL_checktype(L, -1, LUA_TTABLE); int index_count = luaL_getn(L, -1); luaL_argcheck(L, index_count >= 3, 3, "Tuple of indices expected"); faces[i - 1].resize(index_count); get_tuple(L, -1, &faces[i - 1][0], index_count); lua_pop(L, 1); } Mesh* mesh = new Mesh(verts, faces); GRLUA_DEBUG(*mesh); data->node = new GeometryNode(name, mesh); luaL_getmetatable(L, "gr.node"); lua_setmetatable(L, -2); return 1; }
static int tbl_matchkey(const char* name, int index) { int result, i; if (lua_isnumber(L, index)) return 0; if (!lua_istable(L, index)) { const char* key = lua_tostring(L, index); return matches(key, name); } /* If key is a table, scan for value */ for (i = 1; i <= luaL_getn(L, index); ++i) { lua_rawgeti(L, index, i); result = tbl_matchkey(name, -1); lua_pop(L, 1); if (result) return 1; } return 0; }
void LootGroup::readObject(lua_State* L) { LootLuaManager* luaManager = MainWindow::instance->getLootLuaManager(); description = LuaParser::getStringField(L, "description"); minimumLevel = LuaParser::getIntField(L, "minimumLevel"); maximumLevel = LuaParser::getIntField(L, "maximumLevel"); lua_pushstring(L, "lootItems"); lua_gettable(L, -2); int totalItems = luaL_getn(L, -1); for (int i = 1; i <= totalItems; ++i) { lua_rawgeti(L, -1, i); QString itemTemplate = LuaParser::getStringField(L, "itemTemplate"); int weight = LuaParser::getIntField(L, "weight"); if (!luaManager->itemTemplateExists(itemTemplate)) continue; QExplicitlySharedDataPointer<LootItemTemplate> lootItemTemplate = luaManager->getItemTemplate(itemTemplate); lootItemTemplate->registerLootGroup(groupName); addLootItemTemplate(itemTemplate, weight); lua_pop(L, 1); } lua_pop(L, 1); }
/// table.anyi(table, predicate) /// /// Iterates over the vector part of a *table* invoking a *predicate* function /// to check whether a index-value pair matches some specific criteria. /// Returns *true* if any match was found and *false* otherwise. /// /// The predicate has the following signature: *predicate(key, value)* and must /// return a boolean. /// /// Example usage: /// table.anyi( /// { /// ['a'] = 1, /// ['b'] = 2, /// ['c'] = 3 /// }, /// function (k, v) /// return v % 2 == 0 /// end /// ) /// --> true /// /// TODO: return the matched index as well OR remove in favor of findi() static int libE_anyi(lua_State* const L) { lxs_assert_stack_begin(L); luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); int len = luaL_getn(L, 1); for (int i = 1; i <= len; ++i) { lua_pushvalue(L, 2); lua_pushinteger(L, i); lua_rawgeti(L, 1, i); lua_call(L, 2, 1); if (lua_toboolean(L, -1) != 0) { lua_pushboolean(L, 1); lxs_assert_stack_end(L, 2); return 1; } lua_pop(L, 1); } lua_pushboolean(L, 0); lxs_assert_stack_end(L, 1); return 1; }
/** * Copy all command line arguments into the script-side _ARGV global, and * check for the presence of a /scripts=<path> argument to help locate * the manifest if needed. * \returns OKAY if successful. */ int process_arguments(lua_State* L, int argc, const char** argv) { int i; /* Copy all arguments in the _ARGV global */ lua_newtable(L); for (i = 1; i < argc; ++i) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, luaL_getn(L, -2) + 1); /* The /scripts option gets picked up here; used later to find the * manifest and scripts later if necessary */ if (strncmp(argv[i], "/scripts=", 9) == 0) { scripts_path = argv[i] + 9; } else if (strncmp(argv[i], "--scripts=", 10) == 0) { scripts_path = argv[i] + 10; } } lua_setglobal(L, "_ARGV"); return OKAY; }
static int __draw_poly ( lua_State *_l ) { int nargs = lua_gettop(_l); int i, num; ex_vec2f_t *verts; const ex_color4f_t *c; bool close; // check if the first arguments is table luaL_checktype(_l, 1, LUA_TTABLE); num = luaL_getn(_l,1); verts = (ex_vec2f_t *)ex_malloc( sizeof(ex_vec2f_t) * num ); for ( i = 0; i < num; ++i ) { lua_rawgeti(_l, 1, i+1); verts[i] = *ex_lua_tovec2f(_l,-1); lua_remove(_l, -1); } // c = &ex_color4f_white; close = true; if ( nargs == 2 ) { c = ex_lua_checkcolor4f(_l,2); } else if ( nargs == 3 ) { c = ex_lua_checkcolor4f(_l,2); close = lua_toboolean(_l,3); } // ex_draw_poly ( verts, num, c, close ); ex_free( verts ); return 0; }
real* popRealArray(lua_State* luaSt, int* outN) { real* arr; int i, n, table; table = lua_gettop(luaSt); luaL_checktype(luaSt, table, LUA_TTABLE); n = luaL_getn(luaSt, table); /* get size of table */ arr = (real*) mwMalloc(sizeof(real) * n); for (i = 0; i < n; ++i) { lua_rawgeti(luaSt, table, i + 1); /* push t[i] */ luaL_checktype(luaSt, -1, LUA_TNUMBER); arr[i] = lua_tonumber(luaSt, -1); lua_pop(luaSt, 1); } lua_pop(luaSt, 1); if (outN) *outN = n; return arr; }
int Lua_SendCANMessage(lua_State *L) { size_t args = lua_gettop(L); if (args >= 4) { CAN_msg msg; uint8_t channel = (uint8_t)lua_tointeger(L, 1); msg.addressValue = (unsigned int)lua_tointeger(L, 2); msg.isExtendedAddress = lua_tointeger(L, 3); int size = luaL_getn(L, 4); size_t timeout = args >= 5 ? lua_tointeger(L, 5) : DEFAULT_CAN_TIMEOUT; if (size <= CAN_MSG_SIZE) { for (int i = 1; i <= size; i++) { lua_pushnumber(L,i); lua_gettable(L, 4); int val = lua_tonumber(L, -1); msg.data[i - 1] = val; lua_pop(L, 1); } } msg.dataLength = size; int rc = CAN_tx_msg(channel, &msg, timeout); lua_pushinteger(L, rc); return 1; } return 0; }
static int luaB_unpack (lua_State *L) { if (lua_gettop(L)==1 && lua_isvector3(L,1)) { float x, y, z; lua_checkvector3(L, 1, &x, &y, &z); lua_pushnumber(L,x); lua_pushnumber(L,y); lua_pushnumber(L,z); return 3; } else if (lua_gettop(L)==1 && lua_isquat(L,1)) { float w, x, y, z; lua_checkquat(L, 1, &w, &x, &y, &z); lua_pushnumber(L,w); lua_pushnumber(L,x); lua_pushnumber(L,y); lua_pushnumber(L,z); return 4; } else { int i, e, n; luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 2, 1); e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); if (i > e) return 0; /* empty range */ n = e - i + 1; /* number of elements */ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ return luaL_error(L, "too many results to unpack"); lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ while (i++ < e) /* push arg[i + 1...e] */ lua_rawgeti(L, 1, i); return n; } }
/// table.alli(table, selector) /// /// Iterates over the vector part of a *table* invoking a *selector* function /// for each index-value pair, copying every pair that matches the selector /// into a new table and returning it. /// /// The selector has the following signature: *selector(key, value)* and must /// return a boolean. /// /// Usage example: /// table.alli( /// { 1, 2, 3 }, /// function (k, v) /// return v % 2 ~= 0 /// end /// ) /// --> { /// [1] = 1, /// [2] = 3 /// } static int libE_alli(lua_State* const L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); lua_settop(L, 2); int len = luaL_getn(L, 1); int j = 0; lua_createtable(L, max(0, len - 1), 0); for (int i = 1; i <= len; ++i) { lua_pushvalue(L, 2); lua_pushinteger(L, i); lua_rawgeti(L, 1, i); lua_call(L, 2, 1); if (lua_toboolean(L, -1) != 0) { lua_rawgeti(L, 1, i); lua_rawseti(L, 3, ++j); } lua_pop(L, 1); } lxs_assert_stack_at(L, 3); return 1; }
int LuaShrunkDefaultLibraries::luaB_unpack( lua_State* L ) { int i = 0; int e = 0; int n = 0; luaL_checktype(L,1,LUA_TTABLE); i = luaL_optint(L,2,1); e = luaL_opt(L,luaL_checkint,3,luaL_getn(L,1)); if ( i > e) return 0; /* empty range */ n = e - i + 1; /* number of elements */ if ( n <= 0 || !lua_checkstack(L,n) ) /* n <= 0 means arith. overflow */ return luaL_error(L,"too many results to unpack"); lua_rawgeti(L,1,i); /* push arg[i] (avoiding overflow problems) */ while ( i++ < e ) /* push arg[i + 1...e] */ lua_rawgeti(L,1,i); return n; }
/// table.sum(table, selector) /// /// Iterates over the vector part of a *table* summing up values using a /// *selector* function. /// For empty tables nil is returned. /// /// The selector has the following signature: *selector(index, value)* and must /// return the number to sum up. /// /// Usage example: /// table.sumi( /// { 1, 2, 3 }, /// function (k, v) /// return v % 2 ~= 0 and v or 0 /// end /// ) /// --> 4 static int libE_sumi(lua_State* const L) { lxs_assert_stack_begin(L); luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); double sum = 0; int len = luaL_getn(L, 1); for (int i = 1; i <= len; ++i) { lua_pushvalue(L, 2); lua_pushinteger(L, i); lua_rawgeti(L, 1, i); lua_call(L, 2, 1); sum += (luaL_checktype(L, -1, LUA_TNUMBER), lua_tonumber(L, -1)); lua_pop(L, 1); } if (len == 0) lua_pushnil(L); else lua_pushnumber(L, sum); lxs_assert_stack_end(L, 1); return 1; }
static int evaluateIntegralAreas(lua_State* luaSt) { int i, table; lua_getglobal(luaSt, AREAS_NAME); table = lua_gettop(luaSt); mw_lua_checktable(luaSt, table); _nCut = luaL_getn(luaSt, table); if (_nCut == 0) { lua_pop(luaSt, 1); return luaL_error(luaSt, "At least one cut required"); } _ias = mwMallocA(_nCut * sizeof(IntegralArea)); for (i = 0; i < (int) _nCut; ++i) { lua_rawgeti(luaSt, table, i + 1); readIntegralArea(luaSt, &_ias[i], lua_gettop(luaSt)); lua_pop(luaSt, 1); } lua_pop(luaSt, 1); return 0; }
static int evaluateStreams(lua_State* luaSt) { int table; int i, n; lua_getglobal(luaSt, STREAMS_NAME); table = lua_gettop(luaSt); if (expectTable(luaSt, table)) luaL_error(luaSt, "Expected '%s' to be a table", STREAMS_NAME); n = luaL_getn(luaSt, table); /* CHECKME: Is this valid? */ if (n == 0) { lua_pop(luaSt, 1); return 0; } _streams->number_streams = n; _streams->parameters = mwMallocA(n * sizeof(StreamParameters)); for (i = 0; i < n; ++i) { lua_rawgeti(luaSt, table, i + 1); readStreamTable(luaSt, &_streams->parameters[i], lua_gettop(luaSt)); lua_pop(luaSt, 1); } lua_pop(luaSt, 1); return 0; }
static const char* tbl_getstringi_worker(int arr, int* index) { int i; const char* result = NULL; lua_getref(L, arr); for (i = 1; i <= luaL_getn(L, -1) && *index > 0; ++i) { lua_rawgeti(L, -1, i); if (lua_istable(L, -1)) { int ref = lua_ref(L, -1); result = tbl_getstringi_worker(ref, index); } else if (*index == 1) { result = lua_tostring(L, -1); lua_pop(L, 1); (*index)--; } else { lua_pop(L, 1); (*index)--; } } lua_pop(L, 1); return result; }
static void thread_prog(cyg_addrword_t data) { lua_State *L = (lua_State *) data; lua_ecos_state_t *state = (lua_ecos_state_t *) lua_getuserspace(L); int i, n, ref; // get arguments table size n = luaL_getn(L, 1); // push arguments from table for (i = 1; i <= n; i++) lua_rawgeti(L, 1, i); lua_remove(L, 1); // run program lua_call(L, n, 0); ref = state->thread->ref; // put ourselves into terminated list cyg_scheduler_lock(); { state->thread->next = terminated_threads; terminated_threads = state->thread; state->thread = NULL; } cyg_scheduler_unlock(); // unreference the lua thread lua_unref(L, ref); }
LUA_API int LglutInit(lua_State *L) { int argc, i; char **argv; assert(L == ref_L); if ( (lua_gettop(L) == 0) || lua_isnil(L, -1)) { argc = 0; argv = NULL; } else { luaL_checktype(L, -1, LUA_TTABLE); argc = (int) luaL_getn(L, -1) + 1; argv = (char **) malloc( (argc) * sizeof(char *) ); for (i = 0; i < argc; ++i) { lua_rawgeti(L, -1, i); if (lua_type(L, -1) == LUA_TSTRING) { argv[i] = (char *) malloc(lua_strlen(L, -1) + 1); strcpy(argv[i], lua_tostring(L, -1)); } else { argv[i] = (char *) malloc(1); argv[i][0] = '\0'; } lua_pop(L, 1); } } glutInit(&argc, argv); return 0; // small memory leak, but it is ok for now // will deal with this later }
mrp_lua_strarray_t *mrp_lua_check_strarray(lua_State *L, int t) { size_t len; size_t size; mrp_lua_strarray_t *arr; size_t i; luaL_checktype(L, t, LUA_TTABLE); len = luaL_getn(L, t); size = sizeof(mrp_lua_strarray_t) + sizeof(const char *) * (len + 1); if (!(arr = malloc(size))) luaL_error(L, "can't allocate %d byte long memory", size); arr->nstring = len; lua_pushvalue(L, t); for (i = 0; i < len; i++) { lua_pushnumber(L, (int)(i+1)); lua_gettable(L, -2); arr->strings[i] = strdup(luaL_checklstring(L, -1, NULL)); lua_pop(L, 1); } arr->strings[i] = NULL; lua_pop(L, 1); return arr; }
void cLuaChunkStay::AddChunkCoord(cLuaState & L, int a_Index) { // Check that the element has 2 coords: int NumCoords = luaL_getn(L, -1); if (NumCoords != 2) { LOGWARNING("%s: Element #%d doesn't contain 2 coords (got %d). Ignoring the element.", __FUNCTION__, a_Index, NumCoords ); return; } // Read the two coords from the element: lua_rawgeti(L, -1, 1); lua_rawgeti(L, -2, 2); int ChunkX = luaL_checkint(L, -2); int ChunkZ = luaL_checkint(L, -1); lua_pop(L, 2); // Check that a coord is not yet present: for (cChunkCoordsVector::iterator itr = m_Chunks.begin(), end = m_Chunks.end(); itr != end; ++itr) { if ((itr->m_ChunkX == ChunkX) && (itr->m_ChunkZ == ChunkZ)) { LOGWARNING("%s: Element #%d is a duplicate, ignoring it.", __FUNCTION__, a_Index ); return; } } // for itr - m_Chunks[] m_Chunks.push_back(cChunkCoords(ChunkX, ZERO_CHUNK_Y, ChunkZ)); }
void rpmluaSetVar(rpmlua _lua, rpmluav var) { INITSTATE(_lua, lua); lua_State *L = lua->L; if (var->listmode && lua->pushsize > 0) { if (var->keyType != RPMLUAV_NUMBER || var->key.num == (double)0) { var->keyType = RPMLUAV_NUMBER; var->key.num = (double) luaL_getn(L, -1); } var->key.num++; } if (!var->listmode || lua->pushsize > 0) { #if defined(LUA_GLOBALSINDEX) if (lua->pushsize == 0) lua_pushvalue(L, LUA_GLOBALSINDEX); #endif if (pushvar(L, var->keyType, &var->key) != -1) { if (pushvar(L, var->valueType, &var->value) != -1) lua_rawset(L, -3); else lua_pop(L, 1); } if (lua->pushsize == 0) lua_pop(L, 1); } }
/** ** Change unit owner ** ** @param l Lua state. */ static int CclChangeUnitsOwner(lua_State *l) { CUnit *table[UnitMax]; int n; int oldp; int newp; int x1; int y1; int x2; int y2; LuaCheckArgs(l, 4); if (!lua_istable(l, 1) || !lua_istable(l, 2)) { LuaError(l, "incorrect argument"); } if (luaL_getn(l, 1) != 2) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, 1, 1); x1 = LuaToNumber(l, -1); lua_pop(l, 1); lua_rawgeti(l, 1, 1); y1 = LuaToNumber(l, -1); lua_pop(l, 1); if (luaL_getn(l, 2) != 2) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, 2, 1); x2 = LuaToNumber(l, -1); lua_pop(l, 1); lua_rawgeti(l, 2, 1); y2 = LuaToNumber(l, -1); lua_pop(l, 1); n = UnitCacheSelect(x1, y1, x2 + 1, y2 + 1, table); oldp = LuaToNumber(l, 3); newp = LuaToNumber(l, 4); while (n) { if (table[n - 1]->Player->Index == oldp) { table[n - 1]->ChangeOwner(&Players[newp]); } --n; } return 0; }
static void add_extra (lua_State* L, char* value) { int len; lua_getglobal(L, "_extra_parameters"); len = luaL_getn(L, -1); lua_pushstring(L, value); lua_rawseti(L, -2, len+1); lua_pop(L, 1); };
static int LuaObject_length(LuaObject *obj) { int len; lua_rawgeti(L, LUA_REGISTRYINDEX, ((LuaObject*)obj)->ref); len = luaL_getn(L, -1); lua_settop(L, 0); return len; }
static int tbl_getlen(int tbl) { int size; lua_getref(L, tbl); size = luaL_getn(L, -1); lua_pop(L, 1); return size; }
static int luaB_unpack (lua_State *L) { int n, i; luaL_checktype(L, 1, LUA_TTABLE); n = luaL_getn(L, 1); luaL_checkstack(L, n, "table too big to unpack"); for (i=1; i<=n; i++) /* push arg[1...n] */ lua_rawgeti(L, 1, i); return n; }
/** ** Define player colors ** ** @param l Lua state. */ static int CclDefinePlayerColors(lua_State *l) { int i; int args; int j; int numcolors; LuaCheckArgs(l, 1); if (!lua_istable(l, 1)) { LuaError(l, "incorrect argument"); } args = luaL_getn(l, 1); for (i = 0; i < args; ++i) { lua_rawgeti(l, 1, i + 1); delete[] PlayerColorNames[i / 2]; PlayerColorNames[i / 2] = new_strdup(LuaToString(l, -1)); lua_pop(l, 1); ++i; lua_rawgeti(l, 1, i + 1); if (!lua_istable(l, -1)) { LuaError(l, "incorrect argument"); } numcolors = luaL_getn(l, -1); if (numcolors != PlayerColorIndexCount) { LuaError(l, "You should use %d colors (See DefinePlayerColorIndex())" _C_ PlayerColorIndexCount); } for (j = 0; j < numcolors; ++j) { lua_rawgeti(l, -1, j + 1); if (!lua_istable(l, -1) || luaL_getn(l, -1) != 3) { LuaError(l, "incorrect argument"); } lua_rawgeti(l, -1, 1); lua_rawgeti(l, -2, 2); lua_rawgeti(l, -3, 3); PlayerColorsRGB[i / 2][j].r = LuaToNumber(l, -3); PlayerColorsRGB[i / 2][j].g = LuaToNumber(l, -2); PlayerColorsRGB[i / 2][j].b = LuaToNumber(l, -1); lua_pop(l, 3 + 1); } } return 0; }
int CScriptTable::Count() { CHECK_STACK(L); PushRef(); int count = luaL_getn(L,-1); lua_pop(L, 1); return count; }
void get_tuple(lua_State* L, int arg, T* data, int n) { luaL_checktype(L, arg, LUA_TTABLE); luaL_argcheck(L, luaL_getn(L, arg) == n, arg, "N-tuple expected"); for (int i = 1; i <= n; i++) { lua_rawgeti(L, arg, i); data[i - 1] = luaL_checknumber(L, -1); lua_pop(L, 1); } }