diff --git a/build.zig b/build.zig index f24273a7..3d737073 100644 --- a/build.zig +++ b/build.zig @@ -91,7 +91,7 @@ const BuzzBuildOptions = struct { } }; -fn getBuzzPrefix(b: *Build) []const u8 { +fn getBuzzPrefix(b: *Build) ![]const u8 { return std.posix.getenv("BUZZ_PATH") orelse std.fs.path.dirname(b.exe_dir).?; } @@ -492,7 +492,7 @@ pub fn build(b: *Build) !void { .{ .path = b.fmt( "{s}" ++ std.fs.path.sep_str ++ "lib/buzz", - .{getBuzzPrefix(b)}, + .{try getBuzzPrefix(b)}, ), }, ); @@ -535,7 +535,7 @@ pub fn build(b: *Build) !void { const test_step = b.step("test", "Run all the tests"); const run_tests = b.addRunArtifact(tests); run_tests.cwd = Build.LazyPath{ .path = "." }; - run_tests.setEnvironmentVariable("BUZZ_PATH", getBuzzPrefix(b)); + run_tests.setEnvironmentVariable("BUZZ_PATH", try getBuzzPrefix(b)); run_tests.step.dependOn(install_step); // wait for libraries to be installed test_step.dependOn(&run_tests.step); diff --git a/src/Parser.zig b/src/Parser.zig index 1cc4c766..dbd743fd 100644 --- a/src/Parser.zig +++ b/src/Parser.zig @@ -132,11 +132,16 @@ pub fn defaultBuzzPrefix() []const u8 { var _buzz_path_buffer: [4096]u8 = undefined; pub fn buzzPrefix() []const u8 { - if (std.posix.getenv("BUZZ_PATH")) |buzz_path| return buzz_path; + // FIXME: don't use std.posix directly + if (std.posix.getenv("BUZZ_PATH")) |buzz_path| { + return buzz_path; + } + const path = if (!is_wasm) std.fs.selfExePath(&_buzz_path_buffer) catch return defaultBuzzPrefix() else defaultBuzzPrefix(); + const path1 = std.fs.path.dirname(path) orelse defaultBuzzPrefix(); const path2 = std.fs.path.dirname(path1) orelse defaultBuzzPrefix(); return path2; @@ -144,10 +149,16 @@ pub fn buzzPrefix() []const u8 { var _buzz_path_buffer2: [4096]u8 = undefined; /// the returned string can be used only until next call to this function -pub fn buzzLibPath() []const u8 { +pub fn buzzLibPath() ![]const u8 { const path2 = buzzPrefix(); const sep = std.fs.path.sep_str; - return std.fmt.bufPrint(&_buzz_path_buffer2, "{s}" ++ sep ++ "lib" ++ sep ++ "buzz", .{path2}) catch unreachable; + return std.fmt.bufPrint( + &_buzz_path_buffer2, + "{s}" ++ sep ++ "lib" ++ sep ++ "buzz", + .{ + path2, + }, + ) catch unreachable; } pub const CompileError = error{ @@ -6936,11 +6947,31 @@ fn searchPaths(self: *Self, file_name: []const u8) ![][]const u8 { defer paths.shrinkAndFree(paths.items.len); for (search_paths) |path| { - const filled = try std.mem.replaceOwned(u8, self.gc.allocator, path, "?", file_name); + const filled = try std.mem.replaceOwned( + u8, + self.gc.allocator, + path, + "?", + file_name, + ); defer self.gc.allocator.free(filled); - const suffixed = try std.mem.replaceOwned(u8, self.gc.allocator, filled, "!", "buzz"); + const suffixed = try std.mem.replaceOwned( + u8, + self.gc.allocator, + filled, + "!", + "buzz", + ); defer self.gc.allocator.free(suffixed); - const prefixed = try std.mem.replaceOwned(u8, self.gc.allocator, suffixed, "$", buzzLibPath()); + const prefixed = try std.mem.replaceOwned( + u8, + self.gc.allocator, + suffixed, + "$", + try buzzLibPath(), + ); + + std.debug.print("> {s}\n", .{prefixed}); try paths.append(prefixed); } @@ -6952,7 +6983,13 @@ fn searchLibPaths(self: *Self, file_name: []const u8) !std.ArrayList([]const u8) var paths = std.ArrayList([]const u8).init(self.gc.allocator); for (lib_search_paths) |path| { - const filled = try std.mem.replaceOwned(u8, self.gc.allocator, path, "?", file_name); + const filled = try std.mem.replaceOwned( + u8, + self.gc.allocator, + path, + "?", + file_name, + ); defer self.gc.allocator.free(filled); const suffixed = try std.mem.replaceOwned( u8, @@ -6967,7 +7004,13 @@ fn searchLibPaths(self: *Self, file_name: []const u8) !std.ArrayList([]const u8) }, ); defer self.gc.allocator.free(suffixed); - const prefixed = try std.mem.replaceOwned(u8, self.gc.allocator, suffixed, "$", buzzLibPath()); + const prefixed = try std.mem.replaceOwned( + u8, + self.gc.allocator, + suffixed, + "$", + try buzzLibPath(), + ); try paths.append(prefixed); } diff --git a/src/Reporter.zig b/src/Reporter.zig index 78c63a02..a4a42aaf 100644 --- a/src/Reporter.zig +++ b/src/Reporter.zig @@ -185,8 +185,10 @@ pub const Report = struct { pub fn report(self: *Report, reporter: *Self, out: anytype) !void { assert(self.items.len > 0); + var env_map = try std.process.getEnvMap(reporter.allocator); + defer env_map.deinit(); - const colorterm = std.posix.getenv("COLORTERM"); + const colorterm = env_map.get("COLORTERM"); const true_color = if (colorterm) |ct| std.mem.eql(u8, ct, "24bit") or std.mem.eql(u8, ct, "truecolor") else diff --git a/src/jit_extern_api.zig b/src/jit_extern_api.zig index ca337c91..9207e5f4 100644 --- a/src/jit_extern_api.zig +++ b/src/jit_extern_api.zig @@ -6,7 +6,7 @@ const JIT = @import("Jit.zig"); const jmp = @import("jmp.zig").jmp; export fn bz_exit(code: c_int) noreturn { - std.posix.exit(@truncate(@as(c_uint, @bitCast(code)))); + std.process.exit(@truncate(@as(c_uint, @bitCast(code)))); } pub const ExternApi = enum { diff --git a/src/lib/buzz_ffi.zig b/src/lib/buzz_ffi.zig index 79fc0954..e5f5b14a 100644 --- a/src/lib/buzz_ffi.zig +++ b/src/lib/buzz_ffi.zig @@ -14,7 +14,12 @@ pub export fn alignOf(ctx: *api.NativeCtx) c_int { var msg = std.ArrayList(u8).init(api.VM.allocator); defer msg.deinit(); - msg.writer().print("Could not parse zig type `{s}`", .{zig_type_str[0..len]}) catch @panic("Out of memory"); + msg.writer().print( + "Could not parse zig type `{s}`", + .{ + zig_type_str[0..len], + }, + ) catch @panic("Out of memory"); ctx.vm.pushError("ffi.FFIZigTypeParseError", msg.items); @@ -37,7 +42,12 @@ pub export fn sizeOf(ctx: *api.NativeCtx) c_int { var msg = std.ArrayList(u8).init(api.VM.allocator); defer msg.deinit(); - msg.writer().print("Could not parse zig type `{s}`", .{zig_type_str[0..len]}) catch @panic("Out of memory"); + msg.writer().print( + "Could not parse zig type `{s}`", + .{ + zig_type_str[0..len], + }, + ) catch @panic("Out of memory"); ctx.vm.pushError("ffi.FFIZigTypeParseError", msg.items); diff --git a/src/lib/buzz_os.zig b/src/lib/buzz_os.zig index 708166ed..74eaed8e 100644 --- a/src/lib/buzz_os.zig +++ b/src/lib/buzz_os.zig @@ -27,7 +27,8 @@ pub export fn env(ctx: *api.NativeCtx) c_int { const key_slice = api.VM.allocator.dupeZ(u8, key.?[0..len]) catch @panic("Out of memory"); defer api.VM.allocator.free(key_slice); - if (std.posix.getenvZ(key_slice)) |value| { + // FIXME: don't use std.posix directly + if (std.posix.getenv(key_slice)) |value| { ctx.vm.bz_pushString(api.ObjString.bz_string(ctx.vm, if (value.len > 0) @as([*]const u8, @ptrCast(value)) else null, value.len) orelse { @panic("Out of memory"); }); @@ -96,7 +97,7 @@ pub export fn tmpFilename(ctx: *api.NativeCtx) c_int { pub export fn buzzExit(ctx: *api.NativeCtx) c_int { const exitCode: i32 = ctx.vm.bz_peek(0).integer(); - std.posix.exit(@intCast(exitCode)); + std.process.exit(@intCast(exitCode)); return 0; } diff --git a/src/lib/buzz_std.zig b/src/lib/buzz_std.zig index a6f9a39b..d1a20bda 100644 --- a/src/lib/buzz_std.zig +++ b/src/lib/buzz_std.zig @@ -56,7 +56,7 @@ pub export fn toInt(ctx: *api.NativeCtx) c_int { ctx.vm.bz_push( api.Value.fromInteger( if (value.isFloat()) - @as(i32, @intFromFloat(value.float())) + @intFromFloat(value.float()) else value.integer(), ), @@ -71,7 +71,7 @@ pub export fn toFloat(ctx: *api.NativeCtx) c_int { ctx.vm.bz_push( api.Value.fromFloat( if (value.isInteger()) - @as(f64, @floatFromInt(value.integer())) + @floatFromInt(value.integer()) else value.float(), ), @@ -219,7 +219,7 @@ pub export fn assert(ctx: *api.NativeCtx) c_int { } if (!is_wasm) { - std.posix.exit(1); + std.process.exit(1); } } diff --git a/src/main.zig b/src/main.zig index 267c2cca..b6525182 100644 --- a/src/main.zig +++ b/src/main.zig @@ -245,7 +245,7 @@ pub fn main() !void { printBanner(std.io.getStdOut().writer(), true); if (!is_wasm) { - std.posix.exit(0); + std.process.exit(0); } } @@ -272,7 +272,7 @@ pub fn main() !void { ); if (!is_wasm) { - std.posix.exit(0); + std.process.exit(0); } } @@ -314,7 +314,7 @@ pub fn main() !void { if (!is_wasm and flavor == .Repl) { repl(allocator) catch { if (!is_wasm) { - std.posix.exit(1); + std.process.exit(1); } std.debug.print("REPL stopped", .{}); @@ -327,7 +327,7 @@ pub fn main() !void { flavor, ) catch { if (!is_wasm) { - std.posix.exit(1); + std.process.exit(1); } std.debug.print("VM stopped", .{}); @@ -339,7 +339,7 @@ pub fn main() !void { } if (!is_wasm) { - std.posix.exit(0); + std.process.exit(0); } } @@ -406,7 +406,13 @@ test "Testing behavior" { const reader = test_file.reader(); const first_line = try reader.readUntilDelimiterAlloc(allocator, '\n', std.math.maxInt(usize)); defer allocator.free(first_line); - const arg0 = std.fmt.allocPrintZ(allocator, "{s}/bin/buzz", .{Parser.buzzPrefix()}) catch unreachable; + const arg0 = std.fmt.allocPrintZ( + allocator, + "{s}/bin/buzz", + .{ + Parser.buzzPrefix(), + }, + ) catch unreachable; defer allocator.free(arg0); const result = try std.ChildProcess.run( @@ -449,5 +455,5 @@ test "Testing behavior" { fail_count, }); - std.posix.exit(if (fail_count == 0) 0 else 1); + std.process.exit(if (fail_count == 0) 0 else 1); } diff --git a/src/vm.zig b/src/vm.zig index bcdfd893..d27bc6a8 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -783,7 +783,7 @@ pub const VM = struct { fn vmPanic(e: anytype) void { std.debug.print("{}\n", .{e}); if (!is_wasm) { - std.posix.exit(1); + std.process.exit(1); } } @@ -4013,7 +4013,7 @@ pub const VM = struct { ); if (!is_wasm) { - std.posix.exit(1); + std.process.exit(1); } else { return Error.RuntimeError; }