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
|
<html>
<head>
<script type="text/javascript" src="../../http/tests/inspector-protocol/resources/protocol-test.js"></script>
<script>
function test()
{
// A general-purpose engine for sending a sequence of protocol commands.
// The clients provide requests and response handlers, while the engine catches
// errors and makes sure that once there's nothing to do completeTest() is called.
// @param step is an object with command, params and callback fields
function runRequestSeries(step) {
processStep(step);
function processStep(s) {
try {
processStepOrFail(s);
} catch (e) {
InspectorTest.log(e.stack);
InspectorTest.completeTest();
}
}
function processStepOrFail(s) {
if (!s) {
InspectorTest.completeTest();
return;
}
if (!s.command) {
// A simple loopback step.
var next = s.callback();
processStep(next);
return;
}
var innerCallback = function(response) {
var next;
if ("error" in response) {
if (!("errorHandler" in s)) {
// Error message is not logged intentionally, it may be platform-specific.
InspectorTest.log("Protocol command '" + s.command + "' failed");
InspectorTest.completeTest();
return;
}
try {
next = s.errorHandler(response.error);
} catch (e) {
InspectorTest.log(e.stack);
InspectorTest.completeTest();
return;
}
} else {
try {
next = s.callback(response.result);
} catch (e) {
InspectorTest.log(e.stack);
InspectorTest.completeTest();
return;
}
}
processStep(next);
}
InspectorTest.sendCommand(s.command, s.params, innerCallback);
}
}
var firstStep = { callback: enableDebugger };
runRequestSeries(firstStep);
function enableDebugger() {
return { command: "Debugger.enable", params: {}, callback: evalFunction };
}
// Testing function/closure scopes.
function evalFunction(response) {
var expression = "(function(p){var r=5;with({year:2013}){return function Closure(q){return p+q+r+year};}})('ttt')";
return { command: "Runtime.evaluate", params: {expression: expression}, callback: callbackEvalFunction };
}
function callbackEvalFunction(result) {
var id = result.result.objectId;
if (id === undefined)
throw new Error("objectId is expected");
return createCheckFunctionStepChain(id);
}
function createCheckFunctionStepChain(functionObjectId) {
var params = {
objectId: functionObjectId,
functionDeclaration: "function(){return this(true);}"
};
return {
command: "Runtime.callFunctionOn", params: params, callback: callbackLogClosureEval
};
function callbackLogClosureEval(result) {
InspectorTest.log("Closure returns: " + JSON.stringify(result.result));
InspectorTest.log(" (expected: 'ttttrue52013')");
var params = {
functionObjectId: functionObjectId,
scopeNumber: 1,
variableName: "r",
newValue: { value: 4 }
};
return {
command: "Debugger.setVariableValue", params: params, callback: setVariableCallback
};
}
function setVariableCallback() {
InspectorTest.log("Debugger.setVariableValue OK");
var params = {
objectId: functionObjectId,
functionDeclaration: "function(){return this(true);}"
};
return {
command: "Runtime.callFunctionOn", params: params, callback: callbackLogClosureEval2
};
}
function callbackLogClosureEval2(result) {
InspectorTest.log("Closure returns: " + JSON.stringify(result.result));
InspectorTest.log(" (expected: 'ttttrue42013')");
var params = {
// No target is specified
scopeNumber: 1,
variableName: "r",
newValue: { value: 4 }
};
return {
command: "Debugger.setVariableValue", params: params, errorHandler: setVariableErrorCallback3
};
}
function setVariableErrorCallback3(error) {
InspectorTest.log("Expected error: " + JSON.stringify(error));
var params = {
functionObjectId: functionObjectId,
scopeNumber: 100, // Wrong scope number
variableName: "r",
newValue: { value: 4 }
};
return {
command: "Debugger.setVariableValue", params: params, errorHandler: setVariableErrorCallback4
};
}
function setVariableErrorCallback4(error) {
InspectorTest.log("Expected error");
var params = {
functionObjectId: functionObjectId,
scopeNumber: 1,
variableName: "bad", // Wrong variable name
newValue: { value: 4 }
};
return {
command: "Debugger.setVariableValue", params: params, errorHandler: setVariableErrorCallback5
};
}
function setVariableErrorCallback5(error) {
InspectorTest.log("Expected error");
// End of test.
return;
}
}
}
</script>
</head>
<body onLoad="runTest();">
</body>
</html>
|