OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 12 matching lines...) Expand all Loading... |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 "use strict"; | 28 "use strict"; |
29 | 29 |
30 var $Symbol = global.Symbol; | 30 var $Symbol = global.Symbol; |
31 | 31 |
32 function SymbolConstructor(x) { | 32 function SymbolConstructor(x) { |
33 var value = IS_SYMBOL(x) ? x : %CreateSymbol(); | 33 var value = |
| 34 IS_SYMBOL(x) ? x : %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); |
34 if (%_IsConstructCall()) { | 35 if (%_IsConstructCall()) { |
35 %_SetValueOf(this, value); | 36 %_SetValueOf(this, value); |
36 } else { | 37 } else { |
37 return value; | 38 return value; |
38 } | 39 } |
39 } | 40 } |
40 | 41 |
| 42 function SymbolGetName() { |
| 43 var symbol = IS_SYMBOL_WRAPPER(this) ? %_ValueOf(this) : this; |
| 44 if (!IS_SYMBOL(symbol)) { |
| 45 throw MakeTypeError( |
| 46 'incompatible_method_receiver', ["Symbol.prototype.name", this]); |
| 47 |
| 48 } |
| 49 return %SymbolName(symbol); |
| 50 } |
| 51 |
41 function SymbolToString() { | 52 function SymbolToString() { |
42 throw new $TypeError("Symbol.prototype.toString is not allowed"); | 53 throw new $TypeError("Symbol.prototype.toString is not allowed"); |
43 } | 54 } |
44 | 55 |
45 function SymbolValueOf() { | 56 function SymbolValueOf() { |
46 // NOTE: Both Symbol objects and values can enter here as | 57 // NOTE: Both Symbol objects and values can enter here as |
47 // 'this'. This is not as dictated by ECMA-262. | 58 // 'this'. This is not as dictated by ECMA-262. |
48 if (!IS_SYMBOL(this) && !IS_SYMBOL_WRAPPER(this)) { | 59 if (!IS_SYMBOL(this) && !IS_SYMBOL_WRAPPER(this)) { |
49 throw new $TypeError("Symbol.prototype.valueOf is not generic"); | 60 throw new $TypeError("Symbol.prototype.valueOf is not generic"); |
50 } | 61 } |
51 return %_ValueOf(this); | 62 return %_ValueOf(this); |
52 } | 63 } |
53 | 64 |
54 //------------------------------------------------------------------- | 65 //------------------------------------------------------------------- |
55 | 66 |
56 function SetUpSymbol() { | 67 function SetUpSymbol() { |
57 %CheckIsBootstrapping(); | 68 %CheckIsBootstrapping(); |
58 | 69 |
59 %SetCode($Symbol, SymbolConstructor); | 70 %SetCode($Symbol, SymbolConstructor); |
60 %FunctionSetPrototype($Symbol, new $Symbol()); | 71 %FunctionSetPrototype($Symbol, new $Symbol()); |
61 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); | 72 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM); |
62 | 73 |
| 74 InstallGetter($Symbol.prototype, "name", SymbolGetName); |
63 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( | 75 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array( |
64 "toString", SymbolToString, | 76 "toString", SymbolToString, |
65 "valueOf", SymbolValueOf | 77 "valueOf", SymbolValueOf |
66 )); | 78 )); |
67 } | 79 } |
68 | 80 |
69 SetUpSymbol(); | 81 SetUpSymbol(); |
OLD | NEW |