From e73ea68a99cdebae482c1337709ad2d9d3fd3908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bargull?= Date: Thu, 17 Aug 2017 15:45:27 +0200 Subject: [PATCH] Bug 1383393 - Also ignore null as Symbol.toPrimitive method. r=jandem --HG-- extra : rebase_source : c8a11f2152d5f4c1cb7729d521cbceb43f0be37a --- js/src/jsobj.cpp | 2 +- .../Symbol/toPrimitive-undefined-or-null.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 js/src/tests/ecma_6/Symbol/toPrimitive-undefined-or-null.js diff --git a/js/src/jsobj.cpp b/js/src/jsobj.cpp index ba363baa3cd4..abacceb870ef 100644 --- a/js/src/jsobj.cpp +++ b/js/src/jsobj.cpp @@ -3151,7 +3151,7 @@ js::ToPrimitiveSlow(JSContext* cx, JSType preferredType, MutableHandleValue vp) return false; // Step 6. - if (!method.isUndefined()) { + if (!method.isNullOrUndefined()) { // Step 6 of GetMethod. js::Call() below would do this check and throw a // TypeError anyway, but this produces a better error message. if (!IsCallable(method)) diff --git a/js/src/tests/ecma_6/Symbol/toPrimitive-undefined-or-null.js b/js/src/tests/ecma_6/Symbol/toPrimitive-undefined-or-null.js new file mode 100644 index 000000000000..f43642e0f89c --- /dev/null +++ b/js/src/tests/ecma_6/Symbol/toPrimitive-undefined-or-null.js @@ -0,0 +1,18 @@ +for (let method of [undefined, null]) { + let obj = { + [Symbol.toPrimitive]: method, + toString: () => "pass", + }; + assertEq("" + obj, "pass"); +} + +for (let method of [true, false, 0, 123, "", "abc", Symbol(), {}]) { + let obj = { + [Symbol.toPrimitive]: method, + toString: () => "pass", + }; + assertThrowsInstanceOf(() => "" + obj, TypeError); +} + +if (typeof reportCompare === "function") + reportCompare(0, 0);