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

Unified Diff: lib/src/functionref.dart

Issue 928663003: Add IsolateRunner as a helper around Isolate. (Closed) Base URL: https://github.com/dart-lang/isolate.git@master
Patch Set: Add .status. Created 5 years, 10 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 | « lib/src/errors.dart ('k') | lib/src/lists.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/functionref.dart
diff --git a/lib/src/functionref.dart b/lib/src/functionref.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d4c0e8efec60bd48a7e56d8d6e6c54c146561054
--- /dev/null
+++ b/lib/src/functionref.dart
@@ -0,0 +1,80 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * Convert top-level or static functions to objects that can be sent to
+ * other isolates.
+ *
+ * This package is only needed until such functions can be sent directly
+ * through send-ports.
+ */
+library pkg.isolate.functionref;
+
+import "dart:mirrors";
+
+abstract class FunctionRef {
+ static FunctionRef from(Function function) {
+ var cm = reflect(function);
+ if (cm is ClosureMirror) {
+ MethodMirror fm = cm.function;
+ if (fm.isRegularMethod && fm.isStatic) {
+ Symbol functionName = fm.simpleName;
+ Symbol className;
+ DeclarationMirror owner = fm.owner;
+ if (owner is ClassMirror) {
+ className = owner.simpleName;
+ owner = owner.owner;
+ }
+ if (owner is LibraryMirror) {
+ LibraryMirror ownerLibrary = owner;
+ Uri libraryUri = ownerLibrary.uri;
+ return new _FunctionRef(libraryUri, className, functionName);
+ }
+ }
+ throw new ArgumentError.value(function, "function",
+ "Not a static or top-level function");
+ }
+ // It's a Function but not a closure, so it's a callable object.
+ return new _CallableObjectRef(function);
+ }
+
+ Function get function;
+}
+
+class _FunctionRef implements FunctionRef {
+ final Uri libraryUri;
+ final Symbol className;
+ final Symbol functionName;
+
+ _FunctionRef(this.libraryUri, this.className, this.functionName);
+
+ Function get function {
+ LibraryMirror lm = currentMirrorSystem().libraries[libraryUri];
+ if (lm != null) {
+ ObjectMirror owner = lm;
+ if (className != null) {
+ ClassMirror cm = lm.declarations[className];
+ owner = cm;
+ }
+ if (owner != null) {
+ ClosureMirror function = owner.getField(this.functionName);
+ if (function != null) return function.reflectee;
+ }
+ }
+ String functionName = MirrorSystem.getName(this.functionName);
+ String classQualifier = "";
+ if (this.className != null) {
+ classQualifier = " in class ${MirrorSystem.getName(this.className)}";
+ }
+ throw new UnsupportedError(
+ "Function $functionName${classQualifier} not found in library $libraryUri"
+ );
+ }
+}
+
+class _CallableObjectRef implements FunctionRef {
+ final Function object;
+ _CallableObjectRef(this.object);
+ Function get function => object;
+}
« no previous file with comments | « lib/src/errors.dart ('k') | lib/src/lists.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698