collection.js 8.19 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
"use strict";
29

30 31 32 33
// This file relies on the fact that the following declaration has been made
// in runtime.js:
// var $Array = global.Array;

34 35 36
var $Set = global.Set;
var $Map = global.Map;
var $WeakMap = global.WeakMap;
37

38 39 40 41
// Global sentinel to be used instead of undefined keys, which are not
// supported internally but required for Harmony sets and maps.
var undefined_sentinel = {};

42 43
// -------------------------------------------------------------------
// Harmony Set
44

45 46 47 48 49 50 51 52 53 54
function SetConstructor() {
  if (%_IsConstructCall()) {
    %SetInitialize(this);
  } else {
    return new $Set();
  }
}


function SetAdd(key) {
55 56 57 58
  if (!IS_SET(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Set.prototype.add', this]);
  }
59 60 61
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
62 63 64 65 66
  return %SetAdd(this, key);
}


function SetHas(key) {
67 68 69 70
  if (!IS_SET(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Set.prototype.has', this]);
  }
71 72 73
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
74 75 76 77 78
  return %SetHas(this, key);
}


function SetDelete(key) {
79 80 81 82
  if (!IS_SET(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Set.prototype.delete', this]);
  }
83 84 85
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
86 87 88 89 90 91
  if (%SetHas(this, key)) {
    %SetDelete(this, key);
    return true;
  } else {
    return false;
  }
92 93 94
}


95 96 97 98 99 100 101 102 103
function SetGetSize() {
  if (!IS_SET(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Set.prototype.size', this]);
  }
  return %SetGetSize(this);
}


104 105 106 107 108 109 110 111 112 113
function SetClear() {
  if (!IS_SET(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Set.prototype.clear', this]);
  }
  // Replace the internal table with a new empty table.
  %SetInitialize(this);
}


114 115 116 117 118 119
// -------------------------------------------------------------------

function SetUpSet() {
  %CheckIsBootstrapping();

  %SetCode($Set, SetConstructor);
120
  %FunctionSetPrototype($Set, new $Object());
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  %SetProperty($Set.prototype, "constructor", $Set, DONT_ENUM);

  // Set up the non-enumerable functions on the Set prototype object.
  InstallGetter($Set.prototype, "size", SetGetSize);
  InstallFunctions($Set.prototype, DONT_ENUM, $Array(
    "add", SetAdd,
    "has", SetHas,
    "delete", SetDelete,
    "clear", SetClear
  ));
}

SetUpSet();


// -------------------------------------------------------------------
// Harmony Map

139 140 141 142 143 144 145 146 147 148
function MapConstructor() {
  if (%_IsConstructCall()) {
    %MapInitialize(this);
  } else {
    return new $Map();
  }
}


function MapGet(key) {
149 150 151 152
  if (!IS_MAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Map.prototype.get', this]);
  }
153 154 155
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
156 157 158 159 160
  return %MapGet(this, key);
}


function MapSet(key, value) {
161 162 163 164
  if (!IS_MAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Map.prototype.set', this]);
  }
165 166 167
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
168 169 170 171 172
  return %MapSet(this, key, value);
}


function MapHas(key) {
173 174 175 176
  if (!IS_MAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Map.prototype.has', this]);
  }
177 178 179
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
180
  return %MapHas(this, key);
181 182 183 184
}


function MapDelete(key) {
185 186 187 188
  if (!IS_MAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Map.prototype.delete', this]);
  }
189 190 191
  if (IS_UNDEFINED(key)) {
    key = undefined_sentinel;
  }
192
  return %MapDelete(this, key);
193 194
}

195

196 197 198 199 200 201 202 203 204
function MapGetSize() {
  if (!IS_MAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Map.prototype.size', this]);
  }
  return %MapGetSize(this);
}


205 206 207 208 209 210 211 212 213 214
function MapClear() {
  if (!IS_MAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['Map.prototype.clear', this]);
  }
  // Replace the internal table with a new empty table.
  %MapInitialize(this);
}


215 216 217 218 219 220
// -------------------------------------------------------------------

function SetUpMap() {
  %CheckIsBootstrapping();

  %SetCode($Map, MapConstructor);
221
  %FunctionSetPrototype($Map, new $Object());
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  %SetProperty($Map.prototype, "constructor", $Map, DONT_ENUM);

  // Set up the non-enumerable functions on the Map prototype object.
  InstallGetter($Map.prototype, "size", MapGetSize);
  InstallFunctions($Map.prototype, DONT_ENUM, $Array(
    "get", MapGet,
    "set", MapSet,
    "has", MapHas,
    "delete", MapDelete,
    "clear", MapClear
  ));
}

SetUpMap();


// -------------------------------------------------------------------
// Harmony WeakMap

241
function WeakMapConstructor() {
242 243 244 245 246
  if (%_IsConstructCall()) {
    %WeakMapInitialize(this);
  } else {
    return new $WeakMap();
  }
247
}
248 249 250


function WeakMapGet(key) {
251 252 253 254
  if (!IS_WEAKMAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['WeakMap.prototype.get', this]);
  }
255
  if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
256 257 258 259 260 261 262
    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
  }
  return %WeakMapGet(this, key);
}


function WeakMapSet(key, value) {
263 264 265 266
  if (!IS_WEAKMAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['WeakMap.prototype.set', this]);
  }
267
  if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
268 269 270 271 272
    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
  }
  return %WeakMapSet(this, key, value);
}

273 274

function WeakMapHas(key) {
275 276 277 278
  if (!IS_WEAKMAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['WeakMap.prototype.has', this]);
  }
279
  if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
280 281
    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
  }
282
  return %WeakMapHas(this, key);
283 284 285 286
}


function WeakMapDelete(key) {
287 288 289 290
  if (!IS_WEAKMAP(this)) {
    throw MakeTypeError('incompatible_method_receiver',
                        ['WeakMap.prototype.delete', this]);
  }
291
  if (!(IS_SPEC_OBJECT(key) || IS_SYMBOL(key))) {
292 293
    throw %MakeTypeError('invalid_weakmap_key', [this, key]);
  }
294
  return %WeakMapDelete(this, key);
295 296
}

297

298 299
// -------------------------------------------------------------------

300
function SetUpWeakMap() {
301
  %CheckIsBootstrapping();
302

303
  %SetCode($WeakMap, WeakMapConstructor);
304
  %FunctionSetPrototype($WeakMap, new $Object());
305 306 307
  %SetProperty($WeakMap.prototype, "constructor", $WeakMap, DONT_ENUM);

  // Set up the non-enumerable functions on the WeakMap prototype object.
308
  InstallFunctions($WeakMap.prototype, DONT_ENUM, $Array(
309
    "get", WeakMapGet,
310 311 312
    "set", WeakMapSet,
    "has", WeakMapHas,
    "delete", WeakMapDelete
313
  ));
314 315 316
}

SetUpWeakMap();