Skip to content

Commit 61e4635

Browse files
Ian Hallidaytcare
authored andcommitted
Fix incorrect error when calling non-existent symbol property
We threw 'Object does not support ToString' when trying to call a symbol property on an object when that property didn't exist. This happened because we try to do a toString conversion on the index var for the error message string, but Symbol objects do not support toString via JavascriptConversion::ToString(). Fix it by special casing symbol properties and directly calling the static JavascriptSymbol::ToString method to get a useful string. Fixes #1409
1 parent 0cdf92d commit 61e4635

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/Runtime/Language/JavascriptOperators.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4038,7 +4038,15 @@ namespace Js
40384038

40394039
if (!hasProperty)
40404040
{
4041-
JavascriptString* varName = JavascriptConversion::ToString(index, scriptContext);
4041+
JavascriptString* varName = nullptr;
4042+
if (indexType == IndexType_PropertyId && propertyRecord != nullptr && propertyRecord->IsSymbol())
4043+
{
4044+
varName = JavascriptSymbol::ToString(propertyRecord, scriptContext);
4045+
}
4046+
else
4047+
{
4048+
varName = JavascriptConversion::ToString(index, scriptContext);
4049+
}
40424050

40434051
// ES5 11.2.3 #2: We evaluate the call target but don't throw yet if target member is missing. We need to evaluate argList
40444052
// first (#3). Postpone throwing error to invoke time.

test/es6/ES6Symbol.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,17 @@ var tests = [
961961
assert.areEqual('Symbol()', Symbol(undefined).toString(), 'Symbol(undefined).toString() === "Symbol()"');
962962
assert.areEqual('Symbol()', Symbol("").toString(), 'Symbol("").toString() === "Symbol()"');
963963
}
964-
}
964+
},
965+
{
966+
name: 'Calling symbol property on object that does not exist should give appropriate error',
967+
body: function () {
968+
// https://github.com/microsoft/ChakraCore/issues/1409
969+
var o = Object.create(null);
970+
assert.throws(function () { o[Symbol()](); }, TypeError, "Calling non-existent symbol property with no description fails", "Object doesn't support property or method 'Symbol()'");
971+
assert.throws(function () { o[Symbol('foo')](); }, TypeError, "Calling non-existent symbol property with description fails", "Object doesn't support property or method 'Symbol(foo)'");
972+
assert.throws(function () { o[Symbol.iterator](); }, TypeError, "Calling non-existent built-in symbol property with description fails", "Object doesn't support property or method 'Symbol(Symbol.iterator)'");
973+
}
974+
},
965975
];
966976

967977
testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });

0 commit comments

Comments
 (0)