Add support for version specifications

This commit adds a `#[wasm_bindgen(version = "...")]` attribute support. This
information is eventually written into a `__wasm_pack_unstable` section.
Currently this is a strawman for the proposal in ashleygwilliams/wasm-pack#101
This commit is contained in:
Alex Crichton
2018-04-25 07:26:33 -07:00
parent d9a71b43db
commit 412bebca72
7 changed files with 183 additions and 0 deletions

View File

@ -20,6 +20,7 @@ pub struct Export {
pub struct Import {
pub module: Option<String>,
pub version: Option<String>,
pub js_namespace: Option<syn::Ident>,
pub kind: ImportKind,
}
@ -294,6 +295,7 @@ impl Program {
BindgenAttrs::find(attrs)
};
let module = item_opts.module().or(opts.module()).map(|s| s.to_string());
let version = item_opts.version().or(opts.version()).map(|s| s.to_string());
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),
@ -304,6 +306,7 @@ impl Program {
self.imports.push(Import {
module,
version,
js_namespace,
kind,
});
@ -584,8 +587,29 @@ impl Variant {
impl Import {
fn shared(&self) -> shared::Import {
match (&self.module, &self.version) {
(&Some(ref m), None) if m.starts_with("./") => {}
(&Some(ref m), &Some(_)) if m.starts_with("./") => {
panic!("when a module path starts with `./` that indicates \
that a local file is being imported so the `version` \
key cannot also be specified");
}
(&Some(_), &Some(_)) => {}
(&Some(_), &None) => {
panic!("when the `module` directive doesn't start with `./` \
then it's interpreted as an NPM package which requires \
a `version` to be specified as well, try using \
#[wasm_bindgen(module = \"...\", version = \"...\")]")
}
(&None, &Some(_)) => {
panic!("the #[wasm_bindgen(version = \"...\")] attribute can only \
be used when `module = \"...\"` is also specified");
}
(&None, &None) => {}
}
shared::Import {
module: self.module.clone(),
version: self.version.clone(),
js_namespace: self.js_namespace.map(|s| s.as_ref().to_string()),
kind: self.kind.shared(),
}
@ -746,6 +770,16 @@ impl BindgenAttrs {
.next()
}
fn version(&self) -> Option<&str> {
self.attrs
.iter()
.filter_map(|a| match *a {
BindgenAttr::Version(ref s) => Some(&s[..]),
_ => None,
})
.next()
}
pub fn catch(&self) -> bool {
self.attrs.iter().any(|a| match *a {
BindgenAttr::Catch => true,
@ -844,6 +878,7 @@ enum BindgenAttr {
Method,
JsNamespace(syn::Ident),
Module(String),
Version(String),
Getter(Option<syn::Ident>),
Setter(Option<syn::Ident>),
Structural,
@ -897,6 +932,13 @@ impl syn::synom::Synom for BindgenAttr {
(s.value())
)=> { BindgenAttr::Module }
|
do_parse!(
call!(term, "version") >>
punct!(=) >>
s: syn!(syn::LitStr) >>
(s.value())
)=> { BindgenAttr::Version }
|
do_parse!(
call!(term, "js_name") >>
punct!(=) >>