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  try {
36    var context = { what_is_capybara: "a fish" };
37    var context2 = { what_is_capybara: "a fish", what_is_parrot: "a beard" };
38
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);
44
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);
48
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);
53
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);
58
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);
64
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);
68
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);
73
74    listenerComplete = true;
75  } catch (e) {
76    exception = e
77  };
78};
79
80
81function f() {
82  var what_is_parrot = "a bird";
83  debugger;
84};
85
86function runF() {
87  exception = false;
88  listenerComplete = false;
89
90  Debug.setListener(listener);
91
92  // Add the debug event listener.
93  Debug.setListener(listener);
94
95  f();
96
97  assertFalse(exception, "exception in listener")
98  assertTrue(listenerComplete);
99}
100
101evaluate_callback = {
102  in_top_frame: function(exec_state, expression, additional_context) {
103    return exec_state.frame(0).evaluate(expression, void 0, additional_context).value();
104  },
105  globally: function(exec_state, expression, additional_context) {
106    return exec_state.evaluateGlobal(expression, void 0, additional_context).value();
107  },
108};
109
110
111runF();
112
113// Now try all the same, but via debug protocol.
114
115function 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() });
123    }
124    request_json.arguments.additional_context = context_json;
125  }
126  var request = JSON.stringify(request_json);
127  var response_json = dcp.processDebugJSONRequest(request);
128  var response = JSON.parse(response_json);
129
130  assertTrue(response.success);
131  var str_result = response.body.value;
132  return str_result;
133}
134
135evaluate_callback = {
136  in_top_frame: function(exec_state, expression, additional_context) {
137    return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.frame = 0; });
138  },
139  globally: function(exec_state, expression, additional_context) {
140    return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.global = true; });
141  },
142};
143
144runF();
145