diff --git a/js/src/wasm/WasmJS.cpp b/js/src/wasm/WasmJS.cpp index ba50d40850cb..11de6abb1d43 100644 --- a/js/src/wasm/WasmJS.cpp +++ b/js/src/wasm/WasmJS.cpp @@ -1133,7 +1133,7 @@ static JSString* UTF8CharsToString(JSContext* cx, const char* chars) { const ValTypeVector& valTypes) { Rooted arrayObj(cx, NewDenseEmptyArray(cx)); for (ValType valType : valTypes) { - RootedString type(cx, UTF8CharsToString(cx, ToJSAPIString(valType).get())); + RootedString type(cx, UTF8CharsToString(cx, ToString(valType).get())); if (!type) { return nullptr; } @@ -1170,8 +1170,7 @@ static JSObject* TableTypeToObject(JSContext* cx, RefType type, uint32_t initial, Maybe maximum) { Rooted props(cx, IdValueVector(cx)); - RootedString elementType(cx, - UTF8CharsToString(cx, ToJSAPIString(type).get())); + RootedString elementType(cx, UTF8CharsToString(cx, ToString(type).get())); if (!elementType || !props.append(IdValuePair(NameToId(cx->names().element), StringValue(elementType)))) { ReportOutOfMemory(cx); @@ -1258,7 +1257,7 @@ static JSObject* GlobalTypeToObject(JSContext* cx, ValType type, return nullptr; } - RootedString valueType(cx, UTF8CharsToString(cx, ToJSAPIString(type).get())); + RootedString valueType(cx, UTF8CharsToString(cx, ToString(type).get())); if (!valueType || !props.append(IdValuePair(NameToId(cx->names().value), StringValue(valueType)))) { ReportOutOfMemory(cx); diff --git a/js/src/wasm/WasmValType.cpp b/js/src/wasm/WasmValType.cpp index f76f9da8df1f..eb856dbdfa5a 100644 --- a/js/src/wasm/WasmValType.cpp +++ b/js/src/wasm/WasmValType.cpp @@ -86,20 +86,44 @@ bool wasm::ToRefType(JSContext* cx, JSLinearString* typeLinearStr, return true; } -#ifdef ENABLE_WASM_TYPE_REFLECTIONS - -UniqueChars wasm::ToJSAPIString(RefType type) { - return ToJSAPIString(ValType(type)); -} - -UniqueChars wasm::ToJSAPIString(ValType type) { - if (type.kind() == ValType::Ref && type.refTypeKind() == RefType::Func) { - return JS_smprintf("anyfunc"); +UniqueChars wasm::ToString(RefType type) { + // Try to emit a shorthand version first + if (type.isNullable() && !type.isTypeIndex()) { + const char* literal = nullptr; + switch (type.kind()) { + case RefType::Func: + literal = "funcref"; + break; + case RefType::Extern: + literal = "externref"; + break; + case RefType::Eq: + literal = "eqref"; + break; + case RefType::TypeIndex: + MOZ_ASSERT_UNREACHABLE(); + } + return DuplicateString(literal); } - return ToString(type); -} -#endif + // Emit the full reference type with heap type + const char* heapType = nullptr; + switch (type.kind()) { + case RefType::Func: + heapType = "func"; + break; + case RefType::Extern: + heapType = "extern"; + break; + case RefType::Eq: + heapType = "eq"; + break; + case RefType::TypeIndex: + return JS_smprintf("(ref %s%d)", type.isNullable() ? "null " : "", + type.typeIndex()); + } + return JS_smprintf("(ref %s%s)", type.isNullable() ? "null " : "", heapType); +} UniqueChars wasm::ToString(ValType type) { const char* literal = nullptr; @@ -120,47 +144,14 @@ UniqueChars wasm::ToString(ValType type) { literal = "f64"; break; case ValType::Ref: - if (type.isNullable() && !type.isTypeIndex()) { - switch (type.refTypeKind()) { - case RefType::Func: - literal = "funcref"; - break; - case RefType::Extern: - literal = "externref"; - break; - case RefType::Eq: - literal = "eqref"; - break; - case RefType::TypeIndex: - MOZ_ASSERT_UNREACHABLE(); - } - } else { - const char* heapType = nullptr; - switch (type.refTypeKind()) { - case RefType::Func: - heapType = "func"; - break; - case RefType::Extern: - heapType = "extern"; - break; - case RefType::Eq: - heapType = "eq"; - break; - case RefType::TypeIndex: - return JS_smprintf("(ref %s%d)", type.isNullable() ? "null " : "", - type.refType().typeIndex()); - } - return JS_smprintf("(ref %s%s)", type.isNullable() ? "null " : "", - heapType); - } - break; + return ToString(type.refType()); case ValType::Rtt: if (!type.hasRttDepth()) { return JS_smprintf("(rtt %d)", type.typeIndex()); } return JS_smprintf("(rtt %d %d)", type.rttDepth(), type.typeIndex()); } - return JS_smprintf("%s", literal); + return DuplicateString(literal); } UniqueChars wasm::ToString(const Maybe& type) { diff --git a/js/src/wasm/WasmValType.h b/js/src/wasm/WasmValType.h index 3736ada269ea..c6ac2693562c 100644 --- a/js/src/wasm/WasmValType.h +++ b/js/src/wasm/WasmValType.h @@ -748,16 +748,6 @@ extern UniqueChars ToString(ValType type); extern UniqueChars ToString(const Maybe& type); -#ifdef ENABLE_WASM_TYPE_REFLECTIONS - -// The JS API sometimes uses different names ("anyfunc" rather than "funcref"). - -extern UniqueChars ToJSAPIString(RefType type); - -extern UniqueChars ToJSAPIString(ValType type); - -#endif - } // namespace wasm } // namespace js diff --git a/testing/web-platform/tests/wasm/jsapi/global/type.tentative.any.js b/testing/web-platform/tests/wasm/jsapi/global/type.tentative.any.js index 173af647f27d..95adc2af0f68 100644 --- a/testing/web-platform/tests/wasm/jsapi/global/type.tentative.any.js +++ b/testing/web-platform/tests/wasm/jsapi/global/type.tentative.any.js @@ -50,12 +50,12 @@ test(() => { }, "externref, immutable") test(() => { - assert_type({"value": "anyfunc", "mutable": true}) -}, "anyfunc, mutable") + assert_type({"value": "funcref", "mutable": true}) +}, "funcref, mutable") test(() => { - assert_type({"value": "anyfunc", "mutable": false}) -}, "anyfunc, immutable") + assert_type({"value": "funcref", "mutable": false}) +}, "funcref, immutable") test(() => { const myglobal = new WebAssembly.Global({"value": "i32", "mutable": true}); diff --git a/testing/web-platform/tests/wasm/jsapi/table/type.tentative.any.js b/testing/web-platform/tests/wasm/jsapi/table/type.tentative.any.js index 596e10b6bf54..ef1ceecb17d6 100644 --- a/testing/web-platform/tests/wasm/jsapi/table/type.tentative.any.js +++ b/testing/web-platform/tests/wasm/jsapi/table/type.tentative.any.js @@ -10,17 +10,17 @@ function assert_type(argument) { } test(() => { - assert_type({ "minimum": 0, "element": "anyfunc"}); + assert_type({ "minimum": 0, "element": "funcref"}); }, "Zero initial, no maximum"); test(() => { - assert_type({ "minimum": 5, "element": "anyfunc" }); + assert_type({ "minimum": 5, "element": "funcref" }); }, "Non-zero initial, no maximum"); test(() => { - assert_type({ "minimum": 0, "maximum": 0, "element": "anyfunc" }); + assert_type({ "minimum": 0, "maximum": 0, "element": "funcref" }); }, "Zero maximum"); test(() => { - assert_type({ "minimum": 0, "maximum": 5, "element": "anyfunc" }); + assert_type({ "minimum": 0, "maximum": 5, "element": "funcref" }); }, "Non-zero maximum");