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

Side by Side 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, 5 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
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 "vm/bootstrap_natives.h" 5 #include "vm/bootstrap_natives.h"
6 6
7 #include "platform/json.h" 7 #include "platform/json.h"
8 #include "include/dart_api.h" 8 #include "include/dart_api.h"
9 #include "include/dart_debugger_api.h" 9 #include "include/dart_debugger_api.h"
10 #include "vm/dart_entry.h" 10 #include "vm/dart_entry.h"
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } else { 191 } else {
192 // Simple value. 192 // Simple value.
193 ASSERT(IsSimpleValue(arg)); 193 ASSERT(IsSimpleValue(arg));
194 arg_array->Add(arg); 194 arg_array->Add(arg);
195 } 195 }
196 } 196 }
197 return Dart_True(); 197 return Dart_True();
198 } 198 }
199 199
200 200
201 static Dart_Handle CreateLazyLibraryMirror(Dart_Handle lib) { 201 static Dart_Handle CreateLazyMirror(Dart_Handle target) {
202 if (Dart_IsNull(lib)) { 202 if (Dart_IsNull(target)) {
203 return lib; 203 return target;
204 } 204 }
205 Dart_Handle cls_name = Dart_NewString("_LazyLibraryMirror"); 205 if (Dart_IsLibrary(target)) {
206 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name); 206 Dart_Handle cls_name = Dart_NewString("_LazyLibraryMirror");
207 const int kNumArgs = 1; 207 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
208 Dart_Handle args[kNumArgs]; 208 const int kNumArgs = 1;
209 args[0] = Dart_LibraryName(lib); 209 Dart_Handle args[kNumArgs];
210 return Dart_New(cls, Dart_Null(), kNumArgs, args); 210 args[0] = Dart_LibraryName(target);
211 return Dart_New(cls, Dart_Null(), kNumArgs, args);
212 } else {
213 ASSERT(Dart_IsClass(target) || Dart_IsInterface(target));
214 Dart_Handle cls_name = Dart_NewString("_LazyInterfaceMirror");
215 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
216
217 Dart_Handle lib = Dart_ClassGetLibrary(target);
218 Dart_Handle lib_name;
219 if (Dart_IsNull(lib)) {
220 lib_name = Dart_Null();
221 } else {
222 lib_name = Dart_LibraryName(lib);
223 }
224
225 const int kNumArgs = 2;
226 Dart_Handle args[kNumArgs];
227 args[0] = lib_name;
228 args[1] = Dart_ClassName(target);
229 return Dart_New(cls, Dart_Null(), kNumArgs, args);
230 }
211 } 231 }
212 232
213 233
214 static Dart_Handle CreateLazyInterfaceMirror(Dart_Handle intf) {
215 if (Dart_IsNull(intf)) {
216 return intf;
217 }
218 Dart_Handle cls_name = Dart_NewString("_LazyInterfaceMirror");
219 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
220 const int kNumArgs = 2;
221 Dart_Handle args[kNumArgs];
222 args[0] = Dart_LibraryName(Dart_ClassGetLibrary(intf));
223 args[1] = Dart_ClassName(intf);
224 return Dart_New(cls, Dart_Null(), kNumArgs, args);
225 }
226
227
228 static Dart_Handle CreateImplementsList(Dart_Handle intf) { 234 static Dart_Handle CreateImplementsList(Dart_Handle intf) {
229 intptr_t len = 0; 235 intptr_t len = 0;
230 Dart_Handle result = Dart_ClassGetInterfaceCount(intf, &len); 236 Dart_Handle result = Dart_ClassGetInterfaceCount(intf, &len);
231 if (Dart_IsError(result)) { 237 if (Dart_IsError(result)) {
232 return result; 238 return result;
233 } 239 }
234 240
235 Dart_Handle mirror_list = Dart_NewList(len); 241 Dart_Handle mirror_list = Dart_NewList(len);
236 if (Dart_IsError(mirror_list)) { 242 if (Dart_IsError(mirror_list)) {
237 return mirror_list; 243 return mirror_list;
238 } 244 }
239 245
240 for (int i = 0; i < len; i++) { 246 for (int i = 0; i < len; i++) {
241 Dart_Handle interface = Dart_ClassGetInterfaceAt(intf, i); 247 Dart_Handle interface = Dart_ClassGetInterfaceAt(intf, i);
242 if (Dart_IsError(interface)) { 248 if (Dart_IsError(interface)) {
243 return interface; 249 return interface;
244 } 250 }
245 Dart_Handle mirror = CreateLazyInterfaceMirror(interface); 251 Dart_Handle mirror = CreateLazyMirror(interface);
246 if (Dart_IsError(mirror)) { 252 if (Dart_IsError(mirror)) {
247 return mirror; 253 return mirror;
248 } 254 }
249 Dart_Handle result = Dart_ListSetAt(mirror_list, i, mirror); 255 Dart_Handle result = Dart_ListSetAt(mirror_list, i, mirror);
250 if (Dart_IsError(result)) { 256 if (Dart_IsError(result)) {
251 return result; 257 return result;
252 } 258 }
253 } 259 }
254 return mirror_list; 260 return mirror_list;
255 } 261 }
256 262
257 263
264 static Dart_Handle CreateMemberMap(Dart_Handle owner);
265
266
258 static Dart_Handle CreateInterfaceMirror(Dart_Handle intf, 267 static Dart_Handle CreateInterfaceMirror(Dart_Handle intf,
259 Dart_Handle intf_name, 268 Dart_Handle intf_name,
260 Dart_Handle lib) { 269 Dart_Handle lib,
270 Dart_Handle lib_mirror) {
261 ASSERT(Dart_IsClass(intf) || Dart_IsInterface(intf)); 271 ASSERT(Dart_IsClass(intf) || Dart_IsInterface(intf));
262 Dart_Handle cls_name = Dart_NewString("_LocalInterfaceMirrorImpl"); 272 Dart_Handle cls_name = Dart_NewString("_LocalInterfaceMirrorImpl");
263 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name); 273 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
264 if (Dart_IsError(cls)) { 274 if (Dart_IsError(cls)) {
265 return cls; 275 return cls;
266 } 276 }
267 277
268 // TODO(turnidge): Why am I getting Null when I expect Object? 278 // TODO(turnidge): Why am I getting Null when I expect Object?
269 Dart_Handle super_class = Dart_GetSuperclass(intf); 279 Dart_Handle super_class = Dart_GetSuperclass(intf);
270 if (Dart_IsNull(super_class)) { 280 if (Dart_IsNull(super_class)) {
271 super_class = Dart_GetClass(CoreLib(), Dart_NewString("Object")); 281 super_class = Dart_GetClass(CoreLib(), Dart_NewString("Object"));
272 } 282 }
273 Dart_Handle default_class = Dart_ClassGetDefault(intf); 283 Dart_Handle default_class = Dart_ClassGetDefault(intf);
274 284 Dart_Handle member_map = CreateMemberMap(intf);
275 const int kNumArgs = 7; 285 if (Dart_IsError(member_map)) {
286 return member_map;
287 }
288
289 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.
276 Dart_Handle args[kNumArgs]; 290 Dart_Handle args[kNumArgs];
277 args[0] = CreateVMReference(intf); 291 args[0] = CreateVMReference(intf);
278 args[1] = intf_name; 292 args[1] = intf_name;
279 args[2] = Dart_NewBoolean(Dart_IsClass(intf)); 293 args[2] = Dart_NewBoolean(Dart_IsClass(intf));
280 args[3] = CreateLazyLibraryMirror(lib); 294 args[3] = lib_mirror;
281 args[4] = CreateLazyInterfaceMirror(super_class); 295 args[4] = CreateLazyMirror(super_class);
282 args[5] = CreateImplementsList(intf); 296 args[5] = CreateImplementsList(intf);
283 args[6] = CreateLazyInterfaceMirror(default_class); 297 args[6] = CreateLazyMirror(default_class);
298 args[7] = member_map;
284 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args); 299 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
285 return mirror; 300 return mirror;
286 } 301 }
287 302
288 303
289 static Dart_Handle CreateLibraryMemberMap(Dart_Handle lib) { 304 static Dart_Handle CreateMethodMirror(Dart_Handle func,
305 Dart_Handle func_name,
306 Dart_Handle lib_mirror) {
307 ASSERT(Dart_IsFunction(func));
308 Dart_Handle cls_name = Dart_NewString("_LocalMethodMirrorImpl");
309 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
310 if (Dart_IsError(cls)) {
311 return cls;
312 }
313
314 bool is_static = false;
315 bool is_abstract = false;
316 bool is_getter = false;
317 bool is_setter = false;
318 bool is_constructor = false;
319
320 Dart_Handle result = Dart_FunctionIsStatic(func, &is_static);
321 if (Dart_IsError(result)) {
322 return result;
323 }
324 result = Dart_FunctionIsAbstract(func, &is_abstract);
325 if (Dart_IsError(result)) {
326 return result;
327 }
328 result = Dart_FunctionIsGetter(func, &is_getter);
329 if (Dart_IsError(result)) {
330 return result;
331 }
332 result = Dart_FunctionIsSetter(func, &is_setter);
333 if (Dart_IsError(result)) {
334 return result;
335 }
336 result = Dart_FunctionIsConstructor(func, &is_constructor);
337 if (Dart_IsError(result)) {
338 return result;
339 }
340
341 // TODO(turnidge): Implement constructor kinds (arguments 7 - 10).
342 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.
343 Dart_Handle args[kNumArgs];
344 args[0] = func_name;
345 args[1] = lib_mirror;
346 args[2] = Dart_NewBoolean(is_static);
347 args[3] = Dart_NewBoolean(is_abstract);
348 args[4] = Dart_NewBoolean(is_getter);
349 args[5] = Dart_NewBoolean(is_setter);
350 args[6] = Dart_NewBoolean(is_constructor);
351 args[7] = Dart_False();
352 args[8] = Dart_False();
353 args[9] = Dart_False();
354 args[10] = Dart_False();
355 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
356 return mirror;
357 }
358
359
360 static Dart_Handle CreateVariableMirror(Dart_Handle var,
361 Dart_Handle var_name,
362 Dart_Handle lib_mirror) {
363 ASSERT(Dart_IsVariable(var));
364 Dart_Handle cls_name = Dart_NewString("_LocalVariableMirrorImpl");
365 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
366 if (Dart_IsError(cls)) {
367 return cls;
368 }
369
370 bool is_static = false;
371 bool is_final = false;
372
373 Dart_Handle result = Dart_VariableIsStatic(var, &is_static);
374 if (Dart_IsError(result)) {
375 return result;
376 }
377 result = Dart_VariableIsFinal(var, &is_final);
378 if (Dart_IsError(result)) {
379 return result;
380 }
381
382 const int kNumArgs = 4;
cshapiro 2012/06/28 23:57:47 Same here too.
turnidge 2012/07/09 23:45:17 Done.
383 Dart_Handle args[kNumArgs];
384 args[0] = var_name;
385 args[1] = lib_mirror;
386 args[2] = Dart_NewBoolean(is_static);
387 args[3] = Dart_NewBoolean(is_final);
388 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
389 return mirror;
390 }
391
392
393 static Dart_Handle CreateMemberMap(Dart_Handle owner) {
290 // TODO(turnidge): This should be an immutable map. 394 // TODO(turnidge): This should be an immutable map.
395 Dart_Handle owner_mirror = CreateLazyMirror(owner);
396 if (Dart_IsError(owner_mirror)) {
397 return owner_mirror;
398 }
291 Dart_Handle map = MapNew(); 399 Dart_Handle map = MapNew();
292 400
293 Dart_Handle intf_names = Dart_LibraryGetClassNames(lib); 401 Dart_Handle names;
294 if (Dart_IsError(intf_names)) { 402 Dart_Handle result;
295 return intf_names;
296 }
297 intptr_t len; 403 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
298 Dart_Handle result = Dart_ListLength(intf_names, &len); 404
405 if (Dart_IsLibrary(owner)) {
406 // Add classes/interfaces.
407 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.
408 if (Dart_IsError(names)) {
409 return names;
410 }
411 result = Dart_ListLength(names, &len);
412 if (Dart_IsError(result)) {
413 return result;
414 }
415 for (int i = 0; i < len; i++) {
416 Dart_Handle intf_name = Dart_ListGetAt(names, i);
417 Dart_Handle intf = Dart_GetClass(owner, intf_name);
418 if (Dart_IsError(intf)) {
419 return intf;
420 }
421 Dart_Handle intf_mirror =
422 CreateInterfaceMirror(intf, intf_name, owner, owner_mirror);
423 if (Dart_IsError(intf_mirror)) {
424 return intf_mirror;
425 }
426 result = MapAdd(map, intf_name, intf_mirror);
427 if (Dart_IsError(result)) {
428 return result;
429 }
430 }
431 }
432
433 // Add functions.
434 names = Dart_GetFunctionNames(owner);
435 if (Dart_IsError(names)) {
436 return names;
437 }
438 result = Dart_ListLength(names, &len);
299 if (Dart_IsError(result)) { 439 if (Dart_IsError(result)) {
300 return result; 440 return result;
301 } 441 }
302 for (int i = 0; i < len; i++) { 442 for (int i = 0; i < len; i++) {
303 Dart_Handle intf_name = Dart_ListGetAt(intf_names, i); 443 Dart_Handle func_name = Dart_ListGetAt(names, i);
304 Dart_Handle intf = Dart_GetClass(lib, intf_name); 444 Dart_Handle func = Dart_LookupFunction(owner, func_name);
305 if (Dart_IsError(intf)) { 445 if (Dart_IsError(func)) {
306 return intf; 446 return func;
307 } 447 }
308 Dart_Handle intf_mirror = CreateInterfaceMirror(intf, intf_name, lib); 448 ASSERT(!Dart_IsNull(func));
309 if (Dart_IsError(intf_mirror)) { 449 Dart_Handle func_mirror = CreateMethodMirror(func, func_name, owner_mirror);
310 return intf_mirror; 450 if (Dart_IsError(func_mirror)) {
311 } 451 return func_mirror;
312 result = MapAdd(map, intf_name, intf_mirror); 452 }
313 } 453 result = MapAdd(map, func_name, func_mirror);
454 if (Dart_IsError(result)) {
455 return result;
456 }
457 }
458
459 // Add variables.
460 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.
461 if (Dart_IsError(names)) {
462 return names;
463 }
464 result = Dart_ListLength(names, &len);
465 if (Dart_IsError(result)) {
466 return result;
467 }
468 for (int i = 0; i < len; i++) {
469 Dart_Handle var_name = Dart_ListGetAt(names, i);
470 Dart_Handle var = Dart_LookupVariable(owner, var_name);
471 if (Dart_IsError(var)) {
472 return var;
473 }
474 ASSERT(!Dart_IsNull(var));
475 Dart_Handle var_mirror = CreateVariableMirror(var, var_name, owner_mirror);
476 if (Dart_IsError(var_mirror)) {
477 return var_mirror;
478 }
479 result = MapAdd(map, var_name, var_mirror);
480 if (Dart_IsError(result)) {
481 return result;
482 }
483 }
484
314 return map; 485 return map;
315 } 486 }
316 487
317 488
318 static Dart_Handle CreateLibraryMirror(Dart_Handle lib) { 489 static Dart_Handle CreateLibraryMirror(Dart_Handle lib) {
319 Dart_Handle cls_name = Dart_NewString("_LocalLibraryMirrorImpl"); 490 Dart_Handle cls_name = Dart_NewString("_LocalLibraryMirrorImpl");
320 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name); 491 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
321 if (Dart_IsError(cls)) { 492 if (Dart_IsError(cls)) {
322 return cls; 493 return cls;
323 } 494 }
324 Dart_Handle member_map = CreateLibraryMemberMap(lib); 495 Dart_Handle member_map = CreateMemberMap(lib);
325 if (Dart_IsError(member_map)) { 496 if (Dart_IsError(member_map)) {
326 return member_map; 497 return member_map;
327 } 498 }
328 const int kNumArgs = 4; 499 const int kNumArgs = 4;
329 Dart_Handle args[kNumArgs]; 500 Dart_Handle args[kNumArgs];
330 args[0] = CreateVMReference(lib); 501 args[0] = CreateVMReference(lib);
331 args[1] = Dart_LibraryName(lib); 502 args[1] = Dart_LibraryName(lib);
332 args[2] = Dart_LibraryUrl(lib); 503 args[2] = Dart_LibraryUrl(lib);
333 args[3] = member_map; 504 args[3] = member_map;
334 Dart_Handle lib_mirror = Dart_New(cls, Dart_Null(), kNumArgs, args); 505 Dart_Handle lib_mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 if (Dart_IsError(cls)) { 581 if (Dart_IsError(cls)) {
411 return cls; 582 return cls;
412 } 583 }
413 584
414 // TODO(turnidge): This is wrong. The Null class is distinct from object. 585 // TODO(turnidge): This is wrong. The Null class is distinct from object.
415 Dart_Handle object_class = Dart_GetClass(CoreLib(), Dart_NewString("Object")); 586 Dart_Handle object_class = Dart_GetClass(CoreLib(), Dart_NewString("Object"));
416 587
417 const int kNumArgs = 4; 588 const int kNumArgs = 4;
418 Dart_Handle args[kNumArgs]; 589 Dart_Handle args[kNumArgs];
419 args[0] = CreateVMReference(Dart_Null()); 590 args[0] = CreateVMReference(Dart_Null());
420 args[1] = CreateLazyInterfaceMirror(object_class); 591 args[1] = CreateLazyMirror(object_class);
421 args[2] = Dart_True(); 592 args[2] = Dart_True();
422 args[3] = Dart_Null(); 593 args[3] = Dart_Null();
423 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args); 594 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
424 return mirror; 595 return mirror;
425 } 596 }
426 597
427 598
428 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) { 599 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) {
429 if (Dart_IsNull(instance)) { 600 if (Dart_IsNull(instance)) {
430 return CreateNullMirror(); 601 return CreateNullMirror();
431 } 602 }
432 ASSERT(Dart_IsInstance(instance)); 603 ASSERT(Dart_IsInstance(instance));
433 Dart_Handle cls_name = Dart_NewString("_LocalInstanceMirrorImpl"); 604 Dart_Handle cls_name = Dart_NewString("_LocalInstanceMirrorImpl");
434 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name); 605 Dart_Handle cls = Dart_GetClass(MirrorLib(), cls_name);
435 if (Dart_IsError(cls)) { 606 if (Dart_IsError(cls)) {
436 return cls; 607 return cls;
437 } 608 }
438 Dart_Handle instance_cls = Dart_InstanceGetClass(instance); 609 Dart_Handle instance_cls = Dart_InstanceGetClass(instance);
439 if (Dart_IsError(instance_cls)) { 610 if (Dart_IsError(instance_cls)) {
440 return instance_cls; 611 return instance_cls;
441 } 612 }
442 bool is_simple = IsSimpleValue(instance); 613 bool is_simple = IsSimpleValue(instance);
443 const int kNumArgs = 4; 614 const int kNumArgs = 4;
444 Dart_Handle args[kNumArgs]; 615 Dart_Handle args[kNumArgs];
445 args[0] = CreateVMReference(instance); 616 args[0] = CreateVMReference(instance);
446 args[1] = CreateLazyInterfaceMirror(instance_cls); 617 args[1] = CreateLazyMirror(instance_cls);
447 args[2] = Dart_NewBoolean(is_simple); 618 args[2] = Dart_NewBoolean(is_simple);
448 args[3] = (is_simple ? instance : Dart_Null()); 619 args[3] = (is_simple ? instance : Dart_Null());
449 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args); 620 Dart_Handle mirror = Dart_New(cls, Dart_Null(), kNumArgs, args);
450 return mirror; 621 return mirror;
451 } 622 }
452 623
453 624
454 static Dart_Handle CreateMirroredError(Dart_Handle error) { 625 static Dart_Handle CreateMirroredError(Dart_Handle error) {
455 ASSERT(Dart_IsError(error)); 626 ASSERT(Dart_IsError(error));
456 if (Dart_IsUnhandledExceptionError(error)) { 627 if (Dart_IsUnhandledExceptionError(error)) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 Dart_SetReturnValue(args, wrapped_result); 713 Dart_SetReturnValue(args, wrapped_result);
543 } 714 }
544 715
545 void HandleMirrorsMessage(Isolate* isolate, 716 void HandleMirrorsMessage(Isolate* isolate,
546 Dart_Port reply_port, 717 Dart_Port reply_port,
547 const Instance& message) { 718 const Instance& message) {
548 UNIMPLEMENTED(); 719 UNIMPLEMENTED();
549 } 720 }
550 721
551 } // namespace dart 722 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698