length.js (2904B)
1 // |reftest| shell-option(--enable-shadow-realms) skip-if(!xulRuntime.shell) -- requires shell-options 2 // Copyright (C) 2022 Chengzhong Wu. All rights reserved. 3 // This code is governed by the BSD license found in the LICENSE file. 4 /*--- 5 esid: sec-wrappedfunctioncreate 6 description: > 7 The value of WrappedFunction.name is copied from the target function 8 info: | 9 WrappedFunctionCreate ( callerRealm: a Realm Record, Target: a function object, ) 10 11 ... 12 7. Let result be CopyNameAndLength(wrapped, Target). 13 ... 14 15 CopyNameAndLength ( F: a function object, Target: a function object, prefix: a String, optional argCount: a Number, ) 16 17 1. If argCount is undefined, then set argCount to 0. 18 2. Let L be 0. 19 3. Let targetHasLength be ? HasOwnProperty(Target, "length"). 20 4. If targetHasLength is true, then 21 a. Let targetLen be ? Get(Target, "length"). 22 b. If Type(targetLen) is Number, then 23 i. If targetLen is +∞𝔽, set L to +∞. 24 ii. Else if targetLen is -∞𝔽, set L to 0. 25 iii. Else, 26 1. Let targetLenAsInt be ! ToIntegerOrInfinity(targetLen). 27 2. Assert: targetLenAsInt is finite. 28 3. Set L to max(targetLenAsInt - argCount, 0). 29 5. Perform ! SetFunctionLength(F, L). 30 ... 31 32 SetFunctionLength ( F, length ) 33 34 ... 35 2. Return ! DefinePropertyOrThrow(F, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }). 36 37 includes: [propertyHelper.js] 38 features: [ShadowRealm] 39 ---*/ 40 41 assert.sameValue( 42 typeof ShadowRealm.prototype.evaluate, 43 'function', 44 'This test must fail if ShadowRealm.prototype.evaluate is not a function' 45 ); 46 47 const r = new ShadowRealm(); 48 49 50 let wrapped = r.evaluate(` 51 function fn(foo, bar) {} 52 fn; 53 `); 54 verifyProperty(wrapped, "length", { 55 value: 2, 56 enumerable: false, 57 writable: false, 58 configurable: true, 59 }); 60 61 62 wrapped = r.evaluate(` 63 function fn() {} 64 delete fn.length; 65 fn; 66 `); 67 verifyProperty(wrapped, "length", { 68 value: 0, 69 enumerable: false, 70 writable: false, 71 configurable: true, 72 }); 73 74 75 wrapped = r.evaluate(` 76 function fn() {} 77 Object.defineProperty(fn, 'length', { 78 get: () => Infinity, 79 enumerable: false, 80 configurable: true, 81 }); 82 fn; 83 `); 84 verifyProperty(wrapped, "length", { 85 value: Infinity, 86 enumerable: false, 87 writable: false, 88 configurable: true, 89 }); 90 91 92 wrapped = r.evaluate(` 93 function fn() {} 94 Object.defineProperty(fn, 'length', { 95 get: () => -Infinity, 96 enumerable: false, 97 configurable: true, 98 }); 99 fn; 100 `); 101 verifyProperty(wrapped, "length", { 102 value: 0, 103 enumerable: false, 104 writable: false, 105 configurable: true, 106 }); 107 108 109 wrapped = r.evaluate(` 110 function fn() {} 111 Object.defineProperty(fn, 'length', { 112 get: () => -1, 113 enumerable: false, 114 configurable: true, 115 }); 116 fn; 117 `); 118 verifyProperty(wrapped, "length", { 119 value: 0, 120 enumerable: false, 121 writable: false, 122 configurable: true, 123 }); 124 125 reportCompare(0, 0);