Skip to content
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

Add support for importing reserved JS words #1091

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ matrix:
- if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then aws s3 sync ~/$TRAVIS_BUILD_NUMBER s3://wasm-bindgen-ci/$TRAVIS_BUILD_NUMBER; fi
if: branch = master

# The `cli-support` crate's tests pass
- name: "test cli-support crate"
script: cargo test -p wasm-bindgen-cli-support
if: branch = master

# The `web-sys` crate's tests pass
- name: "test web-sys crate"
install:
Expand Down
1 change: 1 addition & 0 deletions crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Shared support for the wasm-bindgen-cli package, an internal dependency
[dependencies]
base64 = "0.9"
failure = "0.1.2"
lazy_static = "1.0.0"
parity-wasm = "0.35"
tempfile = "3.0"
wasm-bindgen-gc = { path = '../gc', version = '=0.2.29' }
Expand Down
96 changes: 92 additions & 4 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,60 @@ const INITIAL_HEAP_VALUES: &[&str] = &["undefined", "null", "true", "false"];
// Must be kept in sync with `src/lib.rs` of the `wasm-bindgen` crate
const INITIAL_HEAP_OFFSET: usize = 32;

lazy_static! {
static ref JS_RESERVED_WORDS: HashSet<&'static str> = {
let mut words = HashSet::with_capacity(47);
words.insert("arguments");
words.insert("await");
words.insert("break");
words.insert("case");
words.insert("catch");
words.insert("class");
words.insert("const");
words.insert("continue");
words.insert("debugger");
words.insert("default");
words.insert("delete");
words.insert("do");
words.insert("else");
words.insert("enum");
words.insert("eval");
words.insert("export");
words.insert("extends");
words.insert("false");
words.insert("finally");
words.insert("for");
words.insert("function");
words.insert("if");
words.insert("implements");
words.insert("import");
words.insert("in");
words.insert("instanceof");
words.insert("interface");
words.insert("let");
words.insert("new");
words.insert("null");
words.insert("package");
words.insert("private");
words.insert("protected");
words.insert("public");
words.insert("return");
words.insert("super");
words.insert("switch");
words.insert("this");
words.insert("throw");
words.insert("true");
words.insert("try");
words.insert("typeof");
words.insert("var");
words.insert("void");
words.insert("while");
words.insert("with");
words.insert("yield");
words
};
}

impl<'a> Context<'a> {
fn export(&mut self, name: &str, contents: &str, comments: Option<String>) {
let contents = contents.trim();
Expand Down Expand Up @@ -1770,7 +1824,12 @@ impl<'a> Context<'a> {
.or_insert_with(Default::default)
.entry(import.name())
.or_insert_with(|| {
let name = generate_identifier(import.name(), imported_identifiers);
let name = match &import {
Import::Module { .. } => {
generate_identifier(import.name(), imported_identifiers, true)
}
_ => generate_identifier(import.name(), imported_identifiers, false),
};
match &import {
Import::Module { module, .. } => {
if use_node_require {
Expand Down Expand Up @@ -1858,7 +1917,7 @@ impl<'a> Context<'a> {
let class = self.import_identifier(name);
let op = match &method_data.kind {
decode::MethodKind::Constructor => {
return Ok(ImportTarget::Constructor(class.to_string()))
return Ok(ImportTarget::Constructor(class.to_string()));
}
decode::MethodKind::Operation(op) => op,
};
Expand Down Expand Up @@ -2646,10 +2705,14 @@ impl<'a> Import<'a> {
}
}

fn generate_identifier(name: &str, used_names: &mut HashMap<String, usize>) -> String {
fn generate_identifier(
name: &str,
used_names: &mut HashMap<String, usize>,
mangle_reserved_names: bool,
) -> String {
let cnt = used_names.entry(name.to_string()).or_insert(0);
*cnt += 1;
if *cnt == 1 {
if *cnt == 1 && !(mangle_reserved_names && JS_RESERVED_WORDS.contains(name)) {
name.to_string()
} else {
format!("{}{}", name, cnt)
Expand All @@ -2668,3 +2731,28 @@ fn format_doc_comments(comments: &[&str], js_doc_comments: Option<String>) -> St
};
format!("/**\n{}{}*/\n", body, doc)
}

#[test]
fn test_generate_identifier() {
let mut used_names: HashMap<String, usize> = HashMap::new();
assert_eq!(
generate_identifier("someVar", &mut used_names, true),
"someVar".to_string()
);
assert_eq!(
generate_identifier("someVar", &mut used_names, true),
"someVar2".to_string()
);
assert_eq!(
generate_identifier("default", &mut used_names, true),
"default1".to_string()
);
assert_eq!(
generate_identifier("default", &mut used_names, true),
"default2".to_string()
);
assert_eq!(
generate_identifier("eval", &mut used_names, false),
"eval".to_string()
);
}
2 changes: 2 additions & 0 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-cli-support/0.2")]

#[macro_use]
extern crate lazy_static;
extern crate parity_wasm;
#[macro_use]
extern crate wasm_bindgen_shared as shared;
Expand Down
7 changes: 5 additions & 2 deletions tests/wasm/import_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ class Construct {
Construct.internal_string = '';
exports.Construct = Construct;

exports.NewConstructors = class {
class NewConstructor {
constructor(field) {
this.field = field;
}

get() {
return this.field + 1;
}
};
}

exports.NewConstructors = NewConstructor;
exports.default = NewConstructor;

let switch_called = false;
class SwitchMethods {
Expand Down
13 changes: 13 additions & 0 deletions tests/wasm/import_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ extern "C" {
#[wasm_bindgen(method)]
fn get(this: &NewConstructors) -> i32;

#[wasm_bindgen(js_name = default)]
type RenamedTypes;
#[wasm_bindgen(constructor, js_class = default)]
fn new(arg: i32) -> RenamedTypes;
#[wasm_bindgen(method, js_class = default)]
fn get(this: &RenamedTypes) -> i32;

fn switch_methods_a();
fn switch_methods_b();
type SwitchMethods;
Expand Down Expand Up @@ -125,6 +132,12 @@ fn new_constructors() {
assert_eq!(f.get(), 2);
}

#[wasm_bindgen_test]
fn rename_type() {
let f = RenamedTypes::new(1);
assert_eq!(f.get(), 2);
}

#[wasm_bindgen_test]
fn switch_methods() {
assert!(!switch_methods_called());
Expand Down