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

Unified Diff: tools/dom/src/native_DOMImplementation.dart

Issue 258503008: Support Dart debugger API directly (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/dom/src/native_DOMImplementation.dart
diff --git a/tools/dom/src/native_DOMImplementation.dart b/tools/dom/src/native_DOMImplementation.dart
index d8c4edc808c2dca027d1e8b4ee61a7a0c39124c9..6583e481bde5a66c3121c4ccade9e0b7ee645d5c 100644
--- a/tools/dom/src/native_DOMImplementation.dart
+++ b/tools/dom/src/native_DOMImplementation.dart
@@ -4,6 +4,32 @@
part of html;
+class _Property {
+ _Property(this.name) :
+ _hasValue = false,
+ writable = false,
+ isMethod = false,
+ isOwn = true,
+ wasThrown = false;
+
+ bool get hasValue => _hasValue;
+ get value => _value;
+ set value(v) {
+ _value = v;
+ _hasValue = true;
+ }
+
+ final String name;
+ Function setter;
+ Function getter;
+ var _value;
+ bool _hasValue;
+ bool writable;
+ bool isMethod;
+ bool isOwn;
+ bool wasThrown;
+}
+
class _ConsoleVariables {
Map<String, Object> _data = new Map<String, Object>();
@@ -31,6 +57,52 @@ class _ConsoleVariables {
List variables() => _data.keys.toList(growable: false);
}
+/**
+ * Base class for invocation trampolines used to closurize methods, getters
+ * and setters.
+ */
+abstract class _Trampoline implements Function {
+ final ObjectMirror _receiver;
+ final MethodMirror _methodMirror;
+ final Symbol _selector;
+
+ _Trampoline(this._receiver, this._methodMirror, this._selector);
+}
+
+class _MethodTrampoline extends _Trampoline {
+ _MethodTrampoline(ObjectMirror receiver, MethodMirror methodMirror, Symbol selector) :
Leaf 2014/05/07 01:47:27 line width?
Jacob 2014/05/07 20:57:08 Done.
+ super(receiver, methodMirror, selector);
+
+ noSuchMethod(Invocation msg) {
+ if (msg.memberName != #call) return super.noSuchMethod(msg);
+ return _receiver.invoke(_selector,
+ msg.positionalArguments,
+ msg.namedArguments).reflectee;
+ }
+}
+
+/**
+ * Invocation trampoline class used to closurize getters.
+ */
+class _GetterTrampoline extends _Trampoline {
+ _GetterTrampoline(ObjectMirror receiver, MethodMirror methodMirror, Symbol selector) :
Leaf 2014/05/07 01:47:27 ditto
Jacob 2014/05/07 20:57:08 Done.
+ super(receiver, methodMirror, selector);
+
+ call() => _receiver.getField(_selector).reflectee;
+}
+
+/**
+ * Invocation trampoline class used to closurize setters.
+ */
+class _SetterTrampoline extends _Trampoline {
+ _SetterTrampoline(ObjectMirror receiver, MethodMirror methodMirror, Symbol selector) :
Leaf 2014/05/07 01:47:27 also
Jacob 2014/05/07 20:57:08 Done.
+ super(receiver, methodMirror, selector);
+
+ call(value) {
+ _receiver.setField(_selector, value);
+ }
+}
+
class _Utils {
static double dateTimeToDouble(DateTime dateTime) =>
dateTime.millisecondsSinceEpoch.toDouble();
@@ -144,17 +216,6 @@ class _Utils {
static _ConsoleVariables _consoleTempVariables = new _ConsoleVariables();
/**
- * Header passed in from the Dartium Developer Tools when an expression is
- * evaluated in the console as opposed to the watch window or another context
- * that does not expect REPL support.
- */
- static const _CONSOLE_API_SUPPORT_HEADER =
- 'with ((console && console._commandLineAPI) || { __proto__: null }) {\n';
- static bool expectsConsoleApi(String expression) {
- return expression.indexOf(_CONSOLE_API_SUPPORT_HEADER) == 0;;
- }
-
- /**
* Takes an [expression] and a list of [local] variable and returns an
* expression for a closure with a body matching the original expression
* where locals are passed in as arguments. Returns a list containing the
@@ -166,8 +227,7 @@ class _Utils {
* For example:
* <code>
* _consoleTempVariables = {'a' : someValue, 'b': someOtherValue}
- * wrapExpressionAsClosure("${_CONSOLE_API_SUPPORT_HEADER}foo + bar + a",
- * ["bar", 40, "foo", 2])
+ * wrapExpressionAsClosure("foo + bar + a", ["bar", 40, "foo", 2], true)
* </code>
* will return:
* <code>
@@ -177,9 +237,8 @@ class _Utils {
* [_consoleTempVariables, 40, 2, someValue, someOtherValue]]
* </code>
*/
- static List wrapExpressionAsClosure(String expression, List locals) {
- // FIXME: dartbug.com/10434 find a less fragile way to determine whether
- // we need to strip off console API support added by InjectedScript.
+ static List wrapExpressionAsClosure(String expression, List locals,
+ bool includeCommandLineAPI) {
var args = {};
var sb = new StringBuffer("(");
addArg(arg, value) {
@@ -198,10 +257,7 @@ class _Utils {
args[arg] = value;
}
- if (expectsConsoleApi(expression)) {
- expression = expression.substring(expression.indexOf('\n') + 1);
- expression = expression.substring(0, expression.lastIndexOf('\n'));
-
+ if (includeCommandLineAPI) {
addArg("\$consoleVariables", _consoleTempVariables);
// FIXME: use a real Dart tokenizer. The following regular expressions
@@ -258,48 +314,395 @@ class _Utils {
return [sb.toString(), args.values.toList(growable: false)];
}
+ static String _getShortSymbolName(Symbol symbol,
+ DeclarationMirror declaration) {
+ var name = MirrorSystem.getName(symbol);
+ if (declaration is MethodMirror) {
+ if (declaration.isSetter && name[name.length-1] == "=") {
+ return name.substring(0, name.length-1);
+ }
+ if (declaration.isConstructor) {
+ return name.substring(name.indexOf('.') + 1);
+ }
+ }
+ return name;
+ }
+
+ /**
+ * Adds all candidate String completitions from [declarations] to [output]
+ * filtering based on [staticContext] and [includePrivate].
+ */
+ static void _getCompletionsHelper(ClassMirror classMirror,
+ bool staticContext, bool includePrivate, Set<String> output) {
+ classMirror.declarations.forEach((symbol, declaration) {
+ if (!includePrivate && declaration.isPrivate) return;
+ if (declaration is VariableMirror) {
+ if (staticContext != declaration.isStatic) return;
+ } else if (declaration is MethodMirror) {
+ if (staticContext != declaration.isStatic) return;
rmacnak 2014/05/06 21:14:43 Should do this check after considering constructor
Jacob 2014/05/07 00:05:24 Good catch! I added a test (see utils.dart in the
+ if (declaration.isOperator) return;
rmacnak 2014/05/06 21:14:43 Because the completions assume dot notation?
Jacob 2014/05/07 00:05:24 Correct. Completions are after a dot.
+ if (declaration.isConstructor) {
+ var name = MirrorSystem.getName(declaration.constructorName);
+ if (name.isNotEmpty) output.add(name);
+ return;
+ }
+ } else if (declaration is TypeMirror) {
+ return;
+ }
+ output.add(_getShortSymbolName(symbol, declaration));
+ });
+ }
+
+ static void _getLibraryCompletionsHelper(
+ LibraryMirror library, bool includePrivate, Set<String> output) {
+ library.declarations.forEach((symbol, declaration) {
rmacnak 2014/05/06 21:14:43 Add the prefixed imports (but not their declaratio
Jacob 2014/05/07 00:05:24 Correct. Interesting idea to show all completions.
+ if (!includePrivate && declaration.isPrivate) return;
+ output.add(_getShortSymbolName(symbol, declaration));
+ });
+ }
+
+ static LibraryMirror getLibraryMirror(String url) =>
+ currentMirrorSystem().libraries[Uri.parse(url)];
+
+ /**
+ * Get code completions for [o] only showing privates from [libraryUrl].
+ */
+ static List<String> getObjectCompletions(o, String libraryUrl) {
+ var classMirror;
+ bool staticContext;
+ if (o is Type) {
+ classMirror = reflectClass(o);
+ staticContext = true;
+ } else {
+ classMirror = reflect(o).type;
+ staticContext = false;
+ }
+ var names = new Set<String>();
+ getClassCompletions(classMirror, names, staticContext, libraryUrl);
+ return names.toList()..sort();
+ }
+
+ static void getClassCompletions(ClassMirror classMirror, Set<String> names,
+ bool staticContext, String libraryUrl) {
+ LibraryMirror libraryMirror = getLibraryMirror(libraryUrl);
+ if (!staticContext) {
+ for (var interface in classMirror.superinterfaces) {
rmacnak 2014/05/06 21:14:43 Interfaces declared on superclasses?
Jacob 2014/05/07 00:05:24 Fixed.
+ _getCompletionsHelper(interface, staticContext,
+ libraryMirror == interface.owner, names);
+ }
+ }
+ while (classMirror != null) {
+ _getCompletionsHelper(classMirror, staticContext,
+ libraryMirror == classMirror.owner, names);
+ // Do not walk up the superclass tree when displaying statics.
+ if (staticContext) break;
+ classMirror = classMirror.superclass;
+ }
+ }
+
+ static List<String> getLibraryCompletions(String url) {
+ var names = new Set<String>();
+ _getLibraryCompletionsHelper(getLibraryMirror(url), true, names);
+ return names.toList();
+ }
+
/**
- * TODO(jacobr): this is a big hack to get around the fact that we are still
- * passing some JS expression to the evaluate method even when in a Dart
- * context.
+ * Get valid code completitions from within a library and all libraries
+ * imported by that library.
*/
- static bool isJsExpression(String expression) =>
- expression.startsWith("(function getCompletions");
+ static List<String> getLibraryCompletionsIncludingImports(String url) {
+ var names = new Set<String>();
+ var libraryMirror = getLibraryMirror(url);
+ _getLibraryCompletionsHelper(libraryMirror, true, names);
+ for (var dependency in libraryMirror.libraryDependencies) {
+ if (dependency.isImport) {
+ if (dependency.prefix == null) {
+ _getLibraryCompletionsHelper(dependency.targetLibrary, false, names);
+ } else {
+ names.add(MirrorSystem.getName(dependency.prefix));
+ }
+ }
+ }
+ return names.toList();
+ }
/**
- * Returns a list of completions to use if the receiver is o.
+ * For parity with the JavaScript debugger, we treat some getters as if
+ * they are fields so that users can see their values immediately.
+ * This matches JavaScript's behavior for getters on DOM objects.
+ * In the future we should consider adding an annotation to tag getters
+ * in user libraries as side effect free.
+ */
+ static bool _isSideEffectFreeGetter(MethodMirror methodMirror) {
+ var owner = methodMirror.owner;
+ var libraryMirror;
+ if (owner is ClassMirror) {
+ libraryMirror = owner.owner;
Leaf 2014/05/07 01:47:27 Does this do the right thing if the class is produ
rmacnak 2014/05/07 16:59:51 A class's owner will always be a library.
Jacob 2014/05/07 20:57:08 yep.
+ } else if (owner is LibraryMirror) {
+ libraryMirror = owner;
+ }
+ if (libraryMirror != null) {
+ var libraryUrl = MirrorSystem.getName(libraryMirror.qualifiedName);
rmacnak 2014/05/06 21:14:43 You're not comparing the url here. The qualified n
Jacob 2014/05/07 00:05:24 Tweaked to actually use the URI instead.
+ // This matches JavaScript behavior. We should consider displaying
+ // getters for all dart platform libraries rather than just dart.dom.
rmacnak 2014/05/06 21:14:43 I wouldn't do this for all Dart platform libraries
Jacob 2014/05/07 00:05:24 Makes sense. I think the right solution is to pro
+ if (libraryUrl.startsWith("dart.dom."))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Whether we should treat a property as a field for the purposes of the
+ * debugger.
*/
- static List<String> getCompletions(o) {
- MirrorSystem system = currentMirrorSystem();
- var completions = new Set<String>();
- addAll(Map<Symbol, dynamic> map, bool isStatic) {
- map.forEach((symbol, mirror) {
- if (mirror.isStatic == isStatic && !mirror.isPrivate) {
+ static bool treatPropertyAsField(MethodMirror methodMirror) =>
+ (methodMirror.isGetter || methodMirror.isSetter) &&
+ (methodMirror.isSynthetic || _isSideEffectFreeGetter(methodMirror));
+
+ // TODO(jacobr): generate more concise function descriptions instead of
+ // dumping the entire function source.
+ static String describeFunction(function) {
+ if (function is _Trampoline) return function._methodMirror.source;
+ try {
+ return reflect(function).function.source;
+ } catch (e) {
+ return function.toString();
+ }
+ }
+
+ static List getInvocationTrampolineDetails(_Trampoline method) {
+ var loc = method._methodMirror.location;
+ return [loc.line, loc.column, loc.sourceUri.toString(),
+ MirrorSystem.getName(method._selector)];
+ }
+
+ static List getLibraryProperties(String libraryUrl, bool ownProperties,
+ bool accessorPropertiesOnly) {
+ var properties = new Map<String, _Property>();
+ var libraryMirror = getLibraryMirror(libraryUrl);
+ _addInstanceMirrors(libraryMirror, libraryMirror,
+ libraryMirror.declarations,
+ ownProperties, accessorPropertiesOnly, false, false,
+ properties);
+ if (!accessorPropertiesOnly) {
+ // We need to add class properties for all classes in the library.
+ libraryMirror.declarations.forEach((symbol, declarationMirror) {
+ if (declarationMirror is ClassMirror) {
var name = MirrorSystem.getName(symbol);
- if (mirror is MethodMirror && mirror.isSetter)
- name = name.substring(0, name.length - 1);
- completions.add(name);
+ if (declarationMirror.hasReflectedType
+ && !properties.containsKey(name)) {
+ properties[name] = new _Property(name)
+ ..value = declarationMirror.reflectedType;
Leaf 2014/05/07 01:47:27 Is this idiomatic in Dart? I'm presuming that thi
Jacob 2014/05/07 20:57:08 This is the idiomatic way to use Dart cascades. A
Leaf 2014/05/08 04:04:01 Ok, I'll get used to it. On 2014/05/07 20:57:08,
+ }
}
});
}
+ return packageProperties(properties);
+ }
- addForClass(ClassMirror mirror, bool isStatic) {
- if (mirror == null)
+ static List getObjectProperties(o, bool ownProperties,
+ bool accessorPropertiesOnly) {
+ var properties = new Map<String, _Property>();
+ var names = new Set<String>();
+ var objectMirror = reflect(o);
+ var classMirror = objectMirror.type;
+ _addInstanceMirrors(objectMirror, classMirror.owner,
Leaf 2014/05/07 01:47:27 Same question as before about class mirror owners
Jacob 2014/05/07 20:57:08 Same answer as before. the owner is a library.
+ classMirror.instanceMembers,
+ ownProperties, accessorPropertiesOnly, false, true,
+ properties);
+ return packageProperties(properties);
+ }
+
+ static List getObjectClassProperties(o, bool ownProperties,
+ bool accessorPropertiesOnly) {
+ var properties = new Map<String, _Property>();
+ var objectMirror = reflect(o);
+ var classMirror = objectMirror.type;
+ _addInstanceMirrors(objectMirror, classMirror.owner,
Leaf 2014/05/07 01:47:27 ditto
Jacob 2014/05/07 20:57:08 same answer as other case.
+ classMirror.instanceMembers,
+ ownProperties, accessorPropertiesOnly, true, false,
+ properties);
+ _addStatics(classMirror, properties, accessorPropertiesOnly);
+ return packageProperties(properties);
+ }
+
+ static List getClassProperties(Type t, bool ownProperties,
+ bool accessorPropertiesOnly) {
+ var properties = new Map<String, _Property>();
+ var classMirror = reflectClass(t);
+ _addStatics(classMirror, properties, accessorPropertiesOnly);
+ return packageProperties(properties);
+ }
+
+ static void _addStatics(ClassMirror classMirror,
+ Map<String, _Property> properties,
+ bool accessorPropertiesOnly) {
+ classMirror.declarations.forEach((symbol, declaration) {
+ var name = _getShortSymbolName(symbol, declaration);
+ if (declaration is VariableMirror) {
+ if (accessorPropertiesOnly) return;
+ if (!declaration.isStatic) return;
+ properties.putIfAbsent(name, () => new _Property(name))
+ ..value = classMirror.getField(symbol).reflectee
+ ..writable = declaration.isFinal && !declaration.isConst;
Leaf 2014/05/07 01:47:27 I don't understand this line (possibly my fault).
Jacob 2014/05/07 20:57:08 I don't understand what I was thinking either. The
+ } else if (declaration is MethodMirror) {
+ MethodMirror methodMirror = declaration;
+ // FIXMEDART: should we display constructors?
+ if (methodMirror.isConstructor) return;
+ if (!methodMirror.isStatic) return;
+ if (accessorPropertiesOnly) {
+ if (methodMirror.isRegularMethod ||
+ treatPropertyAsField(methodMirror)) {
+ return;
+ }
+ } else if (!methodMirror.isRegularMethod &&
+ !treatPropertyAsField(methodMirror)) {
+ return;
+ }
+ var property = properties.putIfAbsent(name, () => new _Property(name));
Leaf 2014/05/07 01:47:27 I'm not sure I get why this is right. If I unders
rmacnak 2014/05/07 16:59:51 Implicit getters and setters are not part of Class
Jacob 2014/05/07 20:57:08 Correct. That is why I have to add these cases.
+ if (methodMirror.isRegularMethod) {
+ property
+ ..value = new _MethodTrampoline(classMirror, methodMirror, symbol)
+ ..isMethod = true;
+ } else if (methodMirror.isGetter) {
+ if (treatPropertyAsField(methodMirror)) {
+ try {
+ property.value = classMirror.getField(symbol).reflectee;
+ } catch (e) {
+ property
+ ..wasThrown = true
+ ..value = e;
+ }
+ } else if (accessorPropertiesOnly) {
+ property.getter = new _GetterTrampoline(classMirror,
+ methodMirror, symbol);
+ }
+ } else if (methodMirror.isSetter) {
+ if (accessorPropertiesOnly && !treatPropertyAsField(methodMirror)) {
+ property.setter = new _SetterTrampoline(classMirror,
+ methodMirror, classMirror.owner);
+ }
+ property.writable = true;
+ }
+ }
+ });
+ }
+
+ /**
+ * Helper method that handles collecting up properties from classes
+ * or libraries using the filters [ownProperties], [accessorPropertiesOnly],
+ * [hideFields], and [hideMethods] to determine which properties are
+ * collected. [accessorPropertiesOnly] specifies whether all properties
+ * should be returned or just accessors. [hideFields] specifies whether
+ * fields should be hidden. hideMethods specifies whether methods should be
+ * shown or hidden. [ownProperties] is not currently used but is part of the
+ * Blink devtools API for enumerating properties.
+ */
+ static void _addInstanceMirrors(
+ ObjectMirror objectMirror,
+ LibraryMirror libraryMirror,
+ Map<Symbol, Mirror> declarations,
+ bool ownProperties, bool accessorPropertiesOnly,
+ bool hideFields, bool hideMethods,
+ Map<String, _Property> properties) {
+ declarations.forEach((Symbol symbol, Mirror declaration) {
+ if (declaration is TypedefMirror || declaration is ClassMirror) return;
+ var name = _getShortSymbolName(symbol, declaration);
+ bool isField = declaration is VariableMirror ||
+ (declaration is MethodMirror && treatPropertyAsField(declaration));
+ if ((isField && hideFields) || (hideMethods && !isField)) return;
+ if (accessorPropertiesOnly) {
+ if (declaration is ClassMirror || declaration is VariableMirror ||
Leaf 2014/05/07 01:47:27 declaration is known not to be ClassMirror here.
Jacob 2014/05/07 20:57:08 Fixed. I originally tried to use this method for b
+ declaration.isRegularMethod || isField) {
+ return;
+ }
+ } else if (declaration is MethodMirror &&
+ (declaration.isGetter || declaration.isSetter) &&
+ !treatPropertyAsField(declaration)) {
return;
- addAll(mirror.declarations, isStatic);
- if (mirror.superclass != null)
- addForClass(mirror.superclass, isStatic);
- for (var interface in mirror.superinterfaces) {
- addForClass(interface, isStatic);
}
- }
+ var property = properties.putIfAbsent(name, () => new _Property(name));
+ if (declaration is VariableMirror) {
+ property.value = objectMirror.getField(symbol).reflectee;
+ property.writable = !declaration.isFinal && !declaration.isConst;
+ } else if (declaration is ClassMirror) {
Leaf 2014/05/07 01:47:27 Again, known to be false.
Jacob 2014/05/07 20:57:08 Done.
+ property.value = declaration.runtimeType;
+ } else if (declaration.isRegularMethod) {
Leaf 2014/05/07 01:47:27 The section from here to the end is pretty close t
Jacob 2014/05/07 20:57:08 Done.
+ property.value = new _MethodTrampoline(objectMirror,
+ declaration, symbol);
+ property.isMethod = true;
+ } else if (declaration.isGetter) {
+ if (treatPropertyAsField(declaration)) {
+ try {
+ property.value = objectMirror.getField(symbol).reflectee;
+ } catch (e) {
+ property
+ ..wasThrown = true
+ ..value = e;
+ }
+ } else if (accessorPropertiesOnly) {
+ property.getter = new _GetterTrampoline(objectMirror,
+ declaration, symbol);
+ }
+ } else if (declaration.isSetter) {
+ property.writable = true;
+ if (accessorPropertiesOnly && !treatPropertyAsField(declaration)) {
+ property.setter = new _SetterTrampoline(objectMirror,
+ declaration, MirrorSystem.getSymbol(name, libraryMirror));
+ }
+ }
+ });
+ }
- if (o is Type) {
- addForClass(reflectClass(o), true);
- } else {
- addForClass(reflect(o).type, false);
+ /**
+ * Flatten down the properties data structure into a List that is easy to
+ * access from native code.
+ */
+ static List packageProperties(Map<String, _Property> properties) {
+ var ret = [];
+ for (var property in properties.values) {
+ ret.addAll([property.name,
+ property.setter,
+ property.getter,
+ property.value,
+ property.hasValue,
+ property.writable,
+ property.isMethod,
+ property.isOwn,
+ property.wasThrown]);
+ }
+ return ret;
+ }
+
+ /**
+ * Get a property, returning null if the property does not exist.
+ * For private property names, we attempt to resolve the property in the
+ * context of each library that the property name could be associated with.
+ */
+ static getObjectPropertySafe(o, String propertyName) {
+ var objectMirror = reflect(o);
+ var classMirror = objectMirror.type;
+ if (propertyName.startsWith("_")) {
+ var attemptedLibraries = new Set<LibraryMirror>();
+ while (classMirror != null) {
+ LibraryMirror library = classMirror.owner;
+ if (!attemptedLibraries.contains(library)) {
+ try {
+ return objectMirror.getField(
+ MirrorSystem.getSymbol(propertyName, library)).reflectee;
+ } catch (e) { }
+ attemptedLibraries.add(library);
+ }
+ classMirror = classMirror.superclass;
+ }
+ return null;
+ }
+ try {
+ return objectMirror.getField(
+ MirrorSystem.getSymbol(propertyName)).reflectee;
+ } catch (e) {
+ return null;
}
- return completions.toList(growable: false);
}
/**
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698