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 var $Symbol = global.Symbol;
13 // -------------------------------------------------------------------
15 function SymbolConstructor(x) {
16 if (%_IsConstructCall()) {
17 throw MakeTypeError('not_constructor', ["Symbol"]);
19 // NOTE: Passing in a Symbol value will throw on ToString().
20 return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x));
24 function SymbolToString() {
25 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
27 'incompatible_method_receiver', ["Symbol.prototype.toString", this]);
29 var description = %SymbolDescription(%_ValueOf(this));
30 return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")";
34 function SymbolValueOf() {
35 if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
37 'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]);
39 return %_ValueOf(this);
43 function InternalSymbol(key) {
44 var internal_registry = %SymbolRegistry().for_intern;
45 if (IS_UNDEFINED(internal_registry[key])) {
46 internal_registry[key] = %CreateSymbol(key);
48 return internal_registry[key];
52 function SymbolFor(key) {
53 key = TO_STRING_INLINE(key);
54 var registry = %SymbolRegistry();
55 if (IS_UNDEFINED(registry.for[key])) {
56 var symbol = %CreateSymbol(key);
57 registry.for[key] = symbol;
58 registry.keyFor[symbol] = key;
60 return registry.for[key];
64 function SymbolKeyFor(symbol) {
65 if (!IS_SYMBOL(symbol)) throw MakeTypeError("not_a_symbol", [symbol]);
66 return %SymbolRegistry().keyFor[symbol];
71 function ObjectGetOwnPropertySymbols(obj) {
74 // TODO(arv): Proxies use a shared trap for String and Symbol keys.
76 return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
80 //-------------------------------------------------------------------
82 var symbolHasInstance = InternalSymbol("Symbol.hasInstance");
83 var symbolIsConcatSpreadable = InternalSymbol("Symbol.isConcatSpreadable");
84 var symbolIsRegExp = InternalSymbol("Symbol.isRegExp");
85 var symbolIterator = InternalSymbol("Symbol.iterator");
86 var symbolToStringTag = InternalSymbol("Symbol.toStringTag");
87 var symbolUnscopables = InternalSymbol("Symbol.unscopables");
90 //-------------------------------------------------------------------
92 function SetUpSymbol() {
93 %CheckIsBootstrapping();
95 %SetCode($Symbol, SymbolConstructor);
96 %FunctionSetPrototype($Symbol, new $Object());
98 InstallConstants($Symbol, $Array(
99 // TODO(rossberg): expose when implemented.
100 // "hasInstance", symbolHasInstance,
101 // "isConcatSpreadable", symbolIsConcatSpreadable,
102 // "isRegExp", symbolIsRegExp,
103 "iterator", symbolIterator,
104 // TODO(dslomov, caitp): Currently defined in harmony-tostring.js ---
105 // Move here when shipping
106 // "toStringTag", symbolToStringTag,
107 "unscopables", symbolUnscopables
109 InstallFunctions($Symbol, DONT_ENUM, $Array(
111 "keyFor", SymbolKeyFor
114 %AddNamedProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM);
116 $Symbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY);
117 InstallFunctions($Symbol.prototype, DONT_ENUM, $Array(
118 "toString", SymbolToString,
119 "valueOf", SymbolValueOf
126 function ExtendObject() {
127 %CheckIsBootstrapping();
129 InstallFunctions($Object, DONT_ENUM, $Array(
130 "getOwnPropertySymbols", ObjectGetOwnPropertySymbols