600d4fd9ed6b2473259b8ba82cf818de61550bab
[platform/upstream/v8.git] / src / symbol.js
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.
4
5 (function(global, utils) {
6
7 "use strict";
8
9 %CheckIsBootstrapping();
10
11 // -------------------------------------------------------------------
12 // Imports
13
14 var GlobalObject = global.Object;
15 var GlobalSymbol = global.Symbol;
16 var hasInstanceSymbol = utils.ImportNow("has_instance_symbol");
17 var isConcatSpreadableSymbol =
18     utils.ImportNow("is_concat_spreadable_symbol");
19 var isRegExpSymbol = utils.ImportNow("is_regexp_symbol");
20 var iteratorSymbol = utils.ImportNow("iterator_symbol");
21 var ObjectGetOwnPropertyKeys;
22 var toPrimitiveSymbol = utils.ImportNow("to_primitive_symbol");
23 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
24 var unscopablesSymbol = utils.ImportNow("unscopables_symbol");
25
26 utils.Import(function(from) {
27   ObjectGetOwnPropertyKeys = from.ObjectGetOwnPropertyKeys;
28 });
29
30 // -------------------------------------------------------------------
31
32 // 19.4.3.4 Symbol.prototype [ @@toPrimitive ] ( hint )
33 function SymbolToPrimitive(hint) {
34   if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
35     throw MakeTypeError(kIncompatibleMethodReceiver,
36                         "Symbol.prototype [ @@toPrimitive ]", this);
37   }
38   return %_ValueOf(this);
39 }
40
41
42 function SymbolToString() {
43   if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
44     throw MakeTypeError(kIncompatibleMethodReceiver,
45                         "Symbol.prototype.toString", this);
46   }
47   return %SymbolDescriptiveString(%_ValueOf(this));
48 }
49
50
51 function SymbolValueOf() {
52   if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
53     throw MakeTypeError(kIncompatibleMethodReceiver,
54                         "Symbol.prototype.valueOf", this);
55   }
56   return %_ValueOf(this);
57 }
58
59
60 function SymbolFor(key) {
61   key = TO_STRING_INLINE(key);
62   var registry = %SymbolRegistry();
63   if (IS_UNDEFINED(registry.for[key])) {
64     var symbol = %CreateSymbol(key);
65     registry.for[key] = symbol;
66     registry.keyFor[symbol] = key;
67   }
68   return registry.for[key];
69 }
70
71
72 function SymbolKeyFor(symbol) {
73   if (!IS_SYMBOL(symbol)) throw MakeTypeError(kSymbolKeyFor, symbol);
74   return %SymbolRegistry().keyFor[symbol];
75 }
76
77
78 // ES6 19.1.2.8
79 function ObjectGetOwnPropertySymbols(obj) {
80   obj = TO_OBJECT(obj);
81
82   // TODO(arv): Proxies use a shared trap for String and Symbol keys.
83
84   return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
85 }
86
87 // -------------------------------------------------------------------
88
89 %FunctionSetPrototype(GlobalSymbol, new GlobalObject());
90
91 utils.InstallConstants(GlobalSymbol, [
92   // TODO(rossberg): expose when implemented.
93   // "hasInstance", hasInstanceSymbol,
94   // "isConcatSpreadable", isConcatSpreadableSymbol,
95   // "isRegExp", isRegExpSymbol,
96   "iterator", iteratorSymbol,
97   "toPrimitive", toPrimitiveSymbol,
98   // TODO(dslomov, caitp): Currently defined in harmony-tostring.js ---
99   // Move here when shipping
100   // "toStringTag", toStringTagSymbol,
101   "unscopables", unscopablesSymbol,
102 ]);
103
104 utils.InstallFunctions(GlobalSymbol, DONT_ENUM, [
105   "for", SymbolFor,
106   "keyFor", SymbolKeyFor
107 ]);
108
109 %AddNamedProperty(
110     GlobalSymbol.prototype, "constructor", GlobalSymbol, DONT_ENUM);
111 %AddNamedProperty(
112     GlobalSymbol.prototype, toStringTagSymbol, "Symbol", DONT_ENUM | READ_ONLY);
113
114 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM | READ_ONLY, [
115   toPrimitiveSymbol, SymbolToPrimitive
116 ]);
117
118 utils.InstallFunctions(GlobalSymbol.prototype, DONT_ENUM, [
119   "toString", SymbolToString,
120   "valueOf", SymbolValueOf
121 ]);
122
123 utils.InstallFunctions(GlobalObject, DONT_ENUM, [
124   "getOwnPropertySymbols", ObjectGetOwnPropertySymbols
125 ]);
126
127 // -------------------------------------------------------------------
128 // Exports
129
130 utils.Export(function(to) {
131   to.SymbolToString = SymbolToString;
132 })
133
134 })