From 170fb19425ca3d03a32cbebe6fb50f68683cd3b9 Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 5 Sep 2024 10:50:23 +0100 Subject: [PATCH] Emulator: ensure we include CallFunction stubs for Promises (fix regression after Promise refactor) --- ChangeLog | 1 + scripts/build_jswrapper.py | 80 ++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index 398e72e82..01a30423d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535) Bangle.js1: Switch to space-optimised sin/atan/atan2 to save enough space to continue building ESP32C3: don't allow AP *AND* STA mode at the same time - solves issues when just calling 'wifi.connect' at boot + Emulator: ensure we include CallFunction stubs for Promises (fix regression after Promise refactor) 2v24 : Bangle.js2: Add 'Bangle.touchRd()', 'Bangle.touchWr()' Bangle.js2: After Bangle.showTestScreen, put Bangle.js into a hard off state (not soft off) diff --git a/scripts/build_jswrapper.py b/scripts/build_jswrapper.py index 349bd1cd7..2d157d952 100755 --- a/scripts/build_jswrapper.py +++ b/scripts/build_jswrapper.py @@ -768,6 +768,41 @@ for lib in jsmodules: codeOut(' return "'+','.join(librarynames)+'";') codeOut('}') +def writeCallFunctionHackEntry(argSpecs, jsondata): + argSpec = getArgumentSpecifier(jsondata) + if not argSpec in argSpecs: + argSpecs.append(argSpec) + params = getParams(jsondata) + result = getResult(jsondata); + pTypes = [] + pValues = [] + if hasThis(jsondata): + pTypes.append("JsVar*") + pValues.append("thisParam") + cmd = ""; + cmdstart = ""; + cmdend = ""; + n = 0 + for param in params: + pTypes.append(toCType(param[1])); + if param[1]=="JsVarArray": + cmdstart = " JsVar *argArray = (paramCount>"+str(n)+")?jsvNewArray(¶mData["+str(n)+"],paramCount-"+str(n)+"):jsvNewEmptyArray();\n"; + pValues.append("argArray"); + cmdend = " jsvUnLock(argArray);\n\n"; + else: + pValues.append(toCUnbox(param[1])+"((paramCount>"+str(n)+")?paramData["+str(n)+"]:0)"); + n = n+1 + + codeOut(" case "+argSpec+": {"); + codeOut(" JsVar *result = 0;"); + if cmdstart: codeOut(cmdstart); + cmd = "(("+toCType(result[0])+"(*)("+",".join(pTypes)+"))function)("+",".join(pValues)+")"; + if result[0]: codeOut(" result = "+toCBox(result[0])+"("+cmd+");"); + else: codeOut(" "+cmd+";"); + if cmdend: codeOut(cmdend); + codeOut(" return result;"); + codeOut(" }"); + if "USE_CALLFUNCTION_HACK" in board.defines: codeOut('// on Emscripten and i386 we cant easily hack around function calls with floats/etc, plus we have enough') codeOut('// resources, so just brute-force by handling every call pattern we use in a switch') @@ -776,41 +811,18 @@ if "USE_CALLFUNCTION_HACK" in board.defines: #for argSpec in argSpecs: # codeOut(' case '+argSpec+":") argSpecs = [] + # Ensure we force-add any entries we need + writeCallFunctionHackEntry(argSpecs, {'type': 'function', 'name': 'X', 'generate': 'X', 'params': []}) # jswrap_io + writeCallFunctionHackEntry(argSpecs, {'type': 'method', 'class': 'X', 'name': 'X', 'generate': 'X', 'params': [['1', 'JsVar', '']]}) # jswrap_promise + writeCallFunctionHackEntry(argSpecs, {'type': 'method', 'class': 'X', 'name': 'X', 'generate': 'X', 'params': [['1', 'JsVar', ''],['2', 'JsVar', '']]}) # jswrap_promise + writeCallFunctionHackEntry(argSpecs, {'type': 'method', 'class': 'X', 'name': 'X', 'generate': 'X', 'params': [['1', 'JsVar', ''],['2', 'JsVar', ''],['3', 'JsVar', '']]}) # jswrap_promise + writeCallFunctionHackEntry(argSpecs, {'type': 'method', 'class': 'X', 'name': 'X', 'generate': 'X', 'params': [['1', 'bool', '']]}) # jswrap_pixljs/banglejs + writeCallFunctionHackEntry(argSpecs, {'type': 'function', 'name': 'X', 'generate': 'X', 'params': [['1', 'int', ''],['1', 'int', '']], 'return': ['JsVar','']}) # jswrap_banglejs + writeCallFunctionHackEntry(argSpecs, {'type': 'function', 'name': 'X', 'generate': 'X', 'params': [['1', 'float', ''],['1', 'float', '']], 'return': ['float','']}) # jswrap_banglejs + writeCallFunctionHackEntry(argSpecs, {'type': 'function', 'name': 'X', 'generate': 'X', 'params': [['1', 'int', ''],['1', 'int', '']], 'return': ['int','']}) # jswrap_arraybuffer for jsondata in jsondatas: - if "generate" in jsondata: - argSpec = getArgumentSpecifier(jsondata) - if not argSpec in argSpecs: - argSpecs.append(argSpec) - params = getParams(jsondata) - result = getResult(jsondata); - pTypes = [] - pValues = [] - if hasThis(jsondata): - pTypes.append("JsVar*") - pValues.append("thisParam") - cmd = ""; - cmdstart = ""; - cmdend = ""; - n = 0 - for param in params: - pTypes.append(toCType(param[1])); - if param[1]=="JsVarArray": - cmdstart = " JsVar *argArray = (paramCount>"+str(n)+")?jsvNewArray(¶mData["+str(n)+"],paramCount-"+str(n)+"):jsvNewEmptyArray();\n"; - pValues.append("argArray"); - cmdend = " jsvUnLock(argArray);\n\n"; - else: - pValues.append(toCUnbox(param[1])+"((paramCount>"+str(n)+")?paramData["+str(n)+"]:0)"); - n = n+1 - - codeOut(" case "+argSpec+": {"); - codeOut(" JsVar *result = 0;"); - if cmdstart: codeOut(cmdstart); - cmd = "(("+toCType(result[0])+"(*)("+",".join(pTypes)+"))function)("+",".join(pValues)+")"; - if result[0]: codeOut(" result = "+toCBox(result[0])+"("+cmd+");"); - else: codeOut(" "+cmd+";"); - if cmdend: codeOut(cmdend); - codeOut(" return result;"); - codeOut(" }"); + if "generate" in jsondata: + writeCallFunctionHackEntry(argSpecs, jsondata) #((uint32_t (*)(size_t,size_t,size_t,size_t))function)(argData[0],argData[1],argData[2],argData[3]); codeOut(' default: jsExceptionHere(JSET_ERROR,"Unknown argspec %d",argumentSpecifier);') codeOut(' }')