Chapter 27.3
This commit is contained in:
parent
f1e60bd641
commit
f4185577e5
4 changed files with 38 additions and 0 deletions
|
@ -95,6 +95,12 @@ static void blackenObject(Obj *object) {
|
|||
markArray(&function->chunk.constants);
|
||||
break;
|
||||
}
|
||||
case OBJ_INSTANCE: {
|
||||
ObjInstance *instance = (ObjInstance *)object;
|
||||
markObject((Obj *)instance->klass);
|
||||
markTable(&instance->fields);
|
||||
break;
|
||||
}
|
||||
case OBJ_UPVALUE:
|
||||
markValue(((ObjUpvalue *)object)->closed);
|
||||
break;
|
||||
|
@ -126,6 +132,12 @@ static void freeObject(Obj *object) {
|
|||
FREE(ObjFunction, object);
|
||||
break;
|
||||
}
|
||||
case OBJ_INSTANCE: {
|
||||
ObjInstance *instance = (ObjInstance *)object;
|
||||
freeTable(&instance->fields);
|
||||
FREE(ObjInstance, object);
|
||||
break;
|
||||
}
|
||||
case OBJ_NATIVE:
|
||||
FREE(ObjNative, object);
|
||||
break;
|
||||
|
|
|
@ -53,6 +53,13 @@ ObjFunction *newFunction() {
|
|||
return function;
|
||||
}
|
||||
|
||||
ObjInstance *newInstance(ObjClass *klass) {
|
||||
ObjInstance *instance = ALLOCATE_OBJ(ObjInstance, OBJ_INSTANCE);
|
||||
instance->klass = klass;
|
||||
initTable(&instance->fields);
|
||||
return instance;
|
||||
}
|
||||
|
||||
ObjNative *newNative(NativeFn function) {
|
||||
ObjNative *native = ALLOCATE_OBJ(ObjNative, OBJ_NATIVE);
|
||||
native->function = function;
|
||||
|
@ -131,6 +138,9 @@ void printObject(Value value) {
|
|||
case OBJ_FUNCTION:
|
||||
printFunction(AS_FUNCTION(value));
|
||||
break;
|
||||
case OBJ_INSTANCE:
|
||||
printf("%s instance", AS_INSTANCE(value)->klass->name->chars);
|
||||
break;
|
||||
case OBJ_NATIVE:
|
||||
printf("<native fn>");
|
||||
break;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "chunk.h"
|
||||
#include "common.h"
|
||||
#include "table.h"
|
||||
#include "value.h"
|
||||
|
||||
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
|
||||
|
@ -10,12 +11,14 @@
|
|||
#define IS_CLASS(value) isObjType(value, OBJ_CLASS)
|
||||
#define IS_CLOSURE(value) isObjType(value, OBJ_CLOSURE)
|
||||
#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
|
||||
#define IS_INSTANCE(value) isObjType(value, OBJ_INSTANCE)
|
||||
#define IS_NATIVE(value) isObjType(value, OBJ_NATIVE)
|
||||
#define IS_STRING(value) isObjType(value, OBJ_STRING)
|
||||
|
||||
#define AS_CLASS(value) ((ObjClass *)AS_OBJ(value))
|
||||
#define AS_CLOSURE(value) ((ObjClosure *)AS_OBJ(value))
|
||||
#define AS_FUNCTION(value) ((ObjFunction *)AS_OBJ(value))
|
||||
#define AS_INSTANCE(value) ((ObjInstance *)AS_OBJ(value))
|
||||
#define AS_NATIVE(value) (((ObjNative *)AS_OBJ(value))->function)
|
||||
#define AS_STRING(value) ((ObjString *)AS_OBJ(value))
|
||||
#define AS_CSTRING(value) (((ObjString *)AS_OBJ(value))->chars)
|
||||
|
@ -24,6 +27,7 @@ typedef enum {
|
|||
OBJ_CLASS,
|
||||
OBJ_CLOSURE,
|
||||
OBJ_FUNCTION,
|
||||
OBJ_INSTANCE,
|
||||
OBJ_NATIVE,
|
||||
OBJ_STRING,
|
||||
OBJ_UPVALUE,
|
||||
|
@ -76,9 +80,16 @@ typedef struct {
|
|||
ObjString *name;
|
||||
} ObjClass;
|
||||
|
||||
typedef struct {
|
||||
Obj obj;
|
||||
ObjClass *klass;
|
||||
Table fields;
|
||||
} ObjInstance;
|
||||
|
||||
ObjClass *newClass(ObjString *name);
|
||||
ObjClosure *newClosure(ObjFunction *function);
|
||||
ObjFunction *newFunction();
|
||||
ObjInstance *newInstance(ObjClass *klass);
|
||||
ObjNative *newNative(NativeFn function);
|
||||
ObjString *takeString(char *chars, int length);
|
||||
ObjString *copyString(const char *chars, int length);
|
||||
|
|
|
@ -108,6 +108,11 @@ static bool call(ObjClosure *closure, int argCount) {
|
|||
static bool callValue(Value callee, int argCount) {
|
||||
if (IS_OBJ(callee)) {
|
||||
switch (OBJ_TYPE(callee)) {
|
||||
case OBJ_CLASS: {
|
||||
ObjClass *klass = AS_CLASS(callee);
|
||||
vm.stackTop[-argCount - 1] = OBJ_VAL(newInstance(klass));
|
||||
return true;
|
||||
}
|
||||
case OBJ_CLOSURE:
|
||||
return call(AS_CLOSURE(callee), argCount);
|
||||
case OBJ_NATIVE: {
|
||||
|
|
Loading…
Reference in a new issue