diff --git a/DESIGN.md b/DESIGN.md index 89c546076..3e8507e43 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -658,7 +658,7 @@ extern { #[wasm_bindgen(constructor)] fn new(arg: i32) -> Bar; - #[wasm_bindgen(namespace = Bar)] + #[wasm_bindgen(js_namespace = Bar)] fn another_function() -> i32; #[wasm_bindgen(method)] @@ -698,7 +698,7 @@ let's go through one-by-one: * `#[wasm_bindgen(constructor)]` - this indicates that the binding's name isn't actually used in JS but rather translates to `new Bar()`. The return value of this function must be a bare type, like `Bar`. -* `#[wasm_bindgen(namespace = Bar)]` - this attribute indicates that the +* `#[wasm_bindgen(js_namespace = Bar)]` - this attribute indicates that the function declaration is namespaced through the `Bar` class in JS. * `#[wasm_bindgen(method)]` - and finally, this attribute indicates that a method call is going to happen. The first argument must be a JS struct, like diff --git a/crates/wasm-bindgen-cli-support/src/js.rs b/crates/wasm-bindgen-cli-support/src/js.rs index 6e064344a..db9fedac1 100644 --- a/crates/wasm-bindgen-cli-support/src/js.rs +++ b/crates/wasm-bindgen-cli-support/src/js.rs @@ -1558,7 +1558,7 @@ impl<'a, 'b> SubContext<'a, 'b> { fn import_name(&mut self, import: &shared::Import, item: &str) -> String { if let Some(ref module) = import.module { - let name = import.namespace.as_ref().map(|s| &**s).unwrap_or(item); + let name = import.js_namespace.as_ref().map(|s| &**s).unwrap_or(item); if self.cx.imported_names.insert(name.to_string()) { self.cx.imports.push_str(&format!(" @@ -1566,7 +1566,7 @@ impl<'a, 'b> SubContext<'a, 'b> { ", name, module)); } } - match import.namespace { + match import.js_namespace { Some(ref s) => format!("{}.{}", s, item), None => item.to_string(), } diff --git a/crates/wasm-bindgen-macro/src/ast.rs b/crates/wasm-bindgen-macro/src/ast.rs index 7fa191a4b..fa61e5448 100644 --- a/crates/wasm-bindgen-macro/src/ast.rs +++ b/crates/wasm-bindgen-macro/src/ast.rs @@ -21,7 +21,7 @@ pub struct Export { pub struct Import { pub module: Option, - pub namespace: Option, + pub js_namespace: Option, pub kind: ImportKind, } @@ -269,7 +269,7 @@ impl Program { BindgenAttrs::find(attrs) }; let module = item_opts.module().or(opts.module()).map(|s| s.to_string()); - let namespace = item_opts.namespace().or(opts.namespace()); + let js_namespace = item_opts.js_namespace().or(opts.js_namespace()); let mut kind = match item { syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, item_opts), syn::ForeignItem::Type(t) => self.push_foreign_ty(t), @@ -277,7 +277,7 @@ impl Program { _ => panic!("only foreign functions/types allowed for now"), }; - self.imports.push(Import { module, namespace, kind }); + self.imports.push(Import { module, js_namespace, kind }); } } @@ -640,11 +640,11 @@ impl BindgenAttrs { }) } - fn namespace(&self) -> Option { + fn js_namespace(&self) -> Option { self.attrs.iter() .filter_map(|a| { match *a { - BindgenAttr::Namespace(s) => Some(s), + BindgenAttr::JsNamespace(s) => Some(s), _ => None, } }) @@ -691,7 +691,7 @@ enum BindgenAttr { Catch, Constructor, Method, - Namespace(syn::Ident), + JsNamespace(syn::Ident), Module(String), Getter, Setter, @@ -710,11 +710,11 @@ impl syn::synom::Synom for BindgenAttr { call!(term, "setter") => { |_| BindgenAttr::Setter } | do_parse!( - call!(term, "namespace") >> + call!(term, "js_namespace") >> punct!(=) >> ns: syn!(syn::Ident) >> (ns) - )=> { BindgenAttr::Namespace } + )=> { BindgenAttr::JsNamespace } | do_parse!( call!(term, "module") >> diff --git a/crates/wasm-bindgen-macro/src/lib.rs b/crates/wasm-bindgen-macro/src/lib.rs index 410552f33..0c691e992 100755 --- a/crates/wasm-bindgen-macro/src/lib.rs +++ b/crates/wasm-bindgen-macro/src/lib.rs @@ -13,7 +13,7 @@ extern crate wasm_bindgen_shared as shared; use std::borrow::Cow; use std::env; use std::sync::atomic::*; -use std::collections::{BTreeMap, HashSet}; +use std::collections::HashSet; use proc_macro::TokenStream; use proc_macro2::Span; @@ -71,39 +71,18 @@ impl ToTokens for ast::Program { s.to_tokens(tokens); } let mut types = HashSet::new(); - let mut buckets = BTreeMap::new(); for i in self.imports.iter() { - buckets.entry(i.namespace) - .or_insert(Vec::new()) - .push(i); if let ast::ImportKind::Type(ref t) = i.kind { types.insert(t.name); } } - for (namespace, imports) in buckets { - let mut sub_tokens = Tokens::new(); - for import in imports { - import.kind.to_tokens(&mut sub_tokens); - } - match namespace { + for i in self.imports.iter() { + match i.js_namespace { Some(ns) if types.contains(&ns) => { - (quote! { impl #ns { #sub_tokens } }).to_tokens(tokens); + let kind = &i.kind; + (quote! { impl #ns { #kind } }).to_tokens(tokens); } - Some(ns) => { - (quote! { - // TODO: allow controlling `pub` here. - // - // TODO: we don't really want to generate a type here, - // it'd be preferrable to generate a namespace indicator - // or something like that (but modules interact weirdly - // with imports and such) - #[allow(bad_style)] - pub struct #ns { _priv: () } - - impl #ns { #sub_tokens } - }).to_tokens(tokens); - } - None => sub_tokens.to_tokens(tokens), + _ => i.kind.to_tokens(tokens), } } for e in self.enums.iter() { diff --git a/crates/wasm-bindgen-macro/src/literal.rs b/crates/wasm-bindgen-macro/src/literal.rs index 6d64bf22c..9c0f14a22 100644 --- a/crates/wasm-bindgen-macro/src/literal.rs +++ b/crates/wasm-bindgen-macro/src/literal.rs @@ -204,7 +204,7 @@ impl Literal for ast::Import { Some(ref s) => a.str(s), None => a.append("null"), }), - ("namespace", &|a| match self.namespace { + ("js_namespace", &|a| match self.js_namespace { Some(ref s) => a.str(s.as_ref()), None => a.append("null"), }), diff --git a/crates/wasm-bindgen-shared/src/lib.rs b/crates/wasm-bindgen-shared/src/lib.rs index eef59005c..68c114d35 100644 --- a/crates/wasm-bindgen-shared/src/lib.rs +++ b/crates/wasm-bindgen-shared/src/lib.rs @@ -20,7 +20,7 @@ pub struct Program { #[derive(Deserialize)] pub struct Import { pub module: Option, - pub namespace: Option, + pub js_namespace: Option, pub kind: ImportKind, } diff --git a/examples/console_log/src/lib.rs b/examples/console_log/src/lib.rs index f985facf4..76ebd4c37 100644 --- a/examples/console_log/src/lib.rs +++ b/examples/console_log/src/lib.rs @@ -6,11 +6,11 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] extern { - #[wasm_bindgen(namespace = console)] + #[wasm_bindgen(js_namespace = console)] fn log(s: &str); } #[wasm_bindgen] pub fn run() { - console::log("Hello from Rust!"); + log("Hello from Rust!"); } diff --git a/examples/math/src/lib.rs b/examples/math/src/lib.rs index 8546bdb41..c10b9ae5a 100644 --- a/examples/math/src/lib.rs +++ b/examples/math/src/lib.rs @@ -6,21 +6,21 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] extern { - #[wasm_bindgen(namespace = Math)] + #[wasm_bindgen(js_namespace = Math)] fn log2(a: f64) -> f64; - #[wasm_bindgen(namespace = Math)] + #[wasm_bindgen(js_namespace = Math)] fn sin(a: f64) -> f64; - #[wasm_bindgen(namespace = console)] + #[wasm_bindgen(js_namespace = console)] fn log(a: &str); } macro_rules! println { - ($($t:tt)*) => (console::log(&format_args!($($t)*).to_string())) + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) } #[wasm_bindgen] pub fn run() { - println!("Math.log2(10.0) = {}", Math::log2(10.0)); - println!("Math.sin(1.2) = {}", Math::sin(1.2)); + println!("Math.log2(10.0) = {}", log2(10.0)); + println!("Math.sin(1.2) = {}", sin(1.2)); } diff --git a/examples/performance/src/lib.rs b/examples/performance/src/lib.rs index 8502e26cd..77b01c8c9 100644 --- a/examples/performance/src/lib.rs +++ b/examples/performance/src/lib.rs @@ -24,12 +24,12 @@ extern { #[wasm_bindgen(method, getter)] fn responseEnd(this: &PerformanceTiming) -> f64; - #[wasm_bindgen(namespace = console)] + #[wasm_bindgen(js_namespace = console)] fn log(a: &str); } macro_rules! println { - ($($t:tt)*) => (console::log(&format_args!($($t)*).to_string())) + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) } // Called by our JS entry point to run the example diff --git a/tests/import-class.rs b/tests/import-class.rs index 1543f2957..d76b172bb 100644 --- a/tests/import-class.rs +++ b/tests/import-class.rs @@ -12,19 +12,19 @@ fn simple() { #[wasm_bindgen] pub fn get_random() -> f64 { - Math::random() + random() } #[wasm_bindgen] pub fn do_log(a: f64) -> f64 { - Math::log(a) + log(a) } #[wasm_bindgen] extern { - #[wasm_bindgen(namespace = Math)] + #[wasm_bindgen(js_namespace = Math)] fn random() -> f64; - #[wasm_bindgen(namespace = Math)] + #[wasm_bindgen(js_namespace = Math)] fn log(a: f64) -> f64; } "#) @@ -52,22 +52,22 @@ fn import_class() { #[wasm_bindgen(module = "./another")] extern { - #[wasm_bindgen(namespace = Foo)] + #[wasm_bindgen(js_namespace = Foo)] fn bar(); } #[wasm_bindgen] - pub fn bar() { - Foo::bar(); + pub fn baz() { + bar(); } "#) .file("test.ts", r#" - import { bar } from "./out"; + import { baz } from "./out"; import { called } from "./another"; import * as assert from "assert"; export function test() { - bar(); + baz(); assert.strictEqual(called, true); } "#) @@ -96,7 +96,7 @@ fn construct() { #[wasm_bindgen(module = "./another")] extern { type Foo; - #[wasm_bindgen(namespace = Foo)] + #[wasm_bindgen(js_namespace = Foo)] fn create() -> Foo; #[wasm_bindgen(method)] fn get_internal_string(this: &Foo) -> String; @@ -217,7 +217,7 @@ fn switch_methods() { #[wasm_bindgen(constructor)] fn new() -> Foo; - #[wasm_bindgen(namespace = Foo)] + #[wasm_bindgen(js_namespace = Foo)] fn a(); #[wasm_bindgen(method)]