From 91975ac3a86e3644503e263d53c1ea870816354a Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Wed, 27 Apr 2022 04:52:13 -0700 Subject: web: malloc c strings explicitly; pass null pointer when empty --- web/static/dartboat.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'web/static') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 9921cf3..e43488b 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -47,8 +47,20 @@ function promptMsgR(p) { document.getElementById('prompt-msg-r').textContent = UTF8ToString(p); } +function stringToCString(str) { // caller must free + const len = lengthBytesUTF8(str) + 1; + const cstr = _malloc(len); + stringToUTF8(str, cstr, len); + return cstr; +} + function promptHandle(command, data) { - Module.ccall('prompt_handle', null, ['string', 'string'], [command, data]); + const str_c = stringToCString(command); + const str_d = data && stringToCString(data); + _prompt_handle(str_c, str_d); + + _free(str_c); + if (str_d) _free(str_d); } function setKeypad(keypad) { -- cgit v1.2.3-70-g09d2