-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
#2806 bugfix: Added checks for JS keywords in signatures. #2855
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
Merged
alexcrichton
merged 2 commits into
rustwasm:main
from
petrstudynka:js-keywords-prefix-fix
Jun 17, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const wasm = require("wasm-bindgen-test.js"); | ||
const assert = require("assert"); | ||
|
||
exports.js_keywords_compile = () => { | ||
assert.strictEqual(wasm._throw(1), 1); | ||
assert.strictEqual(wasm._class(1, 2), false); | ||
assert.strictEqual(wasm.classy(3), 3); | ||
let obj = new wasm.Class("class"); | ||
assert.strictEqual(wasm.Class.void("string"), "string"); | ||
assert.strictEqual(obj.catch, "class"); | ||
assert.strictEqual(obj.instanceof("Class"), "class is instance of Class"); | ||
}; | ||
|
||
exports.test_keyword_1_as_fn_name = (x) => { | ||
return wasm._throw(x); | ||
}; | ||
|
||
exports.test_keyword_2_as_fn_name = (x, y) => { | ||
return wasm._class(x, y); | ||
}; | ||
|
||
exports.test_keyword_as_fn_arg = (x) => { | ||
return wasm.classy(x); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_test::*; | ||
|
||
#[wasm_bindgen(module = "tests/wasm/js_keywords.js")] | ||
extern "C" { | ||
fn js_keywords_compile(); | ||
fn test_keyword_1_as_fn_name(x: u8) -> u8; | ||
fn test_keyword_2_as_fn_name(x: u8, y: u8) -> bool; | ||
fn test_keyword_as_fn_arg(x: u8) -> u8; | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn throw(class: u8) -> u8 { | ||
class | ||
} | ||
|
||
#[wasm_bindgen(js_name = class)] | ||
pub fn fn_parsed_to_keyword(instanceof: u8, catch: u8) -> bool { | ||
instanceof > catch | ||
} | ||
|
||
#[wasm_bindgen(js_name = classy)] | ||
pub fn arg_is_keyword(class: u8) -> u8 { | ||
class | ||
} | ||
|
||
#[wasm_bindgen] | ||
struct Class { | ||
name: String, | ||
} | ||
#[wasm_bindgen] | ||
impl Class { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(void: String) -> Self { | ||
Class { name: void } | ||
} | ||
pub fn instanceof(&self, class: String) -> String { | ||
format!("{} is instance of {}", self.name.clone(), class) | ||
} | ||
#[wasm_bindgen(getter)] | ||
pub fn catch(&self) -> String { | ||
self.name.clone() | ||
} | ||
pub fn void(void: String) -> String { | ||
void | ||
} | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn compile() { | ||
js_keywords_compile(); | ||
assert_eq!(test_keyword_1_as_fn_name(1), 1); | ||
assert_eq!(test_keyword_2_as_fn_name(1, 2), false); | ||
assert_eq!(test_keyword_as_fn_arg(1), 1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should not silently replace the name of exported and imported identifiers where they make a semantic difference. Accessing them by bracket instead of dot syntax sounds like a reasonable workaround if the user actually does use a js keyword. I.e.
wasm["class"]
instead of silently changing towasm._class
against explicit annotation inhttps://github.com/rustwasm/wasm-bindgen/pull/2855/files#diff-f7d340b325e0e9f30bd18d30036698d1edfece6974682dd3480457e4383806d3R17
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.
That sounds like a reasonable solution that should be easy to implement, would you like to make a PR? Happy to review it.