Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
use crate::descriptor::{Descriptor, Function};
|
2018-10-18 08:43:36 -07:00
|
|
|
use crate::js::js2rust::ExportedShim;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
use crate::js::{Context, ImportTarget, Js2Rust};
|
|
|
|
use failure::{bail, Error};
|
2018-04-16 13:31:56 -07:00
|
|
|
|
2018-05-22 12:34:41 -05:00
|
|
|
/// Helper struct for manufacturing a shim in JS used to translate Rust types to
|
2018-04-16 13:31:56 -07:00
|
|
|
/// JS, then invoking an imported JS function.
|
|
|
|
pub struct Rust2Js<'a, 'b: 'a> {
|
2018-11-12 11:59:13 -08:00
|
|
|
pub cx: &'a mut Context<'b>,
|
2018-04-16 13:31:56 -07:00
|
|
|
|
|
|
|
/// Arguments of the JS shim that we're generating, aka the variables passed
|
|
|
|
/// from Rust which are only numbers.
|
|
|
|
shim_arguments: Vec<String>,
|
|
|
|
|
|
|
|
/// Arguments which are forwarded to the imported JS function
|
|
|
|
js_arguments: Vec<String>,
|
|
|
|
|
|
|
|
/// Conversions that happen before we invoke the wasm function, such as
|
|
|
|
/// converting a string to a ptr/length pair.
|
|
|
|
prelude: String,
|
|
|
|
|
|
|
|
/// "Destructors" or cleanup that must happen after the wasm function
|
|
|
|
/// finishes. This is scheduled in a `finally` block.
|
|
|
|
finally: String,
|
|
|
|
|
|
|
|
/// Next global index to write to when passing arguments via the single
|
|
|
|
/// global stack.
|
|
|
|
global_idx: usize,
|
|
|
|
|
|
|
|
/// Index of the next argument for unique name generation purposes.
|
|
|
|
arg_idx: usize,
|
|
|
|
|
|
|
|
/// Expression used to generate the return value. The string "JS" in this
|
|
|
|
/// expression is replaced with the actual JS invocation eventually.
|
|
|
|
ret_expr: String,
|
|
|
|
|
|
|
|
/// Whether or not we're catching JS exceptions
|
|
|
|
catch: bool,
|
2019-01-14 15:59:31 -08:00
|
|
|
catch_and_rethrow: bool,
|
2018-08-19 13:39:16 +01:00
|
|
|
|
|
|
|
/// Whether or not the last argument is a slice representing variadic arguments.
|
|
|
|
variadic: bool,
|
2018-10-18 08:43:36 -07:00
|
|
|
|
|
|
|
/// list of arguments that are anyref, and whether they're an owned anyref
|
|
|
|
/// or not.
|
|
|
|
pub anyref_args: Vec<(usize, bool)>,
|
|
|
|
pub ret_anyref: bool,
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Rust2Js<'a, 'b> {
|
|
|
|
pub fn new(cx: &'a mut Context<'b>) -> Rust2Js<'a, 'b> {
|
|
|
|
Rust2Js {
|
|
|
|
cx,
|
|
|
|
shim_arguments: Vec::new(),
|
|
|
|
js_arguments: Vec::new(),
|
|
|
|
prelude: String::new(),
|
|
|
|
finally: String::new(),
|
|
|
|
global_idx: 0,
|
|
|
|
arg_idx: 0,
|
|
|
|
ret_expr: String::new(),
|
|
|
|
catch: false,
|
2019-01-14 15:59:31 -08:00
|
|
|
catch_and_rethrow: false,
|
2018-08-19 13:39:16 +01:00
|
|
|
variadic: false,
|
2018-10-18 08:43:36 -07:00
|
|
|
anyref_args: Vec::new(),
|
|
|
|
ret_anyref: false,
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn catch(&mut self, catch: bool) -> &mut Self {
|
|
|
|
self.catch = catch;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-01-14 15:59:31 -08:00
|
|
|
pub fn catch_and_rethrow(&mut self, catch_and_rethrow: bool) -> &mut Self {
|
|
|
|
self.catch_and_rethrow = catch_and_rethrow;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-19 13:39:16 +01:00
|
|
|
pub fn variadic(&mut self, variadic: bool) -> &mut Self {
|
|
|
|
self.variadic = variadic;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-16 13:31:56 -07:00
|
|
|
/// Generates all bindings necessary for the signature in `Function`,
|
|
|
|
/// creating necessary argument conversions and return value processing.
|
2018-04-25 11:42:22 -07:00
|
|
|
pub fn process(&mut self, function: &Function) -> Result<&mut Self, Error> {
|
2018-04-16 13:31:56 -07:00
|
|
|
for arg in function.arguments.iter() {
|
2018-04-25 11:42:22 -07:00
|
|
|
self.argument(arg)?;
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
2018-04-25 11:42:22 -07:00
|
|
|
self.ret(&function.ret)?;
|
|
|
|
Ok(self)
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
2018-08-19 13:39:16 +01:00
|
|
|
/// Get a generated name for an argument.
|
2018-05-02 21:03:50 -07:00
|
|
|
fn shim_argument(&mut self) -> String {
|
|
|
|
let s = format!("arg{}", self.arg_idx);
|
2018-04-16 13:31:56 -07:00
|
|
|
self.arg_idx += 1;
|
2018-05-02 21:03:50 -07:00
|
|
|
self.shim_arguments.push(s.clone());
|
|
|
|
s
|
|
|
|
}
|
2018-04-16 13:31:56 -07:00
|
|
|
|
2018-05-02 21:03:50 -07:00
|
|
|
fn argument(&mut self, arg: &Descriptor) -> Result<(), Error> {
|
|
|
|
let abi = self.shim_argument();
|
2018-04-16 13:31:56 -07:00
|
|
|
|
2018-07-19 14:44:23 -05:00
|
|
|
let (arg, optional) = match arg {
|
|
|
|
Descriptor::Option(t) => (&**t, true),
|
|
|
|
_ => (arg, false),
|
|
|
|
};
|
|
|
|
|
2018-04-16 13:31:56 -07:00
|
|
|
if let Some(ty) = arg.vector_kind() {
|
2018-05-02 21:03:50 -07:00
|
|
|
let abi2 = self.shim_argument();
|
2018-10-18 08:43:36 -07:00
|
|
|
let f = self.cx.expose_get_vector_from_wasm(ty)?;
|
2018-06-27 22:42:34 -07:00
|
|
|
self.prelude(&format!(
|
2018-07-19 14:44:23 -05:00
|
|
|
"let v{0} = {prefix}{func}({0}, {1});",
|
2018-06-27 22:42:34 -07:00
|
|
|
abi,
|
|
|
|
abi2,
|
2018-07-19 14:44:23 -05:00
|
|
|
func = f,
|
2018-09-26 08:26:00 -07:00
|
|
|
prefix = if optional {
|
|
|
|
format!("{} == 0 ? undefined : ", abi)
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
},
|
2018-06-27 22:42:34 -07:00
|
|
|
));
|
2018-04-16 13:31:56 -07:00
|
|
|
|
2018-09-24 13:49:12 -07:00
|
|
|
if !arg.is_by_ref() && !arg.is_clamped_by_ref() {
|
2018-06-27 22:42:34 -07:00
|
|
|
self.prelude(&format!(
|
|
|
|
"\
|
2018-07-19 14:44:23 -05:00
|
|
|
{start}
|
|
|
|
v{0} = v{0}.slice();
|
|
|
|
wasm.__wbindgen_free({0}, {1} * {size});
|
|
|
|
{end}\
|
2018-06-27 22:42:34 -07:00
|
|
|
",
|
|
|
|
abi,
|
|
|
|
abi2,
|
2018-07-19 14:44:23 -05:00
|
|
|
size = ty.size(),
|
2018-09-26 08:26:00 -07:00
|
|
|
start = if optional {
|
|
|
|
format!("if ({} !== 0) {{", abi)
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
},
|
2018-07-19 14:44:23 -05:00
|
|
|
end = if optional { "}" } else { "" },
|
2018-06-27 22:42:34 -07:00
|
|
|
));
|
2018-04-25 11:42:22 -07:00
|
|
|
self.cx.require_internal_export("__wbindgen_free")?;
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
2018-05-02 21:03:50 -07:00
|
|
|
self.js_arguments.push(format!("v{}", abi));
|
2018-06-27 22:42:34 -07:00
|
|
|
return Ok(());
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:44:23 -05:00
|
|
|
// No need to special case `optional` here because `takeObject` will
|
|
|
|
// naturally work.
|
|
|
|
if arg.is_anyref() {
|
2018-10-18 08:43:36 -07:00
|
|
|
let arg = self.cx.take_object(&abi);
|
|
|
|
self.js_arguments.push(arg);
|
|
|
|
self.anyref_args.push((self.arg_idx - 1, true));
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(());
|
2018-08-02 22:28:13 -07:00
|
|
|
} else if arg.is_ref_anyref() {
|
2018-10-18 08:43:36 -07:00
|
|
|
let arg = self.cx.get_object(&abi);
|
|
|
|
self.js_arguments.push(arg);
|
|
|
|
self.anyref_args.push((self.arg_idx - 1, false));
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(());
|
2018-07-19 14:44:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if optional {
|
2018-08-03 18:40:55 +03:00
|
|
|
if arg.is_wasm_native() {
|
2018-08-03 16:28:35 +03:00
|
|
|
let value = self.shim_argument();
|
|
|
|
self.js_arguments.push(format!(
|
|
|
|
"{present} === 0 ? undefined : {value}",
|
|
|
|
value = value,
|
|
|
|
present = abi,
|
|
|
|
));
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(());
|
2018-08-03 16:28:35 +03:00
|
|
|
}
|
|
|
|
|
2018-08-03 18:40:55 +03:00
|
|
|
if arg.is_abi_as_u32() {
|
2018-09-26 08:26:00 -07:00
|
|
|
self.js_arguments
|
|
|
|
.push(format!("{0} === 0xFFFFFF ? undefined : {0}", abi));
|
|
|
|
return Ok(());
|
2018-08-03 16:28:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(signed) = arg.get_64() {
|
|
|
|
let f = if signed {
|
|
|
|
self.cx.expose_int64_cvt_shim()
|
|
|
|
} else {
|
|
|
|
self.cx.expose_uint64_cvt_shim()
|
|
|
|
};
|
|
|
|
self.shim_argument();
|
|
|
|
let low = self.shim_argument();
|
|
|
|
let high = self.shim_argument();
|
|
|
|
let name = format!("n{}", abi);
|
|
|
|
self.prelude(&format!(
|
2018-08-03 18:40:55 +03:00
|
|
|
"
|
|
|
|
u32CvtShim[0] = {present} === 0 ? 0 : {low};
|
|
|
|
u32CvtShim[1] = {present} === 0 ? 0 : {high};
|
|
|
|
const {name} = {present} === 0 ? undefined : {f}[0];
|
2018-08-03 16:28:35 +03:00
|
|
|
",
|
|
|
|
present = abi,
|
|
|
|
low = low,
|
|
|
|
high = high,
|
|
|
|
f = f,
|
|
|
|
name = name,
|
|
|
|
));
|
|
|
|
self.js_arguments.push(name);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-08-03 19:07:12 +03:00
|
|
|
match *arg {
|
|
|
|
Descriptor::Boolean => {
|
2018-09-26 08:26:00 -07:00
|
|
|
self.js_arguments
|
|
|
|
.push(format!("{0} === 0xFFFFFF ? undefined : {0} !== 0", abi));
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-01-28 14:12:02 -08:00
|
|
|
Descriptor::Enum { hole } => {
|
|
|
|
self.js_arguments
|
|
|
|
.push(format!("{0} === {1} ? undefined : {0}", abi, hole));
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-08-03 20:45:57 +03:00
|
|
|
Descriptor::Char => {
|
2019-04-16 10:52:27 -07:00
|
|
|
self.js_arguments.push(format!(
|
|
|
|
"{0} === 0xFFFFFF ? undefined : String.fromCodePoint({0})",
|
|
|
|
abi
|
|
|
|
));
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-02-19 09:08:37 -08:00
|
|
|
Descriptor::RustStruct(ref class) => {
|
|
|
|
self.cx.require_class_wrap(class);
|
2019-03-26 08:00:16 -07:00
|
|
|
let assign = format!(
|
|
|
|
"let c{0} = {0} === 0 ? undefined : {1}.__wrap({0});",
|
|
|
|
abi, class
|
|
|
|
);
|
2019-02-19 09:08:37 -08:00
|
|
|
self.prelude(&assign);
|
|
|
|
self.js_arguments.push(format!("c{}", abi));
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-09-26 08:26:00 -07:00
|
|
|
_ => bail!(
|
|
|
|
"unsupported optional argument type for calling JS function from Rust: {:?}",
|
|
|
|
arg
|
|
|
|
),
|
2018-08-03 19:07:12 +03:00
|
|
|
};
|
2018-07-19 14:44:23 -05:00
|
|
|
}
|
|
|
|
|
2018-08-03 16:28:35 +03:00
|
|
|
if let Some(signed) = arg.get_64() {
|
2018-05-05 14:10:25 -07:00
|
|
|
let f = if signed {
|
|
|
|
self.cx.expose_int64_cvt_shim()
|
|
|
|
} else {
|
|
|
|
self.cx.expose_uint64_cvt_shim()
|
|
|
|
};
|
2018-08-03 16:28:35 +03:00
|
|
|
let high = self.shim_argument();
|
2018-05-05 14:10:25 -07:00
|
|
|
let name = format!("n{}", abi);
|
2018-06-27 22:42:34 -07:00
|
|
|
self.prelude(&format!(
|
|
|
|
"\
|
2018-08-03 18:40:55 +03:00
|
|
|
u32CvtShim[0] = {low};
|
|
|
|
u32CvtShim[1] = {high};
|
|
|
|
const {name} = {f}[0];
|
2018-06-27 22:42:34 -07:00
|
|
|
",
|
2018-08-03 16:28:35 +03:00
|
|
|
low = abi,
|
|
|
|
high = high,
|
2018-05-05 14:10:25 -07:00
|
|
|
f = f,
|
|
|
|
name = name,
|
|
|
|
));
|
|
|
|
self.js_arguments.push(name);
|
2018-06-27 22:42:34 -07:00
|
|
|
return Ok(());
|
2018-05-05 14:10:25 -07:00
|
|
|
}
|
|
|
|
|
2018-04-16 13:31:56 -07:00
|
|
|
if let Some(class) = arg.rust_struct() {
|
|
|
|
if arg.is_by_ref() {
|
2018-04-25 11:42:22 -07:00
|
|
|
bail!("cannot invoke JS functions with custom ref types yet")
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
2018-09-21 15:45:31 -07:00
|
|
|
self.cx.require_class_wrap(class);
|
|
|
|
let assign = format!("let c{0} = {1}.__wrap({0});", abi, class);
|
2018-04-16 13:31:56 -07:00
|
|
|
self.prelude(&assign);
|
2018-05-02 21:03:50 -07:00
|
|
|
self.js_arguments.push(format!("c{}", abi));
|
2018-06-27 22:42:34 -07:00
|
|
|
return Ok(());
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((f, mutable)) = arg.stack_closure() {
|
2018-11-29 12:01:16 -08:00
|
|
|
let arg2 = self.shim_argument();
|
2018-10-18 08:43:36 -07:00
|
|
|
let mut shim = f.shim_idx;
|
2018-07-09 11:07:57 -05:00
|
|
|
let (js, _ts, _js_doc) = {
|
2018-04-16 13:31:56 -07:00
|
|
|
let mut builder = Js2Rust::new("", self.cx);
|
|
|
|
if mutable {
|
2018-06-27 22:42:34 -07:00
|
|
|
builder
|
|
|
|
.prelude("let a = this.a;\n")
|
2018-04-16 13:31:56 -07:00
|
|
|
.prelude("this.a = 0;\n")
|
|
|
|
.rust_argument("a")
|
|
|
|
.finally("this.a = a;\n");
|
|
|
|
} else {
|
|
|
|
builder.rust_argument("this.a");
|
|
|
|
}
|
2019-03-14 08:46:42 -03:00
|
|
|
builder.rust_argument("this.b").process(f, None)?.finish(
|
2018-10-18 08:43:36 -07:00
|
|
|
"function",
|
|
|
|
"this.f",
|
|
|
|
ExportedShim::TableElement(&mut shim),
|
|
|
|
)
|
2018-04-16 13:31:56 -07:00
|
|
|
};
|
|
|
|
self.cx.function_table_needed = true;
|
|
|
|
self.global_idx();
|
2018-06-27 22:42:34 -07:00
|
|
|
self.prelude(&format!(
|
|
|
|
"\
|
|
|
|
let cb{0} = {js};\n\
|
2018-11-29 12:01:16 -08:00
|
|
|
cb{0}.f = wasm.__wbg_function_table.get({idx});\n\
|
|
|
|
cb{0}.a = {0};\n\
|
|
|
|
cb{0}.b = {1};\n\
|
2018-06-27 22:42:34 -07:00
|
|
|
",
|
|
|
|
abi,
|
2018-11-29 12:01:16 -08:00
|
|
|
arg2,
|
2018-06-27 22:42:34 -07:00
|
|
|
js = js,
|
2018-10-18 08:43:36 -07:00
|
|
|
idx = shim,
|
2018-06-27 22:42:34 -07:00
|
|
|
));
|
2018-05-02 21:03:50 -07:00
|
|
|
self.finally(&format!("cb{0}.a = cb{0}.b = 0;", abi));
|
|
|
|
self.js_arguments.push(format!("cb{0}.bind(cb{0})", abi));
|
2018-06-27 22:42:34 -07:00
|
|
|
return Ok(());
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
2019-03-26 17:29:53 -07:00
|
|
|
if let Some(num) = arg.number() {
|
|
|
|
if num.is_u32() {
|
|
|
|
self.js_arguments.push(format!("{} >>> 0", abi));
|
|
|
|
} else {
|
|
|
|
self.js_arguments.push(abi);
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-04-16 13:31:56 -07:00
|
|
|
let invoc_arg = match *arg {
|
2018-05-02 21:03:50 -07:00
|
|
|
Descriptor::Boolean => format!("{} !== 0", abi),
|
2018-06-27 22:42:34 -07:00
|
|
|
Descriptor::Char => format!("String.fromCodePoint({})", abi),
|
2018-09-26 08:26:00 -07:00
|
|
|
_ => bail!(
|
|
|
|
"unsupported argument type for calling JS function from Rust: {:?}",
|
|
|
|
arg
|
|
|
|
),
|
2018-04-16 13:31:56 -07:00
|
|
|
};
|
|
|
|
self.js_arguments.push(invoc_arg);
|
2018-04-25 11:42:22 -07:00
|
|
|
Ok(())
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 18:26:45 -07:00
|
|
|
fn ret(&mut self, ty: &Descriptor) -> Result<(), Error> {
|
|
|
|
if let Descriptor::Unit = ty {
|
|
|
|
self.ret_expr = "JS;".to_string();
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-07-19 14:44:23 -05:00
|
|
|
let (ty, optional) = match ty {
|
|
|
|
Descriptor::Option(t) => (&**t, true),
|
|
|
|
_ => (ty, false),
|
|
|
|
};
|
2018-04-16 13:31:56 -07:00
|
|
|
if ty.is_by_ref() {
|
2018-04-25 11:42:22 -07:00
|
|
|
bail!("cannot return a reference from JS to Rust")
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
if let Some(ty) = ty.vector_kind() {
|
2018-04-25 11:42:22 -07:00
|
|
|
let f = self.cx.pass_to_wasm_function(ty)?;
|
2018-04-16 13:31:56 -07:00
|
|
|
self.cx.expose_uint32_memory();
|
2018-05-02 21:03:50 -07:00
|
|
|
self.shim_arguments.insert(0, "ret".to_string());
|
2018-07-19 14:44:23 -05:00
|
|
|
let mut prelude = String::new();
|
|
|
|
let expr = if optional {
|
|
|
|
prelude.push_str("const val = JS;");
|
|
|
|
self.cx.expose_is_like_none();
|
|
|
|
format!("isLikeNone(val) ? [0, 0] : {}(val)", f)
|
|
|
|
} else {
|
|
|
|
format!("{}(JS)", f)
|
|
|
|
};
|
2018-06-27 22:42:34 -07:00
|
|
|
self.ret_expr = format!(
|
|
|
|
"\
|
2018-07-19 14:44:23 -05:00
|
|
|
{}
|
2018-11-13 08:10:05 -08:00
|
|
|
const retptr = {};
|
|
|
|
const retlen = WASM_VECTOR_LEN;
|
2018-05-02 21:03:50 -07:00
|
|
|
const mem = getUint32Memory();
|
|
|
|
mem[ret / 4] = retptr;
|
|
|
|
mem[ret / 4 + 1] = retlen;
|
2018-06-15 23:39:51 -07:00
|
|
|
",
|
2018-09-26 08:26:00 -07:00
|
|
|
prelude, expr
|
2018-06-27 22:42:34 -07:00
|
|
|
);
|
|
|
|
return Ok(());
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
2018-07-19 14:44:23 -05:00
|
|
|
if ty.is_anyref() {
|
2018-10-18 08:43:36 -07:00
|
|
|
if self.cx.config.anyref {
|
|
|
|
if optional {
|
|
|
|
self.cx.expose_add_to_anyref_table()?;
|
|
|
|
self.cx.expose_is_like_none();
|
|
|
|
self.ret_expr = "
|
|
|
|
const val = JS;
|
|
|
|
return isLikeNone(val) ? 0 : addToAnyrefTable(val);
|
|
|
|
"
|
|
|
|
.to_string();
|
|
|
|
} else {
|
|
|
|
self.ret_anyref = true;
|
|
|
|
self.ret_expr = "return JS;".to_string()
|
|
|
|
}
|
2018-07-19 14:44:23 -05:00
|
|
|
} else {
|
2018-10-18 08:43:36 -07:00
|
|
|
self.cx.expose_add_heap_object();
|
|
|
|
if optional {
|
|
|
|
self.cx.expose_is_like_none();
|
|
|
|
self.ret_expr = "
|
|
|
|
const val = JS;
|
|
|
|
return isLikeNone(val) ? 0 : addHeapObject(val);
|
|
|
|
"
|
|
|
|
.to_string();
|
|
|
|
} else {
|
|
|
|
self.ret_expr = "return addHeapObject(JS);".to_string()
|
|
|
|
}
|
2018-07-19 14:44:23 -05:00
|
|
|
}
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(());
|
2018-07-19 14:44:23 -05:00
|
|
|
}
|
|
|
|
if optional {
|
2019-03-26 18:27:49 +00:00
|
|
|
self.cx.expose_is_like_none();
|
|
|
|
|
2018-08-03 18:40:55 +03:00
|
|
|
if ty.is_wasm_native() {
|
2018-08-03 16:28:35 +03:00
|
|
|
self.cx.expose_uint32_memory();
|
|
|
|
match ty {
|
|
|
|
Descriptor::I32 => self.cx.expose_int32_memory(),
|
|
|
|
Descriptor::U32 => (),
|
|
|
|
Descriptor::F32 => self.cx.expose_f32_memory(),
|
|
|
|
Descriptor::F64 => self.cx.expose_f64_memory(),
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
self.shim_arguments.insert(0, "ret".to_string());
|
|
|
|
self.ret_expr = format!(
|
2018-08-03 18:40:55 +03:00
|
|
|
"
|
|
|
|
const val = JS;
|
|
|
|
getUint32Memory()[ret / 4] = !isLikeNone(val);
|
|
|
|
{mem}[ret / {size} + 1] = isLikeNone(val) ? 0 : val;
|
2018-08-03 16:28:35 +03:00
|
|
|
",
|
|
|
|
size = match ty {
|
|
|
|
Descriptor::I32 => 4,
|
|
|
|
Descriptor::U32 => 4,
|
|
|
|
Descriptor::F32 => 4,
|
|
|
|
Descriptor::F64 => 8,
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
mem = match ty {
|
|
|
|
Descriptor::I32 => "getInt32Memory()",
|
|
|
|
Descriptor::U32 => "getUint32Memory()",
|
|
|
|
Descriptor::F32 => "getFloat32Memory()",
|
|
|
|
Descriptor::F64 => "getFloat64Memory()",
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:40:55 +03:00
|
|
|
if ty.is_abi_as_u32() {
|
2018-08-03 16:28:35 +03:00
|
|
|
self.ret_expr = "
|
|
|
|
const val = JS;
|
|
|
|
return isLikeNone(val) ? 0xFFFFFF : val;
|
2018-11-27 12:07:59 -08:00
|
|
|
"
|
|
|
|
.to_string();
|
2018-08-03 16:28:35 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(signed) = ty.get_64() {
|
|
|
|
self.cx.expose_uint32_memory();
|
|
|
|
let f = if signed {
|
|
|
|
self.cx.expose_int64_memory();
|
|
|
|
"getInt64Memory"
|
|
|
|
} else {
|
|
|
|
self.cx.expose_uint64_memory();
|
|
|
|
"getUint64Memory"
|
|
|
|
};
|
|
|
|
self.shim_arguments.insert(0, "ret".to_string());
|
|
|
|
self.ret_expr = format!(
|
2018-08-03 18:40:55 +03:00
|
|
|
"
|
|
|
|
const val = JS;
|
|
|
|
getUint32Memory()[ret / 4] = !isLikeNone(val);
|
|
|
|
{}()[ret / 8 + 1] = isLikeNone(val) ? BigInt(0) : val;
|
2018-08-03 16:28:35 +03:00
|
|
|
",
|
|
|
|
f
|
|
|
|
);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-08-03 19:07:12 +03:00
|
|
|
match *ty {
|
|
|
|
Descriptor::Boolean => {
|
|
|
|
self.ret_expr = "
|
|
|
|
const val = JS;
|
|
|
|
return isLikeNone(val) ? 0xFFFFFF : val ? 1 : 0;
|
2018-11-27 12:07:59 -08:00
|
|
|
"
|
|
|
|
.to_string();
|
2018-09-26 08:26:00 -07:00
|
|
|
}
|
2018-08-03 20:45:57 +03:00
|
|
|
Descriptor::Char => {
|
|
|
|
self.ret_expr = "
|
|
|
|
const val = JS;
|
2019-03-26 18:12:24 +00:00
|
|
|
return isLikeNone(val) ? 0xFFFFFF : val.codePointAt(0);
|
2018-11-27 12:07:59 -08:00
|
|
|
"
|
|
|
|
.to_string();
|
2018-09-26 08:26:00 -07:00
|
|
|
}
|
2019-01-28 14:12:02 -08:00
|
|
|
Descriptor::Enum { hole } => {
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
self.ret_expr = format!(
|
|
|
|
"
|
2019-01-28 14:12:02 -08:00
|
|
|
const val = JS;
|
|
|
|
return isLikeNone(val) ? {} : val;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
",
|
|
|
|
hole
|
|
|
|
);
|
2019-01-28 14:12:02 -08:00
|
|
|
}
|
2019-02-19 09:08:37 -08:00
|
|
|
Descriptor::RustStruct(ref class) => {
|
|
|
|
// Like below, assert the type
|
|
|
|
self.ret_expr = format!(
|
|
|
|
"\
|
|
|
|
const val = JS;
|
2019-03-26 18:27:49 +00:00
|
|
|
if (isLikeNone(val))
|
2019-02-19 09:08:37 -08:00
|
|
|
return 0;
|
|
|
|
if (!(val instanceof {0})) {{
|
|
|
|
throw new Error('expected value of type {0}');
|
|
|
|
}}
|
|
|
|
const ret = val.ptr;
|
|
|
|
val.ptr = 0;
|
|
|
|
return ret;\
|
|
|
|
",
|
|
|
|
class
|
|
|
|
);
|
|
|
|
}
|
2018-09-26 08:26:00 -07:00
|
|
|
_ => bail!(
|
|
|
|
"unsupported optional return type for calling JS function from Rust: {:?}",
|
|
|
|
ty
|
|
|
|
),
|
2018-08-03 19:07:12 +03:00
|
|
|
};
|
2019-03-26 18:27:49 +00:00
|
|
|
|
|
|
|
return Ok(());
|
2018-07-19 14:44:23 -05:00
|
|
|
}
|
2019-03-26 17:29:53 -07:00
|
|
|
if ty.number().is_some() {
|
2018-04-16 13:31:56 -07:00
|
|
|
self.ret_expr = "return JS;".to_string();
|
2018-06-27 22:42:34 -07:00
|
|
|
return Ok(());
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
2018-08-03 16:28:35 +03:00
|
|
|
if let Some(signed) = ty.get_64() {
|
2018-05-05 14:10:25 -07:00
|
|
|
let f = if signed {
|
|
|
|
self.cx.expose_int64_memory();
|
|
|
|
"getInt64Memory"
|
|
|
|
} else {
|
|
|
|
self.cx.expose_uint64_memory();
|
|
|
|
"getUint64Memory"
|
|
|
|
};
|
|
|
|
self.shim_arguments.insert(0, "ret".to_string());
|
2018-06-27 22:42:34 -07:00
|
|
|
self.ret_expr = format!(
|
|
|
|
"\
|
|
|
|
const val = JS;\n\
|
|
|
|
{}()[ret / 8] = val;\n\
|
|
|
|
",
|
|
|
|
f
|
|
|
|
);
|
|
|
|
return Ok(());
|
2018-05-05 14:10:25 -07:00
|
|
|
}
|
2018-06-28 20:08:02 -05:00
|
|
|
|
|
|
|
if let Some(class) = ty.rust_struct() {
|
|
|
|
if ty.is_by_ref() {
|
|
|
|
bail!("cannot invoke JS functions returning custom ref types yet")
|
|
|
|
}
|
|
|
|
// Insert an assertion to the type of the returned value as
|
|
|
|
// otherwise this will cause memory unsafety on the Rust side of
|
|
|
|
// things.
|
2018-07-08 22:09:00 -04:00
|
|
|
self.ret_expr = format!(
|
|
|
|
"\
|
2018-06-28 20:08:02 -05:00
|
|
|
const val = JS;
|
|
|
|
if (!(val instanceof {0})) {{
|
|
|
|
throw new Error('expected value of type {0}');
|
|
|
|
}}
|
|
|
|
const ret = val.ptr;
|
|
|
|
val.ptr = 0;
|
|
|
|
return ret;\
|
2018-07-08 22:09:00 -04:00
|
|
|
",
|
|
|
|
class
|
|
|
|
);
|
|
|
|
return Ok(());
|
2018-06-28 20:08:02 -05:00
|
|
|
}
|
|
|
|
|
2018-04-16 13:31:56 -07:00
|
|
|
self.ret_expr = match *ty {
|
2018-11-08 13:06:03 -08:00
|
|
|
Descriptor::Boolean => "return JS;".to_string(),
|
2018-06-15 12:55:37 -05:00
|
|
|
Descriptor::Char => "return JS.codePointAt(0);".to_string(),
|
2018-09-26 08:26:00 -07:00
|
|
|
_ => bail!(
|
|
|
|
"unsupported return type for calling JS function from Rust: {:?}",
|
|
|
|
ty
|
|
|
|
),
|
2018-04-25 11:42:22 -07:00
|
|
|
};
|
|
|
|
Ok(())
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
2018-11-12 11:59:13 -08:00
|
|
|
/// Returns whether this shim won't actually do anything when called other
|
|
|
|
/// than forward the invocation somewhere else.
|
|
|
|
///
|
|
|
|
/// This is used as an optimization to wire up imports directly where
|
|
|
|
/// possible and avoid a shim in some circumstances.
|
|
|
|
pub fn is_noop(&self) -> bool {
|
|
|
|
let Rust2Js {
|
|
|
|
// fields which may affect whether we do nontrivial work
|
|
|
|
catch,
|
2019-01-14 15:59:31 -08:00
|
|
|
catch_and_rethrow,
|
2018-11-12 11:59:13 -08:00
|
|
|
finally,
|
|
|
|
js_arguments,
|
|
|
|
prelude,
|
|
|
|
ret_expr,
|
|
|
|
variadic,
|
|
|
|
shim_arguments,
|
|
|
|
|
|
|
|
// all other fields, listed explicitly here so if one is added we'll
|
|
|
|
// trigger a nonexhaustive error.
|
|
|
|
arg_idx: _,
|
|
|
|
cx: _,
|
|
|
|
global_idx: _,
|
2018-10-18 08:43:36 -07:00
|
|
|
anyref_args: _,
|
|
|
|
ret_anyref: _,
|
2018-11-12 11:59:13 -08:00
|
|
|
} = self;
|
|
|
|
|
|
|
|
!catch &&
|
2019-01-14 15:59:31 -08:00
|
|
|
!catch_and_rethrow &&
|
2018-11-12 11:59:13 -08:00
|
|
|
!variadic &&
|
|
|
|
prelude.is_empty() &&
|
|
|
|
finally.is_empty() &&
|
|
|
|
// make sure our faux return expression is "simple" by not
|
|
|
|
// performing any sort of transformation on the return value
|
|
|
|
(ret_expr == "JS;" || ret_expr == "return JS;") &&
|
|
|
|
// similarly we want to make sure that all the arguments are simply
|
|
|
|
// forwarded from the shim we would generate to the import,
|
|
|
|
// requiring no transformations
|
|
|
|
js_arguments == shim_arguments
|
|
|
|
}
|
|
|
|
|
2018-10-18 08:43:36 -07:00
|
|
|
pub fn finish(&mut self, invoc: &ImportTarget, shim: &str) -> Result<String, Error> {
|
2018-04-16 13:31:56 -07:00
|
|
|
let mut ret = String::new();
|
|
|
|
ret.push_str("function(");
|
|
|
|
ret.push_str(&self.shim_arguments.join(", "));
|
|
|
|
if self.catch {
|
|
|
|
if self.shim_arguments.len() > 0 {
|
|
|
|
ret.push_str(", ")
|
|
|
|
}
|
|
|
|
ret.push_str("exnptr");
|
|
|
|
}
|
|
|
|
ret.push_str(") {\n");
|
2018-06-15 12:55:37 -05:00
|
|
|
ret.push_str(&self.prelude);
|
2018-04-16 13:31:56 -07:00
|
|
|
|
2018-11-29 17:50:13 -08:00
|
|
|
let variadic = self.variadic;
|
|
|
|
let ret_expr = &self.ret_expr;
|
2018-11-08 12:31:39 -08:00
|
|
|
let handle_variadic = |invoc: &str, js_arguments: &[String]| {
|
2018-11-29 17:50:13 -08:00
|
|
|
let ret = if variadic {
|
2018-11-08 12:31:39 -08:00
|
|
|
let (last_arg, args) = match js_arguments.split_last() {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => bail!("a function with no arguments cannot be variadic"),
|
|
|
|
};
|
|
|
|
if args.len() > 0 {
|
2018-11-29 17:50:13 -08:00
|
|
|
ret_expr.replace(
|
2018-11-08 12:31:39 -08:00
|
|
|
"JS",
|
|
|
|
&format!("{}({}, ...{})", invoc, args.join(", "), last_arg),
|
|
|
|
)
|
|
|
|
} else {
|
2018-11-30 13:04:05 -08:00
|
|
|
ret_expr.replace("JS", &format!("{}(...{})", invoc, last_arg))
|
2018-11-08 12:31:39 -08:00
|
|
|
}
|
2018-09-06 20:00:38 +03:00
|
|
|
} else {
|
2018-11-30 13:04:05 -08:00
|
|
|
ret_expr.replace("JS", &format!("{}({})", invoc, js_arguments.join(", ")))
|
2018-11-08 12:31:39 -08:00
|
|
|
};
|
|
|
|
Ok(ret)
|
|
|
|
};
|
|
|
|
|
2018-10-18 08:43:36 -07:00
|
|
|
let js_arguments = &self.js_arguments;
|
2018-11-08 12:31:39 -08:00
|
|
|
let fixed = |desc: &str, class: &Option<String>, amt: usize| {
|
2018-11-29 17:50:13 -08:00
|
|
|
if variadic {
|
2018-11-08 12:31:39 -08:00
|
|
|
bail!("{} cannot be variadic", desc);
|
|
|
|
}
|
2018-11-29 17:50:13 -08:00
|
|
|
match (class, js_arguments.len()) {
|
2018-11-30 13:04:05 -08:00
|
|
|
(None, n) if n == amt + 1 => Ok((js_arguments[0].clone(), &js_arguments[1..])),
|
2018-11-08 12:31:39 -08:00
|
|
|
(None, _) => bail!("setters must have {} arguments", amt + 1),
|
2018-11-29 17:50:13 -08:00
|
|
|
(Some(class), n) if n == amt => Ok((class.clone(), &js_arguments[..])),
|
2018-11-08 12:31:39 -08:00
|
|
|
(Some(_), _) => bail!("static setters must have {} arguments", amt),
|
2018-09-06 20:00:38 +03:00
|
|
|
}
|
2018-08-19 13:39:16 +01:00
|
|
|
};
|
2018-11-08 12:31:39 -08:00
|
|
|
|
|
|
|
let mut invoc = match invoc {
|
2018-11-27 12:07:59 -08:00
|
|
|
ImportTarget::Function(f) => handle_variadic(&f, &self.js_arguments)?,
|
2018-11-08 12:31:39 -08:00
|
|
|
ImportTarget::Constructor(c) => {
|
|
|
|
handle_variadic(&format!("new {}", c), &self.js_arguments)?
|
|
|
|
}
|
2018-11-27 12:07:59 -08:00
|
|
|
ImportTarget::Method(f) => handle_variadic(&format!("{}.call", f), &self.js_arguments)?,
|
2018-11-08 12:31:39 -08:00
|
|
|
ImportTarget::StructuralMethod(f) => {
|
|
|
|
let (receiver, args) = match self.js_arguments.split_first() {
|
|
|
|
Some(pair) => pair,
|
|
|
|
None => bail!("methods must have at least one argument"),
|
|
|
|
};
|
|
|
|
handle_variadic(&format!("{}.{}", receiver, f), args)?
|
|
|
|
}
|
|
|
|
ImportTarget::StructuralGetter(class, field) => {
|
|
|
|
let (receiver, _) = fixed("getter", class, 0)?;
|
|
|
|
let expr = format!("{}.{}", receiver, field);
|
|
|
|
self.ret_expr.replace("JS", &expr)
|
|
|
|
}
|
|
|
|
ImportTarget::StructuralSetter(class, field) => {
|
|
|
|
let (receiver, val) = fixed("setter", class, 1)?;
|
|
|
|
let expr = format!("{}.{} = {}", receiver, field, val[0]);
|
|
|
|
self.ret_expr.replace("JS", &expr)
|
|
|
|
}
|
|
|
|
ImportTarget::StructuralIndexingGetter(class) => {
|
|
|
|
let (receiver, field) = fixed("indexing getter", class, 1)?;
|
|
|
|
let expr = format!("{}[{}]", receiver, field[0]);
|
|
|
|
self.ret_expr.replace("JS", &expr)
|
|
|
|
}
|
|
|
|
ImportTarget::StructuralIndexingSetter(class) => {
|
|
|
|
let (receiver, field) = fixed("indexing setter", class, 2)?;
|
|
|
|
let expr = format!("{}[{}] = {}", receiver, field[0], field[1]);
|
|
|
|
self.ret_expr.replace("JS", &expr)
|
|
|
|
}
|
|
|
|
ImportTarget::StructuralIndexingDeleter(class) => {
|
|
|
|
let (receiver, field) = fixed("indexing deleter", class, 1)?;
|
|
|
|
let expr = format!("delete {}[{}]", receiver, field[0]);
|
|
|
|
self.ret_expr.replace("JS", &expr)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-16 13:31:56 -07:00
|
|
|
if self.catch {
|
2018-10-18 08:43:36 -07:00
|
|
|
self.cx.expose_handle_error()?;
|
2018-06-27 22:42:34 -07:00
|
|
|
invoc = format!(
|
|
|
|
"\
|
2018-06-15 23:39:51 -07:00
|
|
|
try {{\n\
|
2018-06-28 20:08:02 -05:00
|
|
|
{}
|
2018-06-15 23:39:51 -07:00
|
|
|
}} catch (e) {{\n\
|
2019-02-04 02:08:08 +01:00
|
|
|
handleError(exnptr, e);\n\
|
2018-06-15 23:39:51 -07:00
|
|
|
}}\
|
|
|
|
",
|
2019-02-04 02:08:08 +01:00
|
|
|
&invoc
|
2018-06-27 22:42:34 -07:00
|
|
|
);
|
2019-01-14 15:59:31 -08:00
|
|
|
} else if self.catch_and_rethrow {
|
|
|
|
invoc = format!(
|
|
|
|
"\
|
|
|
|
try {{\n\
|
|
|
|
{}
|
|
|
|
}} catch (e) {{\n\
|
|
|
|
console.error(\"wasm-bindgen: imported JS function that \
|
|
|
|
was not marked as `catch` threw an error:\", \
|
|
|
|
e);
|
|
|
|
throw e;
|
|
|
|
}}\
|
|
|
|
",
|
|
|
|
&invoc,
|
|
|
|
);
|
|
|
|
}
|
2018-04-16 13:31:56 -07:00
|
|
|
|
|
|
|
if self.finally.len() > 0 {
|
2018-06-27 22:42:34 -07:00
|
|
|
invoc = format!(
|
|
|
|
"\
|
2018-06-15 23:39:51 -07:00
|
|
|
try {{\n\
|
2018-06-28 20:08:02 -05:00
|
|
|
{}
|
2018-06-15 23:39:51 -07:00
|
|
|
}} finally {{\n\
|
2018-06-28 20:08:02 -05:00
|
|
|
{}
|
2018-06-15 23:39:51 -07:00
|
|
|
}}\
|
|
|
|
",
|
2018-06-27 22:42:34 -07:00
|
|
|
&invoc, &self.finally
|
|
|
|
);
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
2018-06-15 12:55:37 -05:00
|
|
|
ret.push_str(&invoc);
|
2018-04-16 13:31:56 -07:00
|
|
|
|
2018-06-15 12:55:37 -05:00
|
|
|
ret.push_str("\n}\n");
|
2018-10-18 08:43:36 -07:00
|
|
|
|
|
|
|
if self.ret_anyref || self.anyref_args.len() > 0 {
|
|
|
|
// Some return values go at the the beginning of the argument list
|
|
|
|
// (they force a return pointer). Handle that here by offsetting all
|
|
|
|
// our arg indices by one, but throw in some sanity checks for if
|
|
|
|
// this ever changes.
|
|
|
|
if let Some(start) = self.shim_arguments.get(0) {
|
|
|
|
if start == "ret" {
|
|
|
|
assert!(!self.ret_anyref);
|
|
|
|
if let Some(next) = self.shim_arguments.get(1) {
|
|
|
|
assert_eq!(next, "arg0");
|
|
|
|
}
|
|
|
|
for (idx, _) in self.anyref_args.iter_mut() {
|
|
|
|
*idx += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert_eq!(start, "arg0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.cx.anyref.import_xform(
|
|
|
|
"__wbindgen_placeholder__",
|
|
|
|
shim,
|
|
|
|
&self.anyref_args,
|
|
|
|
self.ret_anyref,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-03 09:50:26 +01:00
|
|
|
Ok(ret)
|
2018-04-16 13:31:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn global_idx(&mut self) -> usize {
|
|
|
|
let ret = self.global_idx;
|
|
|
|
self.global_idx += 1;
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prelude(&mut self, s: &str) -> &mut Self {
|
|
|
|
for line in s.lines() {
|
|
|
|
self.prelude.push_str(line);
|
|
|
|
self.prelude.push_str("\n");
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finally(&mut self, s: &str) -> &mut Self {
|
|
|
|
for line in s.lines() {
|
|
|
|
self.finally.push_str(line);
|
|
|
|
self.finally.push_str("\n");
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|