Skip to content

Commit

Permalink
Bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNex committed Jul 27, 2023
1 parent 0ae3938 commit 69e1dca
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions internal/obj/native.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ struct object native_getsetter_get(struct getsetter *gs) {
struct object native_getsetter_set(struct getsetter *gs, struct object val) {
return errorf("cannot assign values to type %s", otype_str(gs->l.type));
}

void dispose_native_obj(struct object o) {
free(o.marked);
free(o.data.handle);
}
1 change: 1 addition & 0 deletions internal/obj/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ void dispose_getsetter_obj(struct object o);
// Native object.
struct object native_getsetter_get(struct getsetter *gs);
struct object native_getsetter_set(struct getsetter *gs, struct object val);
void dispose_native_obj(struct object o);

// Util functions.
char *otype_str(enum obj_type t);
Expand Down
3 changes: 3 additions & 0 deletions internal/obj/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ void free_obj(struct object o) {
case obj_getsetter:
dispose_getsetter_obj(o);
return;
case obj_native:
dispose_native_obj(o);
return;
default:
return;
}
Expand Down
9 changes: 6 additions & 3 deletions internal/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ static inline void vm_call_native(struct vm * restrict vm, struct object *n, siz
void *arg_values[numargs];

// Convert Tau types to C types.
for (size_t i = 0; i < numargs; i++) {
for (int64_t i = numargs - 1; i >= 0; i--) {
struct object *o = unwrap(&vm_stack_pop(vm));

switch (o->type) {
Expand Down Expand Up @@ -691,6 +691,7 @@ static inline void vm_call_native(struct vm * restrict vm, struct object *n, siz
vm_errorf(vm, "unsupported argument type %s for native objects", otype_str(o->type));
}
}
vm->sp--;

if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, numargs, &ffi_type_pointer, arg_types) != FFI_OK) {
vm_stack_push(vm, errorf("failed to prepare the native function"));
Expand All @@ -700,12 +701,14 @@ static inline void vm_call_native(struct vm * restrict vm, struct object *n, siz
void *return_value = malloc(sizeof(&ffi_type_pointer));
ffi_call(&cif, n->data.handle, return_value, arg_values);

struct object ret = (struct object) {
struct object res = (struct object) {
.data.handle = return_value,
.type = obj_native,
.marked = MARKPTR()
};
vm_stack_push(vm, ret);
vm_stack_push(vm, res);
vm_heap_add(vm, res);
gc(vm);
}

static inline void vm_exec_call(struct vm * restrict vm, size_t numargs) {
Expand Down

0 comments on commit 69e1dca

Please sign in to comment.