mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-21 08:41:35 +00: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:
@ -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"),
|
||||
}),
|
||||
|
Reference in New Issue
Block a user