Skip to content

Commit

Permalink
Fix incorrect error when calling non-existent symbol property
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ian Halliday authored and tcare committed Nov 1, 2016
1 parent 0cdf92d commit 61e4635
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/Runtime/Language/JavascriptOperators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4038,7 +4038,15 @@ namespace Js

if (!hasProperty)
{
JavascriptString* varName = JavascriptConversion::ToString(index, scriptContext);
JavascriptString* varName = nullptr;
if (indexType == IndexType_PropertyId && propertyRecord != nullptr && propertyRecord->IsSymbol())
{
varName = JavascriptSymbol::ToString(propertyRecord, scriptContext);
}
else
{
varName = JavascriptConversion::ToString(index, scriptContext);
}

// 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
// first (#3). Postpone throwing error to invoke time.
Expand Down
12 changes: 11 additions & 1 deletion test/es6/ES6Symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,17 @@ var tests = [
assert.areEqual('Symbol()', Symbol(undefined).toString(), 'Symbol(undefined).toString() === "Symbol()"');
assert.areEqual('Symbol()', Symbol("").toString(), 'Symbol("").toString() === "Symbol()"');
}
}
},
{
name: 'Calling symbol property on object that does not exist should give appropriate error',
body: function () {
// https://github.com/microsoft/ChakraCore/issues/1409
var o = Object.create(null);
assert.throws(function () { o[Symbol()](); }, TypeError, "Calling non-existent symbol property with no description fails", "Object doesn't support property or method 'Symbol()'");
assert.throws(function () { o[Symbol('foo')](); }, TypeError, "Calling non-existent symbol property with description fails", "Object doesn't support property or method 'Symbol(foo)'");
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)'");
}
},
];

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

0 comments on commit 61e4635

Please sign in to comment.