diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-27 04:35:46 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-27 04:35:46 -0700 |
commit | 44f14e515e111164c552d0f9a9ad8dce38b3d136 (patch) | |
tree | 86c3a21c8b4c95eafcf4c5efa292f2341d1d59a1 | |
parent | 5dce742cc9593dce4c07233d15e11e1320ef6d13 (diff) | |
download | dartboat-44f14e515e111164c552d0f9a9ad8dce38b3d136.tar.gz dartboat-44f14e515e111164c552d0f9a9ad8dce38b3d136.tar.xz |
web: move some memory management to c; use ccall wrapper less
-rw-r--r-- | web/dartboat_wasm.c | 16 | ||||
-rw-r--r-- | web/static/dartboat.js | 20 |
2 files changed, 15 insertions, 21 deletions
diff --git a/web/dartboat_wasm.c b/web/dartboat_wasm.c index c3091e9..8c871f1 100644 --- a/web/dartboat_wasm.c +++ b/web/dartboat_wasm.c @@ -170,6 +170,7 @@ EMSCRIPTEN_KEEPALIVE void draw_boat_throwing(int pts, char *str) update_player_rem(2, rem - pts); EM_ASM({setPromptInput($0)}, pts_str); EM_ASM({promptMsgR($0)}, str); + free(str); } void handle_next(); @@ -206,21 +207,22 @@ EMSCRIPTEN_KEEPALIVE void boat_visit() int pts = 0; char str[15] = {0}; + size_t len_str = 0; for (int i = 0; i < v->n_darts; ++i) { pts += segment_points(v->darts[i]); char *seg_name = segment_name(v->darts[i]); - strcat(i ? strcat(str, "-") : str, seg_name); + len_str += sprintf(str + len_str, i ? "-%s" : "%s", seg_name); free(seg_name); - EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)}, - "draw_boat_throwing", delay_ms * (i+1), - "number", pts, "string", str); + char *tmp = malloc(len_str + 1); // free in draw_boat_throwing + memcpy(tmp, str, len_str + 1); + EM_ASM({scheduleCCall($0, $1, $2, $3)}, "draw_boat_throwing", + delay_ms * (i+1), pts, tmp); } - EM_ASM({scheduleCCall($0, $1, $2, $3, $4, $5)}, - "end_boat_visit", delay_ms * (v->n_darts + 1), - "number", l->rem, "number", avg); + EM_ASM({scheduleCCall($0, $1, $2, $3)}, "end_boat_visit", + delay_ms * (v->n_darts + 1), l->rem, avg); } void clear_player_info(int pn) diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 06d9d10..9921cf3 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -15,15 +15,8 @@ function clearOi() { } function scheduleCCall(f, ms, ...args) { - let types = []; - let vals = []; - - for (let i = 0; i < args.length/2; i++) - vals[i] = (types[i] = UTF8ToString(args[i*2])) == 'string' ? - UTF8ToString(args[i*2+1]) : args[i*2+1]; - - let func = UTF8ToString(f); - setTimeout(() => Module.ccall(func, null, types, vals), ms); + const func = UTF8ToString(f); + setTimeout(() => Module[`_${func}`](...args), ms); } function promptGet() { @@ -142,7 +135,7 @@ function drawVisit(visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts) { function setDelay(val, update_opts = true) { if (update_opts) localStorage.setItem('dartboat_delay', val); - Module.ccall('set_delay', null, ['number'], [val]); + _set_delay(Number(val)); } function updateDelay(val) { @@ -151,7 +144,7 @@ function updateDelay(val) { function setStdev(val, update_opts = true) { if (update_opts) localStorage.setItem('dartboat_stdev', val); - Module.ccall('set_stdev', null, ['number', 'number'], [val, val]); + _set_stdev(Number(val), Number(val)); } function updateStdev(val) { @@ -212,8 +205,7 @@ document.addEventListener('keydown', e => { function boatReady() { if (document.readyState === 'loading') - document.addEventListener('DOMContentLoaded', () => - Module.ccall('init')); + document.addEventListener('DOMContentLoaded', () => _init()); else - Module.ccall('init'); + _init(); } |