int lua_next(lua_State* L, int index) { Value* table = GetValueForIndex(L, index); luai_apicheck(L, Value_GetIsTable(table) ); Value* key = GetValueForIndex(L, -1); const Value* value = Table_Next(table->table, key); if (value == NULL) { Pop(L, 1); return 0; } PushValue(L, value); return 1; }
Prototype* Function_CreatePrototype(lua_State* L, Function* function, String* source) { int convertedCodeSize = Prototype_GetConvertedCodeSize( function->code, function->codeSize ); Prototype* prototype = Prototype_Create( L, function->codeSize, convertedCodeSize, function->numConstants, function->numFunctions, function->numUpValues); PushPrototype(L, prototype); // Store the up values. memcpy(prototype->upValue, function->upValue, sizeof(String*) * function->numUpValues); // Store the code. memcpy(prototype->code, function->code, function->codeSize * sizeof(Instruction)); // Store the source information. prototype->source = source; Gc_IncrementReference(&L->gc, prototype, prototype->source); memcpy(prototype->sourceLine, function->sourceLine, function->codeSize * sizeof(int)); // Store the functions. for (int i = 0; i < function->numFunctions; ++i) { prototype->prototype[i] = Function_CreatePrototype(L, function->function[i], source); Gc_IncrementReference(&L->gc, prototype, prototype->prototype[i]); } // Store the constants. Value key; SetNil(&key); Table* constants = function->constants; const Value* value; while (value = Table_Next(L, constants, &key)) { int i; if (!Value_GetIsInteger(value, &i)) { ASSERT(0); } if (Value_GetIsTable(&key) && key.table == constants) { // The table itself is used to indicate nil values since nil values // cannot be saved in the table. SetNil(&prototype->constant[i]); } else { prototype->constant[i] = key; } Gc_IncrementReference(&L->gc, prototype, &prototype->constant[i]); } prototype->varArg = function->varArg; prototype->numParams = function->numParams; prototype->maxStackSize = function->maxStackSize; prototype->numUpValues = function->numUpValues; prototype->lineDefined = 0; prototype->lastLineDefined = 0; //PrintFunction(prototype); ASSERT( (L->stackTop - 1)->object == prototype ); Pop(L, 1); return prototype; }