mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-28 14:27:36 +03:00
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:
parent
fc81d8f6d3
commit
0e1fee5ddd
@ -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
|
||||
|
@ -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(),
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ pub struct Export {
|
||||
|
||||
pub struct Import {
|
||||
pub module: Option<String>,
|
||||
pub namespace: Option<syn::Ident>,
|
||||
pub js_namespace: Option<syn::Ident>,
|
||||
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<syn::Ident> {
|
||||
fn js_namespace(&self) -> Option<syn::Ident> {
|
||||
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") >>
|
||||
|
@ -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() {
|
||||
|
@ -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"),
|
||||
}),
|
||||
|
@ -20,7 +20,7 @@ pub struct Program {
|
||||
#[derive(Deserialize)]
|
||||
pub struct Import {
|
||||
pub module: Option<String>,
|
||||
pub namespace: Option<String>,
|
||||
pub js_namespace: Option<String>,
|
||||
pub kind: ImportKind,
|
||||
}
|
||||
|
||||
|
@ -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!");
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)]
|
||||
|
Loading…
Reference in New Issue
Block a user