Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,9 @@ pub const LibExeObjStep = struct {
/// Uses system QEMU installation to run cross compiled foreign architecture build artifacts.
enable_qemu: bool = false,

/// Uses system Wasmtime installation to run cross compiled wasm/wasi build artifacts.
enable_wasmtime: bool = false,

/// After following the steps in https://github.com/ziglang/zig/wiki/Updating-libc#glibc,
/// this will be the directory $glibc-build-dir/install/glibcs
/// Given the example of the aarch64 target, this is the directory
Expand Down Expand Up @@ -1863,6 +1866,11 @@ pub const LibExeObjStep = struct {
try zig_args.append(bin_name);
try zig_args.append("--test-cmd-bin");
},
.wasmtime => |bin_name| if (self.enable_wasmtime) {
try zig_args.append("--test-cmd");
try zig_args.append(bin_name);
try zig_args.append("--test-cmd-bin");
},
}
for (self.packages.toSliceConst()) |pkg| {
zig_args.append("--pkg-begin") catch unreachable;
Expand Down
3 changes: 2 additions & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ pub fn assert(ok: bool) void {

pub fn panic(comptime format: []const u8, args: ...) noreturn {
@setCold(true);
const first_trace_addr = @returnAddress();
// TODO: remove conditional once wasi / LLVM defines __builtin_return_address
const first_trace_addr = if (builtin.os == .wasi) null else @returnAddress();
panicExtra(null, first_trace_addr, format, args);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn cShrink(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new

/// This allocator makes a syscall directly for every allocation and free.
/// Thread-safe and lock-free.
pub const direct_allocator = &direct_allocator_state;
pub const direct_allocator = if (builtin.arch == .wasm32) wasm_allocator else &direct_allocator_state;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better done on the DirectAllocator type itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Since the wasm allocator has global state, we need to ensure that direct_allocator and wasm_allocator refer to the same instance.

var direct_allocator_state = Allocator{
.reallocFn = DirectAllocator.realloc,
.shrinkFn = DirectAllocator.shrink,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ pub fn isatty(handle: fd_t) bool {
return system.isatty(handle) != 0;
}
if (builtin.os == .wasi) {
@compileError("TODO implement std.os.isatty for WASI");
return system.isatty(handle);
}
if (builtin.os == .linux) {
var wsz: linux.winsize = undefined;
Expand Down
8 changes: 7 additions & 1 deletion lib/std/os/bits/wasi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub const FDFLAG_NONBLOCK: fdflags_t = 0x0004;
pub const FDFLAG_RSYNC: fdflags_t = 0x0008;
pub const FDFLAG_SYNC: fdflags_t = 0x0010;

const fdstat_t = extern struct {
pub const fdstat_t = extern struct {
fs_filetype: filetype_t,
fs_flags: fdflags_t,
fs_rights_base: rights_t,
Expand Down Expand Up @@ -298,10 +298,16 @@ pub const subscription_t = extern struct {
};

pub const timestamp_t = u64;
pub const time_t = i64; // match https://github.com/CraneStation/wasi-libc

pub const userdata_t = u64;

pub const whence_t = u8;
pub const WHENCE_CUR: whence_t = 0;
pub const WHENCE_END: whence_t = 1;
pub const WHENCE_SET: whence_t = 2;

pub const timespec = extern struct {
tv_sec: time_t,
tv_nsec: isize,
};
51 changes: 51 additions & 0 deletions lib/std/os/wasi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,54 @@ pub extern "wasi_unstable" fn sched_yield() errno_t;
pub extern "wasi_unstable" fn sock_recv(sock: fd_t, ri_data: *const iovec_t, ri_data_len: usize, ri_flags: riflags_t, ro_datalen: *usize, ro_flags: *roflags_t) errno_t;
pub extern "wasi_unstable" fn sock_send(sock: fd_t, si_data: *const ciovec_t, si_data_len: usize, si_flags: siflags_t, so_datalen: *usize) errno_t;
pub extern "wasi_unstable" fn sock_shutdown(sock: fd_t, how: sdflags_t) errno_t;

/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) usize {
const signed_r = @bitCast(isize, r);
return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
}

pub fn clock_getres(clock_id: i32, res: *timespec) errno_t {
var ts: timestamp_t = undefined;
const err = clock_res_get(@bitCast(u32, clock_id), &ts);
if (err != 0) {
return err;
}
res.* = .{
.tv_sec = @intCast(i64, ts / std.time.ns_per_s),
.tv_nsec = @intCast(isize, ts % std.time.ns_per_s),
};
return 0;
}

pub fn clock_gettime(clock_id: i32, tp: *timespec) errno_t {
var ts: timestamp_t = undefined;
const err = clock_time_get(@bitCast(u32, clock_id), 1, &ts);
if (err != 0) {
return err;
}
tp.* = .{
.tv_sec = @intCast(i64, ts / std.time.ns_per_s),
.tv_nsec = @intCast(isize, ts % std.time.ns_per_s),
};
return 0;
}

pub fn isatty(fd: fd_t) bool {
var statbuf: fdstat_t = undefined;
const err = fd_fdstat_get(fd, &statbuf);
if (err != 0) {
// errno = err;
return false;
}

// A tty is a character device that we can't seek or tell on.
if (statbuf.fs_filetype != FILETYPE_CHARACTER_DEVICE or
(statbuf.fs_rights_base & (RIGHT_FD_SEEK | RIGHT_FD_TELL)) != 0)
{
// errno = ENOTTY;
return false;
}

return true;
}
8 changes: 8 additions & 0 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ pub const Target = union(enum) {
native,
qemu: []const u8,
wine: []const u8,
wasmtime: []const u8,
unavailable,
};

Expand Down Expand Up @@ -649,6 +650,13 @@ pub const Target = union(enum) {
}
}

if (self.isWasm()) {
switch (self.getArchPtrBitWidth()) {
32 => return Executor{ .wasmtime = "wasmtime" },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious... have you tried this and it didn't worked? @fengb

                32 => return Executor{ .wasi = "wasmer run" },

Copy link
Contributor Author

@fengb fengb Nov 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zig build args are generated separately --test-cmd wasmer --test-cmd run so I'd have to update the plumbing to support multiple args.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That make sense! Thanks for the info.

To support this use case, we are working now on allowing to pass a file as first argument without being explicit with run (so you can do wasmer myfile.wasm).
I'll post an update here once it's ready to be used (which should be very soon!)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just added support for running wasm files directly without the run argument.
wasmerio/wasmer#990

We will publish a release soon :)

else => return .unavailable,
}
}

return .unavailable;
}
};