Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / 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 "use strict";
6
7 // This file relies on the fact that the following declaration has been made
8 // in runtime.js:
9 // var $Array = global.Array;
10
11 var $Symbol = global.Symbol;
12
13 // -------------------------------------------------------------------
14
15 function SymbolConstructor(x) {
16   if (%_IsConstructCall()) {
17     throw MakeTypeError('not_constructor', ["Symbol"]);
18   }
19   // NOTE: Passing in a Symbol value will throw on ToString().
20   return %CreateSymbol(IS_UNDEFINED(x) ? x : ToString(x));
21 }
22
23
24 function SymbolToString() {
25   if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
26     throw MakeTypeError(
27       'incompatible_method_receiver', ["Symbol.prototype.toString", this]);
28   }
29   var description = %SymbolDescription(%_ValueOf(this));
30   return "Symbol(" + (IS_UNDEFINED(description) ? "" : description) + ")";
31 }
32
33
34 function SymbolValueOf() {
35   if (!(IS_SYMBOL(this) || IS_SYMBOL_WRAPPER(this))) {
36     throw MakeTypeError(
37       'incompatible_method_receiver', ["Symbol.prototype.valueOf", this]);
38   }
39   return %_ValueOf(this);
40 }
41
42
43 function InternalSymbol(key) {
44   var internal_registry = %SymbolRegistry().for_intern;
45   if (IS_UNDEFINED(internal_registry[key])) {
46     internal_registry[key] = %CreateSymbol(key);
47   }
48   return internal_registry[key];
49 }
50
51
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;
59   }
60   return registry.for[key];
61 }
62
63
64 function SymbolKeyFor(symbol) {
65   if (!IS_SYMBOL(symbol)) throw MakeTypeError("not_a_symbol", [symbol]);
66   return %SymbolRegistry().keyFor[symbol];
67 }
68
69
70 // ES6 19.1.2.8
71 function ObjectGetOwnPropertySymbols(obj) {
72   obj = ToObject(obj);
73
74   // TODO(arv): Proxies use a shared trap for String and Symbol keys.
75
76   return ObjectGetOwnPropertyKeys(obj, PROPERTY_ATTRIBUTES_STRING);
77 }
78
79
80 //-------------------------------------------------------------------
81
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");
88
89
90 //-------------------------------------------------------------------
91
92 function SetUpSymbol() {
93   %CheckIsBootstrapping();
94
95   %SetCode($Symbol, SymbolConstructor);
96   %FunctionSetPrototype($Symbol, new $Object());
97
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
108   ));
109   InstallFunctions($Symbol, DONT_ENUM, $Array(
110     "for", SymbolFor,
111     "keyFor", SymbolKeyFor
112   ));
113
114   %AddNamedProperty($Symbol.prototype, "constructor", $Symbol, DONT_ENUM);
115   %AddNamedProperty(
116       $Symbol.prototype, symbolToStringTag, "Symbol", DONT_ENUM | READ_ONLY);
117   InstallFunctions($Symbol.prototype, DONT_ENUM, $Array(
118     "toString", SymbolToString,
119     "valueOf", SymbolValueOf
120   ));
121 }
122
123 SetUpSymbol();
124
125
126 function ExtendObject() {
127   %CheckIsBootstrapping();
128
129   InstallFunctions($Object, DONT_ENUM, $Array(
130     "getOwnPropertySymbols", ObjectGetOwnPropertySymbols
131   ));
132 }
133
134 ExtendObject();