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

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

Issue 20216002: Make ClassMirror.typeVariables lazy and convert dependencies to native code. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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
« 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 "include/dart_mirrors_api.h" 7 #include "include/dart_mirrors_api.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_api_state.h" // TODO(11742): Remove with CreateMirrorRef. 9 #include "vm/dart_api_state.h" // TODO(11742): Remove with CreateMirrorRef.
10 #include "vm/bootstrap_natives.h" 10 #include "vm/bootstrap_natives.h"
11 #include "vm/dart_entry.h" 11 #include "vm/dart_entry.h"
12 #include "vm/exceptions.h" 12 #include "vm/exceptions.h"
13 #include "vm/message.h" 13 #include "vm/message.h"
14 #include "vm/port.h" 14 #include "vm/port.h"
15 #include "vm/resolver.h" 15 #include "vm/resolver.h"
16 #include "vm/symbols.h" 16 #include "vm/symbols.h"
17 #include "lib/invocation_mirror.h" 17 #include "lib/invocation_mirror.h"
18 18
19 namespace dart { 19 namespace dart {
20 20
21 static void ThrowInvokeError(const Error& error);
22
21 static RawInstance* CreateMirror(const String& mirror_class_name, 23 static RawInstance* CreateMirror(const String& mirror_class_name,
22 const Array& constructor_arguments) { 24 const Array& constructor_arguments) {
23 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); 25 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary());
24 const String& constructor_name = Symbols::Dot(); 26 const String& constructor_name = Symbols::Dot();
25 27
26 const Object& result = Object::Handle( 28 const Object& result = Object::Handle(
27 DartLibraryCalls::InstanceCreate(mirrors_lib, 29 DartLibraryCalls::InstanceCreate(mirrors_lib,
28 mirror_class_name, 30 mirror_class_name,
29 constructor_name, 31 constructor_name,
30 constructor_arguments)); 32 constructor_arguments));
31 ASSERT(result.IsInstance()); 33 #ifdef DEBUG
34 if (result.IsError()) {
35 ThrowInvokeError(Error::Cast(result));
36 }
37 #endif // DEBUG
siva 2013/07/26 22:10:34 Not sure I understand why ThrowInvokeError should
Michael Lippautz (Google) 2013/07/26 23:30:07 Done.
32 return Instance::Cast(result).raw(); 38 return Instance::Cast(result).raw();
33 } 39 }
34 40
35 41
36 inline Dart_Handle NewString(const char* str) { 42 inline Dart_Handle NewString(const char* str) {
37 return Dart_NewStringFromCString(str); 43 return Dart_NewStringFromCString(str);
38 } 44 }
39 45
40 46
41 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { 47 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) {
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 } 356 }
351 Dart_Handle result = Dart_ListSetAt(mirror_list, i, mirror); 357 Dart_Handle result = Dart_ListSetAt(mirror_list, i, mirror);
352 if (Dart_IsError(result)) { 358 if (Dart_IsError(result)) {
353 return result; 359 return result;
354 } 360 }
355 } 361 }
356 return mirror_list; 362 return mirror_list;
357 } 363 }
358 364
359 365
360 static Dart_Handle CreateTypeVariableMirrorUsingApi(Dart_Handle type_var,
361 Dart_Handle type_var_name,
362 Dart_Handle owner_mirror) {
363 ASSERT(Dart_IsTypeVariable(type_var));
364 Dart_Handle cls_name = NewString("_LocalTypeVariableMirrorImpl");
365 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL);
366 if (Dart_IsError(type)) {
367 return type;
368 }
369
370 Dart_Handle upper_bound = Dart_TypeVariableUpperBound(type_var);
371 if (Dart_IsError(upper_bound)) {
372 return upper_bound;
373 }
374
375 Dart_Handle args[] = {
376 CreateMirrorReference(type_var),
377 type_var_name,
378 owner_mirror,
379 CreateLazyMirror(upper_bound),
380 };
381 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args);
382 return mirror;
383 }
384
385
386 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, 366 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param,
387 const Instance& owner_mirror) { 367 const Instance& owner_mirror) {
388 Instance& retvalue = Instance::Handle(); 368 const Array& args = Array::Handle(Array::New(3));
389 Dart_EnterScope(); 369 args.SetAt(0, param);
390 Isolate* isolate = Isolate::Current(); 370 args.SetAt(1, String::Handle(param.name()));
391 Dart_Handle param_handle = Api::NewHandle(isolate, param.raw()); 371 args.SetAt(2, owner_mirror);
392 if (Dart_IsError(param_handle)) { 372 return CreateMirror(Symbols::_LocalTypeVariableMirrorImpl(), args);
393 Dart_PropagateError(param_handle);
394 }
395 Dart_Handle name_handle = Api::NewHandle(isolate, param.Name());
396 if (Dart_IsError(name_handle)) {
397 Dart_PropagateError(name_handle);
398 }
399 // Until we get rid of lazy mirrors, we must have owners.
400 Dart_Handle owner_handle;
401 if (owner_mirror.IsNull()) {
402 owner_handle = Api::NewHandle(isolate, param.parameterized_class());
403 if (Dart_IsError(owner_handle)) {
404 Dart_PropagateError(owner_handle);
405 }
406 owner_handle = CreateLazyMirror(owner_handle);
407 if (Dart_IsError(owner_handle)) {
408 Dart_PropagateError(owner_handle);
409 }
410 } else {
411 owner_handle = Api::NewHandle(isolate, owner_mirror.raw());
412 if (Dart_IsError(owner_handle)) {
413 Dart_PropagateError(owner_handle);
414 }
415 }
416 // TODO(11742): At some point the handle calls will be replaced by inlined
417 // functionality.
418 Dart_Handle result = CreateTypeVariableMirrorUsingApi(param_handle,
419 name_handle,
420 owner_handle);
421 if (Dart_IsError(result)) {
422 Dart_PropagateError(result);
423 }
424 retvalue ^= Api::UnwrapHandle(result);
425 Dart_ExitScope();
426 return retvalue.raw();
427 } 373 }
428 374
429 375
430 static Dart_Handle CreateTypeVariableMap(Dart_Handle owner, 376 // We create a list in native code and let Dart code create the actual ordered
431 Dart_Handle owner_mirror) { 377 // map then.
siva 2013/07/26 22:10:34 "let Dart code create the type mirror object and t
Michael Lippautz (Google) 2013/07/26 23:30:07 Done.
432 ASSERT(Dart_IsClass(owner)); 378 static RawInstance* CreateTypeVariableList(const Class& cls) {
433 // TODO(turnidge): This should be an immutable map. 379 ASSERT(cls.IsClass());
siva 2013/07/26 22:10:34 The function prototype states it is Class handle s
Michael Lippautz (Google) 2013/07/26 23:30:07 Removed.
434 Dart_Handle map = MapNew(); 380 TypeArguments& args = TypeArguments::Handle(cls.type_parameters());
435 if (Dart_IsError(map)) { 381 const Array& result = Array::Handle(Array::New(args.Length() * 2));
436 return map; 382 TypeParameter& type = TypeParameter::Handle();
383 String& name = String::Handle();
384 for (intptr_t i = 0; i < result.Length(); i += 2) {
385 type ^= args.TypeAt(i);
386 ASSERT(type.IsTypeParameter());
387 name ^= type.name();
388 result.SetAt(i, name);
389 result.SetAt(i + 1, type);
437 } 390 }
438 391 return result.raw();
439 Dart_Handle names = Dart_GetTypeVariableNames(owner);
440 if (Dart_IsError(names)) {
441 return names;
442 }
443 intptr_t len;
444 Dart_Handle result = Dart_ListLength(names, &len);
445 if (Dart_IsError(result)) {
446 return result;
447 }
448 for (intptr_t i = 0; i < len; i++) {
449 Dart_Handle type_var_name = Dart_ListGetAt(names, i);
450 Dart_Handle type_var = Dart_LookupTypeVariable(owner, type_var_name);
451 if (Dart_IsError(type_var)) {
452 return type_var;
453 }
454 ASSERT(!Dart_IsNull(type_var));
455 Dart_Handle type_var_mirror =
456 CreateTypeVariableMirrorUsingApi(type_var, type_var_name, owner_mirror);
457 if (Dart_IsError(type_var_mirror)) {
458 return type_var_mirror;
459 }
460 result = MapAdd(map, type_var_name, type_var_mirror);
461 if (Dart_IsError(result)) {
462 return result;
463 }
464 }
465 return map;
466 } 392 }
467 393
468 394
469 static Dart_Handle CreateTypedefMirror(Dart_Handle cls, 395 static Dart_Handle CreateTypedefMirror(Dart_Handle cls,
470 Dart_Handle cls_name, 396 Dart_Handle cls_name,
471 Dart_Handle owner_mirror) { 397 Dart_Handle owner_mirror) {
472 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl"); 398 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl");
473 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); 399 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL);
474 if (Dart_IsError(mirror_type)) { 400 if (Dart_IsError(mirror_type)) {
475 return mirror_type; 401 return mirror_type;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 Dart_Handle default_class = Dart_Null(); 447 Dart_Handle default_class = Dart_Null();
522 448
523 Dart_Handle intf_mirror = CreateLazyMirror(intf); 449 Dart_Handle intf_mirror = CreateLazyMirror(intf);
524 if (Dart_IsError(intf_mirror)) { 450 if (Dart_IsError(intf_mirror)) {
525 return intf_mirror; 451 return intf_mirror;
526 } 452 }
527 Dart_Handle constructor_map = CreateConstructorMap(intf, intf_mirror); 453 Dart_Handle constructor_map = CreateConstructorMap(intf, intf_mirror);
528 if (Dart_IsError(constructor_map)) { 454 if (Dart_IsError(constructor_map)) {
529 return constructor_map; 455 return constructor_map;
530 } 456 }
531 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror);
532 if (Dart_IsError(type_var_map)) {
533 return type_var_map;
534 }
535 457
536 Dart_Handle args[] = { 458 Dart_Handle args[] = {
537 CreateMirrorReference(intf), 459 CreateMirrorReference(intf),
538 Dart_Null(), // "name" 460 Dart_Null(), // "name"
539 Dart_NewBoolean(Dart_IsClass(intf)), 461 Dart_NewBoolean(Dart_IsClass(intf)),
540 lib_mirror, 462 lib_mirror,
541 CreateLazyMirror(super_class), 463 CreateLazyMirror(super_class),
542 CreateImplementsList(intf), 464 CreateImplementsList(intf),
543 CreateLazyMirror(default_class), 465 CreateLazyMirror(default_class),
544 constructor_map, 466 constructor_map
545 type_var_map,
546 }; 467 };
547 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); 468 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args);
548 return mirror; 469 return mirror;
549 } 470 }
550 471
551 472
552 static RawInstance* CreateMethodMirror(const Function& func, 473 static RawInstance* CreateMethodMirror(const Function& func,
553 const Instance& owner_mirror) { 474 const Instance& owner_mirror) {
554 const Array& args = Array::Handle(Array::New(11)); 475 const Array& args = Array::Handle(Array::New(11));
555 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); 476 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func)));
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 member_mirror = CreateMethodMirror(func, owner_mirror); 1041 member_mirror = CreateMethodMirror(func, owner_mirror);
1121 member_mirrors.Add(member_mirror); 1042 member_mirrors.Add(member_mirror);
1122 } 1043 }
1123 } 1044 }
1124 } 1045 }
1125 1046
1126 return member_mirrors.raw(); 1047 return member_mirrors.raw();
1127 } 1048 }
1128 1049
1129 1050
1051 DEFINE_NATIVE_ENTRY(ClassMirror_type_variables, 1) {
1052 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1053 const Class& klass = Class::Handle(ref.GetClassReferent());
1054 return CreateTypeVariableList(klass);
1055 }
1056
1057
1058 DEFINE_NATIVE_ENTRY(LocalTypeVariableMirror_owner, 1) {
1059 GET_NON_NULL_NATIVE_ARGUMENT(TypeParameter, param, arguments->NativeArgAt(0));
1060 return CreateClassMirror(Class::Handle(param.parameterized_class()),
1061 Instance::null_instance());
1062 }
1063
1064
1065 DEFINE_NATIVE_ENTRY(LocalTypeVariableMirror_upper_bound, 1) {
1066 GET_NON_NULL_NATIVE_ARGUMENT(TypeParameter, param, arguments->NativeArgAt(0));
1067 return CreateTypeMirror(AbstractType::Handle(param.bound()));
1068 }
1069
1070
1130 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled 1071 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled
1131 // exceptions. Wrap and propagate any compilation errors. 1072 // exceptions. Wrap and propagate any compilation errors.
1132 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver, 1073 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver,
1133 const Function& function, 1074 const Function& function,
1134 const String& target_name, 1075 const String& target_name,
1135 const Array& arguments) { 1076 const Array& arguments) {
1136 // Note "arguments" is already the internal arguments with the receiver as 1077 // Note "arguments" is already the internal arguments with the receiver as
1137 // the first element. 1078 // the first element.
1138 Object& result = Object::Handle(); 1079 Object& result = Object::Handle();
1139 if (function.IsNull()) { 1080 if (function.IsNull()) {
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after
1757 1698
1758 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { 1699 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) {
1759 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1700 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1760 const Field& field = Field::Handle(ref.GetFieldReferent()); 1701 const Field& field = Field::Handle(ref.GetFieldReferent());
1761 1702
1762 const AbstractType& type = AbstractType::Handle(field.type()); 1703 const AbstractType& type = AbstractType::Handle(field.type());
1763 return CreateTypeMirror(type); 1704 return CreateTypeMirror(type);
1764 } 1705 }
1765 1706
1766 } // namespace dart 1707 } // 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