@@ -6,10 +6,7 @@ comptime {
6
6
_ = std .testing .refAllDeclsRecursive (@This ());
7
7
}
8
8
9
- extern fn emscripten_err ([* c ]const u8 ) void ;
10
- extern fn emscripten_console_error ([* c ]const u8 ) void ;
11
- extern fn emscripten_console_warn ([* c ]const u8 ) void ;
12
- extern fn emscripten_console_log ([* c ]const u8 ) void ;
9
+ pub extern fn emscripten_sleep (ms : u32 ) void ;
13
10
14
11
pub const MainLoopCallback = * const fn () callconv (.C ) void ;
15
12
extern fn emscripten_set_main_loop (MainLoopCallback , c_int , c_int ) void ;
@@ -21,6 +18,123 @@ pub const AnimationFrameCallback = *const fn (f64, ?*anyopaque) callconv(.C) c_i
21
18
extern fn emscripten_request_animation_frame_loop (AnimationFrameCallback , ? * anyopaque ) void ;
22
19
pub const requestAnimationFrameLoop = emscripten_request_animation_frame_loop ;
23
20
21
+ pub const EmscriptenResult = enum (i16 ) {
22
+ success = 0 ,
23
+ deferred = 1 ,
24
+ not_supported = -1 ,
25
+ failed_not_deferred = -2 ,
26
+ invalid_target = -3 ,
27
+ unknown_target = -4 ,
28
+ invalid_param = -5 ,
29
+ failed = -6 ,
30
+ no_data = -7 ,
31
+ timed_out = -8 ,
32
+ };
33
+ pub const CanvasSizeChangedCallback = * const fn (
34
+ i16 ,
35
+ * anyopaque ,
36
+ ? * anyopaque ,
37
+ ) callconv (.C ) c_int ;
38
+ pub fn setResizeCallback (
39
+ cb : CanvasSizeChangedCallback ,
40
+ use_capture : bool ,
41
+ user_data : ? * anyopaque ,
42
+ ) EmscriptenResult {
43
+ const result = emscripten_set_resize_callback_on_thread (
44
+ "2" ,
45
+ user_data ,
46
+ @intFromBool (use_capture ),
47
+ cb ,
48
+ 2 ,
49
+ );
50
+ return @enumFromInt (result );
51
+ }
52
+ extern fn emscripten_set_resize_callback_on_thread (
53
+ [* :0 ]const u8 ,
54
+ ? * anyopaque ,
55
+ c_int ,
56
+ CanvasSizeChangedCallback ,
57
+ c_int ,
58
+ ) c_int ;
59
+
60
+ pub fn getElementCssSize (
61
+ target_id : [:0 ]const u8 ,
62
+ width : * f64 ,
63
+ height : * f64 ,
64
+ ) EmscriptenResult {
65
+ return @enumFromInt (emscripten_get_element_css_size (
66
+ target_id ,
67
+ width ,
68
+ height ,
69
+ ));
70
+ }
71
+ extern fn emscripten_get_element_css_size ([* :0 ]const u8 , * f64 , * f64 ) c_int ;
72
+
73
+ // EmmalocAllocator allocator
74
+ // use with linker flag -sMALLOC=emmalloc
75
+ // for details see docs: https://github.com/emscripten-core/emscripten/blob/main/system/lib/emmalloc.c
76
+ extern fn emmalloc_memalign (u32 , u32 ) ? * anyopaque ;
77
+ extern fn emmalloc_realloc_try (? * anyopaque , u32 ) ? * anyopaque ;
78
+ extern fn emmalloc_free (? * anyopaque ) void ;
79
+ pub const EmmalocAllocator = struct {
80
+ const Self = @This ();
81
+ dummy : u32 = undefined ,
82
+
83
+ pub fn allocator (self : * Self ) std.mem.Allocator {
84
+ return .{
85
+ .ptr = self ,
86
+ .vtable = &.{
87
+ .alloc = & alloc ,
88
+ .resize = & resize ,
89
+ .free = & free ,
90
+ },
91
+ };
92
+ }
93
+
94
+ fn alloc (
95
+ ctx : * anyopaque ,
96
+ len : usize ,
97
+ ptr_align_log2 : u8 ,
98
+ return_address : usize ,
99
+ ) ? [* ]u8 {
100
+ _ = ctx ;
101
+ _ = return_address ;
102
+ const ptr_align : u32 = @as (u32 , 1 ) << @as (u5 , @intCast (ptr_align_log2 ));
103
+ if (! std .math .isPowerOfTwo (ptr_align )) unreachable ;
104
+ const ptr = emmalloc_memalign (ptr_align , len ) orelse return null ;
105
+ return @ptrCast (ptr );
106
+ }
107
+
108
+ fn resize (
109
+ ctx : * anyopaque ,
110
+ buf : []u8 ,
111
+ buf_align_log2 : u8 ,
112
+ new_len : usize ,
113
+ return_address : usize ,
114
+ ) bool {
115
+ _ = ctx ;
116
+ _ = return_address ;
117
+ _ = buf_align_log2 ;
118
+ return emmalloc_realloc_try (buf .ptr , new_len ) != null ;
119
+ }
120
+
121
+ fn free (
122
+ ctx : * anyopaque ,
123
+ buf : []u8 ,
124
+ buf_align_log2 : u8 ,
125
+ return_address : usize ,
126
+ ) void {
127
+ _ = ctx ;
128
+ _ = buf_align_log2 ;
129
+ _ = return_address ;
130
+ return emmalloc_free (buf .ptr );
131
+ }
132
+ };
133
+
134
+ extern fn emscripten_err ([* c ]const u8 ) void ;
135
+ extern fn emscripten_console_error ([* c ]const u8 ) void ;
136
+ extern fn emscripten_console_warn ([* c ]const u8 ) void ;
137
+ extern fn emscripten_console_log ([* c ]const u8 ) void ;
24
138
/// std.panic impl
25
139
pub fn panic (msg : []const u8 , error_return_trace : ? * std.builtin.StackTrace , ret_addr : ? usize ) noreturn {
26
140
_ = error_return_trace ;
0 commit comments