Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bamless committed Jul 18, 2023
1 parent eea2702 commit 0e2a09c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
15 changes: 1 addition & 14 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -299,4 +286,4 @@ void garbageCollect(JStarVM* vm) {
prevAlloc, vm->allocated, curr, vm->nextGC);
printf("*--- End of GC ---*\n");
#endif
}
}
45 changes: 24 additions & 21 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0e2a09c

Please sign in to comment.