Skip to content

Commit

Permalink
Add support for WASM/WASI
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Jul 12, 2023
1 parent 1089817 commit 0046619
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cross-release() {
powerpc-linux-musl
riscv64-linux-gnu
riscv64-linux-musl
# wasm32-wasi-musl TODO
wasm32-wasi-musl
x86_64-linux-gnu
x86_64-linux-musl
)
Expand Down
15 changes: 10 additions & 5 deletions src/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();

const builtin = @import("builtin");

const Token = @import("tokens.zig").Token;

const main = @import("main.zig");
Expand All @@ -26,15 +28,18 @@ pub fn defineAll(machine: *DtMachine) !void {
try machine.define("version", "( -- <version> ) Produce the version of dt in use.", .{ .builtin = version });

try machine.define("cwd", "( -- <dirname> ) Produce the current working directory.", .{ .builtin = cwd });
try machine.define("cd", "( <dirname> -- ) Change the process's working directory.", .{ .builtin = cd });
if (builtin.os.tag != .wasi) {
try machine.define("cd", "( <dirname> -- ) Change the process's working directory.", .{ .builtin = cd });
}
try machine.define("ls", "( -- [<filename>] ) Produce a quote of files and directories in the process's working directory.", .{ .builtin = ls });
try machine.define("readf", "( <filename> -- <contents> ) Read a file's contents as a string.", .{ .builtin = readf });
try machine.define("writef", "( <contents> <filename> -- ) Write a string as a file. If a file previously existed, it will be overwritten.", .{ .builtin = writef });
try machine.define("appendf", "( <contents> <filename> -- ) Write a string to a file. If a file previously existed, the new content will be appended.", .{ .builtin = appendf });
// TODO: pathsep/filesep, env get, env set

try machine.define("exec", "( <process> -- ) Execute a child process (from a String). When successful, returns stdout as a string. When unsuccessful, prints the child's stderr to stderr, and returns boolean false.", .{ .builtin = exec });

if (builtin.os.tag != .wasi) {
try machine.define("exec", "( <process> -- ) Execute a child process (from a String). When successful, returns stdout as a string. When unsuccessful, prints the child's stderr to stderr, and returns boolean false.", .{ .builtin = exec });
}
try machine.define("def!", "( <action> <name> -- ) Defines a new command. " ++ bangDescription, .{ .builtin = @"def!" });
try machine.define("defs", "( -- [<name>] ) Produce a quote of all defined commands.", .{ .builtin = defs });
try machine.define("def?", "( <name> -- <bool> ) Determine whether a command is defined.", .{ .builtin = @"def?" });
Expand Down Expand Up @@ -505,14 +510,14 @@ pub fn @"read-lines"(dt: *DtMachine) !void {
}

pub fn procname(dt: *DtMachine) !void {
var procArgs = std.process.args();
var procArgs = try std.process.argsWithAllocator(dt.alloc);
var name = procArgs.next() orelse return Error.ProcessNameUnknown;
try dt.push(.{ .string = name });
}

pub fn args(dt: *DtMachine) !void {
var quote = Quote.init(dt.alloc);
var procArgs = std.process.args();
var procArgs = try std.process.argsWithAllocator(dt.alloc);
_ = procArgs.next(); // Discard process name

while (procArgs.next()) |arg| {
Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ fn doneOrDie(dt: *DtMachine, reason: anyerror) !void {
}

fn readShebangFile(allocator: Allocator) !?[]const u8 {
var args = std.process.args();
_ = args.skip();
var args = try std.process.argsWithAllocator(allocator);
_ = args.skip(); // Discard process name

if (args.next()) |maybeFilepath| {
// We get a Dir from CWD so we can resolve relative paths
Expand Down

0 comments on commit 0046619

Please sign in to comment.