basics.js 4.84 KB
Newer Older
1 2 3 4 5 6
// Copyright 2018 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.

// Flags: --harmony-weak-refs

7 8 9 10 11
(function TestConstructFinalizationGroup() {
  let fg = new FinalizationGroup(() => {});
  assertEquals(fg.toString(), "[object FinalizationGroup]");
  assertNotSame(fg.__proto__, Object.prototype);
  assertSame(fg.__proto__.__proto__, Object.prototype);
12 13
})();

14
(function TestFinalizationGroupConstructorCallAsFunction() {
15 16 17
  let caught = false;
  let message = "";
  try {
18
    let f = FinalizationGroup(() => {});
19 20 21 22 23
  } catch (e) {
    message = e.message;
    caught = true;
  } finally {
    assertTrue(caught);
24
    assertEquals(message, "Constructor FinalizationGroup requires 'new'");
25 26 27
  }
})();

28 29 30 31 32
(function TestConstructFinalizationGroupCleanupNotCallable() {
  let message = "FinalizationGroup: cleanup must be callable";
  assertThrows(() => { let fg = new FinalizationGroup(); }, TypeError, message);
  assertThrows(() => { let fg = new FinalizationGroup(1); }, TypeError, message);
  assertThrows(() => { let fg = new FinalizationGroup(null); }, TypeError, message);
33 34
})();

35
(function TestConstructFinalizationGroupWithCallableProxyAsCleanup() {
36 37 38
  let handler = {};
  let obj = () => {};
  let proxy = new Proxy(obj, handler);
39
  let fg = new FinalizationGroup(proxy);
40 41
})();

42 43
(function TestConstructFinalizationGroupWithNonCallableProxyAsCleanup() {
  let message = "FinalizationGroup: cleanup must be callable";
44 45 46
  let handler = {};
  let obj = {};
  let proxy = new Proxy(obj, handler);
47
  assertThrows(() => { let fg = new FinalizationGroup(proxy); }, TypeError, message);
48 49
})();

50 51 52 53 54 55 56 57 58
(function TestRegisterWithNonObjectTarget() {
  let fg = new FinalizationGroup(() => {});
  let message = "FinalizationGroup.prototype.register: target must be an object";
  assertThrows(() => fg.register(1, "holdings"), TypeError, message);
  assertThrows(() => fg.register(false, "holdings"), TypeError, message);
  assertThrows(() => fg.register("foo", "holdings"), TypeError, message);
  assertThrows(() => fg.register(Symbol(), "holdings"), TypeError, message);
  assertThrows(() => fg.register(null, "holdings"), TypeError, message);
  assertThrows(() => fg.register(undefined, "holdings"), TypeError, message);
59 60
})();

61
(function TestRegisterWithProxy() {
62 63 64
  let handler = {};
  let obj = {};
  let proxy = new Proxy(obj, handler);
65 66
  let fg = new FinalizationGroup(() => {});
  fg.register(proxy);
67 68
})();

69 70
(function TestRegisterTargetAndHoldingsSameValue() {
  let fg = new FinalizationGroup(() => {});
71 72
  let obj = {a: 1};
  // SameValue(target, holdings) not ok
73 74
  assertThrows(() => fg.register(obj, obj), TypeError,
               "FinalizationGroup.prototype.register: target and holdings must not be same");
75
  let holdings = {a: 1};
76
  fg.register(obj, holdings);
77 78
})();

79 80
(function TestRegisterWithoutFinalizationGroup() {
  assertThrows(() => FinalizationGroup.prototype.register.call({}, {}, "holdings"), TypeError);
81
  // Does not throw:
82 83
  let fg = new FinalizationGroup(() => {});
  FinalizationGroup.prototype.register.call(fg, {}, "holdings");
84 85
})();

86 87 88
(function TestUnregisterWithNonExistentKey() {
  let fg = new FinalizationGroup(() => {});
  fg.unregister({"k": "whatever"});
89
})();
90

91 92
(function TestWeakRefConstructor() {
  let wr = new WeakRef({});
93 94 95 96 97 98 99 100 101
  assertEquals(wr.toString(), "[object WeakRef]");
  assertNotSame(wr.__proto__, Object.prototype);

  let deref_desc = Object.getOwnPropertyDescriptor(wr.__proto__, "deref");
  assertEquals(true, deref_desc.configurable);
  assertEquals(false, deref_desc.enumerable);
  assertEquals("function", typeof deref_desc.value);
})();

102 103 104 105 106 107 108 109 110
(function TestWeakRefConstructorWithNonObject() {
  let message = "WeakRef: target must be an object";
  assertThrows(() => new WeakRef(), TypeError, message);
  assertThrows(() => new WeakRef(1), TypeError, message);
  assertThrows(() => new WeakRef(false), TypeError, message);
  assertThrows(() => new WeakRef("foo"), TypeError, message);
  assertThrows(() => new WeakRef(Symbol()), TypeError, message);
  assertThrows(() => new WeakRef(null), TypeError, message);
  assertThrows(() => new WeakRef(undefined), TypeError, message);
111 112
})();

113 114 115 116 117 118 119 120 121 122 123 124
(function TestWeakRefConstructorCallAsFunction() {
  let caught = false;
  let message = "";
  try {
    let f = WeakRef({});
  } catch (e) {
    message = e.message;
    caught = true;
  } finally {
    assertTrue(caught);
    assertEquals(message, "Constructor WeakRef requires 'new'");
  }
125 126
})();

127
(function TestWeakRefWithProxy() {
128 129 130
  let handler = {};
  let obj = {};
  let proxy = new Proxy(obj, handler);
131
  let wr = new WeakRef(proxy);
132 133
})();

134 135
(function TestCleanupSomeWithoutFinalizationGroup() {
  assertThrows(() => FinalizationGroup.prototype.cleanupSome.call({}), TypeError);
136
  // Does not throw:
137 138
  let fg = new FinalizationGroup(() => {});
  let rv = FinalizationGroup.prototype.cleanupSome.call(fg);
139 140
  assertEquals(undefined, rv);
})();