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 // Expects following symbols to be set in the bootstrapper during genesis:
7 // - symbolIsConcatSpreadable
10 // - symbolToStringTag
11 // - symbolUnscopables
15 (function(global, utils) {
19 %CheckIsBootstrapping();
21 // -------------------------------------------------------------------
24 var GlobalObject = global.Object;
25 var GlobalSymbol = global.Symbol;
26 var ObjectGetOwnPropertyKeys;
29 utils.Import(function(from) {
30 ObjectGetOwnPropertyKeys = from.ObjectGetOwnPropertyKeys;
31 ToString = from.ToString;
34 // -------------------------------------------------------------------
36 function SymbolConstructor(x) {
37 if (%_IsConstructCall()) throw MakeTypeError(kNotConstructor, "Symbol");
38 // NOTE: Passing in a Symbol value will throw on ToString().
39 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x));
43 function SymbolToString() {
44 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
45 throw MakeTypeError(kIncompatibleMethodReceiver,
46 "Symbol.prototype.toString", this);
48 var description = %SymbolDescription(%_ValueOf(this));
49 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")";
53 function SymbolValueOf() {
54 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
55 throw MakeTypeError(kIncompatibleMethodReceiver,
56 "Symbol.prototype.valueOf", this);
58 return %_ValueOf(this);
62 function SymbolFor(key) {
63 key = TO_STRING_INLINE(key);
64 var registry = %SymbolRegistry();
65 if (IS_UNDEFINED(registry.for[key])) {
66 var symbol = %CreateSymbol(key);
67 registry.for[key] = symbol;
68 registry.keyFor[symbol] = key;
70 return registry.for[key];
74 function SymbolKeyFor(symbol) {
75 if (!IS_SYMBOL(symbol)) throw MakeTypeError(kSymbolKeyFor, symbol);
76 return %SymbolRegistry().keyFor[symbol];
81 function ObjectGetOwnPropertySymbols(obj) {
84 // TODO(arv): Proxies use a shared trap for String and Symbol keys.
86 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
89 //-------------------------------------------------------------------
91 %SetCode(GlobalSymbol, SymbolConstructor);
92 %FunctionSetPrototype(GlobalSymbol, new GlobalObject());
94 utils.InstallConstants(GlobalSymbol, [
95 // TODO(rossberg): expose when implemented.
96 // "hasInstance", symbolHasInstance,
97 // "isConcatSpreadable", symbolIsConcatSpreadable,
98 // "isRegExp", symbolIsRegExp,
99 "iterator", symbolIterator,
100 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js ---
101 // Move here when shipping
102 // "toStringTag", symbolToStringTag,
103 "unscopables", symbolUnscopables
106 utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [
108 "keyFor", SymbolKeyFor
112 GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM);
114 GlobalSymbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY);
116 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [
117 "toString", SymbolToString,
118 "valueOf", SymbolValueOf
121 utils.InstallFunctions(GlobalObject, DONT_ENUM, [
122 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols
125 $symbolToString = SymbolToString;