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 // - symbolToPrimitive
11 // - symbolToStringTag
12 // - symbolUnscopables
16 (function(global, utils) {
20 %CheckIsBootstrapping();
22 // -------------------------------------------------------------------
25 var GlobalObject = global.Object;
26 var GlobalSymbol = global.Symbol;
27 var ObjectGetOwnPropertyKeys;
30 utils.Import(function(from) {
31 ObjectGetOwnPropertyKeys = from.ObjectGetOwnPropertyKeys;
32 ToString = from.ToString;
35 // -------------------------------------------------------------------
37 function SymbolConstructor(x) {
38 if (%_IsConstructCall()) throw MakeTypeError(kNotConstructor, "Symbol");
39 // NOTE: Passing in a Symbol value will throw on ToString().
40 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x));
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);
50 return %_ValueOf(this);
54 function SymbolToString() {
55 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
56 throw MakeTypeError(kIncompatibleMethodReceiver,
57 "Symbol.prototype.toString", this);
59 var description = %SymbolDescription(%_ValueOf(this));
60 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")";
64 function SymbolValueOf() {
65 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
66 throw MakeTypeError(kIncompatibleMethodReceiver,
67 "Symbol.prototype.valueOf", this);
69 return %_ValueOf(this);
73 function SymbolFor(key) {
74 key = TO_STRING_INLINE(key);
75 var registry = %SymbolRegistry();
76 if (IS_UNDEFINED(registry.for[key])) {
77 var symbol = %CreateSymbol(key);
78 registry.for[key] = symbol;
79 registry.keyFor[symbol] = key;
81 return registry.for[key];
85 function SymbolKeyFor(symbol) {
86 if (!IS_SYMBOL(symbol)) throw MakeTypeError(kSymbolKeyFor, symbol);
87 return %SymbolRegistry().keyFor[symbol];
92 function ObjectGetOwnPropertySymbols(obj) {
95 // TODO(arv): Proxies use a shared trap for String and Symbol keys.
97 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
100 //-------------------------------------------------------------------
102 %SetCode(GlobalSymbol, SymbolConstructor);
103 %FunctionSetPrototype(GlobalSymbol, new GlobalObject());
105 utils.InstallConstants(GlobalSymbol, [
106 // TODO(rossberg): expose when implemented.
107 // "hasInstance", symbolHasInstance,
108 // "isConcatSpreadable", symbolIsConcatSpreadable,
109 // "isRegExp", symbolIsRegExp,
110 "iterator", symbolIterator,
111 "toPrimitive", symbolToPrimitive,
112 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js ---
113 // Move here when shipping
114 // "toStringTag", symbolToStringTag,
115 "unscopables", symbolUnscopables
118 utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [
120 "keyFor", SymbolKeyFor
124 GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM);
125 utils.SetFunctionName(SymbolToPrimitive, symbolToPrimitive);
127 GlobalSymbol.prototype, symbolToPrimitive, SymbolToPrimitive,
128 DONT_ENUM | READ_ONLY);
130 GlobalSymbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY);
132 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [
133 "toString", SymbolToString,
134 "valueOf", SymbolValueOf
137 utils.InstallFunctions(GlobalObject, DONT_ENUM, [
138 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols
141 $symbolToString = SymbolToString;