-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Generated JS code can end up with (reserved) keywords as identifiers #2806
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
Comments
I am not sure whether generating idents with prefixes is the "right" approach. It breaks the Rust -> JS interface mapping contract. I'd suggest issue compiler warnings once user uses idents that are keywords in js. |
I guess I will tack on to this issue, but a slightly more insidious version of the same problem is that names in your rust code can also shadow the same names in the generated JS code. I just ran into this with a function with the signature: export function process_frame(wasm, time) {
_assertClass(wasm, WasmEngine);
if (wasm.ptr === 0) {
throw new Error('Attempt to use a moved value');
}
var ptr0 = wasm.ptr;
wasm.ptr = 0;
uint64CvtShim[0] = time;
const low1 = u32CvtShim[0];
const high1 = u32CvtShim[1];
wasm.process_frame(ptr0, low1, high1);
} which in quite hilarious fashion breaks, because the parameter called 'wasm' shadows the fairly important import: Ideally I guess all generated JS code should prefix their names with something, but some warnings for names like 'wasm' would be a good idea. 😂 |
It seems wasm-bindgen does not check that identifiers that are valid in Rust code might not be valid in JavaScript.
Building a project containing a function like this:
... generates this piece of JS code:
which is invalid (
class
is a keyword and can't be used for identifiers in JS)Steps to Reproduce
wasm-pack new bug
wasm-pack build
pkg/bug_bg.js
(filename depends on the project name)Expected Behavior
Prefix identifier with
_
so it can't clash with keywords (or mangle identifiers in some other way)Actual Behavior
The identifier is not changed and clashes with the JavaScript "class" keyword
The text was updated successfully, but these errors were encountered: