From ce1ee2d2f595627b6a63aa9c1bca3a0e5416b4a9 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 7 Feb 2020 11:44:07 -0800 Subject: [PATCH] Enable `ref.func` global initializers (#1380) * Fix comment referencing an outdated instruction name * cranelift-wasm: Enable `ref.func` global initializers --- cranelift/wasm/src/sections_translator.rs | 3 +++ cranelift/wasm/src/translation_utils.rs | 4 +++- cranelift/wasmtests/ref-func-0.wat | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 cranelift/wasmtests/ref-func-0.wat diff --git a/cranelift/wasm/src/sections_translator.rs b/cranelift/wasm/src/sections_translator.rs index 008b9b7134..f4c5079a6e 100644 --- a/cranelift/wasm/src/sections_translator.rs +++ b/cranelift/wasm/src/sections_translator.rs @@ -224,6 +224,9 @@ pub fn parse_global_section( GlobalInit::V128Const(V128Imm::from(value.bytes().to_vec().as_slice())) } Operator::RefNull => GlobalInit::RefNullConst, + Operator::RefFunc { function_index } => { + GlobalInit::RefFunc(FuncIndex::from_u32(function_index)) + } Operator::GlobalGet { global_index } => { GlobalInit::GetGlobal(GlobalIndex::from_u32(global_index)) } diff --git a/cranelift/wasm/src/translation_utils.rs b/cranelift/wasm/src/translation_utils.rs index 796cc5e49b..cd19ed820f 100644 --- a/cranelift/wasm/src/translation_utils.rs +++ b/cranelift/wasm/src/translation_utils.rs @@ -81,10 +81,12 @@ pub enum GlobalInit { F64Const(u64), /// A `vconst`. V128Const(V128Imm), - /// A `get_global` of another global. + /// A `global.get` of another global. GetGlobal(GlobalIndex), /// A `ref.null`. RefNullConst, + /// A `ref.func `. + RefFunc(FuncIndex), ///< The global is imported from, and thus initialized by, a different module. Import, } diff --git a/cranelift/wasmtests/ref-func-0.wat b/cranelift/wasmtests/ref-func-0.wat new file mode 100644 index 0000000000..5a6a6a3b68 --- /dev/null +++ b/cranelift/wasmtests/ref-func-0.wat @@ -0,0 +1,12 @@ +(module + (func $imported (import "env" "f") (param i32) (result i32)) + (func $local (result anyref anyref funcref funcref) + global.get 0 + global.get 1 + global.get 2 + global.get 3) + + (global (export "anyref-imported") anyref (ref.func $imported)) + (global (export "anyref-local") anyref (ref.func $local)) + (global (export "funcref-imported") funcref (ref.func $imported)) + (global (export "funcref-local") funcref (ref.func $local)))