Skip to content

Commit

Permalink
Remove support for the version attribute
Browse files Browse the repository at this point in the history
First added in rustwasm#161 this never ended up panning out, so let's remove the
experimental suport which isn't actually used by anything today and hold off on
any other changes until an RFC happens.
  • Loading branch information
alexcrichton committed Aug 6, 2018
1 parent 7f8d510 commit 85fe257
Show file tree
Hide file tree
Showing 39 changed files with 34 additions and 237 deletions.
26 changes: 0 additions & 26 deletions crates/backend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub enum MethodSelf {
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub struct Import {
pub module: Option<String>,
pub version: Option<String>,
pub js_namespace: Option<Ident>,
pub kind: ImportKind,
}
Expand Down Expand Up @@ -301,33 +300,8 @@ impl Variant {

impl Import {
fn shared(&self) -> Result<shared::Import, Diagnostic> {
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) => {}
}
Ok(shared::Import {
module: self.module.clone(),
version: self.version.clone(),
js_namespace: self.js_namespace.as_ref().map(|s| s.to_string()),
kind: self.kind.shared(),
})
Expand Down
1 change: 0 additions & 1 deletion crates/backend/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub fn ident_ty(ident: Ident) -> syn::Type {
pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import {
ast::Import {
module: None,
version: None,
js_namespace: None,
kind: ast::ImportKind::Function(function),
}
Expand Down
1 change: 0 additions & 1 deletion crates/cli-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ base64 = "0.9"
failure = "0.1.2"
parity-wasm = "0.31"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tempfile = "3.0"
wasm-bindgen-shared = { path = "../shared", version = '=0.2.15' }
Expand Down
61 changes: 0 additions & 61 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::mem;
use failure::{Error, ResultExt};
use parity_wasm;
use parity_wasm::elements::*;
use serde_json;
use shared;
use wasm_gc;

Expand Down Expand Up @@ -43,7 +42,6 @@ pub struct Context<'a> {
pub exported_classes: HashMap<String, ExportedClass>,
pub function_table_needed: bool,
pub run_descriptor: &'a Fn(&str) -> Option<Vec<u32>>,
pub module_versions: Vec<(String, String)>,
}

#[derive(Default)]
Expand Down Expand Up @@ -458,7 +456,6 @@ impl<'a> Context<'a> {

self.export_table();
self.gc()?;
self.add_wasm_pack_section();

while js.contains("\n\n\n") {
js = js.replace("\n\n\n", "\n\n");
Expand Down Expand Up @@ -1629,28 +1626,6 @@ impl<'a> Context<'a> {
self.globals.push_str("\n");
}

fn add_wasm_pack_section(&mut self) {
if self.module_versions.len() == 0 {
return;
}

#[derive(Serialize)]
struct WasmPackSchema<'a> {
version: &'a str,
modules: &'a [(String, String)],
}

let contents = serde_json::to_string(&WasmPackSchema {
version: "0.0.1",
modules: &self.module_versions,
}).unwrap();

let mut section = CustomSection::default();
*section.name_mut() = "__wasm_pack_unstable".to_string();
*section.payload_mut() = contents.into_bytes();
self.module.sections_mut().push(Section::Custom(section));
}

fn use_node_require(&self) -> bool {
self.config.nodejs && !self.config.nodejs_experimental_modules
}
Expand Down Expand Up @@ -1766,7 +1741,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
}

fn generate_import(&mut self, import: &shared::Import) -> Result<(), Error> {
self.validate_import_module(import)?;
match import.kind {
shared::ImportKind::Function(ref f) => {
self.generate_import_function(import, f).with_context(|_| {
Expand All @@ -1787,41 +1761,6 @@ impl<'a, 'b> SubContext<'a, 'b> {
Ok(())
}

fn validate_import_module(&mut self, import: &shared::Import) -> Result<(), Error> {
let version = match import.version {
Some(ref s) => s,
None => return Ok(()),
};
let module = match import.module {
Some(ref s) => s,
None => return Ok(()),
};
if module.starts_with("./") {
return Ok(());
}
let pkg = if module.starts_with("@") {
// Translate `@foo/bar/baz` to `@foo/bar` and `@foo/bar` to itself
let first_slash = match module.find('/') {
Some(i) => i,
None => bail!(
"packages starting with `@` must be of the form \
`@foo/bar`, but found: `{}`",
module
),
};
match module[first_slash + 1..].find('/') {
Some(i) => &module[..i],
None => module,
}
} else {
module.split('/').next().unwrap()
};
self.cx
.module_versions
.push((pkg.to_string(), version.clone()));
Ok(())
}

fn generate_import_static(
&mut self,
info: &shared::Import,
Expand Down
3 changes: 0 additions & 3 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

extern crate parity_wasm;
extern crate wasm_bindgen_shared as shared;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate wasm_gc;
extern crate wasmi;
Expand Down Expand Up @@ -203,7 +201,6 @@ impl Bindgen {
config: &self,
module: &mut module,
function_table_needed: false,
module_versions: Default::default(),
run_descriptor: &|name| {
let mut v = MyExternals(Vec::new());
match instance.invoke_export(name, &[], &mut v) {
Expand Down
2 changes: 1 addition & 1 deletion crates/js-sys/tests/wasm/Function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn apply() {
assert_eq!(Array::from(&arr).length(), 1);
}

#[wasm_bindgen(module = "tests/wasm/Function.js", version = "*")]
#[wasm_bindgen(module = "tests/wasm/Function.js")]
extern {
fn get_function_to_bind() -> Function;
fn get_value_to_bind_to() -> JsValue;
Expand Down
2 changes: 1 addition & 1 deletion crates/js-sys/tests/wasm/Generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;

#[wasm_bindgen(module = "tests/wasm/Generator.js", version = "*")]
#[wasm_bindgen(module = "tests/wasm/Generator.js")]
extern {
fn one_two_generator() -> Generator;
fn dummy_generator() -> Generator;
Expand Down
2 changes: 1 addition & 1 deletion crates/js-sys/tests/wasm/Object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern {
fn set_foo(this: &Foo42, val: JsValue);
}

#[wasm_bindgen(module = "tests/wasm/Object.js", version = "*")]
#[wasm_bindgen(module = "tests/wasm/Object.js")]
extern {
fn map_with_symbol_key() -> Object;
fn symbol_key() -> JsValue;
Expand Down
2 changes: 1 addition & 1 deletion crates/js-sys/tests/wasm/Proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;

#[wasm_bindgen(module = "tests/wasm/Proxy.js", version = "*")]
#[wasm_bindgen(module = "tests/wasm/Proxy.js")]
extern {
fn proxy_target() -> JsValue;
fn proxy_handler() -> Object;
Expand Down
2 changes: 1 addition & 1 deletion crates/js-sys/tests/wasm/Reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;

#[wasm_bindgen(module = "tests/wasm/Reflect.js", version = "*")]
#[wasm_bindgen(module = "tests/wasm/Reflect.js")]
extern {
fn get_char_at() -> Function;

Expand Down
2 changes: 1 addition & 1 deletion crates/js-sys/tests/wasm/Symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;

#[wasm_bindgen(module = "tests/wasm/Symbol.js", version = "*")]
#[wasm_bindgen(module = "tests/wasm/Symbol.js")]
extern {
fn test_has_instance(sym: &Symbol);
fn test_is_concat_spreadable(sym: &Symbol);
Expand Down
24 changes: 0 additions & 24 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ impl BindgenAttrs {
.next()
}

/// Get the first version attribute
fn version(&self) -> Option<&str> {
self.attrs
.iter()
.filter_map(|a| match a {
BindgenAttr::Version(s) => Some(&s[..]),
_ => None,
})
.next()
}

/// Whether the catch attribute is present
fn catch(&self) -> bool {
self.attrs.iter().any(|a| match a {
Expand Down Expand Up @@ -195,7 +184,6 @@ pub enum BindgenAttr {
StaticMethodOf(Ident),
JsNamespace(Ident),
Module(String),
Version(String),
Getter(Option<Ident>),
Setter(Option<Ident>),
Structural,
Expand Down Expand Up @@ -257,13 +245,6 @@ 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!(=) >>
Expand Down Expand Up @@ -909,10 +890,6 @@ impl<'a> MacroParse<&'a BindgenAttrs> for syn::ForeignItem {
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()).cloned();
let kind = match self {
syn::ForeignItem::Fn(f) => f.convert((item_opts, &module))?,
Expand All @@ -923,7 +900,6 @@ impl<'a> MacroParse<&'a BindgenAttrs> for syn::ForeignItem {

program.imports.push(ast::Import {
module,
version,
js_namespace,
kind,
});
Expand Down
1 change: 0 additions & 1 deletion crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct Program {
#[derive(Deserialize, Serialize)]
pub struct Import {
pub module: Option<String>,
pub version: Option<String>,
pub js_namespace: Option<String>,
pub kind: ImportKind,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/webidl-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
#[wasm_bindgen(module = "{}", version = "*")]
#[wasm_bindgen(module = "{}")]
extern {{
fn not_actually_a_function{1}();
}}
Expand Down
2 changes: 0 additions & 2 deletions crates/webidl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ impl<'src> WebidlParse<'src, ()> for weedle::InterfaceDefinition<'src> {

program.imports.push(backend::ast::Import {
module: None,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Type(backend::ast::ImportType {
vis: public(),
Expand Down Expand Up @@ -718,7 +717,6 @@ impl<'src> WebidlParse<'src, ()> for weedle::EnumDefinition<'src> {
let variants = &self.values.body.list;
program.imports.push(backend::ast::Import {
module: None,
version: None,
js_namespace: None,
kind: backend::ast::ImportKind::Enum(backend::ast::ImportEnum {
vis: public(),
Expand Down
31 changes: 0 additions & 31 deletions guide/src/design/import-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,6 @@ controlling precisely how imports are imported and what they map to in JS. This
section is intended to hopefully be an exhaustive reference of the
possibilities!

* `module` and `version` - we've seen `module` so far indicating where we can
import items from but `version` is also allowed:

```rust
#[wasm_bindgen(module = "moment", version = "^2.0.0")]
extern {
type Moment;
fn moment() -> Moment;
#[wasm_bindgen(method)]
fn format(this: &Moment) -> String;
}
```

The `module` key is used to configure the module that each item is imported
from. The `version` key does not affect the generated wasm itself but rather
it's an informative directive for tools like [wasm-pack]. Tools like wasm-pack
will generate a `package.json` for you and the `version` listed here, when
`module` is also an NPM package, will correspond to what to write down in
`package.json`.

In other words the usage of `module` as the name of an NPM package and
`version` as the version requirement allows you to, inline in Rust, depend on
the NPM ecosystem and import functionality from those packages. When bundled
with a tool like [wasm-pack] everything will automatically get wired up with
bundlers and you should be good to go!

Note that the `version` is *required* if `module` doesn't start with `./`. If
`module` starts with `./` then it is an error to provide a version.

[wasm-pack]: https://github.com/rustwasm/wasm-pack

* `catch` - this attribute allows catching a JS exception. This can be attached
to any imported function and the function must return a `Result` where the
`Err` payload is a `JsValue`, like so:
Expand Down
Loading

0 comments on commit 85fe257

Please sign in to comment.