Skip to content

Commit df004b2

Browse files
authored
Don't inject assertions onto the ready promise (#23176)
This is a debug feature that means users who did not await the promise and tried to access stuff directly on it would see a nice error message. However with the advent of more usage of async/await, for example in \#23157 it is not always the ready promise that gets returned to the user but a language-generated promise from an `await` call. We could try to jump through more hoops to continue to support this debug feature, but I don't think added complexity in the toolchain or the generated code would be worth it. Split out from #23157.
1 parent d766ef1 commit df004b2

File tree

4 files changed

+0
-60
lines changed

4 files changed

+0
-60
lines changed

src/parseTools.mjs

-29
Original file line numberDiff line numberDiff line change
@@ -971,34 +971,6 @@ function to64(x) {
971971
return `BigInt(${x})`;
972972
}
973973

974-
// Add assertions to catch common errors when using the Promise object we
975-
// return from MODULARIZE Module() invocations.
976-
function addReadyPromiseAssertions() {
977-
// Warn on someone doing
978-
//
979-
// var instance = Module();
980-
// ...
981-
// instance._main();
982-
const properties = Array.from(EXPORTED_FUNCTIONS.values());
983-
// Also warn on onRuntimeInitialized which might be a common pattern with
984-
// older MODULARIZE-using codebases.
985-
properties.push('onRuntimeInitialized');
986-
const warningEnding =
987-
' on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js';
988-
const res = JSON.stringify(properties);
989-
return (
990-
res +
991-
`.forEach((prop) => {
992-
if (!Object.getOwnPropertyDescriptor(readyPromise, prop)) {
993-
Object.defineProperty(readyPromise, prop, {
994-
get: () => abort('You are getting ' + prop + '${warningEnding}'),
995-
set: () => abort('You are setting ' + prop + '${warningEnding}'),
996-
});
997-
}
998-
});`
999-
);
1000-
}
1001-
1002974
function asyncIf(condition) {
1003975
return condition ? 'async' : '';
1004976
}
@@ -1118,7 +1090,6 @@ addToCompileTimeContext({
11181090
ENVIRONMENT_IS_WORKER_THREAD,
11191091
addAtExit,
11201092
addAtInit,
1121-
addReadyPromiseAssertions,
11221093
asyncIf,
11231094
awaitIf,
11241095
buildStringArray,

src/shell.js

-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ var readyPromise = new Promise((resolve, reject) => {
4747
readyPromiseResolve = resolve;
4848
readyPromiseReject = reject;
4949
});
50-
#if ASSERTIONS
51-
{{{ addReadyPromiseAssertions() }}}
52-
#endif
5350
#endif
5451

5552
// Determine the runtime environment we are in. You can customize this by

src/shell_minimal.js

-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var readyPromise = new Promise((resolve, reject) => {
4141
readyPromiseResolve = resolve;
4242
readyPromiseReject = reject;
4343
});
44-
#if ASSERTIONS
45-
{{{ addReadyPromiseAssertions() }}}
46-
#endif
4744
#endif
4845

4946
#if ENVIRONMENT_MAY_BE_NODE

test/test_other.py

-25
Original file line numberDiff line numberDiff line change
@@ -11962,31 +11962,6 @@ def test_assertions_on_outgoing_module_api_changes(self):
1196211962
'''
1196311963
self.do_runf('src.cpp', expected, emcc_args=['-sASSERTIONS'])
1196411964

11965-
def test_modularize_assertions_on_ready_promise(self):
11966-
# check that when assertions are on we give useful error messages for
11967-
# mistakenly thinking the Promise is an instance. I.e., once you could do
11968-
# Module()._main to get an instance and the main function, but after
11969-
# the breaking change in #10697 Module() now returns a promise, and to get
11970-
# the instance you must use .then() to get a callback with the instance.
11971-
create_file('test.js', r'''
11972-
try {
11973-
Module()._main;
11974-
} catch(e) {
11975-
console.log(e);
11976-
}
11977-
try {
11978-
Module().onRuntimeInitialized = 42;
11979-
} catch(e) {
11980-
console.log(e);
11981-
}
11982-
''')
11983-
self.run_process([EMCC, test_file('hello_world.c'), '-sMODULARIZE', '-sASSERTIONS', '--extern-post-js', 'test.js'])
11984-
# A return code of 1 is from an uncaught exception not handled by
11985-
# the domain or the 'uncaughtException' event handler.
11986-
out = self.run_js('a.out.js', assert_returncode=1)
11987-
self.assertContained('You are getting _main on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js', out)
11988-
self.assertContained('You are setting onRuntimeInitialized on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js', out)
11989-
1199011965
def test_modularize_assertions_on_reject_promise(self):
1199111966
# Check that there is an uncaught exception in modularize mode.
1199211967
# Once we added an `uncaughtException` handler to the global process

0 commit comments

Comments
 (0)