jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
rossberg@chromium.org | 3484964 | 2014-04-29 16:30:47 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5 | "use strict"; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 6 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 7 | // 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.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11 | var $Set = global.Set; |
| 12 | var $Map = global.Map; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13 | |
machenbach@chromium.org | 05150ab | 2014-01-29 08:13:29 +0000 | [diff] [blame] | 14 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 15 | // ------------------------------------------------------------------- |
| 16 | // Harmony Set |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 17 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 18 | function SetConstructor() { |
| 19 | if (%_IsConstructCall()) { |
| 20 | %SetInitialize(this); |
| 21 | } else { |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 22 | throw MakeTypeError('constructor_not_function', ['Set']); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
| 26 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 27 | function SetAddJS(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 28 | if (!IS_SET(this)) { |
| 29 | throw MakeTypeError('incompatible_method_receiver', |
| 30 | ['Set.prototype.add', this]); |
| 31 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 32 | return %SetAdd(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 36 | function SetHasJS(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 37 | if (!IS_SET(this)) { |
| 38 | throw MakeTypeError('incompatible_method_receiver', |
| 39 | ['Set.prototype.has', this]); |
| 40 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 41 | return %SetHas(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 45 | function SetDeleteJS(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 46 | if (!IS_SET(this)) { |
| 47 | throw MakeTypeError('incompatible_method_receiver', |
| 48 | ['Set.prototype.delete', this]); |
| 49 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 50 | return %SetDelete(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 54 | function SetGetSizeJS() { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 55 | 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.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 63 | function SetClearJS() { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 64 | if (!IS_SET(this)) { |
| 65 | throw MakeTypeError('incompatible_method_receiver', |
| 66 | ['Set.prototype.clear', this]); |
| 67 | } |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 68 | %SetClear(this); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | function 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.org | 196eb60 | 2014-06-04 00:06:13 +0000 | [diff] [blame] | 82 | var iterator = new SetIterator(this, ITERATOR_KIND_VALUES); |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 83 | var entry; |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 84 | var stepping = %_DebugCallbackSupportsStepping(f); |
machenbach@chromium.org | 6a4d394 | 2014-05-21 00:04:49 +0000 | [diff] [blame] | 85 | while (!(entry = %SetIteratorNext(iterator)).done) { |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 86 | if (stepping) %DebugPrepareStepInIfStepping(f); |
machenbach@chromium.org | 6a4d394 | 2014-05-21 00:04:49 +0000 | [diff] [blame] | 87 | %_CallFunction(receiver, entry.value, entry.value, this, f); |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 88 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 92 | // ------------------------------------------------------------------- |
| 93 | |
| 94 | function SetUpSet() { |
| 95 | %CheckIsBootstrapping(); |
| 96 | |
| 97 | %SetCode($Set, SetConstructor); |
| 98 | %FunctionSetPrototype($Set, new $Object()); |
| 99 | %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM); |
| 100 | |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 101 | %FunctionSetLength(SetForEach, 1); |
| 102 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 103 | // Set up the non-enumerable functions on the Set prototype object. |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 104 | InstallGetter($Set.prototype, "size", SetGetSizeJS); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 105 | InstallFunctions($Set.prototype, DONT_ENUM, $Array( |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 106 | "add", SetAddJS, |
| 107 | "has", SetHasJS, |
| 108 | "delete", SetDeleteJS, |
| 109 | "clear", SetClearJS, |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 110 | "forEach", SetForEach |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 111 | )); |
| 112 | } |
| 113 | |
| 114 | SetUpSet(); |
| 115 | |
| 116 | |
| 117 | // ------------------------------------------------------------------- |
| 118 | // Harmony Map |
| 119 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 120 | function MapConstructor() { |
| 121 | if (%_IsConstructCall()) { |
| 122 | %MapInitialize(this); |
| 123 | } else { |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 124 | throw MakeTypeError('constructor_not_function', ['Map']); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 129 | function MapGetJS(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 130 | if (!IS_MAP(this)) { |
| 131 | throw MakeTypeError('incompatible_method_receiver', |
| 132 | ['Map.prototype.get', this]); |
| 133 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 134 | return %MapGet(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 138 | function MapSetJS(key, value) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 139 | if (!IS_MAP(this)) { |
| 140 | throw MakeTypeError('incompatible_method_receiver', |
| 141 | ['Map.prototype.set', this]); |
| 142 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 143 | return %MapSet(this, key, value); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 147 | function MapHasJS(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 148 | if (!IS_MAP(this)) { |
| 149 | throw MakeTypeError('incompatible_method_receiver', |
| 150 | ['Map.prototype.has', this]); |
| 151 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 152 | return %MapHas(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 156 | function MapDeleteJS(key) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 157 | if (!IS_MAP(this)) { |
| 158 | throw MakeTypeError('incompatible_method_receiver', |
| 159 | ['Map.prototype.delete', this]); |
| 160 | } |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 161 | return %MapDelete(this, key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 162 | } |
| 163 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 164 | |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 165 | function MapGetSizeJS() { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 166 | 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.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 174 | function MapClearJS() { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 175 | if (!IS_MAP(this)) { |
| 176 | throw MakeTypeError('incompatible_method_receiver', |
| 177 | ['Map.prototype.clear', this]); |
| 178 | } |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 179 | %MapClear(this); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | function 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.org | 196eb60 | 2014-06-04 00:06:13 +0000 | [diff] [blame] | 193 | var iterator = new MapIterator(this, ITERATOR_KIND_ENTRIES); |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 194 | var entry; |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 195 | var stepping = %_DebugCallbackSupportsStepping(f); |
machenbach@chromium.org | 6a4d394 | 2014-05-21 00:04:49 +0000 | [diff] [blame] | 196 | while (!(entry = %MapIteratorNext(iterator)).done) { |
machenbach@chromium.org | 79d0704 | 2014-05-22 00:04:38 +0000 | [diff] [blame] | 197 | if (stepping) %DebugPrepareStepInIfStepping(f); |
machenbach@chromium.org | 6a4d394 | 2014-05-21 00:04:49 +0000 | [diff] [blame] | 198 | %_CallFunction(receiver, entry.value[1], entry.value[0], this, f); |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 199 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 203 | // ------------------------------------------------------------------- |
| 204 | |
| 205 | function SetUpMap() { |
| 206 | %CheckIsBootstrapping(); |
| 207 | |
| 208 | %SetCode($Map, MapConstructor); |
| 209 | %FunctionSetPrototype($Map, new $Object()); |
| 210 | %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM); |
| 211 | |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 212 | %FunctionSetLength(MapForEach, 1); |
| 213 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 214 | // Set up the non-enumerable functions on the Map prototype object. |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 215 | InstallGetter($Map.prototype, "size", MapGetSizeJS); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 216 | InstallFunctions($Map.prototype, DONT_ENUM, $Array( |
machenbach@chromium.org | aa107b2 | 2014-05-15 00:04:44 +0000 | [diff] [blame] | 217 | "get", MapGetJS, |
| 218 | "set", MapSetJS, |
| 219 | "has", MapHasJS, |
| 220 | "delete", MapDeleteJS, |
| 221 | "clear", MapClearJS, |
machenbach@chromium.org | 4ef23ee | 2014-04-21 00:04:36 +0000 | [diff] [blame] | 222 | "forEach", MapForEach |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 223 | )); |
| 224 | } |
| 225 | |
| 226 | SetUpMap(); |