table.insert and basics threads api

This commit is contained in:
Mid
2025-09-05 21:15:44 +03:00
parent 8cd83188ad
commit 5d6734be4a
3 changed files with 96 additions and 24 deletions

21
vm.c
View File

@@ -7,9 +7,9 @@
#include<math.h>
#include<malloc.h>
static size_t lvm_run_internal(LVM *L, LFunc *func, size_t arg_count, set_LValueU *heap, LRegSet *regset) {
size_t lvm_call(LVM *L, LFunc *func, size_t arg_count, set_LValueU *heap, LRegSet *regset) {
if(func->is_native) {
return func->native_func(L, func->ud, arg_count, regset);
return func->native_func(L, func->ud, arg_count, heap, regset);
}
static const void *dispatch_table[] = {
@@ -38,6 +38,7 @@ static size_t lvm_run_internal(LVM *L, LFunc *func, size_t arg_count, set_LValue
[L_COND_NEQ] = &&do_cond_neq,
[L_SETFIELD] = &&do_setfield,
[L_GETFIELD] = &&do_getfield,
[L_LEN] = &&do_len,
};
LUnit *unit = func->unit;
@@ -248,7 +249,7 @@ do_call:;
for(int i = 0; i < arg_count; i++) {
regset2.regs[i] = regset->regs[args[i]];
}
size_t returned_count = lvm_run_internal(L, (LFunc*) (regset->regs[inst->a].u & ~LTAG_MASK), arg_count, heap, &regset2);
size_t returned_count = lvm_call(L, (LFunc*) (regset->regs[inst->a].u & ~LTAG_MASK), arg_count, heap, &regset2);
if(returned_count) {
// TODO: more than 1 return
@@ -286,7 +287,7 @@ do_setfield:;
goto err;
}
if(lvalue_tag(regset->regs[inst->b]) == LTAG_NIL) {
if(regset->regs[inst->b].u == LTAG_NIL) {
goto err;
}
@@ -307,6 +308,14 @@ do_getfield:;
regset->regs[inst->a] = ltable_get(tbl, regset->regs[inst->c]);
}
DISPATCH();
do_len:
if(lvalue_tag(regset->regs[inst->b]) == LTAG_STRING) {
regset->regs[inst->a] = lvalue_from_int32(((LString*) (regset->regs[inst->b].u & ~LTAG_MASK))->length);
} else if(lvalue_tag(regset->regs[inst->b]) == LTAG_TABLE) {
regset->regs[inst->a] = lvalue_from_int32(ltable_len((LTable*) (regset->regs[inst->b].u & ~LTAG_MASK)));
} else goto err;
DISPATCH();
err:;
puts("Error");
@@ -320,7 +329,7 @@ size_t lvm_run(LVM *L, LFunc *func, size_t arg_count, LRegSet *regset) {
atomic_fetch_add(&L->active_thread_count, 1);
size_t ret = lvm_run_internal(L, func, arg_count, &heap, regset);
size_t ret = lvm_call(L, func, arg_count, &heap, regset);
mtx_lock(&L->dead_heap_mut);
for(c_each(i, set_LValueU, heap)) {
@@ -400,7 +409,7 @@ static void gc_mark(LValue v) {
LTable *tbl = gco;
tbl->ref = true;
for(size_t i = 0; tbl->buckets->capacity; i++) {
for(size_t i = 0; i < tbl->buckets->capacity; i++) {
LTableEntry e = tbl->buckets->data[i];
gc_mark(e.key);
gc_mark(e.val);