-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzemscripten.zig
188 lines (170 loc) · 5.29 KB
/
zemscripten.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Zig bindings and glue for Emscripten
const std = @import("std");
comptime {
_ = std.testing.refAllDeclsRecursive(@This());
}
pub extern fn emscripten_sleep(ms: u32) void;
pub const MainLoopCallback = *const fn () callconv(.C) void;
extern fn emscripten_set_main_loop(MainLoopCallback, c_int, c_int) void;
pub fn setMainLoop(cb: MainLoopCallback, maybe_fps: ?i16, simulate_infinite_loop: bool) void {
emscripten_set_main_loop(cb, if (maybe_fps) |fps| fps else -1, @intFromBool(simulate_infinite_loop));
}
pub const AnimationFrameCallback = *const fn (f64, ?*anyopaque) callconv(.C) c_int;
extern fn emscripten_request_animation_frame_loop(AnimationFrameCallback, ?*anyopaque) void;
pub const requestAnimationFrameLoop = emscripten_request_animation_frame_loop;
pub const EmscriptenResult = enum(i16) {
success = 0,
deferred = 1,
not_supported = -1,
failed_not_deferred = -2,
invalid_target = -3,
unknown_target = -4,
invalid_param = -5,
failed = -6,
no_data = -7,
timed_out = -8,
};
pub const CanvasSizeChangedCallback = *const fn (
i16,
*anyopaque,
?*anyopaque,
) callconv(.C) c_int;
pub fn setResizeCallback(
cb: CanvasSizeChangedCallback,
use_capture: bool,
user_data: ?*anyopaque,
) EmscriptenResult {
const result = emscripten_set_resize_callback_on_thread(
"2",
user_data,
@intFromBool(use_capture),
cb,
2,
);
return @enumFromInt(result);
}
extern fn emscripten_set_resize_callback_on_thread(
[*:0]const u8,
?*anyopaque,
c_int,
CanvasSizeChangedCallback,
c_int,
) c_int;
pub fn getElementCssSize(
target_id: [:0]const u8,
width: *f64,
height: *f64,
) EmscriptenResult {
return @enumFromInt(emscripten_get_element_css_size(
target_id,
width,
height,
));
}
extern fn emscripten_get_element_css_size([*:0]const u8, *f64, *f64) c_int;
// EmmalocAllocator allocator
// use with linker flag -sMALLOC=emmalloc
// for details see docs: https://github.com/emscripten-core/emscripten/blob/main/system/lib/emmalloc.c
extern fn emmalloc_memalign(u32, u32) ?*anyopaque;
extern fn emmalloc_realloc_try(?*anyopaque, u32) ?*anyopaque;
extern fn emmalloc_free(?*anyopaque) void;
pub const EmmalocAllocator = struct {
const Self = @This();
dummy: u32 = undefined,
pub fn allocator(self: *Self) std.mem.Allocator {
return .{
.ptr = self,
.vtable = &.{
.alloc = &alloc,
.resize = &resize,
.remap = &remap,
.free = &free,
},
};
}
fn alloc(
ctx: *anyopaque,
len: usize,
ptr_align_log2: std.mem.Alignment,
return_address: usize,
) ?[*]u8 {
_ = ctx;
_ = return_address;
const ptr_align: u32 = @as(u32, 1) << @as(u5, @intFromEnum(ptr_align_log2));
if (!std.math.isPowerOfTwo(ptr_align)) unreachable;
const ptr = emmalloc_memalign(ptr_align, len) orelse return null;
return @ptrCast(ptr);
}
fn resize(
ctx: *anyopaque,
buf: []u8,
buf_align_log2: std.mem.Alignment,
new_len: usize,
return_address: usize,
) bool {
_ = ctx;
_ = return_address;
_ = buf_align_log2;
return emmalloc_realloc_try(buf.ptr, new_len) != null;
}
fn remap(
ctx: *anyopaque,
buf: []u8,
buf_align_log2: std.mem.Alignment,
new_len: usize,
return_address: usize,
) ?[*]u8 {
return if (resize(ctx, buf, buf_align_log2, new_len, return_address)) buf.ptr else null;
}
fn free(
ctx: *anyopaque,
buf: []u8,
buf_align_log2: std.mem.Alignment,
return_address: usize,
) void {
_ = ctx;
_ = buf_align_log2;
_ = return_address;
return emmalloc_free(buf.ptr);
}
};
extern fn emscripten_err([*c]const u8) void;
extern fn emscripten_console_error([*c]const u8) void;
extern fn emscripten_console_warn([*c]const u8) void;
extern fn emscripten_console_log([*c]const u8) void;
/// std.panic impl
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
_ = error_return_trace;
_ = ret_addr;
var buf: [1024]u8 = undefined;
const error_msg: [:0]u8 = std.fmt.bufPrintZ(&buf, "PANIC! {s}", .{msg}) catch unreachable;
emscripten_err(error_msg.ptr);
while (true) {
@breakpoint();
}
}
/// std.log impl
pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
const level_txt = comptime level.asText();
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
const prefix = level_txt ++ prefix2;
var buf: [1024]u8 = undefined;
const msg = std.fmt.bufPrintZ(buf[0 .. buf.len - 1], prefix ++ format, args) catch |err| {
switch (err) {
error.NoSpaceLeft => {
emscripten_console_error("log message too long, skipped.");
return;
},
}
};
switch (level) {
.err => emscripten_console_error(@ptrCast(msg.ptr)),
.warn => emscripten_console_warn(@ptrCast(msg.ptr)),
else => emscripten_console_log(@ptrCast(msg.ptr)),
}
}