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

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

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

Powered by Google App Engine
This is Rietveld 408576698