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

Side by Side Diff: runtime/lib/mirrors.cc

Issue 16757007: Changes to mirrors in support of metadata access at runtime. Assumes VM changes in flight.… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | runtime/lib/mirrors_impl.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "include/dart_debugger_api.h" 6 #include "include/dart_debugger_api.h"
7 #include "vm/dart_api_impl.h" 7 #include "vm/dart_api_impl.h"
8 #include "vm/bootstrap_natives.h" 8 #include "vm/bootstrap_natives.h"
9 #include "vm/dart_entry.h" 9 #include "vm/dart_entry.h"
10 #include "vm/exceptions.h" 10 #include "vm/exceptions.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 if (Dart_IsError(cls)) { 72 if (Dart_IsError(cls)) {
73 return cls; 73 return cls;
74 } 74 }
75 Dart_Handle result = Dart_ObjectIsType(object, cls, is_mirror); 75 Dart_Handle result = Dart_ObjectIsType(object, cls, is_mirror);
76 if (Dart_IsError(result)) { 76 if (Dart_IsError(result)) {
77 return result; 77 return result;
78 } 78 }
79 return Dart_True(); // Indicates success. Result is in is_mirror. 79 return Dart_True(); // Indicates success. Result is in is_mirror.
80 } 80 }
81 81
82 static Dart_Handle IsMethodMirror(Dart_Handle object, bool* is_mirror) {
83 Dart_Handle cls_name = NewString("MethodMirror");
84 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
85 if (Dart_IsError(cls)) {
86 return cls;
87 }
88 Dart_Handle result = Dart_ObjectIsType(object, cls, is_mirror);
89 if (Dart_IsError(result)) {
90 return result;
91 }
92 return Dart_True(); // Indicates success. Result is in is_mirror.
93 }
94
95 static Dart_Handle IsVariableMirror(Dart_Handle object, bool* is_mirror) {
96 Dart_Handle cls_name = NewString("VariableMirror");
97 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
98 if (Dart_IsError(cls)) {
99 return cls;
100 }
101 Dart_Handle result = Dart_ObjectIsType(object, cls, is_mirror);
102 if (Dart_IsError(result)) {
103 return result;
104 }
105 return Dart_True(); // Indicates success. Result is in is_mirror.
106 }
107
82 108
83 static bool IsSimpleValue(Dart_Handle object) { 109 static bool IsSimpleValue(Dart_Handle object) {
84 return (Dart_IsNull(object) || 110 return (Dart_IsNull(object) ||
85 Dart_IsNumber(object) || 111 Dart_IsNumber(object) ||
86 Dart_IsString(object) || 112 Dart_IsString(object) ||
87 Dart_IsBoolean(object)); 113 Dart_IsBoolean(object));
88 } 114 }
89 115
90 116
91 static void FreeVMReference(Dart_WeakPersistentHandle weak_ref, void* data) { 117 static void FreeVMReference(Dart_WeakPersistentHandle weak_ref, void* data) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // handle. 152 // handle.
127 void* perm_handle_data = reinterpret_cast<void*>(perm_handle); 153 void* perm_handle_data = reinterpret_cast<void*>(perm_handle);
128 Dart_WeakPersistentHandle weak_ref = 154 Dart_WeakPersistentHandle weak_ref =
129 Dart_NewWeakPersistentHandle(vm_ref, perm_handle_data, FreeVMReference); 155 Dart_NewWeakPersistentHandle(vm_ref, perm_handle_data, FreeVMReference);
130 ASSERT(weak_ref != NULL); 156 ASSERT(weak_ref != NULL);
131 157
132 // Success. 158 // Success.
133 return vm_ref; 159 return vm_ref;
134 } 160 }
135 161
162 static Dart_Handle StringFromSymbol(Dart_Handle symbol) {
163 Dart_Handle result = Dart_Invoke(MirrorLib(), NewString("_n"), 1, &symbol);
164 return result;
165 }
166
136 167
137 static Dart_Handle UnwrapVMReference(Dart_Handle vm_ref) { 168 static Dart_Handle UnwrapVMReference(Dart_Handle vm_ref) {
138 // Retrieve the persistent handle from the VMReference 169 // Retrieve the persistent handle from the VMReference
139 intptr_t perm_handle_value = 0; 170 intptr_t perm_handle_value = 0;
140 Dart_Handle result = 171 Dart_Handle result =
141 Dart_GetNativeInstanceField(vm_ref, 0, &perm_handle_value); 172 Dart_GetNativeInstanceField(vm_ref, 0, &perm_handle_value);
142 if (Dart_IsError(result)) { 173 if (Dart_IsError(result)) {
143 return result; 174 return result;
144 } 175 }
145 Dart_PersistentHandle perm_handle = 176 Dart_PersistentHandle perm_handle =
146 reinterpret_cast<Dart_PersistentHandle>(perm_handle_value); 177 reinterpret_cast<Dart_PersistentHandle>(perm_handle_value);
147 ASSERT(perm_handle != NULL); 178 ASSERT(perm_handle != NULL);
148 Dart_Handle handle = Dart_HandleFromPersistent(perm_handle); 179 Dart_Handle handle = Dart_HandleFromPersistent(perm_handle);
149 ASSERT(handle != NULL); 180 ASSERT(handle != NULL);
150 ASSERT(!Dart_IsError(handle)); 181 ASSERT(!Dart_IsError(handle));
151 return handle; 182 return handle;
152 } 183 }
153 184
185 static Dart_Handle UnwrapMirror(Dart_Handle mirror);
Ivan Posva 2013/06/14 15:16:48 Indentation. Here and many places below.
gbracha 2013/06/14 20:42:46 Done.
154 186
155 static Dart_Handle UnwrapMirror(Dart_Handle mirror) { 187 static Dart_Handle UnwrapObjectMirror(Dart_Handle mirror) {
156 Dart_Handle field_name = NewString("_reference"); 188 Dart_Handle field_name = NewString("_reference");
157 Dart_Handle vm_ref = Dart_GetField(mirror, field_name); 189 Dart_Handle vm_ref = Dart_GetField(mirror, field_name);
158 if (Dart_IsError(vm_ref)) { 190 if (Dart_IsError(vm_ref)) {
159 return vm_ref; 191 return vm_ref;
160 } 192 }
161 return UnwrapVMReference(vm_ref); 193 return UnwrapVMReference(vm_ref);
162 } 194 }
163 195
164 196
197 static Dart_Handle UnwrapMethodMirror(Dart_Handle methodMirror) {
198 Dart_Handle namefield_name = NewString("simpleName");
199 Dart_Handle name_ref = Dart_GetField(methodMirror, namefield_name);
200 if (Dart_IsError(name_ref)) {
201 return name_ref;
202 }
203 Dart_Handle ownerfield_name = NewString("_owner");
204 Dart_Handle owner_mirror = Dart_GetField(methodMirror, ownerfield_name);
205 if (Dart_IsError(owner_mirror)) {
206 return owner_mirror;
207 }
208 Dart_Handle owner = UnwrapMirror(owner_mirror);
209 if (Dart_IsError(owner)) {
210 return owner;
211 }
212 Dart_Handle func = Dart_LookupFunction(owner, StringFromSymbol(name_ref));
213 if (Dart_IsError(func)) {
214 return func;
215 }
216 ASSERT(!Dart_IsNull(func));
217 return func;
218 }
219
220 static Dart_Handle UnwrapVariableMirror(Dart_Handle variableMirror) {
221 Dart_Handle namefield_name = NewString("simpleName");
222 Dart_Handle name_ref = Dart_GetField(variableMirror, namefield_name);
223 if (Dart_IsError(name_ref)) {
224 return name_ref;
225 }
226 Dart_Handle ownerfield_name = NewString("_owner");
227 Dart_Handle owner_mirror = Dart_GetField(variableMirror, ownerfield_name);
228 ASSERT(!Dart_IsNull(owner_mirror));
229 if (Dart_IsError(owner_mirror)) {
230 return owner_mirror;
231 }
232 Dart_Handle owner = UnwrapMirror(owner_mirror);
233 if (Dart_IsError(owner)) {
234 return owner;
235 }
236 Dart_Handle variable =
237 Dart_LookupVariable(owner, StringFromSymbol(name_ref));
238 if (Dart_IsError(variable)) {
239 return variable;
240 }
241 ASSERT(!Dart_IsNull(variable));
242 return variable;
243 }
244
245 static Dart_Handle UnwrapMirror(Dart_Handle mirror) {
246 // Caveat Emptor:
247 // only works for ObjectMirrors, VariableMirrors and MethodMirrors
248 // and their subtypes
249 bool is_method_mirror = false;
250 Dart_Handle result = IsMethodMirror(mirror, &is_method_mirror);
251 if (Dart_IsError(result)) {
252 return result;
253 }
254 if (is_method_mirror) {
255 return UnwrapMethodMirror(mirror);
256 }
257 bool is_variable_mirror = false;
258 result = IsVariableMirror(mirror, &is_variable_mirror);
259 if (Dart_IsError(result)) {
260 return result;
261 }
262 if (is_variable_mirror) {
263 return UnwrapVariableMirror(mirror);
264 }
265 return UnwrapObjectMirror(mirror);
266 // will return nonsense if mirror is not an ObjectMirror
267 }
268
165 static Dart_Handle UnwrapArg(Dart_Handle arg) { 269 static Dart_Handle UnwrapArg(Dart_Handle arg) {
166 if (Dart_IsError(arg)) { 270 if (Dart_IsError(arg)) {
167 return arg; 271 return arg;
168 } 272 }
169 bool is_mirror = false; 273 bool is_mirror = false;
170 Dart_Handle result = IsMirror(arg, &is_mirror); 274 Dart_Handle result = IsMirror(arg, &is_mirror);
171 if (Dart_IsError(result)) { 275 if (Dart_IsError(result)) {
172 return result; 276 return result;
173 } 277 }
174 if (is_mirror) { 278 if (is_mirror) {
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 return CreateTypedefMirror(intf, intf_name, lib, lib_mirror); 563 return CreateTypedefMirror(intf, intf_name, lib, lib_mirror);
460 } 564 }
461 565
462 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); 566 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl");
463 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name); 567 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
464 if (Dart_IsError(cls)) { 568 if (Dart_IsError(cls)) {
465 return cls; 569 return cls;
466 } 570 }
467 571
468 // TODO(turnidge): Why am I getting Null when I expect Object? 572 // TODO(turnidge): Why am I getting Null when I expect Object?
573 // TODO(gbracha): this is probably the root of bug 7868
469 Dart_Handle super_class = Dart_GetSuperclass(intf); 574 Dart_Handle super_class = Dart_GetSuperclass(intf);
470 if (Dart_IsNull(super_class)) { 575 if (Dart_IsNull(super_class)) {
471 super_class = Dart_GetClass(CoreLib(), NewString("Object")); 576 super_class = Dart_GetClass(CoreLib(), NewString("Object"));
472 } 577 }
473 // TODO(turnidge): Simplify code, now that default classes have been removed. 578 // TODO(turnidge): Simplify code, now that default classes have been removed.
474 Dart_Handle default_class = Dart_Null(); 579 Dart_Handle default_class = Dart_Null();
475 580
476 Dart_Handle intf_mirror = CreateLazyMirror(intf); 581 Dart_Handle intf_mirror = CreateLazyMirror(intf);
477 if (Dart_IsError(intf_mirror)) { 582 if (Dart_IsError(intf_mirror)) {
478 return intf_mirror; 583 return intf_mirror;
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
1101 name_handle, 1206 name_handle,
1102 lib_handle, 1207 lib_handle,
1103 lib_mirror); 1208 lib_mirror);
1104 if (Dart_IsError(result)) { 1209 if (Dart_IsError(result)) {
1105 Dart_PropagateError(result); 1210 Dart_PropagateError(result);
1106 } 1211 }
1107 Dart_SetReturnValue(args, result); 1212 Dart_SetReturnValue(args, result);
1108 Dart_ExitScope(); 1213 Dart_ExitScope();
1109 } 1214 }
1110 1215
1216 void NATIVE_ENTRY_FUNCTION(Mirrors_metadata)(
1217 Dart_NativeArguments args) {
Ivan Posva 2013/06/14 15:16:48 One line?
gbracha 2013/06/14 20:42:46 Done.
1218 Dart_EnterScope();
1219 Dart_Handle mirror = Dart_GetNativeArgument(args, 0);
1220
1221 Dart_Handle reflectee = UnwrapMirror(mirror);
1222 Dart_Handle result = Dart_GetMetadata(reflectee);
1223 if (Dart_IsError(result)) {
1224 Dart_PropagateError(result);
1225 }
1226 ASSERT(Dart_IsList(result));
1227 Dart_SetReturnValue(args, result);
1228 Dart_ExitScope();
1229 }
1230
1111 void NATIVE_ENTRY_FUNCTION(LocalObjectMirrorImpl_invoke)( 1231 void NATIVE_ENTRY_FUNCTION(LocalObjectMirrorImpl_invoke)(
1112 Dart_NativeArguments args) { 1232 Dart_NativeArguments args) {
1113 Dart_EnterScope(); 1233 Dart_EnterScope();
1114 Dart_Handle mirror = Dart_GetNativeArgument(args, 0); 1234 Dart_Handle mirror = Dart_GetNativeArgument(args, 0);
1115 Dart_Handle member_name = Dart_GetNativeArgument(args, 1); 1235 Dart_Handle member_name = Dart_GetNativeArgument(args, 1);
1116 // The arguments are either simple values or instance mirrors. 1236 // The arguments are either simple values or instance mirrors.
1117 Dart_Handle positional_arguments = Dart_GetNativeArgument(args, 2); 1237 Dart_Handle positional_arguments = Dart_GetNativeArgument(args, 2);
1118 Dart_Handle async = Dart_GetNativeArgument(args, 3); 1238 Dart_Handle async = Dart_GetNativeArgument(args, 3);
1119 1239
1120 Dart_Handle reflectee = UnwrapMirror(mirror); 1240 Dart_Handle reflectee = UnwrapMirror(mirror);
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1271 } 1391 }
1272 1392
1273 Dart_Handle wrapped_result = CreateInstanceMirror(result); 1393 Dart_Handle wrapped_result = CreateInstanceMirror(result);
1274 if (Dart_IsError(wrapped_result)) { 1394 if (Dart_IsError(wrapped_result)) {
1275 Dart_PropagateError(wrapped_result); 1395 Dart_PropagateError(wrapped_result);
1276 } 1396 }
1277 Dart_SetReturnValue(args, wrapped_result); 1397 Dart_SetReturnValue(args, wrapped_result);
1278 Dart_ExitScope(); 1398 Dart_ExitScope();
1279 } 1399 }
1280 1400
1281
1282 void HandleMirrorsMessage(Isolate* isolate, 1401 void HandleMirrorsMessage(Isolate* isolate,
1283 Dart_Port reply_port, 1402 Dart_Port reply_port,
1284 const Instance& message) { 1403 const Instance& message) {
1285 UNIMPLEMENTED(); 1404 UNIMPLEMENTED();
1286 } 1405 }
1287 1406
1288 } // namespace dart 1407 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | runtime/lib/mirrors_impl.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698