OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Expects following symbols to be set in the bootstrapper during genesis: | 5 // Expects following symbols to be set in the bootstrapper during genesis: |
6 // - symbolHasInstance | 6 // - symbolHasInstance |
7 // - symbolIsConcatSpreadable | 7 // - symbolIsConcatSpreadable |
8 // - symbolIsRegExp | 8 // - symbolIsRegExp |
9 // - symbolIterator | 9 // - symbolIterator |
| 10 // - symbolToPrimitive |
10 // - symbolToStringTag | 11 // - symbolToStringTag |
11 // - symbolUnscopables | 12 // - symbolUnscopables |
12 | 13 |
13 var $symbolToString; | 14 var $symbolToString; |
14 | 15 |
15 (function(global, utils) { | 16 (function(global, utils) { |
16 | 17 |
17 "use strict"; | 18 "use strict"; |
18 | 19 |
19 %CheckIsBootstrapping(); | 20 %CheckIsBootstrapping(); |
(...skipping 13 matching lines...) Expand all Loading... |
33 | 34 |
34 // ------------------------------------------------------------------- | 35 // ------------------------------------------------------------------- |
35 | 36 |
36 function SymbolConstructor(x) { | 37 function SymbolConstructor(x) { |
37 if (%_IsConstructCall()) throw MakeTypeError(kNotConstructor, "Symbol"); | 38 if (%_IsConstructCall()) throw MakeTypeError(kNotConstructor, "Symbol"); |
38 // NOTE: Passing in a Symbol value will throw on ToString(). | 39 // NOTE: Passing in a Symbol value will throw on ToString(). |
39 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); | 40 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x)); |
40 } | 41 } |
41 | 42 |
42 | 43 |
| 44 // 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint ) |
| 45 function SymbolToPrimitive(hint) { |
| 46 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { |
| 47 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 48 "Symbol.prototype [ @@toPrimitive ]", this); |
| 49 } |
| 50 return %_ValueOf(this); |
| 51 } |
| 52 |
| 53 |
43 function SymbolToString() { | 54 function SymbolToString() { |
44 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { | 55 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) { |
45 throw MakeTypeError(kIncompatibleMethodReceiver, | 56 throw MakeTypeError(kIncompatibleMethodReceiver, |
46 "Symbol.prototype.toString", this); | 57 "Symbol.prototype.toString", this); |
47 } | 58 } |
48 var description = %SymbolDescription(%_ValueOf(this)); | 59 var description = %SymbolDescription(%_ValueOf(this)); |
49 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")"; | 60 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")"; |
50 } | 61 } |
51 | 62 |
52 | 63 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 101 |
91 %SetCode(GlobalSymbol, SymbolConstructor); | 102 %SetCode(GlobalSymbol, SymbolConstructor); |
92 %FunctionSetPrototype(GlobalSymbol, new GlobalObject()); | 103 %FunctionSetPrototype(GlobalSymbol, new GlobalObject()); |
93 | 104 |
94 utils.InstallConstants(GlobalSymbol, [ | 105 utils.InstallConstants(GlobalSymbol, [ |
95 // TODO(rossberg): expose when implemented. | 106 // TODO(rossberg): expose when implemented. |
96 // "hasInstance", symbolHasInstance, | 107 // "hasInstance", symbolHasInstance, |
97 // "isConcatSpreadable", symbolIsConcatSpreadable, | 108 // "isConcatSpreadable", symbolIsConcatSpreadable, |
98 // "isRegExp", symbolIsRegExp, | 109 // "isRegExp", symbolIsRegExp, |
99 "iterator", symbolIterator, | 110 "iterator", symbolIterator, |
| 111 "toPrimitive", symbolToPrimitive, |
100 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js --- | 112 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js --- |
101 // Move here when shipping | 113 // Move here when shipping |
102 // "toStringTag", symbolToStringTag, | 114 // "toStringTag", symbolToStringTag, |
103 "unscopables", symbolUnscopables | 115 "unscopables", symbolUnscopables |
104 ]); | 116 ]); |
105 | 117 |
106 utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [ | 118 utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [ |
107 "for", SymbolFor, | 119 "for", SymbolFor, |
108 "keyFor", SymbolKeyFor | 120 "keyFor", SymbolKeyFor |
109 ]); | 121 ]); |
110 | 122 |
111 %AddNamedProperty( | 123 %AddNamedProperty( |
112 GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM); | 124 GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM); |
| 125 utils.SetFunctionName(SymbolToPrimitive, symbolToPrimitive); |
| 126 %AddNamedProperty( |
| 127 GlobalSymbol.prototype, symbolToPrimitive, SymbolToPrimitive, |
| 128 DONT_ENUM | READ_ONLY); |
113 %AddNamedProperty( | 129 %AddNamedProperty( |
114 GlobalSymbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY); | 130 GlobalSymbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY); |
115 | 131 |
116 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [ | 132 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [ |
117 "toString", SymbolToString, | 133 "toString", SymbolToString, |
118 "valueOf", SymbolValueOf | 134 "valueOf", SymbolValueOf |
119 ]); | 135 ]); |
120 | 136 |
121 utils.InstallFunctions(GlobalObject, DONT_ENUM, [ | 137 utils.InstallFunctions(GlobalObject, DONT_ENUM, [ |
122 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols | 138 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols |
123 ]); | 139 ]); |
124 | 140 |
125 $symbolToString = SymbolToString; | 141 $symbolToString = SymbolToString; |
126 | 142 |
127 }) | 143 }) |
OLD | NEW |