Skip to content

Commit

Permalink
Emulator: ensure we include CallFunction stubs for Promises (fix regr…
Browse files Browse the repository at this point in the history
…ession after Promise refactor)
  • Loading branch information
gfwilliams committed Sep 5, 2024
1 parent 86d04c5 commit 170fb19
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
80 changes: 46 additions & 34 deletions scripts/build_jswrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,41 @@ def removeBlacklistForWrapper(blacklistfile,datas):
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(&paramData["+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')
Expand All @@ -776,41 +811,18 @@ def removeBlacklistForWrapper(blacklistfile,datas):
#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(&paramData["+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(' }')
Expand Down

0 comments on commit 170fb19

Please sign in to comment.