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
4// met:
5//
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.
15//
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.
27
28// Flags: --expose-debug-as debug
29// Get the Debug object exposed from the debug context global object.
30Debug = debug.Debug
31
32var evaluate_callback;
33
34function listener(event, exec_state, event_data, data) {
35  if (event !== Debug.DebugEvent.Break) return;
36  try {
37    var context = { what_is_capybara: "a fish" };
38    var context2 = { what_is_capybara: "a fish", what_is_parrot: "a beard" };
39
40    // Try in frame's scope.
41    var local_expression =
42        "(what_is_capybara ? what_is_capybara : 'a beast') + '/' + what_is_parrot";
43    var result = evaluate_callback.in_top_frame(exec_state, local_expression, context);
44    assertEquals('a fish/a bird', result);
45
46    // Try in frame's scope with overrididen local variables.
47    var result = evaluate_callback.in_top_frame(exec_state, local_expression, context2);
48    assertEquals('a fish/a beard', result);
49
50    // Try in frame's scope, without context.
51    var local_expression2 = "what_is_parrot";
52    var result = evaluate_callback.in_top_frame(exec_state, local_expression2, void 0);
53    assertEquals('a bird', result);
54
55    // Try in global additional scope.
56    var global_expression = "what_is_capybara ? what_is_capybara : 'a beast'";
57    var result = evaluate_callback.globally(exec_state, global_expression, context);
58    assertEquals('a fish', result);
59
60    // Try in global scope with overridden global variables.
61    var context_with_undefined = { undefined: 'kitten' };
62    var global_expression2 = "'cat' + '/' + undefined";
63    var result = evaluate_callback.globally(exec_state, global_expression2, context_with_undefined);
64    assertEquals('cat/kitten', result);
65
66    // Try in global scope with no overridden global variables.
67    var result = evaluate_callback.globally(exec_state, global_expression2, void 0);
68    assertEquals('cat/undefined', result);
69
70    // Try in global scope without additional context.
71    var global_expression3 = "'cat' + '/' + 'dog'";
72    var result = evaluate_callback.globally(exec_state, global_expression3, void 0);
73    assertEquals('cat/dog', result);
74
75    listenerComplete = true;
76  } catch (e) {
77    exception = e
78  };
79};
80
81
82function f() {
83  var what_is_parrot = "a bird";
84  debugger;
85};
86
87function runF() {
88  exception = false;
89  listenerComplete = false;
90
91  Debug.setListener(listener);
92
93  // Add the debug event listener.
94  Debug.setListener(listener);
95
96  f();
97
98  assertFalse(exception, "exception in listener")
99  assertTrue(listenerComplete);
100}
101
102evaluate_callback = {
103  in_top_frame: function(exec_state, expression, additional_context) {
104    return exec_state.frame(0).evaluate(expression, void 0, additional_context).value();
105  },
106  globally: function(exec_state, expression, additional_context) {
107    return exec_state.evaluateGlobal(expression, void 0, additional_context).value();
108  },
109};
110
111
112runF();
113
114// Now try all the same, but via debug protocol.
115
116function evaluateViaProtocol(exec_state, expression, additional_context, frame_argument_adder) {
117  var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
118  request_json = {"seq":17,"type":"request","command":"evaluate", arguments: { "expression": expression } };
119  frame_argument_adder(request_json.arguments);
120  if (additional_context) {
121    var context_json = [];
122    for (var key in additional_context) {
123      context_json.push({ name: key, handle: Debug.MakeMirror(additional_context[key]).handle() });
124    }
125    request_json.arguments.additional_context = context_json;
126  }
127  var request = JSON.stringify(request_json);
128  var response_json = dcp.processDebugJSONRequest(request);
129  var response = JSON.parse(response_json);
130
131  assertTrue(response.success);
132  var str_result = response.body.value;
133  return str_result;
134}
135
136evaluate_callback = {
137  in_top_frame: function(exec_state, expression, additional_context) {
138    return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.frame = 0; });
139  },
140  globally: function(exec_state, expression, additional_context) {
141    return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.global = true; });
142  },
143};
144
145runF();
146