Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / collection.js
1 // Copyright 2012 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 $Set = global.Set;
12 var $Map = global.Map;
13
14 // Global sentinel to be used instead of undefined keys, which are not
15 // supported internally but required for Harmony sets and maps.
16 var undefined_sentinel = {};
17
18
19 // Map and Set uses SameValueZero which means that +0 and -0 should be treated
20 // as the same value.
21 function NormalizeKey(key) {
22   if (IS_UNDEFINED(key)) {
23     return undefined_sentinel;
24   }
25
26   if (key === 0) {
27     return 0;
28   }
29
30   return key;
31 }
32
33
34 // -------------------------------------------------------------------
35 // Harmony Set
36
37 function SetConstructor() {
38   if (%_IsConstructCall()) {
39     %SetInitialize(this);
40   } else {
41     throw MakeTypeError('constructor_not_function', ['Set']);
42   }
43 }
44
45
46 function SetAdd(key) {
47   if (!IS_SET(this)) {
48     throw MakeTypeError('incompatible_method_receiver',
49                         ['Set.prototype.add', this]);
50   }
51   return %SetAdd(this, NormalizeKey(key));
52 }
53
54
55 function SetHas(key) {
56   if (!IS_SET(this)) {
57     throw MakeTypeError('incompatible_method_receiver',
58                         ['Set.prototype.has', this]);
59   }
60   return %SetHas(this, NormalizeKey(key));
61 }
62
63
64 function SetDelete(key) {
65   if (!IS_SET(this)) {
66     throw MakeTypeError('incompatible_method_receiver',
67                         ['Set.prototype.delete', this]);
68   }
69   key = NormalizeKey(key);
70   if (%SetHas(this, key)) {
71     %SetDelete(this, key);
72     return true;
73   } else {
74     return false;
75   }
76 }
77
78
79 function SetGetSize() {
80   if (!IS_SET(this)) {
81     throw MakeTypeError('incompatible_method_receiver',
82                         ['Set.prototype.size', this]);
83   }
84   return %SetGetSize(this);
85 }
86
87
88 function SetClear() {
89   if (!IS_SET(this)) {
90     throw MakeTypeError('incompatible_method_receiver',
91                         ['Set.prototype.clear', this]);
92   }
93   %SetClear(this);
94 }
95
96
97 function SetForEach(f, receiver) {
98   if (!IS_SET(this)) {
99     throw MakeTypeError('incompatible_method_receiver',
100                         ['Set.prototype.forEach', this]);
101   }
102
103   if (!IS_SPEC_FUNCTION(f)) {
104     throw MakeTypeError('called_non_callable', [f]);
105   }
106
107   var iterator = %SetCreateIterator(this, ITERATOR_KIND_VALUES);
108   var entry;
109   try {
110     while (!(entry = %SetIteratorNext(iterator)).done) {
111       %_CallFunction(receiver, entry.value, entry.value, this, f);
112     }
113   } finally {
114     %SetIteratorClose(iterator);
115   }
116 }
117
118
119 // -------------------------------------------------------------------
120
121 function SetUpSet() {
122   %CheckIsBootstrapping();
123
124   %SetCode($Set, SetConstructor);
125   %FunctionSetPrototype($Set, new $Object());
126   %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
127
128   %FunctionSetLength(SetForEach, 1);
129
130   // Set up the non-enumerable functions on the Set prototype object.
131   InstallGetter($Set.prototype, "size", SetGetSize);
132   InstallFunctions($Set.prototype, DONT_ENUM, $Array(
133     "add", SetAdd,
134     "has", SetHas,
135     "delete", SetDelete,
136     "clear", SetClear,
137     "forEach", SetForEach
138   ));
139 }
140
141 SetUpSet();
142
143
144 // -------------------------------------------------------------------
145 // Harmony Map
146
147 function MapConstructor() {
148   if (%_IsConstructCall()) {
149     %MapInitialize(this);
150   } else {
151     throw MakeTypeError('constructor_not_function', ['Map']);
152   }
153 }
154
155
156 function MapGet(key) {
157   if (!IS_MAP(this)) {
158     throw MakeTypeError('incompatible_method_receiver',
159                         ['Map.prototype.get', this]);
160   }
161   return %MapGet(this, NormalizeKey(key));
162 }
163
164
165 function MapSet(key, value) {
166   if (!IS_MAP(this)) {
167     throw MakeTypeError('incompatible_method_receiver',
168                         ['Map.prototype.set', this]);
169   }
170   return %MapSet(this, NormalizeKey(key), value);
171 }
172
173
174 function MapHas(key) {
175   if (!IS_MAP(this)) {
176     throw MakeTypeError('incompatible_method_receiver',
177                         ['Map.prototype.has', this]);
178   }
179   return %MapHas(this, NormalizeKey(key));
180 }
181
182
183 function MapDelete(key) {
184   if (!IS_MAP(this)) {
185     throw MakeTypeError('incompatible_method_receiver',
186                         ['Map.prototype.delete', this]);
187   }
188   return %MapDelete(this, NormalizeKey(key));
189 }
190
191
192 function MapGetSize() {
193   if (!IS_MAP(this)) {
194     throw MakeTypeError('incompatible_method_receiver',
195                         ['Map.prototype.size', this]);
196   }
197   return %MapGetSize(this);
198 }
199
200
201 function MapClear() {
202   if (!IS_MAP(this)) {
203     throw MakeTypeError('incompatible_method_receiver',
204                         ['Map.prototype.clear', this]);
205   }
206   %MapClear(this);
207 }
208
209
210 function MapForEach(f, receiver) {
211   if (!IS_MAP(this)) {
212     throw MakeTypeError('incompatible_method_receiver',
213                         ['Map.prototype.forEach', this]);
214   }
215
216   if (!IS_SPEC_FUNCTION(f)) {
217     throw MakeTypeError('called_non_callable', [f]);
218   }
219
220   var iterator = %MapCreateIterator(this, ITERATOR_KIND_ENTRIES);
221   var entry;
222   try {
223     while (!(entry = %MapIteratorNext(iterator)).done) {
224       %_CallFunction(receiver, entry.value[1], entry.value[0], this, f);
225     }
226   } finally {
227     %MapIteratorClose(iterator);
228   }
229 }
230
231
232 // -------------------------------------------------------------------
233
234 function SetUpMap() {
235   %CheckIsBootstrapping();
236
237   %SetCode($Map, MapConstructor);
238   %FunctionSetPrototype($Map, new $Object());
239   %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
240
241   %FunctionSetLength(MapForEach, 1);
242
243   // Set up the non-enumerable functions on the Map prototype object.
244   InstallGetter($Map.prototype, "size", MapGetSize);
245   InstallFunctions($Map.prototype, DONT_ENUM, $Array(
246     "get", MapGet,
247     "set", MapSet,
248     "has", MapHas,
249     "delete", MapDelete,
250     "clear", MapClear,
251     "forEach", MapForEach
252   ));
253 }
254
255 SetUpMap();