weakRefs-basic.js (2607B)
1 assertEq('WeakRef' in this, true); 2 3 function checkPropertyDescriptor(obj, property, writable, enumerable, 4 configurable) { 5 let desc = Object.getOwnPropertyDescriptor(obj, property); 6 assertEq(typeof desc, "object"); 7 assertEq(desc.writable, writable); 8 assertEq(desc.enumerable, enumerable); 9 assertEq(desc.configurable, configurable); 10 } 11 12 function assertThrowsTypeError(thunk) { 13 let error; 14 try { 15 thunk(); 16 } catch (e) { 17 error = e; 18 } 19 assertEq(error instanceof TypeError, true); 20 } 21 22 assertEq(typeof this.WeakRef, "function"); 23 24 // https://tc39.es/proposal-weakrefs/ 25 26 // 1.1.1.1 27 // If NewTarget is undefined, throw a TypeError exception. 28 assertThrowsTypeError(() => new WeakRef()); 29 30 // 1.1.1.2 31 // If Type(target) is not Object, throw a TypeError exception. 32 assertThrowsTypeError(() => new WeakRef(1)); 33 assertThrowsTypeError(() => new WeakRef(true)); 34 assertThrowsTypeError(() => new WeakRef("string")); 35 assertThrowsTypeError(() => new WeakRef(Symbol.for("foo"))); 36 assertThrowsTypeError(() => new WeakRef(null)); 37 assertThrowsTypeError(() => new WeakRef(undefined)); 38 new WeakRef({}); 39 40 // 1.2 41 // The WeakRef constructor has a [[Prototype]] internal slot whose value is the 42 // intrinsic object %FunctionPrototype%. 43 assertEq(Object.getPrototypeOf(WeakRef), Function.prototype); 44 45 // 1.2.1 46 // The initial value of WeakRef.prototype is the intrinsic %WeakRefPrototype% 47 // object. 48 // This property has the attributes { [[Writable]]: false, [[Enumerable]]: false 49 // , [[Configurable]]: false }. 50 checkPropertyDescriptor(WeakRef, 'prototype', false, false, false); 51 52 // 1.3 53 // The WeakRef prototype object has a [[Prototype]] internal slot whose value is 54 // the intrinsic object %ObjectPrototype%. 55 let proto = WeakRef.prototype; 56 assertEq(Object.getPrototypeOf(proto), Object.prototype); 57 58 // 1.3.1 59 // The initial value of WeakRef.prototype.constructor is the intrinsic object 60 // %WeakRef%. 61 assertEq(proto.constructor, WeakRef); 62 63 // 1.3.2 64 // WeakRef.prototype.deref () 65 assertEq(proto.hasOwnProperty('deref'), true); 66 assertEq(typeof proto.deref, 'function'); 67 68 // 1.3.3 69 // The initial value of the @@toStringTag property is the String value 70 // "WeakRef". 71 // This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, 72 // [[Configurable]]: true }. 73 assertEq(proto[Symbol.toStringTag], "WeakRef"); 74 checkPropertyDescriptor(proto, Symbol.toStringTag, false, false, true); 75 76 // 1.4 77 // WeakRef instances are ordinary objects that inherit properties from the 78 // WeakRef prototype 79 let weakRef = new WeakRef({}); 80 assertEq(Object.getPrototypeOf(weakRef), proto);