mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +00:00
Merge pull request #1353 from alexcrichton/raw-module
Add a `raw_module` attribute to `#[wasm_bindgen]`
This commit is contained in:
commit
2177dee9e4
@ -79,6 +79,7 @@ pub struct Import {
|
|||||||
pub enum ImportModule {
|
pub enum ImportModule {
|
||||||
None,
|
None,
|
||||||
Named(String, Span),
|
Named(String, Span),
|
||||||
|
RawNamed(String, Span),
|
||||||
Inline(usize, Span),
|
Inline(usize, Span),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,6 +97,10 @@ impl Hash for ImportModule {
|
|||||||
2u8.hash(h);
|
2u8.hash(h);
|
||||||
idx.hash(h);
|
idx.hash(h);
|
||||||
}
|
}
|
||||||
|
ImportModule::RawNamed(name, _) => {
|
||||||
|
3u8.hash(h);
|
||||||
|
name.hash(h);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,6 +208,7 @@ fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner) -> Result<Import<
|
|||||||
ast::ImportModule::Named(m, span) => {
|
ast::ImportModule::Named(m, span) => {
|
||||||
ImportModule::Named(intern.resolve_import_module(m, *span)?)
|
ImportModule::Named(intern.resolve_import_module(m, *span)?)
|
||||||
}
|
}
|
||||||
|
ast::ImportModule::RawNamed(m, _span) => ImportModule::RawNamed(intern.intern_str(m)),
|
||||||
ast::ImportModule::Inline(idx, _) => ImportModule::Inline(*idx as u32),
|
ast::ImportModule::Inline(idx, _) => ImportModule::Inline(*idx as u32),
|
||||||
ast::ImportModule::None => ImportModule::None,
|
ast::ImportModule::None => ImportModule::None,
|
||||||
},
|
},
|
||||||
|
@ -2836,6 +2836,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
// not sure how to import them.
|
// not sure how to import them.
|
||||||
let is_local_snippet = match import.module {
|
let is_local_snippet = match import.module {
|
||||||
decode::ImportModule::Named(s) => self.cx.local_modules.contains_key(s),
|
decode::ImportModule::Named(s) => self.cx.local_modules.contains_key(s),
|
||||||
|
decode::ImportModule::RawNamed(_) => false,
|
||||||
decode::ImportModule::Inline(_) => true,
|
decode::ImportModule::Inline(_) => true,
|
||||||
decode::ImportModule::None => false,
|
decode::ImportModule::None => false,
|
||||||
};
|
};
|
||||||
@ -2921,11 +2922,13 @@ impl<'a, 'b> SubContext<'a, 'b> {
|
|||||||
name,
|
name,
|
||||||
field,
|
field,
|
||||||
},
|
},
|
||||||
decode::ImportModule::Named(module) => Import::Module {
|
decode::ImportModule::Named(module) | decode::ImportModule::RawNamed(module) => {
|
||||||
|
Import::Module {
|
||||||
module,
|
module,
|
||||||
name,
|
name,
|
||||||
field,
|
field,
|
||||||
},
|
}
|
||||||
|
}
|
||||||
decode::ImportModule::Inline(idx) => {
|
decode::ImportModule::Inline(idx) => {
|
||||||
let offset = *self
|
let offset = *self
|
||||||
.cx
|
.cx
|
||||||
|
@ -33,6 +33,7 @@ macro_rules! attrgen {
|
|||||||
(static_method_of, StaticMethodOf(Span, Ident)),
|
(static_method_of, StaticMethodOf(Span, Ident)),
|
||||||
(js_namespace, JsNamespace(Span, Ident)),
|
(js_namespace, JsNamespace(Span, Ident)),
|
||||||
(module, Module(Span, String, Span)),
|
(module, Module(Span, String, Span)),
|
||||||
|
(raw_module, RawModule(Span, String, Span)),
|
||||||
(inline_js, InlineJs(Span, String, Span)),
|
(inline_js, InlineJs(Span, String, Span)),
|
||||||
(getter, Getter(Span, Option<Ident>)),
|
(getter, Getter(Span, Option<Ident>)),
|
||||||
(setter, Setter(Span, Option<Ident>)),
|
(setter, Setter(Span, Option<Ident>)),
|
||||||
@ -1085,24 +1086,28 @@ impl MacroParse<BindgenAttrs> for syn::ItemForeignMod {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let module = match opts.module() {
|
let module = if let Some((name, span)) = opts.module() {
|
||||||
Some((name, span)) => {
|
|
||||||
if opts.inline_js().is_some() {
|
if opts.inline_js().is_some() {
|
||||||
let msg = "cannot specify both `module` and `inline_js`";
|
let msg = "cannot specify both `module` and `inline_js`";
|
||||||
errors.push(Diagnostic::span_error(span, msg));
|
errors.push(Diagnostic::span_error(span, msg));
|
||||||
}
|
}
|
||||||
ast::ImportModule::Named(name.to_string(), span)
|
if opts.raw_module().is_some() {
|
||||||
|
let msg = "cannot specify both `module` and `raw_module`";
|
||||||
|
errors.push(Diagnostic::span_error(span, msg));
|
||||||
}
|
}
|
||||||
None => {
|
ast::ImportModule::Named(name.to_string(), span)
|
||||||
match opts.inline_js() {
|
} else if let Some((name, span)) = opts.raw_module() {
|
||||||
Some((js, span)) => {
|
if opts.inline_js().is_some() {
|
||||||
|
let msg = "cannot specify both `raw_module` and `inline_js`";
|
||||||
|
errors.push(Diagnostic::span_error(span, msg));
|
||||||
|
}
|
||||||
|
ast::ImportModule::RawNamed(name.to_string(), span)
|
||||||
|
} else if let Some((js, span)) = opts.inline_js() {
|
||||||
let i = program.inline_js.len();
|
let i = program.inline_js.len();
|
||||||
program.inline_js.push(js.to_string());
|
program.inline_js.push(js.to_string());
|
||||||
ast::ImportModule::Inline(i, span)
|
ast::ImportModule::Inline(i, span)
|
||||||
}
|
} else {
|
||||||
None => ast::ImportModule::None
|
ast::ImportModule::None
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
for item in self.items.into_iter() {
|
for item in self.items.into_iter() {
|
||||||
if let Err(e) = item.macro_parse(program, module.clone()) {
|
if let Err(e) = item.macro_parse(program, module.clone()) {
|
||||||
|
@ -28,6 +28,7 @@ macro_rules! shared_api {
|
|||||||
enum ImportModule<'a> {
|
enum ImportModule<'a> {
|
||||||
None,
|
None,
|
||||||
Named(&'a str),
|
Named(&'a str),
|
||||||
|
RawNamed(&'a str),
|
||||||
Inline(u32),
|
Inline(u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +66,7 @@
|
|||||||
- [`js_namespace`](./reference/attributes/on-js-imports/js_namespace.md)
|
- [`js_namespace`](./reference/attributes/on-js-imports/js_namespace.md)
|
||||||
- [`method`](./reference/attributes/on-js-imports/method.md)
|
- [`method`](./reference/attributes/on-js-imports/method.md)
|
||||||
- [`module = "blah"`](./reference/attributes/on-js-imports/module.md)
|
- [`module = "blah"`](./reference/attributes/on-js-imports/module.md)
|
||||||
|
- [`raw_module = "blah"`](./reference/attributes/on-js-imports/raw_module.md)
|
||||||
- [`static_method_of = Blah`](./reference/attributes/on-js-imports/static_method_of.md)
|
- [`static_method_of = Blah`](./reference/attributes/on-js-imports/static_method_of.md)
|
||||||
- [`structural`](./reference/attributes/on-js-imports/structural.md)
|
- [`structural`](./reference/attributes/on-js-imports/structural.md)
|
||||||
- [`variadic`](./reference/attributes/on-js-imports/variadic.md)
|
- [`variadic`](./reference/attributes/on-js-imports/variadic.md)
|
||||||
|
@ -31,3 +31,8 @@ generates JavaScript import glue like:
|
|||||||
```js
|
```js
|
||||||
let illmatic = this.illmatic;
|
let illmatic = this.illmatic;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note that if the string specified with `module` starts with `./`, `../`, or `/`
|
||||||
|
then it's interpreted as a path to a [local JS snippet](../../js-snippets.html).
|
||||||
|
If this doesn't work for your use case you might be interested in the
|
||||||
|
[`raw_module` attribute](raw_module.html)
|
||||||
|
19
guide/src/reference/attributes/on-js-imports/raw_module.md
Normal file
19
guide/src/reference/attributes/on-js-imports/raw_module.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# `raw_module = "blah"`
|
||||||
|
|
||||||
|
This attribute performs exactly the same purpose as the [`module`
|
||||||
|
attribute](module.html) on JS imports, but it does not attempt to interpret
|
||||||
|
paths starting with `./`, `../`, or `/` as JS snippets. For example:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[wasm_bindgen(raw_module = "./some/js/file.js")]
|
||||||
|
extern "C" {
|
||||||
|
fn the_function();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that if you use this attribute with a relative or absolute path, it's
|
||||||
|
likely up to the final bundler or project to assign meaning to that path. This
|
||||||
|
typically means that the JS file or module will be resolved relative to the
|
||||||
|
final location of the wasm file itself. That means that `raw_module` is likely
|
||||||
|
unsuitable for libraries on crates.io, but may be usable within end-user
|
||||||
|
applications.
|
Loading…
x
Reference in New Issue
Block a user