Alex Crichton 8e56cdacc5
Rewrite wasm-bindgen with updated interface types proposal (#1882)
This commit is a pretty large scale rewrite of the internals of wasm-bindgen. No user-facing changes are expected as a result of this PR, but due to the scale of changes here it's likely inevitable that at least something will break. I'm hoping to get more testing in though before landing!

The purpose of this PR is to update wasm-bindgen to the current state of the interface types proposal. The wasm-bindgen tool was last updated when it was still called "WebIDL bindings" so it's been awhile! All support is now based on https://github.com/bytecodealliance/wasm-interface-types which defines parsers/binary format/writers/etc for wasm-interface types.

This is a pretty massive PR and unfortunately can't really be split up any more afaik. I don't really expect realistic review of all the code here (or commits), but some high-level changes are:

* Interface types now consists of a set of "adapter functions". The IR in wasm-bindgen is modeled the same way not.
* Each adapter function has a list of instructions, and these instructions work at a higher level than wasm itself, for example with strings.
* The wasm-bindgen tool has a suite of instructions which are specific to it and not present in the standard. (like before with webidl bindings)
* The anyref/multi-value transformations are now greatly simplified. They're simply "optimization passes" over adapter functions, removing instructions that are otherwise present. This way we don't have to juggle so much all over the place, and instructions always have the same meaning.
2019-12-03 11:16:44 -06:00

173 lines
5.6 KiB
Rust

//! Definition of all wasm-bindgen intrinsics.
//!
//! This contains a definition of all intrinsics used by `src/lib.rs` in the
//! wasm-bindgen crate. Each intrinsic listed here is part of an `enum
//! Intrinsic` and is generated through a macro to reduce repetition.
//!
//! Intrinsics in this module currently largely contain their expected symbol
//! name as well as the signature of the function that it expects.
use crate::descriptor::{self, Descriptor, Function};
macro_rules! intrinsics {
(pub enum Intrinsic {
$(
#[symbol = $sym:tt]
#[signature = fn($($arg:expr),*) -> $ret:expr]
$name:ident,
)*
}) => {
/// All wasm-bindgen intrinsics that could be depended on by a wasm
/// module.
#[derive(Debug)]
pub enum Intrinsic {
$($name,)*
}
impl Intrinsic {
/// Returns the corresponding intrinsic for a symbol name, if one
/// matches.
pub fn from_symbol(symbol: &str) -> Option<Intrinsic> {
match symbol {
$($sym => Some(Intrinsic::$name),)*
_ => None,
}
}
/// Returns the expected signature of this intrinsic, used for
/// generating a JS shim.
pub fn signature(&self) -> Function {
use crate::descriptor::Descriptor::*;
match self {
$(
Intrinsic::$name => {
descriptor::Function {
shim_idx: 0,
arguments: vec![$($arg),*],
ret: $ret,
}
}
)*
}
}
/// Returns the symbol name of this intrinsic
pub fn name(&self) -> &'static str {
match self {
$(
Intrinsic::$name => $sym,
)*
}
}
}
};
}
fn ref_anyref() -> Descriptor {
Descriptor::Ref(Box::new(Descriptor::Anyref))
}
fn ref_string() -> Descriptor {
Descriptor::Ref(Box::new(Descriptor::String))
}
fn opt_string() -> Descriptor {
Descriptor::Option(Box::new(Descriptor::String))
}
fn opt_f64() -> Descriptor {
Descriptor::Option(Box::new(Descriptor::F64))
}
intrinsics! {
pub enum Intrinsic {
#[symbol = "__wbindgen_jsval_eq"]
#[signature = fn(ref_anyref(), ref_anyref()) -> Boolean]
JsvalEq,
#[symbol = "__wbindgen_is_function"]
#[signature = fn(ref_anyref()) -> Boolean]
IsFunction,
#[symbol = "__wbindgen_is_undefined"]
#[signature = fn(ref_anyref()) -> Boolean]
IsUndefined,
#[symbol = "__wbindgen_is_null"]
#[signature = fn(ref_anyref()) -> Boolean]
IsNull,
#[symbol = "__wbindgen_is_object"]
#[signature = fn(ref_anyref()) -> Boolean]
IsObject,
#[symbol = "__wbindgen_is_symbol"]
#[signature = fn(ref_anyref()) -> Boolean]
IsSymbol,
#[symbol = "__wbindgen_is_string"]
#[signature = fn(ref_anyref()) -> Boolean]
IsString,
#[symbol = "__wbindgen_is_falsy"]
#[signature = fn(ref_anyref()) -> Boolean]
IsFalsy,
#[symbol = "__wbindgen_object_clone_ref"]
#[signature = fn(ref_anyref()) -> Anyref]
ObjectCloneRef,
#[symbol = "__wbindgen_object_drop_ref"]
#[signature = fn(Anyref) -> Unit]
ObjectDropRef,
#[symbol = "__wbindgen_cb_drop"]
#[signature = fn(Anyref) -> Boolean]
CallbackDrop,
#[symbol = "__wbindgen_cb_forget"]
#[signature = fn(Anyref) -> Unit]
CallbackForget,
#[symbol = "__wbindgen_number_new"]
#[signature = fn(F64) -> Anyref]
NumberNew,
#[symbol = "__wbindgen_string_new"]
#[signature = fn(ref_string()) -> Anyref]
StringNew,
#[symbol = "__wbindgen_symbol_anonymous_new"]
#[signature = fn() -> Anyref]
SymbolAnonymousNew,
#[symbol = "__wbindgen_symbol_named_new"]
#[signature = fn(ref_string()) -> Anyref]
SymbolNamedNew,
#[symbol = "__wbindgen_number_get"]
#[signature = fn(ref_anyref()) -> opt_f64()]
NumberGet,
#[symbol = "__wbindgen_string_get"]
#[signature = fn(ref_anyref()) -> opt_string()]
StringGet,
#[symbol = "__wbindgen_boolean_get"]
#[signature = fn(ref_anyref()) -> I32]
BooleanGet,
#[symbol = "__wbindgen_throw"]
#[signature = fn(ref_string()) -> Unit]
Throw,
#[symbol = "__wbindgen_rethrow"]
#[signature = fn(Anyref) -> Unit]
Rethrow,
#[symbol = "__wbindgen_memory"]
#[signature = fn() -> Anyref]
Memory,
#[symbol = "__wbindgen_module"]
#[signature = fn() -> Anyref]
Module,
#[symbol = "__wbindgen_function_table"]
#[signature = fn() -> Anyref]
FunctionTable,
#[symbol = "__wbindgen_debug_string"]
#[signature = fn(ref_anyref()) -> String]
DebugString,
#[symbol = "__wbindgen_json_parse"]
#[signature = fn(ref_string()) -> Anyref]
JsonParse,
#[symbol = "__wbindgen_json_serialize"]
#[signature = fn(ref_anyref()) -> String]
JsonSerialize,
#[symbol = "__wbindgen_anyref_heap_live_count"]
#[signature = fn() -> I32]
AnyrefHeapLiveCount,
#[symbol = "__wbindgen_init_anyref_table"]
#[signature = fn() -> Unit]
InitAnyrefTable,
}
}