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

Unified Diff: runtime/lib/mirrors.cc

Issue 10687004: Implement method and variable reflection in dart:mirrors. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 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 side-by-side diff with in-line comments
Download patch
Index: runtime/lib/mirrors.cc
===================================================================
--- runtime/lib/mirrors.cc (revision 9071)
+++ runtime/lib/mirrors.cc (working copy)
@@ -198,30 +198,36 @@
}
-static Dart_Handle CreateLazyLibraryMirror(Dart_Handle lib) {
- if (Dart_IsNull(lib)) {
- return lib;
+static Dart_Handle CreateLazyMirror(Dart_Handle target) {
+ if (Dart_IsNull(target)) {
+ return target;
}
- Dart_Handle cls_name = Dart_NewString("_LazyLibraryMirror");
- Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
- const int kNumArgs = 1;
- Dart_Handle args[kNumArgs];
- args[0] = Dart_LibraryName(lib);
- return Dart_New(cls, Dart_Null(), kNumArgs, args);
-}
+ if (Dart_IsLibrary(target)) {
+ Dart_Handle cls_name = Dart_NewString("_LazyLibraryMirror");
+ Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
+ const int kNumArgs = 1;
+ Dart_Handle args[kNumArgs];
+ args[0] = Dart_LibraryName(target);
+ return Dart_New(cls, Dart_Null(), kNumArgs, args);
+ } else {
+ ASSERT(Dart_IsClass(target) || Dart_IsInterface(target));
+ Dart_Handle cls_name = Dart_NewString("_LazyInterfaceMirror");
+ Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
+ Dart_Handle lib = Dart_ClassGetLibrary(target);
+ Dart_Handle lib_name;
+ if (Dart_IsNull(lib)) {
+ lib_name = Dart_Null();
+ } else {
+ lib_name = Dart_LibraryName(lib);
+ }
-static Dart_Handle CreateLazyInterfaceMirror(Dart_Handle intf) {
- if (Dart_IsNull(intf)) {
- return intf;
+ const int kNumArgs = 2;
+ Dart_Handle args[kNumArgs];
+ args[0] = lib_name;
+ args[1] = Dart_ClassName(target);
+ return Dart_New(cls, Dart_Null(), kNumArgs, args);
}
- Dart_Handle cls_name = Dart_NewString("_LazyInterfaceMirror");
- Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
- const int kNumArgs = 2;
- Dart_Handle args[kNumArgs];
- args[0] = Dart_LibraryName(Dart_ClassGetLibrary(intf));
- args[1] = Dart_ClassName(intf);
- return Dart_New(cls, Dart_Null(), kNumArgs, args);
}
@@ -242,7 +248,7 @@
if (Dart_IsError(interface)) {
return interface;
}
- Dart_Handle mirror = CreateLazyInterfaceMirror(interface);
+ Dart_Handle mirror = CreateLazyMirror(interface);
if (Dart_IsError(mirror)) {
return mirror;
}
@@ -255,9 +261,13 @@
}
+static Dart_Handle CreateMemberMap(Dart_Handle owner);
+
+
static Dart_Handle CreateInterfaceMirror(Dart_Handle intf,
Dart_Handle intf_name,
- Dart_Handle lib) {
+ Dart_Handle lib,
+ Dart_Handle lib_mirror) {
ASSERT(Dart_IsClass(intf) || Dart_IsInterface(intf));
Dart_Handle cls_name = Dart_NewString("_LocalInterfaceMirrorImpl");
Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
@@ -271,46 +281,207 @@
super_class = Dart_GetClass(CoreLib(), Dart_NewString("Object"));
}
Dart_Handle default_class = Dart_ClassGetDefault(intf);
+ Dart_Handle member_map = CreateMemberMap(intf);
+ if (Dart_IsError(member_map)) {
+ return member_map;
+ }
- const int kNumArgs = 7;
+ const int kNumArgs = 8;
cshapiro 2012/06/28 23:57:47 Can this be eliminated, maybe by something like th
turnidge 2012/07/09 23:45:17 Done.
Dart_Handle args[kNumArgs];
args[0] = CreateVMReference(intf);
args[1] = intf_name;
args[2] = Dart_NewBoolean(Dart_IsClass(intf));
- args[3] = CreateLazyLibraryMirror(lib);
- args[4] = CreateLazyInterfaceMirror(super_class);
+ args[3] = lib_mirror;
+ args[4] = CreateLazyMirror(super_class);
args[5] = CreateImplementsList(intf);
- args[6] = CreateLazyInterfaceMirror(default_class);
+ args[6] = CreateLazyMirror(default_class);
+ args[7] = member_map;
Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
return mirror;
}
-static Dart_Handle CreateLibraryMemberMap(Dart_Handle lib) {
+static Dart_Handle CreateMethodMirror(Dart_Handle func,
+ Dart_Handle func_name,
+ Dart_Handle lib_mirror) {
+ ASSERT(Dart_IsFunction(func));
+ Dart_Handle cls_name = Dart_NewString("_LocalMethodMirrorImpl");
+ Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
+ if (Dart_IsError(cls)) {
+ return cls;
+ }
+
+ bool is_static = false;
+ bool is_abstract = false;
+ bool is_getter = false;
+ bool is_setter = false;
+ bool is_constructor = false;
+
+ Dart_Handle result = Dart_FunctionIsStatic(func, &is_static);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ result = Dart_FunctionIsAbstract(func, &is_abstract);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ result = Dart_FunctionIsGetter(func, &is_getter);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ result = Dart_FunctionIsSetter(func, &is_setter);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ result = Dart_FunctionIsConstructor(func, &is_constructor);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+
+ // TODO(turnidge): Implement constructor kinds (arguments 7 - 10).
+ const int kNumArgs = 11;
cshapiro 2012/06/28 23:57:47 Same here, see above regarding the kNumArgs stuff.
turnidge 2012/07/09 23:45:17 Done.
+ Dart_Handle args[kNumArgs];
+ args[0] = func_name;
+ args[1] = lib_mirror;
+ args[2] = Dart_NewBoolean(is_static);
+ args[3] = Dart_NewBoolean(is_abstract);
+ args[4] = Dart_NewBoolean(is_getter);
+ args[5] = Dart_NewBoolean(is_setter);
+ args[6] = Dart_NewBoolean(is_constructor);
+ args[7] = Dart_False();
+ args[8] = Dart_False();
+ args[9] = Dart_False();
+ args[10] = Dart_False();
+ Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
+ return mirror;
+}
+
+
+static Dart_Handle CreateVariableMirror(Dart_Handle var,
+ Dart_Handle var_name,
+ Dart_Handle lib_mirror) {
+ ASSERT(Dart_IsVariable(var));
+ Dart_Handle cls_name = Dart_NewString("_LocalVariableMirrorImpl");
+ Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
+ if (Dart_IsError(cls)) {
+ return cls;
+ }
+
+ bool is_static = false;
+ bool is_final = false;
+
+ Dart_Handle result = Dart_VariableIsStatic(var, &is_static);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ result = Dart_VariableIsFinal(var, &is_final);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+
+ const int kNumArgs = 4;
cshapiro 2012/06/28 23:57:47 Same here too.
turnidge 2012/07/09 23:45:17 Done.
+ Dart_Handle args[kNumArgs];
+ args[0] = var_name;
+ args[1] = lib_mirror;
+ args[2] = Dart_NewBoolean(is_static);
+ args[3] = Dart_NewBoolean(is_final);
+ Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
+ return mirror;
+}
+
+
+static Dart_Handle CreateMemberMap(Dart_Handle owner) {
// TODO(turnidge): This should be an immutable map.
+ Dart_Handle owner_mirror = CreateLazyMirror(owner);
+ if (Dart_IsError(owner_mirror)) {
+ return owner_mirror;
+ }
Dart_Handle map = MapNew();
- Dart_Handle intf_names = Dart_LibraryGetClassNames(lib);
- if (Dart_IsError(intf_names)) {
- return intf_names;
+ Dart_Handle names;
+ Dart_Handle result;
+ intptr_t len;
cshapiro 2012/06/28 23:57:47 How do we know that len is always initialized? Ca
turnidge 2012/07/09 23:45:17 Taken care of by splitting out the 3 helper functi
+
+ if (Dart_IsLibrary(owner)) {
+ // Add classes/interfaces.
+ names = Dart_LibraryGetClassNames(owner);
cshapiro 2012/06/28 23:57:47 Since names here is not the same as names below, i
turnidge 2012/07/09 23:45:17 Done.
+ if (Dart_IsError(names)) {
+ return names;
+ }
+ result = Dart_ListLength(names, &len);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ for (int i = 0; i < len; i++) {
+ Dart_Handle intf_name = Dart_ListGetAt(names, i);
+ Dart_Handle intf = Dart_GetClass(owner, intf_name);
+ if (Dart_IsError(intf)) {
+ return intf;
+ }
+ Dart_Handle intf_mirror =
+ CreateInterfaceMirror(intf, intf_name, owner, owner_mirror);
+ if (Dart_IsError(intf_mirror)) {
+ return intf_mirror;
+ }
+ result = MapAdd(map, intf_name, intf_mirror);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ }
}
- intptr_t len;
- Dart_Handle result = Dart_ListLength(intf_names, &len);
+
+ // Add functions.
+ names = Dart_GetFunctionNames(owner);
+ if (Dart_IsError(names)) {
+ return names;
+ }
+ result = Dart_ListLength(names, &len);
if (Dart_IsError(result)) {
return result;
}
for (int i = 0; i < len; i++) {
- Dart_Handle intf_name = Dart_ListGetAt(intf_names, i);
- Dart_Handle intf = Dart_GetClass(lib, intf_name);
- if (Dart_IsError(intf)) {
- return intf;
+ Dart_Handle func_name = Dart_ListGetAt(names, i);
+ Dart_Handle func = Dart_LookupFunction(owner, func_name);
+ if (Dart_IsError(func)) {
+ return func;
}
- Dart_Handle intf_mirror = CreateInterfaceMirror(intf, intf_name, lib);
- if (Dart_IsError(intf_mirror)) {
- return intf_mirror;
+ ASSERT(!Dart_IsNull(func));
+ Dart_Handle func_mirror = CreateMethodMirror(func, func_name, owner_mirror);
+ if (Dart_IsError(func_mirror)) {
+ return func_mirror;
}
- result = MapAdd(map, intf_name, intf_mirror);
+ result = MapAdd(map, func_name, func_mirror);
+ if (Dart_IsError(result)) {
+ return result;
+ }
}
+
+ // Add variables.
+ names = Dart_GetVariableNames(owner);
cshapiro 2012/06/28 23:57:47 It looks like there are 3 different functions insi
turnidge 2012/07/09 23:45:17 Done.
+ if (Dart_IsError(names)) {
+ return names;
+ }
+ result = Dart_ListLength(names, &len);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ for (int i = 0; i < len; i++) {
+ Dart_Handle var_name = Dart_ListGetAt(names, i);
+ Dart_Handle var = Dart_LookupVariable(owner, var_name);
+ if (Dart_IsError(var)) {
+ return var;
+ }
+ ASSERT(!Dart_IsNull(var));
+ Dart_Handle var_mirror = CreateVariableMirror(var, var_name, owner_mirror);
+ if (Dart_IsError(var_mirror)) {
+ return var_mirror;
+ }
+ result = MapAdd(map, var_name, var_mirror);
+ if (Dart_IsError(result)) {
+ return result;
+ }
+ }
+
return map;
}
@@ -321,7 +492,7 @@
if (Dart_IsError(cls)) {
return cls;
}
- Dart_Handle member_map = CreateLibraryMemberMap(lib);
+ Dart_Handle member_map = CreateMemberMap(lib);
if (Dart_IsError(member_map)) {
return member_map;
}
@@ -417,7 +588,7 @@
const int kNumArgs = 4;
Dart_Handle args[kNumArgs];
args[0] = CreateVMReference(Dart_Null());
- args[1] = CreateLazyInterfaceMirror(object_class);
+ args[1] = CreateLazyMirror(object_class);
args[2] = Dart_True();
args[3] = Dart_Null();
Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
@@ -443,7 +614,7 @@
const int kNumArgs = 4;
Dart_Handle args[kNumArgs];
args[0] = CreateVMReference(instance);
- args[1] = CreateLazyInterfaceMirror(instance_cls);
+ args[1] = CreateLazyMirror(instance_cls);
args[2] = Dart_NewBoolean(is_simple);
args[3] = (is_simple ? instance : Dart_Null());
Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);

Powered by Google App Engine
This is Rietveld 408576698