/
issue5873.js
313 lines (284 loc) · 13.4 KB
/
issue5873.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
function runTest()
{
FBTest.sysout("issue5873.START");
FBTest.setPref("commandLineShowCompleterPopup", true);
FBTest.openNewTab(basePath + "commandLine/5873/issue5873.html", function(win)
{
FBTest.openFirebug();
FBTest.enableConsolePanel();
FBTest.enableScriptPanel(function(win)
{
var panel = FW.Firebug.chrome.selectPanel("console");
var doc = FW.Firebug.chrome.window.document;
var cmdLine = doc.getElementById("fbCommandLine");
var completionBox = doc.getElementById("fbCommandLineCompletion");
var popup = doc.getElementById("fbCommandLineCompletionList");
cmdLine.value = "";
function waitForOpen(callback)
{
if (popup.state === "opening")
{
setTimeout(waitForOpen, 10, callback);
return;
}
if (popup.state === "closed")
FBTest.compare("open", "closed", "Completion popup should open.");
callback();
}
function createA(callback)
{
// Run the code of createA in the Firebug command line.
function createA()
{
window.global = 1;
with({withVar: 2})
{
window.A = function(param)
{
function helper(x)
{
return x + 4;
}
var local = 5, unused = 15;
try
{
throw 6;
}
catch (catched)
{
this.someFunction = function(someFuncParam)
{
return helper(global + withVar + param + local + catched + someFuncParam);
};
}
};
}
window.a = new A(3);
}
var src = (createA + "").replace(/\n/g,' ').replace(/ +/g, ' ');
src += " createA();";
var sync = false;
FW.Firebug.CommandLine.evaluate(src, FW.Firebug.currentContext, undefined,
undefined, function()
{
sync = true;
}, function(e)
{
sync = true;
FBTest.compare(1, 0, "evaluation error: " + e);
});
FBTest.compare(true, sync, "Evaluation must be syncronous.");
callback();
}
function verifyNothingInjected(callback)
{
FBTest.compare("undefined", typeof win.wrappedJSObject.__fb_scopedVars,
"The scope getter function must not yet be available to page content.");
callback();
}
function verifyCompletionPopupForA(callback)
{
FBTest.typeCommand("a.%");
waitForOpen(function()
{
var completions = popup.getElementsByClassName("completionText");
var joined = [].slice.call(completions).map(function(el)
{
return el.textContent;
}).join(",");
var wanted = "catched,helper,local,param,unused,withVar";
FBTest.compare(wanted, joined, "The completion popup should show the right list of closure variables.");
cmdLine.value = "";
FBTest.setPref("commandLineShowCompleterPopup", false);
callback();
});
}
function testCompletion(callback, expr, shouldComplete)
{
// To save on time, only send the last character as a key press.
cmdLine.focus();
cmdLine.value = expr.slice(0, -1);
FBTest.synthesizeKey(expr.slice(-1), null, win);
var hasCompletion = (completionBox.value.length > expr.length);
FBTest.compare(shouldComplete, hasCompletion,
"Completions should " + (shouldComplete ? "" : "not ") +
"appear for: " + expr);
callback();
}
function verifyParsing(callback)
{
// Some unit tests for verifying parsing.
function tr(expr)
{
return FW.Firebug.JSAutoCompleter.transformScopeOperator(expr, "f");
}
var tests = [
["a.%b", "f(a).b"],
["a.%%b", "f(a).%b"],
["a.%b.%c", "f(f(a).b).c"],
["a\na.%b", "a\nf(a).b"],
["anew\na.%b", "anew\nf(a).b"],
["new\na.%b()", "new\n(f(a)).b()"],
["a new\na.%b()", "a new\n(f(a)).b()"],
["a.%b.%c", "f(f(a).b).c"],
["z.a.%b.c.%d", "f(f(z.a).b.c).d"],
["0.%a", "0.%a"],
["'a.%a'", "'a.%a'"]
];
for (var i = 0; i < tests.length; ++i)
{
var from = tests[i][0], to = tests[i][1];
FBTest.compare(to, tr(from), "Should transform |" + from + "| -> |" + to);
}
callback();
}
function testInDebugger(callback)
{
function step0()
{
// Break into the debugger.
FBTest.waitForBreakInDebugger(null, 9, false, step1);
win.wrappedJSObject.breakSoon();
}
function step1()
{
// Test that completions work.
FBTest.clickConsolePreviewButton();
testCompletion(step2, "func.%pr", true);
}
function step2()
{
// Test that evaluations work.
FBTest.executeCommandAndVerify(step3, "a.%global",
"1", "pre", "objectBox-number");
}
function step3()
{
// Set a conditional breakpoint with .% syntax.
FBTest.setBreakpoint(null, null, 18, {
condition: "count.%counter === 4"
}, step4);
}
function step4()
{
// Hit it.
FBTest.waitForBreakInDebugger(null, 18, true, step5);
FBTest.clickToolbarButton(null, "fbContinueButton");
}
function step5()
{
// Check that it hit at the right point.
FBTest.executeCommandAndVerify(step6, "i", "4", "pre", "objectBox-number");
}
function step6()
{
// Remove the breakpoint.
FBTest.removeBreakpoint(null, null, 18, step7);
}
function step7()
{
// Resume.
FBTest.waitForDebuggerResume(step8);
FBTest.clickToolbarButton(null, "fbContinueButton");
}
function step8()
{
// Whew, done. Switch back to the console panel.
panel = FW.Firebug.chrome.selectPanel("console");
callback();
}
step0();
}
var taskList = new FBTest.TaskList();
taskList.push(createA);
taskList.push(verifyNothingInjected);
taskList.push(verifyCompletionPopupForA);
// Verify cross-compartment permissions
taskList.push(FBTest.executeCommandAndVerify, "_FirebugCommandLine.%cmd",
"Error: permission denied to access cross origin scope", "span", "errorMessage");
if ("sandbox" in document.createElement("iframe"))
{
taskList.push(FBTest.executeCommandAndVerify, "frames[1].%framePriv",
"Error: permission denied to access cross origin scope", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "frames[1].location.%framePriv",
"Error: permission denied to access cross origin scope", "span", "errorMessage");
}
taskList.push(FBTest.executeCommandAndVerify, "frames[0].location.%framePriv",
"2", "pre", "objectBox-number");
taskList.push(FBTest.executeCommandAndVerify, "frames[0].%framePriv",
"2", "pre", "objectBox-number");
taskList.push(FBTest.executeCommandAndVerify, "frames[0].frameA.%framePriv",
"2", "pre", "objectBox-number");
// Test getting
taskList.push(FBTest.executeCommandAndVerify, "a.%global",
"1", "pre", "objectBox-number");
taskList.push(FBTest.executeCommandAndVerify, "typeof a.%values",
"\"function\"", "pre", "objectBox-string");
taskList.push(FBTest.executeCommandAndVerify, "a.%nonExistent",
"undefined", "pre", "objectBox-undefined");
taskList.push(FBTest.executeCommandAndVerify, "emptyObject.%blah",
"Error: missing closure", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "a.%local.%blah",
"TypeError: can't get scope of non-object", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "innerA.%b",
"2", "pre", "objectBox-number");
taskList.push(FBTest.executeCommandAndVerify, "innerB.%a",
"1", "pre", "objectBox-number");
// Test setting
taskList.push(FBTest.executeCommandAndVerify, "a.%nonExistent = 1",
"Error: can't create new closure variable", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "a.%unused = 1",
"Error: can't set optimized-away closure variable", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "delete a.%unused",
"Error: can't delete closure variable", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "++a.%local",
"6", "pre", "objectBox-number");
taskList.push(FBTest.executeCommandAndVerify, "emptyObject.%blah = 1",
"Error: missing closure", "span", "errorMessage");
// Verify the setting
taskList.push(FBTest.executeCommandAndVerify, "a.%unused",
"(optimized away)", "pre", "objectBox-optimizedAway");
taskList.push(FBTest.executeCommandAndVerify, "a.%local",
"6", "pre", "objectBox-number");
// Test that error sources are faked
taskList.push(FBTest.executeCommandAndVerify, "innerA.%a()",
"TypeError: <get closure>(...).a is not a function", "span", "errorMessage");
taskList.push(FBTest.executeCommandAndVerify, "innerA.%a()",
"innerA.%a()", "pre", "errorSourceCode");
taskList.push(FBTest.executeCommandAndVerify, "a.%nonExistent = 1",
"a.%nonExistent = 1", "pre", "errorSourceCode");
// Test object->function heuristics:
// * already a function
taskList.push(FBTest.executeCommandAndVerify, "func.%priv",
"1", "pre", "objectBox-number");
// * already a function, but would need scope anyway
taskList.push(FBTest.executeCommandAndVerify, "scopelessFunc.%priv",
"Error: missing closure", "span", "errorMessage");
// * use of own functions
taskList.push(FBTest.executeCommandAndVerify, "funcWithProto.prototype.%priv",
"10", "pre", "objectBox-number");
// * use of own "constructor"
taskList.push(FBTest.executeCommandAndVerify, "func.prototype.%priv",
"1", "pre", "objectBox-number");
// * use of inherited functions
taskList.push(FBTest.executeCommandAndVerify, "new funcWithProto().%priv",
"10", "pre", "objectBox-number");
// * use of "constructor"
taskList.push(FBTest.executeCommandAndVerify, "new func().%priv",
"1", "pre", "objectBox-number");
// Test completion
taskList.push(testCompletion, "new func().%pr", true);
taskList.push(testCompletion, "func.prototype.%", true);
taskList.push(testCompletion, "func.%priv.toP", true);
taskList.push(testCompletion, "func.%oth.%u", true);
taskList.push(testCompletion, "func.%oth.reg", true);
taskList.push(testCompletion, "new func.%oth().%u", true);
taskList.push(verifyParsing);
taskList.push(testInDebugger);
// Run!
taskList.run(function() {
FBTest.testDone("issue5873.DONE");
}, 0)
});
});
}