static const char* FormatConstant(const Value* value, char buffer[64]) { if (Value_GetIsNumber(value)) { lua_number2str(buffer, value->number); } else if (Value_GetIsString(value)) { return String_GetData(value->string); } else if (Value_GetIsBoolean(value)) { strcpy(buffer, value->boolean ? "true" : "false"); } else if (Value_GetIsNil(value)) { strcpy(buffer, "nil"); } else { // Some other type of constant was stored that can't be saved into // the file. If this happens, it means we've introduced some new type // of constant but haven't handled it here. ASSERT(0); return "Unknown"; } return buffer; }
size_t lua_objlen(lua_State* L, int index) { Value* value = GetValueForIndex(L, index); if (Value_GetIsTable(value)) { return Table_GetSize(L, value->table); } else if (Value_GetIsUserData(value)) { return value->userData->size; } else if (Value_GetIsString(value) || (Value_GetIsNumber(value) && ToString(L, value))) { return value->string->length; } return 0; }
static void Output_Prototype( Output* output, Prototype* prototype ) { Output_WriteString( output, prototype->source ); Output_Write( output, prototype->lineDefined ); Output_Write( output, prototype->lastLineDefined ); Output_WriteByte( output, prototype->numUpValues ); Output_WriteByte( output, prototype->numParams ); Output_WriteByte( output, prototype->varArg ? 2 : 0 ); Output_WriteByte( output, prototype->maxStackSize ); Output_Write( output, prototype->codeSize ); Output_WriteBlock( output, prototype->code, prototype->codeSize * sizeof(Instruction) ); Output_Write( output, prototype->numConstants ); for (int i = 0; i < prototype->numConstants; ++i) { const Value* constant = &prototype->constant[i]; if (Value_GetIsNil(constant)) { Output_WriteByte( output, LUA_TNIL ); } else if (Value_GetIsString(constant)) { Output_WriteByte( output, LUA_TSTRING ); Output_WriteString( output, constant->string ); } else if (Value_GetIsNumber(constant)) { Output_WriteByte( output, LUA_TNUMBER ); Output_Write( output, constant->number ); } else if (Value_GetIsBoolean(constant)) { Output_WriteByte( output, LUA_TBOOLEAN ); Output_WriteByte( output, constant->boolean ); } else { // Unexpected constant type. ASSERT(0); } } Output_Write( output, prototype->numPrototypes ); for (int i = 0; i < prototype->numPrototypes; ++i) { Output_Prototype( output, prototype->prototype[i] ); } Output_Write( output, prototype->codeSize ); Output_WriteBlock( output, prototype->sourceLine, prototype->codeSize * sizeof(int) ); // List of locals (optional debug data) Output_Write( output, static_cast<int>(0) ); Output_Write( output, prototype->numUpValues ); for (int i = 0; i < prototype->numUpValues; ++i) { Output_WriteString( output, prototype->upValue[i] ); } }