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.
7 // This file relies on the fact that the following declaration has been made
9 // var $Array = global.Array;
11 // And requires following symbols to be set in the bootstrapper during genesis:
12 // - symbolHasInstance
13 // - symbolIsConcatSpreadable
16 // - symbolToStringTag
17 // - symbolUnscopables
19 var $Symbol = global.Symbol;
21 // -------------------------------------------------------------------
23 function SymbolConstructor(x) {
24 if (%_IsConstructCall()) {
25 throw MakeTypeError('not_constructor', ["Symbol"]);
27 // NOTE: Passing in a Symbol value will throw on ToString().
28 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x));
32 function SymbolToString() {
33 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
35 'incompatible_method_receiver', ["Symbol.prototype.toString", this]);
37 var description = %SymbolDescription(%_ValueOf(this));
38 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")";
42 function SymbolValueOf() {
43 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
45 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]);
47 return %_ValueOf(this);
51 function SymbolFor(key) {
52 key = TO_STRING_INLINE(key);
53 var registry = %SymbolRegistry();
54 if (IS_UNDEFINED(registry.for[key])) {
55 var symbol = %CreateSymbol(key);
56 registry.for[key] = symbol;
57 registry.keyFor[symbol] = key;
59 return registry.for[key];
63 function SymbolKeyFor(symbol) {
64 if (!IS_SYMBOL(symbol)) throw MakeTypeError("not_a_symbol", [symbol]);
65 return %SymbolRegistry().keyFor[symbol];
70 function ObjectGetOwnPropertySymbols(obj) {
73 // TODO(arv): Proxies use a shared trap for String and Symbol keys.
75 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
78 //-------------------------------------------------------------------
80 function SetUpSymbol() {
81 %CheckIsBootstrapping();
83 %SetCode($Symbol, SymbolConstructor);
84 %FunctionSetPrototype($Symbol, new $Object());
86 InstallConstants($Symbol, $Array(
87 // TODO(rossberg): expose when implemented.
88 // "hasInstance", symbolHasInstance,
89 // "isConcatSpreadable", symbolIsConcatSpreadable,
90 // "isRegExp", symbolIsRegExp,
91 "iterator", symbolIterator,
92 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js ---
93 // Move here when shipping
94 // "toStringTag", symbolToStringTag,
95 "unscopables", symbolUnscopables
97 InstallFunctions($Symbol, DONT_ENUM, $Array(
99 "keyFor", SymbolKeyFor
102 %AddNamedProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM);
104 $Symbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY);
105 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array(
106 "toString", SymbolToString,
107 "valueOf", SymbolValueOf
114 function ExtendObject() {
115 %CheckIsBootstrapping();
117 InstallFunctions($Object, DONT_ENUM, $Array(
118 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols