vars-as-objects system actually working! :O
authorcassowarii <2374677+cassowarii@users.noreply.github.com>
Sun, 19 Jul 2026 03:02:21 +0000 (20:02 -0700)
committercassowarii <2374677+cassowarii@users.noreply.github.com>
Sun, 19 Jul 2026 03:02:21 +0000 (20:02 -0700)
src/data/closure.c
src/data/closure.h
src/data/data.c
src/data/value.c
src/data/variable.c
src/data/variable.h
src/main.c
src/vm/exec.c

index 4029d9b..1908fea 100644 (file)
@@ -60,12 +60,12 @@ void sbClosure_set_var(hClosure which, usize index, hVar what) {
   }
 }
 
-hVal sbClosure_get_value(hClosure which, usize index) {
+hVal *sbClosure_get_value(hClosure which, usize index) {
   sbClosure *c = find_closure_by_handle(which);
   if (c->num_vars <= INLINE_VAR_COUNT) {
-    return sbVar_get_value(&c->internal_vars[index]);
+    return sbVar_get_value_ptr(&c->internal_vars[index]);
   } else {
-    return sbVar_get_value(&c->external_vars[index]);
+    return sbVar_get_value_ptr(&c->external_vars[index]);
   }
 }
 
index 1acfb8d..372450c 100644 (file)
@@ -10,6 +10,6 @@ void sbClosure_set_value(hClosure which, usize index, hVal *var);
 
 void sbClosure_set_var(hClosure which, usize index, hVar what);
 
-hVal sbClosure_get_value(hClosure which, usize index);
+hVal *sbClosure_get_value(hClosure which, usize index);
 
 hVar sbClosure_get_var(hClosure which, usize index);
index 013ed30..d6d3d35 100644 (file)
@@ -7,6 +7,7 @@
 #include "data/list.h"
 #include "data/reference.h"
 #include "data/closure.h"
+#include "data/variable.h"
 
 #include "lib/lib.h"
 
@@ -20,6 +21,7 @@ void data_sys_init() {
   sbInteger_sys_init();
   sbClosure_sys_init();
   sbList_sys_init();
+  sbVar_sys_init();
 
   sbLib_sys_init();
 }
@@ -27,6 +29,7 @@ void data_sys_init() {
 void data_sys_deinit() {
   sbLib_sys_deinit();
 
+  sbVar_sys_deinit();
   sbList_sys_deinit();
   sbClosure_sys_deinit();
   sbInteger_sys_deinit();
index 12d2887..baf4219 100644 (file)
@@ -95,7 +95,7 @@ void sbV_release(const hVal *a) {
     case IT_INTEGER:
       sbInteger_release(a->string);
       break;
-    case ITX_HEAPVAR:
+    case IT_REF:
       sbVar_release(sbVar_deref(a));
       break;
     default:
index a5d793a..d0e1d6c 100644 (file)
@@ -18,6 +18,10 @@ void sbVar_sys_init() {
   sbPool_initialize(&g_heapvar_pool, sizeof(HeapVar), VARS_PER_BLOCK);
 }
 
+void sbVar_sys_deinit() {
+  //sbPool_deinitialize(&g_heapvar_pool, sizeof(HeapVar), VARS_PER_BLOCK);
+}
+
 hVal sbVar_get_value(hVar v) {
   if (v->value.type == ITX_HEAPVAR) {
     return ((HeapVar*)v->value.ptrdata)->value;
@@ -77,7 +81,10 @@ hVar sbVar_deref(const hVal *v) {
 }
 
 sbVar sbVar_retain(hVar v) {
-  sbVar_move_to_heap(v);
+  if (v->value.type != ITX_HEAPVAR) {
+    sbVar_move_to_heap(v);
+  }
+
   ((HeapVar*)v->value.ptrdata)->gcinfo.refcount ++;
   return *v;
 }
index 2fab14d..99f5834 100644 (file)
@@ -7,6 +7,10 @@ typedef struct sbVar {
 
 typedef sbVar *hVar;
 
+void sbVar_sys_init();
+
+void sbVar_sys_deinit();
+
 hVal sbVar_get_value(hVar v);
 
 hVal *sbVar_get_value_ptr(hVar v);
index 143a06f..7cdf90b 100644 (file)
@@ -73,8 +73,8 @@ int main(int argc, char **argv) {
 
     if (vm.debugmode) {
       printf("Stack result: ");
-      for (hVal **p = (hVal**)vm.vstack; p < (hVal**)vm.vsp; p++) {
-        printf("%16llx %16llx ", (long long)(*p)->type, (long long)(*p)->data);
+      for (hVal *p = (hVal*)vm.vstack; p < (hVal*)vm.vsp; p++) {
+        printf("%16llx %16llx ", (long long)p->type, (long long)p->data);
       }
       printf("\n");
     }
index c20ffd0..9751632 100644 (file)
@@ -138,8 +138,8 @@ void sbVm_print_stack(hVm vm) {
 
 void print_stack(hVm vm) {
   debug("Stack: ");
-  for (hVal **p = (hVal**)vm->vstack; p < (hVal**)vm->vsp; p++) {
-    debug("%16llx %16llx ", (long long)(*p)->type, (long long)(*p)->data);
+  for (hVal *p = (hVal*)vm->vstack; p < (hVal*)vm->vsp; p++) {
+    debug("%16llx %16llx ", (long long)p->type, (long long)p->data);
   }
   debug("\n");
 }
@@ -180,6 +180,7 @@ void call_bound_method(hVm vm, hVal *to_call) {
 
   /* bound method is just a symbol + a closure containing one variable */
   hSymbol sym = (hSymbol)(to_call->type & ~IT_FLAG_BOUND_METHOD);
+  hVal *ref = sbVar_get_attached_ref(to_call);
 
   /* we should already have parameters, so we just need to line it back up
    * on the stack and call the method again */
@@ -189,7 +190,7 @@ void call_bound_method(hVm vm, hVal *to_call) {
     CHECK("bound method call should receive an integer arg count!");
   }
   peek_stack(vm, 0)->integer ++;
-  push_stack(vm, sbVar_get_attached_ref(to_call));
+  push_stack(vm, ref);
 
   /* resolve method normally */
   sbLib_resolve_method(vm);
@@ -222,21 +223,21 @@ void store_local(hVm vm, usize local_index, const hVal *value) {
 
 void push_stack(hVm vm, hVal *value) {
   *(hVal*)vm->vsp = *value;
-  vm->vsp += sizeof(hVal*);
+  vm->vsp += sizeof(hVal);
 }
 
 void push_stack_immediate(hVm vm, const hVal *value) {
   *(hVal*)vm->vsp = *value;
-  vm->vsp += sizeof(hVal*);
+  vm->vsp += sizeof(hVal);
 }
 
 hVal *pop_stack(hVm vm) {
-  vm->vsp -= sizeof(hVal*);
+  vm->vsp -= sizeof(hVal);
   return (hVal*)vm->vsp;
 }
 
 hVal *npop_stack(hVm vm, usize count) {
-  vm->vsp -= count * sizeof(hVal*);
+  vm->vsp -= count * sizeof(hVal);
   return (hVal*)vm->vsp;
 }
 
@@ -366,8 +367,8 @@ void execute_instruction(hVm vm) {
     case BC_LD_UPVAL:
       /* Hey, was there upval in there? */
       param = get_param(vm);
-      res = sbClosure_get_value(vm->fp->block_func.closure, param);
-      push_stack(vm, &res);
+      v = sbClosure_get_value(vm->fp->block_func.closure, param);
+      push_stack(vm, v);
       break;
     case BC_LD_UPREF:
       param = get_param(vm);
@@ -378,10 +379,10 @@ void execute_instruction(hVm vm) {
       push_stack_immediate(vm, &HVFUNC(param, 0));
       break;
     case BC_LD_TRUE:
-      push_stack_immediate(vm, &HVBOOL(1));
+      push_stack_immediate(vm, &HVBOOL(TRUE));
       break;
     case BC_LD_FALSE:
-      push_stack_immediate(vm, &HVBOOL(0));
+      push_stack_immediate(vm, &HVBOOL(FALSE));
       break;
     case BC_LD_NIL:
       push_stack_immediate(vm, &HVNIL);