From a1464d9e004a43ef6d3f2fa83444e7906d9b620f Mon Sep 17 00:00:00 2001 From: Thomas Lively <7121787+tlively@users.noreply.github.com> Date: Mon, 25 Jul 2022 17:15:08 -0700 Subject: Update reference type shorthands in binary output (#4828) Add support for emitting the string type reference shorthands, which had previously been omitted accidentally due to the `default` case in that switch. Also avoid emitting shorthands for non-nullable reference types as a first step towards transitioning the shorthands to represent nullable types instead. Not emitting these shorthands at all will give V8 the flexibility it needs to change its interpretation of the shorthands without breaking any workflows using Binaryen. --- src/wasm/wasm-binary.cpp | 57 ++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index edcf5f7bf..9f28c00bb 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1342,32 +1342,37 @@ void WasmBinaryWriter::writeInlineBuffer(const char* data, size_t size) { void WasmBinaryWriter::writeType(Type type) { if (type.isRef()) { auto heapType = type.getHeapType(); - if (heapType.isBasic()) { - if (type.isNullable()) { - switch (heapType.getBasic()) { - case HeapType::any: - o << S32LEB(BinaryConsts::EncodedType::anyref); - return; - case HeapType::func: - o << S32LEB(BinaryConsts::EncodedType::funcref); - return; - case HeapType::eq: - o << S32LEB(BinaryConsts::EncodedType::eqref); - return; - default: - break; - } - } else { - switch (heapType.getBasic()) { - case HeapType::i31: - o << S32LEB(BinaryConsts::EncodedType::i31ref); - return; - case HeapType::data: - o << S32LEB(BinaryConsts::EncodedType::dataref); - return; - default: - break; - } + if (heapType.isBasic() && type.isNullable()) { + switch (heapType.getBasic()) { + case HeapType::any: + o << S32LEB(BinaryConsts::EncodedType::anyref); + return; + case HeapType::func: + o << S32LEB(BinaryConsts::EncodedType::funcref); + return; + case HeapType::eq: + o << S32LEB(BinaryConsts::EncodedType::eqref); + return; + case HeapType::i31: + // TODO: Emit i31ref once V8 (and Binaryen itself) treats it as + // nullable. + break; + case HeapType::data: + // TODO: Emit dataref once V8 (and Binaryen itself) treats it as + // nullable. + break; + case HeapType::string: + o << S32LEB(BinaryConsts::EncodedType::stringref); + return; + case HeapType::stringview_wtf8: + o << S32LEB(BinaryConsts::EncodedType::stringview_wtf8); + return; + case HeapType::stringview_wtf16: + o << S32LEB(BinaryConsts::EncodedType::stringview_wtf16); + return; + case HeapType::stringview_iter: + o << S32LEB(BinaryConsts::EncodedType::stringview_iter); + return; } } if (type.isNullable()) { -- cgit v1.2.3