OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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_mirrors_api.h" | 5 #include "include/dart_mirrors_api.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/dart.h" | 9 #include "vm/dart.h" |
10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #define Z (T->zone()) | 23 #define Z (T->zone()) |
24 | 24 |
25 | 25 |
26 // --- Classes and Interfaces Reflection --- | 26 // --- Classes and Interfaces Reflection --- |
27 | 27 |
28 DART_EXPORT Dart_Handle Dart_TypeName(Dart_Handle object) { | 28 DART_EXPORT Dart_Handle Dart_TypeName(Dart_Handle object) { |
29 DARTSCOPE(Thread::Current()); | 29 DARTSCOPE(Thread::Current()); |
30 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); | 30 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); |
31 if (obj.IsType()) { | 31 if (obj.IsType()) { |
32 const Class& cls = Class::Handle(Type::Cast(obj).type_class()); | 32 const Class& cls = Class::Handle(Type::Cast(obj).type_class()); |
33 return Api::NewHandle(I, cls.UserVisibleName()); | 33 return Api::NewHandle(T, cls.UserVisibleName()); |
34 } else { | 34 } else { |
35 RETURN_TYPE_ERROR(Z, object, Class/Type); | 35 RETURN_TYPE_ERROR(Z, object, Class/Type); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 | 39 |
40 DART_EXPORT Dart_Handle Dart_QualifiedTypeName(Dart_Handle object) { | 40 DART_EXPORT Dart_Handle Dart_QualifiedTypeName(Dart_Handle object) { |
41 DARTSCOPE(Thread::Current()); | 41 DARTSCOPE(Thread::Current()); |
42 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); | 42 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); |
43 if (obj.IsType() || obj.IsClass()) { | 43 if (obj.IsType() || obj.IsClass()) { |
44 const Class& cls = (obj.IsType()) ? | 44 const Class& cls = (obj.IsType()) ? |
45 Class::Handle(Z, Type::Cast(obj).type_class()) : Class::Cast(obj); | 45 Class::Handle(Z, Type::Cast(obj).type_class()) : Class::Cast(obj); |
46 const char* str = cls.ToCString(); | 46 const char* str = cls.ToCString(); |
47 if (str == NULL) { | 47 if (str == NULL) { |
48 RETURN_NULL_ERROR(str); | 48 RETURN_NULL_ERROR(str); |
49 } | 49 } |
50 CHECK_CALLBACK_STATE(T); | 50 CHECK_CALLBACK_STATE(T); |
51 return Api::NewHandle(I, String::New(str)); | 51 return Api::NewHandle(T, String::New(str)); |
52 } else { | 52 } else { |
53 RETURN_TYPE_ERROR(Z, object, Class/Type); | 53 RETURN_TYPE_ERROR(Z, object, Class/Type); |
54 } | 54 } |
55 } | 55 } |
56 | 56 |
57 | 57 |
58 // --- Function and Variable Reflection --- | 58 // --- Function and Variable Reflection --- |
59 | 59 |
60 // Outside of the vm, we expose setter names with a trailing '='. | 60 // Outside of the vm, we expose setter names with a trailing '='. |
61 static bool HasExternalSetterSuffix(const String& name) { | 61 static bool HasExternalSetterSuffix(const String& name) { |
(...skipping 16 matching lines...) Expand all Loading... |
78 | 78 |
79 const GrowableObjectArray& names = | 79 const GrowableObjectArray& names = |
80 GrowableObjectArray::Handle(Z, GrowableObjectArray::New()); | 80 GrowableObjectArray::Handle(Z, GrowableObjectArray::New()); |
81 Function& func = Function::Handle(Z); | 81 Function& func = Function::Handle(Z); |
82 String& name = String::Handle(Z); | 82 String& name = String::Handle(Z); |
83 | 83 |
84 if (obj.IsType()) { | 84 if (obj.IsType()) { |
85 const Class& cls = Class::Handle(Z, Type::Cast(obj).type_class()); | 85 const Class& cls = Class::Handle(Z, Type::Cast(obj).type_class()); |
86 const Error& error = Error::Handle(Z, cls.EnsureIsFinalized(T)); | 86 const Error& error = Error::Handle(Z, cls.EnsureIsFinalized(T)); |
87 if (!error.IsNull()) { | 87 if (!error.IsNull()) { |
88 return Api::NewHandle(I, error.raw()); | 88 return Api::NewHandle(T, error.raw()); |
89 } | 89 } |
90 const Array& func_array = Array::Handle(Z, cls.functions()); | 90 const Array& func_array = Array::Handle(Z, cls.functions()); |
91 | 91 |
92 // Some special types like 'dynamic' have a null functions list. | 92 // Some special types like 'dynamic' have a null functions list. |
93 if (!func_array.IsNull()) { | 93 if (!func_array.IsNull()) { |
94 for (intptr_t i = 0; i < func_array.Length(); ++i) { | 94 for (intptr_t i = 0; i < func_array.Length(); ++i) { |
95 func ^= func_array.At(i); | 95 func ^= func_array.At(i); |
96 | 96 |
97 // Skip implicit getters and setters. | 97 // Skip implicit getters and setters. |
98 if (func.kind() == RawFunction::kImplicitGetter || | 98 if (func.kind() == RawFunction::kImplicitGetter || |
(...skipping 18 matching lines...) Expand all Loading... |
117 func ^= obj.raw(); | 117 func ^= obj.raw(); |
118 name = func.UserVisibleName(); | 118 name = func.UserVisibleName(); |
119 names.Add(name); | 119 names.Add(name); |
120 } | 120 } |
121 } | 121 } |
122 } else { | 122 } else { |
123 return Api::NewError( | 123 return Api::NewError( |
124 "%s expects argument 'target' to be a class or library.", | 124 "%s expects argument 'target' to be a class or library.", |
125 CURRENT_FUNC); | 125 CURRENT_FUNC); |
126 } | 126 } |
127 return Api::NewHandle(I, Array::MakeArray(names)); | 127 return Api::NewHandle(T, Array::MakeArray(names)); |
128 } | 128 } |
129 | 129 |
130 | 130 |
131 DART_EXPORT Dart_Handle Dart_LookupFunction(Dart_Handle target, | 131 DART_EXPORT Dart_Handle Dart_LookupFunction(Dart_Handle target, |
132 Dart_Handle function_name) { | 132 Dart_Handle function_name) { |
133 DARTSCOPE(Thread::Current()); | 133 DARTSCOPE(Thread::Current()); |
134 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(target)); | 134 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(target)); |
135 if (obj.IsError()) { | 135 if (obj.IsError()) { |
136 return target; | 136 return target; |
137 } | 137 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 #if defined(DEBUG) | 198 #if defined(DEBUG) |
199 if (!func.IsNull()) { | 199 if (!func.IsNull()) { |
200 // We only provide access to a subset of function kinds. | 200 // We only provide access to a subset of function kinds. |
201 RawFunction::Kind func_kind = func.kind(); | 201 RawFunction::Kind func_kind = func.kind(); |
202 ASSERT(func_kind == RawFunction::kRegularFunction || | 202 ASSERT(func_kind == RawFunction::kRegularFunction || |
203 func_kind == RawFunction::kGetterFunction || | 203 func_kind == RawFunction::kGetterFunction || |
204 func_kind == RawFunction::kSetterFunction || | 204 func_kind == RawFunction::kSetterFunction || |
205 func_kind == RawFunction::kConstructor); | 205 func_kind == RawFunction::kConstructor); |
206 } | 206 } |
207 #endif | 207 #endif |
208 return Api::NewHandle(I, func.raw()); | 208 return Api::NewHandle(T, func.raw()); |
209 } | 209 } |
210 | 210 |
211 | 211 |
212 DART_EXPORT Dart_Handle Dart_FunctionName(Dart_Handle function) { | 212 DART_EXPORT Dart_Handle Dart_FunctionName(Dart_Handle function) { |
213 DARTSCOPE(Thread::Current()); | 213 DARTSCOPE(Thread::Current()); |
214 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 214 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
215 if (func.IsNull()) { | 215 if (func.IsNull()) { |
216 RETURN_TYPE_ERROR(Z, function, Function); | 216 RETURN_TYPE_ERROR(Z, function, Function); |
217 } | 217 } |
218 return Api::NewHandle(I, func.UserVisibleName()); | 218 return Api::NewHandle(T, func.UserVisibleName()); |
219 } | 219 } |
220 | 220 |
221 | 221 |
222 DART_EXPORT Dart_Handle Dart_FunctionOwner(Dart_Handle function) { | 222 DART_EXPORT Dart_Handle Dart_FunctionOwner(Dart_Handle function) { |
223 DARTSCOPE(Thread::Current()); | 223 DARTSCOPE(Thread::Current()); |
224 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 224 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
225 if (func.IsNull()) { | 225 if (func.IsNull()) { |
226 RETURN_TYPE_ERROR(Z, function, Function); | 226 RETURN_TYPE_ERROR(Z, function, Function); |
227 } | 227 } |
228 if (func.IsNonImplicitClosureFunction()) { | 228 if (func.IsNonImplicitClosureFunction()) { |
229 RawFunction* parent_function = func.parent_function(); | 229 RawFunction* parent_function = func.parent_function(); |
230 return Api::NewHandle(I, parent_function); | 230 return Api::NewHandle(T, parent_function); |
231 } | 231 } |
232 const Class& owner = Class::Handle(Z, func.Owner()); | 232 const Class& owner = Class::Handle(Z, func.Owner()); |
233 ASSERT(!owner.IsNull()); | 233 ASSERT(!owner.IsNull()); |
234 if (owner.IsTopLevel()) { | 234 if (owner.IsTopLevel()) { |
235 // Top-level functions are implemented as members of a hidden class. We hide | 235 // Top-level functions are implemented as members of a hidden class. We hide |
236 // that class here and instead answer the library. | 236 // that class here and instead answer the library. |
237 #if defined(DEBUG) | 237 #if defined(DEBUG) |
238 const Library& lib = Library::Handle(Z, owner.library()); | 238 const Library& lib = Library::Handle(Z, owner.library()); |
239 if (lib.IsNull()) { | 239 if (lib.IsNull()) { |
240 ASSERT(owner.IsDynamicClass() || owner.IsVoidClass()); | 240 ASSERT(owner.IsDynamicClass() || owner.IsVoidClass()); |
241 } | 241 } |
242 #endif | 242 #endif |
243 return Api::NewHandle(I, owner.library()); | 243 return Api::NewHandle(T, owner.library()); |
244 } else { | 244 } else { |
245 return Api::NewHandle(I, owner.RareType()); | 245 return Api::NewHandle(T, owner.RareType()); |
246 } | 246 } |
247 } | 247 } |
248 | 248 |
249 | 249 |
250 DART_EXPORT Dart_Handle Dart_FunctionIsStatic(Dart_Handle function, | 250 DART_EXPORT Dart_Handle Dart_FunctionIsStatic(Dart_Handle function, |
251 bool* is_static) { | 251 bool* is_static) { |
252 DARTSCOPE(Thread::Current()); | 252 DARTSCOPE(Thread::Current()); |
253 if (is_static == NULL) { | 253 if (is_static == NULL) { |
254 RETURN_NULL_ERROR(is_static); | 254 RETURN_NULL_ERROR(is_static); |
255 } | 255 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // --- Libraries Reflection --- | 310 // --- Libraries Reflection --- |
311 | 311 |
312 DART_EXPORT Dart_Handle Dart_LibraryName(Dart_Handle library) { | 312 DART_EXPORT Dart_Handle Dart_LibraryName(Dart_Handle library) { |
313 DARTSCOPE(Thread::Current()); | 313 DARTSCOPE(Thread::Current()); |
314 const Library& lib = Api::UnwrapLibraryHandle(Z, library); | 314 const Library& lib = Api::UnwrapLibraryHandle(Z, library); |
315 if (lib.IsNull()) { | 315 if (lib.IsNull()) { |
316 RETURN_TYPE_ERROR(Z, library, Library); | 316 RETURN_TYPE_ERROR(Z, library, Library); |
317 } | 317 } |
318 const String& name = String::Handle(Z, lib.name()); | 318 const String& name = String::Handle(Z, lib.name()); |
319 ASSERT(!name.IsNull()); | 319 ASSERT(!name.IsNull()); |
320 return Api::NewHandle(I, name.raw()); | 320 return Api::NewHandle(T, name.raw()); |
321 } | 321 } |
322 | 322 |
323 DART_EXPORT Dart_Handle Dart_LibraryGetClassNames(Dart_Handle library) { | 323 DART_EXPORT Dart_Handle Dart_LibraryGetClassNames(Dart_Handle library) { |
324 DARTSCOPE(Thread::Current()); | 324 DARTSCOPE(Thread::Current()); |
325 const Library& lib = Api::UnwrapLibraryHandle(Z, library); | 325 const Library& lib = Api::UnwrapLibraryHandle(Z, library); |
326 if (lib.IsNull()) { | 326 if (lib.IsNull()) { |
327 RETURN_TYPE_ERROR(Z, library, Library); | 327 RETURN_TYPE_ERROR(Z, library, Library); |
328 } | 328 } |
329 | 329 |
330 const GrowableObjectArray& names = | 330 const GrowableObjectArray& names = |
331 GrowableObjectArray::Handle(Z, GrowableObjectArray::New()); | 331 GrowableObjectArray::Handle(Z, GrowableObjectArray::New()); |
332 ClassDictionaryIterator it(lib); | 332 ClassDictionaryIterator it(lib); |
333 Class& cls = Class::Handle(Z); | 333 Class& cls = Class::Handle(Z); |
334 String& name = String::Handle(Z); | 334 String& name = String::Handle(Z); |
335 while (it.HasNext()) { | 335 while (it.HasNext()) { |
336 cls = it.GetNextClass(); | 336 cls = it.GetNextClass(); |
337 if (cls.IsSignatureClass()) { | 337 if (cls.IsSignatureClass()) { |
338 if (!cls.IsCanonicalSignatureClass()) { | 338 if (!cls.IsCanonicalSignatureClass()) { |
339 // This is a typedef. Add it to the list of class names. | 339 // This is a typedef. Add it to the list of class names. |
340 name = cls.UserVisibleName(); | 340 name = cls.UserVisibleName(); |
341 names.Add(name); | 341 names.Add(name); |
342 } else { | 342 } else { |
343 // Skip canonical signature classes. These are not named. | 343 // Skip canonical signature classes. These are not named. |
344 } | 344 } |
345 } else { | 345 } else { |
346 name = cls.UserVisibleName(); | 346 name = cls.UserVisibleName(); |
347 names.Add(name); | 347 names.Add(name); |
348 } | 348 } |
349 } | 349 } |
350 return Api::NewHandle(I, Array::MakeArray(names)); | 350 return Api::NewHandle(T, Array::MakeArray(names)); |
351 } | 351 } |
352 | 352 |
353 | 353 |
354 // --- Closures Reflection --- | 354 // --- Closures Reflection --- |
355 | 355 |
356 DART_EXPORT Dart_Handle Dart_ClosureFunction(Dart_Handle closure) { | 356 DART_EXPORT Dart_Handle Dart_ClosureFunction(Dart_Handle closure) { |
357 DARTSCOPE(Thread::Current()); | 357 DARTSCOPE(Thread::Current()); |
358 const Instance& closure_obj = Api::UnwrapInstanceHandle(Z, closure); | 358 const Instance& closure_obj = Api::UnwrapInstanceHandle(Z, closure); |
359 if (closure_obj.IsNull() || !closure_obj.IsClosure()) { | 359 if (closure_obj.IsNull() || !closure_obj.IsClosure()) { |
360 RETURN_TYPE_ERROR(Z, closure, Instance); | 360 RETURN_TYPE_ERROR(Z, closure, Instance); |
361 } | 361 } |
362 | 362 |
363 ASSERT(ClassFinalizer::AllClassesFinalized()); | 363 ASSERT(ClassFinalizer::AllClassesFinalized()); |
364 | 364 |
365 RawFunction* rf = Closure::function(closure_obj); | 365 RawFunction* rf = Closure::function(closure_obj); |
366 return Api::NewHandle(I, rf); | 366 return Api::NewHandle(T, rf); |
367 } | 367 } |
368 | 368 |
369 } // namespace dart | 369 } // namespace dart |
OLD | NEW |