-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix webidl_binder so it can't handle missing assert function #21171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This can happen when ASSERTIONS is not set and we are in STRICT mode or MINIMAL_RUNTIME mode. See emscripten-core#20592
if (typeof assert == "undefined") { | ||
assert = (cond) => {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (typeof assert == "undefined") { | |
assert = (cond) => {} | |
} | |
if (typeof assert === "undefined") { | |
var assert = () => {}; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may not work, if the generated code here is in a different scope than where assert
is defined,
function assert() { .. }
function other() {
if (typeof assert === "undefined") {
// assert is always overridden here
var assert = () => {};
}
}
I think the WebIDL binder output here is usually added in a pre-js, so maybe that would be fine. However, it seems a little risky. Might be simplest to just define a new wassert
and use that in the WebIDL binder code, perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the above example would work just fine wouldn't it typeof assert === "undefined"
will return false if there is an assert in the output/global scope.
webidl code certainly must be the same scope as the emscripten module code and --post-js
is the canonical way to use it.
In the long term I hope to add --debug
flag to webidl_binder.py but for now I think this solution should work fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because JS does function hoisting the check will always work regardless of whether this code comes after of before the assert declartion.
For example the following code always prints "good" regardless of whether it is run at the global scope or in a function:
if (typeof assert == 'undefined') {
console.error('oops');
} else {
console.error('good');
}
function assert() {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the above example would work just fine wouldn't it typeof assert === "undefined" will return false if there is an assert in the output/global scope.
No, there is a problem with scoping. Here is an extension of my example that is runnable:
function assert() {
throw "true assert";
}
function other() {
if (typeof assert === "undefined") {
// assert is always overridden here
var assert = () => {};
}
assert(1 == 0);
}
other();
Running that does not throw true assert
, showing the assert was always overridden.
var
is scoped to the function, so entering other
means that the assert
in the outer scope does not matter. Just the presence of the var
means that assert
is equal to undefined
in that scope (as it is undefined until it actually reaches the var
).
This can happen when ASSERTIONS is not set and we are in STRICT mode or MINIMAL_RUNTIME mode.
See #20592