Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Clearer separation of WASI POSIX support with (or without) wasi-libc by rootbeer · Pull Request #20991 · ziglang/zig · GitHub
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/Run.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1089,7 +1089,7 @@ fn runCommand(
// the `--` before the module name. This appears to work for both old and
// new Wasmtime versions.
try interp_argv.append(bin_name);
try interp_argv.append("--dir=.");
try interp_argv.append("--dir=."); // Preopen '.' at file descriptor 3 for cwd
try interp_argv.append("--");
try interp_argv.append(argv[0]);
try interp_argv.appendSlice(argv[1..]);
Expand Down
86 changes: 63 additions & 23 deletions lib/std/c.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,9 +133,10 @@ pub const dev_t = switch (native_os) {
pub const mode_t = switch (native_os) {
.linux => linux.mode_t,
.emscripten => emscripten.mode_t,
.openbsd, .haiku, .netbsd, .solaris, .illumos, .wasi => u32,
.openbsd, .haiku, .netbsd, .solaris, .illumos => u32,
.freebsd, .macos, .ios, .tvos, .watchos, .visionos => u16,
else => u0,
.wasi, .windows => void,
else => void,
};

pub const nlink_t = switch (native_os) {
Expand DownExpand Up@@ -671,6 +672,7 @@ pub const F = switch (native_os) {
.linux => linux.F,
.emscripten => emscripten.F,
.wasi => struct {
// Match F_* constants from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
Expand DownExpand Up@@ -1702,17 +1704,49 @@ pub const S = switch (native_os) {
.linux => linux.S,
.emscripten => emscripten.S,
.wasi => struct {
pub const IEXEC = @compileError("TODO audit this");
// Match S_* constants from lib/libc/include/wasm-wasi-musl/__mode_t.h
//
// Note, a bug in wasi-libc means both IFIFO and IFSOCK have the same value (0xc000).
// IFIFO should be 0x1000 (see https://github.com/WebAssembly/wasi-libc/pull/463), and
// 0x1000 is used by the wasi-libc bottom-half. So we use 0x1000 here. But the actual bit
// values we get back from a wasi-libc may get masked with the wrong values, or may get
// mistranslated. So the FIFO and Socket file-type bits are not trustworthy.
pub const IFBLK = 0x6000;
pub const IFCHR = 0x2000;
pub const IFDIR = 0x4000;
pub const IFIFO = 0xc000;
pub const IFIFO = 0x1000; // buggy
pub const IFLNK = 0xa000;
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;
pub const IFREG = 0x8000;
/// There's no concept of UNIX domain socket but we define this value here
/// in order to line with other OSes.
pub const IFSOCK = 0x1;
pub const IFSOCK = 0xc000; // buggy
pub const IFMT = IFBLK | IFCHR | IFDIR | IFIFO | IFLNK | IFREG | IFSOCK;

pub fn ISBLK(m: u32) bool {
return m & IFMT == IFBLK;
}

pub fn ISCHR(m: u32) bool {
return m & IFMT == IFCHR;
}

pub fn ISDIR(m: u32) bool {
return m & IFMT == IFDIR;
}

pub fn ISFIFO(m: u32) bool {
return m & IFMT == IFIFO;
}

pub fn ISLNK(m: u32) bool {
return m & IFMT == IFLNK;
}

pub fn ISREG(m: u32) bool {
return m & IFMT == IFREG;
}

pub fn ISSOCK(m: u32) bool {
return m & IFMT == IFSOCK;
}
},
.macos, .ios, .tvos, .watchos, .visionos => struct {
pub const IFMT = 0o170000;
Expand DownExpand Up@@ -6486,10 +6520,11 @@ pub const Stat = switch (native_os) {
},
.emscripten => emscripten.Stat,
.wasi => extern struct {
// Matches wasi-libc's struct stat in lib/libc/include/wasm-wasi-musl/__struct_stat.h
dev: dev_t,
ino: ino_t,
nlink: nlink_t,
mode: mode_t,
mode: c_uint, // only the file-type bits (S.IFMT), no permission bits
uid: uid_t,
gid: gid_t,
__pad0: c_uint = 0,
Expand DownExpand Up@@ -7185,17 +7220,13 @@ pub const AT = switch (native_os) {
pub const RECURSIVE = 0x8000;
},
.wasi => struct {
pub const SYMLINK_NOFOLLOW = 0x100;
pub const SYMLINK_FOLLOW = 0x400;
pub const REMOVEDIR: u32 = 0x4;
/// When linking libc, we follow their convention and use -2 for current working directory.
/// However, without libc, Zig does a different convention: it assumes the
/// current working directory is the first preopen. This behavior can be
/// overridden with a public function called `wasi_cwd` in the root source
/// file.
pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
// Match AT_* constants in lib/libc/include/wasm-wasi-musl/__header_fcntl.h
pub const FDCWD = -2;
pub const EACCESS = 0x0;
pub const SYMLINK_NOFOLLOW = 0x1;
pub const SYMLINK_FOLLOW = 0x2;
pub const REMOVEDIR = 0x4;
},

else => void,
};

Expand DownExpand Up@@ -7224,6 +7255,7 @@ pub const O = switch (native_os) {
_: u9 = 0,
},
.wasi => packed struct(u32) {
// Match O_* bits from lib/libc/include/wasm-wasi-musl/__header_fcntl.h
APPEND: bool = false,
DSYNC: bool = false,
NONBLOCK: bool = false,
Expand All@@ -7236,10 +7268,17 @@ pub const O = switch (native_os) {
TRUNC: bool = false,
_16: u8 = 0,
NOFOLLOW: bool = false,

// Logical O_ACCMODE is the following 4 bits, note O_SEARCH is between RD and WR,
// so can't easily reuse std.posix.ACCMODE in this struct.
EXEC: bool = false,
read: bool = false,
RDONLY: bool = true, // equivalent to "ACCMODE = .RDONLY" default
SEARCH: bool = false,
write: bool = false,
WRONLY: bool = false,

// CLOEXEC, TTY_ININT, NOCTTY are mapped 0, so they're silently
// ignored in C code.

_: u3 = 0,
},
.solaris, .illumos => packed struct(u32) {
Expand DownExpand Up@@ -9069,6 +9108,7 @@ pub const gettimeofday = switch (native_os) {

pub const msync = switch (native_os) {
.netbsd => private.__msync13,
.wasi => {},
else => private.msync,
};

Expand DownExpand Up@@ -9211,8 +9251,8 @@ pub const fork = switch (native_os) {
pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
pub extern "c" fn mkdir(path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: usize) c_int;
pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
pub extern "c" fn symlinkat(oldpath: [*:0]const u8, newdirfd: fd_t, newpath: [*:0]const u8) c_int;
pub extern "c" fn rename(old: [*:0]const u8, new: [*:0]const u8) c_int;
Expand Down
19 changes: 12 additions & 7 deletions lib/std/debug.zig
Original file line numberDiff line numberDiff line change
Expand Up@@ -1017,12 +1017,12 @@ test printLineFromFileAnyOs {

var test_dir = std.testing.tmpDir(.{});
defer test_dir.cleanup();
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths.
// Relies on testing.tmpDir internals which is not ideal, but SourceLocation requires paths relative to cwd.
const test_dir_path = try join(allocator, &.{ ".zig-cache", "tmp", test_dir.sub_path[0..] });
defer allocator.free(test_dir_path);

// Cases
{
// no newlines in file
const path = try join(allocator, &.{ test_dir_path, "one_line.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{ .sub_path = "one_line.zig", .data = "no new lines in this file, but one is printed anyway" });
Expand All@@ -1034,7 +1034,8 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
const path = try fs.path.join(allocator, &.{ test_dir_path, "three_lines.zig" });
// print 1 & 3 of 3-line file
const path = try join(allocator, &.{ test_dir_path, "three_lines.zig" });
defer allocator.free(path);
try test_dir.dir.writeFile(.{
.sub_path = "three_lines.zig",
Expand All@@ -1054,9 +1055,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size boundary crossing line
const file = try test_dir.dir.createFile("line_overlaps_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "line_overlaps_page_boundary.zig" });
defer allocator.free(path);

const overlap = 10;
Expand All@@ -1070,9 +1072,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// ends on mem.page_size boundary
const file = try test_dir.dir.createFile("file_ends_on_page_boundary.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_ends_on_page_boundary.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1083,9 +1086,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// multi-mem.page_size "line"
const file = try test_dir.dir.createFile("very_long_first_line_spanning_multiple_pages.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
const path = try join(allocator, &.{ test_dir_path, "very_long_first_line_spanning_multiple_pages.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand All@@ -1108,9 +1112,10 @@ test printLineFromFileAnyOs {
output.clearRetainingCapacity();
}
{
// mem.page_size pages of newlines
const file = try test_dir.dir.createFile("file_of_newlines.zig", .{});
defer file.close();
const path = try fs.path.join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
const path = try join(allocator, &.{ test_dir_path, "file_of_newlines.zig" });
defer allocator.free(path);

var writer = file.writer();
Expand Down
Loading