Put the class name on the stack instead of as an arg.

This commit is contained in:
Bob Nystrom 2014-01-29 07:47:09 -08:00
parent 5c2cf641ae
commit a5e65faf0d
3 changed files with 11 additions and 13 deletions

View File

@ -2420,7 +2420,7 @@ static void classDefinition(Compiler* compiler)
int nameConstant = addConstant(compiler, wrenNewString(compiler->parser->vm, int nameConstant = addConstant(compiler, wrenNewString(compiler->parser->vm,
compiler->parser->previous.start, compiler->parser->previous.length)); compiler->parser->previous.start, compiler->parser->previous.length));
wrenByteBufferClear(compiler->parser->vm, &compiler->parser->string); emitShort(compiler, CODE_CONSTANT, nameConstant);
// Load the superclass (if there is one). // Load the superclass (if there is one).
if (match(compiler, TOKEN_IS)) if (match(compiler, TOKEN_IS))
@ -2433,12 +2433,10 @@ static void classDefinition(Compiler* compiler)
emit(compiler, CODE_NULL); emit(compiler, CODE_NULL);
} }
emitShort(compiler, CODE_CLASS, nameConstant);
// Store a placeholder for the number of fields argument. We don't know // Store a placeholder for the number of fields argument. We don't know
// the value until we've compiled all the methods to see which fields are // the value until we've compiled all the methods to see which fields are
// used. // used.
int numFieldsInstruction = emit(compiler, 255); int numFieldsInstruction = emitByte(compiler, CODE_CLASS, 255);
// Store it in its name. // Store it in its name.
defineVariable(compiler, symbol); defineVariable(compiler, symbol);

View File

@ -971,9 +971,7 @@ static bool runInterpreter(WrenVM* vm)
CASE_CODE(CLASS): CASE_CODE(CLASS):
{ {
ObjString* name = AS_STRING(fn->constants[READ_SHORT()]); ObjString* name = AS_STRING(PEEK2());
int numFields = READ_BYTE();
ObjClass* superclass; ObjClass* superclass;
if (IS_NULL(PEEK())) if (IS_NULL(PEEK()))
@ -987,6 +985,7 @@ static bool runInterpreter(WrenVM* vm)
superclass = AS_CLASS(PEEK()); superclass = AS_CLASS(PEEK());
} }
int numFields = READ_BYTE();
ObjClass* classObj = wrenNewClass(vm, superclass, numFields, name); ObjClass* classObj = wrenNewClass(vm, superclass, numFields, name);
// Now that we know the total number of fields, make sure we don't // Now that we know the total number of fields, make sure we don't
@ -998,8 +997,9 @@ static bool runInterpreter(WrenVM* vm)
return false; return false;
} }
// Don't pop the superclass off the stack until the subclass is done // Don't pop the superclass and name off the stack until the subclass is
// being created, to make sure it doesn't get collected. // done being created, to make sure it doesn't get collected.
POP();
POP(); POP();
PUSH(OBJ_VAL(classObj)); PUSH(OBJ_VAL(classObj));

View File

@ -168,9 +168,9 @@ typedef enum
// Pushes the created closure. // Pushes the created closure.
CODE_CLOSURE, CODE_CLOSURE,
// TODO: Doc. // Creates a class. Top of stack is the superclass, or `null` if the class
// Define a new empty class and push it. Short [arg1] is a constant for the // inherits Object. Below that is a string for the name of the class. Byte
// name of the class. Byte [arg2] is the number of fields in the class. // [arg] is the number of fields in the class.
CODE_CLASS, CODE_CLASS,
// Define a method for symbol [arg]. The class receiving the method is popped // Define a method for symbol [arg]. The class receiving the method is popped