How to read template literals from js? #2996
-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
#[wasm_bindgen]
pub fn parsing(template: ?) -> Result<Self, JsError> {
// some parsing progress
}
parsing`template {raw} text` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Looking at that MDN page, the signature of a tag functions seems to be: function tag(strings: string[] & { raw: string[] }, ...args: unknown[]): unknown; The variadic So, using a git version of wasm-bindgen, a tag function can be written like this: use js_sys::Array;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(variadic)]
pub fn tag(strings: Array, args: Array) -> Array {
// ...
}
And, if you need to access the use js_sys::Array;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Array)]
pub type Strings;
#[wasm_bindgen(method, getter)]
pub fn raw(this: &Strings) -> Array;
}
#[wasm_bindgen(variadic)]
pub fn tag(strings: Strings, args: Array) -> Array {
// ...
} Again though, this support hasn't been released yet, so to use it you'll need a git version of wasm-bindgen. To do that, you have to add this to your [patch.crates-io]
wasm-bindgen = { git = "https://github.com/rustwasm/wasm-bindgen" } And install the git version of the CLI (note that this will be incompatible with anything compiled with non-git versions of wasm-bindgen): cargo install --git=https://github.com/rustwasm/wasm-bindgen |
Beta Was this translation helpful? Give feedback.
Looking at that MDN page, the signature of a tag functions seems to be:
The variadic
args
argument is the tricky part, since exporting functions with variadic arguments isn't supported as of wasm-bindgen 0.2.81. However, support was added in #2954, and will be there in the next release.So, using a git version of wasm-bindgen, a tag function can be written like this:
#[wasm_bindgen(variadic)]
makes the last argument of the function an array of all the arguments passed af…