1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Flags: --expose-debug-as debug
29 // Get the Debug object exposed from the debug context global object.
32 var evaluate_callback;
34 function listener(event, exec_state, event_data, data) {
36 var context = { what_is_capybara: "a fish" };
37 var context2 = { what_is_capybara: "a fish", what_is_parrot: "a beard" };
39 // Try in frame's scope.
40 var local_expression =
41 "(what_is_capybara ? what_is_capybara : 'a beast') + '/' + what_is_parrot";
42 var result = evaluate_callback.in_top_frame(exec_state, local_expression, context);
43 assertEquals('a fish/a bird', result);
45 // Try in frame's scope with overrididen local variables.
46 var result = evaluate_callback.in_top_frame(exec_state, local_expression, context2);
47 assertEquals('a fish/a beard', result);
49 // Try in frame's scope, without context.
50 var local_expression2 = "what_is_parrot";
51 var result = evaluate_callback.in_top_frame(exec_state, local_expression2, void 0);
52 assertEquals('a bird', result);
54 // Try in global additional scope.
55 var global_expression = "what_is_capybara ? what_is_capybara : 'a beast'";
56 var result = evaluate_callback.globally(exec_state, global_expression, context);
57 assertEquals('a fish', result);
59 // Try in global scope with overridden global variables.
60 var context_with_undefined = { undefined: 'kitten' };
61 var global_expression2 = "'cat' + '/' + undefined";
62 var result = evaluate_callback.globally(exec_state, global_expression2, context_with_undefined);
63 assertEquals('cat/kitten', result);
65 // Try in global scope with no overridden global variables.
66 var result = evaluate_callback.globally(exec_state, global_expression2, void 0);
67 assertEquals('cat/undefined', result);
69 // Try in global scope without additional context.
70 var global_expression3 = "'cat' + '/' + 'dog'";
71 var result = evaluate_callback.globally(exec_state, global_expression3, void 0);
72 assertEquals('cat/dog', result);
74 listenerComplete = true;
82 var what_is_parrot = "a bird";
88 listenerComplete = false;
90 Debug.setListener(listener);
92 // Add the debug event listener.
93 Debug.setListener(listener);
97 assertFalse(exception, "exception in listener")
98 assertTrue(listenerComplete);
101 evaluate_callback = {
102 in_top_frame: function(exec_state, expression, additional_context) {
103 return exec_state.frame(0).evaluate(expression, void 0, additional_context).value();
105 globally: function(exec_state, expression, additional_context) {
106 return exec_state.evaluateGlobal(expression, void 0, additional_context).value();
113 // Now try all the same, but via debug protocol.
115 function evaluateViaProtocol(exec_state, expression, additional_context, frame_argument_adder) {
116 var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
117 request_json = {"seq":17,"type":"request","command":"evaluate", arguments: { "expression": expression } };
118 frame_argument_adder(request_json.arguments);
119 if (additional_context) {
120 var context_json = [];
121 for (var key in additional_context) {
122 context_json.push({ name: key, handle: Debug.MakeMirror(additional_context[key]).handle() });
124 request_json.arguments.additional_context = context_json;
126 var request = JSON.stringify(request_json);
127 var response_json = dcp.processDebugJSONRequest(request);
128 var response = JSON.parse(response_json);
130 assertTrue(response.success);
131 var str_result = response.body.value;
135 evaluate_callback = {
136 in_top_frame: function(exec_state, expression, additional_context) {
137 return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.frame = 0; });
139 globally: function(exec_state, expression, additional_context) {
140 return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.global = true; });