1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Verify the environment chain for frame scripts described in
8 // js/src/vm/EnvironmentObject.h.
10 const { XPCShellContentUtils } = ChromeUtils.importESModule(
11 "resource://testing-common/XPCShellContentUtils.sys.mjs"
14 XPCShellContentUtils.init(this);
16 add_task(async function unique_scope() {
17 const page = await XPCShellContentUtils.loadContentPage("about:blank", {
21 const envsPromise = new Promise(resolve => {
22 Services.mm.addMessageListener("unique-envs-result", msg => {
26 const sharePromise = new Promise(resolve => {
27 Services.mm.addMessageListener("unique-share-result", msg => {
32 const runInUniqueScope = true;
33 const runInGlobalScope = !runInUniqueScope;
35 Services.mm.loadFrameScript(`data:,
36 var unique_qualified = 10;
37 unique_unqualified = 20;
38 let unique_lexical = 30;
39 this.unique_prop = 40;
41 const funcs = Cu.getJSTestingFunctions();
43 let env = funcs.getInnerMostEnvironmentObject();
46 type: funcs.getEnvironmentObjectType(env) || "*SystemGlobal*",
47 qualified: !!Object.getOwnPropertyDescriptor(env, "unique_qualified"),
48 unqualified: !!Object.getOwnPropertyDescriptor(env, "unique_unqualified"),
49 lexical: !!Object.getOwnPropertyDescriptor(env, "unique_lexical"),
50 prop: !!Object.getOwnPropertyDescriptor(env, "unique_prop"),
53 env = funcs.getEnclosingEnvironmentObject(env);
56 sendSyncMessage("unique-envs-result", envs);
57 `, false, runInGlobalScope);
59 Services.mm.loadFrameScript(`data:,
60 sendSyncMessage("unique-share-result", {
61 unique_qualified: typeof unique_qualified,
62 unique_unqualified: typeof unique_unqualified,
63 unique_lexical: typeof unique_lexical,
64 unique_prop: this.unique_prop,
66 `, false, runInGlobalScope);
68 const envs = await envsPromise;
69 const share = await sharePromise;
71 Assert.equal(envs.length, 5);
76 Assert.equal(env.type, "NonSyntacticLexicalEnvironmentObject");
77 Assert.equal(env.qualified, false);
78 Assert.equal(env.unqualified, false);
79 Assert.equal(env.lexical, true, "lexical must live in the NSLEO");
80 Assert.equal(env.prop, false);
83 Assert.equal(env.type, "WithEnvironmentObject");
84 Assert.equal(env.qualified, false);
85 Assert.equal(env.unqualified, false);
86 Assert.equal(env.lexical, false);
87 Assert.equal(env.prop, true, "this property must live in the with env");
90 Assert.equal(env.type, "NonSyntacticVariablesObject");
91 Assert.equal(env.qualified, true, "qualified var must live in the NSVO");
92 Assert.equal(env.unqualified, true, "unqualified var must live in the NSVO");
93 Assert.equal(env.lexical, false);
94 Assert.equal(env.prop, false);
97 Assert.equal(env.type, "GlobalLexicalEnvironmentObject");
98 Assert.equal(env.qualified, false);
99 Assert.equal(env.unqualified, false);
100 Assert.equal(env.lexical, false);
101 Assert.equal(env.prop, false);
104 Assert.equal(env.type, "*SystemGlobal*");
105 Assert.equal(env.qualified, false);
106 Assert.equal(env.unqualified, false);
107 Assert.equal(env.lexical, false);
108 Assert.equal(env.prop, false);
110 Assert.equal(share.unique_qualified, "undefined", "qualified var must not be shared");
111 Assert.equal(share.unique_unqualified, "undefined", "unqualified name must not be shared");
112 Assert.equal(share.unique_lexical, "undefined", "lexical must not be shared");
113 Assert.equal(share.unique_prop, 40, "this property must be shared");
118 add_task(async function non_unique_scope() {
119 const page = await XPCShellContentUtils.loadContentPage("about:blank", {
123 const envsPromise = new Promise(resolve => {
124 Services.mm.addMessageListener("non-unique-envs-result", msg => {
128 const sharePromise = new Promise(resolve => {
129 Services.mm.addMessageListener("non-unique-share-result", msg => {
134 const runInUniqueScope = false;
135 const runInGlobalScope = !runInUniqueScope;
137 Services.mm.loadFrameScript(`data:,
138 var non_unique_qualified = 10;
139 non_unique_unqualified = 20;
140 let non_unique_lexical = 30;
141 this.non_unique_prop = 40;
143 const funcs = Cu.getJSTestingFunctions();
145 let env = funcs.getInnerMostEnvironmentObject();
148 type: funcs.getEnvironmentObjectType(env) || "*SystemGlobal*",
149 qualified: !!Object.getOwnPropertyDescriptor(env, "non_unique_qualified"),
150 unqualified: !!Object.getOwnPropertyDescriptor(env, "non_unique_unqualified"),
151 lexical: !!Object.getOwnPropertyDescriptor(env, "non_unique_lexical"),
152 prop: !!Object.getOwnPropertyDescriptor(env, "non_unique_prop"),
155 env = funcs.getEnclosingEnvironmentObject(env);
158 sendSyncMessage("non-unique-envs-result", envs);
159 `, false, runInGlobalScope);
161 Services.mm.loadFrameScript(`data:,
162 sendSyncMessage("non-unique-share-result", {
163 non_unique_qualified,
164 non_unique_unqualified,
168 `, false, runInGlobalScope);
170 const envs = await envsPromise;
171 const share = await sharePromise;
173 Assert.equal(envs.length, 4);
178 Assert.equal(env.type, "NonSyntacticLexicalEnvironmentObject");
179 Assert.equal(env.qualified, false);
180 Assert.equal(env.unqualified, false);
181 Assert.equal(env.lexical, true, "lexical must live in the NSLEO");
182 Assert.equal(env.prop, false);
185 Assert.equal(env.type, "WithEnvironmentObject");
186 Assert.equal(env.qualified, true, "qualified var must live in the with env");
187 Assert.equal(env.unqualified, false);
188 Assert.equal(env.lexical, false);
189 Assert.equal(env.prop, true, "this property must live in the with env");
192 Assert.equal(env.type, "GlobalLexicalEnvironmentObject");
193 Assert.equal(env.qualified, false);
194 Assert.equal(env.unqualified, false);
195 Assert.equal(env.lexical, false);
196 Assert.equal(env.prop, false);
199 Assert.equal(env.type, "*SystemGlobal*");
200 Assert.equal(env.qualified, false);
201 Assert.equal(env.unqualified, true, "unqualified name must live in the system global");
202 Assert.equal(env.lexical, false);
203 Assert.equal(env.prop, false);
205 Assert.equal(share.non_unique_qualified, 10, "qualified var must be shared");
206 Assert.equal(share.non_unique_unqualified, 20, "unqualified name must be shared");
207 Assert.equal(share.non_unique_lexical, 30, "lexical must be shared");
208 Assert.equal(share.non_unique_prop, 40, "this property must be shared");