summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/ShadowRealm/WrappedFunction/length.js
blob: 058375cd7b47d4f3676beb290903c59a9fc8932b (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
// |reftest| shell-option(--enable-shadow-realms) skip-if(!xulRuntime.shell) -- requires shell-options
// Copyright (C) 2022 Chengzhong Wu. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-wrappedfunctioncreate
description: >
  The value of WrappedFunction.name is copied from the target function
info: |
  WrappedFunctionCreate ( callerRealm: a Realm Record, Target: a function object, )

  ...
  7. Let result be CopyNameAndLength(wrapped, Target).
  ...

  CopyNameAndLength ( F: a function object, Target: a function object, prefix: a String, optional argCount: a Number, )

  1. If argCount is undefined, then set argCount to 0.
  2. Let L be 0.
  3. Let targetHasLength be ? HasOwnProperty(Target, "length").
  4. If targetHasLength is true, then
    a. Let targetLen be ? Get(Target, "length").
    b. If Type(targetLen) is Number, then
        i. If targetLen is +∞𝔽, set L to +∞.
        ii. Else if targetLen is -∞𝔽, set L to 0.
        iii. Else,
            1. Let targetLenAsInt be ! ToIntegerOrInfinity(targetLen).
            2. Assert: targetLenAsInt is finite.
            3. Set L to max(targetLenAsInt - argCount, 0).
  5. Perform ! SetFunctionLength(F, L).
  ...

  SetFunctionLength ( F, length )

  ...
  2. Return ! DefinePropertyOrThrow(F, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }).

includes: [propertyHelper.js]
features: [ShadowRealm]
---*/

assert.sameValue(
  typeof ShadowRealm.prototype.evaluate,
  'function',
  'This test must fail if ShadowRealm.prototype.evaluate is not a function'
);

const r = new ShadowRealm();


let wrapped = r.evaluate(`
function fn(foo, bar) {}
fn;
`);
verifyProperty(wrapped, "length", {
  value: 2,
  enumerable: false,
  writable: false,
  configurable: true,
});


wrapped = r.evaluate(`
function fn() {}
delete fn.length;
fn;
`);
verifyProperty(wrapped, "length", {
  value: 0,
  enumerable: false,
  writable: false,
  configurable: true,
});


wrapped = r.evaluate(`
function fn() {}
Object.defineProperty(fn, 'length', {
  get: () => Infinity,
  enumerable: false,
  configurable: true,
});
fn;
`);
verifyProperty(wrapped, "length", {
  value: Infinity,
  enumerable: false,
  writable: false,
  configurable: true,
});


wrapped = r.evaluate(`
function fn() {}
Object.defineProperty(fn, 'length', {
  get: () => -Infinity,
  enumerable: false,
  configurable: true,
});
fn;
`);
verifyProperty(wrapped, "length", {
  value: 0,
  enumerable: false,
  writable: false,
  configurable: true,
});


wrapped = r.evaluate(`
function fn() {}
Object.defineProperty(fn, 'length', {
  get: () => -1,
  enumerable: false,
  configurable: true,
});
fn;
`);
verifyProperty(wrapped, "length", {
  value: 0,
  enumerable: false,
  writable: false,
  configurable: true,
});

reportCompare(0, 0);