Rename namespace to js_namespace

Along the way remove the namespace in Rust as this ended up causing too many
problems, alas! The `js_namespace` attribute now almost exclusively modifies the
JS bindings, hence the "js" in the name now.
This commit is contained in:
Alex Crichton 2018-03-22 16:59:48 -07:00
parent fc81d8f6d3
commit 0e1fee5ddd
10 changed files with 41 additions and 62 deletions

View File

@ -658,7 +658,7 @@ extern {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
fn new(arg: i32) -> Bar; fn new(arg: i32) -> Bar;
#[wasm_bindgen(namespace = Bar)] #[wasm_bindgen(js_namespace = Bar)]
fn another_function() -> i32; fn another_function() -> i32;
#[wasm_bindgen(method)] #[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 * `#[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 actually used in JS but rather translates to `new Bar()`. The return value of
this function must be a bare type, like `Bar`. 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. function declaration is namespaced through the `Bar` class in JS.
* `#[wasm_bindgen(method)]` - and finally, this attribute indicates that a * `#[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 method call is going to happen. The first argument must be a JS struct, like

View File

@ -1558,7 +1558,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
fn import_name(&mut self, import: &shared::Import, item: &str) -> String { fn import_name(&mut self, import: &shared::Import, item: &str) -> String {
if let Some(ref module) = import.module { 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()) { if self.cx.imported_names.insert(name.to_string()) {
self.cx.imports.push_str(&format!(" self.cx.imports.push_str(&format!("
@ -1566,7 +1566,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
", name, module)); ", name, module));
} }
} }
match import.namespace { match import.js_namespace {
Some(ref s) => format!("{}.{}", s, item), Some(ref s) => format!("{}.{}", s, item),
None => item.to_string(), None => item.to_string(),
} }

View File

@ -21,7 +21,7 @@ pub struct Export {
pub struct Import { pub struct Import {
pub module: Option<String>, pub module: Option<String>,
pub namespace: Option<syn::Ident>, pub js_namespace: Option<syn::Ident>,
pub kind: ImportKind, pub kind: ImportKind,
} }
@ -269,7 +269,7 @@ impl Program {
BindgenAttrs::find(attrs) BindgenAttrs::find(attrs)
}; };
let module = item_opts.module().or(opts.module()).map(|s| s.to_string()); 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 { let mut kind = match item {
syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, item_opts), syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, item_opts),
syn::ForeignItem::Type(t) => self.push_foreign_ty(t), syn::ForeignItem::Type(t) => self.push_foreign_ty(t),
@ -277,7 +277,7 @@ impl Program {
_ => panic!("only foreign functions/types allowed for now"), _ => 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<syn::Ident> { fn js_namespace(&self) -> Option<syn::Ident> {
self.attrs.iter() self.attrs.iter()
.filter_map(|a| { .filter_map(|a| {
match *a { match *a {
BindgenAttr::Namespace(s) => Some(s), BindgenAttr::JsNamespace(s) => Some(s),
_ => None, _ => None,
} }
}) })
@ -691,7 +691,7 @@ enum BindgenAttr {
Catch, Catch,
Constructor, Constructor,
Method, Method,
Namespace(syn::Ident), JsNamespace(syn::Ident),
Module(String), Module(String),
Getter, Getter,
Setter, Setter,
@ -710,11 +710,11 @@ impl syn::synom::Synom for BindgenAttr {
call!(term, "setter") => { |_| BindgenAttr::Setter } call!(term, "setter") => { |_| BindgenAttr::Setter }
| |
do_parse!( do_parse!(
call!(term, "namespace") >> call!(term, "js_namespace") >>
punct!(=) >> punct!(=) >>
ns: syn!(syn::Ident) >> ns: syn!(syn::Ident) >>
(ns) (ns)
)=> { BindgenAttr::Namespace } )=> { BindgenAttr::JsNamespace }
| |
do_parse!( do_parse!(
call!(term, "module") >> call!(term, "module") >>

View File

@ -13,7 +13,7 @@ extern crate wasm_bindgen_shared as shared;
use std::borrow::Cow; use std::borrow::Cow;
use std::env; use std::env;
use std::sync::atomic::*; use std::sync::atomic::*;
use std::collections::{BTreeMap, HashSet}; use std::collections::HashSet;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use proc_macro2::Span; use proc_macro2::Span;
@ -71,39 +71,18 @@ impl ToTokens for ast::Program {
s.to_tokens(tokens); s.to_tokens(tokens);
} }
let mut types = HashSet::new(); let mut types = HashSet::new();
let mut buckets = BTreeMap::new();
for i in self.imports.iter() { 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 { if let ast::ImportKind::Type(ref t) = i.kind {
types.insert(t.name); types.insert(t.name);
} }
} }
for (namespace, imports) in buckets { for i in self.imports.iter() {
let mut sub_tokens = Tokens::new(); match i.js_namespace {
for import in imports {
import.kind.to_tokens(&mut sub_tokens);
}
match namespace {
Some(ns) if types.contains(&ns) => { 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) => { _ => i.kind.to_tokens(tokens),
(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),
} }
} }
for e in self.enums.iter() { for e in self.enums.iter() {

View File

@ -204,7 +204,7 @@ impl Literal for ast::Import {
Some(ref s) => a.str(s), Some(ref s) => a.str(s),
None => a.append("null"), None => a.append("null"),
}), }),
("namespace", &|a| match self.namespace { ("js_namespace", &|a| match self.js_namespace {
Some(ref s) => a.str(s.as_ref()), Some(ref s) => a.str(s.as_ref()),
None => a.append("null"), None => a.append("null"),
}), }),

View File

@ -20,7 +20,7 @@ pub struct Program {
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Import { pub struct Import {
pub module: Option<String>, pub module: Option<String>,
pub namespace: Option<String>, pub js_namespace: Option<String>,
pub kind: ImportKind, pub kind: ImportKind,
} }

View File

@ -6,11 +6,11 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
extern { extern {
#[wasm_bindgen(namespace = console)] #[wasm_bindgen(js_namespace = console)]
fn log(s: &str); fn log(s: &str);
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn run() { pub fn run() {
console::log("Hello from Rust!"); log("Hello from Rust!");
} }

View File

@ -6,21 +6,21 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
extern { extern {
#[wasm_bindgen(namespace = Math)] #[wasm_bindgen(js_namespace = Math)]
fn log2(a: f64) -> f64; fn log2(a: f64) -> f64;
#[wasm_bindgen(namespace = Math)] #[wasm_bindgen(js_namespace = Math)]
fn sin(a: f64) -> f64; fn sin(a: f64) -> f64;
#[wasm_bindgen(namespace = console)] #[wasm_bindgen(js_namespace = console)]
fn log(a: &str); fn log(a: &str);
} }
macro_rules! println { macro_rules! println {
($($t:tt)*) => (console::log(&format_args!($($t)*).to_string())) ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn run() { pub fn run() {
println!("Math.log2(10.0) = {}", Math::log2(10.0)); println!("Math.log2(10.0) = {}", log2(10.0));
println!("Math.sin(1.2) = {}", Math::sin(1.2)); println!("Math.sin(1.2) = {}", sin(1.2));
} }

View File

@ -24,12 +24,12 @@ extern {
#[wasm_bindgen(method, getter)] #[wasm_bindgen(method, getter)]
fn responseEnd(this: &PerformanceTiming) -> f64; fn responseEnd(this: &PerformanceTiming) -> f64;
#[wasm_bindgen(namespace = console)] #[wasm_bindgen(js_namespace = console)]
fn log(a: &str); fn log(a: &str);
} }
macro_rules! println { 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 // Called by our JS entry point to run the example

View File

@ -12,19 +12,19 @@ fn simple() {
#[wasm_bindgen] #[wasm_bindgen]
pub fn get_random() -> f64 { pub fn get_random() -> f64 {
Math::random() random()
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn do_log(a: f64) -> f64 { pub fn do_log(a: f64) -> f64 {
Math::log(a) log(a)
} }
#[wasm_bindgen] #[wasm_bindgen]
extern { extern {
#[wasm_bindgen(namespace = Math)] #[wasm_bindgen(js_namespace = Math)]
fn random() -> f64; fn random() -> f64;
#[wasm_bindgen(namespace = Math)] #[wasm_bindgen(js_namespace = Math)]
fn log(a: f64) -> f64; fn log(a: f64) -> f64;
} }
"#) "#)
@ -52,22 +52,22 @@ fn import_class() {
#[wasm_bindgen(module = "./another")] #[wasm_bindgen(module = "./another")]
extern { extern {
#[wasm_bindgen(namespace = Foo)] #[wasm_bindgen(js_namespace = Foo)]
fn bar(); fn bar();
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn bar() { pub fn baz() {
Foo::bar(); bar();
} }
"#) "#)
.file("test.ts", r#" .file("test.ts", r#"
import { bar } from "./out"; import { baz } from "./out";
import { called } from "./another"; import { called } from "./another";
import * as assert from "assert"; import * as assert from "assert";
export function test() { export function test() {
bar(); baz();
assert.strictEqual(called, true); assert.strictEqual(called, true);
} }
"#) "#)
@ -96,7 +96,7 @@ fn construct() {
#[wasm_bindgen(module = "./another")] #[wasm_bindgen(module = "./another")]
extern { extern {
type Foo; type Foo;
#[wasm_bindgen(namespace = Foo)] #[wasm_bindgen(js_namespace = Foo)]
fn create() -> Foo; fn create() -> Foo;
#[wasm_bindgen(method)] #[wasm_bindgen(method)]
fn get_internal_string(this: &Foo) -> String; fn get_internal_string(this: &Foo) -> String;
@ -217,7 +217,7 @@ fn switch_methods() {
#[wasm_bindgen(constructor)] #[wasm_bindgen(constructor)]
fn new() -> Foo; fn new() -> Foo;
#[wasm_bindgen(namespace = Foo)] #[wasm_bindgen(js_namespace = Foo)]
fn a(); fn a();
#[wasm_bindgen(method)] #[wasm_bindgen(method)]