Skip to content

Commit

Permalink
call processMicrotasks before exiting context to ensure pending pro…
Browse files Browse the repository at this point in the history
…mises are settled (see [1])

afais these two places are the only ones where functions are called directly, but there might be more

[1] https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/Context.java#L2326
  • Loading branch information
grob committed Jan 4, 2022
1 parent 9bb6a2e commit da2b2a9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/org/ringojs/engine/RingoGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ public static Object spawn(Context cx, Scriptable thisObj,
public Object call() {
return cxfactory.call(new ContextAction<Object>() {
public Object run(Context cx) {
return function.call(cx, scope, scope, fnArgs);
Object result = function.call(cx, scope, scope, fnArgs);
cx.processMicrotasks();
return result;
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/org/ringojs/engine/RingoWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public Object invoke(Object module, Object function, Object... args)
}
} finally {
releaseWorker(previous);
cx.processMicrotasks();
Context.exit();
}
}
Expand Down
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports.testBinary = require('./binary/all');
exports.testRepository = require('./repository/all');
exports.testIo = require('./io_test');
exports.testModules = require('./modules/all');
exports.testRhino = require("./rhino/all");

// Also include integration tests
exports.testIntegration = require('./integration-tests/all');
Expand Down
4 changes: 4 additions & 0 deletions test/rhino/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.testArrowFunctions = require("./arrow_functions");
exports.testConstStatement = require("./const_statement");
exports.testLetStatement = require("./let_statement");
exports.testPromise = require("./promise_test");
22 changes: 22 additions & 0 deletions test/rhino/promise_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const assert = require("assert");
const {Semaphore} = require("ringo/concurrent");

exports.testResolve = () => {
const semaphore = new Semaphore();
let expected = false;
spawn(() => {
return new Promise(resolve => {
java.lang.Thread.sleep(100);
resolve(true);
}).then(value => {
expected = value;
semaphore.signal();
});
});
semaphore.tryWait(150);
assert.isTrue(expected);
};

if (require.main === module) {
require("system").exit(require("test").run(module.id));
}

0 comments on commit da2b2a9

Please sign in to comment.