Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: Source/bindings/dart/DartInjectedScript.cpp

Issue 300393002: Merge DevTools Refactor CL to Blink36 (Closed) Base URL: svn://svn.chromium.org/blink/branches/dart/1985
Patch Set: PTAL Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/bindings/dart/DartInjectedScript.h ('k') | Source/bindings/dart/DartScriptDebugServer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #include "bindings/dart/DartInjectedScript.h"
34
35 #include "DartInjectedScriptHost.h"
36 #include "DartNode.h"
37 #include "bindings/dart/DartHandleProxy.h"
38 #include "bindings/dart/DartJsInterop.h"
39 #include "bindings/dart/DartScriptDebugServer.h"
40 #include "bindings/dart/DartScriptState.h"
41 #include "bindings/dart/DartUtilities.h"
42 #include "bindings/dart/V8Converter.h"
43 #include "bindings/v8/ScriptFunctionCall.h"
44 #include "core/inspector/InjectedScriptHost.h"
45 #include "core/inspector/JSONParser.h"
46 #include "platform/JSONValues.h"
47
48 using WebCore::TypeBuilder::Array;
49 using WebCore::TypeBuilder::Debugger::CallFrame;
50 using WebCore::TypeBuilder::Debugger::Location;
51 using WebCore::TypeBuilder::Debugger::Scope;
52 using WebCore::TypeBuilder::Runtime::PropertyDescriptor;
53 using WebCore::TypeBuilder::Runtime::InternalPropertyDescriptor;
54 using WebCore::TypeBuilder::Debugger::FunctionDetails;
55 using WebCore::TypeBuilder::Runtime::RemoteObject;
56 using WebCore::TypeBuilder::Runtime::PropertyPreview;
57
58 namespace WebCore {
59
60 Dart_Handle getLibraryUrl(Dart_Handle handle)
61 {
62 intptr_t libraryId = 0;
63 Dart_Handle ALLOW_UNUSED result = Dart_LibraryId(handle, &libraryId);
64 ASSERT(!Dart_IsError(result));
65 Dart_Handle libraryUrl = Dart_GetLibraryURL(libraryId);
66 ASSERT(Dart_IsString(libraryUrl));
67 return libraryUrl;
68 }
69
70 Dart_Handle getObjectCompletions(Dart_Handle object, Dart_Handle library)
71 {
72 Dart_Handle args[2] = {object, getLibraryUrl(library)};
73 return DartUtilities::invokeUtilsMethod("getObjectCompletions", 2, args);
74 }
75
76 Dart_Handle getLibraryCompletions(Dart_Handle library)
77 {
78 Dart_Handle libraryUrl = getLibraryUrl(library);
79 return DartUtilities::invokeUtilsMethod("getLibraryCompletions", 1, &library Url);
80 }
81
82 Dart_Handle getLibraryCompletionsIncludingImports(Dart_Handle library)
83 {
84 Dart_Handle libraryUrl = getLibraryUrl(library);
85 return DartUtilities::invokeUtilsMethod("getLibraryCompletionsIncludingImpor ts", 1, &libraryUrl);
86 }
87
88 Dart_Handle getObjectProperties(Dart_Handle object, bool ownProperties, bool acc essorPropertiesOnly)
89 {
90 Dart_Handle args[3] = {object, DartUtilities::boolToDart(ownProperties), Dar tUtilities::boolToDart(accessorPropertiesOnly)};
91 return DartUtilities::invokeUtilsMethod("getObjectProperties", 3, args);
92 }
93
94 Dart_Handle getObjectPropertySafe(Dart_Handle object, const String& propertyName )
95 {
96 Dart_Handle args[2] = {object, DartUtilities::stringToDartString(propertyNam e)};
97 return DartUtilities::invokeUtilsMethod("getObjectPropertySafe", 2, args);
98 }
99
100 Dart_Handle getObjectClassProperties(Dart_Handle object, bool ownProperties, boo l accessorPropertiesOnly)
101 {
102 Dart_Handle args[3] = {object, DartUtilities::boolToDart(ownProperties), Dar tUtilities::boolToDart(accessorPropertiesOnly)};
103 return DartUtilities::invokeUtilsMethod("getObjectClassProperties", 3, args) ;
104 }
105
106 Dart_Handle getClassProperties(Dart_Handle kind, bool ownProperties, bool access orPropertiesOnly)
107 {
108 Dart_Handle args[3] = {kind, DartUtilities::boolToDart(ownProperties), DartU tilities::boolToDart(accessorPropertiesOnly)};
109 return DartUtilities::invokeUtilsMethod("getClassProperties", 3, args);
110 }
111
112 Dart_Handle getLibraryProperties(Dart_Handle library, bool ownProperties, bool a ccessorPropertiesOnly)
113 {
114 Dart_Handle args[3] = {getLibraryUrl(library), DartUtilities::boolToDart(own Properties), DartUtilities::boolToDart(accessorPropertiesOnly)};
115 return DartUtilities::invokeUtilsMethod("getLibraryProperties", 3, args);
116 }
117
118 Dart_Handle describeFunction(Dart_Handle function)
119 {
120 return DartUtilities::invokeUtilsMethod("describeFunction", 1, &function);
121 }
122
123 Dart_Handle getInvocationTrampolineDetails(Dart_Handle function)
124 {
125 return DartUtilities::invokeUtilsMethod("getInvocationTrampolineDetails", 1, &function);
126 }
127
128 Dart_Handle findReceiver(Dart_Handle locals)
129 {
130 intptr_t length = 0;
131 Dart_Handle ALLOW_UNUSED result = Dart_ListLength(locals, &length);
132 ASSERT(!Dart_IsError(result));
133 ASSERT(length % 2 == 0);
134 String thisStr("this");
135 for (intptr_t i = 0; i < length; i+= 2) {
136 Dart_Handle name = Dart_ListGetAt(locals, i);
137 if (DartUtilities::toString(name) == thisStr) {
138 Dart_Handle ret = Dart_ListGetAt(locals, i + 1);
139 return Dart_IsNull(ret) ? 0 : ret;
140 }
141 }
142 return 0;
143 }
144
145 Dart_Handle lookupEnclosingType(Dart_Handle functionOwner)
146 {
147 // Walk up the chain of function owners until we reach a type or library
148 // handle.
149 while (Dart_IsFunction(functionOwner))
150 functionOwner = Dart_FunctionOwner(functionOwner);
151 return functionOwner;
152 }
153
154 DartDebuggerObject::DartDebuggerObject(Dart_PersistentHandle h, const String& ob jectGroup, Kind kind)
155 : m_handle(h)
156 , m_group(objectGroup)
157 , m_kind(kind)
158 {
159 }
160
161 DartDebuggerObject::~DartDebuggerObject()
162 {
163 Dart_DeletePersistentHandle(m_handle);
164 }
165
166 DartInjectedScript::DartInjectedScript()
167 : m_name("DartInjectedScript")
168 , m_inspectedStateAccessCheck(0)
169 , m_scriptState(0)
170 , m_nextObjectId(1)
171 , m_host(0)
172 , m_consoleApi(0)
173 {
174 }
175
176 DartInjectedScript::DartInjectedScript(DartScriptState* scriptState, InspectedSt ateAccessCheck accessCheck, int injectedScriptId, InjectedScriptHost* host)
177 : m_name("DartInjectedScript")
178 , m_inspectedStateAccessCheck(accessCheck)
179 , m_scriptState(scriptState)
180 , m_nextObjectId(1)
181 , m_injectedScriptId(injectedScriptId)
182 , m_host(host)
183 , m_consoleApi(0)
184 {
185 }
186
187 bool DartInjectedScript::canAccessInspectedWindow() const
188 {
189 return m_inspectedStateAccessCheck(scriptState());
190 }
191
192 bool DartInjectedScript::validateObjectId(const String& objectId)
193 {
194 RefPtr<JSONValue> parsedObjectId = parseJSON(objectId);
195 if (parsedObjectId && parsedObjectId->type() == JSONValue::TypeObject) {
196 long injectedScriptId = 0;
197 bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId);
198 return success && injectedScriptId == m_injectedScriptId;
199 }
200 return false;
201 }
202
203 void DartInjectedScript::packageResult(Dart_Handle dartHandle, DartDebuggerObjec t::Kind kind, const String& objectGroup, ErrorString* errorString, bool returnBy Value, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
204 {
205 switch (kind) {
206 case DartDebuggerObject::Object:
207 packageObjectResult(dartHandle, objectGroup, errorString, returnByValue, generatePreview, result, wasThrown);
208 return;
209 case DartDebuggerObject::ObjectClass:
210 packageObjectClassResult(dartHandle, objectGroup, errorString, returnByV alue, generatePreview, result, wasThrown);
211 return;
212 case DartDebuggerObject::Function:
213 packageFunctionResult(dartHandle, objectGroup, errorString, returnByValu e, generatePreview, result, wasThrown);
214 return;
215 case DartDebuggerObject::Method:
216 packageMethodResult(dartHandle, objectGroup, errorString, returnByValue, generatePreview, result, wasThrown);
217 return;
218 case DartDebuggerObject::Class:
219 case DartDebuggerObject::StaticClass:
220 packageClassResult(dartHandle, kind, objectGroup, errorString, returnByV alue, generatePreview, result, wasThrown);
221 return;
222 case DartDebuggerObject::Library:
223 case DartDebuggerObject::CurrentLibrary:
224 packageLibraryResult(dartHandle, kind, objectGroup, errorString, returnB yValue, generatePreview, result, wasThrown);
225 return;
226 case DartDebuggerObject::Isolate:
227 packageIsolateResult(dartHandle, objectGroup, errorString, returnByValue , generatePreview, result, wasThrown);
228 return;
229 case DartDebuggerObject::LocalVariables:
230 packageLocalVariablesResult(dartHandle, objectGroup, errorString, return ByValue, generatePreview, result, wasThrown);
231 return;
232 case DartDebuggerObject::Error:
233 packageErrorResult(dartHandle, objectGroup, errorString, returnByValue, generatePreview, result, wasThrown);
234 return;
235 default:
236 ASSERT_NOT_REACHED();
237 }
238 }
239
240 DartInjectedScript::~DartInjectedScript()
241 {
242 DartIsolateScope scope(m_scriptState->isolate());
243 DartApiScope apiScope;
244
245 for (DebuggerObjectMap::iterator it = m_objects.begin(); it != m_objects.end (); ++it) {
246 delete it->value;
247 }
248
249 if (m_consoleApi)
250 Dart_DeletePersistentHandle(m_consoleApi);
251 }
252
253 Dart_Handle DartInjectedScript::consoleApi()
254 {
255 if (!m_consoleApi) {
256 Dart_Handle host = DartInjectedScriptHost::toDart(m_host);
257 Dart_SetPeer(host, this);
258 Dart_Handle consoleApi = DartUtilities::invokeUtilsMethod("consoleApi", 1, &host);
259 ASSERT(!Dart_IsError(consoleApi));
260 m_consoleApi = Dart_NewPersistentHandle(consoleApi);
261 }
262 return m_consoleApi;
263 }
264
265 Dart_Handle DartInjectedScript::evaluateHelper(Dart_Handle target, const String& rawExpression, Dart_Handle localVariables, bool includeCommandLineAPI, Dart_Han dle& exception)
266 {
267 DartDOMData* ALLOW_UNUSED domData = DartDOMData::current();
268 ASSERT(domData);
269 ASSERT(Dart_IsList(localVariables) || Dart_IsNull(localVariables));
270
271 Dart_Handle expression = DartUtilities::stringToDart(rawExpression);
272
273 if (includeCommandLineAPI) {
274 ASSERT(m_host);
275 // Vector of local variables and injected console variables.
276 Vector<Dart_Handle> locals;
277 if (Dart_IsList(localVariables)) {
278 DartUtilities::extractListElements(localVariables, exception, locals );
279 ASSERT(!exception);
280 }
281
282 ScriptState* v8ScriptState = DartUtilities::v8ScriptStateForCurrentIsola te();
283 for (unsigned i = 0; i < m_host->numInspectedObjects(); i++) {
284 ScriptValue value = m_host->inspectedObject(i)->get(v8ScriptState);
285 v8::TryCatch tryCatch;
286 v8::Handle<v8::Value> v8Value = value.v8Value();
287 Dart_Handle dartValue = DartHandleProxy::unwrapValue(v8Value);
288 ASSERT(!Dart_IsError(dartValue));
289 locals.append(DartUtilities::stringToDartString(String::format("$%d" , i)));
290 locals.append(dartValue);
291 }
292
293 Dart_Handle list = consoleApi();
294 intptr_t length = 0;
295 ASSERT(Dart_IsList(list));
296 Dart_Handle ALLOW_UNUSED ret = Dart_ListLength(list, &length);
297 ASSERT(!(length % 2));
298 ASSERT(!Dart_IsError(ret));
299 for (intptr_t i = 0; i < length; i += 2) {
300 Dart_Handle name = Dart_ListGetAt(list, i);
301 ASSERT(Dart_IsString(name));
302 locals.append(name);
303 Dart_Handle value = Dart_ListGetAt(list, i+1);
304 ASSERT(!Dart_IsError(value));
305 locals.append(value);
306 }
307 localVariables = DartUtilities::toList(locals, exception);
308 ASSERT(!exception);
309 }
310
311 Dart_Handle wrapExpressionArgs[3] = { expression, localVariables, DartUtilit ies::boolToDart(includeCommandLineAPI) };
312 Dart_Handle wrappedExpressionTuple =
313 DartUtilities::invokeUtilsMethod("wrapExpressionAsClosure", 3, wrapExpre ssionArgs);
314 ASSERT(Dart_IsList(wrappedExpressionTuple));
315 Dart_Handle wrappedExpression = Dart_ListGetAt(wrappedExpressionTuple, 0);
316 Dart_Handle wrappedExpressionArgs = Dart_ListGetAt(wrappedExpressionTuple, 1 );
317 // TODO(jacobr): replace most of this logic with Dart_ActivationFrameEvaluat e.
318
319 ASSERT(Dart_IsString(wrappedExpression));
320 Dart_Handle closure = Dart_EvaluateExpr(target, wrappedExpression);
321 // There was a parse error. FIXME: consider cleaning up the line numbers in
322 // the error message.
323 if (Dart_IsError(closure)) {
324 exception = closure;
325 return 0;
326 }
327
328 // Invoke the closure passing in the expression arguments specified by
329 // wrappedExpressionTuple.
330 ASSERT(DartUtilities::isFunction(domData, closure));
331 intptr_t length = 0;
332 Dart_ListLength(wrappedExpressionArgs, &length);
333 Vector<Dart_Handle> dartFunctionArgs;
334 for (intptr_t i = 0; i < length; i ++)
335 dartFunctionArgs.append(Dart_ListGetAt(wrappedExpressionArgs, i));
336
337 return Dart_InvokeClosure(closure, dartFunctionArgs.size(), dartFunctionArgs .data());
338 }
339
340 void DartInjectedScript::evaluateAndPackageResult(Dart_Handle target, const Stri ng& rawExpression, Dart_Handle localVariables, bool includeCommandLineAPI, const String& objectGroup, ErrorString* errorString, bool returnByValue, bool generat ePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOu tput<bool>* wasThrown)
341 {
342 Dart_Handle exception = 0;
343 {
344 Dart_Handle evalResult = evaluateHelper(target, rawExpression, localVari ables, includeCommandLineAPI, exception);
345 if (exception)
346 goto fail;
347
348 packageResult(evalResult, inferKind(evalResult), objectGroup, errorStrin g, returnByValue, generatePreview, result, wasThrown);
349 return;
350 }
351 fail:
352 ASSERT(exception);
353 packageResult(exception, inferKind(exception), objectGroup, errorString, ret urnByValue, generatePreview, result, wasThrown);
354 }
355
356 void DartInjectedScript::packageObjectResult(Dart_Handle dartHandle, const Strin g& objectGroup, ErrorString* errorString, bool returnByValue, bool generatePrevi ew, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<b ool>* wasThrown)
357 {
358 ASSERT(Dart_IsInstance(dartHandle) || Dart_IsNull(dartHandle));
359
360 // FIXMEDART: support returnByValue for Dart types that are expressible as J SON.
361 bool wasThrownVal = false;
362 Dart_Handle exception = 0;
363 if (Dart_IsError(dartHandle)) {
364 wasThrownVal = true;
365 Dart_Handle exception = Dart_ErrorGetException(dartHandle);
366 ASSERT(Dart_IsInstance(exception));
367 if (!Dart_IsInstance(exception)) {
368 *errorString = Dart_GetError(dartHandle);
369 return;
370 }
371 dartHandle = exception;
372 }
373
374 // Primitive value
375 RefPtr<JSONValue> value = nullptr;
376 TypeBuilder::Runtime::RemoteObject::Type::Enum remoteObjectType = TypeBuilde r::Runtime::RemoteObject::Type::Object;
377 ASSERT(Dart_IsInstance(dartHandle) || Dart_IsNull(dartHandle));
378
379 if (Dart_IsNull(dartHandle)) {
380 value = JSONValue::null();
381 } else {
382 if (Dart_IsString(dartHandle)) {
383 remoteObjectType = TypeBuilder::Runtime::RemoteObject::Type::String;
384 value = JSONString::create(DartUtilities::toString(dartHandle));
385 } else if (Dart_IsDouble(dartHandle)) {
386 // FIXMEDART: add an extra entry for int?
387 remoteObjectType = TypeBuilder::Runtime::RemoteObject::Type::Number;
388 value = JSONBasicValue::create(DartUtilities::dartToDouble(dartHandl e, exception));
389 ASSERT(!exception);
390 } else if (Dart_IsNumber(dartHandle)) {
391 // FIXMEDART: handle ints that are larger than 50 bits.
392 remoteObjectType = TypeBuilder::Runtime::RemoteObject::Type::Number;
393 value = JSONBasicValue::create(DartUtilities::dartToDouble(dartHandl e, exception));
394 ASSERT(!exception);
395 } else if (Dart_IsBoolean(dartHandle)) {
396 remoteObjectType = TypeBuilder::Runtime::RemoteObject::Type::Boolean ;
397 value = JSONBasicValue::create(DartUtilities::dartToBool(dartHandle, exception));
398 ASSERT(!exception);
399 }
400 }
401
402 String typeName;
403 String description;
404 bool isNode = false;
405 if (Dart_IsNull(dartHandle)) {
406 typeName = "null";
407 description = "null";
408 } else {
409 ASSERT(!exception);
410 description = DartUtilities::dartToString(Dart_ToString(dartHandle), exc eption);
411 Dart_Handle dartType = Dart_InstanceGetType(dartHandle);
412 Dart_Handle typeNameHandle = Dart_TypeName(dartType);
413 ASSERT(!Dart_IsError(typeNameHandle));
414 typeName = DartUtilities::dartToString(typeNameHandle, exception);
415 ASSERT(!exception);
416 if (DartDOMWrapper::subtypeOf(dartHandle, DartNode::dartClassId))
417 isNode = true;
418 }
419
420 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(remoteObjectType).release();
421 remoteObject->setLanguage("dart");
422 remoteObject->setClassName(typeName);
423 remoteObject->setDescription(description);
424 if (value)
425 remoteObject->setValue(value);
426
427 if (isNode)
428 remoteObject->setSubtype(RemoteObject::Subtype::Node);
429
430 // FIXMEDART: generate preview if generatePreview is true.
431 String objectId = cacheObject(dartHandle, objectGroup, DartDebuggerObject::O bject);
432 remoteObject->setObjectId(objectId);
433 *result = remoteObject;
434 if (wasThrown) {
435 *wasThrown = exception || wasThrownVal;
436 }
437 }
438
439 void DartInjectedScript::packageObjectClassResult(Dart_Handle dartHandle, const String& objectGroup, ErrorString* errorString, bool returnByValue, bool generate Preview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOut put<bool>* wasThrown)
440 {
441 ASSERT(Dart_IsInstance(dartHandle) || Dart_IsNull(dartHandle));
442 bool wasThrownVal = false;
443 Dart_Handle exception = 0;
444
445 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Object).release();
446 remoteObject->setLanguage("dart");
447
448 Dart_Handle typeHandle = Dart_InstanceGetType(dartHandle);
449 String typeName = DartUtilities::toString(Dart_TypeName(typeHandle));
450
451 remoteObject->setClassName(typeName);
452 remoteObject->setDescription(typeName);
453
454 // Don't generate a preview for types.
455 String objectId = cacheObject(dartHandle, objectGroup, DartDebuggerObject::O bjectClass);
456 remoteObject->setObjectId(objectId);
457 *result = remoteObject;
458 if (wasThrown)
459 *wasThrown = exception || wasThrownVal;
460 }
461
462 void DartInjectedScript::packageClassResult(Dart_Handle dartHandle, DartDebugger Object::Kind kind, const String& objectGroup, ErrorString* errorString, bool ret urnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* re sult, TypeBuilder::OptOutput<bool>* wasThrown)
463 {
464 bool wasThrownVal = false;
465 Dart_Handle exception = 0;
466
467 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Object).release();
468 remoteObject->setLanguage("dart");
469 String typeName = DartUtilities::toString(Dart_TypeName(dartHandle));
470 String typeDescription("class ");
471 typeDescription.append(typeName);
472
473 remoteObject->setClassName(typeName);
474 remoteObject->setDescription(typeDescription);
475
476 // Don't generate a preview for types.
477 String objectId = cacheObject(dartHandle, objectGroup, kind);
478 remoteObject->setObjectId(objectId);
479 *result = remoteObject;
480 if (wasThrown)
481 *wasThrown = exception || wasThrownVal;
482 }
483
484 void DartInjectedScript::packageFunctionResult(Dart_Handle dartHandle, const Str ing& objectGroup, ErrorString* errorString, bool returnByValue, bool generatePre view, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput <bool>* wasThrown)
485 {
486 ASSERT(DartUtilities::isFunction(DartDOMData::current(), dartHandle));
487 bool wasThrownVal = false;
488 Dart_Handle exception = 0;
489
490 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Function).release();
491 remoteObject->setLanguage("dart");
492 String description = DartUtilities::toString(describeFunction(dartHandle));
493 remoteObject->setClassName("<Dart Function>");
494 remoteObject->setDescription(description);
495
496 String objectId = cacheObject(dartHandle, objectGroup, DartDebuggerObject::F unction);
497 remoteObject->setObjectId(objectId);
498 *result = remoteObject;
499 if (wasThrown) {
500 *wasThrown = exception || wasThrownVal;
501 }
502 }
503
504 void DartInjectedScript::packageMethodResult(Dart_Handle dartHandle, const Strin g& objectGroup, ErrorString* errorString, bool returnByValue, bool generatePrevi ew, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<b ool>* wasThrown)
505 {
506 ASSERT(DartUtilities::isFunction(DartDOMData::current(), dartHandle));
507 bool wasThrownVal = false;
508 Dart_Handle exception = 0;
509
510 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Function).release();
511 remoteObject->setLanguage("dart");
512 String description = DartUtilities::toString(describeFunction(dartHandle));
513 remoteObject->setClassName("<Dart Method>");
514 remoteObject->setDescription(description);
515
516 String objectId = cacheObject(dartHandle, objectGroup, DartDebuggerObject::M ethod);
517 remoteObject->setObjectId(objectId);
518 *result = remoteObject;
519 if (wasThrown)
520 *wasThrown = exception || wasThrownVal;
521 }
522
523 void DartInjectedScript::packageLocalVariablesResult(Dart_Handle dartHandle, con st String& objectGroup, ErrorString* errorString, bool returnByValue, bool gener atePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::Opt Output<bool>* wasThrown)
524 {
525 bool wasThrownVal = false;
526 Dart_Handle exception = 0;
527
528 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Object).release();
529 remoteObject->setLanguage("dart");
530 remoteObject->setClassName("Object");
531 remoteObject->setDescription("Local Variables");
532
533 String objectId = cacheObject(dartHandle, objectGroup, DartDebuggerObject::L ocalVariables);
534 remoteObject->setObjectId(objectId);
535 *result = remoteObject;
536 if (wasThrown)
537 *wasThrown = exception || wasThrownVal;
538 }
539
540 void DartInjectedScript::packageErrorResult(Dart_Handle dartHandle, const String & objectGroup, ErrorString* errorString, bool returnByValue, bool generatePrevie w, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bo ol>* wasThrown)
541 {
542 ASSERT(Dart_IsError(dartHandle));
543 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Object).release();
544 remoteObject->setLanguage("dart");
545 remoteObject->setClassName("Error");
546 remoteObject->setDescription(Dart_GetError(dartHandle));
547
548 Dart_Handle exception = Dart_ErrorGetException(dartHandle);
549 String objectId = cacheObject(exception, objectGroup, DartDebuggerObject::Er ror);
550 remoteObject->setObjectId(objectId);
551 *result = remoteObject;
552 if (wasThrown)
553 *wasThrown = true;
554 }
555
556 void DartInjectedScript::packageLibraryResult(Dart_Handle dartHandle, DartDebugg erObject::Kind kind, const String& objectGroup, ErrorString* errorString, bool r eturnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
557 {
558 ASSERT(Dart_IsLibrary(dartHandle));
559 bool wasThrownVal = false;
560 intptr_t libraryId = 0;
561 Dart_Handle exception = 0;
562 Dart_Handle ALLOW_UNUSED ret;
563 ret = Dart_LibraryId(dartHandle, &libraryId);
564 ASSERT(!Dart_IsError(ret));
565
566 String libraryName = DartUtilities::toString(Dart_GetLibraryURL(libraryId));
567
568 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Object).release();
569 remoteObject->setLanguage("dart");
570 remoteObject->setClassName(libraryName);
571 remoteObject->setDescription("Dart Library");
572 String objectId = cacheObject(dartHandle, objectGroup, kind);
573 remoteObject->setObjectId(objectId);
574 *result = remoteObject;
575 if (wasThrown)
576 *wasThrown = exception || wasThrownVal;
577 }
578
579 void DartInjectedScript::packageIsolateResult(Dart_Handle dartHandle, const Stri ng& objectGroup, ErrorString* errorString, bool returnByValue, bool generatePrev iew, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeBuilder::OptOutput< bool>* wasThrown)
580 {
581 RefPtr<RemoteObject> remoteObject = TypeBuilder::Runtime::RemoteObject::crea te().setType(TypeBuilder::Runtime::RemoteObject::Type::Object).release();
582 remoteObject->setLanguage("dart");
583 remoteObject->setClassName("Dart Isolate");
584 remoteObject->setDescription("Dart Isolate");
585 String objectId = cacheObject(dartHandle, objectGroup, DartDebuggerObject::I solate);
586 remoteObject->setObjectId(objectId);
587 *result = remoteObject;
588 }
589
590 Dart_Handle DartInjectedScript::library()
591 {
592 ASSERT(m_scriptState);
593 return Dart_GetLibraryFromId(m_scriptState->libraryId());
594 }
595
596 void DartInjectedScript::evaluate(ErrorString* errorString, const String& expres sion, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeB uilder::OptOutput<bool>* wasThrown)
597 {
598 if (!m_scriptState) {
599 *errorString = "Invalid DartInjectedScript";
600 return;
601 }
602 DartIsolateScope scope(m_scriptState->isolate());
603 DartApiScope apiScope;
604 V8Scope v8scope(DartDOMData::current());
605 evaluateAndPackageResult(library(), expression, Dart_Null(), includeCommandL ineAPI, objectGroup, errorString, returnByValue, generatePreview, result, wasThr own);
606 }
607
608 void DartInjectedScript::callFunctionOn(ErrorString* errorString, const String& objectId, const String& expression, const String& arguments, bool returnByValue, bool generatePreview, RefPtr<TypeBuilder::Runtime::RemoteObject>* result, TypeB uilder::OptOutput<bool>* wasThrown)
609 {
610 Dart_Handle exception = 0;
611 String objectGroup;
612 {
613 if (!m_scriptState) {
614 *errorString = "Invalid DartInjectedScript";
615 return;
616 }
617 DartIsolateScope scope(m_scriptState->isolate());
618 DartApiScope apiScope;
619
620 DartDebuggerObject* object = lookupObject(objectId);
621 if (!object) {
622 *errorString = "Object has been deleted";
623 return;
624 }
625 objectGroup = object->group();
626 RefPtr<JSONValue> parsedArguments = parseJSON(arguments);
627 Vector<Dart_Handle> dartFunctionArgs;
628 if (!parsedArguments->isNull()) {
629 if (!parsedArguments->type() == JSONValue::TypeArray) {
630 *errorString = "Invalid arguments";
631 return;
632 }
633 RefPtr<JSONArray> argumentsArray = parsedArguments->asArray();
634 for (JSONArray::iterator it = argumentsArray->begin(); it != argumen tsArray->end(); ++it) {
635 RefPtr<JSONObject> arg;
636 if (!(*it)->asObject(&arg)) {
637 *errorString = "Invalid argument passed to callFunctionOn";
638 return;
639 }
640 String argObjectId;
641
642 if (!arg->getString("objectId", &argObjectId)) {
643 // FIXME: support primitive values passed as arguments as we ll.
644 *errorString = "Unspecified object id";
645 }
646
647 DartDebuggerObject* argObject = lookupObject(argObjectId);
648 if (!argObject) {
649 *errorString = "Argument has been deleted";
650 return;
651 }
652 dartFunctionArgs.append(argObject->handle());
653 }
654 }
655
656 Dart_Handle dartClosure = evaluateHelper(object->handle(), expression, D art_Null(), false, exception);
657 if (exception)
658 goto fail;
659
660 if (Dart_IsError(dartClosure)) {
661 *errorString = Dart_GetError(dartClosure);
662 return;
663 }
664 if (!Dart_IsClosure(dartClosure)) {
665 *errorString = "Given expression does not evaluate to a closure";
666 return;
667 }
668 Dart_Handle evalResult = Dart_InvokeClosure(dartClosure, dartFunctionArg s.size(), dartFunctionArgs.data());
669 packageResult(evalResult, inferKind(evalResult), objectGroup, errorStrin g, returnByValue, generatePreview, result, wasThrown);
670 return;
671 }
672 fail:
673 ASSERT(exception);
674 packageResult(exception, inferKind(exception), objectGroup, errorString, ret urnByValue, generatePreview, result, wasThrown);
675
676
677 }
678
679 void DartInjectedScript::evaluateOnCallFrame(ErrorString* errorString, const Sta ckTrace& callFrames, const Vector<StackTrace>& asyncCallStacks, const String& ca llFrameId, const String& expression, const String& objectGroup, bool includeComm andLineAPI, bool returnByValue, bool generatePreview, RefPtr<RemoteObject>* resu lt, TypeBuilder::OptOutput<bool>* wasThrown)
680 {
681 ASSERT(!callFrames.isJavaScript());
682 if (!m_scriptState) {
683 *errorString = "Invalid DartInjectedScript";
684 return;
685 }
686 DartIsolateScope scope(m_scriptState->isolate());
687 DartApiScope apiScope;
688 // FIXMEDART: add v8Scope calls elsewhere.
689 V8Scope v8scope(DartDOMData::current());
690
691 Dart_ActivationFrame frame = callFrameForId(callFrames, asyncCallStacks, cal lFrameId);
692 ASSERT(frame);
693 if (!frame) {
694 *errorString = "Call frame not found";
695 return;
696 }
697
698 Dart_Handle function = 0;
699 Dart_ActivationFrameGetLocation(frame, 0, &function, 0);
700 ASSERT(function);
701 Dart_Handle localVariables = Dart_GetLocalVariables(frame);
702 Dart_Handle thisHandle = findReceiver(localVariables);
703 Dart_Handle context = thisHandle ? thisHandle : lookupEnclosingType(function );
704 evaluateAndPackageResult(context, expression, localVariables, includeCommand LineAPI, objectGroup, errorString, returnByValue, generatePreview, result, wasTh rown);
705 }
706
707 void DartInjectedScript::restartFrame(ErrorString* errorString, const StackTrace & callFrames, const String& callFrameId, RefPtr<JSONObject>* result)
708 {
709 ASSERT(!callFrames.isJavaScript());
710 *errorString = "Dart does not yet support restarting call frames";
711 return;
712 }
713
714 void DartInjectedScript::getStepInPositions(ErrorString* errorString, const Stac kTrace& callFrames, const String& callFrameId, RefPtr<Array<TypeBuilder::Debugge r::Location> >& positions)
715 {
716 ASSERT(!callFrames.isJavaScript());
717 if (!m_scriptState) {
718 *errorString = "Invalid DartInjectedScript";
719 return;
720 }
721 DartIsolateScope scope(m_scriptState->isolate());
722 DartApiScope apiScope;
723 *errorString = "FIXME: support dart.";
724 return;
725 }
726
727 void DartInjectedScript::setVariableValue(ErrorString* errorString, const StackT race& callFrames, const String* callFrameIdOpt, const String* functionObjectIdOp t, int scopeNumber, const String& variableName, const String& newValueStr)
728 {
729 if (!m_scriptState) {
730 *errorString = "Invalid DartInjectedScript";
731 return;
732 }
733 DartIsolateScope scope(m_scriptState->isolate());
734 DartApiScope apiScope;
735 ASSERT(!callFrames.isJavaScript());
736 *errorString = "Not supported by Dart.";
737 return;
738 }
739
740 void DartInjectedScript::getFunctionDetails(ErrorString* errorString, const Stri ng& functionId, RefPtr<FunctionDetails>* result)
741 {
742 if (!m_scriptState) {
743 *errorString = "Invalid DartInjectedScript";
744 return;
745 }
746 DartIsolateScope scope(m_scriptState->isolate());
747 DartApiScope apiScope;
748 DartDebuggerObject* object = lookupObject(functionId);
749 if (!object) {
750 *errorString = "Object has been deleted";
751 return;
752 }
753
754 int line = 0;
755 int column = 0;
756 DartScriptDebugServer& debugServer = DartScriptDebugServer::shared();
757 Dart_Handle url;
758 Dart_Handle name = 0;
759 Dart_Handle exception = 0;
760
761 switch (object->kind()) {
762 case DartDebuggerObject::Function: {
763 Dart_CodeLocation location;
764 Dart_Handle ret = Dart_GetClosureInfo(object->handle(), &name, 0, &locat ion);
765 if (Dart_IsError(ret)) {
766 *errorString = "Unable to determine source location.";
767 return;
768 }
769
770 debugServer.resolveCodeLocation(location, &line, &column);
771 url = location.script_url;
772 break;
773 }
774 case DartDebuggerObject::Method:
775 {
776 Dart_Handle ret = getInvocationTrampolineDetails(object->handle());
777
778 if (Dart_IsError(ret)) {
779 *errorString = Dart_GetError(ret);
780 return;
781 }
782 ASSERT(Dart_IsList(ret));
783 line = DartUtilities::toInteger(Dart_ListGetAt(ret, 0), exception);
784 column = DartUtilities::toInteger(Dart_ListGetAt(ret, 1), exception) ;
785 url = Dart_ListGetAt(ret, 2);
786 name = Dart_ListGetAt(ret, 3);
787 break;
788 }
789 default:
790 *errorString = "Object is not a function.";
791 return;
792 }
793
794 ASSERT(!exception);
795
796 RefPtr<Location> locationJson = Location::create()
797 .setScriptId(debugServer.getScriptId(DartUtilities::toString(url), Dart_ CurrentIsolate()))
798 .setLineNumber(line - 1);
799 locationJson->setColumnNumber(column);
800
801 *result = FunctionDetails::create().setLocation(locationJson).setFunctionNam e(DartUtilities::toString(name)).release();
802 }
803
804 void addCompletions(Dart_Handle completions, RefPtr<TypeBuilder::Array<String> > * result)
805 {
806 ASSERT(Dart_IsList(completions));
807 intptr_t length = 0;
808 Dart_ListLength(completions, &length);
809 for (intptr_t i = 0; i < length; ++i)
810 (*result)->addItem(DartUtilities::toString(Dart_ListGetAt(completions, i )));
811 }
812
813 void DartInjectedScript::getCompletionsOnCallFrame(ErrorString* errorString, con st StackTrace& callFrames, const Vector<StackTrace>& asyncCallStacks, const Stri ng& callFrameId, const String& expression, RefPtr<TypeBuilder::Array<String> >* result)
814 {
815 ASSERT(!callFrames.isJavaScript());
816 *result = TypeBuilder::Array<String>::create();
817 if (!m_scriptState) {
818 *errorString = "Invalid DartInjectedScript";
819 return;
820 }
821 DartIsolateScope scope(m_scriptState->isolate());
822 DartApiScope apiScope;
823 V8Scope v8scope(DartDOMData::current());
824
825 Dart_ActivationFrame frame = callFrameForId(callFrames, asyncCallStacks, cal lFrameId);
826 ASSERT(frame);
827 if (!frame) {
828 *errorString = "Call frame not found";
829 return;
830 }
831
832 Dart_Handle function = 0;
833 Dart_ActivationFrameGetLocation(frame, 0, &function, 0);
834 ASSERT(function);
835 Dart_Handle localVariables = Dart_GetLocalVariables(frame);
836 Dart_Handle thisHandle = findReceiver(localVariables);
837 Dart_Handle enclosingType = lookupEnclosingType(function);
838 Dart_Handle context = thisHandle ? thisHandle : enclosingType;
839
840 if (expression.isEmpty()) {
841 addCompletions(getLibraryCompletionsIncludingImports(library()), result) ;
842 if (!Dart_IsLibrary(context)) {
843 addCompletions(getObjectCompletions(context, library()), result);
844 }
845 if (context != enclosingType) {
846 addCompletions(getObjectCompletions(enclosingType, library()), resul t);
847 }
848 intptr_t length = 0;
849 Dart_ListLength(localVariables, &length);
850 for (intptr_t i = 0; i < length; i += 2)
851 (*result)->addItem(DartUtilities::toString(Dart_ListGetAt(localVaria bles, i)));
852 } else {
853 // FIXME: we can do better than evaluating the expression and getting
854 // all completions for that object if an exception is not thrown. For
855 // example run the Dart Analyzer to get completions of complex
856 // expressions without triggering side effects or failing for
857 // expressions that do not evaluate to a first class object. For
858 // example, the html library is imported with prefix html and the
859 // expression html is used.
860 Dart_Handle exception = 0;
861 Dart_Handle handle = evaluateHelper(context, expression, localVariables, true, exception);
862
863 // No completions if the expression cannot be evaluated.
864 if (exception)
865 return;
866 addCompletions(getObjectCompletions(handle, library()), result);
867 }
868 }
869
870 void DartInjectedScript::getCompletions(ErrorString* errorString, const String& expression, RefPtr<TypeBuilder::Array<String> >* result)
871 {
872 *result = TypeBuilder::Array<String>::create();
873
874 if (!m_scriptState) {
875 *errorString = "Invalid DartInjectedScript";
876 return;
877 }
878 DartIsolateScope scope(m_scriptState->isolate());
879 DartApiScope apiScope;
880 V8Scope v8scope(DartDOMData::current());
881
882 Dart_Handle completions;
883 if (expression.isEmpty()) {
884 completions = getLibraryCompletionsIncludingImports(library());
885 } else {
886 // FIXME: we can do better than evaluating the expression and getting
887 // all completions for that object if an exception is not thrown. For
888 // example run the Dart Analyzer to get completions of complex
889 // expressions without triggering side effects or failing for
890 // expressions that do not evaluate to a first class object. For
891 // example, the html library is imported with prefix html and the
892 // expression html is used.
893 Dart_Handle exception = 0;
894 Dart_Handle handle = evaluateHelper(library(), expression, Dart_Null(), true, exception);
895 // No completions if the expression cannot be evaluated.
896 if (exception)
897 return;
898 completions = getObjectCompletions(handle, library());
899 }
900
901 addCompletions(completions, result);
902 }
903
904 void DartInjectedScript::getProperties(ErrorString* errorString, const String& o bjectId, bool ownProperties, bool accessorPropertiesOnly, RefPtr<Array<PropertyD escriptor> >* properties)
905 {
906 Dart_Handle exception = 0;
907 if (!m_scriptState) {
908 *errorString = "Invalid DartInjectedScript";
909 return;
910 }
911 DartIsolateScope scope(m_scriptState->isolate());
912 DartApiScope apiScope;
913
914 DartDebuggerObject* object = lookupObject(objectId);
915 if (!object) {
916 *errorString = "Unknown objectId";
917 return;
918 }
919 Dart_Handle handle = object->handle();
920 String objectGroup = object->group();
921
922 *properties = Array<PropertyDescriptor>::create();
923 Dart_Handle propertiesList;
924 switch (object->kind()) {
925 case DartDebuggerObject::Object:
926 case DartDebuggerObject::Function:
927 case DartDebuggerObject::Error:
928 propertiesList = getObjectProperties(handle, ownProperties, accessorProp ertiesOnly);
929 break;
930 case DartDebuggerObject::ObjectClass:
931 propertiesList = getObjectClassProperties(handle, ownProperties, accesso rPropertiesOnly);
932 break;
933 case DartDebuggerObject::Method:
934 // There aren't any meaningful properties to display for a Dart method.
935 return;
936 case DartDebuggerObject::Class:
937 case DartDebuggerObject::StaticClass:
938 propertiesList = getClassProperties(handle, ownProperties, accessorPrope rtiesOnly);
939 break;
940 case DartDebuggerObject::Library:
941 case DartDebuggerObject::CurrentLibrary:
942 propertiesList = getLibraryProperties(handle, ownProperties, accessorPro pertiesOnly);
943 break;
944 case DartDebuggerObject::LocalVariables:
945 {
946 if (accessorPropertiesOnly)
947 return;
948 ASSERT(Dart_IsList(handle));
949 intptr_t length = 0;
950 Dart_Handle ALLOW_UNUSED ret = Dart_ListLength(handle, &length);
951 ASSERT(!Dart_IsError(ret));
952 for (intptr_t i = 0; i < length; i += 2) {
953 const String& name = DartUtilities::toString(Dart_ListGetAt(hand le, i));
954 Dart_Handle value = Dart_ListGetAt(handle, i + 1);
955 RefPtr<PropertyDescriptor> descriptor = PropertyDescriptor::crea te().setName(name).setConfigurable(false).setEnumerable(true).release();
956 descriptor->setValue(wrapDartHandle(value, inferKind(value), obj ectGroup, false));
957 descriptor->setWritable(false);
958 descriptor->setWasThrown(false);
959 descriptor->setIsOwn(true);
960 (*properties)->addItem(descriptor);
961 }
962 return;
963 }
964 case DartDebuggerObject::Isolate:
965 {
966 if (accessorPropertiesOnly)
967 return;
968
969 Dart_Handle libraries = handle;
970 ASSERT(Dart_IsList(libraries));
971
972 intptr_t librariesLength = 0;
973 Dart_Handle ALLOW_UNUSED result = Dart_ListLength(libraries, &librar iesLength);
974 ASSERT(!Dart_IsError(result));
975 for (intptr_t i = 0; i < librariesLength; ++i) {
976 Dart_Handle libraryIdHandle = Dart_ListGetAt(libraries, i);
977 ASSERT(!Dart_IsError(libraryIdHandle));
978 Dart_Handle exception = 0;
979 int64_t libraryId = DartUtilities::toInteger(libraryIdHandle, ex ception);
980 const String& name = DartUtilities::toString(Dart_GetLibraryURL( libraryId));
981 RefPtr<PropertyDescriptor> descriptor = PropertyDescriptor::crea te().setName(name).setConfigurable(false).setEnumerable(true).release();
982 descriptor->setValue(wrapDartHandle(Dart_GetLibraryFromId(librar yId), DartDebuggerObject::Library, objectGroup, false));
983 descriptor->setWritable(false);
984 descriptor->setWasThrown(false);
985 descriptor->setIsOwn(true);
986 (*properties)->addItem(descriptor);
987 ASSERT(!exception);
988 }
989 return;
990 }
991 default:
992 ASSERT_NOT_REACHED();
993 *errorString = "Internal error";
994 return;
995 }
996
997 if (Dart_IsError(propertiesList)) {
998 *errorString = Dart_GetError(propertiesList);
999 return;
1000 }
1001
1002 ASSERT(Dart_IsList(propertiesList));
1003 intptr_t length = 0;
1004 Dart_Handle ALLOW_UNUSED ret = Dart_ListLength(propertiesList, &length);
1005 ASSERT(!Dart_IsError(ret));
1006 ASSERT(!(length % 9));
1007 for (intptr_t i = 0; i < length; i += 9) {
1008 String name = DartUtilities::toString(Dart_ListGetAt(propertiesList, i)) ;
1009 Dart_Handle setter = Dart_ListGetAt(propertiesList, i + 1);
1010 Dart_Handle getter = Dart_ListGetAt(propertiesList, i + 2);
1011 Dart_Handle value = Dart_ListGetAt(propertiesList, i + 3);
1012 bool hasValue = DartUtilities::dartToBool(Dart_ListGetAt(propertiesList, i + 4), exception);
1013 ASSERT(!exception);
1014 bool writable = DartUtilities::dartToBool(Dart_ListGetAt(propertiesList, i + 5), exception);
1015 ASSERT(!exception);
1016 bool isMethod = DartUtilities::dartToBool(Dart_ListGetAt(propertiesList, i + 6), exception);
1017 ASSERT(!exception);
1018 bool isOwn = DartUtilities::dartToBool(Dart_ListGetAt(propertiesList, i + 7), exception);
1019 ASSERT(!exception);
1020 bool wasThrown = DartUtilities::dartToBool(Dart_ListGetAt(propertiesList , i + 8), exception);
1021 ASSERT(!exception);
1022 RefPtr<PropertyDescriptor> descriptor = PropertyDescriptor::create().set Name(name).setConfigurable(false).setEnumerable(true).release();
1023 if (isMethod) {
1024 ASSERT(hasValue);
1025 descriptor->setValue(wrapDartHandle(value, DartDebuggerObject::Metho d, objectGroup, false));
1026 } else {
1027 if (hasValue)
1028 descriptor->setValue(wrapDartHandle(value, inferKind(value), obj ectGroup, false));
1029 if (!Dart_IsNull(setter))
1030 descriptor->setSet(wrapDartHandle(setter, DartDebuggerObject::Me thod, objectGroup, false));
1031 if (!Dart_IsNull(getter))
1032 descriptor->setGet(wrapDartHandle(getter, DartDebuggerObject::Me thod, objectGroup, false));
1033 }
1034 descriptor->setWritable(writable);
1035 descriptor->setWasThrown(wasThrown);
1036 descriptor->setIsOwn(isOwn);
1037
1038 (*properties)->addItem(descriptor);
1039 }
1040
1041 if (object->kind() == DartDebuggerObject::Object && !accessorPropertiesOnly && !Dart_IsNull(handle)) {
1042 RefPtr<PropertyDescriptor> descriptor = PropertyDescriptor::create().set Name("[[class]]").setConfigurable(false).setEnumerable(true).release();
1043 descriptor->setValue(wrapDartHandle(handle, DartDebuggerObject::ObjectCl ass, objectGroup, false));
1044 descriptor->setWritable(false);
1045 descriptor->setWasThrown(false);
1046 descriptor->setIsOwn(true);
1047 (*properties)->addItem(descriptor);
1048 }
1049 }
1050
1051 void DartInjectedScript::getInternalProperties(ErrorString* errorString, const S tring& objectId, RefPtr<Array<InternalPropertyDescriptor> >* properties)
1052 {
1053 if (!m_scriptState) {
1054 *errorString = "Invalid DartInjectedScript";
1055 return;
1056 }
1057 // FIXME: add internal properties such as [[PrimitiveValue], [[BoundThis]], etc.
1058 *properties = Array<InternalPropertyDescriptor>::create();
1059 }
1060
1061 void DartInjectedScript::getProperty(ErrorString* errorString, const String& obj ectId, const RefPtr<JSONArray>& propertyPath, RefPtr<TypeBuilder::Runtime::Remot eObject>* result, TypeBuilder::OptOutput<bool>* wasThrown)
1062 {
1063 if (!m_scriptState) {
1064 *errorString = "Invalid DartInjectedScript";
1065 return;
1066 }
1067 DartIsolateScope scope(m_scriptState->isolate());
1068 DartApiScope apiScope;
1069
1070 DartDebuggerObject* object = lookupObject(objectId);
1071 if (!object) {
1072 *errorString = "Unknown objectId";
1073 return;
1074 }
1075 Dart_Handle handle = object->handle();
1076 const String& objectGroup = object->group();
1077
1078
1079 for (unsigned i = 0; i < propertyPath->length(); i++) {
1080 RefPtr<JSONValue> value = propertyPath->get(i);
1081 String propertyName;
1082 if (!value->asString(&propertyName)) {
1083 *errorString = "Invalid property name";
1084 return;
1085 }
1086
1087 handle = getObjectPropertySafe(handle, propertyName);
1088 ASSERT(!Dart_IsError(handle));
1089 }
1090 *result = wrapDartHandle(handle, inferKind(handle), objectGroup, false);
1091 }
1092
1093 Node* DartInjectedScript::nodeForObjectId(const String& objectId)
1094 {
1095 DartIsolateScope scope(m_scriptState->isolate());
1096 DartApiScope apiScope;
1097
1098 DartDebuggerObject* object = lookupObject(objectId);
1099 if (!object || object->kind() != DartDebuggerObject::Object)
1100 return 0;
1101
1102 Dart_Handle handle = object->handle();
1103 if (DartDOMWrapper::subtypeOf(handle, DartNode::dartClassId)) {
1104 Dart_Handle exception = 0;
1105 Node* node = DartNode::toNative(handle, exception);
1106 ASSERT(!exception);
1107 return node;
1108 }
1109 return 0;
1110 }
1111
1112 String DartInjectedScript::cacheObject(Dart_Handle handle, const String& objectG roup, DartDebuggerObject::Kind kind)
1113 {
1114 Dart_PersistentHandle persistentHandle = Dart_NewPersistentHandle(handle);
1115 String objectId = String::format("{\"injectedScriptId\":%d,\"id\":%lu}", m_i njectedScriptId, m_nextObjectId);
1116 m_nextObjectId++;
1117
1118 if (!objectGroup.isNull()) {
1119 ObjectGroupMap::AddResult addResult = m_objectGroups.add(objectGroup, Ve ctor<String>());
1120 Vector<String>& groupMembers = addResult.storedValue->value;
1121 groupMembers.append(objectId);
1122 }
1123
1124 m_objects.set(objectId, new DartDebuggerObject(persistentHandle, objectGroup , kind));
1125 return objectId;
1126 }
1127
1128 void DartInjectedScript::releaseObject(const String& objectId)
1129 {
1130 DartIsolateScope scope(m_scriptState->isolate());
1131 DartApiScope apiScope;
1132 ASSERT(validateObjectId(objectId));
1133 DebuggerObjectMap::iterator it = m_objects.find(objectId);
1134 if (it != m_objects.end()) {
1135 delete it->value;
1136 m_objects.remove(objectId);
1137 }
1138 }
1139
1140 String DartInjectedScript::getCallFrameId(int ordinal, int asyncOrdinal)
1141 {
1142 // FIXME: what if the stack trace contains frames from multiple
1143 // injectedScripts?
1144 return String::format("{\"ordinal\":%d,\"injectedScriptId\":%d,\"asyncOrdina l\":%d}", ordinal, m_injectedScriptId, asyncOrdinal);
1145 }
1146
1147 Dart_ActivationFrame DartInjectedScript::callFrameForId(const StackTrace& callFr ames, const Vector<StackTrace>& asyncCallStacks, const String& callFrameId)
1148 {
1149 ASSERT(!callFrames.isJavaScript());
1150 if (callFrames.isJavaScript())
1151 return 0;
1152 Dart_StackTrace trace = callFrames.asDart();
1153 Dart_ActivationFrame frame = 0;
1154 int ordinal = 0;
1155 int asyncOrdinal = 0;
1156 RefPtr<JSONValue> json = parseJSON(callFrameId);
1157 if (json && json->type() == JSONValue::TypeObject) {
1158 bool ALLOW_UNUSED success = json->asObject()->getNumber("ordinal", &ordi nal);
1159 ASSERT(success);
1160 success = json->asObject()->getNumber("asyncOrdinal", &asyncOrdinal);
1161 ASSERT(success);
1162 } else {
1163 ASSERT(json && json->type() == JSONValue::TypeObject);
1164 return 0;
1165 }
1166 Dart_Handle ALLOW_UNUSED result;
1167 if (asyncOrdinal > 0) { // 1-based index
1168 ASSERT(asyncOrdinal <= (int)asyncCallStacks.size());
1169 if (asyncOrdinal <= (int)asyncCallStacks.size()) {
1170 ASSERT(!asyncCallStacks[asyncOrdinal-1].isJavaScript());
1171 result = Dart_GetActivationFrame(asyncCallStacks[asyncOrdinal-1].asD art(), ordinal, &frame);
1172 } else {
1173 return 0;
1174 }
1175 } else {
1176 Dart_GetActivationFrame(trace, ordinal, &frame);
1177 }
1178 ASSERT(result);
1179 return frame;
1180 }
1181
1182 PassRefPtr<Array<CallFrame> > DartInjectedScript::wrapCallFrames(const StackTrac e& callFrames, int asyncOrdinal)
1183 {
1184 ASSERT(!callFrames.isJavaScript());
1185 if (callFrames.isJavaScript())
1186 return nullptr;
1187 Dart_StackTrace trace = callFrames.asDart();
1188 intptr_t length = 0;
1189 Dart_Handle ALLOW_UNUSED result;
1190 RefPtr<Array<CallFrame> > ret = Array<CallFrame>::create();
1191 result = Dart_StackTraceLength(trace, &length);
1192 ASSERT(!Dart_IsError(result));
1193 DartScriptDebugServer& debugServer = DartScriptDebugServer::shared();
1194 Dart_Handle libraries = Dart_GetLibraryIds();
1195 for (intptr_t i = 0; i < length; i++) {
1196 Dart_ActivationFrame frame = 0;
1197 result = Dart_GetActivationFrame(trace, i, &frame);
1198 ASSERT(!Dart_IsError(result));
1199 Dart_Handle functionName = 0;
1200 Dart_Handle function = 0;
1201 Dart_CodeLocation location;
1202 Dart_ActivationFrameGetLocation(frame, &functionName, &function, &locati on);
1203 const String& url = DartUtilities::toString(location.script_url);
1204 int line = 0;
1205 int column = 0;
1206 debugServer.resolveCodeLocation(location, &line, &column);
1207 RefPtr<Location> locationJson = Location::create()
1208 .setScriptId(debugServer.getScriptId(url, Dart_CurrentIsolate()))
1209 .setLineNumber(line - 1);
1210 locationJson->setColumnNumber(column);
1211 Dart_Handle localVariables = Dart_GetLocalVariables(frame);
1212 Dart_Handle thisHandle = findReceiver(localVariables);
1213 Dart_Handle enclosingType = lookupEnclosingType(function);
1214 RefPtr<TypeBuilder::Array<Scope> > scopeChain = TypeBuilder::Array<Scope >::create();
1215 RefPtr<TypeBuilder::Runtime::RemoteObject> thisObject =
1216 wrapDartHandle(thisHandle ? thisHandle : Dart_Null(), DartDebuggerOb ject::Object, "backtrace", false);
1217
1218 intptr_t localVariablesLength = 0;
1219 result = Dart_ListLength(localVariables, &localVariablesLength);
1220 ASSERT(!Dart_IsError(result));
1221 if (localVariablesLength > 0) {
1222 scopeChain->addItem(Scope::create()
1223 .setType(Scope::Type::Local)
1224 .setObject(wrapDartHandle(localVariables, DartDebuggerObject::Lo calVariables, "backtrace", false))
1225 .release());
1226 }
1227
1228 if (thisHandle) {
1229 scopeChain->addItem(Scope::create()
1230 .setType(Scope::Type::Instance)
1231 .setObject(thisObject)
1232 .release());
1233 }
1234
1235 if (Dart_IsType(enclosingType)) {
1236 scopeChain->addItem(Scope::create()
1237 .setType(Scope::Type::Class)
1238 .setObject(wrapDartHandle(enclosingType, DartDebuggerObject::Sta ticClass, "backtrace", false))
1239 .release());
1240 }
1241
1242 Dart_Handle library = Dart_GetLibraryFromId(location.library_id);
1243 ASSERT(Dart_IsLibrary(library));
1244 if (Dart_IsLibrary(library)) {
1245 scopeChain->addItem(Scope::create()
1246 .setType(Scope::Type::Global)
1247 .setObject(wrapDartHandle(library, DartDebuggerObject::CurrentLi brary, "backtrace", false))
1248 .release());
1249 }
1250
1251 scopeChain->addItem(Scope::create()
1252 .setType(Scope::Type::Library)
1253 .setObject(wrapDartHandle(libraries, DartDebuggerObject::Isolate, "b acktrace", false))
1254 .release());
1255
1256 ret->addItem(CallFrame::create()
1257 .setCallFrameId(getCallFrameId(i, asyncOrdinal))
1258 .setFunctionName(DartUtilities::toString(functionName))
1259 .setLocation(locationJson)
1260 .setScopeChain(scopeChain)
1261 .setThis(thisObject)
1262 .release());
1263 }
1264 return ret;
1265 }
1266
1267 DartDebuggerObject::Kind DartInjectedScript::inferKind(Dart_Handle handle)
1268 {
1269 DartDOMData* domData = DartDOMData::current();
1270 ASSERT(domData);
1271 if (Dart_IsType(handle))
1272 return DartDebuggerObject::Class;
1273 if (Dart_IsError(handle))
1274 return DartDebuggerObject::Error;
1275 if (Dart_IsNull(handle))
1276 return DartDebuggerObject::Object;
1277 if (DartUtilities::isFunction(domData, handle))
1278 return DartDebuggerObject::Function;
1279 if (Dart_IsInstance(handle))
1280 return DartDebuggerObject::Object;
1281 ASSERT(Dart_IsLibrary(handle));
1282 return DartDebuggerObject::Library;
1283 }
1284
1285 PassRefPtr<TypeBuilder::Runtime::RemoteObject> DartInjectedScript::wrapDartObjec t(Dart_Handle dartHandle, const String& groupName, bool generatePreview)
1286 {
1287 return wrapDartHandle(dartHandle, inferKind(dartHandle), groupName, generate Preview);
1288 }
1289
1290 PassRefPtr<TypeBuilder::Runtime::RemoteObject> DartInjectedScript::wrapDartHandl e(Dart_Handle dartHandle, DartDebuggerObject::Kind kind, const String& groupName , bool generatePreview)
1291 {
1292 RefPtr<TypeBuilder::Runtime::RemoteObject> remoteObject;
1293 packageResult(dartHandle, kind, groupName, 0, false, generatePreview, &remot eObject, 0);
1294 return remoteObject;
1295 }
1296
1297 PassRefPtr<TypeBuilder::Runtime::RemoteObject> DartInjectedScript::wrapObject(co nst ScriptValue& value, const String& groupName, bool generatePreview)
1298 {
1299 if (!m_scriptState) {
1300 return nullptr;
1301 }
1302 DartIsolateScope scope(m_scriptState->isolate());
1303 DartApiScope apiScope;
1304 // FIXME: should we use the ScriptValue's isolate instead?
1305 V8Scope v8scope(DartDOMData::current());
1306
1307 v8::TryCatch tryCatch;
1308 v8::Handle<v8::Value> v8Value = value.v8Value();
1309 return wrapDartObject(DartHandleProxy::unwrapValue(v8Value), groupName, gene ratePreview);
1310 }
1311
1312 PassRefPtr<TypeBuilder::Runtime::RemoteObject> DartInjectedScript::wrapTable(con st ScriptValue& table, const ScriptValue& columns)
1313 {
1314 if (!m_scriptState)
1315 return nullptr;
1316 DartIsolateScope scope(m_scriptState->isolate());
1317 DartApiScope apiScope;
1318 // FIXME: implement this rarely used method or call out to the JS version.
1319 ASSERT_NOT_REACHED();
1320 return nullptr;
1321 }
1322
1323 ActivationFrame DartInjectedScript::findCallFrameById(ErrorString* errorString, const StackTrace& topCallFrame, const String& callFrameId)
1324 {
1325 if (!m_scriptState) {
1326 *errorString = "Internal error";
1327 return ActivationFrame();
1328 }
1329 DartIsolateScope scope(m_scriptState->isolate());
1330 DartApiScope apiScope;
1331 ASSERT_NOT_REACHED();
1332 return ActivationFrame();
1333 }
1334
1335 void DartInjectedScript::releaseObjectGroup(const String& objectGroup)
1336 {
1337 if (!m_scriptState)
1338 return;
1339 DartIsolateScope scope(m_scriptState->isolate());
1340 DartApiScope apiScope;
1341 ObjectGroupMap::iterator it = m_objectGroups.find(objectGroup);
1342 if (it != m_objectGroups.end()) {
1343 Vector<String>& ids = it->value;
1344 for (Vector<String>::iterator it = ids.begin(); it != ids.end(); ++it) {
1345 const String& id = *it;
1346 DebuggerObjectMap::iterator objectIt = m_objects.find(id);
1347 if (objectIt != m_objects.end()) {
1348 delete objectIt->value;
1349 m_objects.remove(id);
1350 }
1351 }
1352 m_objectGroups.remove(objectGroup);
1353 }
1354 }
1355
1356 ScriptState* DartInjectedScript::scriptState() const
1357 {
1358 return m_scriptState;
1359 }
1360
1361 DartDebuggerObject* DartInjectedScript::lookupObject(const String& objectId)
1362 {
1363 ASSERT(validateObjectId(objectId));
1364 DebuggerObjectMap::iterator it = m_objects.find(objectId);
1365 return it != m_objects.end() ? it->value : 0;
1366 }
1367
1368 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/dart/DartInjectedScript.h ('k') | Source/bindings/dart/DartScriptDebugServer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698