Skip to content

Commit

Permalink
add python as a first level dep and fix UB with stdout override
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Jun 30, 2024
1 parent 58fe1e2 commit 859c1d0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
15 changes: 14 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,20 @@ pub fn build(b: *std.Build) !void {
exe.root_module.addImport("cpython", cpython_dep.module("cpython"));
exe.root_module.addImport("vaxis", libvaxis.module("vaxis"));

exe_options.addOption([]const u8, "lib_path", "../python/Lib");
// install the Lib folder from python
const python_dep = b.dependency("python", .{});
const libpython_install = b.addInstallDirectory(.{
.source_dir = python_dep.path("Lib"),
.install_dir = .{ .custom = "python" },
.install_subdir = "Lib",
});
exe.step.dependOn(&libpython_install.step);

exe_options.addOption(
[]const u8,
"lib_path",
b.getInstallPath(.{ .custom = "python" }, "Lib"),
);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
Expand Down
10 changes: 8 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
.url = "https://github.com/Rexicon226/zig-tracer/archive/6400e066bb3849be4906f3b6e124d6c8c5a5083f.tar.gz",
.hash = "122057453dba0b0cf6f07610d692cc7ab3f1d66defa150c936b398144812564a0c08",
},
.cpython = .{
.path = "../zig-cpython",
// needs to be kept in sync with cpython dep
.python = .{
.url = "https://github.com/Rexicon226/cpython/archive/cdb93314157b15ba6ba0832f4ad7c84c11088331.tar.gz",
.hash = "12201233efd4fb9453166782f1beb083953b3e9cc2b347621a55482addf0714e6962",
},
.libgc = .{
.url = "https://github.com/Rexicon226/zig-libgc/archive/1ea12e140ec1471af5d15dae71b225764c9747ba.tar.gz",
Expand All @@ -24,5 +26,9 @@
.url = "https://github.com/rockorager/libvaxis/archive/c213919849114ce03def64806af546b9fc391859.tar.gz",
.hash = "12206f2265ca2262f919ec125ff7fe15fa6f3be622806e211d7a9bd3055ba51a0908",
},
.cpython = .{
.url = "https://github.com/Rexicon226/zig-cpython/archive/6ee418f9f7be7e811e5555f73a0335905c386914.tar.gz",
.hash = "122017c3777f28ab50709a456371d710e3a00c9ac82959128319ffbdfb18f1528c2a",
},
},
}
6 changes: 3 additions & 3 deletions src/modules/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn print(vm: *Vm, args: []const Object, maybe_kw: ?KW_Type) BuiltinError!void {
const t = tracer.trace(@src(), "builtin-print", .{});
defer t.end();

const stdout = vm.stdout;
const stdout = vm.getStdout();
for (args, 0..) |arg, i| {
printSafe(stdout, "{}", .{arg});

Expand Down Expand Up @@ -146,8 +146,8 @@ fn input(vm: *Vm, args: []const Object, maybe_kw: ?KW_Type) BuiltinError!void {
const prompt = args[0];
const prompt_string = prompt.get(.string);

const stdout = std.io.getStdOut();
printSafe(stdout.writer(), "{s}", .{prompt_string});
const stdout = vm.getStdout();
printSafe(stdout, "{s}", .{prompt_string});
}

const stdin = std.io.getStdIn();
Expand Down
9 changes: 7 additions & 2 deletions src/vm/Vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ crash_info: crash_report.VmContext,
builtin_mods: std.StringHashMapUnmanaged(Object.Payload.Module) = .{},

/// Default set to FD1
stdout: std.io.AnyWriter,
stdout_override: ?std.io.AnyWriter,

/// Takes ownership of `co`.
pub fn init(allocator: Allocator, co: CodeObject) !Vm {
Expand All @@ -69,7 +69,7 @@ pub fn init(allocator: Allocator, co: CodeObject) !Vm {
.co = co,
.crash_info = crash_report.prepVmContext(co),
.stack = try std.ArrayListUnmanaged(Object).initCapacity(allocator, co.stacksize),
.stdout = std.io.getStdOut().writer().any(),
.stdout_override = null,
};

assert(vm.scopes.items.len == 0);
Expand Down Expand Up @@ -795,6 +795,11 @@ fn setNewCo(
vm.co = new_co;
}

const stdout = std.io.getStdOut().writer();
pub fn getStdout(vm: *const Vm) std.io.AnyWriter {
return vm.stdout_override orelse stdout.any();
}

pub fn fail(
vm: *Vm,
comptime fmt: []const u8,
Expand Down
2 changes: 1 addition & 1 deletion src/vm/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const State = struct {
}

const vm = state.vm;
vm.stdout = state.stdout.writer().any();
vm.stdout_override = state.stdout.writer().any();
vm.is_running = true;

while (vm.is_running) {
Expand Down

0 comments on commit 859c1d0

Please sign in to comment.