blob: 1729408e5ce0c644efb99213e39de0a425067b99 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/debug/debug-evaluate.h"
6
7#include "src/accessors.h"
8#include "src/contexts.h"
9#include "src/debug/debug.h"
10#include "src/debug/debug-frames.h"
11#include "src/debug/debug-scopes.h"
12#include "src/frames-inl.h"
13#include "src/isolate-inl.h"
14
15namespace v8 {
16namespace internal {
17
18static inline bool IsDebugContext(Isolate* isolate, Context* context) {
19 return context->native_context() == *isolate->debug()->debug_context();
20}
21
22
23MaybeHandle<Object> DebugEvaluate::Global(
24 Isolate* isolate, Handle<String> source, bool disable_break,
25 Handle<HeapObject> context_extension) {
26 // Handle the processing of break.
27 DisableBreak disable_break_scope(isolate->debug(), disable_break);
28
29 // Enter the top context from before the debugger was invoked.
30 SaveContext save(isolate);
31 SaveContext* top = &save;
32 while (top != NULL && IsDebugContext(isolate, *top->context())) {
33 top = top->prev();
34 }
35 if (top != NULL) isolate->set_context(*top->context());
36
37 // Get the native context now set to the top context from before the
38 // debugger was invoked.
39 Handle<Context> context = isolate->native_context();
40 Handle<JSObject> receiver(context->global_proxy());
41 Handle<SharedFunctionInfo> outer_info(context->closure()->shared(), isolate);
42 return Evaluate(isolate, outer_info, context, context_extension, receiver,
43 source);
44}
45
46
47MaybeHandle<Object> DebugEvaluate::Local(Isolate* isolate,
48 StackFrame::Id frame_id,
49 int inlined_jsframe_index,
50 Handle<String> source,
51 bool disable_break,
52 Handle<HeapObject> context_extension) {
53 // Handle the processing of break.
54 DisableBreak disable_break_scope(isolate->debug(), disable_break);
55
56 // Get the frame where the debugging is performed.
57 JavaScriptFrameIterator it(isolate, frame_id);
58 JavaScriptFrame* frame = it.frame();
59
60 // Traverse the saved contexts chain to find the active context for the
61 // selected frame.
62 SaveContext* save =
63 DebugFrameHelper::FindSavedContextForFrame(isolate, frame);
64 SaveContext savex(isolate);
65 isolate->set_context(*(save->context()));
66
67 // This is not a lot different than DebugEvaluate::Global, except that
68 // variables accessible by the function we are evaluating from are
69 // materialized and included on top of the native context. Changes to
70 // the materialized object are written back afterwards.
71 // Note that the native context is taken from the original context chain,
72 // which may not be the current native context of the isolate.
73 ContextBuilder context_builder(isolate, frame, inlined_jsframe_index);
74 if (isolate->has_pending_exception()) return MaybeHandle<Object>();
75
Ben Murdochda12d292016-06-02 14:46:10 +010076 Handle<Context> context = context_builder.evaluation_context();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 Handle<JSObject> receiver(context->global_proxy());
Ben Murdochda12d292016-06-02 14:46:10 +010078 MaybeHandle<Object> maybe_result =
79 Evaluate(isolate, context_builder.outer_info(), context,
80 context_extension, receiver, source);
81 if (!maybe_result.is_null()) context_builder.UpdateValues();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 return maybe_result;
83}
84
85
86// Compile and evaluate source for the given context.
87MaybeHandle<Object> DebugEvaluate::Evaluate(
88 Isolate* isolate, Handle<SharedFunctionInfo> outer_info,
89 Handle<Context> context, Handle<HeapObject> context_extension,
90 Handle<Object> receiver, Handle<String> source) {
91 if (context_extension->IsJSObject()) {
92 Handle<JSObject> extension = Handle<JSObject>::cast(context_extension);
93 Handle<JSFunction> closure(context->closure(), isolate);
94 context = isolate->factory()->NewWithContext(closure, context, extension);
95 }
96
97 Handle<JSFunction> eval_fun;
98 ASSIGN_RETURN_ON_EXCEPTION(isolate, eval_fun,
99 Compiler::GetFunctionFromEval(
100 source, outer_info, context, SLOPPY,
101 NO_PARSE_RESTRICTION, RelocInfo::kNoPosition),
102 Object);
103
104 Handle<Object> result;
105 ASSIGN_RETURN_ON_EXCEPTION(
106 isolate, result, Execution::Call(isolate, eval_fun, receiver, 0, NULL),
107 Object);
108
109 // Skip the global proxy as it has no properties and always delegates to the
110 // real global object.
111 if (result->IsJSGlobalProxy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100112 PrototypeIterator iter(isolate, Handle<JSGlobalProxy>::cast(result));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 // TODO(verwaest): This will crash when the global proxy is detached.
114 result = PrototypeIterator::GetCurrent<JSObject>(iter);
115 }
116
117 return result;
118}
119
120
121DebugEvaluate::ContextBuilder::ContextBuilder(Isolate* isolate,
122 JavaScriptFrame* frame,
123 int inlined_jsframe_index)
124 : isolate_(isolate),
125 frame_(frame),
126 inlined_jsframe_index_(inlined_jsframe_index) {
127 FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate);
128 Handle<JSFunction> local_function =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100129 Handle<JSFunction>::cast(frame_inspector.GetFunction());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 Handle<Context> outer_context(local_function->context());
Ben Murdochda12d292016-06-02 14:46:10 +0100131 evaluation_context_ = outer_context;
132 outer_info_ = handle(local_function->shared());
133 Factory* factory = isolate->factory();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134
Ben Murdochda12d292016-06-02 14:46:10 +0100135 // To evaluate as if we were running eval at the point of the debug break,
136 // we reconstruct the context chain as follows:
137 // - To make stack-allocated variables visible, we materialize them and
138 // use a debug-evaluate context to wrap both the materialized object and
139 // the original context.
140 // - We use the original context chain from the function context to the
141 // native context.
142 // - Between the function scope and the native context, we only resolve
143 // variable names that the current function already uses. Only for these
144 // names we can be sure that they will be correctly resolved. For the
145 // rest, we only resolve to with, script, and native contexts. We use a
146 // whitelist to implement that.
147 // Context::Lookup has special handling for debug-evaluate contexts:
148 // - Look up in the materialized stack variables.
149 // - Look up in the original context.
150 // - Check the whitelist to find out whether to skip contexts during lookup.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 const ScopeIterator::Option option = ScopeIterator::COLLECT_NON_LOCALS;
152 for (ScopeIterator it(isolate, &frame_inspector, option);
Ben Murdochda12d292016-06-02 14:46:10 +0100153 !it.Failed() && !it.Done(); it.Next()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 ScopeIterator::ScopeType scope_type = it.Type();
155 if (scope_type == ScopeIterator::ScopeTypeLocal) {
156 DCHECK_EQ(FUNCTION_SCOPE, it.CurrentScopeInfo()->scope_type());
Ben Murdochda12d292016-06-02 14:46:10 +0100157 Handle<JSObject> materialized = factory->NewJSObjectWithNullProto();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158 Handle<Context> local_context =
159 it.HasContext() ? it.CurrentContext() : outer_context;
Ben Murdochda12d292016-06-02 14:46:10 +0100160 Handle<StringSet> non_locals = it.GetNonLocals();
161 MaterializeReceiver(materialized, local_context, local_function,
162 non_locals);
163 frame_inspector.MaterializeStackLocals(materialized, local_function);
164 MaterializeArgumentsObject(materialized, local_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 ContextChainElement context_chain_element;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 context_chain_element.scope_info = it.CurrentScopeInfo();
Ben Murdochda12d292016-06-02 14:46:10 +0100167 context_chain_element.materialized_object = materialized;
168 // Non-locals that are already being referenced by the current function
169 // are guaranteed to be correctly resolved.
170 context_chain_element.whitelist = non_locals;
171 if (it.HasContext()) {
172 context_chain_element.wrapped_context = it.CurrentContext();
173 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 context_chain_.Add(context_chain_element);
Ben Murdochda12d292016-06-02 14:46:10 +0100175 evaluation_context_ = outer_context;
176 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177 } else if (scope_type == ScopeIterator::ScopeTypeCatch ||
178 scope_type == ScopeIterator::ScopeTypeWith) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179 ContextChainElement context_chain_element;
Ben Murdochda12d292016-06-02 14:46:10 +0100180 Handle<Context> current_context = it.CurrentContext();
181 if (!current_context->IsDebugEvaluateContext()) {
182 context_chain_element.wrapped_context = current_context;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 }
Ben Murdochda12d292016-06-02 14:46:10 +0100184 context_chain_.Add(context_chain_element);
185 } else if (scope_type == ScopeIterator::ScopeTypeBlock) {
186 Handle<JSObject> materialized = factory->NewJSObjectWithNullProto();
187 frame_inspector.MaterializeStackLocals(materialized,
188 it.CurrentScopeInfo());
189 ContextChainElement context_chain_element;
190 context_chain_element.scope_info = it.CurrentScopeInfo();
191 context_chain_element.materialized_object = materialized;
192 if (it.HasContext()) {
193 context_chain_element.wrapped_context = it.CurrentContext();
194 }
195 context_chain_.Add(context_chain_element);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 } else {
Ben Murdochda12d292016-06-02 14:46:10 +0100197 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000198 }
199 }
Ben Murdochda12d292016-06-02 14:46:10 +0100200
201 for (int i = context_chain_.length() - 1; i >= 0; i--) {
202 evaluation_context_ = factory->NewDebugEvaluateContext(
203 evaluation_context_, context_chain_[i].materialized_object,
204 context_chain_[i].wrapped_context, context_chain_[i].whitelist);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206}
207
208
209void DebugEvaluate::ContextBuilder::UpdateValues() {
210 // TODO(yangguo): remove updating values.
211 for (int i = 0; i < context_chain_.length(); i++) {
212 ContextChainElement element = context_chain_[i];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 if (!element.materialized_object.is_null()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100214 // Write back potential changes to materialized stack locals to the stack.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215 FrameInspector(frame_, inlined_jsframe_index_, isolate_)
216 .UpdateStackLocalsFromMaterializedObject(element.materialized_object,
217 element.scope_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218 }
219 }
220}
221
222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223void DebugEvaluate::ContextBuilder::MaterializeArgumentsObject(
224 Handle<JSObject> target, Handle<JSFunction> function) {
225 // Do not materialize the arguments object for eval or top-level code.
226 // Skip if "arguments" is already taken.
227 if (!function->shared()->is_function()) return;
228 Maybe<bool> maybe = JSReceiver::HasOwnProperty(
229 target, isolate_->factory()->arguments_string());
230 DCHECK(maybe.IsJust());
231 if (maybe.FromJust()) return;
232
233 // FunctionGetArguments can't throw an exception.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100234 Handle<JSObject> arguments = Accessors::FunctionGetArguments(function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235 Handle<String> arguments_str = isolate_->factory()->arguments_string();
236 JSObject::SetOwnPropertyIgnoreAttributes(target, arguments_str, arguments,
237 NONE)
238 .Check();
239}
240
Ben Murdochda12d292016-06-02 14:46:10 +0100241void DebugEvaluate::ContextBuilder::MaterializeReceiver(
242 Handle<JSObject> target, Handle<Context> local_context,
243 Handle<JSFunction> local_function, Handle<StringSet> non_locals) {
244 Handle<Object> recv = isolate_->factory()->undefined_value();
245 Handle<String> name = isolate_->factory()->this_string();
246 if (non_locals->Has(name)) {
247 // 'this' is allocated in an outer context and is is already being
248 // referenced by the current function, so it can be correctly resolved.
249 return;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 } else if (local_function->shared()->scope_info()->HasReceiver()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100251 recv = handle(frame_->receiver(), isolate_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 }
Ben Murdochda12d292016-06-02 14:46:10 +0100253 JSObject::SetOwnPropertyIgnoreAttributes(target, name, recv, NONE).Check();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254}
255
256} // namespace internal
257} // namespace v8