blob: 0d8dd77f7bd3be944ffcd25b51544d8ebe740c00 [file] [log] [blame]
jkummerow@chromium.org7a6fc812012-06-27 11:12:38 +00001// Copyright 2012 the V8 project authors. All rights reserved.
rossberg@chromium.org34849642014-04-29 16:30:47 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +00005"use strict";
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00006
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00007// This file relies on the fact that the following declaration has been made
8// in runtime.js:
9// var $Array = global.Array;
10
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000011var $Set = global.Set;
12var $Map = global.Map;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000013
machenbach@chromium.org05150ab2014-01-29 08:13:29 +000014
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000015// -------------------------------------------------------------------
16// Harmony Set
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000017
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000018function SetConstructor() {
19 if (%_IsConstructCall()) {
20 %SetInitialize(this);
21 } else {
danno@chromium.orgd3c42102013-08-01 16:58:23 +000022 throw MakeTypeError('constructor_not_function', ['Set']);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000023 }
24}
25
26
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +000027function SetAddJS(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000028 if (!IS_SET(this)) {
29 throw MakeTypeError('incompatible_method_receiver',
30 ['Set.prototype.add', this]);
31 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +000032 return %SetAdd(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000033}
34
35
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +000036function SetHasJS(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000037 if (!IS_SET(this)) {
38 throw MakeTypeError('incompatible_method_receiver',
39 ['Set.prototype.has', this]);
40 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +000041 return %SetHas(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000042}
43
44
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +000045function SetDeleteJS(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000046 if (!IS_SET(this)) {
47 throw MakeTypeError('incompatible_method_receiver',
48 ['Set.prototype.delete', this]);
49 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +000050 return %SetDelete(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000051}
52
53
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +000054function SetGetSizeJS() {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000055 if (!IS_SET(this)) {
56 throw MakeTypeError('incompatible_method_receiver',
57 ['Set.prototype.size', this]);
58 }
59 return %SetGetSize(this);
60}
61
62
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +000063function SetClearJS() {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000064 if (!IS_SET(this)) {
65 throw MakeTypeError('incompatible_method_receiver',
66 ['Set.prototype.clear', this]);
67 }
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +000068 %SetClear(this);
69}
70
71
72function SetForEach(f, receiver) {
73 if (!IS_SET(this)) {
74 throw MakeTypeError('incompatible_method_receiver',
75 ['Set.prototype.forEach', this]);
76 }
77
78 if (!IS_SPEC_FUNCTION(f)) {
79 throw MakeTypeError('called_non_callable', [f]);
80 }
81
machenbach@chromium.org196eb602014-06-04 00:06:13 +000082 var iterator = new SetIterator(this, ITERATOR_KIND_VALUES);
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +000083 var entry;
machenbach@chromium.org79d07042014-05-22 00:04:38 +000084 var stepping = %_DebugCallbackSupportsStepping(f);
machenbach@chromium.org6a4d3942014-05-21 00:04:49 +000085 while (!(entry = %SetIteratorNext(iterator)).done) {
machenbach@chromium.org79d07042014-05-22 00:04:38 +000086 if (stepping) %DebugPrepareStepInIfStepping(f);
machenbach@chromium.org6a4d3942014-05-21 00:04:49 +000087 %_CallFunction(receiver, entry.value, entry.value, this, f);
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +000088 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +000089}
90
91
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000092// -------------------------------------------------------------------
93
94function SetUpSet() {
95 %CheckIsBootstrapping();
96
97 %SetCode($Set, SetConstructor);
98 %FunctionSetPrototype($Set, new $Object());
99 %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);
100
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000101 %FunctionSetLength(SetForEach, 1);
102
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000103 // Set up the non-enumerable functions on the Set prototype object.
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000104 InstallGetter($Set.prototype, "size", SetGetSizeJS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000105 InstallFunctions($Set.prototype, DONT_ENUM, $Array(
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000106 "add", SetAddJS,
107 "has", SetHasJS,
108 "delete", SetDeleteJS,
109 "clear", SetClearJS,
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000110 "forEach", SetForEach
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000111 ));
112}
113
114SetUpSet();
115
116
117// -------------------------------------------------------------------
118// Harmony Map
119
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000120function MapConstructor() {
121 if (%_IsConstructCall()) {
122 %MapInitialize(this);
123 } else {
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000124 throw MakeTypeError('constructor_not_function', ['Map']);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000125 }
126}
127
128
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000129function MapGetJS(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000130 if (!IS_MAP(this)) {
131 throw MakeTypeError('incompatible_method_receiver',
132 ['Map.prototype.get', this]);
133 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +0000134 return %MapGet(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000135}
136
137
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000138function MapSetJS(key, value) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000139 if (!IS_MAP(this)) {
140 throw MakeTypeError('incompatible_method_receiver',
141 ['Map.prototype.set', this]);
142 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +0000143 return %MapSet(this, key, value);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000144}
145
146
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000147function MapHasJS(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000148 if (!IS_MAP(this)) {
149 throw MakeTypeError('incompatible_method_receiver',
150 ['Map.prototype.has', this]);
151 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +0000152 return %MapHas(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000153}
154
155
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000156function MapDeleteJS(key) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000157 if (!IS_MAP(this)) {
158 throw MakeTypeError('incompatible_method_receiver',
159 ['Map.prototype.delete', this]);
160 }
machenbach@chromium.org79d07042014-05-22 00:04:38 +0000161 return %MapDelete(this, key);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000162}
163
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000164
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000165function MapGetSizeJS() {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000166 if (!IS_MAP(this)) {
167 throw MakeTypeError('incompatible_method_receiver',
168 ['Map.prototype.size', this]);
169 }
170 return %MapGetSize(this);
171}
172
173
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000174function MapClearJS() {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000175 if (!IS_MAP(this)) {
176 throw MakeTypeError('incompatible_method_receiver',
177 ['Map.prototype.clear', this]);
178 }
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000179 %MapClear(this);
180}
181
182
183function MapForEach(f, receiver) {
184 if (!IS_MAP(this)) {
185 throw MakeTypeError('incompatible_method_receiver',
186 ['Map.prototype.forEach', this]);
187 }
188
189 if (!IS_SPEC_FUNCTION(f)) {
190 throw MakeTypeError('called_non_callable', [f]);
191 }
192
machenbach@chromium.org196eb602014-06-04 00:06:13 +0000193 var iterator = new MapIterator(this, ITERATOR_KIND_ENTRIES);
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000194 var entry;
machenbach@chromium.org79d07042014-05-22 00:04:38 +0000195 var stepping = %_DebugCallbackSupportsStepping(f);
machenbach@chromium.org6a4d3942014-05-21 00:04:49 +0000196 while (!(entry = %MapIteratorNext(iterator)).done) {
machenbach@chromium.org79d07042014-05-22 00:04:38 +0000197 if (stepping) %DebugPrepareStepInIfStepping(f);
machenbach@chromium.org6a4d3942014-05-21 00:04:49 +0000198 %_CallFunction(receiver, entry.value[1], entry.value[0], this, f);
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000199 }
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000200}
201
202
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000203// -------------------------------------------------------------------
204
205function SetUpMap() {
206 %CheckIsBootstrapping();
207
208 %SetCode($Map, MapConstructor);
209 %FunctionSetPrototype($Map, new $Object());
210 %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);
211
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000212 %FunctionSetLength(MapForEach, 1);
213
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000214 // Set up the non-enumerable functions on the Map prototype object.
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000215 InstallGetter($Map.prototype, "size", MapGetSizeJS);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000216 InstallFunctions($Map.prototype, DONT_ENUM, $Array(
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000217 "get", MapGetJS,
218 "set", MapSetJS,
219 "has", MapHasJS,
220 "delete", MapDeleteJS,
221 "clear", MapClearJS,
machenbach@chromium.org4ef23ee2014-04-21 00:04:36 +0000222 "forEach", MapForEach
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000223 ));
224}
225
226SetUpMap();