From 0e2a09c50cdedd234dd801f38b519c3ea435b473 Mon Sep 17 00:00:00 2001 From: Fabrizio Pietrucci Date: Tue, 18 Jul 2023 22:28:44 +0200 Subject: [PATCH] Refactoring --- src/gc.c | 15 +-------------- src/vm.c | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/gc.c b/src/gc.c index b2b3b5e9..ac090ea2 100644 --- a/src/gc.c +++ b/src/gc.c @@ -215,14 +215,12 @@ void garbageCollect(JStarVM* vm) { puts("*--- Starting GC ---*"); #endif - // init reached object stack vm->reachedStack = malloc(sizeof(Obj*) * REACHED_DEFAULT_SZ); vm->reachedCapacity = REACHED_DEFAULT_SZ; { PROFILE("{reach-objects}::garbageCollect") - // reach builtin classes reachObject(vm, (Obj*)vm->clsClass); reachObject(vm, (Obj*)vm->objClass); reachObject(vm, (Obj*)vm->strClass); @@ -238,52 +236,41 @@ void garbageCollect(JStarVM* vm) { reachObject(vm, (Obj*)vm->tableClass); reachObject(vm, (Obj*)vm->udataClass); - // reach script argument llist reachObject(vm, (Obj*)vm->argv); for(int i = 0; i < SYM_END; i++) { reachObject(vm, (Obj*)vm->methodSyms[i]); } - // reach empty Tuple singleton reachObject(vm, (Obj*)vm->emptyTup); - - // reach loaded modules reachHashTable(vm, &vm->modules); - // reach elements on the stack for(Value* v = vm->stack; v < vm->sp; v++) { reachValue(vm, *v); } - // reach elements on the frame stack for(int i = 0; i < vm->frameCount; i++) { reachObject(vm, vm->frames[i].fn); } - // reach open upvalues for(ObjUpvalue* upvalue = vm->upvalues; upvalue != NULL; upvalue = upvalue->next) { reachObject(vm, (Obj*)upvalue); } - // reach the compiler objects reachCompilerRoots(vm, vm->currCompiler); } { PROFILE("{recursively-reach}::garbageCollect") - // recursevely reach objects held by other reached objects while(vm->reachedCount != 0) { recursevelyReach(vm, vm->reachedStack[--vm->reachedCount]); } } - // free unreached objects sweepStrings(&vm->stringPool); sweepObjects(vm); - // free the reached objects stack free(vm->reachedStack); vm->reachedStack = NULL; vm->reachedCapacity = 0; @@ -299,4 +286,4 @@ void garbageCollect(JStarVM* vm) { prevAlloc, vm->allocated, curr, vm->nextGC); printf("*--- End of GC ---*\n"); #endif -} \ No newline at end of file +} diff --git a/src/vm.c b/src/vm.c index d8756e92..00ba6578 100644 --- a/src/vm.c +++ b/src/vm.c @@ -5,6 +5,7 @@ #include "code.h" #include "gc.h" +#include "hashtable.h" #include "import.h" #include "lib/builtins.h" #include "lib/core/core.h" @@ -709,41 +710,43 @@ bool getValueField(JStarVM* vm, ObjString* name) { if(IS_OBJ(val)) { switch(AS_OBJ(val)->type) { case OBJ_INST: { - Value field; ObjInstance* inst = AS_INSTANCE(val); - // Try top find a field - if(!hashTableGet(&inst->fields, name, &field)) { - // No field, try to bind method - if(!bindMethod(vm, inst->base.cls, name)) { - jsrRaise(vm, "FieldException", "Object %s doesn't have field `%s`.", - inst->base.cls->name->data, name->data); - return false; - } + // Try to find a field + Value field; + if(hashTableGet(&inst->fields, name, &field)) { + pop(vm); + push(vm, field); return true; } - pop(vm); - push(vm, field); + // No field, try to bind method + if(!bindMethod(vm, inst->base.cls, name)) { + jsrRaise(vm, "FieldException", "Object %s doesn't have field `%s`.", + inst->base.cls->name->data, name->data); + return false; + } + return true; } case OBJ_MODULE: { - Value global; ObjModule* mod = AS_MODULE(val); // Try to find global variable - if(!hashTableGet(&mod->globals, name, &global)) { - // No global, try to bind method - if(!bindMethod(vm, mod->base.cls, name)) { - jsrRaise(vm, "NameException", "Name `%s` is not defined in module %s", - name->data, mod->name->data); - return false; - } + Value global; + if(hashTableGet(&mod->globals, name, &global)) { + pop(vm); + push(vm, global); return true; } - pop(vm); - push(vm, global); + // No global, try to bind method + if(!bindMethod(vm, mod->base.cls, name)) { + jsrRaise(vm, "NameException", "Name `%s` is not defined in module %s", + name->data, mod->name->data); + return false; + } + return true; } default: