Use object conversion macros consistently.

This commit is contained in:
Bob Nystrom
2013-11-10 14:21:14 -08:00
parent 915ecb4e58
commit 0339bcdeca
2 changed files with 11 additions and 9 deletions

View File

@ -321,7 +321,7 @@ static ObjClass* getClass(VM* vm, Value object)
{ {
switch (object->type) switch (object->type)
{ {
case OBJ_CLASS: return ((ObjClass*)object)->metaclass; case OBJ_CLASS: return AS_CLASS(object)->metaclass;
case OBJ_FALSE: case OBJ_FALSE:
case OBJ_TRUE: case OBJ_TRUE:
return vm->boolClass; return vm->boolClass;
@ -330,7 +330,7 @@ static ObjClass* getClass(VM* vm, Value object)
case OBJ_NULL: return vm->nullClass; case OBJ_NULL: return vm->nullClass;
case OBJ_NUM: return vm->numClass; case OBJ_NUM: return vm->numClass;
case OBJ_STRING: return vm->stringClass; case OBJ_STRING: return vm->stringClass;
case OBJ_INSTANCE: return ((ObjInstance*)object)->classObj; case OBJ_INSTANCE: return AS_INSTANCE(object)->classObj;
} }
} }
@ -470,14 +470,14 @@ Value interpret(VM* vm, ObjFn* fn)
if (result != NULL) if (result != NULL)
{ {
fiber.stack[fiber.stackSize - numArgs] = result; fiber.stack[fiber.stackSize - numArgs] = result;
// Discard the stack slots for the arguments (but leave one for // Discard the stack slots for the arguments (but leave one for
// the result). // the result).
fiber.stackSize -= numArgs - 1; fiber.stackSize -= numArgs - 1;
} }
break; break;
} }
case METHOD_BLOCK: case METHOD_BLOCK:
callFunction(&fiber, method->fn, numArgs); callFunction(&fiber, method->fn, numArgs);
break; break;
@ -568,11 +568,11 @@ void printValue(Value value)
break; break;
case OBJ_NUM: case OBJ_NUM:
printf("%g", ((ObjNum*)value)->value); printf("%g", AS_NUM(value));
break; break;
case OBJ_STRING: case OBJ_STRING:
printf("%s", ((ObjString*)value)->value); printf("%s", AS_STRING(value));
break; break;
case OBJ_TRUE: case OBJ_TRUE:
@ -583,7 +583,6 @@ void printValue(Value value)
Value primitive_metaclass_new(VM* vm, Fiber* fiber, Value* args) Value primitive_metaclass_new(VM* vm, Fiber* fiber, Value* args)
{ {
ObjClass* classObj = (ObjClass*)args[0];
// TODO(bob): Invoke initializer method. // TODO(bob): Invoke initializer method.
return (Value)makeInstance(classObj); return (Value)makeInstance(AS_CLASS(args[0]));
} }

View File

@ -15,6 +15,9 @@
// Get the function value of [obj] (0 or 1), which must be a function. // Get the function value of [obj] (0 or 1), which must be a function.
#define AS_FN(obj) ((ObjFn*)obj) #define AS_FN(obj) ((ObjFn*)obj)
// Get the double value of [obj], which must be a number.
#define AS_INSTANCE(obj) ((ObjInstance*)obj)
// Get the double value of [obj], which must be a number. // Get the double value of [obj], which must be a number.
#define AS_NUM(obj) (((ObjNum*)obj)->value) #define AS_NUM(obj) (((ObjNum*)obj)->value)
@ -168,7 +171,7 @@ typedef enum
// Pop [a] then [b] and push true if [b] is an instance of [a]. // Pop [a] then [b] and push true if [b] is an instance of [a].
CODE_IS, CODE_IS,
// The current block is done and should be exited. // The current block is done and should be exited.
CODE_END CODE_END
} Code; } Code;