summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_envChain_frameScript.js
blob: 2d877d822fef244710130ae94d3923462dd0d321 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// Verify the environment chain for frame scripts described in
// js/src/vm/EnvironmentObject.h.

const { XPCShellContentUtils } = ChromeUtils.importESModule(
  "resource://testing-common/XPCShellContentUtils.sys.mjs"
);

XPCShellContentUtils.init(this);

add_task(async function unique_scope() {
  const page = await XPCShellContentUtils.loadContentPage("about:blank", {
    remote: true,
  });

  const envsPromise = new Promise(resolve => {
    Services.mm.addMessageListener("unique-envs-result", msg => {
      resolve(msg.data);
    });
  });
  const sharePromise = new Promise(resolve => {
    Services.mm.addMessageListener("unique-share-result", msg => {
      resolve(msg.data);
    });
  });

  const runInUniqueScope = true;
  const runInGlobalScope = !runInUniqueScope;

  Services.mm.loadFrameScript(`data:,
var unique_qualified = 10;
unique_unqualified = 20;
let unique_lexical = 30;
this.unique_prop = 40;

const funcs = Cu.getJSTestingFunctions();
const envs = [];
let env = funcs.getInnerMostEnvironmentObject();
while (env) {
  envs.push({
    type: funcs.getEnvironmentObjectType(env) || "*BackstagePass*",
    qualified: !!Object.getOwnPropertyDescriptor(env, "unique_qualified"),
    unqualified: !!Object.getOwnPropertyDescriptor(env, "unique_unqualified"),
    lexical: !!Object.getOwnPropertyDescriptor(env, "unique_lexical"),
    prop: !!Object.getOwnPropertyDescriptor(env, "unique_prop"),
  });

  env = funcs.getEnclosingEnvironmentObject(env);
}

sendSyncMessage("unique-envs-result", envs);
`, false, runInGlobalScope);

  Services.mm.loadFrameScript(`data:,
sendSyncMessage("unique-share-result", {
  unique_qualified: typeof unique_qualified,
  unique_unqualified: typeof unique_unqualified,
  unique_lexical: typeof unique_lexical,
  unique_prop: this.unique_prop,
});
`, false, runInGlobalScope);

  const envs = await envsPromise;
  const share = await sharePromise;

  Assert.equal(envs.length, 5);

  let i = 0, env;

  env = envs[i]; i++;
  Assert.equal(env.type, "NonSyntacticLexicalEnvironmentObject");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, true, "lexical must live in the NSLEO");
  Assert.equal(env.prop, false);

  env = envs[i]; i++;
  Assert.equal(env.type, "WithEnvironmentObject");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, true, "this property must live in the with env");

  env = envs[i]; i++;
  Assert.equal(env.type, "NonSyntacticVariablesObject");
  Assert.equal(env.qualified, true, "qualified var must live in the NSVO");
  Assert.equal(env.unqualified, true, "unqualified var must live in the NSVO");
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, false);

  env = envs[i]; i++;
  Assert.equal(env.type, "GlobalLexicalEnvironmentObject");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, false);

  env = envs[i]; i++;
  Assert.equal(env.type, "*BackstagePass*");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, false);

  Assert.equal(share.unique_qualified, "undefined", "qualified var must not be shared");
  Assert.equal(share.unique_unqualified, "undefined", "unqualified name must not be shared");
  Assert.equal(share.unique_lexical, "undefined", "lexical must not be shared");
  Assert.equal(share.unique_prop, 40, "this property must be shared");

  await page.close();
});

add_task(async function non_unique_scope() {
  const page = await XPCShellContentUtils.loadContentPage("about:blank", {
    remote: true,
  });

  const envsPromise = new Promise(resolve => {
    Services.mm.addMessageListener("non-unique-envs-result", msg => {
      resolve(msg.data);
    });
  });
  const sharePromise = new Promise(resolve => {
    Services.mm.addMessageListener("non-unique-share-result", msg => {
      resolve(msg.data);
    });
  });

  const runInUniqueScope = false;
  const runInGlobalScope = !runInUniqueScope;

  Services.mm.loadFrameScript(`data:,
var non_unique_qualified = 10;
non_unique_unqualified = 20;
let non_unique_lexical = 30;
this.non_unique_prop = 40;

const funcs = Cu.getJSTestingFunctions();
const envs = [];
let env = funcs.getInnerMostEnvironmentObject();
while (env) {
  envs.push({
    type: funcs.getEnvironmentObjectType(env) || "*BackstagePass*",
    qualified: !!Object.getOwnPropertyDescriptor(env, "non_unique_qualified"),
    unqualified: !!Object.getOwnPropertyDescriptor(env, "non_unique_unqualified"),
    lexical: !!Object.getOwnPropertyDescriptor(env, "non_unique_lexical"),
    prop: !!Object.getOwnPropertyDescriptor(env, "non_unique_prop"),
  });

  env = funcs.getEnclosingEnvironmentObject(env);
}

sendSyncMessage("non-unique-envs-result", envs);
`, false, runInGlobalScope);

  Services.mm.loadFrameScript(`data:,
sendSyncMessage("non-unique-share-result", {
  non_unique_qualified,
  non_unique_unqualified,
  non_unique_lexical,
  non_unique_prop,
});
`, false, runInGlobalScope);

  const envs = await envsPromise;
  const share = await sharePromise;

  Assert.equal(envs.length, 4);

  let i = 0, env;

  env = envs[i]; i++;
  Assert.equal(env.type, "NonSyntacticLexicalEnvironmentObject");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, true, "lexical must live in the NSLEO");
  Assert.equal(env.prop, false);

  env = envs[i]; i++;
  Assert.equal(env.type, "WithEnvironmentObject");
  Assert.equal(env.qualified, true, "qualified var must live in the with env");
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, true, "this property must live in the with env");

  env = envs[i]; i++;
  Assert.equal(env.type, "GlobalLexicalEnvironmentObject");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, false);
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, false);

  env = envs[i]; i++;
  Assert.equal(env.type, "*BackstagePass*");
  Assert.equal(env.qualified, false);
  Assert.equal(env.unqualified, true, "unqualified name must live in the backstage pass");
  Assert.equal(env.lexical, false);
  Assert.equal(env.prop, false);

  Assert.equal(share.non_unique_qualified, 10, "qualified var must be shared");
  Assert.equal(share.non_unique_unqualified, 20, "unqualified name must be shared");
  Assert.equal(share.non_unique_lexical, 30, "lexical must be shared");
  Assert.equal(share.non_unique_prop, 40, "this property must be shared");

  await page.close();
});