From 44f14e515e111164c552d0f9a9ad8dce38b3d136 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 27 Apr 2022 04:35:46 -0700 Subject: web: move some memory management to c; use ccall wrapper less --- web/dartboat_wasm.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'web/dartboat_wasm.c') 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) -- cgit v1.2.3-70-g09d2