add the zig bits for expects

This commit is contained in:
Folkert 2022-07-13 14:40:07 +02:00
parent 68c51782e6
commit d33beedcd0
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 48 additions and 0 deletions

View file

@ -0,0 +1,39 @@
const std = @import("std");
extern fn shm_open(name: *const i8, oflag: c_int, mode: c_uint) c_int;
extern fn mmap(addr: ?*anyopaque, length: c_uint, prot: c_int, flags: c_int, fd: c_int, offset: c_uint) *anyopaque;
extern fn kill(pid: c_int, sig: c_int) c_int;
extern fn getppid() c_int;
const SIGUSR1: c_int = 10;
const O_RDWR: c_int = 2;
const O_CREAT: c_int = 64;
pub const PROT_WRITE: c_int = 2;
pub const MAP_SHARED: c_int = 0x0001;
pub fn expectFailedStart() callconv(.C) [*]u8 {
const name = "/roc_expect_buffer"; // IMPORTANT: shared memory object names must begin with / and contain no other slashes!
const shared_fd = shm_open(@ptrCast(*const i8, name), O_RDWR | O_CREAT, 0o666);
const shared_ptr = mmap(
null,
4096,
PROT_WRITE,
MAP_SHARED,
shared_fd,
0,
);
const ptr = @ptrCast([*]u8, shared_ptr);
return ptr;
}
pub fn expectFailedFinalize() callconv(.C) void {
const parent_pid = getppid();
_ = kill(parent_pid, SIGUSR1);
}

View file

@ -2,6 +2,7 @@ const std = @import("std");
const builtin = @import("builtin");
const math = std.math;
const utils = @import("utils.zig");
const expect = @import("expect.zig");
const ROC_BUILTINS = "roc_builtins";
const NUM = "num";
@ -162,6 +163,11 @@ comptime {
@export(utils.panic, .{ .name = "roc_builtins.utils." ++ "panic", .linkage = .Weak });
if (builtin.target.cpu.arch != .wasm32) {
exportUtilsFn(expect.expectFailedStart, "expect_failed_start");
exportUtilsFn(expect.expectFailedFinalize, "expect_failed_finalize");
}
if (builtin.target.cpu.arch == .aarch64) {
@export(__roc_force_setjmp, .{ .name = "__roc_force_setjmp", .linkage = .Weak });
@export(__roc_force_longjmp, .{ .name = "__roc_force_longjmp", .linkage = .Weak });

View file

@ -375,6 +375,9 @@ pub const UTILS_INCREF: &str = "roc_builtins.utils.incref";
pub const UTILS_DECREF: &str = "roc_builtins.utils.decref";
pub const UTILS_DECREF_CHECK_NULL: &str = "roc_builtins.utils.decref_check_null";
pub const UTILS_EXPECT_FAILED_START: &str = "roc_builtins.utils.expect_failed_start";
pub const UTILS_EXPECT_FAILED_FINALIZE: &str = "roc_builtins.utils.expect_failed_finalize";
pub const UTILS_LONGJMP: &str = "longjmp";
pub const UTILS_SETJMP: &str = "setjmp";