Skip to content

Commit cbf96bc

Browse files
authored
Emterpreter-async-assertions float fix (emscripten-core#6431)
* support float coercions in emterpreter-async assertions code. fixes emscripten-core#6425 * fix EmterpreterAsync check (allow the option to be used without actually using an async function)
1 parent 9a5a9b6 commit cbf96bc

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/postamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ Module['callMain'] = function callMain(args) {
210210
#if EMTERPRETIFY_ASYNC
211211
// if we are saving the stack, then do not call exit, we are not
212212
// really exiting now, just unwinding the JS stack
213-
if (EmterpreterAsync.state !== 1) {
213+
if (typeof EmterpreterAsync === 'object' && EmterpreterAsync.state !== 1) {
214214
#endif // EMTERPRETIFY_ASYNC
215215
// if we're not running an evented main loop, it's time to exit
216216
exit(ret, /* implicit = */ true);

tests/test_other.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5352,6 +5352,36 @@ def test_emterpreter_advise(self):
53525352
out = run_process([PYTHON, EMCC, path_from_root('tests', 'emterpreter_advise_synclist.c'), '-s', 'EMTERPRETIFY=1', '-s', 'EMTERPRETIFY_ASYNC=1', '-s', 'EMTERPRETIFY_ADVISE=1', '-s', 'EMTERPRETIFY_SYNCLIST=["_j","_k"]'], stdout=PIPE).stdout
53535353
self.assertContained('-s EMTERPRETIFY_WHITELIST=\'["_a", "_b", "_e", "_f", "_main"]\'', out)
53545354

5355+
def test_emterpreter_async_assertions(self):
5356+
# emterpretify-async mode with assertions adds checks on each call out of the emterpreter;
5357+
# make sure we handle all possible types there
5358+
for t, out in [
5359+
('int', '18.00'),
5360+
('float', '18.51'),
5361+
('double', '18.51'),
5362+
]:
5363+
print(t, out)
5364+
open('src.c', 'w').write(r'''
5365+
#include <stdio.h>
5366+
#include <emscripten.h>
5367+
5368+
#define TYPE %s
5369+
5370+
TYPE marfoosh(TYPE input) {
5371+
return input * 1.5;
5372+
}
5373+
5374+
TYPE fleefl(TYPE input) {
5375+
return marfoosh(input);
5376+
}
5377+
5378+
int main(void) {
5379+
printf("result: %%.2f\n", (double)fleefl((TYPE)12.34));
5380+
}
5381+
''' % t)
5382+
run_process([PYTHON, EMCC, 'src.c', '-s', 'EMTERPRETIFY=1', '-s', 'EMTERPRETIFY_ASYNC=1', '-s', 'EMTERPRETIFY_WHITELIST=["_fleefl"]', '-s', 'PRECISE_F32=1'])
5383+
self.assertContained('result: ' + out, run_js('a.out.js'))
5384+
53555385
def test_link_with_a_static(self):
53565386
for args in [[], ['-O2']]:
53575387
print(args)

tools/js-optimizer.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,7 @@ function detectType(node, asmInfo, inVarDef) {
19311931
if (!parseHeap(node[1][1])) return ASM_NONE;
19321932
return parseHeapTemp.float ? ASM_DOUBLE : ASM_INT; // XXX ASM_FLOAT?
19331933
}
1934+
case 'stat': return ASM_NONE;
19341935
}
19351936
assert(0 , 'horrible ' + JSON.stringify(node));
19361937
}
@@ -7380,18 +7381,15 @@ function emterpretify(ast) {
73807381
var callType = ASM_NONE;
73817382
var parent = stack[stack.length-1];
73827383
if (parent) {
7384+
callType = detectType(parent, asmData);
73837385
var temp = null;
7384-
if (parent[0] === 'binary' && parent[1] === '|' && parent[3][0] === 'num' && parent[3][1] === 0 &&
7385-
parent[2] === node) {
7386-
// int-coerced call
7387-
callType = ASM_INT;
7388-
temp = 'tempInt';
7389-
} else if (parent[0] === 'unary-prefix' && parent[1] === '+' && parent[2] === node) {
7390-
// double-coerced call
7391-
callType = ASM_DOUBLE;
7392-
temp = 'tempDouble';
7386+
switch (callType) {
7387+
case ASM_INT: temp = 'tempInt'; break;
7388+
case ASM_DOUBLE: temp = 'tempDouble'; break;
7389+
case ASM_FLOAT: temp = 'tempFloat'; break;
7390+
case ASM_NONE: break;
7391+
default: throw 'unhandled parent type in emterpter-async-assertions: ' + callType;
73937392
}
7394-
// XXX fails on other coercions of odd types, like float32, simd, etc!
73957393
if (temp) {
73967394
// assign to temp, assert, return proper value: temp = call() , (asyncState ? abort() : temp)
73977395
trample(node, ['seq',

0 commit comments

Comments
 (0)