summaryrefslogtreecommitdiff
path: root/deps/v8/src/js/weak-collection.js
blob: 3d75ab4138c7d3f232b63c8cd6d8df7f5abd8c95 (plain)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function(global, utils) {

"use strict";

%CheckIsBootstrapping();

// -------------------------------------------------------------------
// Imports

var GlobalWeakMap = global.WeakMap;
var GlobalWeakSet = global.WeakSet;
var MathRandom = global.Math.random;

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

function WeakMapConstructor(iterable) {
  if (IS_UNDEFINED(new.target)) {
    throw %make_type_error(kConstructorNotFunction, "WeakMap");
  }

  %WeakCollectionInitialize(this);

  if (!IS_NULL_OR_UNDEFINED(iterable)) {
    var adder = this.set;
    if (!IS_CALLABLE(adder)) {
      throw %make_type_error(kPropertyNotFunction, adder, 'set', this);
    }
    for (var nextItem of iterable) {
      if (!IS_RECEIVER(nextItem)) {
        throw %make_type_error(kIteratorValueNotAnObject, nextItem);
      }
      %_Call(adder, this, nextItem[0], nextItem[1]);
    }
  }
}


// Set up the non-enumerable functions on the WeakMap prototype object.
DEFINE_METHODS(
  GlobalWeakMap.prototype,
  {
    set(key, value) {
      if (!IS_WEAKMAP(this)) {
        throw %make_type_error(kIncompatibleMethodReceiver,
                            'WeakMap.prototype.set', this);
      }
      if (!IS_RECEIVER(key)) throw %make_type_error(kInvalidWeakMapKey);
      return %WeakCollectionSet(this, key, value, %GenericHash(key));
    }

    delete(key) {
      if (!IS_WEAKMAP(this)) {
        throw %make_type_error(kIncompatibleMethodReceiver,
                            'WeakMap.prototype.delete', this);
      }
      if (!IS_RECEIVER(key)) return false;
      var hash = %GetExistingHash(key);
      if (IS_UNDEFINED(hash)) return false;
      return %WeakCollectionDelete(this, key, hash);
    }
  }
);

// -------------------------------------------------------------------

%SetCode(GlobalWeakMap, WeakMapConstructor);
%FunctionSetLength(GlobalWeakMap, 0);

// -------------------------------------------------------------------
// Harmony WeakSet

function WeakSetConstructor(iterable) {
  if (IS_UNDEFINED(new.target)) {
    throw %make_type_error(kConstructorNotFunction, "WeakSet");
  }

  %WeakCollectionInitialize(this);

  if (!IS_NULL_OR_UNDEFINED(iterable)) {
    var adder = this.add;
    if (!IS_CALLABLE(adder)) {
      throw %make_type_error(kPropertyNotFunction, adder, 'add', this);
    }
    for (var value of iterable) {
      %_Call(adder, this, value);
    }
  }
}


// Set up the non-enumerable functions on the WeakSet prototype object.
DEFINE_METHODS(
  GlobalWeakSet.prototype,
  {
    add(value) {
      if (!IS_WEAKSET(this)) {
        throw %make_type_error(kIncompatibleMethodReceiver,
                            'WeakSet.prototype.add', this);
      }
      if (!IS_RECEIVER(value)) throw %make_type_error(kInvalidWeakSetValue);
      return %WeakCollectionSet(this, value, true, %GenericHash(value));
    }

    delete(value) {
      if (!IS_WEAKSET(this)) {
        throw %make_type_error(kIncompatibleMethodReceiver,
                            'WeakSet.prototype.delete', this);
      }
      if (!IS_RECEIVER(value)) return false;
      var hash = %GetExistingHash(value);
      if (IS_UNDEFINED(hash)) return false;
      return %WeakCollectionDelete(this, value, hash);
    }
  }
);

// -------------------------------------------------------------------

%SetCode(GlobalWeakSet, WeakSetConstructor);
%FunctionSetLength(GlobalWeakSet, 0);

})