Commit 8c3e13ca authored by peter.rybin@gmail.com's avatar peter.rybin@gmail.com

Introduce additional context to evaluate operations

Review URL: http://codereview.chromium.org/5733001

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5997 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 3fe2325d
......@@ -858,6 +858,7 @@ Debug.debuggerFlags = function() {
return debugger_flags;
};
Debug.MakeMirror = MakeMirror;
function MakeExecutionState(break_id) {
return new ExecutionState(break_id);
......@@ -876,9 +877,11 @@ ExecutionState.prototype.prepareStep = function(opt_action, opt_count) {
return %PrepareStep(this.break_id, action, count);
}
ExecutionState.prototype.evaluateGlobal = function(source, disable_break) {
return MakeMirror(
%DebugEvaluateGlobal(this.break_id, source, Boolean(disable_break)));
ExecutionState.prototype.evaluateGlobal = function(source, disable_break,
opt_additional_context) {
return MakeMirror(%DebugEvaluateGlobal(this.break_id, source,
Boolean(disable_break),
opt_additional_context));
};
ExecutionState.prototype.frameCount = function() {
......@@ -1837,6 +1840,7 @@ DebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) {
var frame = request.arguments.frame;
var global = request.arguments.global;
var disable_break = request.arguments.disable_break;
var additional_context = request.arguments.additional_context;
// The expression argument could be an integer so we convert it to a
// string.
......@@ -1851,11 +1855,26 @@ DebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) {
return response.failed('Arguments "frame" and "global" are exclusive');
}
var additional_context_object;
if (additional_context) {
additional_context_object = {};
for (var key in additional_context) {
var context_value_handle = additional_context[key];
var context_value_mirror = LookupMirror(context_value_handle);
if (!context_value_mirror) {
return response.failed(
"Context object '" + key + "' #" + context_value_handle +
"# not found");
}
additional_context_object[key] = context_value_mirror.value();
}
}
// Global evaluate.
if (global) {
// Evaluate in the global context.
response.body =
this.exec_state_.evaluateGlobal(expression, Boolean(disable_break));
response.body = this.exec_state_.evaluateGlobal(
expression, Boolean(disable_break), additional_context_object);
return;
}
......@@ -1877,12 +1896,12 @@ DebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) {
}
// Evaluate in the specified frame.
response.body = this.exec_state_.frame(frame_number).evaluate(
expression, Boolean(disable_break));
expression, Boolean(disable_break), additional_context_object);
return;
} else {
// Evaluate in the selected frame.
response.body = this.exec_state_.frame().evaluate(
expression, Boolean(disable_break));
expression, Boolean(disable_break), additional_context_object);
return;
}
};
......
......@@ -1533,9 +1533,9 @@ FrameMirror.prototype.scope = function(index) {
};
FrameMirror.prototype.evaluate = function(source, disable_break) {
FrameMirror.prototype.evaluate = function(source, disable_break, opt_context_object) {
var result = %DebugEvaluate(this.break_id_, this.details_.frameId(),
source, Boolean(disable_break));
source, Boolean(disable_break), opt_context_object);
return MakeMirror(result);
};
......
......@@ -9689,7 +9689,7 @@ static MaybeObject* Runtime_DebugEvaluate(Arguments args) {
// Check the execution state and decode arguments frame and source to be
// evaluated.
ASSERT(args.length() == 4);
ASSERT(args.length() == 5);
Object* check_result;
{ MaybeObject* maybe_check_result = Runtime_CheckExecutionState(args);
if (!maybe_check_result->ToObject(&check_result)) {
......@@ -9699,6 +9699,7 @@ static MaybeObject* Runtime_DebugEvaluate(Arguments args) {
CONVERT_CHECKED(Smi, wrapped_id, args[1]);
CONVERT_ARG_CHECKED(String, source, 2);
CONVERT_BOOLEAN_CHECKED(disable_break, args[3]);
Handle<Object> additional_context(args[4]);
// Handle the processing of break.
DisableBreak disable_break_save(disable_break);
......@@ -9749,6 +9750,11 @@ static MaybeObject* Runtime_DebugEvaluate(Arguments args) {
Handle<Context> function_context(frame_context->fcontext());
context = CopyWithContextChain(frame_context, context);
if (additional_context->IsJSObject()) {
context = Factory::NewWithContext(context,
Handle<JSObject>::cast(additional_context), false);
}
// Wrap the evaluation statement in a new function compiled in the newly
// created context. The function has one parameter which has to be called
// 'arguments'. This it to have access to what would have been 'arguments' in
......@@ -9803,7 +9809,7 @@ static MaybeObject* Runtime_DebugEvaluateGlobal(Arguments args) {
// Check the execution state and decode arguments frame and source to be
// evaluated.
ASSERT(args.length() == 3);
ASSERT(args.length() == 4);
Object* check_result;
{ MaybeObject* maybe_check_result = Runtime_CheckExecutionState(args);
if (!maybe_check_result->ToObject(&check_result)) {
......@@ -9812,6 +9818,7 @@ static MaybeObject* Runtime_DebugEvaluateGlobal(Arguments args) {
}
CONVERT_ARG_CHECKED(String, source, 1);
CONVERT_BOOLEAN_CHECKED(disable_break, args[2]);
Handle<Object> additional_context(args[3]);
// Handle the processing of break.
DisableBreak disable_break_save(disable_break);
......@@ -9830,11 +9837,24 @@ static MaybeObject* Runtime_DebugEvaluateGlobal(Arguments args) {
// debugger was invoked.
Handle<Context> context = Top::global_context();
bool is_global = true;
if (additional_context->IsJSObject()) {
// Create a function context first, than put 'with' context on top of it.
Handle<JSFunction> go_between = Factory::NewFunction(
Factory::empty_string(), Factory::undefined_value());
go_between->set_context(*context);
context =
Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, go_between);
context->set_extension(JSObject::cast(*additional_context));
is_global = false;
}
// Compile the source to be evaluated.
Handle<SharedFunctionInfo> shared =
Compiler::CompileEval(source,
context,
true);
is_global);
if (shared.is_null()) return Failure::Exception();
Handle<JSFunction> compiled_function =
Handle<JSFunction>(Factory::NewFunctionFromSharedFunctionInfo(shared,
......
......@@ -343,8 +343,8 @@ namespace internal {
F(IsBreakOnException, 1, 1) \
F(PrepareStep, 3, 1) \
F(ClearStepping, 0, 1) \
F(DebugEvaluate, 4, 1) \
F(DebugEvaluateGlobal, 3, 1) \
F(DebugEvaluate, 5, 1) \
F(DebugEvaluateGlobal, 4, 1) \
F(DebugGetLoadedScripts, 0, 1) \
F(DebugReferencedBy, 3, 1) \
F(DebugConstructedBy, 2, 1) \
......
// Copyright 2008 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Flags: --expose-debug-as debug
// Get the Debug object exposed from the debug context global object.
Debug = debug.Debug
var evaluate_callback;
function listener(event, exec_state, event_data, data) {
try {
var context = { what_is_capybara: "a fish" };
var context2 = { what_is_capybara: "a fish", what_is_parrot: "a beard" };
// Try in frame's scope.
var local_expression =
"(what_is_capybara ? what_is_capybara : 'a beast') + '/' + what_is_parrot";
var result = evaluate_callback.in_top_frame(exec_state, local_expression, context);
assertEquals('a fish/a bird', result);
// Try in frame's scope with overrididen local variables.
var result = evaluate_callback.in_top_frame(exec_state, local_expression, context2);
assertEquals('a fish/a beard', result);
// Try in frame's scope, without context.
var local_expression2 = "what_is_parrot";
var result = evaluate_callback.in_top_frame(exec_state, local_expression2, void 0);
assertEquals('a bird', result);
// Try in global additional scope.
var global_expression = "what_is_capybara ? what_is_capybara : 'a beast'";
var result = evaluate_callback.globally(exec_state, global_expression, context);
assertEquals('a fish', result);
// Try in global scope with overridden global variables.
var context_with_undefined = { undefined: 'kitten' };
var global_expression2 = "'cat' + '/' + undefined";
var result = evaluate_callback.globally(exec_state, global_expression2, context_with_undefined);
assertEquals('cat/kitten', result);
// Try in global scope with no overridden global variables.
var result = evaluate_callback.globally(exec_state, global_expression2, void 0);
assertEquals('cat/undefined', result);
// Try in global scope without additional context.
var global_expression3 = "'cat' + '/' + 'dog'";
var result = evaluate_callback.globally(exec_state, global_expression3, void 0);
assertEquals('cat/dog', result);
listenerComplete = true;
} catch (e) {
exception = e
};
};
function f() {
var what_is_parrot = "a bird";
debugger;
};
function runF() {
exception = false;
listenerComplete = false;
Debug.setListener(listener);
// Add the debug event listener.
Debug.setListener(listener);
f();
assertFalse(exception, "exception in listener")
assertTrue(listenerComplete);
}
evaluate_callback = {
in_top_frame: function(exec_state, expression, additional_context) {
return exec_state.frame(0).evaluate(expression, void 0, additional_context).value();
},
globally: function(exec_state, expression, additional_context) {
return exec_state.evaluateGlobal(expression, void 0, additional_context).value();
},
};
runF();
// Now try all the same, but via debug protocol.
function evaluateViaProtocol(exec_state, expression, additional_context, frame_argument_adder) {
var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
request_json = {"seq":17,"type":"request","command":"evaluate", arguments: { "expression": expression } };
frame_argument_adder(request_json.arguments);
if (additional_context) {
var context_json = {}
for (var key in additional_context) {
context_json[key] = Debug.MakeMirror(additional_context[key]).handle();
}
request_json.arguments.additional_context = context_json;
}
var request = JSON.stringify(request_json);
var response_json = dcp.processDebugJSONRequest(request);
var response = JSON.parse(response_json);
assertTrue(response.success);
var str_result = response.body.value;
return str_result;
}
evaluate_callback = {
in_top_frame: function(exec_state, expression, additional_context) {
return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.frame = 0; });
},
globally: function(exec_state, expression, additional_context) {
return evaluateViaProtocol(exec_state, expression, additional_context, function(args) { args.global = true; });
},
};
runF();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment