From 172c602db0de57b1fb1f9143d1dedd152995d21d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 18 May 2020 06:25:53 -0700 Subject: [PATCH] Mass rename anyref to externref Updates a mess of dependencies and tracks the most recent version of the reference types specification. --- crates/backend/src/codegen.rs | 2 +- crates/cli-support/Cargo.toml | 10 +- crates/cli-support/src/descriptor.rs | 22 +-- .../src/{anyref.rs => externref.rs} | 104 +++++------ crates/cli-support/src/intrinsic.rs | 64 +++---- crates/cli-support/src/js/binding.rs | 18 +- crates/cli-support/src/js/mod.rs | 34 ++-- crates/cli-support/src/lib.rs | 31 ++-- crates/cli-support/src/throw2unreachable.rs | 2 +- crates/cli-support/src/wit/incoming.rs | 40 ++--- crates/cli-support/src/wit/mod.rs | 30 ++-- crates/cli-support/src/wit/nonstandard.rs | 20 +-- crates/cli-support/src/wit/outgoing.rs | 40 ++--- crates/cli-support/src/wit/section.rs | 16 +- crates/cli-support/src/wit/standard.rs | 70 ++++---- crates/cli/Cargo.toml | 12 +- crates/cli/tests/interface-types/anyref.wat | 14 +- crates/cli/tests/reference.rs | 4 +- crates/cli/tests/reference/anyref-empty.js | 2 +- crates/cli/tests/reference/anyref-empty.rs | 2 +- crates/cli/tests/reference/anyref-empty.wat | 2 +- .../tests/reference/anyref-import-catch.js | 57 +++--- .../tests/reference/anyref-import-catch.rs | 2 +- .../tests/reference/anyref-import-catch.wat | 13 +- crates/cli/tests/reference/anyref-nop.js | 2 +- crates/cli/tests/reference/anyref-nop.rs | 2 +- crates/cli/tests/reference/anyref-nop.wat | 2 +- .../tests/reference/interface-types-anyref.rs | 2 +- .../reference/interface-types-anyref.wat | 10 +- .../Cargo.toml | 12 +- .../LICENSE-APACHE | 0 .../LICENSE-MIT | 0 .../src/lib.rs | 163 +++++++++--------- .../tests/all.rs | 26 +-- .../tests/anyref-param-owned.wat | 14 +- .../tests/anyref-param.wat | 16 +- .../tests/clone-ref-intrinsic.wat | 14 +- .../tests/drop-ref-intrinsic.wat | 14 +- .../tests/import-anyref-owned.wat | 14 +- .../tests/import-anyref-ret.wat | 16 +- .../tests/import-anyref.wat | 14 +- .../tests/mixed-export.wat | 16 +- .../tests/mixed.wat | 14 +- .../tests/ret-anyref.wat | 14 +- .../tests/table-grow-intrinsic.wat | 18 +- .../tests/table-set-null-intrinsic.wat | 18 +- .../tests/table.wat | 14 +- crates/multi-value-xform/Cargo.toml | 4 +- crates/multi-value-xform/src/lib.rs | 12 +- crates/threads-xform/Cargo.toml | 2 +- crates/threads-xform/src/lib.rs | 2 +- crates/wasm-conventions/Cargo.toml | 2 +- crates/wasm-interpreter/Cargo.toml | 2 +- crates/webidl/src/idl_type.rs | 8 +- publish.rs | 2 +- src/closure.rs | 49 +++--- src/describe.rs | 6 +- src/{anyref.rs => externref.rs} | 32 ++-- src/lib.rs | 23 ++- tests/headless/anyref_heap_live_count.rs | 20 --- tests/headless/externref_heap_live_count.rs | 20 +++ tests/headless/main.rs | 2 +- tests/wasm/no_shims.rs | 8 +- tests/wasm/simple.rs | 8 +- 64 files changed, 610 insertions(+), 618 deletions(-) rename crates/cli-support/src/{anyref.rs => externref.rs} (82%) rename crates/{anyref-xform => externref-xform}/Cargo.toml (64%) rename crates/{anyref-xform => externref-xform}/LICENSE-APACHE (100%) rename crates/{anyref-xform => externref-xform}/LICENSE-MIT (100%) rename crates/{anyref-xform => externref-xform}/src/lib.rs (83%) rename crates/{anyref-xform => externref-xform}/tests/all.rs (92%) rename crates/{anyref-xform => externref-xform}/tests/anyref-param-owned.wat (51%) rename crates/{anyref-xform => externref-xform}/tests/anyref-param.wat (56%) rename crates/{anyref-xform => externref-xform}/tests/clone-ref-intrinsic.wat (70%) rename crates/{anyref-xform => externref-xform}/tests/drop-ref-intrinsic.wat (62%) rename crates/{anyref-xform => externref-xform}/tests/import-anyref-owned.wat (57%) rename crates/{anyref-xform => externref-xform}/tests/import-anyref-ret.wat (56%) rename crates/{anyref-xform => externref-xform}/tests/import-anyref.wat (52%) rename crates/{anyref-xform => externref-xform}/tests/mixed-export.wat (62%) rename crates/{anyref-xform => externref-xform}/tests/mixed.wat (64%) rename crates/{anyref-xform => externref-xform}/tests/ret-anyref.wat (51%) rename crates/{anyref-xform => externref-xform}/tests/table-grow-intrinsic.wat (52%) rename crates/{anyref-xform => externref-xform}/tests/table-set-null-intrinsic.wat (51%) rename crates/{anyref-xform => externref-xform}/tests/table.wat (58%) rename src/{anyref.rs => externref.rs} (84%) delete mode 100644 tests/headless/anyref_heap_live_count.rs create mode 100644 tests/headless/externref_heap_live_count.rs diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index ef1ccc1f97c..cd8b7d77746 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -580,7 +580,7 @@ impl ToTokens for ast::ImportType { let typescript_type_chars = typescript_type.chars().map(|c| c as u32); quote! { use wasm_bindgen::describe::*; - inform(NAMED_ANYREF); + inform(NAMED_EXTERNREF); inform(#typescript_type_len); #(inform(#typescript_type_chars);)* } diff --git a/crates/cli-support/Cargo.toml b/crates/cli-support/Cargo.toml index acc708293f5..85525aae312 100644 --- a/crates/cli-support/Cargo.toml +++ b/crates/cli-support/Cargo.toml @@ -18,13 +18,13 @@ log = "0.4" rustc-demangle = "0.1.13" serde_json = "1.0" tempfile = "3.0" -walrus = "0.16.1" -wasm-bindgen-anyref-xform = { path = '../anyref-xform', version = '=0.2.62' } +walrus = "0.17.0" +wasm-bindgen-externref-xform = { path = '../externref-xform', version = '=0.2.62' } wasm-bindgen-multi-value-xform = { path = '../multi-value-xform', version = '=0.2.62' } wasm-bindgen-shared = { path = "../shared", version = '=0.2.62' } wasm-bindgen-threads-xform = { path = '../threads-xform', version = '=0.2.62' } wasm-bindgen-wasm-conventions = { path = '../wasm-conventions', version = '=0.2.62' } wasm-bindgen-wasm-interpreter = { path = "../wasm-interpreter", version = '=0.2.62' } -wit-text = "0.6.0" -wit-walrus = "0.2.0" -wit-validator = "0.1.0" +wit-text = "0.7.0" +wit-walrus = "0.4.0" +wit-validator = "0.2.0" diff --git a/crates/cli-support/src/descriptor.rs b/crates/cli-support/src/descriptor.rs index 2d1f151b389..d5c4dd7f07a 100644 --- a/crates/cli-support/src/descriptor.rs +++ b/crates/cli-support/src/descriptor.rs @@ -30,8 +30,8 @@ tys! { REFMUT SLICE VECTOR - ANYREF - NAMED_ANYREF + EXTERNREF + NAMED_EXTERNREF ENUM RUST_STRUCT CHAR @@ -62,8 +62,8 @@ pub enum Descriptor { Vector(Box), CachedString, String, - Anyref, - NamedAnyref(String), + Externref, + NamedExternref(String), Enum { hole: u32 }, RustStruct(String), Char, @@ -100,7 +100,7 @@ pub enum VectorKind { F32, F64, String, - Anyref, + Externref, } impl Descriptor { @@ -133,15 +133,15 @@ impl Descriptor { OPTIONAL => Descriptor::Option(Box::new(Descriptor::_decode(data, clamped))), CACHED_STRING => Descriptor::CachedString, STRING => Descriptor::String, - ANYREF => Descriptor::Anyref, + EXTERNREF => Descriptor::Externref, ENUM => Descriptor::Enum { hole: get(data) }, RUST_STRUCT => { let name = get_string(data); Descriptor::RustStruct(name) } - NAMED_ANYREF => { + NAMED_EXTERNREF => { let name = get_string(data); - Descriptor::NamedAnyref(name) + Descriptor::NamedExternref(name) } CHAR => Descriptor::Char, UNIT => Descriptor::Unit, @@ -192,7 +192,7 @@ impl Descriptor { Descriptor::U64 => Some(VectorKind::U64), Descriptor::F32 => Some(VectorKind::F32), Descriptor::F64 => Some(VectorKind::F64), - Descriptor::Anyref => Some(VectorKind::Anyref), + Descriptor::Externref => Some(VectorKind::Externref), _ => None, } } @@ -254,7 +254,7 @@ impl VectorKind { VectorKind::U64 => "BigUint64Array", VectorKind::F32 => "Float32Array", VectorKind::F64 => "Float64Array", - VectorKind::Anyref => "any[]", + VectorKind::Externref => "any[]", } } @@ -272,7 +272,7 @@ impl VectorKind { VectorKind::U64 => 8, VectorKind::F32 => 4, VectorKind::F64 => 8, - VectorKind::Anyref => 4, + VectorKind::Externref => 4, } } } diff --git a/crates/cli-support/src/anyref.rs b/crates/cli-support/src/externref.rs similarity index 82% rename from crates/cli-support/src/anyref.rs rename to crates/cli-support/src/externref.rs index 73f61a33217..5c8a90e8996 100644 --- a/crates/cli-support/src/anyref.rs +++ b/crates/cli-support/src/externref.rs @@ -6,7 +6,7 @@ use crate::wit::{AdapterType, InstructionData, StackChange, WasmBindgenAux}; use anyhow::Result; use std::collections::HashMap; use walrus::{ir::Value, ElementKind, InitExpr, Module}; -use wasm_bindgen_anyref_xform::Context; +use wasm_bindgen_externref_xform::Context; pub fn process(module: &mut Module) -> Result<()> { let mut cfg = Context::default(); @@ -61,20 +61,20 @@ pub fn process(module: &mut Module) -> Result<()> { // them in the aux section to get used. // // FIXME: this is not great, we should ideally have precise tracking of what - // requires what. These are used by catch clauses and anyref slices going - // in/out of wasm. The catch clauses are a bit weird but anyref slices + // requires what. These are used by catch clauses and externref slices going + // in/out of wasm. The catch clauses are a bit weird but externref slices // should ideally track in their own instructions what table/functions // they're referencing. This doesn't fit well in today's model of // slice-related instructions, though, so let's just cop out and only enable // these coarsely. - aux.anyref_table = Some(meta.table); - if module_needs_anyref_metadata(&aux, section) { - aux.anyref_alloc = meta.alloc; - aux.anyref_drop_slice = meta.drop_slice; + aux.externref_table = Some(meta.table); + if module_needs_externref_metadata(&aux, section) { + aux.externref_alloc = meta.alloc; + aux.externref_drop_slice = meta.drop_slice; } // Additonally we may need to update some adapter instructions other than - // those found for the anyref pass. These are some general "fringe support" + // those found for the externref pass. These are some general "fringe support" // things necessary to get absolutely everything working. for (_, adapter) in section.adapters.iter_mut() { let instrs = match &mut adapter.kind { @@ -95,15 +95,15 @@ pub fn process(module: &mut Module) -> Result<()> { None => continue, }; match import { - AuxImport::Intrinsic(Intrinsic::AnyrefHeapLiveCount) => {} + AuxImport::Intrinsic(Intrinsic::ExternrefHeapLiveCount) => {} _ => continue, } instr.instr = Instruction::Standard(wit_walrus::Instruction::CallCore(id)); } - // Optional anyref values are now managed in the wasm module, so + // Optional externref values are now managed in the wasm module, so // we need to store where they're managed. - Instruction::I32FromOptionAnyref { + Instruction::I32FromOptionExternref { ref mut table_and_alloc, } => { *table_and_alloc = meta.alloc.map(|id| (meta.table, id)); @@ -146,7 +146,7 @@ enum Export { /// Adapts the `instrs` given which are an implementation of the import of `id`. /// /// This function will pattern match outgoing arguments and update the -/// instruction stream to remove any anyref-management instructions since +/// instruction stream to remove any externref-management instructions since /// we'll be sinking those into the WebAssembly module. fn import_xform( cx: &mut Context, @@ -157,8 +157,8 @@ fn import_xform( ) { struct Arg { idx: usize, - // Some(false) for a borrowed anyref, Some(true) for an owned one - anyref: Option, + // Some(false) for a borrowed externref, Some(true) for an owned one + externref: Option, } let mut to_delete = Vec::new(); @@ -167,7 +167,7 @@ fn import_xform( while let Some((i, instr)) = iter.next() { match instr.instr { Instruction::CallAdapter(_) => break, - Instruction::AnyrefLoadOwned | Instruction::TableGet => { + Instruction::ExternrefLoadOwned | Instruction::TableGet => { let owned = match instr.instr { Instruction::TableGet => false, _ => true, @@ -176,19 +176,19 @@ fn import_xform( Some(arg) => arg, None => panic!("previous instruction must be `arg.get`"), }; - arg.anyref = Some(owned); + arg.externref = Some(owned); match params[arg.idx] { AdapterType::I32 => {} _ => panic!("must be `i32` type"), } - params[arg.idx] = AdapterType::Anyref; + params[arg.idx] = AdapterType::Externref; args.push(Some(arg)); to_delete.push(i); } Instruction::Standard(wit_walrus::Instruction::ArgGet(n)) => { args.push(Some(Arg { idx: n as usize, - anyref: None, + externref: None, })); } _ => match instr.stack_change { @@ -207,46 +207,46 @@ fn import_xform( } } - let mut ret_anyref = false; + let mut ret_externref = false; while let Some((i, instr)) = iter.next() { match instr.instr { - Instruction::I32FromAnyrefOwned => { + Instruction::I32FromExternrefOwned => { assert_eq!(results.len(), 1); match results[0] { AdapterType::I32 => {} _ => panic!("must be `i32` type"), } - results[0] = AdapterType::Anyref; - ret_anyref = true; + results[0] = AdapterType::Externref; + ret_externref = true; to_delete.push(i); } _ => {} } } - // Delete all unnecessary anyref management insructions + // Delete all unnecessary externref management insructions for idx in to_delete.into_iter().rev() { instrs.remove(idx); } - // Filter down our list of arguments to just the ones that are anyref + // Filter down our list of arguments to just the ones that are externref // values. let args = args .iter() .filter_map(|arg| arg.as_ref()) - .filter_map(|arg| arg.anyref.map(|owned| (arg.idx, owned))) + .filter_map(|arg| arg.externref.map(|owned| (arg.idx, owned))) .collect::>(); - // ... and register this entire transformation with the anyref + // ... and register this entire transformation with the externref // transformation pass. - cx.import_xform(id, &args, ret_anyref); + cx.import_xform(id, &args, ret_externref); } /// Adapts the `instrs` of an adapter function that calls an export. /// /// The `instrs` must be generated by wasm-bindgen itself and follow the -/// pattern matched below to pass off to the anyref transformation pass. The -/// signature of the adapter doesn't change (it remains as anyref-aware) but the +/// pattern matched below to pass off to the externref transformation pass. The +/// signature of the adapter doesn't change (it remains as externref-aware) but the /// signature of the export we're calling will change during the transformation. fn export_xform(cx: &mut Context, export: Export, instrs: &mut Vec) { let mut to_delete = Vec::new(); @@ -255,20 +255,20 @@ fn export_xform(cx: &mut Context, export: Export, instrs: &mut Vec break, - Instruction::I32FromAnyrefOwned => { + Instruction::I32FromExternrefOwned => { args.pop(); args.push(Some(true)); to_delete.push(i); } - Instruction::I32FromAnyrefBorrow => { + Instruction::I32FromExternrefBorrow => { args.pop(); args.push(Some(false)); to_delete.push(i); @@ -289,21 +289,21 @@ fn export_xform(cx: &mut Context, export: Export, instrs: &mut Vec { - ret_anyref = true; + Instruction::ExternrefLoadOwned => { + ret_externref = true; to_delete.push(i); } _ => {} } } - // Filter down our list of arguments to just the ones that are anyref + // Filter down our list of arguments to just the ones that are externref // values. let args = args .iter() @@ -311,20 +311,20 @@ fn export_xform(cx: &mut Context, export: Export, instrs: &mut Vec>(); - // ... and register this entire transformation with the anyref + // ... and register this entire transformation with the externref // transformation pass. match export { Export::Export(id) => { - cx.export_xform(id, &args, ret_anyref); + cx.export_xform(id, &args, ret_externref); } Export::TableElement { idx, call_idx } => { - if let Some(new_idx) = cx.table_element_xform(idx, &args, ret_anyref) { + if let Some(new_idx) = cx.table_element_xform(idx, &args, ret_externref) { instrs[call_idx].instr = Instruction::CallTableElement(new_idx); } } } - // Delete all unnecessary anyref management instructions. We're going to + // Delete all unnecessary externref management instructions. We're going to // sink these instructions into the wasm module itself. for idx in to_delete.into_iter().rev() { instrs.remove(idx); @@ -332,7 +332,7 @@ fn export_xform(cx: &mut Context, export: Export, instrs: &mut Vec bool { +fn module_needs_externref_metadata(aux: &WasmBindgenAux, section: &NonstandardWitSection) -> bool { use Instruction::*; // our `handleError` intrinsic uses a few pieces of metadata to store @@ -341,7 +341,7 @@ fn module_needs_anyref_metadata(aux: &WasmBindgenAux, section: &NonstandardWitSe return true; } - // Look for any instructions which may use `VectorKind::Anyref`. If there + // Look for any instructions which may use `VectorKind::Externref`. If there // are any then we'll need our intrinsics/tables/etc, otherwise we shouldn't // ever need them. section.adapters.iter().any(|(_, adapter)| { @@ -351,31 +351,31 @@ fn module_needs_anyref_metadata(aux: &WasmBindgenAux, section: &NonstandardWitSe }; instructions.iter().any(|instr| match instr.instr { VectorToMemory { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } | MutableSliceToMemory { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } | OptionVector { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } | VectorLoad { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } | OptionVectorLoad { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } | View { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } | OptionView { - kind: VectorKind::Anyref, + kind: VectorKind::Externref, .. } => true, _ => false, diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index d7db4fc94aa..f407f8fc12d 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -63,8 +63,8 @@ macro_rules! intrinsics { }; } -fn ref_anyref() -> Descriptor { - Descriptor::Ref(Box::new(Descriptor::Anyref)) +fn ref_externref() -> Descriptor { + Descriptor::Ref(Box::new(Descriptor::Externref)) } fn ref_string() -> Descriptor { @@ -82,91 +82,91 @@ fn opt_f64() -> Descriptor { intrinsics! { pub enum Intrinsic { #[symbol = "__wbindgen_jsval_eq"] - #[signature = fn(ref_anyref(), ref_anyref()) -> Boolean] + #[signature = fn(ref_externref(), ref_externref()) -> Boolean] JsvalEq, #[symbol = "__wbindgen_is_function"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsFunction, #[symbol = "__wbindgen_is_undefined"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsUndefined, #[symbol = "__wbindgen_is_null"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsNull, #[symbol = "__wbindgen_is_object"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsObject, #[symbol = "__wbindgen_is_symbol"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsSymbol, #[symbol = "__wbindgen_is_string"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsString, #[symbol = "__wbindgen_is_falsy"] - #[signature = fn(ref_anyref()) -> Boolean] + #[signature = fn(ref_externref()) -> Boolean] IsFalsy, #[symbol = "__wbindgen_object_clone_ref"] - #[signature = fn(ref_anyref()) -> Anyref] + #[signature = fn(ref_externref()) -> Externref] ObjectCloneRef, #[symbol = "__wbindgen_object_drop_ref"] - #[signature = fn(Anyref) -> Unit] + #[signature = fn(Externref) -> Unit] ObjectDropRef, #[symbol = "__wbindgen_cb_drop"] - #[signature = fn(Anyref) -> Boolean] + #[signature = fn(Externref) -> Boolean] CallbackDrop, #[symbol = "__wbindgen_cb_forget"] - #[signature = fn(Anyref) -> Unit] + #[signature = fn(Externref) -> Unit] CallbackForget, #[symbol = "__wbindgen_number_new"] - #[signature = fn(F64) -> Anyref] + #[signature = fn(F64) -> Externref] NumberNew, #[symbol = "__wbindgen_string_new"] - #[signature = fn(ref_string()) -> Anyref] + #[signature = fn(ref_string()) -> Externref] StringNew, #[symbol = "__wbindgen_symbol_anonymous_new"] - #[signature = fn() -> Anyref] + #[signature = fn() -> Externref] SymbolAnonymousNew, #[symbol = "__wbindgen_symbol_named_new"] - #[signature = fn(ref_string()) -> Anyref] + #[signature = fn(ref_string()) -> Externref] SymbolNamedNew, #[symbol = "__wbindgen_number_get"] - #[signature = fn(ref_anyref()) -> opt_f64()] + #[signature = fn(ref_externref()) -> opt_f64()] NumberGet, #[symbol = "__wbindgen_string_get"] - #[signature = fn(ref_anyref()) -> opt_string()] + #[signature = fn(ref_externref()) -> opt_string()] StringGet, #[symbol = "__wbindgen_boolean_get"] - #[signature = fn(ref_anyref()) -> I32] + #[signature = fn(ref_externref()) -> I32] BooleanGet, #[symbol = "__wbindgen_throw"] #[signature = fn(ref_string()) -> Unit] Throw, #[symbol = "__wbindgen_rethrow"] - #[signature = fn(Anyref) -> Unit] + #[signature = fn(Externref) -> Unit] Rethrow, #[symbol = "__wbindgen_memory"] - #[signature = fn() -> Anyref] + #[signature = fn() -> Externref] Memory, #[symbol = "__wbindgen_module"] - #[signature = fn() -> Anyref] + #[signature = fn() -> Externref] Module, #[symbol = "__wbindgen_function_table"] - #[signature = fn() -> Anyref] + #[signature = fn() -> Externref] FunctionTable, #[symbol = "__wbindgen_debug_string"] - #[signature = fn(ref_anyref()) -> String] + #[signature = fn(ref_externref()) -> String] DebugString, #[symbol = "__wbindgen_json_parse"] - #[signature = fn(ref_string()) -> Anyref] + #[signature = fn(ref_string()) -> Externref] JsonParse, #[symbol = "__wbindgen_json_serialize"] - #[signature = fn(ref_anyref()) -> String] + #[signature = fn(ref_externref()) -> String] JsonSerialize, - #[symbol = "__wbindgen_anyref_heap_live_count"] + #[symbol = "__wbindgen_externref_heap_live_count"] #[signature = fn() -> I32] - AnyrefHeapLiveCount, - #[symbol = "__wbindgen_init_anyref_table"] + ExternrefHeapLiveCount, + #[symbol = "__wbindgen_init_externref_table"] #[signature = fn() -> Unit] - InitAnyrefTable, + InitExternrefTable, } } diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index f08e4f48daa..8a86bd1282b 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -616,13 +616,13 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> js.push(format!("{}.codePointAt(0)", val)); } - Instruction::I32FromAnyrefOwned => { + Instruction::I32FromExternrefOwned => { js.cx.expose_add_heap_object(); let val = js.pop(); js.push(format!("addHeapObject({})", val)); } - Instruction::I32FromAnyrefBorrow => { + Instruction::I32FromExternrefBorrow => { js.cx.expose_borrowed_objects(); js.cx.expose_global_stack_pointer(); let val = js.pop(); @@ -630,7 +630,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> js.finally("heap[stack_pointer++] = undefined;"); } - Instruction::I32FromAnyrefRustOwned { class } => { + Instruction::I32FromExternrefRustOwned { class } => { let val = js.pop(); js.assert_class(&val, &class); js.assert_not_moved(&val); @@ -640,7 +640,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> js.push(format!("ptr{}", i)); } - Instruction::I32FromAnyrefRustBorrow { class } => { + Instruction::I32FromExternrefRustBorrow { class } => { let val = js.pop(); js.assert_class(&val, &class); js.assert_not_moved(&val); @@ -707,12 +707,12 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> js.push(format!("high{}", i)); } - Instruction::I32FromOptionAnyref { table_and_alloc } => { + Instruction::I32FromOptionExternref { table_and_alloc } => { let val = js.pop(); js.cx.expose_is_like_none(); match table_and_alloc { Some((table, alloc)) => { - let alloc = js.cx.expose_add_to_anyref_table(*table, *alloc)?; + let alloc = js.cx.expose_add_to_externref_table(*table, *alloc)?; js.push(format!("isLikeNone({0}) ? 0 : {1}({0})", val, alloc)); } None => { @@ -869,7 +869,7 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) -> js.push(format!("{} !== 0", val)); } - Instruction::AnyrefLoadOwned => { + Instruction::ExternrefLoadOwned => { js.cx.expose_take_object(); let val = js.pop(); js.push(format!("takeObject({})", val)); @@ -1224,14 +1224,14 @@ fn adapter2ts(ty: &AdapterType, dst: &mut String) { | AdapterType::F64 => dst.push_str("number"), AdapterType::I64 | AdapterType::S64 | AdapterType::U64 => dst.push_str("BigInt"), AdapterType::String => dst.push_str("string"), - AdapterType::Anyref => dst.push_str("any"), + AdapterType::Externref => dst.push_str("any"), AdapterType::Bool => dst.push_str("boolean"), AdapterType::Vector(kind) => dst.push_str(kind.js_ty()), AdapterType::Option(ty) => { adapter2ts(ty, dst); dst.push_str(" | undefined"); } - AdapterType::NamedAnyref(name) => dst.push_str(name), + AdapterType::NamedExternref(name) => dst.push_str(name), AdapterType::Struct(name) => dst.push_str(name), AdapterType::Function => dst.push_str("any"), } diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index afd5dfe9855..f5030a642e8 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -858,7 +858,7 @@ impl<'a> Context<'a> { if !self.should_write_global("heap") { return; } - assert!(!self.config.anyref); + assert!(!self.config.externref); self.global(&format!( "const heap = new Array({}).fill(undefined);", INITIAL_HEAP_OFFSET @@ -1143,11 +1143,11 @@ impl<'a> Context<'a> { return Ok(ret); } self.expose_wasm_vector_len(); - match (self.aux.anyref_table, self.aux.anyref_alloc) { + match (self.aux.externref_table, self.aux.externref_alloc) { (Some(table), Some(alloc)) => { - // TODO: using `addToAnyrefTable` goes back and forth between wasm + // TODO: using `addToExternrefTable` goes back and forth between wasm // and JS a lot, we should have a bulk operation for this. - let add = self.expose_add_to_anyref_table(table, alloc)?; + let add = self.expose_add_to_externref_table(table, alloc)?; self.global(&format!( " function {}(array, malloc) {{ @@ -1341,7 +1341,7 @@ impl<'a> Context<'a> { if !self.should_write_global(ret.to_string()) { return Ok(ret); } - match (self.aux.anyref_table, self.aux.anyref_drop_slice) { + match (self.aux.externref_table, self.aux.externref_drop_slice) { (Some(table), Some(drop)) => { let table = self.export_name_of(table); let drop = self.export_name_of(drop); @@ -1520,7 +1520,7 @@ impl<'a> Context<'a> { VectorKind::U64 => self.expose_uint64_memory(memory), VectorKind::F32 => self.expose_f32_memory(memory), VectorKind::F64 => self.expose_f64_memory(memory), - VectorKind::Anyref => self.expose_uint32_memory(memory), + VectorKind::Externref => self.expose_uint32_memory(memory), } } @@ -1665,9 +1665,9 @@ impl<'a> Context<'a> { .exn_store .ok_or_else(|| anyhow!("failed to find `__wbindgen_exn_store` intrinsic"))?; let store = self.export_name_of(store); - match (self.aux.anyref_table, self.aux.anyref_alloc) { + match (self.aux.externref_table, self.aux.externref_alloc) { (Some(table), Some(alloc)) => { - let add = self.expose_add_to_anyref_table(table, alloc)?; + let add = self.expose_add_to_externref_table(table, alloc)?; self.global(&format!( " function handleError(f) {{ @@ -1750,7 +1750,7 @@ impl<'a> Context<'a> { VectorKind::I64 | VectorKind::U64 => self.expose_pass_array64_to_wasm(memory), VectorKind::F32 => self.expose_pass_array_f32_to_wasm(memory), VectorKind::F64 => self.expose_pass_array_f64_to_wasm(memory), - VectorKind::Anyref => self.expose_pass_array_jsvalue_to_wasm(memory), + VectorKind::Externref => self.expose_pass_array_jsvalue_to_wasm(memory), } } @@ -1772,7 +1772,7 @@ impl<'a> Context<'a> { VectorKind::U64 => self.expose_get_array_u64_from_wasm(memory), VectorKind::F32 => self.expose_get_array_f32_from_wasm(memory), VectorKind::F64 => self.expose_get_array_f64_from_wasm(memory), - VectorKind::Anyref => self.expose_get_array_js_value_from_wasm(memory)?, + VectorKind::Externref => self.expose_get_array_js_value_from_wasm(memory)?, }) } @@ -2052,13 +2052,13 @@ impl<'a> Context<'a> { true } - fn expose_add_to_anyref_table( + fn expose_add_to_externref_table( &mut self, table: TableId, alloc: FunctionId, ) -> Result { - let view = self.memview_table("addToAnyrefTable", table); - assert!(self.config.anyref); + let view = self.memview_table("addToExternrefTable", table); + assert!(self.config.externref); if !self.should_write_global(view.to_string()) { return Ok(view); } @@ -2921,7 +2921,7 @@ impl<'a> Context<'a> { "JSON.stringify(obj === undefined ? null : obj)".to_string() } - Intrinsic::AnyrefHeapLiveCount => { + Intrinsic::ExternrefHeapLiveCount => { assert_eq!(args.len(), 0); self.expose_global_heap(); prelude.push_str( @@ -2941,11 +2941,11 @@ impl<'a> Context<'a> { ) } - Intrinsic::InitAnyrefTable => { + Intrinsic::InitExternrefTable => { let table = self .aux - .anyref_table - .ok_or_else(|| anyhow!("must enable anyref to use anyref intrinsic"))?; + .externref_table + .ok_or_else(|| anyhow!("must enable externref to use externref intrinsic"))?; let name = self.export_name_of(table); // Grow the table to insert our initial values, and then also // set the 0th slot to `undefined` since that's what we've diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index faa37abf736..dc45ebf55f0 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -11,10 +11,10 @@ use walrus::Module; pub(crate) const PLACEHOLDER_MODULE: &str = "__wbindgen_placeholder__"; -mod anyref; mod decode; mod descriptor; mod descriptors; +mod externref; mod intrinsic; mod js; mod multivalue; @@ -40,7 +40,7 @@ pub struct Bindgen { // Support for the wasm threads proposal, transforms the wasm module to be // "ready to be instantiated on any thread" threads: wasm_bindgen_threads_xform::Config, - anyref: bool, + externref: bool, multi_value: bool, wasm_interface_types: bool, encode_into: EncodeInto, @@ -90,7 +90,8 @@ pub enum EncodeInto { impl Bindgen { pub fn new() -> Bindgen { - let anyref = env::var("WASM_BINDGEN_ANYREF").is_ok(); + let externref = env::var("WASM_BINDGEN_externref").is_ok() + || env::var("WASM_BINDGEN_EXTERNREF").is_ok(); let wasm_interface_types = env::var("WASM_INTERFACE_TYPES").is_ok(); let multi_value = env::var("WASM_BINDGEN_MULTI_VALUE").is_ok(); Bindgen { @@ -109,7 +110,7 @@ impl Bindgen { emit_start: true, weak_refs: env::var("WASM_BINDGEN_WEAKREF").is_ok(), threads: threads_config(), - anyref: anyref || wasm_interface_types, + externref: externref || wasm_interface_types, multi_value: multi_value || wasm_interface_types, wasm_interface_types, encode_into: EncodeInto::Test, @@ -347,37 +348,37 @@ impl Bindgen { // interface types. wit::process( &mut module, - self.anyref, + self.externref, self.wasm_interface_types, self.emit_start, )?; // Now that we've got type information from the webidl processing pass, - // touch up the output of rustc to insert anyref shims where necessary. - // This is only done if the anyref pass is enabled, which it's - // currently off-by-default since `anyref` is still in development in + // touch up the output of rustc to insert externref shims where necessary. + // This is only done if the externref pass is enabled, which it's + // currently off-by-default since `externref` is still in development in // engines. // - // If the anyref pass isn't necessary, then we blanket delete the - // export of all our anyref intrinsics which will get cleaned up in the + // If the externref pass isn't necessary, then we blanket delete the + // export of all our externref intrinsics which will get cleaned up in the // GC pass before JS generation. - if self.anyref { - anyref::process(&mut module)?; + if self.externref { + externref::process(&mut module)?; } else { let ids = module .exports .iter() - .filter(|e| e.name.starts_with("__anyref")) + .filter(|e| e.name.starts_with("__externref")) .map(|e| e.id()) .collect::>(); for id in ids { module.exports.delete(id); } // Clean up element segments as well if they have holes in them - // after some of our transformations, because non-anyref engines + // after some of our transformations, because non-externref engines // only support contiguous arrays of function references in element // segments. - anyref::force_contiguous_elements(&mut module)?; + externref::force_contiguous_elements(&mut module)?; } // If wasm interface types are enabled then the `__wbindgen_throw` diff --git a/crates/cli-support/src/throw2unreachable.rs b/crates/cli-support/src/throw2unreachable.rs index 1358806a27f..6050911b482 100644 --- a/crates/cli-support/src/throw2unreachable.rs +++ b/crates/cli-support/src/throw2unreachable.rs @@ -9,7 +9,7 @@ use walrus::Module; /// /// This pass is executed as part of the wasm interface types support. This is /// done to support debug mode executables with wasm interface types. Debug mode -/// executables will use malloc as well as anyref intrinsics. These intrinsics +/// executables will use malloc as well as externref intrinsics. These intrinsics /// internally, when they fail, abort the instance. This abort is done through /// the `__wbindgen_throw` intrinsic in debug mode to provide a hopefully /// useful error message. In release mode it's simply an `unreachable` diff --git a/crates/cli-support/src/wit/incoming.rs b/crates/cli-support/src/wit/incoming.rs index 515027bb019..05a7d757ba4 100644 --- a/crates/cli-support/src/wit/incoming.rs +++ b/crates/cli-support/src/wit/incoming.rs @@ -60,24 +60,24 @@ impl InstructionBuilder<'_, '_> { &[AdapterType::I32], ); } - Descriptor::Anyref => { + Descriptor::Externref => { self.instruction( - &[AdapterType::Anyref], - Instruction::I32FromAnyrefOwned, + &[AdapterType::Externref], + Instruction::I32FromExternrefOwned, &[AdapterType::I32], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( - &[AdapterType::NamedAnyref(name.clone())], - Instruction::I32FromAnyrefOwned, + &[AdapterType::NamedExternref(name.clone())], + Instruction::I32FromExternrefOwned, &[AdapterType::I32] ) } Descriptor::RustStruct(class) => { self.instruction( &[AdapterType::Struct(class.clone())], - Instruction::I32FromAnyrefRustOwned { + Instruction::I32FromExternrefRustOwned { class: class.clone(), }, &[AdapterType::I32], @@ -155,23 +155,23 @@ impl InstructionBuilder<'_, '_> { Descriptor::RustStruct(class) => { self.instruction( &[AdapterType::Struct(class.clone())], - Instruction::I32FromAnyrefRustBorrow { + Instruction::I32FromExternrefRustBorrow { class: class.clone(), }, &[AdapterType::I32], ); } - Descriptor::Anyref => { + Descriptor::Externref => { self.instruction( - &[AdapterType::Anyref], - Instruction::I32FromAnyrefBorrow, + &[AdapterType::Externref], + Instruction::I32FromExternrefBorrow, &[AdapterType::I32], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( - &[AdapterType::NamedAnyref(name.clone())], - Instruction::I32FromAnyrefBorrow, + &[AdapterType::NamedExternref(name.clone())], + Instruction::I32FromExternrefBorrow, &[AdapterType::I32], ); } @@ -229,19 +229,19 @@ impl InstructionBuilder<'_, '_> { fn incoming_option(&mut self, arg: &Descriptor) -> Result<(), Error> { match arg { - Descriptor::Anyref => { + Descriptor::Externref => { self.instruction( - &[AdapterType::Anyref.option()], - Instruction::I32FromOptionAnyref { + &[AdapterType::Externref.option()], + Instruction::I32FromOptionExternref { table_and_alloc: None, }, &[AdapterType::I32], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( - &[AdapterType::NamedAnyref(name.clone()).option()], - Instruction::I32FromOptionAnyref { + &[AdapterType::NamedExternref(name.clone()).option()], + Instruction::I32FromOptionExternref { table_and_alloc: None, }, &[AdapterType::I32], diff --git a/crates/cli-support/src/wit/mod.rs b/crates/cli-support/src/wit/mod.rs index d7b4c930b41..ce82efdf13d 100644 --- a/crates/cli-support/src/wit/mod.rs +++ b/crates/cli-support/src/wit/mod.rs @@ -28,7 +28,7 @@ struct Context<'a> { vendor_prefixes: HashMap>, unique_crate_identifier: &'a str, descriptors: HashMap, - anyref_enabled: bool, + externref_enabled: bool, wasm_interface_types: bool, support_start: bool, } @@ -43,7 +43,7 @@ struct InstructionBuilder<'a, 'b> { pub fn process( module: &mut Module, - anyref_enabled: bool, + externref_enabled: bool, wasm_interface_types: bool, support_start: bool, ) -> Result<(NonstandardWitSectionId, WasmBindgenAuxId), Error> { @@ -61,7 +61,7 @@ pub fn process( memory: wasm_bindgen_wasm_conventions::get_memory(module).ok(), module, start_found: false, - anyref_enabled, + externref_enabled, wasm_interface_types, support_start, }; @@ -147,7 +147,7 @@ impl<'a> Context<'a> { } self.handle_duplicate_imports(&duplicate_import_map); - self.inject_anyref_initialization()?; + self.inject_externref_initialization()?; if let Some(custom) = self .module @@ -187,7 +187,7 @@ impl<'a> Context<'a> { let signature = Function { shim_idx: 0, arguments: vec![Descriptor::I32; 3], - ret: Descriptor::Anyref, + ret: Descriptor::Externref, }; let id = self.import_adapter(id, signature, AdapterJsImportKind::Normal)?; // Synthesize the two integer pointers we pass through which @@ -284,22 +284,22 @@ impl<'a> Context<'a> { } // Ensure that the `start` function for this module calls the - // `__wbindgen_init_anyref_table` function. This'll ensure that all - // instances of this module have the initial slots of the anyref table + // `__wbindgen_init_externref_table` function. This'll ensure that all + // instances of this module have the initial slots of the externref table // initialized correctly. // // Note that this is disabled if WebAssembly interface types are enabled // since that's a slightly different environment for now which doesn't have // quite the same initialization. - fn inject_anyref_initialization(&mut self) -> Result<(), Error> { - if !self.anyref_enabled || self.wasm_interface_types { + fn inject_externref_initialization(&mut self) -> Result<(), Error> { + if !self.externref_enabled || self.wasm_interface_types { return Ok(()); } let ty = self.module.types.add(&[], &[]); let (import, import_id) = self.module - .add_import_func(PLACEHOLDER_MODULE, "__wbindgen_init_anyref_table", ty); + .add_import_func(PLACEHOLDER_MODULE, "__wbindgen_init_externref_table", ty); self.module.start = Some(match self.module.start { Some(prev_start) => { @@ -309,7 +309,7 @@ impl<'a> Context<'a> { } None => import, }); - self.bind_intrinsic(import_id, Intrinsic::InitAnyrefTable)?; + self.bind_intrinsic(import_id, Intrinsic::InitExternrefTable)?; Ok(()) } @@ -481,8 +481,8 @@ impl<'a> Context<'a> { // Note that we call the previous start function, if any, first. This is // because the start function currently only shows up when it's injected - // through thread/anyref transforms. These injected start functions need - // to happen before user code, so we always schedule them first. + // through thread/externref transforms. These injected start functions + // need to happen before user code, so we always schedule them first. let mut builder = walrus::FunctionBuilder::new(&mut self.module.types, &[], &[]); builder.func_body().call(prev_start).call(id); let new_start = builder.finish(Vec::new(), &mut self.module.funcs); @@ -743,7 +743,7 @@ impl<'a> Context<'a> { let id = self.import_adapter( import_id, Function { - arguments: vec![Descriptor::Ref(Box::new(Descriptor::Anyref))], + arguments: vec![Descriptor::Ref(Box::new(Descriptor::Externref))], shim_idx: 0, ret: Descriptor::Boolean, }, @@ -850,7 +850,7 @@ impl<'a> Context<'a> { let signature = Function { shim_idx: 0, arguments: vec![Descriptor::I32], - ret: Descriptor::Anyref, + ret: Descriptor::Externref, }; let id = self.import_adapter(import_id, signature, AdapterJsImportKind::Normal)?; self.aux diff --git a/crates/cli-support/src/wit/nonstandard.rs b/crates/cli-support/src/wit/nonstandard.rs index d090f3385d4..1bebca63c28 100644 --- a/crates/cli-support/src/wit/nonstandard.rs +++ b/crates/cli-support/src/wit/nonstandard.rs @@ -47,12 +47,12 @@ pub struct WasmBindgenAux { /// exported structs from Rust and their fields they've got exported. pub structs: Vec, - /// Information about various internal functions used to manage the `anyref` + /// Information about various internal functions used to manage the `externref` /// table, later used to process JS bindings. - pub anyref_table: Option, + pub externref_table: Option, pub function_table: Option, - pub anyref_alloc: Option, - pub anyref_drop_slice: Option, + pub externref_alloc: Option, + pub externref_drop_slice: Option, /// Various intrinsics used for JS glue generation pub exn_store: Option, @@ -89,7 +89,7 @@ pub struct AuxExport { /// sort of an "export map" saying how to wire up all the free functions from /// the wasm module into the output expected JS module. All our functions here /// currently take integer parameters and require a JS wrapper, but ideally -/// we'd change them one day to taking/receiving `anyref` which then use some +/// we'd change them one day to taking/receiving `externref` which then use some /// sort of webidl import to customize behavior or something like that. In any /// case this doesn't feel quite right in terms of priviledge separation, so /// we'll want to work on this. For now though it works. @@ -175,7 +175,7 @@ pub enum AuxImport { /// function call is expected to always be the class. ValueWithThis(JsImport, String), - /// This import is expected to be a function that takes an `anyref` and + /// This import is expected to be a function that takes an `externref` and /// returns a `bool`. It's expected that it tests if the argument is an /// instance of (using `instanceof`) the name specified. /// @@ -290,7 +290,7 @@ pub enum AuxImport { /// is one that is exported from the Rust/wasm. /// /// TODO: sort of like the export map below we should ideally create the - /// `anyref` from within Rust itself and then return it directly rather than + /// `externref` from within Rust itself and then return it directly rather than /// requiring an intrinsic here to do so. WrapInExportedClass(String), @@ -371,16 +371,16 @@ impl walrus::CustomSection for WasmBindgenAux { } fn add_gc_roots(&self, roots: &mut walrus::passes::Roots) { - if let Some(id) = self.anyref_table { + if let Some(id) = self.externref_table { roots.push_table(id); } if let Some(id) = self.function_table { roots.push_table(id); } - if let Some(id) = self.anyref_alloc { + if let Some(id) = self.externref_alloc { roots.push_func(id); } - if let Some(id) = self.anyref_drop_slice { + if let Some(id) = self.externref_drop_slice { roots.push_func(id); } if let Some(id) = self.exn_store { diff --git a/crates/cli-support/src/wit/outgoing.rs b/crates/cli-support/src/wit/outgoing.rs index 69d15c31b11..d6b4692c4a0 100644 --- a/crates/cli-support/src/wit/outgoing.rs +++ b/crates/cli-support/src/wit/outgoing.rs @@ -32,18 +32,18 @@ impl InstructionBuilder<'_, '_> { &[AdapterType::Bool], ); } - Descriptor::Anyref => { + Descriptor::Externref => { self.instruction( &[AdapterType::I32], - Instruction::AnyrefLoadOwned, - &[AdapterType::Anyref], + Instruction::ExternrefLoadOwned, + &[AdapterType::Externref], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( &[AdapterType::I32], - Instruction::AnyrefLoadOwned, - &[AdapterType::NamedAnyref(name.clone())], + Instruction::ExternrefLoadOwned, + &[AdapterType::NamedExternref(name.clone())], ); } Descriptor::I8 => self.outgoing_i32(AdapterType::S8), @@ -162,18 +162,18 @@ impl InstructionBuilder<'_, '_> { fn outgoing_ref(&mut self, mutable: bool, arg: &Descriptor) -> Result<(), Error> { match arg { - Descriptor::Anyref => { + Descriptor::Externref => { self.instruction( &[AdapterType::I32], Instruction::TableGet, - &[AdapterType::Anyref], + &[AdapterType::Externref], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( &[AdapterType::I32], Instruction::TableGet, - &[AdapterType::NamedAnyref(name.clone())], + &[AdapterType::NamedExternref(name.clone())], ); } Descriptor::CachedString => self.cached_string(false, false)?, @@ -232,20 +232,20 @@ impl InstructionBuilder<'_, '_> { fn outgoing_option(&mut self, arg: &Descriptor) -> Result<(), Error> { match arg { - Descriptor::Anyref => { + Descriptor::Externref => { // This is set to `undefined` in the `None` case and otherwise // is the valid owned index. self.instruction( &[AdapterType::I32], - Instruction::AnyrefLoadOwned, - &[AdapterType::Anyref.option()], + Instruction::ExternrefLoadOwned, + &[AdapterType::Externref.option()], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( &[AdapterType::I32], - Instruction::AnyrefLoadOwned, - &[AdapterType::NamedAnyref(name.clone()).option()], + Instruction::ExternrefLoadOwned, + &[AdapterType::NamedExternref(name.clone()).option()], ); } Descriptor::I8 => self.out_option_sentinel(AdapterType::S8), @@ -328,20 +328,20 @@ impl InstructionBuilder<'_, '_> { fn outgoing_option_ref(&mut self, _mutable: bool, arg: &Descriptor) -> Result<(), Error> { match arg { - Descriptor::Anyref => { + Descriptor::Externref => { // If this is `Some` then it's the index, otherwise if it's // `None` then it's the index pointing to undefined. self.instruction( &[AdapterType::I32], Instruction::TableGet, - &[AdapterType::Anyref.option()], + &[AdapterType::Externref.option()], ); } - Descriptor::NamedAnyref(name) => { + Descriptor::NamedExternref(name) => { self.instruction( &[AdapterType::I32], Instruction::TableGet, - &[AdapterType::NamedAnyref(name.clone()).option()], + &[AdapterType::NamedExternref(name.clone()).option()], ); } Descriptor::CachedString => self.cached_string(true, false)?, diff --git a/crates/cli-support/src/wit/section.rs b/crates/cli-support/src/wit/section.rs index 10e2a62ca20..e2703c4713a 100644 --- a/crates/cli-support/src/wit/section.rs +++ b/crates/cli-support/src/wit/section.rs @@ -45,9 +45,9 @@ pub fn add(module: &mut Module) -> Result<(), Error> { structs, // irrelevant ids used to track various internal intrinsics and such - anyref_table: _, - anyref_alloc: _, - anyref_drop_slice: _, + externref_table: _, + externref_alloc: _, + externref_drop_slice: _, exn_store: _, shadow_stack_pointer: _, function_table: _, @@ -243,17 +243,19 @@ fn translate_instruction( I32FromStringFirstChar | StringFromChar => { bail!("chars aren't supported in wasm interface types"); } - I32FromAnyrefOwned | I32FromAnyrefBorrow | AnyrefLoadOwned | TableGet => { - bail!("anyref pass failed to sink into wasm module"); + I32FromExternrefOwned | I32FromExternrefBorrow | ExternrefLoadOwned | TableGet => { + bail!("externref pass failed to sink into wasm module"); } - I32FromAnyrefRustOwned { .. } | I32FromAnyrefRustBorrow { .. } | RustFromI32 { .. } => { + I32FromExternrefRustOwned { .. } + | I32FromExternrefRustBorrow { .. } + | RustFromI32 { .. } => { bail!("rust types aren't supported in wasm interface types"); } I32Split64 { .. } | I64FromLoHi { .. } => { bail!("64-bit integers aren't supported in wasm-bindgen"); } I32SplitOption64 { .. } - | I32FromOptionAnyref { .. } + | I32FromOptionExternref { .. } | I32FromOptionU32Sentinel | I32FromOptionRust { .. } | I32FromOptionBool diff --git a/crates/cli-support/src/wit/standard.rs b/crates/cli-support/src/wit/standard.rs index e3920a06a84..0ab0d34ec59 100644 --- a/crates/cli-support/src/wit/standard.rs +++ b/crates/cli-support/src/wit/standard.rs @@ -55,7 +55,7 @@ pub enum StackChange { #[derive(Debug, Copy, Clone, PartialEq)] pub enum AdapterJsImportKind { - /// The first argument is an `anyref` which is the `this` of the function + /// The first argument is an `externref` which is the `this` of the function /// call Method, /// The value imported should be invoked as `new` @@ -77,14 +77,14 @@ pub enum AdapterType { F32, F64, String, - Anyref, + Externref, Bool, I32, I64, Vector(VectorKind), Option(Box), Struct(String), - NamedAnyref(String), + NamedExternref(String), Function, } @@ -120,24 +120,24 @@ pub enum Instruction { I32FromBool, /// Pops a `string` from the stack and pushes the first character as `i32` I32FromStringFirstChar, - /// Pops an `anyref` from the stack, allocates space in the anyref table, + /// Pops an `externref` from the stack, allocates space in the externref table, /// returns the index it was stored at. - I32FromAnyrefOwned, - /// Pops an `anyref` from the stack, pushes it onto the anyref wasm table + I32FromExternrefOwned, + /// Pops an `externref` from the stack, pushes it onto the externref wasm table /// stack, and returns the index it was stored at. - I32FromAnyrefBorrow, - /// Pops an `anyref` from the stack, assumes it's a Rust class given, and + I32FromExternrefBorrow, + /// Pops an `externref` from the stack, assumes it's a Rust class given, and /// deallocates the JS object and returns the i32 Rust pointer. - I32FromAnyrefRustOwned { + I32FromExternrefRustOwned { class: String, }, - /// Pops an `anyref` from the stack, assumes it's a Rust class given, and + /// Pops an `externref` from the stack, assumes it's a Rust class given, and /// passes the pointer to Rust which will be borrowed for the duration of a /// call - I32FromAnyrefRustBorrow { + I32FromExternrefRustBorrow { class: String, }, - /// Pops an `anyref` from the stack, pushes 0 if it's "none" or the + /// Pops an `externref` from the stack, pushes 0 if it's "none" or the /// consumed pointer value if it's "some". I32FromOptionRust { class: String, @@ -152,28 +152,28 @@ pub enum Instruction { I32SplitOption64 { signed: bool, }, - /// Pops an `anyref` from the stack, pushes either 0 if it's "none" or and + /// Pops an `externref` from the stack, pushes either 0 if it's "none" or and /// index into the owned wasm table it was stored at if it's "some" - I32FromOptionAnyref { - /// Set to `Some` by the anyref pass of where to put it in the wasm + I32FromOptionExternref { + /// Set to `Some` by the externref pass of where to put it in the wasm /// module, otherwise it's shoved into the JS shim. table_and_alloc: Option<(walrus::TableId, walrus::FunctionId)>, }, - /// Pops an `anyref` from the stack, pushes either a sentinel value if it's + /// Pops an `externref` from the stack, pushes either a sentinel value if it's /// "none" or the integer value of it if it's "some" I32FromOptionU32Sentinel, - /// Pops an `anyref` from the stack, pushes 0 for "none", 1 for + /// Pops an `externref` from the stack, pushes 0 for "none", 1 for /// "some(false)', and 2 for "some(true)" I32FromOptionBool, - /// Pops an `anyref` from the stack, pushes a sentinel for "none" or the + /// Pops an `externref` from the stack, pushes a sentinel for "none" or the /// value if it's "some" I32FromOptionChar, - /// Pops an `anyref` from the stack, pushes `hole` for "none" or the + /// Pops an `externref` from the stack, pushes `hole` for "none" or the /// value if it's "some" I32FromOptionEnum { hole: u32, }, - /// Pops any anyref from the stack and then pushes two values. First is a + /// Pops any externref from the stack and then pushes two values. First is a /// 0/1 if it's none/some and second is `ty` value if it was there or 0 if /// it wasn't there. FromOptionNative { @@ -208,7 +208,7 @@ pub enum Instruction { realloc: Option, }, - /// Pops an anyref, pushes pointer/length or all zeros + /// Pops an externref, pushes pointer/length or all zeros OptionVector { kind: VectorKind, malloc: walrus::FunctionId, @@ -217,15 +217,15 @@ pub enum Instruction { /// pops a `i32`, pushes `bool` BoolFromI32, - /// pops `i32`, loads anyref at that slot, dealloates anyref, pushes `anyref` - AnyrefLoadOwned, + /// pops `i32`, loads externref at that slot, dealloates externref, pushes `externref` + ExternrefLoadOwned, /// pops `i32`, pushes string from that `char` StringFromChar, /// pops two `i32`, pushes a 64-bit number I64FromLoHi { signed: bool, }, - /// pops `i32`, pushes an anyref for the wrapped rust class + /// pops `i32`, pushes an externref for the wrapped rust class RustFromI32 { class: String, }, @@ -251,9 +251,9 @@ pub enum Instruction { mem: walrus::MemoryId, free: walrus::FunctionId, }, - /// pops i32, loads anyref from anyref table + /// pops i32, loads externref from externref table TableGet, - /// pops two i32 data pointers, pushes an anyref closure + /// pops two i32 data pointers, pushes an externref closure StackClosure { adapter: AdapterId, nargs: usize, @@ -271,7 +271,7 @@ pub enum Instruction { }, /// pops i32, pushes it viewed as an optional value with a known sentinel OptionU32Sentinel, - /// pops an i32, then `ty`, then pushes anyref + /// pops an i32, then `ty`, then pushes externref ToOptionNative { ty: walrus::ValType, signed: bool, @@ -300,7 +300,7 @@ impl AdapterType { wit_walrus::ValType::F32 => AdapterType::F32, wit_walrus::ValType::F64 => AdapterType::F64, wit_walrus::ValType::String => AdapterType::String, - wit_walrus::ValType::Anyref => AdapterType::Anyref, + wit_walrus::ValType::Externref => AdapterType::Externref, wit_walrus::ValType::I32 => AdapterType::I32, wit_walrus::ValType::I64 => AdapterType::I64, } @@ -312,10 +312,8 @@ impl AdapterType { walrus::ValType::I64 => AdapterType::I64, walrus::ValType::F32 => AdapterType::F32, walrus::ValType::F64 => AdapterType::F64, - walrus::ValType::Anyref => AdapterType::Anyref, - walrus::ValType::Funcref | walrus::ValType::Nullref | walrus::ValType::V128 => { - return None - } + walrus::ValType::Externref => AdapterType::Externref, + walrus::ValType::Funcref | walrus::ValType::V128 => return None, }) } @@ -325,7 +323,7 @@ impl AdapterType { AdapterType::I64 => walrus::ValType::I64, AdapterType::F32 => walrus::ValType::F32, AdapterType::F64 => walrus::ValType::F64, - AdapterType::Anyref | AdapterType::NamedAnyref(_) => walrus::ValType::Anyref, + AdapterType::Externref | AdapterType::NamedExternref(_) => walrus::ValType::Externref, _ => return None, }) } @@ -343,7 +341,9 @@ impl AdapterType { AdapterType::F32 => wit_walrus::ValType::F32, AdapterType::F64 => wit_walrus::ValType::F64, AdapterType::String => wit_walrus::ValType::String, - AdapterType::Anyref | AdapterType::NamedAnyref(_) => wit_walrus::ValType::Anyref, + AdapterType::Externref | AdapterType::NamedExternref(_) => { + wit_walrus::ValType::Externref + } AdapterType::I32 => wit_walrus::ValType::I32, AdapterType::I64 => wit_walrus::ValType::I64, @@ -492,7 +492,7 @@ impl walrus::CustomSection for NonstandardWitSection { roots.push_func(id); } } - I32FromOptionAnyref { table_and_alloc } => { + I32FromOptionExternref { table_and_alloc } => { if let Some((table, alloc)) = table_and_alloc { roots.push_table(table); roots.push_func(alloc); diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index c592642374e..170a291a744 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -24,7 +24,7 @@ rouille = { version = "3.0.0", default-features = false } serde = { version = "1.0", features = ['derive'] } serde_derive = "1.0" serde_json = "1.0" -walrus = { version = "0.16.0", features = ['parallel'] } +walrus = { version = "0.17.0", features = ['parallel'] } wasm-bindgen-cli-support = { path = "../cli-support", version = "=0.2.62" } wasm-bindgen-shared = { path = "../shared", version = "=0.2.62" } @@ -34,11 +34,11 @@ diff = "0.1" predicates = "1.0.0" rayon = "1.0" tempfile = "3.0" -walrus = "0.16" -wit-printer = "0.1" -wit-text = "0.6" -wit-validator = "0.1" -wit-walrus = "0.2" +walrus = "0.17" +wit-printer = "0.2" +wit-text = "0.7" +wit-validator = "0.2" +wit-walrus = "0.4" [[test]] name = "reference" diff --git a/crates/cli/tests/interface-types/anyref.wat b/crates/cli/tests/interface-types/anyref.wat index 8903f853ddb..7a7a8ee3592 100644 --- a/crates/cli/tests/interface-types/anyref.wat +++ b/crates/cli/tests/interface-types/anyref.wat @@ -1,26 +1,26 @@ (module - (func $foo (param anyref) (result anyref) + (func $foo (param externref) (result externref) local.get 0) - (func $store (param anyref) + (func $store (param externref) i32.const 0 local.get 0 table.set 0) - (func $load (result anyref) + (func $load (result externref) i32.const 0 table.get 0) - (table 1 anyref) + (table 1 externref) - (@interface func (export "foo") (param anyref) (result anyref) + (@interface func (export "foo") (param externref) (result externref) arg.get 0 call-core $foo) - (@interface func (export "store") (param anyref) + (@interface func (export "store") (param externref) arg.get 0 call-core $store) - (@interface func (export "load") (result anyref) + (@interface func (export "load") (result externref) call-core $load) ) diff --git a/crates/cli/tests/reference.rs b/crates/cli/tests/reference.rs index 742d05dd732..4e0f806a4d7 100644 --- a/crates/cli/tests/reference.rs +++ b/crates/cli/tests/reference.rs @@ -98,8 +98,8 @@ fn runtest(test: &Path) -> Result<()> { .arg(td.path()) .arg(&wasm) .arg("--no-typescript"); - if contents.contains("// enable-anyref") { - bindgen.env("WASM_BINDGEN_ANYREF", "1"); + if contents.contains("// enable-externref") { + bindgen.env("WASM_BINDGEN_EXTERNREF", "1"); } if interface_types { bindgen.env("WASM_INTERFACE_TYPES", "1"); diff --git a/crates/cli/tests/reference/anyref-empty.js b/crates/cli/tests/reference/anyref-empty.js index 440a088a147..66c76d2ef6c 100644 --- a/crates/cli/tests/reference/anyref-empty.js +++ b/crates/cli/tests/reference/anyref-empty.js @@ -1,6 +1,6 @@ import * as wasm from './reference_test_bg.wasm'; -export const __wbindgen_init_anyref_table = function() { +export const __wbindgen_init_externref_table = function() { const table = wasm.__wbindgen_export_0; const offset = table.grow(4); table.set(0, undefined); diff --git a/crates/cli/tests/reference/anyref-empty.rs b/crates/cli/tests/reference/anyref-empty.rs index afcbb92424d..e49fcc21681 100644 --- a/crates/cli/tests/reference/anyref-empty.rs +++ b/crates/cli/tests/reference/anyref-empty.rs @@ -1 +1 @@ -// enable-anyref +// enable-externref diff --git a/crates/cli/tests/reference/anyref-empty.wat b/crates/cli/tests/reference/anyref-empty.wat index 8e3348797e2..f5cc60b84a0 100644 --- a/crates/cli/tests/reference/anyref-empty.wat +++ b/crates/cli/tests/reference/anyref-empty.wat @@ -1,6 +1,6 @@ (module (type (;0;) (func)) - (import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) + (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) (table (;0;) 32 externref) (memory (;0;) 16) (export "memory" (memory 0)) diff --git a/crates/cli/tests/reference/anyref-import-catch.js b/crates/cli/tests/reference/anyref-import-catch.js index 6a059a659ad..6b0f9fb225e 100644 --- a/crates/cli/tests/reference/anyref-import-catch.js +++ b/crates/cli/tests/reference/anyref-import-catch.js @@ -1,26 +1,31 @@ import * as wasm from './reference_test_bg.wasm'; -const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; +const heap = new Array(32).fill(undefined); -let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); +heap.push(undefined, null, true, false); -cachedTextDecoder.decode(); +function getObject(idx) { return heap[idx]; } -let cachegetUint8Memory0 = null; -function getUint8Memory0() { - if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) { - cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer); - } - return cachegetUint8Memory0; +let heap_next = heap.length; + +function dropObject(idx) { + if (idx < 36) return; + heap[idx] = heap_next; + heap_next = idx; } -function getStringFromWasm0(ptr, len) { - return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +function takeObject(idx) { + const ret = getObject(idx); + dropObject(idx); + return ret; } -function addToAnyrefTable0(obj) { - const idx = wasm.__anyref_table_alloc(); - wasm.__wbindgen_export_0.set(idx, obj); +function addHeapObject(obj) { + if (heap_next === heap.length) heap.push(heap.length + 1); + const idx = heap_next; + heap_next = heap[idx]; + + heap[idx] = obj; return idx; } @@ -30,8 +35,7 @@ function handleError(f) { return f.apply(this, arguments); } catch (e) { - const idx = addToAnyrefTable0(e); - wasm.__wbindgen_exn_store(idx); + wasm.__wbindgen_exn_store(addHeapObject(e)); } }; } @@ -41,26 +45,15 @@ export function exported() { wasm.exported(); } +export const __wbindgen_object_drop_ref = function(arg0) { + takeObject(arg0); +}; + export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() { foo(); }); -export const __wbindgen_throw = function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -}; - export const __wbindgen_rethrow = function(arg0) { - throw arg0; -}; - -export const __wbindgen_init_anyref_table = function() { - const table = wasm.__wbindgen_export_0; - const offset = table.grow(4); - table.set(0, undefined); - table.set(offset + 0, undefined); - table.set(offset + 1, null); - table.set(offset + 2, true); - table.set(offset + 3, false); - ; + throw takeObject(arg0); }; diff --git a/crates/cli/tests/reference/anyref-import-catch.rs b/crates/cli/tests/reference/anyref-import-catch.rs index f51161fea88..6e9f94a6032 100644 --- a/crates/cli/tests/reference/anyref-import-catch.rs +++ b/crates/cli/tests/reference/anyref-import-catch.rs @@ -1,4 +1,4 @@ -// enable-anyref +// enable-externref use wasm_bindgen::prelude::*; diff --git a/crates/cli/tests/reference/anyref-import-catch.wat b/crates/cli/tests/reference/anyref-import-catch.wat index fa730f6fbed..702b6ae029c 100644 --- a/crates/cli/tests/reference/anyref-import-catch.wat +++ b/crates/cli/tests/reference/anyref-import-catch.wat @@ -1,16 +1,9 @@ (module (type (;0;) (func)) - (type (;1;) (func (result i32))) - (type (;2;) (func (param i32))) - (import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) - (func $__wbindgen_exn_store (type 2) (param i32)) - (func $__anyref_table_alloc (type 1) (result i32)) + (type (;1;) (func (param i32))) + (func $__wbindgen_exn_store (type 1) (param i32)) (func $exported (type 0)) - (table (;0;) 32 externref) (memory (;0;) 17) (export "memory" (memory 0)) (export "exported" (func $exported)) - (export "__wbindgen_export_0" (table 0)) - (export "__wbindgen_exn_store" (func $__wbindgen_exn_store)) - (export "__anyref_table_alloc" (func $__anyref_table_alloc)) - (export "__wbindgen_start" (func 0))) + (export "__wbindgen_exn_store" (func $__wbindgen_exn_store))) diff --git a/crates/cli/tests/reference/anyref-nop.js b/crates/cli/tests/reference/anyref-nop.js index 43bbe06c766..364e40de679 100644 --- a/crates/cli/tests/reference/anyref-nop.js +++ b/crates/cli/tests/reference/anyref-nop.js @@ -6,7 +6,7 @@ export function foo() { wasm.foo(); } -export const __wbindgen_init_anyref_table = function() { +export const __wbindgen_init_externref_table = function() { const table = wasm.__wbindgen_export_0; const offset = table.grow(4); table.set(0, undefined); diff --git a/crates/cli/tests/reference/anyref-nop.rs b/crates/cli/tests/reference/anyref-nop.rs index 38359372f11..0224340f89e 100644 --- a/crates/cli/tests/reference/anyref-nop.rs +++ b/crates/cli/tests/reference/anyref-nop.rs @@ -1,4 +1,4 @@ -// enable-anyref +// enable-externref use wasm_bindgen::prelude::*; diff --git a/crates/cli/tests/reference/anyref-nop.wat b/crates/cli/tests/reference/anyref-nop.wat index ad819270130..7c42a885375 100644 --- a/crates/cli/tests/reference/anyref-nop.wat +++ b/crates/cli/tests/reference/anyref-nop.wat @@ -1,6 +1,6 @@ (module (type (;0;) (func)) - (import "./reference_test_bg.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) + (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) (func $foo (type 0)) (table (;0;) 32 externref) (memory (;0;) 17) diff --git a/crates/cli/tests/reference/interface-types-anyref.rs b/crates/cli/tests/reference/interface-types-anyref.rs index 5b69d115174..86fb2b93dec 100644 --- a/crates/cli/tests/reference/interface-types-anyref.rs +++ b/crates/cli/tests/reference/interface-types-anyref.rs @@ -3,6 +3,6 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] -pub fn anyref_in_out(a: &JsValue, b: JsValue) -> JsValue { +pub fn externref_in_out(a: &JsValue, b: JsValue) -> JsValue { b } diff --git a/crates/cli/tests/reference/interface-types-anyref.wat b/crates/cli/tests/reference/interface-types-anyref.wat index 0f7e3603fcd..2761dd98493 100644 --- a/crates/cli/tests/reference/interface-types-anyref.wat +++ b/crates/cli/tests/reference/interface-types-anyref.wat @@ -1,12 +1,12 @@ (module (type (;0;) (func (param externref externref) (result externref))) - (func $anyref_in_out anyref shim (type 0) (param externref externref) (result externref)) + (func $externref_in_out externref shim (type 0) (param externref externref) (result externref)) (memory (;0;) 17) (export "memory" (memory 0)) - (export "anyref_in_out" (func $anyref_in_out anyref shim)) - (@interface type (;0;) (func (param anyref) (param anyref) (result anyref))) + (export "externref_in_out" (func $externref_in_out externref shim)) + (@interface type (;0;) (func (param externref) (param externref) (result externref))) (@interface func (;0;) (type 0) arg.get 0 arg.get 1 - call-core $anyref_in_out anyref shim) - (@interface export "anyref_in_out" (func 0))) + call-core $externref_in_out externref shim) + (@interface export "externref_in_out" (func 0))) diff --git a/crates/anyref-xform/Cargo.toml b/crates/externref-xform/Cargo.toml similarity index 64% rename from crates/anyref-xform/Cargo.toml rename to crates/externref-xform/Cargo.toml index e5900d46008..371870879e3 100644 --- a/crates/anyref-xform/Cargo.toml +++ b/crates/externref-xform/Cargo.toml @@ -1,24 +1,24 @@ [package] -name = "wasm-bindgen-anyref-xform" +name = "wasm-bindgen-externref-xform" version = "0.2.62" authors = ["The wasm-bindgen Developers"] license = "MIT/Apache-2.0" -repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/anyref-xform" +repository = "https://github.com/rustwasm/wasm-bindgen/tree/master/crates/externref-xform" homepage = "https://rustwasm.github.io/wasm-bindgen/" -documentation = "https://docs.rs/wasm-bindgen-anyref-xform" +documentation = "https://docs.rs/wasm-bindgen-externref-xform" description = """ -Internal anyref transformations for wasm-bindgen +Internal externref transformations for wasm-bindgen """ edition = '2018' [dependencies] anyhow = "1.0" -walrus = "0.16.0" +walrus = "0.17.0" [dev-dependencies] rayon = "1.0" wasmprinter = "0.2" -wast = "3.0" +wast = "17.0" wat = "1.0" [[test]] diff --git a/crates/anyref-xform/LICENSE-APACHE b/crates/externref-xform/LICENSE-APACHE similarity index 100% rename from crates/anyref-xform/LICENSE-APACHE rename to crates/externref-xform/LICENSE-APACHE diff --git a/crates/anyref-xform/LICENSE-MIT b/crates/externref-xform/LICENSE-MIT similarity index 100% rename from crates/anyref-xform/LICENSE-MIT rename to crates/externref-xform/LICENSE-MIT diff --git a/crates/anyref-xform/src/lib.rs b/crates/externref-xform/src/lib.rs similarity index 83% rename from crates/anyref-xform/src/lib.rs rename to crates/externref-xform/src/lib.rs index 5ef2cda22f1..881bba713eb 100644 --- a/crates/anyref-xform/src/lib.rs +++ b/crates/externref-xform/src/lib.rs @@ -1,11 +1,11 @@ -//! Transformation for wasm-bindgen to enable usage of `anyref` in a wasm +//! Transformation for wasm-bindgen to enable usage of `externref` in a wasm //! module. //! //! This crate is in charge of enabling code using `wasm-bindgen` to use the -//! `anyref` type inside of the wasm module. This transformation pass primarily -//! wraps exports and imports in shims which use `anyref`, but quickly turn them +//! `externref` type inside of the wasm module. This transformation pass primarily +//! wraps exports and imports in shims which use `externref`, but quickly turn them //! into `i32` value types. This is all largely a stopgap until Rust has -//! first-class support for the `anyref` type, but that's thought to be in the +//! first-class support for the `externref` type, but that's thought to be in the //! far future and will take quite some time to implement. In the meantime, we //! have this! //! @@ -13,7 +13,7 @@ //! about imports and exports. Afterwards this pass runs in one go against a //! wasm module, updating exports, imports, calls to these functions, etc. The //! goal at least is to have valid wasm modules coming in that don't use -//! `anyref` and valid wasm modules going out which use `anyref` at the fringes. +//! `externref` and valid wasm modules going out which use `externref` at the fringes. use anyhow::{anyhow, bail, Error}; use std::cmp; @@ -23,16 +23,16 @@ use walrus::ir::*; use walrus::{ElementId, ExportId, ImportId, InstrLocId, TypeId}; use walrus::{FunctionId, GlobalId, InitExpr, Module, TableId, ValType}; -// must be kept in sync with src/lib.rs and ANYREF_HEAP_START +// must be kept in sync with src/lib.rs and EXTERNREF_HEAP_START const DEFAULT_MIN: u32 = 32; -/// State of the anyref pass, used to collect information while bindings are +/// State of the externref pass, used to collect information while bindings are /// generated and used eventually to actually execute the entire pass. #[derive(Default)] pub struct Context { // Functions within the module that we're gonna be wrapping, organized by // type. The `Function` contains information about what arguments/return - // values in the function signature should turn into anyref. + // values in the function signature should turn into externref. imports: HashMap, exports: HashMap, @@ -49,7 +49,7 @@ pub struct Context { // final offset plus the element segment used to initialized that range. elements: BTreeMap, - // The anyref table we'll be using, injected after construction + // The externref table we'll be using, injected after construction table: Option, } @@ -81,10 +81,10 @@ struct Transform<'a> { } struct Function { - // A map of argument index to whether it's an owned or borrowed anyref + // A map of argument index to whether it's an owned or borrowed externref // (owned = true) args: HashMap, - ret_anyref: bool, + ret_externref: bool, } enum Intrinsic { @@ -120,9 +120,13 @@ impl Context { } } - // Add in an anyref table to the module, which we'll be using for + // Add in an externref table to the module, which we'll be using for // our transform below. - self.table = Some(module.tables.add_local(DEFAULT_MIN, None, ValType::Anyref)); + self.table = Some( + module + .tables + .add_local(DEFAULT_MIN, None, ValType::Externref), + ); Ok(()) } @@ -132,10 +136,10 @@ impl Context { pub fn import_xform( &mut self, id: ImportId, - anyref: &[(usize, bool)], - ret_anyref: bool, + externref: &[(usize, bool)], + ret_externref: bool, ) -> &mut Self { - if let Some(f) = self.function(anyref, ret_anyref) { + if let Some(f) = self.function(externref, ret_externref) { self.imports.insert(id, f); } self @@ -146,10 +150,10 @@ impl Context { pub fn export_xform( &mut self, id: ExportId, - anyref: &[(usize, bool)], - ret_anyref: bool, + externref: &[(usize, bool)], + ret_externref: bool, ) -> &mut Self { - if let Some(f) = self.function(anyref, ret_anyref) { + if let Some(f) = self.function(externref, ret_externref) { self.exports.insert(id, f); } self @@ -161,22 +165,22 @@ impl Context { pub fn table_element_xform( &mut self, idx: u32, - anyref: &[(usize, bool)], - ret_anyref: bool, + externref: &[(usize, bool)], + ret_externref: bool, ) -> Option { - self.function(anyref, ret_anyref).map(|f| { + self.function(externref, ret_externref).map(|f| { self.new_elements.push((idx, f)); self.new_elements.len() as u32 + self.new_element_offset - 1 }) } - fn function(&self, anyref: &[(usize, bool)], ret_anyref: bool) -> Option { - if !ret_anyref && anyref.len() == 0 { + fn function(&self, externref: &[(usize, bool)], ret_externref: bool) -> Option { + if !ret_externref && externref.len() == 0 { return None; } Some(Function { - args: anyref.iter().cloned().collect(), - ret_anyref, + args: externref.iter().cloned().collect(), + ret_externref, }) } @@ -184,7 +188,7 @@ impl Context { let table = self.table.unwrap(); // Inject a stack pointer global which will be used for managing the - // stack on the anyref table. + // stack on the externref table. let init = InitExpr::Value(Value::I32(DEFAULT_MIN as i32)); let stack_pointer = module.globals.add_local(ValType::I32, true, init); @@ -202,10 +206,10 @@ impl Context { _ => continue, }; match export.name.as_str() { - "__anyref_table_alloc" => heap_alloc = Some(f), - "__anyref_table_dealloc" => heap_dealloc = Some(f), - "__anyref_drop_slice" => drop_slice = Some(f), - "__anyref_heap_live_count" => live_count = Some(f), + "__externref_table_alloc" => heap_alloc = Some(f), + "__externref_table_dealloc" => heap_dealloc = Some(f), + "__externref_drop_slice" => drop_slice = Some(f), + "__externref_heap_live_count" => live_count = Some(f), _ => continue, } to_delete.push(export.id()); @@ -268,7 +272,7 @@ impl Context { impl Transform<'_> { fn run(&mut self, module: &mut Module) -> Result<(), Error> { // Detect all the various intrinsics and such. This will also along the - // way inject an intrinsic for cloning an anyref. + // way inject an intrinsic for cloning an externref. self.find_intrinsics(module)?; // Perform transformations of imports, exports, and function pointers. @@ -300,12 +304,12 @@ impl Transform<'_> { walrus::ImportKind::Function(f) => f, _ => continue, }; - if import.module == "__wbindgen_anyref_xform__" { + if import.module == "__wbindgen_externref_xform__" { match import.name.as_str() { - "__wbindgen_anyref_table_grow" => { + "__wbindgen_externref_table_grow" => { self.intrinsic_map.insert(f, Intrinsic::TableGrow); } - "__wbindgen_anyref_table_set_null" => { + "__wbindgen_externref_table_set_null" => { self.intrinsic_map.insert(f, Intrinsic::TableSetNull); } n => bail!("unknown intrinsic: {}", n), @@ -334,8 +338,9 @@ impl Transform<'_> { } fn heap_alloc(&self) -> Result { - self.heap_alloc - .ok_or_else(|| anyhow!("failed to find the `__wbindgen_anyref_table_alloc` function")) + self.heap_alloc.ok_or_else(|| { + anyhow!("failed to find the `__wbindgen_externref_table_alloc` function") + }) } fn clone_ref(&self) -> Result { @@ -344,8 +349,9 @@ impl Transform<'_> { } fn heap_dealloc(&self) -> Result { - self.heap_dealloc - .ok_or_else(|| anyhow!("failed to find the `__wbindgen_anyref_table_dealloc` function")) + self.heap_dealloc.ok_or_else(|| { + anyhow!("failed to find the `__wbindgen_externref_table_dealloc` function") + }) } fn process_imports(&mut self, module: &mut Module) -> Result<(), Error> { @@ -359,7 +365,7 @@ impl Transform<'_> { None => continue, }; - let (shim, anyref_ty) = self.append_shim( + let (shim, externref_ty) = self.append_shim( f, &import.name, func, @@ -369,7 +375,7 @@ impl Transform<'_> { )?; self.import_map.insert(f, shim); match &mut module.funcs.get_mut(f).kind { - walrus::FunctionKind::Import(f) => f.ty = anyref_ty, + walrus::FunctionKind::Import(f) => f.ty = externref_ty, _ => unreachable!(), } } @@ -387,7 +393,7 @@ impl Transform<'_> { Some(s) => s, None => continue, }; - let (shim, _anyref_ty) = self.append_shim( + let (shim, _externref_ty) = self.append_shim( f, &export.name, function, @@ -420,7 +426,7 @@ impl Transform<'_> { let target = module.elements.get(orig_element).members[(idx - offset) as usize].ok_or( anyhow!("function index {} not present in element segment", idx), )?; - let (shim, _anyref_ty) = self.append_shim( + let (shim, _externref_ty) = self.append_shim( target, &format!("closure{}", idx), function, @@ -477,24 +483,24 @@ impl Transform<'_> { } let mut param_tys = Vec::new(); let mut param_convert = Vec::new(); - let mut anyref_stack = 0; + let mut externref_stack = 0; for (i, old_ty) in target_ty.params().iter().enumerate() { let is_owned = func.args.remove(&i); let new_ty = is_owned - .map(|_which| ValType::Anyref) + .map(|_which| ValType::Externref) .unwrap_or(old_ty.clone()); param_tys.push(new_ty.clone()); if new_ty == *old_ty { param_convert.push(Convert::None); } else if is_export { - // We're calling an export, so we need to push this anyref into + // We're calling an export, so we need to push this externref into // a table somehow. param_convert.push(Convert::Store { owned: is_owned.unwrap(), }); if is_owned == Some(false) { - anyref_stack += 1; + externref_stack += 1; } } else { // We're calling an import, so we just need to fetch our table @@ -505,21 +511,21 @@ impl Transform<'_> { } } - let new_ret = if func.ret_anyref { + let new_ret = if func.ret_externref { assert_eq!(target_ty.results(), &[ValType::I32]); - vec![ValType::Anyref] + vec![ValType::Externref] } else { target_ty.results().to_vec() }; - let anyref_ty = types.add(¶m_tys, &new_ret); + let externref_ty = types.add(¶m_tys, &new_ret); // If we're an export then our shim is what's actually going to get - // exported, and it's going to have the anyref signature. + // exported, and it's going to have the externref signature. // // If we're an import, then our shim is what the Rust code calls, which // means it'll have the original signature. The existing import's - // signature, however, is transformed to be an anyref signature. - let shim_ty = if is_export { anyref_ty } else { ty }; + // signature, however, is transformed to be an externref signature. + let shim_ty = if is_export { externref_ty } else { ty }; let mut builder = walrus::FunctionBuilder::new( types, @@ -547,12 +553,12 @@ impl Transform<'_> { // gc passes if we don't actually end up using them. let fp = locals.add(ValType::I32); let scratch_i32 = locals.add(ValType::I32); - let scratch_anyref = locals.add(ValType::Anyref); + let scratch_externref = locals.add(ValType::Externref); - // Update our stack pointer if there's any borrowed anyref objects. - if anyref_stack > 0 { + // Update our stack pointer if there's any borrowed externref objects. + if externref_stack > 0 { body.global_get(self.stack_pointer) - .const_(Value::I32(anyref_stack)) + .const_(Value::I32(externref_stack)) .binop(BinaryOp::I32Sub) .local_tee(fp) .global_set(self.stack_pointer); @@ -565,8 +571,8 @@ impl Transform<'_> { body.local_get(params[i]); } Convert::Load { owned: true } => { - // load the anyref onto the stack, then afterwards - // deallocate our index, leaving the anyref on the stack. + // load the externref onto the stack, then afterwards + // deallocate our index, leaving the externref on the stack. body.local_get(params[i]) .table_get(self.table) .local_get(params[i]) @@ -576,8 +582,8 @@ impl Transform<'_> { body.local_get(params[i]).table_get(self.table); } Convert::Store { owned: true } => { - // Allocate space for the anyref, store it, and then leave - // the index of the allocated anyref on the stack. + // Allocate space for the externref, store it, and then leave + // the index of the allocated externref on the stack. body.call(self.heap_alloc()?) .local_tee(scratch_i32) .local_get(params[i]) @@ -585,7 +591,7 @@ impl Transform<'_> { .local_get(scratch_i32); } Convert::Store { owned: false } => { - // Store an anyref at an offset from our function's stack + // Store an externref at an offset from our function's stack // pointer frame. body.local_get(fp); let idx_local = if next_stack_offset == 0 { @@ -609,33 +615,33 @@ impl Transform<'_> { // wrapping. body.call(shim_target); - // If an anyref value is returned, then we need to be sure to apply + // If an externref value is returned, then we need to be sure to apply // special treatment to convert it to an i32 as well. Note that only - // owned anyref values can be returned, so that's all that's handled + // owned externref values can be returned, so that's all that's handled // here. - if func.ret_anyref { + if func.ret_externref { if is_export { // We're an export so we have an i32 on the stack and need to - // convert it to an anyref, basically by doing the same as an + // convert it to an externref, basically by doing the same as an // owned load above: get the value then deallocate our slot. body.local_tee(scratch_i32) .table_get(self.table) .local_get(scratch_i32) .call(self.heap_dealloc()?); } else { - // Imports are the opposite, we have any anyref on the stack + // Imports are the opposite, we have any externref on the stack // and convert it to an i32 by allocating space for it and // storing it there. - body.local_set(scratch_anyref) + body.local_set(scratch_externref) .call(self.heap_alloc()?) .local_tee(scratch_i32) - .local_get(scratch_anyref) + .local_get(scratch_externref) .table_set(self.table) .local_get(scratch_i32); } } - // On function exit restore our anyref stack pointer if we decremented + // On function exit restore our externref stack pointer if we decremented // it to start off. // // Note that we pave over all our stack slots with `ref.null` to ensure @@ -643,18 +649,18 @@ impl Transform<'_> { // no longer in use by our wasm instance. // // TODO: use `table.fill` once that's spec'd - if anyref_stack > 0 { - for i in 0..anyref_stack { + if externref_stack > 0 { + for i in 0..externref_stack { body.local_get(fp); if i > 0 { body.i32_const(i).binop(BinaryOp::I32Add); } - body.ref_null(); + body.ref_null(ValType::Externref); body.table_set(self.table); } body.local_get(fp) - .i32_const(anyref_stack) + .i32_const(externref_stack) .binop(BinaryOp::I32Add) .global_set(self.stack_pointer); } @@ -663,10 +669,10 @@ impl Transform<'_> { // with a fresh type we've been calculating so far. Give the function a // nice name for debugging and then we're good to go! let id = builder.finish(params, funcs); - let name = format!("{} anyref shim", name); + let name = format!("{} externref shim", name); funcs.get_mut(id).name = Some(name); self.shims.insert(id); - Ok((id, anyref_ty)) + Ok((id, externref_ty)) } fn rewrite_calls(&mut self, module: &mut Module) -> Result<(), Error> { @@ -714,6 +720,7 @@ impl Transform<'_> { } }; + let ty = ValType::Externref; match intrinsic { Intrinsic::TableGrow => { // Switch this to a `table.grow` instruction... @@ -725,7 +732,7 @@ impl Transform<'_> { // preceding instruction as the value to grow the // table with. seq.instrs - .insert(i - 1, (RefNull {}.into(), InstrLocId::default())); + .insert(i - 1, (RefNull { ty }.into(), InstrLocId::default())); } Intrinsic::TableSetNull => { // Switch this to a `table.set` instruction... @@ -736,7 +743,7 @@ impl Transform<'_> { // ... and then insert a `ref.null` as the // preceding instruction seq.instrs - .insert(i, (RefNull {}.into(), InstrLocId::default())); + .insert(i, (RefNull { ty }.into(), InstrLocId::default())); } Intrinsic::DropRef => call.func = self.heap_dealloc, Intrinsic::CloneRef => call.func = self.clone_ref, diff --git a/crates/anyref-xform/tests/all.rs b/crates/externref-xform/tests/all.rs similarity index 92% rename from crates/anyref-xform/tests/all.rs rename to crates/externref-xform/tests/all.rs index 7d07cf1b931..d913524ebea 100644 --- a/crates/anyref-xform/tests/all.rs +++ b/crates/externref-xform/tests/all.rs @@ -20,7 +20,7 @@ fn main() { fn runtest(test: &Test) -> Result { let wasm = wat::parse_file(&test.file)?; let mut walrus = walrus::Module::from_buffer(&wasm)?; - let mut cx = wasm_bindgen_anyref_xform::Context::default(); + let mut cx = wasm_bindgen_externref_xform::Context::default(); cx.prepare(&mut walrus)?; for directive in test.directives.iter() { match &directive.kind { @@ -30,7 +30,7 @@ fn runtest(test: &Test) -> Result { .iter() .find(|e| e.name == *name) .ok_or_else(|| anyhow!("failed to find export"))?; - cx.export_xform(export.id(), &directive.args, directive.ret_anyref); + cx.export_xform(export.id(), &directive.args, directive.ret_externref); } DirectiveKind::Import(module, field) => { let import = walrus @@ -38,10 +38,10 @@ fn runtest(test: &Test) -> Result { .iter() .find(|e| e.module == *module && e.name == *field) .ok_or_else(|| anyhow!("failed to find export"))?; - cx.import_xform(import.id(), &directive.args, directive.ret_anyref); + cx.import_xform(import.id(), &directive.args, directive.ret_externref); } DirectiveKind::Table(idx) => { - cx.table_element_xform(*idx, &directive.args, directive.ret_anyref); + cx.table_element_xform(*idx, &directive.args, directive.ret_externref); } } } @@ -123,7 +123,7 @@ struct Test { struct Directive { args: Vec<(usize, bool)>, - ret_anyref: bool, + ret_externref: bool, kind: DirectiveKind, } @@ -216,8 +216,8 @@ fn update_output(path: &Path, output: &str) -> Result<()> { impl<'a> Parse<'a> for Directive { fn parse(parser: Parser<'a>) -> wast::parser::Result { use wast::kw; - wast::custom_keyword!(anyref_owned); - wast::custom_keyword!(anyref_borrowed); + wast::custom_keyword!(externref_owned); + wast::custom_keyword!(externref_borrowed); wast::custom_keyword!(other); let kind = if parser.peek::() { @@ -234,11 +234,11 @@ impl<'a> Parse<'a> for Directive { parser.parens(|p| { let mut i = 0; while !p.is_empty() { - if parser.peek::() { - parser.parse::()?; + if parser.peek::() { + parser.parse::()?; args.push((i, true)); - } else if parser.peek::() { - parser.parse::()?; + } else if parser.peek::() { + parser.parse::()?; args.push((i, false)); } else { parser.parse::()?; @@ -248,10 +248,10 @@ impl<'a> Parse<'a> for Directive { Ok(()) })?; - let ret_anyref = parser.parse::>()?.is_some(); + let ret_externref = parser.parse::>()?.is_some(); Ok(Directive { args, - ret_anyref, + ret_externref, kind, }) } diff --git a/crates/anyref-xform/tests/anyref-param-owned.wat b/crates/externref-xform/tests/anyref-param-owned.wat similarity index 51% rename from crates/anyref-xform/tests/anyref-param-owned.wat rename to crates/externref-xform/tests/anyref-param-owned.wat index a767c056455..b5649a72003 100644 --- a/crates/anyref-xform/tests/anyref-param-owned.wat +++ b/crates/externref-xform/tests/anyref-param-owned.wat @@ -1,18 +1,18 @@ -;; @xform export "foo" (anyref_owned) +;; @xform export "foo" (externref_owned) (module (func $foo (export "foo") (param i32)) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) - (func $foo anyref shim (type 2) (param anyref) + (type (;2;) (func (param externref))) + (func $foo externref shim (type 2) (param externref) (local i32) call $alloc local.tee 1 @@ -23,6 +23,6 @@ (func $alloc (type 0) (result i32) i32.const 0) (func $foo (type 1) (param i32)) - (table (;0;) 32 anyref) - (export "foo" (func $foo anyref shim))) + (table (;0;) 32 externref) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/anyref-param.wat b/crates/externref-xform/tests/anyref-param.wat similarity index 56% rename from crates/anyref-xform/tests/anyref-param.wat rename to crates/externref-xform/tests/anyref-param.wat index d59fec1d95c..b2ad1b902eb 100644 --- a/crates/anyref-xform/tests/anyref-param.wat +++ b/crates/externref-xform/tests/anyref-param.wat @@ -1,17 +1,17 @@ -;; @xform export "foo" (anyref_borrowed) +;; @xform export "foo" (externref_borrowed) (module (func $foo (export "foo") (param i32)) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (param i32))) - (type (;1;) (func (param anyref))) - (func $foo anyref shim (type 1) (param anyref) + (type (;1;) (func (param externref))) + (func $foo externref shim (type 1) (param externref) (local i32) global.get 0 i32.const 1 @@ -24,14 +24,14 @@ local.get 1 call $foo local.get 1 - ref.null + ref.nullextern table.set 0 local.get 1 i32.const 1 i32.add global.set 0) (func $foo (type 0) (param i32)) - (table (;0;) 32 anyref) + (table (;0;) 32 externref) (global (;0;) (mut i32) (i32.const 32)) - (export "foo" (func $foo anyref shim))) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/clone-ref-intrinsic.wat b/crates/externref-xform/tests/clone-ref-intrinsic.wat similarity index 70% rename from crates/anyref-xform/tests/clone-ref-intrinsic.wat rename to crates/externref-xform/tests/clone-ref-intrinsic.wat index f5befbf590b..401befcd25d 100644 --- a/crates/anyref-xform/tests/clone-ref-intrinsic.wat +++ b/crates/externref-xform/tests/clone-ref-intrinsic.wat @@ -1,4 +1,4 @@ -;; @xform export "foo" (anyref_owned) anyref_owned +;; @xform export "foo" (externref_owned) externref_owned (module (import "__wbindgen_placeholder__" "__wbindgen_object_clone_ref" @@ -6,9 +6,9 @@ (func $foo (export "foo") (param i32) (result i32) local.get 0 call $clone) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: @@ -16,8 +16,8 @@ (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) (type (;2;) (func (param i32) (result i32))) - (type (;3;) (func (param anyref) (result anyref))) - (func $foo anyref shim (type 3) (param anyref) (result anyref) + (type (;3;) (func (param externref) (result externref))) + (func $foo externref shim (type 3) (param externref) (result externref) (local i32) call $alloc local.tee 1 @@ -43,6 +43,6 @@ (func $alloc (type 0) (result i32) i32.const 0) (func $dealloc (type 1) (param i32)) - (table (;0;) 32 anyref) - (export "foo" (func $foo anyref shim))) + (table (;0;) 32 externref) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/drop-ref-intrinsic.wat b/crates/externref-xform/tests/drop-ref-intrinsic.wat similarity index 62% rename from crates/anyref-xform/tests/drop-ref-intrinsic.wat rename to crates/externref-xform/tests/drop-ref-intrinsic.wat index 48a2a5676ac..fec08c6c828 100644 --- a/crates/anyref-xform/tests/drop-ref-intrinsic.wat +++ b/crates/externref-xform/tests/drop-ref-intrinsic.wat @@ -1,4 +1,4 @@ -;; @xform export "foo" (anyref_owned) +;; @xform export "foo" (externref_owned) (module (import "__wbindgen_placeholder__" "__wbindgen_object_drop_ref" @@ -6,17 +6,17 @@ (func $foo (export "foo") (param i32) local.get 0 call $drop) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) - (func $foo anyref shim (type 2) (param anyref) + (type (;2;) (func (param externref))) + (func $foo externref shim (type 2) (param externref) (local i32) call $alloc local.tee 1 @@ -30,6 +30,6 @@ (func $alloc (type 0) (result i32) i32.const 0) (func $dealloc (type 1) (param i32)) - (table (;0;) 32 anyref) - (export "foo" (func $foo anyref shim))) + (table (;0;) 32 externref) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/import-anyref-owned.wat b/crates/externref-xform/tests/import-anyref-owned.wat similarity index 57% rename from crates/anyref-xform/tests/import-anyref-owned.wat rename to crates/externref-xform/tests/import-anyref-owned.wat index 506e7982060..8bb18e1cb52 100644 --- a/crates/anyref-xform/tests/import-anyref-owned.wat +++ b/crates/externref-xform/tests/import-anyref-owned.wat @@ -1,22 +1,22 @@ -;; @xform import "" "a" (anyref_owned) +;; @xform import "" "a" (externref_owned) (module (import "" "a" (func $a (param i32))) (func (export "foo") i32.const 0 call $a) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func)) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) + (type (;2;) (func (param externref))) (import "" "a" (func $a (type 2))) - (func $a anyref shim (type 1) (param i32) + (func $a externref shim (type 1) (param i32) local.get 0 table.get 0 local.get 0 @@ -24,8 +24,8 @@ call $a) (func (;2;) (type 0) i32.const 0 - call $a anyref shim) + call $a externref shim) (func $dealloc (type 1) (param i32)) - (table (;0;) 32 anyref) + (table (;0;) 32 externref) (export "foo" (func 2))) ;) diff --git a/crates/anyref-xform/tests/import-anyref-ret.wat b/crates/externref-xform/tests/import-anyref-ret.wat similarity index 56% rename from crates/anyref-xform/tests/import-anyref-ret.wat rename to crates/externref-xform/tests/import-anyref-ret.wat index 638e6149aba..09f2cec65cf 100644 --- a/crates/anyref-xform/tests/import-anyref-ret.wat +++ b/crates/externref-xform/tests/import-anyref-ret.wat @@ -1,21 +1,21 @@ -;; @xform import "" "a" () anyref_owned +;; @xform import "" "a" () externref_owned (module (import "" "a" (func $a (result i32))) (func (export "foo") (result i32) call $a) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) - (type (;1;) (func (result anyref))) + (type (;1;) (func (result externref))) (import "" "a" (func $a (type 1))) - (func $a anyref shim (type 0) (result i32) - (local i32 anyref) + (func $a externref shim (type 0) (result i32) + (local i32 externref) call $a local.set 1 call $alloc @@ -24,9 +24,9 @@ table.set 0 local.get 0) (func (;2;) (type 0) (result i32) - call $a anyref shim) + call $a externref shim) (func $alloc (type 0) (result i32) i32.const 0) - (table (;0;) 32 anyref) + (table (;0;) 32 externref) (export "foo" (func 2))) ;) diff --git a/crates/anyref-xform/tests/import-anyref.wat b/crates/externref-xform/tests/import-anyref.wat similarity index 52% rename from crates/anyref-xform/tests/import-anyref.wat rename to crates/externref-xform/tests/import-anyref.wat index e2ea26aee37..3da24454cd4 100644 --- a/crates/anyref-xform/tests/import-anyref.wat +++ b/crates/externref-xform/tests/import-anyref.wat @@ -1,28 +1,28 @@ -;; @xform import "" "a" (anyref_borrowed) +;; @xform import "" "a" (externref_borrowed) (module (import "" "a" (func $a (param i32))) (func (export "foo") i32.const 0 call $a) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func)) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) + (type (;2;) (func (param externref))) (import "" "a" (func $a (type 2))) - (func $a anyref shim (type 1) (param i32) + (func $a externref shim (type 1) (param i32) local.get 0 table.get 0 call $a) (func (;2;) (type 0) i32.const 0 - call $a anyref shim) - (table (;0;) 32 anyref) + call $a externref shim) + (table (;0;) 32 externref) (export "foo" (func 2))) ;) diff --git a/crates/anyref-xform/tests/mixed-export.wat b/crates/externref-xform/tests/mixed-export.wat similarity index 62% rename from crates/anyref-xform/tests/mixed-export.wat rename to crates/externref-xform/tests/mixed-export.wat index d6e1ca37ab7..fe4290ab087 100644 --- a/crates/anyref-xform/tests/mixed-export.wat +++ b/crates/externref-xform/tests/mixed-export.wat @@ -1,18 +1,18 @@ -;; @xform export "a" (other anyref_borrowed other anyref_owned other) +;; @xform export "a" (other externref_borrowed other externref_owned other) (module (func $a (export "a") (param f32 i32 i64 i32 i32)) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) (type (;1;) (func (param f32 i32 i64 i32 i32))) - (type (;2;) (func (param f32 anyref i64 anyref i32))) - (func $a anyref shim (type 2) (param f32 anyref i64 anyref i32) + (type (;2;) (func (param f32 externref i64 externref i32))) + (func $a externref shim (type 2) (param f32 externref i64 externref i32) (local i32 i32) global.get 0 i32.const 1 @@ -33,7 +33,7 @@ local.get 4 call $a local.get 5 - ref.null + ref.nullextern table.set 0 local.get 5 i32.const 1 @@ -42,7 +42,7 @@ (func $alloc (type 0) (result i32) i32.const 0) (func $a (type 1) (param f32 i32 i64 i32 i32)) - (table (;0;) 32 anyref) + (table (;0;) 32 externref) (global (;0;) (mut i32) (i32.const 32)) - (export "a" (func $a anyref shim))) + (export "a" (func $a externref shim))) ;) diff --git a/crates/anyref-xform/tests/mixed.wat b/crates/externref-xform/tests/mixed.wat similarity index 64% rename from crates/anyref-xform/tests/mixed.wat rename to crates/externref-xform/tests/mixed.wat index 259ac41e07e..499a08fc7ab 100644 --- a/crates/anyref-xform/tests/mixed.wat +++ b/crates/externref-xform/tests/mixed.wat @@ -1,4 +1,4 @@ -;; @xform import "" "a" (other anyref_borrowed other anyref_owned other) +;; @xform import "" "a" (other externref_borrowed other externref_owned other) (module (import "" "a" (func $a (param f32 i32 i64 i32 i32))) @@ -9,9 +9,9 @@ i32.const 4 i32.const 5 call $a) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: @@ -19,9 +19,9 @@ (type (;0;) (func)) (type (;1;) (func (param i32))) (type (;2;) (func (param f32 i32 i64 i32 i32))) - (type (;3;) (func (param f32 anyref i64 anyref i32))) + (type (;3;) (func (param f32 externref i64 externref i32))) (import "" "a" (func $a (type 3))) - (func $a anyref shim (type 2) (param f32 i32 i64 i32 i32) + (func $a externref shim (type 2) (param f32 i32 i64 i32 i32) local.get 0 local.get 1 table.get 0 @@ -38,8 +38,8 @@ i64.const 3 i32.const 4 i32.const 5 - call $a anyref shim) + call $a externref shim) (func $dealloc (type 1) (param i32)) - (table (;0;) 32 anyref) + (table (;0;) 32 externref) (export "foo" (func 2))) ;) diff --git a/crates/anyref-xform/tests/ret-anyref.wat b/crates/externref-xform/tests/ret-anyref.wat similarity index 51% rename from crates/anyref-xform/tests/ret-anyref.wat rename to crates/externref-xform/tests/ret-anyref.wat index 275e51d9862..f223b059906 100644 --- a/crates/anyref-xform/tests/ret-anyref.wat +++ b/crates/externref-xform/tests/ret-anyref.wat @@ -1,20 +1,20 @@ -;; @xform export "foo" () anyref_owned +;; @xform export "foo" () externref_owned (module (func $foo (export "foo") (result i32) i32.const 0) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) - (type (;1;) (func (result anyref))) + (type (;1;) (func (result externref))) (type (;2;) (func (param i32))) - (func $foo anyref shim (type 1) (result anyref) + (func $foo externref shim (type 1) (result externref) (local i32) call $foo local.tee 0 @@ -24,6 +24,6 @@ (func $foo (type 0) (result i32) i32.const 0) (func $dealloc (type 2) (param i32)) - (table (;0;) 32 anyref) - (export "foo" (func $foo anyref shim))) + (table (;0;) 32 externref) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/table-grow-intrinsic.wat b/crates/externref-xform/tests/table-grow-intrinsic.wat similarity index 52% rename from crates/anyref-xform/tests/table-grow-intrinsic.wat rename to crates/externref-xform/tests/table-grow-intrinsic.wat index b8d08f471be..6f4d3c09934 100644 --- a/crates/anyref-xform/tests/table-grow-intrinsic.wat +++ b/crates/externref-xform/tests/table-grow-intrinsic.wat @@ -1,23 +1,23 @@ -;; @xform export "foo" (anyref_owned) +;; @xform export "foo" (externref_owned) (module - (import "__wbindgen_anyref_xform__" "__wbindgen_anyref_table_grow" + (import "__wbindgen_externref_xform__" "__wbindgen_externref_table_grow" (func $grow (param i32) (result i32))) (func $foo (export "foo") (param i32) i32.const 0 call $grow drop) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) - (func $foo anyref shim (type 2) (param anyref) + (type (;2;) (func (param externref))) + (func $foo externref shim (type 2) (param externref) (local i32) call $alloc local.tee 1 @@ -26,12 +26,12 @@ local.get 1 call $foo) (func $foo (type 1) (param i32) - ref.null + ref.nullextern i32.const 0 table.grow 0 drop) (func $alloc (type 0) (result i32) i32.const 0) - (table (;0;) 32 anyref) - (export "foo" (func $foo anyref shim))) + (table (;0;) 32 externref) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/table-set-null-intrinsic.wat b/crates/externref-xform/tests/table-set-null-intrinsic.wat similarity index 51% rename from crates/anyref-xform/tests/table-set-null-intrinsic.wat rename to crates/externref-xform/tests/table-set-null-intrinsic.wat index 3185489c5eb..494a64f8707 100644 --- a/crates/anyref-xform/tests/table-set-null-intrinsic.wat +++ b/crates/externref-xform/tests/table-set-null-intrinsic.wat @@ -1,22 +1,22 @@ -;; @xform export "foo" (anyref_owned) +;; @xform export "foo" (externref_owned) (module - (import "__wbindgen_anyref_xform__" "__wbindgen_anyref_table_set_null" + (import "__wbindgen_externref_xform__" "__wbindgen_externref_table_set_null" (func $set-null (param i32))) (func $foo (export "foo") (param i32) local.get 0 call $set-null) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) - (func $foo anyref shim (type 2) (param anyref) + (type (;2;) (func (param externref))) + (func $foo externref shim (type 2) (param externref) (local i32) call $alloc local.tee 1 @@ -26,10 +26,10 @@ call $foo) (func $foo (type 1) (param i32) local.get 0 - ref.null + ref.nullextern table.set 0) (func $alloc (type 0) (result i32) i32.const 0) - (table (;0;) 32 anyref) - (export "foo" (func $foo anyref shim))) + (table (;0;) 32 externref) + (export "foo" (func $foo externref shim))) ;) diff --git a/crates/anyref-xform/tests/table.wat b/crates/externref-xform/tests/table.wat similarity index 58% rename from crates/anyref-xform/tests/table.wat rename to crates/externref-xform/tests/table.wat index eb658b67a13..0186c50949e 100644 --- a/crates/anyref-xform/tests/table.wat +++ b/crates/externref-xform/tests/table.wat @@ -1,20 +1,20 @@ -;; @xform table 0 (anyref_owned) +;; @xform table 0 (externref_owned) (module (func $foo (param i32)) (table (export "func") 0 funcref) (elem (i32.const 0) 0) - (func $alloc (export "__anyref_table_alloc") (result i32) + (func $alloc (export "__externref_table_alloc") (result i32) i32.const 0) - (func $dealloc (export "__anyref_table_dealloc") (param i32)) + (func $dealloc (export "__externref_table_dealloc") (param i32)) ) (; CHECK-ALL: (module (type (;0;) (func (result i32))) (type (;1;) (func (param i32))) - (type (;2;) (func (param anyref))) - (func $closure0 anyref shim (type 2) (param anyref) + (type (;2;) (func (param externref))) + (func $closure0 externref shim (type 2) (param externref) (local i32) call $alloc local.tee 1 @@ -26,8 +26,8 @@ i32.const 0) (func $foo (type 1) (param i32)) (table (;0;) 2 funcref) - (table (;1;) 32 anyref) + (table (;1;) 32 externref) (export "func" (table 0)) (elem (;0;) (i32.const 0) func $foo) - (elem (;1;) (i32.const 1) func $closure0 anyref shim)) + (elem (;1;) (i32.const 1) func $closure0 externref shim)) ;) diff --git a/crates/multi-value-xform/Cargo.toml b/crates/multi-value-xform/Cargo.toml index 76a99d90e54..ca0d840f678 100644 --- a/crates/multi-value-xform/Cargo.toml +++ b/crates/multi-value-xform/Cargo.toml @@ -13,12 +13,12 @@ edition = "2018" [dependencies] anyhow = "1.0" -walrus = "0.16.0" +walrus = "0.17.0" [dev-dependencies] rayon = "1.0" wasmprinter = "0.2" -wast = "15.0" +wast = "17.0" wat = "1.0" [[test]] diff --git a/crates/multi-value-xform/src/lib.rs b/crates/multi-value-xform/src/lib.rs index 2777748ece4..072aca16462 100644 --- a/crates/multi-value-xform/src/lib.rs +++ b/crates/multi-value-xform/src/lib.rs @@ -162,12 +162,10 @@ fn xform_one( round_up_to_alignment(results_size, 8) + 8 } walrus::ValType::V128 => round_up_to_alignment(results_size, 16) + 16, - walrus::ValType::Anyref | walrus::ValType::Funcref | walrus::ValType::Nullref => { - anyhow::bail!( - "cannot multi-value transform functions that return \ + walrus::ValType::Externref | walrus::ValType::Funcref => anyhow::bail!( + "cannot multi-value transform functions that return \ reference types, since they can't go into linear memory" - ) - } + ), }; } // Round up to 16-byte alignment, since that's what LLVM's emitted Wasm code @@ -286,9 +284,7 @@ fn xform_one( ); offset += 16; } - walrus::ValType::Anyref | walrus::ValType::Funcref | walrus::ValType::Nullref => { - unreachable!() - } + walrus::ValType::Externref | walrus::ValType::Funcref => unreachable!(), } } diff --git a/crates/threads-xform/Cargo.toml b/crates/threads-xform/Cargo.toml index 1dd4a0399d6..462505ccad2 100644 --- a/crates/threads-xform/Cargo.toml +++ b/crates/threads-xform/Cargo.toml @@ -13,5 +13,5 @@ edition = "2018" [dependencies] anyhow = "1.0" -walrus = "0.16.0" +walrus = "0.17.0" wasm-bindgen-wasm-conventions = { path = "../wasm-conventions", version = "=0.2.62" } diff --git a/crates/threads-xform/src/lib.rs b/crates/threads-xform/src/lib.rs index ccb157ebb3a..f423a6779ad 100644 --- a/crates/threads-xform/src/lib.rs +++ b/crates/threads-xform/src/lib.rs @@ -401,7 +401,7 @@ fn inject_start( match segment.offset { InitExpr::Global(id) => body.global_get(id), InitExpr::Value(v) => body.const_(v), - InitExpr::RefNull | InitExpr::RefFunc(_) => { + InitExpr::RefNull(_) | InitExpr::RefFunc(_) => { panic!("not a valid i32 initializer") } }; diff --git a/crates/wasm-conventions/Cargo.toml b/crates/wasm-conventions/Cargo.toml index ec339329d4a..39f4a87d765 100644 --- a/crates/wasm-conventions/Cargo.toml +++ b/crates/wasm-conventions/Cargo.toml @@ -10,5 +10,5 @@ description = "Utilities for working with Wasm codegen conventions (usually esta edition = "2018" [dependencies] -walrus = "0.16.0" +walrus = "0.17.0" anyhow = "1.0" diff --git a/crates/wasm-interpreter/Cargo.toml b/crates/wasm-interpreter/Cargo.toml index a4dceefb6fa..2e9e88befeb 100644 --- a/crates/wasm-interpreter/Cargo.toml +++ b/crates/wasm-interpreter/Cargo.toml @@ -14,7 +14,7 @@ edition = '2018' [dependencies] anyhow = "1.0" log = "0.4" -walrus = "0.16.0" +walrus = "0.17.0" wasm-bindgen-wasm-conventions = { path = "../wasm-conventions", version = "0.2.62" } [dev-dependencies] diff --git a/crates/webidl/src/idl_type.rs b/crates/webidl/src/idl_type.rs index 8790301bc23..23559145934 100644 --- a/crates/webidl/src/idl_type.rs +++ b/crates/webidl/src/idl_type.rs @@ -496,7 +496,7 @@ impl<'a> IdlType<'a> { /// Converts to syn type if possible. pub(crate) fn to_syn_type(&self, pos: TypePosition) -> Result, TypeError> { - let anyref = |ty| { + let externref = |ty| { Some(match pos { TypePosition::Argument => shared_ref(ty, false), TypePosition::Return => ty, @@ -505,11 +505,11 @@ impl<'a> IdlType<'a> { let js_sys = |name: &str| { let path = vec![rust_ident("js_sys"), rust_ident(name)]; let ty = leading_colon_path_ty(path); - anyref(ty) + externref(ty) }; let js_value = { let path = vec![rust_ident("wasm_bindgen"), rust_ident("JsValue")]; - anyref(leading_colon_path_ty(path)) + externref(leading_colon_path_ty(path)) }; match self { IdlType::Boolean => Ok(Some(ident_ty(raw_ident("bool")))), @@ -565,7 +565,7 @@ impl<'a> IdlType<'a> { | IdlType::Dictionary(name) | IdlType::CallbackInterface { name, .. } => { let ty = ident_ty(rust_ident(camel_case_ident(name).as_str())); - Ok(anyref(ty)) + Ok(externref(ty)) } IdlType::Enum(name) => Ok(Some(ident_ty(rust_ident(camel_case_ident(name).as_str())))), diff --git a/publish.rs b/publish.rs index 2f33aa788dc..66b53508b7c 100644 --- a/publish.rs +++ b/publish.rs @@ -29,7 +29,7 @@ const CRATES_TO_PUBLISH: &[&str] = &[ "wasm-bindgen-wasm-conventions", "wasm-bindgen-threads-xform", "wasm-bindgen-multi-value-xform", - "wasm-bindgen-anyref-xform", + "wasm-bindgen-externref-xform", "wasm-bindgen-cli-support", "wasm-bindgen-cli", "wasm-bindgen", diff --git a/src/closure.rs b/src/closure.rs index eb11e5a9327..e1a2ffd187b 100644 --- a/src/closure.rs +++ b/src/closure.rs @@ -464,7 +464,7 @@ where T: WasmClosure + ?Sized, { fn describe() { - inform(ANYREF); + inform(EXTERNREF); } } @@ -713,8 +713,9 @@ doit! { // duplicate. unsafe impl WasmClosure for dyn Fn(&A) -> R - where A: RefFromWasmAbi, - R: ReturnWasmAbi + 'static, +where + A: RefFromWasmAbi, + R: ReturnWasmAbi + 'static, { fn describe() { #[allow(non_snake_case)] @@ -730,8 +731,7 @@ unsafe impl WasmClosure for dyn Fn(&A) -> R // convert `ret` as it may throw (for `Result`, for // example) let ret = { - let f: *const dyn Fn(&A) -> R = - FatPtr { fields: (a, b) }.ptr; + let f: *const dyn Fn(&A) -> R = FatPtr { fields: (a, b) }.ptr; let arg = ::ref_from_abi(arg); (*f)(&*arg) }; @@ -740,17 +740,14 @@ unsafe impl WasmClosure for dyn Fn(&A) -> R inform(invoke:: as u32); - unsafe extern fn destroy( - a: usize, - b: usize, - ) { + unsafe extern "C" fn destroy(a: usize, b: usize) { // See `Fn()` above for why we simply return if a == 0 { return; } - drop(Box::from_raw(FatPtr:: R> { - fields: (a, b) - }.ptr)); + drop(Box::from_raw( + FatPtr:: R> { fields: (a, b) }.ptr, + )); } inform(destroy:: as u32); @@ -759,8 +756,9 @@ unsafe impl WasmClosure for dyn Fn(&A) -> R } unsafe impl WasmClosure for dyn FnMut(&A) -> R - where A: RefFromWasmAbi, - R: ReturnWasmAbi + 'static, +where + A: RefFromWasmAbi, + R: ReturnWasmAbi + 'static, { fn describe() { #[allow(non_snake_case)] @@ -776,8 +774,7 @@ unsafe impl WasmClosure for dyn FnMut(&A) -> R // convert `ret` as it may throw (for `Result`, for // example) let ret = { - let f: *const dyn FnMut(&A) -> R = - FatPtr { fields: (a, b) }.ptr; + let f: *const dyn FnMut(&A) -> R = FatPtr { fields: (a, b) }.ptr; let f = f as *mut dyn FnMut(&A) -> R; let arg = ::ref_from_abi(arg); (*f)(&*arg) @@ -787,17 +784,14 @@ unsafe impl WasmClosure for dyn FnMut(&A) -> R inform(invoke:: as u32); - unsafe extern fn destroy( - a: usize, - b: usize, - ) { + unsafe extern "C" fn destroy(a: usize, b: usize) { // See `Fn()` above for why we simply return if a == 0 { return; } - drop(Box::from_raw(FatPtr:: R> { - fields: (a, b) - }.ptr)); + drop(Box::from_raw( + FatPtr:: R> { fields: (a, b) }.ptr, + )); } inform(destroy:: as u32); @@ -807,9 +801,10 @@ unsafe impl WasmClosure for dyn FnMut(&A) -> R #[allow(non_snake_case)] impl WasmClosureFnOnce<(&A,), R> for T - where T: 'static + FnOnce(&A) -> R, - A: RefFromWasmAbi + 'static, - R: ReturnWasmAbi + 'static +where + T: 'static + FnOnce(&A) -> R, + A: RefFromWasmAbi + 'static, + R: ReturnWasmAbi + 'static, { type FnMut = dyn FnMut(&A) -> R; @@ -822,8 +817,8 @@ impl WasmClosureFnOnce<(&A,), R> for T } fn into_js_function(self) -> JsValue { - use std::rc::Rc; use crate::__rt::WasmRefCell; + use std::rc::Rc; let mut me = Some(self); diff --git a/src/describe.rs b/src/describe.rs index eec05bd4dbf..2d424f8f3f7 100644 --- a/src/describe.rs +++ b/src/describe.rs @@ -36,8 +36,8 @@ tys! { REFMUT SLICE VECTOR - ANYREF - NAMED_ANYREF + EXTERNREF + NAMED_EXTERNREF ENUM RUST_STRUCT CHAR @@ -78,7 +78,7 @@ simple! { f64 => F64 bool => BOOLEAN char => CHAR - JsValue => ANYREF + JsValue => EXTERNREF } cfg_if! { diff --git a/src/anyref.rs b/src/externref.rs similarity index 84% rename from src/anyref.rs rename to src/externref.rs index 9ec6989735b..ce517eea29d 100644 --- a/src/anyref.rs +++ b/src/externref.rs @@ -1,16 +1,16 @@ +use crate::JsValue; use std::alloc::{self, Layout}; +use std::cell::Cell; use std::mem; use std::ptr; use std::slice; use std::vec::Vec; -use std::cell::Cell; -use crate::JsValue; externs! { - #[link(wasm_import_module = "__wbindgen_anyref_xform__")] + #[link(wasm_import_module = "__wbindgen_externref_xform__")] extern "C" { - fn __wbindgen_anyref_table_grow(delta: usize) -> i32; - fn __wbindgen_anyref_table_set_null(idx: usize) -> (); + fn __wbindgen_externref_table_grow(delta: usize) -> i32; + fn __wbindgen_externref_table_set_null(idx: usize) -> (); } } @@ -34,7 +34,7 @@ impl Slab { if ret == self.data.len() { if self.data.len() == self.data.capacity() { let extra = 128; - let r = unsafe { __wbindgen_anyref_table_grow(extra) }; + let r = unsafe { __wbindgen_externref_table_grow(extra) }; if r == -1 { internal_error("table grow failure") } @@ -121,12 +121,12 @@ fn internal_error(msg: &str) -> ! { } } -// Management of `anyref` is always thread local since an `anyref` value can't -// cross threads in wasm. Indices as a result are always thread-local. +// Management of `externref` is always thread local since an `externref` value +// can't cross threads in wasm. Indices as a result are always thread-local. std::thread_local!(pub static HEAP_SLAB: Cell = Cell::new(Slab::new())); #[no_mangle] -pub extern "C" fn __anyref_table_alloc() -> usize { +pub extern "C" fn __externref_table_alloc() -> usize { HEAP_SLAB .try_with(|slot| { let mut slab = slot.replace(Slab::new()); @@ -138,14 +138,14 @@ pub extern "C" fn __anyref_table_alloc() -> usize { } #[no_mangle] -pub extern "C" fn __anyref_table_dealloc(idx: usize) { +pub extern "C" fn __externref_table_dealloc(idx: usize) { if idx < super::JSIDX_RESERVED as usize { return; } // clear this value from the table so while the table slot is un-allocated // we don't keep around a strong reference to a potentially large object unsafe { - __wbindgen_anyref_table_set_null(idx); + __wbindgen_externref_table_set_null(idx); } HEAP_SLAB .try_with(|slot| { @@ -157,16 +157,16 @@ pub extern "C" fn __anyref_table_dealloc(idx: usize) { } #[no_mangle] -pub unsafe extern "C" fn __anyref_drop_slice(ptr: *mut JsValue, len: usize) { +pub unsafe extern "C" fn __externref_drop_slice(ptr: *mut JsValue, len: usize) { for slot in slice::from_raw_parts_mut(ptr, len) { - __anyref_table_dealloc(slot.idx as usize); + __externref_table_dealloc(slot.idx as usize); } } -// Implementation of `__wbindgen_anyref_heap_live_count` for when we are using -// `anyref` instead of the JS `heap`. +// Implementation of `__wbindgen_externref_heap_live_count` for when we are using +// `externref` instead of the JS `heap`. #[no_mangle] -pub unsafe extern "C" fn __anyref_heap_live_count() -> u32 { +pub unsafe extern "C" fn __externref_heap_live_count() -> u32 { HEAP_SLAB .try_with(|slot| { let slab = slot.replace(Slab::new()); diff --git a/src/lib.rs b/src/lib.rs index 2953a4374a9..d7407397336 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,7 +68,7 @@ if_std! { extern crate std; use std::prelude::v1::*; pub mod closure; - mod anyref; + mod externref; mod cache; pub use cache::intern::{intern, unintern}; @@ -495,7 +495,7 @@ externs! { fn __wbindgen_symbol_named_new(ptr: *const u8, len: usize) -> u32; fn __wbindgen_symbol_anonymous_new() -> u32; - fn __wbindgen_anyref_heap_live_count() -> u32; + fn __wbindgen_externref_heap_live_count() -> u32; fn __wbindgen_is_null(idx: u32) -> u32; fn __wbindgen_is_undefined(idx: u32) -> u32; @@ -659,7 +659,7 @@ pub fn throw_val(s: JsValue) -> ! { } } -/// Get the count of live `anyref`s / `JsValue`s in `wasm-bindgen`'s heap. +/// Get the count of live `externref`s / `JsValue`s in `wasm-bindgen`'s heap. /// /// ## Usage /// @@ -671,7 +671,7 @@ pub fn throw_val(s: JsValue) -> ! { /// * get an initial live count, /// /// * perform some series of operations or function calls that should clean up -/// after themselves, and should not keep holding onto `anyref`s / `JsValue`s +/// after themselves, and should not keep holding onto `externref`s / `JsValue`s /// after completion, /// /// * get the final live count, @@ -680,8 +680,8 @@ pub fn throw_val(s: JsValue) -> ! { /// /// ## What is Counted /// -/// Note that this only counts the *owned* `anyref`s / `JsValue`s that end up in -/// `wasm-bindgen`'s heap. It does not count borrowed `anyref`s / `JsValue`s +/// Note that this only counts the *owned* `externref`s / `JsValue`s that end up in +/// `wasm-bindgen`'s heap. It does not count borrowed `externref`s / `JsValue`s /// that are on its stack. /// /// For example, these `JsValue`s are accounted for: @@ -690,7 +690,7 @@ pub fn throw_val(s: JsValue) -> ! { /// #[wasm_bindgen] /// pub fn my_function(this_is_counted: JsValue) { /// let also_counted = JsValue::from_str("hi"); -/// assert!(wasm_bindgen::anyref_heap_live_count() >= 2); +/// assert!(wasm_bindgen::externref_heap_live_count() >= 2); /// } /// ``` /// @@ -703,8 +703,13 @@ pub fn throw_val(s: JsValue) -> ! { /// // ... /// } /// ``` +pub fn externref_heap_live_count() -> u32 { + unsafe { __wbindgen_externref_heap_live_count() } +} + +#[doc(hidden)] pub fn anyref_heap_live_count() -> u32 { - unsafe { __wbindgen_anyref_heap_live_count() } + externref_heap_live_count() } /// An extension trait for `Option` and `Result` for unwraping the `T` @@ -1055,7 +1060,7 @@ pub mod __rt { /// /// Ideas for how to improve this are most welcome! pub fn link_mem_intrinsics() { - crate::anyref::link_intrinsics(); + crate::externref::link_intrinsics(); } static mut GLOBAL_EXNDATA: [u32; 2] = [0; 2]; diff --git a/tests/headless/anyref_heap_live_count.rs b/tests/headless/anyref_heap_live_count.rs deleted file mode 100644 index 1133f33b454..00000000000 --- a/tests/headless/anyref_heap_live_count.rs +++ /dev/null @@ -1,20 +0,0 @@ -use wasm_bindgen::prelude::*; -use wasm_bindgen_test::*; - -// This test is in the headless suite so that we can test the `anyref` table -// implementation of `anyref_heap_live_count` (as opposed to the JS `heap` -// implementation) in Firefox. -#[wasm_bindgen_test] -fn test_anyref_heap_live_count() { - let initial = wasm_bindgen::anyref_heap_live_count(); - - let after_alloc = { - let _vals: Vec<_> = (0..10).map(JsValue::from).collect(); - wasm_bindgen::anyref_heap_live_count() - }; - - let after_dealloc = wasm_bindgen::anyref_heap_live_count(); - - assert_eq!(initial, after_dealloc); - assert_eq!(initial + 10, after_alloc); -} diff --git a/tests/headless/externref_heap_live_count.rs b/tests/headless/externref_heap_live_count.rs new file mode 100644 index 00000000000..c344febbfa3 --- /dev/null +++ b/tests/headless/externref_heap_live_count.rs @@ -0,0 +1,20 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen_test::*; + +// This test is in the headless suite so that we can test the `externref` table +// implementation of `externref_heap_live_count` (as opposed to the JS `heap` +// implementation) in Firefox. +#[wasm_bindgen_test] +fn test_externref_heap_live_count() { + let initial = wasm_bindgen::externref_heap_live_count(); + + let after_alloc = { + let _vals: Vec<_> = (0..10).map(JsValue::from).collect(); + wasm_bindgen::externref_heap_live_count() + }; + + let after_dealloc = wasm_bindgen::externref_heap_live_count(); + + assert_eq!(initial, after_dealloc); + assert_eq!(initial + 10, after_alloc); +} diff --git a/tests/headless/main.rs b/tests/headless/main.rs index fe9d9caa166..b5f6f12a0c7 100644 --- a/tests/headless/main.rs +++ b/tests/headless/main.rs @@ -47,7 +47,7 @@ pub fn import_export_same_name() { import_export_same_name(); } -pub mod anyref_heap_live_count; +pub mod externref_heap_live_count; pub mod modules; pub mod snippets; pub mod strings; diff --git a/tests/wasm/no_shims.rs b/tests/wasm/no_shims.rs index ac92e41f51a..2f9455ae2e4 100644 --- a/tests/wasm/no_shims.rs +++ b/tests/wasm/no_shims.rs @@ -39,7 +39,7 @@ use wasm_bindgen_test::*; return 42; }; - module.exports.works_when_anyref_support_is_enabled = function (v) { + module.exports.works_when_externref_support_is_enabled = function (v) { assert_eq(v, 'hello'); return v; }; @@ -94,11 +94,11 @@ extern "C" { #[wasm_bindgen(assert_no_shim, js_namespace = MyNamespace)] fn outgoing_namespaced(x: f64); - // Note that this should only skip the JS shim if we have anyref support + // Note that this should only skip the JS shim if we have externref support // enabled. // // #[wasm_bindgen(assert_no_shim)] - fn works_when_anyref_support_is_enabled(v: JsValue) -> JsValue; + fn works_when_externref_support_is_enabled(v: JsValue) -> JsValue; } #[wasm_bindgen_test] @@ -147,6 +147,6 @@ fn no_shims() { assert!(b); let v = JsValue::from("hello"); - let vv = works_when_anyref_support_is_enabled(v.clone()); + let vv = works_when_externref_support_is_enabled(v.clone()); assert_eq!(v, vv); } diff --git a/tests/wasm/simple.rs b/tests/wasm/simple.rs index c9f5e3dbafb..611b5320a8b 100644 --- a/tests/wasm/simple.rs +++ b/tests/wasm/simple.rs @@ -218,10 +218,10 @@ pub fn do_string_roundtrip(s: String) -> String { } #[wasm_bindgen_test] -fn anyref_heap_live_count() { - let x = wasm_bindgen::anyref_heap_live_count(); +fn externref_heap_live_count() { + let x = wasm_bindgen::externref_heap_live_count(); let y = JsValue::null().clone(); - assert!(wasm_bindgen::anyref_heap_live_count() > x); + assert!(wasm_bindgen::externref_heap_live_count() > x); drop(y); - assert_eq!(x, wasm_bindgen::anyref_heap_live_count()); + assert_eq!(x, wasm_bindgen::externref_heap_live_count()); }