| OLD | NEW |
| 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 "lib/invocation_mirror.h" |
| 6 #include "include/dart_debugger_api.h" | |
| 7 #include "include/dart_mirrors_api.h" | |
| 8 #include "vm/dart_api_impl.h" | |
| 9 #include "vm/dart_api_state.h" // TODO(11742): Remove with CreateMirrorRef. | |
| 10 #include "vm/bootstrap_natives.h" | 6 #include "vm/bootstrap_natives.h" |
| 11 #include "vm/dart_entry.h" | 7 #include "vm/dart_entry.h" |
| 12 #include "vm/exceptions.h" | 8 #include "vm/exceptions.h" |
| 13 #include "vm/message.h" | |
| 14 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
| 15 #include "vm/port.h" | 10 #include "vm/port.h" |
| 16 #include "vm/symbols.h" | 11 #include "vm/symbols.h" |
| 17 #include "lib/invocation_mirror.h" | |
| 18 | 12 |
| 19 namespace dart { | 13 namespace dart { |
| 20 | 14 |
| 21 static RawInstance* CreateMirror(const String& mirror_class_name, | 15 static RawInstance* CreateMirror(const String& mirror_class_name, |
| 22 const Array& constructor_arguments) { | 16 const Array& constructor_arguments) { |
| 23 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); | 17 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); |
| 24 const String& constructor_name = Symbols::Dot(); | 18 const String& constructor_name = Symbols::Dot(); |
| 25 | 19 |
| 26 const Object& result = Object::Handle( | 20 const Object& result = Object::Handle( |
| 27 DartLibraryCalls::InstanceCreate(mirrors_lib, | 21 DartLibraryCalls::InstanceCreate(mirrors_lib, |
| 28 mirror_class_name, | 22 mirror_class_name, |
| 29 constructor_name, | 23 constructor_name, |
| 30 constructor_arguments)); | 24 constructor_arguments)); |
| 31 ASSERT(!result.IsError()); | 25 ASSERT(!result.IsError()); |
| 32 return Instance::Cast(result).raw(); | 26 return Instance::Cast(result).raw(); |
| 33 } | 27 } |
| 34 | 28 |
| 35 | 29 |
| 36 inline Dart_Handle NewString(const char* str) { | |
| 37 return Dart_NewStringFromCString(str); | |
| 38 } | |
| 39 | |
| 40 | |
| 41 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { | 30 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { |
| 42 GET_NON_NULL_NATIVE_ARGUMENT(Instance, port, arguments->NativeArgAt(0)); | 31 GET_NON_NULL_NATIVE_ARGUMENT(Instance, port, arguments->NativeArgAt(0)); |
| 43 | 32 |
| 44 // Get the port id from the SendPort instance. | 33 // Get the port id from the SendPort instance. |
| 45 const Object& id_obj = Object::Handle(DartLibraryCalls::PortGetId(port)); | 34 const Object& id_obj = Object::Handle(DartLibraryCalls::PortGetId(port)); |
| 46 if (id_obj.IsError()) { | 35 if (id_obj.IsError()) { |
| 47 Exceptions::PropagateError(Error::Cast(id_obj)); | 36 Exceptions::PropagateError(Error::Cast(id_obj)); |
| 48 UNREACHABLE(); | 37 UNREACHABLE(); |
| 49 } | 38 } |
| 50 ASSERT(id_obj.IsSmi() || id_obj.IsMint()); | 39 ASSERT(id_obj.IsSmi() || id_obj.IsMint()); |
| 51 Integer& id = Integer::Handle(); | 40 Integer& id = Integer::Handle(); |
| 52 id ^= id_obj.raw(); | 41 id ^= id_obj.raw(); |
| 53 Dart_Port port_id = static_cast<Dart_Port>(id.AsInt64Value()); | 42 Dart_Port port_id = static_cast<Dart_Port>(id.AsInt64Value()); |
| 54 return Bool::Get(PortMap::IsLocalPort(port_id)); | 43 return Bool::Get(PortMap::IsLocalPort(port_id)); |
| 55 } | 44 } |
| 56 | 45 |
| 57 | 46 |
| 58 static Dart_Handle MirrorLib() { | |
| 59 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); | |
| 60 return Dart_LookupLibrary(mirror_lib_name); | |
| 61 } | |
| 62 | |
| 63 | |
| 64 // TODO(11742): Remove once there are no more users of the Dart_Handle-based | |
| 65 // VMReferences. | |
| 66 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { | |
| 67 Isolate* isolate = Isolate::Current(); | |
| 68 DARTSCOPE(isolate); | |
| 69 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); | |
| 70 const MirrorReference& reference = | |
| 71 MirrorReference::Handle(MirrorReference::New(referent)); | |
| 72 return Api::NewHandle(isolate, reference.raw()); | |
| 73 } | |
| 74 | |
| 75 | |
| 76 static RawInstance* CreateParameterMirrorList(const Function& func) { | 47 static RawInstance* CreateParameterMirrorList(const Function& func) { |
| 77 HANDLESCOPE(Isolate::Current()); | 48 HANDLESCOPE(Isolate::Current()); |
| 78 const intptr_t param_cnt = func.num_fixed_parameters() - | 49 const intptr_t param_cnt = func.num_fixed_parameters() - |
| 79 func.NumImplicitParameters() + | 50 func.NumImplicitParameters() + |
| 80 func.NumOptionalParameters(); | 51 func.NumOptionalParameters(); |
| 81 const Array& results = Array::Handle(Array::New(param_cnt)); | 52 const Array& results = Array::Handle(Array::New(param_cnt)); |
| 82 const Array& args = Array::Handle(Array::New(3)); | 53 const Array& args = Array::Handle(Array::New(3)); |
| 83 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); | 54 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); |
| 84 Smi& pos = Smi::Handle(); | 55 Smi& pos = Smi::Handle(); |
| 85 Instance& param = Instance::Handle(); | 56 Instance& param = Instance::Handle(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 static RawInstance* CreateTypedefMirror(const Class& cls, | 101 static RawInstance* CreateTypedefMirror(const Class& cls, |
| 131 const Instance& owner_mirror) { | 102 const Instance& owner_mirror) { |
| 132 const Array& args = Array::Handle(Array::New(3)); | 103 const Array& args = Array::Handle(Array::New(3)); |
| 133 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(cls))); | 104 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(cls))); |
| 134 args.SetAt(1, String::Handle(cls.UserVisibleName())); | 105 args.SetAt(1, String::Handle(cls.UserVisibleName())); |
| 135 args.SetAt(2, owner_mirror); | 106 args.SetAt(2, owner_mirror); |
| 136 return CreateMirror(Symbols::_LocalTypedefMirrorImpl(), args); | 107 return CreateMirror(Symbols::_LocalTypedefMirrorImpl(), args); |
| 137 } | 108 } |
| 138 | 109 |
| 139 | 110 |
| 140 static Dart_Handle CreateTypedefMirrorUsingApi(Dart_Handle cls, | 111 static RawInstance* CreateFunctionTypeMirror(const Class& cls) { |
| 141 Dart_Handle cls_name, | 112 const Array& args = Array::Handle(Array::New(1)); |
| 142 Dart_Handle owner_mirror) { | 113 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(cls))); |
| 143 Isolate* isolate = Isolate::Current(); | 114 return CreateMirror(Symbols::_LocalFunctionTypeMirrorImpl(), args); |
| 144 return Api::NewHandle( | |
| 145 isolate, CreateTypedefMirror( | |
| 146 Api::UnwrapClassHandle(isolate, cls), | |
| 147 Api::UnwrapInstanceHandle(isolate, owner_mirror))); | |
| 148 } | 115 } |
| 149 | 116 |
| 150 | 117 |
| 151 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf, | |
| 152 Dart_Handle intf_name, | |
| 153 Dart_Handle lib_mirror) { | |
| 154 ASSERT(Dart_IsClass(intf)); | |
| 155 if (Dart_ClassIsTypedef(intf)) { | |
| 156 // This class is actually a typedef. Represent it specially in | |
| 157 // reflection. | |
| 158 return CreateTypedefMirrorUsingApi(intf, intf_name, lib_mirror); | |
| 159 } | |
| 160 | |
| 161 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); | |
| 162 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 163 if (Dart_IsError(type)) { | |
| 164 return type; | |
| 165 } | |
| 166 | |
| 167 Dart_Handle args[] = { | |
| 168 CreateMirrorReference(intf), | |
| 169 Dart_Null(), // "name" | |
| 170 }; | |
| 171 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 172 return mirror; | |
| 173 } | |
| 174 | |
| 175 | |
| 176 static RawInstance* CreateMethodMirror(const Function& func, | 118 static RawInstance* CreateMethodMirror(const Function& func, |
| 177 const Instance& owner_mirror) { | 119 const Instance& owner_mirror) { |
| 178 const Array& args = Array::Handle(Array::New(12)); | 120 const Array& args = Array::Handle(Array::New(12)); |
| 179 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); | 121 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); |
| 180 args.SetAt(1, String::Handle(func.UserVisibleName())); | 122 args.SetAt(1, String::Handle(func.UserVisibleName())); |
| 181 args.SetAt(2, owner_mirror); | 123 args.SetAt(2, owner_mirror); |
| 182 args.SetAt(3, func.is_static() ? Bool::True() : Bool::False()); | 124 args.SetAt(3, func.is_static() ? Bool::True() : Bool::False()); |
| 183 args.SetAt(4, func.is_abstract() ? Bool::True() : Bool::False()); | 125 args.SetAt(4, func.is_abstract() ? Bool::True() : Bool::False()); |
| 184 args.SetAt(5, func.IsGetterFunction() ? Bool::True() : Bool::False()); | 126 args.SetAt(5, func.IsGetterFunction() ? Bool::True() : Bool::False()); |
| 185 args.SetAt(6, func.IsSetterFunction() ? Bool::True() : Bool::False()); | 127 args.SetAt(6, func.IsSetterFunction() ? Bool::True() : Bool::False()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 207 args.SetAt(3, Instance::Handle()); // Null for type. | 149 args.SetAt(3, Instance::Handle()); // Null for type. |
| 208 args.SetAt(4, field.is_static() ? Bool::True() : Bool::False()); | 150 args.SetAt(4, field.is_static() ? Bool::True() : Bool::False()); |
| 209 args.SetAt(5, field.is_final() ? Bool::True() : Bool::False()); | 151 args.SetAt(5, field.is_final() ? Bool::True() : Bool::False()); |
| 210 | 152 |
| 211 return CreateMirror(Symbols::_LocalVariableMirrorImpl(), args); | 153 return CreateMirror(Symbols::_LocalVariableMirrorImpl(), args); |
| 212 } | 154 } |
| 213 | 155 |
| 214 | 156 |
| 215 static RawInstance* CreateClassMirror(const Class& cls, | 157 static RawInstance* CreateClassMirror(const Class& cls, |
| 216 const Instance& owner_mirror) { | 158 const Instance& owner_mirror) { |
| 217 Instance& retvalue = Instance::Handle(); | 159 if (cls.IsSignatureClass()) { |
| 218 Dart_EnterScope(); | 160 if (cls.IsCanonicalSignatureClass()) { |
| 219 Isolate* isolate = Isolate::Current(); | 161 // We represent function types as canonical signature classes. |
| 220 Dart_Handle cls_handle = Api::NewHandle(isolate, cls.raw()); | 162 return CreateFunctionTypeMirror(cls); |
| 221 if (Dart_IsError(cls_handle)) { | 163 } else { |
| 222 Dart_PropagateError(cls_handle); | 164 // We represent typedefs as non-canonical signature classes. |
| 165 return CreateTypedefMirror(cls, owner_mirror); |
| 166 } |
| 223 } | 167 } |
| 224 Dart_Handle name_handle = Api::NewHandle(isolate, cls.Name()); | 168 |
| 225 if (Dart_IsError(name_handle)) { | 169 const Array& args = Array::Handle(Array::New(2)); |
| 226 Dart_PropagateError(name_handle); | 170 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(cls))); |
| 227 } | 171 args.SetAt(1, String::Handle(cls.UserVisibleName())); |
| 228 Dart_Handle lib_mirror = Api::NewHandle(isolate, owner_mirror.raw()); | 172 return CreateMirror(Symbols::_LocalClassMirrorImpl(), args); |
| 229 // TODO(11742): At some point the handle calls will be replaced by inlined | |
| 230 // functionality. | |
| 231 Dart_Handle result = CreateClassMirrorUsingApi(cls_handle, | |
| 232 name_handle, | |
| 233 lib_mirror); | |
| 234 if (Dart_IsError(result)) { | |
| 235 Dart_PropagateError(result); | |
| 236 } | |
| 237 retvalue ^= Api::UnwrapHandle(result); | |
| 238 Dart_ExitScope(); | |
| 239 return retvalue.raw(); | |
| 240 } | 173 } |
| 241 | 174 |
| 242 | 175 |
| 243 static RawInstance* CreateLibraryMirror(const Library& lib) { | 176 static RawInstance* CreateLibraryMirror(const Library& lib) { |
| 244 const Array& args = Array::Handle(Array::New(3)); | 177 const Array& args = Array::Handle(Array::New(3)); |
| 245 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(lib))); | 178 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(lib))); |
| 246 String& str = String::Handle(); | 179 String& str = String::Handle(); |
| 247 str = lib.name(); | 180 str = lib.name(); |
| 248 args.SetAt(1, str); | 181 args.SetAt(1, str); |
| 249 str = lib.url(); | 182 str = lib.url(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 args.SetAt(1, isolate_mirror); | 249 args.SetAt(1, isolate_mirror); |
| 317 return CreateMirror(Symbols::_LocalMirrorSystemImpl(), args); | 250 return CreateMirror(Symbols::_LocalMirrorSystemImpl(), args); |
| 318 } | 251 } |
| 319 | 252 |
| 320 | 253 |
| 321 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) { | 254 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) { |
| 322 return CreateMirrorSystem(); | 255 return CreateMirrorSystem(); |
| 323 } | 256 } |
| 324 | 257 |
| 325 | 258 |
| 326 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalClassMirror)( | 259 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) { |
| 327 Dart_NativeArguments args) { | 260 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0)); |
| 328 Dart_EnterScope(); | |
| 329 Isolate* isolate = Isolate::Current(); | |
| 330 Dart_Handle key = Dart_GetNativeArgument(args, 0); | |
| 331 if (Dart_IsError(key)) { | |
| 332 Dart_PropagateError(key); | |
| 333 } | |
| 334 const Type& type = Api::UnwrapTypeHandle(isolate, key); | |
| 335 const Class& cls = Class::Handle(type.type_class()); | 261 const Class& cls = Class::Handle(type.type_class()); |
| 336 Dart_Handle cls_handle = Api::NewHandle(isolate, cls.raw()); | 262 return CreateClassMirror(cls, Object::null_instance()); |
| 337 if (Dart_IsError(cls_handle)) { | |
| 338 Dart_PropagateError(cls_handle); | |
| 339 } | |
| 340 Dart_Handle name_handle = Api::NewHandle(isolate, cls.Name()); | |
| 341 if (Dart_IsError(name_handle)) { | |
| 342 Dart_PropagateError(name_handle); | |
| 343 } | |
| 344 Dart_Handle lib_mirror = Dart_Null(); | |
| 345 Dart_Handle result = CreateClassMirrorUsingApi(cls_handle, | |
| 346 name_handle, | |
| 347 lib_mirror); | |
| 348 if (Dart_IsError(result)) { | |
| 349 Dart_PropagateError(result); | |
| 350 } | |
| 351 Dart_SetReturnValue(args, result); | |
| 352 Dart_ExitScope(); | |
| 353 } | 263 } |
| 354 | 264 |
| 355 | 265 |
| 356 static void ThrowMirroredCompilationError(const String& message) { | 266 static void ThrowMirroredCompilationError(const String& message) { |
| 357 Array& args = Array::Handle(Array::New(1)); | 267 Array& args = Array::Handle(Array::New(1)); |
| 358 args.SetAt(0, message); | 268 args.SetAt(0, message); |
| 359 | 269 |
| 360 Exceptions::ThrowByType(Exceptions::kMirroredCompilationError, args); | 270 Exceptions::ThrowByType(Exceptions::kMirroredCompilationError, args); |
| 361 UNREACHABLE(); | 271 UNREACHABLE(); |
| 362 } | 272 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 320 |
| 411 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) { | 321 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) { |
| 412 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 322 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 413 const Class& cls = Class::Handle(ref.GetClassReferent()); | 323 const Class& cls = Class::Handle(ref.GetClassReferent()); |
| 414 const Function& func = Function::Handle(cls.signature_function()); | 324 const Function& func = Function::Handle(cls.signature_function()); |
| 415 const AbstractType& return_type = AbstractType::Handle(func.result_type()); | 325 const AbstractType& return_type = AbstractType::Handle(func.result_type()); |
| 416 return CreateTypeMirror(return_type); | 326 return CreateTypeMirror(return_type); |
| 417 } | 327 } |
| 418 | 328 |
| 419 | 329 |
| 420 void HandleMirrorsMessage(Isolate* isolate, | |
| 421 Dart_Port reply_port, | |
| 422 const Instance& message) { | |
| 423 UNIMPLEMENTED(); | |
| 424 } | |
| 425 | |
| 426 | |
| 427 static bool FieldIsUninitialized(const Field& field) { | 330 static bool FieldIsUninitialized(const Field& field) { |
| 428 ASSERT(!field.IsNull()); | 331 ASSERT(!field.IsNull()); |
| 429 | 332 |
| 430 // Return getter method for uninitialized fields, rather than the | 333 // Return getter method for uninitialized fields, rather than the |
| 431 // field object, since the value in the field object will not be | 334 // field object, since the value in the field object will not be |
| 432 // initialized until the first time the getter is invoked. | 335 // initialized until the first time the getter is invoked. |
| 433 const Instance& value = Instance::Handle(field.value()); | 336 const Instance& value = Instance::Handle(field.value()); |
| 434 ASSERT(value.raw() != Object::transition_sentinel().raw()); | 337 ASSERT(value.raw() != Object::transition_sentinel().raw()); |
| 435 return value.raw() == Object::sentinel().raw(); | 338 return value.raw() == Object::sentinel().raw(); |
| 436 } | 339 } |
| (...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 | 1174 |
| 1272 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1175 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1273 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1176 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1274 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1177 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1275 | 1178 |
| 1276 const AbstractType& type = AbstractType::Handle(field.type()); | 1179 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1277 return CreateTypeMirror(type); | 1180 return CreateTypeMirror(type); |
| 1278 } | 1181 } |
| 1279 | 1182 |
| 1280 } // namespace dart | 1183 } // namespace dart |
| OLD | NEW |