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
3 // found in the LICENSE file.
5 (function(global, utils) {
9 %CheckIsBootstrapping();
11 // -------------------------------------------------------------------
14 var GlobalObject = global.Object;
15 var GlobalSymbol = global.Symbol;
16 var hasInstanceSymbol = utils.ImportNow("has_instance_symbol");
17 var isConcatSpreadableSymbol =
18 utils.ImportNow("is_concat_spreadable_symbol");
19 var isRegExpSymbol = utils.ImportNow("is_regexp_symbol");
20 var iteratorSymbol = utils.ImportNow("iterator_symbol");
21 var ObjectGetOwnPropertyKeys;
22 var toPrimitiveSymbol = utils.ImportNow("to_primitive_symbol");
23 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
24 var unscopablesSymbol = utils.ImportNow("unscopables_symbol");
26 utils.Import(function(from) {
27 ObjectGetOwnPropertyKeys = from.ObjectGetOwnPropertyKeys;
30 // -------------------------------------------------------------------
32 // 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint )
33 function SymbolToPrimitive(hint) {
34 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
35 throw MakeTypeError(kIncompatibleMethodReceiver,
36 "Symbol.prototype [ @@toPrimitive ]", this);
38 return %_ValueOf(this);
42 function SymbolToString() {
43 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
44 throw MakeTypeError(kIncompatibleMethodReceiver,
45 "Symbol.prototype.toString", this);
47 return %SymbolDescriptiveString(%_ValueOf(this));
51 function SymbolValueOf() {
52 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
53 throw MakeTypeError(kIncompatibleMethodReceiver,
54 "Symbol.prototype.valueOf", this);
56 return %_ValueOf(this);
60 function SymbolFor(key) {
61 key = TO_STRING_INLINE(key);
62 var registry = %SymbolRegistry();
63 if (IS_UNDEFINED(registry.for[key])) {
64 var symbol = %CreateSymbol(key);
65 registry.for[key] = symbol;
66 registry.keyFor[symbol] = key;
68 return registry.for[key];
72 function SymbolKeyFor(symbol) {
73 if (!IS_SYMBOL(symbol)) throw MakeTypeError(kSymbolKeyFor, symbol);
74 return %SymbolRegistry().keyFor[symbol];
79 function ObjectGetOwnPropertySymbols(obj) {
82 // TODO(arv): Proxies use a shared trap for String and Symbol keys.
84 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
87 // -------------------------------------------------------------------
89 %FunctionSetPrototype(GlobalSymbol, new GlobalObject());
91 utils.InstallConstants(GlobalSymbol, [
92 // TODO(rossberg): expose when implemented.
93 // "hasInstance", hasInstanceSymbol,
94 // "isConcatSpreadable", isConcatSpreadableSymbol,
95 // "isRegExp", isRegExpSymbol,
96 "iterator", iteratorSymbol,
97 "toPrimitive", toPrimitiveSymbol,
98 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js ---
99 // Move here when shipping
100 // "toStringTag", toStringTagSymbol,
101 "unscopables", unscopablesSymbol,
104 utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [
106 "keyFor", SymbolKeyFor
110 GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM);
112 GlobalSymbol.prototype, toStringTagSymbol, "Symbol", DONT_ENUM | READ_ONLY);
114 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM | READ_ONLY, [
115 toPrimitiveSymbol, SymbolToPrimitive
118 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [
119 "toString", SymbolToString,
120 "valueOf", SymbolValueOf
123 utils.InstallFunctions(GlobalObject, DONT_ENUM, [
124 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols
127 // -------------------------------------------------------------------
130 utils.Export(function(to) {
131 to.SymbolToString = SymbolToString;