Skip to content

Commit

Permalink
Fix main.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Sep 2, 2024
1 parent 6819984 commit c32e6fc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
7 changes: 4 additions & 3 deletions src/arc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,12 @@ pub fn countObjects(vm: *cy.VM) usize {
return count;
}

pub fn checkGlobalRC(vm: *cy.VM) !void {
const rc = getGlobalRC(vm);
pub fn checkGlobalRC(ivm: *cy.VM) !void {
const vm: *c.ZVM = @ptrCast(ivm);
const rc = getGlobalRC(ivm);
if (rc != 0) {
std.debug.print("unreleased refcount: {}\n", .{rc});
c.traceDumpLiveObjects(@ptrCast(vm));
vm.traceDumpLiveObjects();

// var iter = cy.vm.traceObjRetains.iterator();
// while (iter.next()) |e| {
Expand Down
88 changes: 46 additions & 42 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CP_UTF8 = 65001;
var prevWinConsoleOutputCP: u32 = undefined;

// Default VM.
var vm: cy.VM = undefined;
var ivm: cy.VM = undefined;

pub fn main() !void {
if (builtin.os.tag == .windows) {
Expand Down Expand Up @@ -154,40 +154,43 @@ const Command = enum {
fn compilePath(alloc: std.mem.Allocator, path: []const u8) !void {
c.setVerbose(verbose);

try vm.init(alloc);
cli.clInitCLI(@ptrCast(&vm));
const vm: *c.ZVM = @ptrCast(&ivm);
try ivm.init(alloc);
cli.clInitCLI(&ivm);
defer {
cli.clDeinitCLI(@ptrCast(&vm));
vm.deinit(false);
cli.clDeinitCLI(&ivm);
ivm.deinit(false);
}

var config = c.defaultCompileConfig();
config.single_run = builtin.mode == .ReleaseFast;
config.file_modules = true;
config.gen_debug_func_markers = true;
config.backend = backend;
_ = vm.compile(path, null, config) catch |err| {
_ = ivm.compile(path, null, config) catch |err| {
if (err == error.CompileError) {
if (!c.silent()) {
const report = c.newErrorReportSummary(@ptrCast(&vm));
defer c.free(@ptrCast(&vm), report);
cy.rt.writeStderr(c.fromStr(report));
const report = vm.newErrorReportSummary();
defer vm.free(report);
cy.rt.writeStderr(report);
}
exit(1);
} else {
fmt.panic("unexpected {}\n", &.{fmt.v(err)});
}
};
try cy.debug.dumpBytecode(&vm, .{ .pcContext = pc });
try cy.debug.dumpBytecode(&ivm, .{ .pcContext = pc });
}

fn repl(alloc: std.mem.Allocator) !void {
c.setVerbose(verbose);
try vm.init(alloc);
cli.clInitCLI(@ptrCast(&vm));

const vm: *c.ZVM = @ptrCast(&ivm);
try ivm.init(alloc);
cli.clInitCLI(&ivm);
defer {
cli.clDeinitCLI(@ptrCast(&vm));
vm.deinit(false);
cli.clDeinitCLI(&ivm);
ivm.deinit(false);
}

var config = c.defaultEvalConfig();
Expand All @@ -203,20 +206,20 @@ fn repl(alloc: std.mem.Allocator) !void {
\\cli.repl()
\\
;
_ = vm.eval("main", src, config) catch |err| {
_ = ivm.eval("main", src, config) catch |err| {
switch (err) {
error.Panic => {
if (!c.silent()) {
const report = c.newPanicSummary(@ptrCast(&vm));
defer c.free(@ptrCast(&vm), report);
try std.io.getStdErr().writeAll(c.fromStr(report));
const report = vm.newPanicSummary();
defer vm.free(report);
try std.io.getStdErr().writeAll(report);
}
},
error.CompileError => {
if (!c.silent()) {
const report = c.newErrorReportSummary(@ptrCast(&vm));
defer c.free(@ptrCast(&vm), report);
try std.io.getStdErr().writeAll(c.fromStr(report));
const report = vm.newErrorReportSummary();
defer vm.free(report);
try std.io.getStdErr().writeAll(report);
}
},
else => {
Expand All @@ -234,26 +237,27 @@ fn repl(alloc: std.mem.Allocator) !void {

if (verbose) {
std.debug.print("\n==VM Info==\n", .{});
try vm.dumpInfo();
try ivm.dumpInfo();
}
if (cy.Trace and dumpStats) {
vm.dumpStats();
ivm.dumpStats();
}
if (cy.TrackGlobalRC) {
vm.deinitRtObjects();
vm.compiler.deinitValues();
try cy.arc.checkGlobalRC(&vm);
ivm.deinitRtObjects();
ivm.compiler.deinitValues();
try cy.arc.checkGlobalRC(&ivm);
}
}

fn evalPath(alloc: std.mem.Allocator, path: []const u8) !void {
c.setVerbose(verbose);

try vm.init(alloc);
cli.clInitCLI(@ptrCast(&vm));
const vm: *c.ZVM = @ptrCast(&ivm);
try ivm.init(alloc);
cli.clInitCLI(&ivm);
defer {
cli.clDeinitCLI(@ptrCast(&vm));
vm.deinit(false);
cli.clDeinitCLI(&ivm);
ivm.deinit(false);
}

var config = c.defaultEvalConfig();
Expand All @@ -262,20 +266,20 @@ fn evalPath(alloc: std.mem.Allocator, path: []const u8) !void {
config.reload = reload;
config.backend = backend;
config.spawn_exe = true;
_ = vm.eval(path, null, config) catch |err| {
_ = ivm.eval(path, null, config) catch |err| {
switch (err) {
error.Panic => {
if (!c.silent()) {
const report = c.newPanicSummary(@ptrCast(&vm));
defer c.free(@ptrCast(&vm), report);
try std.io.getStdErr().writeAll(c.fromStr(report));
const report = vm.newPanicSummary();
defer vm.free(report);
try std.io.getStdErr().writeAll(report);
}
},
error.CompileError => {
if (!c.silent()) {
const report = c.newErrorReportSummary(@ptrCast(&vm));
defer c.free(@ptrCast(&vm), report);
try std.io.getStdErr().writeAll(c.fromStr(report));
const report = vm.newErrorReportSummary();
defer vm.free(report);
try std.io.getStdErr().writeAll(report);
}
},
else => {
Expand All @@ -292,15 +296,15 @@ fn evalPath(alloc: std.mem.Allocator, path: []const u8) !void {
};
if (verbose) {
std.debug.print("\n==VM Info==\n", .{});
try vm.dumpInfo();
try ivm.dumpInfo();
}
if (cy.Trace and dumpStats) {
vm.dumpStats();
ivm.dumpStats();
}
if (cy.TrackGlobalRC) {
vm.deinitRtObjects();
vm.compiler.deinitValues();
try cy.arc.checkGlobalRC(&vm);
ivm.deinitRtObjects();
ivm.compiler.deinitValues();
try cy.arc.checkGlobalRC(&ivm);
}
}

Expand Down

0 comments on commit c32e6fc

Please sign in to comment.