Skip to content

Commit

Permalink
fix: Use std.process instead of std.posix when revelant
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed May 14, 2024
1 parent 4cb8734 commit 7e92948
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 29 deletions.
6 changes: 3 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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).?;
}

Expand Down Expand Up @@ -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)},
),
},
);
Expand Down Expand Up @@ -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);

Expand Down
59 changes: 51 additions & 8 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,33 @@ 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;
}

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{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Reporter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/jit_extern_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions src/lib/buzz_ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/lib/buzz_os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/buzz_std.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
Expand All @@ -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(),
),
Expand Down Expand Up @@ -219,7 +219,7 @@ pub export fn assert(ctx: *api.NativeCtx) c_int {
}

if (!is_wasm) {
std.posix.exit(1);
std.process.exit(1);
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -272,7 +272,7 @@ pub fn main() !void {
);

if (!is_wasm) {
std.posix.exit(0);
std.process.exit(0);
}
}

Expand Down Expand Up @@ -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", .{});
Expand All @@ -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", .{});
Expand All @@ -339,7 +339,7 @@ pub fn main() !void {
}

if (!is_wasm) {
std.posix.exit(0);
std.process.exit(0);
}
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -4013,7 +4013,7 @@ pub const VM = struct {
);

if (!is_wasm) {
std.posix.exit(1);
std.process.exit(1);
} else {
return Error.RuntimeError;
}
Expand Down

0 comments on commit 7e92948

Please sign in to comment.