Skip to content

Commit

Permalink
fix: bz_newVM does not need parent VM
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jan 23, 2025
1 parent 9dfa021 commit 17621c0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
36 changes: 16 additions & 20 deletions src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -611,33 +611,29 @@ export fn bz_getUserDataPtr(userdata: Value) u64 {
return ObjUserData.cast(userdata.obj()).?.userdata;
}

export fn bz_newVM(self: *VM) ?*VM {
const vm = self.gc.allocator.create(VM) catch {
return null;
};
var gc = self.gc.allocator.create(GarbageCollector) catch {
return null;
};
export fn bz_newVM() *VM {
const vm = allocator.create(VM) catch @panic("Out of memory");
var gc = allocator.create(GarbageCollector) catch @panic("Out of memory");
// FIXME: should share strings between gc
gc.* = GarbageCollector.init(self.gc.allocator) catch {
vm.panic("Out of memory");
unreachable;
};
gc.type_registry = TypeRegistry.init(gc) catch {
vm.panic("Out of memory");
unreachable;
};
gc.* = GarbageCollector.init(allocator) catch @panic("Out of memory");
gc.type_registry = TypeRegistry.init(gc) catch @panic("Out of memory");
const import_registry = allocator.create(_vm.ImportRegistry) catch @panic("Out of memory");
import_registry.* = _vm.ImportRegistry.init(allocator);

// FIXME: give reference to JIT?
vm.* = VM.init(gc, self.import_registry, self.flavor) catch {
return null;
};
vm.* = VM.init(
gc,
import_registry,
.Run,
) catch @panic("Out of memory");

return vm;
}

export fn bz_deinitVM(_: *VM) void {
// self.deinit();
export fn bz_deinitVM(self: *VM) void {
self.deinit();
self.import_registry.deinit();
self.gc.deinit();
}

export fn bz_panic(vm: *VM, msg: [*]const u8, len: usize) void {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub const VM = opaque {
else
std.heap.c_allocator;

pub extern fn bz_newVM(self: *VM) *VM;
pub extern fn bz_newVM() *VM;
pub extern fn bz_deinitVM(self: *VM) void;
pub extern fn bz_panic(vm: *VM, msg: [*]const u8, len: usize) void;
pub extern fn bz_run(self: *VM, source: ?[*]const u8, source_len: usize, file_name: ?[*]const u8, file_name_len: usize) bool;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/buzz_io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ pub export fn runFile(ctx: *api.NativeCtx) c_int {
defer api.VM.allocator.free(source);

// Init new VM
var vm = ctx.vm.bz_newVM();
var vm = api.VM.bz_newVM();
defer vm.bz_deinitVM();

// Compile
Expand Down
12 changes: 6 additions & 6 deletions src/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ pub const GarbageCollector = struct {
}

pub fn deinit(self: *Self) void {
self.gray_stack.deinit();
self.strings.deinit();
self.active_vms.deinit();
if (self.debugger) |debugger| {
debugger.deinit();
self.gray_stack.deinit(self.allocator);
self.strings.deinit(self.allocator);
self.active_vms.deinit(self.allocator);
if (self.debugger != null) {
self.debugger.?.deinit();
}

self.allocator.free(self.objfiber_members);
Expand Down Expand Up @@ -1119,7 +1119,7 @@ pub const GarbageCollectorDebugger = struct {
}

pub fn deinit(self: *Self) void {
self.tracker.deinit();
self.tracker.deinit(self.allocator);
}

pub fn allocated(self: *Self, ptr: *Obj, at: ?Token, what: _obj.ObjType) void {
Expand Down
4 changes: 3 additions & 1 deletion src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ pub const VM = struct {
pub fn deinit(self: *Self) void {
// TODO: we can't free this because exported closure refer to it
// self.globals.deinit();
self.ffi.deinit();
if (!is_wasm) {
self.ffi.deinit();
}
}

pub fn cliArgs(self: *Self, args: ?[]const []const u8) !*ObjList {
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/testing.buzz
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace testing;

import "std" as std;
import "std";

final unexported = 42;

Expand Down

0 comments on commit 17621c0

Please sign in to comment.