-
Notifications
You must be signed in to change notification settings - Fork 0
/
ztracing_shared.js
161 lines (144 loc) · 3.32 KB
/
ztracing_shared.js
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
class Heap {
/**
* @param {WebAssembly.Memory} memory
*/
constructor(memory) {
this.memory = memory;
}
load_string(ptr, len) {
return new TextDecoder().decode(
new Uint8Array(this.memory.buffer.slice(ptr, ptr + len)).slice()
);
}
set_uint32(ptr, val) {
const view = new DataView(this.memory.buffer);
view.setUint32(ptr, val, true);
}
set_uint64(ptr, val) {
const view = new DataView(this.memory.buffer);
view.setBigUint64(ptr, val);
}
/**
*
* @param {number} dst
* @param {Uint8Array} src
*/
memcpy(dst, src) {
new Uint8Array(this.memory.buffer).set(src, dst);
}
}
/**
* @param {WebAssembly.Memory} memory
*/
function make_wasm_imports(memory, app) {
const heap = new Heap(memory);
return {
wasi_snapshot_preview1: {
args_get: unreachble,
args_sizes_get: unreachble,
fd_close: unreachble,
fd_fdstat_get: unreachble,
fd_fdstat_set_flags: unreachble,
fd_prestat_get: unreachble,
fd_prestat_dir_name: unreachble,
fd_read: unreachble,
fd_seek: unreachble,
fd_write: unreachble,
path_open: unreachble,
proc_exit: unreachble,
random_get: unreachble,
clock_time_get: (ctx, clock_id, precision, time) => {
if (clock_id != 1) {
unreachble();
}
heap.set_uint64(time, BigInt(Math.round(new Date().getTime() * 1e6)));
return 0;
},
},
js: {
log: (level, ptr, len) => {
const msg = heap.load_string(ptr, len);
switch (level) {
case 0: {
console.error(msg);
break;
}
case 1: {
console.warn(msg);
break;
}
case 2: {
console.info(msg);
break;
}
case 3: {
console.debug(msg);
break;
}
default: {
console.log("[level " + level + "] " + msg);
}
}
},
destory: (ref) => {
app.free_object(ref);
},
rendererCreateFontTexture: (width, height, pixels) => {
return app.renderer.createFontTexture(width, height, pixels);
},
rendererBufferData: (
vtx_buffer_ptr,
vtx_buffer_size,
idx_buffer_ptr,
idx_buffer_size
) => {
app.renderer.bufferData(
vtx_buffer_ptr,
vtx_buffer_size,
idx_buffer_ptr,
idx_buffer_size
);
},
rendererDraw: (
clip_rect_min_x,
clip_rect_min_y,
clip_rect_max_x,
clip_rect_max_y,
texture_ref,
idx_count,
idx_offset
) => {
app.renderer.draw(
clip_rect_min_x,
clip_rect_min_y,
clip_rect_max_x,
clip_rect_max_y,
texture_ref,
idx_count,
idx_offset
);
},
showOpenFilePicker: () => {
if (!app) {
unreachble();
}
app.show_open_file_picker();
},
get_current_timestamp: () => {
return performance.now();
},
},
env: {
memory: memory,
},
wasi: {
"thread-spawn": (arg) => {
return app.spawn_thread(arg);
},
},
};
}
const unreachble = () => {
throw new Error("unreachable");
};
export { make_wasm_imports, Heap };