Diff
Modified: trunk/JSTests/ChangeLog (275568 => 275569)
--- trunk/JSTests/ChangeLog 2021-04-06 23:20:35 UTC (rev 275568)
+++ trunk/JSTests/ChangeLog 2021-04-06 23:28:53 UTC (rev 275569)
@@ -1,5 +1,15 @@
2021-04-06 Alexey Shvayka <[email protected]>
+ Symbol and BigInt wrapper objects should perform OrdinaryToPrimitive
+ https://bugs.webkit.org/show_bug.cgi?id=224208
+
+ Reviewed by Yusuke Suzuki.
+
+ * stress/bigint-object-ordinary-toprimitive.js: Added.
+ * stress/symbol-object-ordinary-toprimitive.js: Added.
+
+2021-04-06 Alexey Shvayka <[email protected]>
+
Array's toString() is incorrect if join() is non-callable
https://bugs.webkit.org/show_bug.cgi?id=224215
Added: trunk/JSTests/stress/bigint-object-ordinary-toprimitive.js (0 => 275569)
--- trunk/JSTests/stress/bigint-object-ordinary-toprimitive.js (rev 0)
+++ trunk/JSTests/stress/bigint-object-ordinary-toprimitive.js 2021-04-06 23:28:53 UTC (rev 275569)
@@ -0,0 +1,71 @@
+function shouldBe(actual, expected) {
+ if (!Object.is(actual, expected))
+ throw new Error(`Bad value: ${actual}!`);
+}
+
+function shouldThrow(func, expectedError) {
+ let errorThrown = false;
+ try {
+ func();
+ } catch (error) {
+ errorThrown = true;
+ if (error.toString() !== expectedError)
+ throw new Error(`Bad error: ${error}!`);
+ }
+ if (!errorThrown)
+ throw new Error("Didn't throw!");
+}
+
+const BigIntToString = BigInt.prototype.toString;
+let toStringGets = 0;
+let toStringCalls = 0;
+let toStringFunction = function() { ++toStringCalls; return `${BigIntToString.call(this)}foo`; };
+Object.defineProperty(BigInt.prototype, "toString", {
+ get: () => { ++toStringGets; return toStringFunction; },
+});
+
+shouldBe("" + Object(1n), "1"); // hint: default
+shouldThrow(() => { +Object(1n); }, "TypeError: Conversion from 'BigInt' to 'number' is not allowed."); // hint: number
+shouldBe(`${Object(1n)}`, "1foo"); // hint: string
+
+shouldBe(toStringGets, 1);
+shouldBe(toStringCalls, 1);
+
+const BigIntValueOf = BigInt.prototype.valueOf;
+let valueOfGets = 0;
+let valueOfCalls = 0;
+let valueOfFunction = function() { ++valueOfCalls; return BigIntValueOf.call(this) * 2n; };
+Object.defineProperty(BigInt.prototype, "valueOf", {
+ get: () => { ++valueOfGets; return valueOfFunction; },
+});
+
+shouldBe(Object(1n) == 2n, true); // hint: default
+shouldBe(Object(1n) + 1n, 3n); // hint: number
+shouldBe({ "1foo": 1, "2": 2 }[Object(1n)], 1); // hint: string
+
+shouldBe(toStringGets, 2);
+shouldBe(toStringCalls, 2);
+shouldBe(valueOfGets, 2);
+shouldBe(valueOfCalls, 2);
+
+toStringFunction = undefined;
+
+shouldThrow(() => { 1 + Object(1n); }, "TypeError: Invalid mix of BigInt and other type in addition."); // hint: default
+shouldBe(Object(1n) * 1n, 2n); // hint: number
+shouldBe("".concat(Object(1n)), "2"); // hint: string
+
+shouldBe(toStringGets, 3);
+shouldBe(toStringCalls, 2);
+shouldBe(valueOfGets, 5);
+shouldBe(valueOfCalls, 5);
+
+valueOfFunction = null;
+
+shouldThrow(() => { new Date(Object(1n)); }, "TypeError: No default value"); // hint: default
+shouldThrow(() => { Number(Object(1n)); }, "TypeError: No default value"); // hint: number
+shouldThrow(() => { String(Object(1n)); }, "TypeError: No default value"); // hint: string
+
+shouldBe(toStringGets, 6);
+shouldBe(toStringCalls, 2);
+shouldBe(valueOfGets, 8);
+shouldBe(valueOfCalls, 5);
Added: trunk/JSTests/stress/symbol-object-ordinary-toprimitive.js (0 => 275569)
--- trunk/JSTests/stress/symbol-object-ordinary-toprimitive.js (rev 0)
+++ trunk/JSTests/stress/symbol-object-ordinary-toprimitive.js 2021-04-06 23:28:53 UTC (rev 275569)
@@ -0,0 +1,102 @@
+function shouldBe(actual, expected) {
+ if (!Object.is(actual, expected))
+ throw new Error(`Bad value: ${actual}!`);
+}
+
+function shouldThrow(func, expectedError) {
+ let errorThrown = false;
+ try {
+ func();
+ } catch (error) {
+ errorThrown = true;
+ if (error.toString() !== expectedError)
+ throw new Error(`Bad error: ${error}!`);
+ }
+ if (!errorThrown)
+ throw new Error("Didn't throw!");
+}
+
+(function toPrimitiveIsNull() {
+ Object.defineProperty(Symbol.prototype, Symbol.toPrimitive, { value: null });
+
+ for (let i = 0; i < 1e5; ++i) {
+ shouldBe(Object(Symbol()) == "Symbol()", false); // hint: default
+ shouldThrow(() => { +Object(Symbol()); }, "TypeError: Cannot convert a symbol to a number"); // hint: number
+ shouldBe(`${Object(Symbol())}`, "Symbol()"); // hint: string
+ }
+})();
+
+(function toPrimitiveIsUndefined() {
+ Object.defineProperty(Symbol.prototype, Symbol.toPrimitive, { value: undefined });
+
+ for (let i = 0; i < 1e5; ++i) {
+ shouldBe(Object(Symbol.iterator) != Symbol.iterator, false); // hint: default
+ shouldThrow(() => { Object(Symbol()) <= ""; }, "TypeError: Cannot convert a symbol to a number"); // hint: number
+ shouldBe({ "Symbol()": 1 }[Object(Symbol())], 1); // hint: string
+ }
+})();
+
+delete Symbol.prototype[Symbol.toPrimitive];
+
+let valueOfGets = 0;
+let valueOfCalls = 0;
+let valueOfFunction = () => { ++valueOfCalls; return 123; };
+Object.defineProperty(Symbol.prototype, "valueOf", {
+ get: () => { ++valueOfGets; return valueOfFunction; },
+});
+
+shouldBe(Object(Symbol()) == 123, true); // hint: default
+shouldBe(Object(Symbol()) - 0, 123); // hint: number
+shouldBe("".concat(Object(Symbol())), "Symbol()"); // hint: string
+
+shouldBe(valueOfGets, 2);
+shouldBe(valueOfCalls, 2);
+
+let toStringGets = 0;
+let toStringCalls = 0;
+let toStringFunction = () => { ++toStringCalls; return "foo"; };
+Object.defineProperty(Symbol.prototype, "toString", {
+ get: () => { ++toStringGets; return toStringFunction; },
+});
+
+shouldBe("" + Object(Symbol()), "123"); // hint: default
+shouldBe(Object(Symbol()) * 1, 123); // hint: number
+shouldBe({ "123": 1, "Symbol()": 2, "foo": 3 }[Object(Symbol())], 3); // hint: string
+
+shouldBe(valueOfGets, 4);
+shouldBe(valueOfCalls, 4);
+shouldBe(toStringGets, 1);
+shouldBe(toStringCalls, 1);
+
+valueOfFunction = null;
+
+shouldBe(new Date(Object(Symbol())).getTime(), NaN); // hint: default
+shouldBe(+Object(Symbol()), NaN); // hint: number
+shouldBe(`${Object(Symbol())}`, "foo"); // hint: string
+
+shouldBe(valueOfGets, 6);
+shouldBe(valueOfCalls, 4);
+shouldBe(toStringGets, 4);
+shouldBe(toStringCalls, 4);
+
+toStringFunction = function() { throw new Error("toString()"); };
+
+shouldThrow(() => { Object(Symbol()) != 123; }, "Error: toString()"); // hint: default
+shouldThrow(() => { Object(Symbol()) / 0; }, "Error: toString()"); // hint: number
+shouldThrow(() => { "".concat(Object(Symbol())); }, "Error: toString()"); // hint: string
+
+shouldBe(valueOfGets, 8);
+shouldBe(valueOfCalls, 4);
+shouldBe(toStringGets, 7);
+shouldBe(toStringCalls, 4);
+
+toStringFunction = undefined;
+
+shouldThrow(() => { 1 + Object(Symbol()); }, "TypeError: No default value"); // hint: default
+shouldThrow(() => { Number(Object(Symbol())); }, "TypeError: No default value"); // hint: number
+shouldThrow(() => { String(Object(Symbol())); }, "TypeError: No default value"); // hint: string
+
+shouldBe(valueOfGets, 11);
+shouldBe(valueOfCalls, 4);
+shouldBe(toStringGets, 10);
+shouldBe(toStringCalls, 4);
Modified: trunk/Source/_javascript_Core/ChangeLog (275568 => 275569)
--- trunk/Source/_javascript_Core/ChangeLog 2021-04-06 23:20:35 UTC (rev 275568)
+++ trunk/Source/_javascript_Core/ChangeLog 2021-04-06 23:28:53 UTC (rev 275569)
@@ -1,5 +1,31 @@
2021-04-06 Alexey Shvayka <[email protected]>
+ Symbol and BigInt wrapper objects should perform OrdinaryToPrimitive
+ https://bugs.webkit.org/show_bug.cgi?id=224208
+
+ Reviewed by Yusuke Suzuki.
+
+ ES6 introduced Symbol.toPrimitive as the only way to override ToPrimitive;
+ if it's nullish, OrdinaryToPrimitive [1] is performed unconditionally.
+
+ This patch removes two redundant defaultValue() overrides, fixing JSC to call
+ (possibly userland) toString() / valueOf() methods of a) Symbol objects whose
+ Symbol.toPrimitive was removed, and b) BigInt wrapper objects.
+
+ Aligns JSC with V8 and SpiderMonkey. Coercion of primitives is unaffected.
+ Also, removes dummy BigIntObject::internalValue() override.
+
+ [1]: https://tc39.es/ecma262/#sec-toprimitive (step 2.d)
+
+ * runtime/BigIntObject.cpp:
+ (JSC::BigIntObject::defaultValue): Deleted.
+ * runtime/BigIntObject.h:
+ * runtime/SymbolObject.cpp:
+ (JSC::SymbolObject::defaultValue): Deleted.
+ * runtime/SymbolObject.h:
+
+2021-04-06 Alexey Shvayka <[email protected]>
+
Array's toString() is incorrect if join() is non-callable
https://bugs.webkit.org/show_bug.cgi?id=224215
Modified: trunk/Source/_javascript_Core/runtime/BigIntObject.cpp (275568 => 275569)
--- trunk/Source/_javascript_Core/runtime/BigIntObject.cpp 2021-04-06 23:20:35 UTC (rev 275568)
+++ trunk/Source/_javascript_Core/runtime/BigIntObject.cpp 2021-04-06 23:28:53 UTC (rev 275569)
@@ -56,10 +56,4 @@
setInternalValue(vm, bigInt);
}
-JSValue BigIntObject::defaultValue(const JSObject* object, JSGlobalObject*, PreferredPrimitiveType)
-{
- const BigIntObject* bigIntObject = jsCast<const BigIntObject*>(object);
- return bigIntObject->internalValue();
-}
-
} // namespace JSC
Modified: trunk/Source/_javascript_Core/runtime/BigIntObject.h (275568 => 275569)
--- trunk/Source/_javascript_Core/runtime/BigIntObject.h 2021-04-06 23:20:35 UTC (rev 275568)
+++ trunk/Source/_javascript_Core/runtime/BigIntObject.h 2021-04-06 23:28:53 UTC (rev 275569)
@@ -44,15 +44,11 @@
DECLARE_EXPORT_INFO;
- JSValue internalValue() const { return JSWrapperObject::internalValue(); }
-
static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
- static JSValue defaultValue(const JSObject*, JSGlobalObject*, PreferredPrimitiveType);
-
private:
JS_EXPORT_PRIVATE void finishCreation(VM&, JSValue);
JS_EXPORT_PRIVATE BigIntObject(VM&, Structure*);
Modified: trunk/Source/_javascript_Core/runtime/SymbolObject.cpp (275568 => 275569)
--- trunk/Source/_javascript_Core/runtime/SymbolObject.cpp 2021-04-06 23:20:35 UTC (rev 275568)
+++ trunk/Source/_javascript_Core/runtime/SymbolObject.cpp 2021-04-06 23:28:53 UTC (rev 275569)
@@ -47,10 +47,4 @@
setInternalValue(vm, symbol);
}
-JSValue SymbolObject::defaultValue(const JSObject* object, JSGlobalObject*, PreferredPrimitiveType)
-{
- const SymbolObject* symbolObject = jsCast<const SymbolObject*>(object);
- return symbolObject->internalValue();
-}
-
} // namespace JSC
Modified: trunk/Source/_javascript_Core/runtime/SymbolObject.h (275568 => 275569)
--- trunk/Source/_javascript_Core/runtime/SymbolObject.h 2021-04-06 23:20:35 UTC (rev 275568)
+++ trunk/Source/_javascript_Core/runtime/SymbolObject.h 2021-04-06 23:28:53 UTC (rev 275569)
@@ -60,8 +60,6 @@
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
- static JSValue defaultValue(const JSObject*, JSGlobalObject*, PreferredPrimitiveType);
-
private:
JS_EXPORT_PRIVATE void finishCreation(VM&, Symbol*);
JS_EXPORT_PRIVATE SymbolObject(VM&, Structure*);