-
Notifications
You must be signed in to change notification settings - Fork 1
/
alexgames_wasm_wrapper.js
374 lines (334 loc) · 10.2 KB
/
alexgames_wasm_wrapper.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
function init_game_api(game_name) {
console.log("[init] init_game_api");
let rc = Module.ccall("init_game_api", "number",
["string", "number"],
[game_name, game_name.length]);
if (rc == 0) {
throw ("init_game_api from js returning " + rc);
}
return rc;
}
function start_game(ptr) {
console.log("[init] start_game, ptr=", ptr.toString(16));
let rc = Module.ccall("start_game", null,
["number"],
[ptr]);
if (rc == 0) {
throw ("start_game from js returning " + rc);
}
return rc;
}
function destroy_game(ptr) {
disable_all_events();
let rc = Module.ccall("destroy_game", null,
["number"],
[ptr]);
if (rc == 0) {
throw ("destroy_game from js returning " + rc);
}
}
function get_game_list() {
let game_count = Module.ccall("get_game_count", "number", [], []);
let list = new Array(game_count);
for (let i=0; i<game_count; i++) {
let str_ptr = Module.ccall("get_game_name", "number", ["number"], [i]);
let str = Module.UTF8ToString(str_ptr);
list[i] = str;
}
return list;
}
/**
* Called when the screen needs to be drawn.
*
* By default this only happens initially,
* but if the `set_timer_period_ms(period_ms)` API is called then
* it is called again every `period_ms` milliseconds.
*
* TODO: maybe it makes sense to have a separate API for this
*/
function update(ptr, dt_ms) {
if (dt_ms === undefined) {
dt_ms = 0;
}
return Module.ccall("update", null, ["number", "number"], [ptr, dt_ms]);
}
// Previously this was an API to receive a string from the user.
// I used it in Go for command line input, and then completely forgot about it,
// since using mouse/touch seemed a lot more natural.
// I'm repurposing it now to receive strings from the user for "enter your name",
// specifically in the poker chips game, where a name is helpful to identify who
// is who in the real world.
// I'm also adding an "is_cancelled" argument, to indicate that the "prompt string" popup
// was closed.
function handle_user_string_input(ptr, user_input, is_cancelled) {
return Module.ccall("handle_user_string_input", null,
["number", "string", "number", "number"],
[ptr, user_input, user_input.length, is_cancelled]);
}
function handle_user_clicked(ptr, pos_y, pos_x) {
pos_y = Math.floor(pos_y);
pos_x = Math.floor(pos_x);
return Module.ccall("handle_user_clicked", null,
["number", "number", "number"],
[ptr, pos_y, pos_x]);
}
function handle_mousemove(ptr, pos_y, pos_x, buttons) {
pos_y = Math.floor(pos_y);
pos_x = Math.floor(pos_x);
return Module.ccall("handle_mousemove", null,
["number", "number", "number", "number"],
[ptr, pos_y, pos_x, buttons]);
}
function handle_mouse_evt(ptr, evt_id, pos_y, pos_x, buttons) {
pos_y = Math.floor(pos_y);
pos_x = Math.floor(pos_x);
return Module.ccall("handle_mouse_evt", null,
["number", "number", "number", "number", "number"],
[ptr, evt_id, pos_y, pos_x, buttons]);
}
function handle_wheel_evt(ptr, delta_y, delta_x) {
return Module.ccall("handle_wheel_changed", null,
["number", "number", "number"],
[ptr, delta_y, delta_x]);
}
function handle_key_evt(ptr, evt_id, code) {
let val = Module.ccall("handle_key_evt", ["number"],
["number", "string", "string"],
[ptr, evt_id, code]);
return !!val;
}
// writes javascript string `str` to address `ptr`,
// up to max size `ptr_size`.
// Adds null terminator.
function write_str(ptr, ptr_size, str) {
if (ptr_size <= 0) {
console.error("write_str: ptr_size is ", ptr_size);
return;
}
// loop from start of string to end inclusive (including null terminator)
for (let i=0; i<=str.length; i++) {
if (i >= ptr_size) {
console.error("write_str: ptr_size", ptr_size, "too small for string len", str.length, "str: ", str);
Module.setValue(ptr, 0);
return;
}
let char_val;
if (i < str.length) {
char_val = str.charCodeAt(i);
} else if (i == str.length) {
char_val = 0;
} else {
console.error("programming error: write_str, i = ", i, ", str.length =", str.length);
char_val = 0;
Module.setValue(ptr + i, char_val);
return;
}
console.log("writing value", char_val, "to ptr", ptr + i);
Module.setValue(ptr + i, char_val);
}
}
function handle_touch_evt(canvas, ptr, evt) {
evt.preventDefault();
let rect = canvas.getBoundingClientRect();
let sizeof_double = 8;
let sizeof_id = 8;
let sizeof_elem = 2 * sizeof_double + sizeof_id;
let touch_ary_ptr = Module._malloc(evt.changedTouches.length * sizeof_elem);
try {
for (let i=0; i<evt.changedTouches.length; i++) {
let touch = evt.changedTouches[i];
let offset = touch_ary_ptr + i * sizeof_elem;
const x = (touch.clientX - rect.left)/(rect.right - rect.left)* canvas.width;
const y = (touch.clientY - rect.top)/(rect.bottom - rect.top) * canvas.height;
Module.setValue(offset + 0, touch.identifier, 'i64');
Module.setValue(offset + sizeof_id, y, 'double');
Module.setValue(offset + sizeof_id + sizeof_double, x, 'double');
}
return Module.ccall("handle_touch_evt", null,
["number", "string", "number", "number", "number"],
[ptr, evt.type, evt.type.length, touch_ary_ptr, evt.changedTouches.length]);
} finally {
Module._free(touch_ary_ptr);
}
}
function i8_to_u8(val) {
if (val < 0) { return 256 + val; }
else { return val; }
}
function newByteArray(msg) {
let msg_ptr = Module._malloc(msg.length);
for (let i=0; i<msg.length; i++) {
Module.setValue(msg_ptr + i, (msg.charCodeAt(i)), 'i8');
}
return msg_ptr;
}
function handle_msg_received(ptr, msg_src, msg) {
let msg_ptr = newByteArray(msg);
try {
let rc = Module.ccall("handle_msg_received", null,
["number", "string", "number", "number", "number"],
[ptr, msg_src, msg_src.length, msg_ptr, msg.length]);
return rc;
} finally {
Module._free(msg_ptr);
}
}
function handle_btn_clicked(ptr, btn_id) {
console.log("js: handle_btn_clicked", btn_id);
return Module.ccall("handle_btn_clicked", null,
["number", "string"],
[ptr, btn_id]);
}
function write_popup_state_to_mem(dst, popup_state) {
let ptr = dst;
Module.setValue(ptr, popup_state.items.length, 'i32');
ptr += 4;
for (let item of popup_state.items) {
Module.setValue(ptr, item.id, 'i32');
ptr += 4;
Module.setValue(ptr, item.selected, 'i32');
ptr += 4;
}
}
function handle_popup_btn_clicked(ptr, popup_id, btn_idx, popup_state) {
let popup_state_buff = Module._malloc(4096);
try {
write_popup_state_to_mem(popup_state_buff, popup_state);
return Module.ccall("handle_popup_btn_clicked", null,
["number", "string", "number", "number"],
[ptr, popup_id, btn_idx, popup_state_buff]);
} finally {
Module._free(popup_state_buff);
}
}
function handle_game_option_evt(ptr, option_type, option_id, value) {
try {
return Module.ccall("handle_game_option_evt", null,
["number", "number", "string", "number"],
[ptr, option_type, option_id, value]);
} finally {
}
}
function start_game_b64(ptr, state_b64, session_id) {
if (session_id === undefined) {
session_id = 0;
}
console.log("start_game_b64", state_b64);
return Module.ccall("start_game_b64", null,
["number", "number", "string", "number"],
[ptr, session_id, state_b64, state_b64.length]);
}
function get_byteary_func(ptr, func_name, buff_len) {
let buff = Module._malloc(buff_len);
try {
let state_len = Module.ccall(func_name, ["number"],
["number", "number", "number"],
[ptr, buff, buff_len]);
let state_str = Module.UTF8ToString(buff, state_len);
return state_str;
} finally {
Module._free(buff);
}
}
function get_state(ptr) {
return get_byteary_func(ptr, "get_state", 64*1024);
}
function get_init_state(ptr) {
return get_byteary_func(ptr, "get_init_state", 64*1024);
}
function new_file(ptr, fname) {
let f = Module.ccall("new_file", "number", ["number", "string"], [ptr, fname]);
return f;
}
function dump_array_buffer(dataView) {
let buff = new Uint8Array(16);
try {
let i=0;
while (i < dataView.byteLength) {
//let slice = arrayBuffer.slice(i, i+16);
//console.log(slice);
//i += 16;
for (let j=0; j<16; j++) {
buff[j] = dataView.getUint8(i+j);
}
console.log(buff);
i += 16;
}
} finally {
Module._free(buff);
}
}
// void write_to_file(void *L, FILE *f, void *data, size_t data_len) {
function write_array_buffer_to_file(ptr, f, arrayBuffer) {
let dataView = new DataView(arrayBuffer);
return write_data_view_to_file(ptr, f, dataView);
}
function write_data_view_to_file(ptr, f, dataView) {
const CHUNK_SIZE = 4096;
let buff = new Uint8Array(CHUNK_SIZE);
try {
let file_pos = 0;
while (file_pos < dataView.byteLength) {
let buff_len = CHUNK_SIZE;
for (let i=0; i<CHUNK_SIZE; i++) {
if (file_pos+i >= dataView.byteLength) {
buff_len = i;
break;
}
buff[i] = dataView.getUint8(file_pos+i);
}
let valid_buff = buff.slice(0, buff_len);
file_pos += CHUNK_SIZE;
//console.log(valid_buff);
Module.ccall("write_to_file", null,
["number", "number", "array", "number"],
[ptr, f, valid_buff, valid_buff.length]);
}
} finally {
Module._free(buff);
}
/*
let buff = Module._malloc(data_js_ary.length);
try {
for (let i=0; i<data_js_ary.length; i++) {
Module.setValue(buff + i, data_js_ary[i], 'i8');
}
Module.ccall("write_to_file", null,
["number", "number", "number", "number"],
[ptr, f, buff, data_js_ary.length]);
} finally {
Module._free(buff);
}
*/
}
function unzip_file(ptr, fname, dst_name) {
return Module.ccall("unzip_file", null,
["number", "string", "string"],
[ptr, fname, dst_name]);
}
function close_file(ptr, f) {
Module.ccall("close_file", [], ["number", "number"], [ptr, f]);
}
function dump_file(ptr, fname) {
Module.ccall("dump_file", [], ["number", "string"], [ptr, fname]);
}
// called from C code
function writeStringToPtr(dst_ptr, dst_ptr_size, word) {
for (let i=0; i<word.length; i++) {
if (i >= dst_ptr_size) {
console.error("writeStrToPtr: word.length >= dst_ptr_size", word, dst_ptr_size);
assert(false);
return;
}
Module.setValue(dst_ptr + i, word.charCodeAt(i), 'i8');
}
}
function update_dict() {
Module.ccall("update_dict", null, [], []);
}
// For debugging only
function lua_run_cmd(ptr, cmd) {
return Module.ccall("lua_run_cmd", null,
["number", "string", "number"],
[ptr, cmd, cmd.length]);
}