| 1 |
// Copyright (C) 2016 the V8 project authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
/*--- |
| 4 |
esid: sec-symbol-constructor |
| 5 |
es6id: 19.4.1 |
| 6 |
description: The first argument is coerced to a String value (from an object) |
| 7 |
info: | |
| 8 |
1. If NewTarget is not undefined, throw a TypeError exception. |
| 9 |
2. If description is undefined, let descString be undefined. |
| 10 |
2. Else, let descString be ? ToString(description). |
| 11 |
3. Return a new unique Symbol value whose [[Description]] value is |
| 12 |
descString. |
| 13 |
features: [Symbol] |
| 14 |
---*/ |
| 15 |
|
| 16 |
var calls, val; |
| 17 |
|
| 18 |
val = { |
| 19 |
toString: function() { |
| 20 |
calls += 'toString'; |
| 21 |
return {}; |
| 22 |
}, |
| 23 |
valueOf: function() { |
| 24 |
calls += 'valueOf'; |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
calls = ''; |
| 29 |
Symbol(val); |
| 30 |
assert.sameValue(calls, 'toStringvalueOf'); |
| 31 |
|
| 32 |
val = { |
| 33 |
toString: function() { |
| 34 |
calls += 'toString'; |
| 35 |
}, |
| 36 |
valueOf: function() { |
| 37 |
calls += 'valueOf'; |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
calls = ''; |
| 42 |
Symbol(val); |
| 43 |
assert.sameValue(calls, 'toString'); |
| 44 |
|
| 45 |
val = { |
| 46 |
toString: null, |
| 47 |
valueOf: function() { |
| 48 |
calls += 'valueOf'; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
calls = ''; |
| 53 |
Symbol(val); |
| 54 |
assert.sameValue(calls, 'valueOf'); |
| 55 |
|
| 56 |
val = { |
| 57 |
toString: null, |
| 58 |
valueOf: function() { |
| 59 |
calls += 'valueOf'; |
| 60 |
return {}; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
calls = ''; |
| 65 |
assert.throws(TypeError, function() { |
| 66 |
Symbol(val); |
| 67 |
}, '`toString` is not callable, and `valueOf` returns a non-primitive value'); |
| 68 |
assert.sameValue( |
| 69 |
calls, 'valueOf', 'invocation pattern for non-callable `toString`' |
| 70 |
); |
| 71 |
|
| 72 |
val = { |
| 73 |
toString: function() { |
| 74 |
calls += 'toString'; |
| 75 |
return {}; |
| 76 |
}, |
| 77 |
valueOf: function() { |
| 78 |
calls += 'valueOf'; |
| 79 |
return {}; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
calls = ''; |
| 84 |
assert.throws(TypeError, function() { |
| 85 |
Symbol(val); |
| 86 |
}, '`toString` nor `valueOf` both return non-primitive values'); |
| 87 |
assert.sameValue( |
| 88 |
calls, |
| 89 |
'toStringvalueOf', |
| 90 |
'invocation pattern for non-callable `toString` and `valueOf`' |
| 91 |
); |