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) {
72 if (!IS_SPEC_OBJECT(obj)) {
73 throw MakeTypeError("called_on_non_object",
74 ["Object.getOwnPropertySymbols"]);
77 // TODO(arv): Proxies use a shared trap for String and Symbol keys.
79 return ObjectGetOwnPropertyKeys(obj, true);
83 //-------------------------------------------------------------------
85 var symbolCreate = InternalSymbol("Symbol.create");
86 var symbolHasInstance = InternalSymbol("Symbol.hasInstance");
87 var symbolIsConcatSpreadable = InternalSymbol("Symbol.isConcatSpreadable");
88 var symbolIsRegExp = InternalSymbol("Symbol.isRegExp");
89 var symbolIterator = InternalSymbol("Symbol.iterator");
90 var symbolToStringTag = InternalSymbol("Symbol.toStringTag");
91 var symbolUnscopables = InternalSymbol("Symbol.unscopables");
94 //-------------------------------------------------------------------
96 function SetUpSymbol() {
97 %CheckIsBootstrapping();
99 %SetCode($Symbol, SymbolConstructor);
100 %FunctionSetPrototype($Symbol, new $Object());
102 InstallConstants($Symbol, $Array(
103 "create", symbolCreate,
104 "hasInstance", symbolHasInstance,
105 "isConcatSpreadable", symbolIsConcatSpreadable,
106 "isRegExp", symbolIsRegExp,
107 "iterator", symbolIterator,
108 "toStringTag", symbolToStringTag,
109 "unscopables", symbolUnscopables
111 InstallFunctions($Symbol, DONT_ENUM, $Array(
113 "keyFor", SymbolKeyFor
116 %SetProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM);
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