-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathwasm_wasm3.c
More file actions
353 lines (300 loc) · 9.82 KB
/
Copy pathwasm_wasm3.c
File metadata and controls
353 lines (300 loc) · 9.82 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <wasm3.h>
#include <m3_env.h>
#include "../wasm.h"
#include "../runtime.h"
static M3Environment* env;
static M3Runtime* runtime;
static M3Module* module;
static M3Function* start;
static M3Function* update;
static m3ApiRawFunction (blit) {
m3ApiGetArgMem(const uint8_t*, sprite);
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
m3ApiGetArg(int, width);
m3ApiGetArg(int, height);
m3ApiGetArg(int, flags);
w4_runtimeBlit(sprite, x, y, width, height, flags);
m3ApiSuccess();
}
static m3ApiRawFunction (blitSub) {
m3ApiGetArgMem(const uint8_t*, sprite);
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
m3ApiGetArg(int, width);
m3ApiGetArg(int, height);
m3ApiGetArg(int, srcX);
m3ApiGetArg(int, srcY);
m3ApiGetArg(int, stride);
m3ApiGetArg(int, flags);
w4_runtimeBlitSub(sprite, x, y, width, height, srcX, srcY, stride, flags);
m3ApiSuccess();
}
static m3ApiRawFunction (line) {
m3ApiGetArg(int, x1);
m3ApiGetArg(int, y1);
m3ApiGetArg(int, x2);
m3ApiGetArg(int, y2);
w4_runtimeLine(x1, y1, x2, y2);
m3ApiSuccess();
}
static m3ApiRawFunction (hline) {
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
m3ApiGetArg(int, len);
w4_runtimeHLine(x, y, len);
m3ApiSuccess();
}
static m3ApiRawFunction (vline) {
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
m3ApiGetArg(int, len);
w4_runtimeVLine(x, y, len);
m3ApiSuccess();
}
static m3ApiRawFunction (oval) {
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
m3ApiGetArg(int, width);
m3ApiGetArg(int, height);
w4_runtimeOval(x, y, width, height);
m3ApiSuccess();
}
static m3ApiRawFunction (rect) {
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
m3ApiGetArg(int, width);
m3ApiGetArg(int, height);
w4_runtimeRect(x, y, width, height);
m3ApiSuccess();
}
static m3ApiRawFunction (text) {
m3ApiGetArgMem(const char*, str);
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
w4_runtimeText(str, x, y);
m3ApiSuccess();
}
static m3ApiRawFunction (textUtf8) {
m3ApiGetArgMem(const uint8_t*, str);
m3ApiGetArg(int, byteLength);
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
w4_runtimeTextUtf8(str, byteLength, x, y);
m3ApiSuccess();
}
static m3ApiRawFunction (textUtf16) {
m3ApiGetArgMem(const uint16_t*, str);
m3ApiGetArg(int, byteLength);
m3ApiGetArg(int, x);
m3ApiGetArg(int, y);
w4_runtimeTextUtf16(str, byteLength, x, y);
m3ApiSuccess();
}
static m3ApiRawFunction (tone) {
m3ApiGetArg(int, frequency);
m3ApiGetArg(int, duration);
m3ApiGetArg(int, volume);
m3ApiGetArg(int, flags);
w4_runtimeTone(frequency, duration, volume, flags);
m3ApiSuccess();
}
static m3ApiRawFunction (diskr) {
m3ApiReturnType(int);
m3ApiGetArgMem(uint8_t*, dest);
m3ApiGetArg(int, size);
m3ApiReturn(w4_runtimeDiskr(dest, size));
}
static m3ApiRawFunction (diskw) {
m3ApiReturnType(int);
m3ApiGetArgMem(const uint8_t*, src);
m3ApiGetArg(int, size);
m3ApiReturn(w4_runtimeDiskw(src, size));
}
static m3ApiRawFunction (trace) {
m3ApiGetArgMem(const char*, str);
w4_runtimeTrace(str);
m3ApiSuccess();
}
static m3ApiRawFunction (traceUtf8) {
m3ApiGetArgMem(const uint8_t*, str);
m3ApiGetArg(int, byteLength);
w4_runtimeTraceUtf8(str, byteLength);
m3ApiSuccess();
}
static m3ApiRawFunction (traceUtf16) {
m3ApiGetArgMem(const uint16_t*, str);
m3ApiGetArg(int, byteLength);
w4_runtimeTraceUtf16(str, byteLength);
m3ApiSuccess();
}
static m3ApiRawFunction (tracef) {
m3ApiGetArgMem(const char*, str);
m3ApiGetArgMem(const void*, stack);
w4_runtimeTracef(str, stack);
m3ApiSuccess();
}
static bool storeLoaded = false;
static bool cartCrashed = false;
static void check (M3Result result) {
if (result != m3Err_none) {
M3ErrorInfo info;
m3_GetErrorInfo(runtime, &info);
if (storeLoaded) {
fprintf(stderr, "WASM warning: %s (%s)\n", result, info.message);
update = NULL;
start = NULL;
cartCrashed = true;
} else {
fprintf(stderr, "WASM error: %s (%s)\n", result, info.message);
exit(1);
}
}
}
bool w4_wasmDidCrash(void) {
if (cartCrashed) {
cartCrashed = false;
return true;
}
return false;
}
void w4_wasmSetStoreLoaded(bool value) {
storeLoaded = value;
}
uint8_t* w4_wasmInit () {
env = m3_NewEnvironment();
// This is an arbitrary limit corresponding to the implementation details
// of the wasm3 interpreter. It's unrelated to the resource constraints of
// the wasm4 VM. Making this too small will ultimately result in `[trap]
// stack overflow` at the command line.
//
// Using 64 KB, since this is the default of wasm3 standalone binary on
// desktop platforms (from wasm3/platforms/app/main.c).
uint32_t wasm3StackSize = 64 * 1024;
runtime = m3_NewRuntime(env, wasm3StackSize, NULL);
runtime->memory.maxPages = 1;
ResizeMemory(runtime, 1);
return m3_GetMemory(runtime, NULL, 0);
}
void w4_wasmDestroy () {
m3_FreeRuntime(runtime);
m3_FreeEnvironment(env);
env = NULL;
runtime = NULL;
module = NULL;
start = NULL;
update = NULL;
}
uint8_t* w4_wasmGetMemory () {
return m3_GetMemory(runtime, NULL, 0);
}
void w4_wasmLoadModule (const uint8_t* wasmBuffer, int byteLength) {
check(m3_ParseModule(env, &module, wasmBuffer, byteLength));
// wasm3 will reallocate a new memory if the module doesn't import a memory. We set this to
// prevent that from happening: https://github.com/aduros/wasm4/issues/292
module->memoryImported = true;
check(m3_LoadModule(runtime, module));
m3_LinkRawFunction(module, "env", "blit", "v(iiiiii)", blit);
m3_LinkRawFunction(module, "env", "blitSub", "v(iiiiiiiii)", blitSub);
m3_LinkRawFunction(module, "env", "line", "v(iiii)", line);
m3_LinkRawFunction(module, "env", "hline", "v(iii)", hline);
m3_LinkRawFunction(module, "env", "vline", "v(iii)", vline);
m3_LinkRawFunction(module, "env", "oval", "v(iiii)", oval);
m3_LinkRawFunction(module, "env", "rect", "v(iiii)", rect);
m3_LinkRawFunction(module, "env", "text", "v(iii)", text);
m3_LinkRawFunction(module, "env", "textUtf8", "v(iiii)", textUtf8);
m3_LinkRawFunction(module, "env", "textUtf16", "v(iiii)", textUtf16);
m3_LinkRawFunction(module, "env", "tone", "v(iiii)", tone);
m3_LinkRawFunction(module, "env", "diskr", "i(ii)", diskr);
m3_LinkRawFunction(module, "env", "diskw", "i(ii)", diskw);
m3_LinkRawFunction(module, "env", "trace", "v(i)", trace);
m3_LinkRawFunction(module, "env", "traceUtf8", "v(ii)", traceUtf8);
m3_LinkRawFunction(module, "env", "traceUtf16", "v(ii)", traceUtf16);
m3_LinkRawFunction(module, "env", "tracef", "v(ii)", tracef);
#ifndef NDEBUG
M3ErrorInfo error;
m3_GetErrorInfo(runtime, &error);
if (error.result) {
fprintf(stderr, "Error in load: %s: %s\n", error.result, error.message);
}
#endif
m3_FindFunction(&start, runtime, "start");
m3_FindFunction(&update, runtime, "update");
// First call wasm built-in start
check(m3_RunStart(module));
// Call WASI start functions
M3Function* func;
m3_FindFunction(&func, runtime, "_start");
if (func) {
check(m3_CallV(func));
}
m3_FindFunction(&func, runtime, "_initialize");
if (func) {
check(m3_CallV(func));
}
}
int w4_wasmLoadModuleSafe (const uint8_t* wasmBuffer, int byteLength) {
M3Result result;
storeLoaded = true;
result = m3_ParseModule(env, &module, wasmBuffer, byteLength);
if (result) {
fprintf(stderr, "WASM parse error: %s\n", result);
return -1;
}
module->memoryImported = true;
result = m3_LoadModule(runtime, module);
if (result) {
fprintf(stderr, "WASM load error: %s\n", result);
return -1;
}
m3_LinkRawFunction(module, "env", "blit", "v(iiiiii)", blit);
m3_LinkRawFunction(module, "env", "blitSub", "v(iiiiiiiii)", blitSub);
m3_LinkRawFunction(module, "env", "line", "v(iiii)", line);
m3_LinkRawFunction(module, "env", "hline", "v(iii)", hline);
m3_LinkRawFunction(module, "env", "vline", "v(iii)", vline);
m3_LinkRawFunction(module, "env", "oval", "v(iiii)", oval);
m3_LinkRawFunction(module, "env", "rect", "v(iiii)", rect);
m3_LinkRawFunction(module, "env", "text", "v(iii)", text);
m3_LinkRawFunction(module, "env", "textUtf8", "v(iiii)", textUtf8);
m3_LinkRawFunction(module, "env", "textUtf16", "v(iiii)", textUtf16);
m3_LinkRawFunction(module, "env", "tone", "v(iiii)", tone);
m3_LinkRawFunction(module, "env", "diskr", "i(ii)", diskr);
m3_LinkRawFunction(module, "env", "diskw", "i(ii)", diskw);
m3_LinkRawFunction(module, "env", "trace", "v(i)", trace);
m3_LinkRawFunction(module, "env", "traceUtf8", "v(ii)", traceUtf8);
m3_LinkRawFunction(module, "env", "traceUtf16", "v(ii)", traceUtf16);
m3_LinkRawFunction(module, "env", "tracef", "v(ii)", tracef);
m3_FindFunction(&start, runtime, "start");
m3_FindFunction(&update, runtime, "update");
result = m3_RunStart(module);
if (result) {
fprintf(stderr, "WASM start error: %s\n", result);
return -1;
}
M3Function* func;
m3_FindFunction(&func, runtime, "_start");
if (func) {
result = m3_CallV(func);
if (result) {
fprintf(stderr, "WASM _start warning: %s (ignored)\n", result);
}
}
m3_FindFunction(&func, runtime, "_initialize");
if (func) {
result = m3_CallV(func);
if (result) {
fprintf(stderr, "WASM _initialize warning: %s (ignored)\n", result);
}
}
return 0;
}
void w4_wasmCallStart () {
if (start) {
check(m3_CallV(start));
}
}
void w4_wasmCallUpdate () {
if (update) {
check(m3_CallV(update));
}
}