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" |
11 #include "vm/dart_api_state.h" | 11 #include "vm/dart_api_state.h" |
12 #include "vm/dart_entry.h" | 12 #include "vm/dart_entry.h" |
13 #include "vm/exceptions.h" | 13 #include "vm/exceptions.h" |
14 #include "vm/growable_array.h" | 14 #include "vm/growable_array.h" |
15 #include "vm/object.h" | 15 #include "vm/object.h" |
16 #include "vm/resolver.h" | 16 #include "vm/resolver.h" |
17 #include "vm/stack_frame.h" | 17 #include "vm/stack_frame.h" |
18 #include "vm/symbols.h" | 18 #include "vm/symbols.h" |
19 | 19 |
20 namespace dart { | 20 namespace dart { |
21 | 21 |
22 // Facilitate quick access to the current zone once we have the current thread. | 22 // Facilitate quick access to the current zone once we have the current thread. |
23 #define Z (T->zone()) | 23 #define Z (T->zone()) |
24 | 24 |
25 | |
26 // --- Classes and Interfaces Reflection --- | 25 // --- Classes and Interfaces Reflection --- |
27 | 26 |
28 DART_EXPORT Dart_Handle Dart_TypeName(Dart_Handle object) { | 27 DART_EXPORT Dart_Handle Dart_TypeName(Dart_Handle object) { |
29 DARTSCOPE(Thread::Current()); | 28 DARTSCOPE(Thread::Current()); |
30 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); | 29 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); |
31 if (obj.IsType()) { | 30 if (obj.IsType()) { |
32 const Class& cls = Class::Handle(Type::Cast(obj).type_class()); | 31 const Class& cls = Class::Handle(Type::Cast(obj).type_class()); |
33 return Api::NewHandle(T, cls.UserVisibleName()); | 32 return Api::NewHandle(T, cls.UserVisibleName()); |
34 } else { | 33 } else { |
35 RETURN_TYPE_ERROR(Z, object, Class / Type); | 34 RETURN_TYPE_ERROR(Z, object, Class / Type); |
36 } | 35 } |
37 } | 36 } |
38 | 37 |
39 | |
40 DART_EXPORT Dart_Handle Dart_QualifiedTypeName(Dart_Handle object) { | 38 DART_EXPORT Dart_Handle Dart_QualifiedTypeName(Dart_Handle object) { |
41 DARTSCOPE(Thread::Current()); | 39 DARTSCOPE(Thread::Current()); |
42 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); | 40 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(object)); |
43 if (obj.IsType() || obj.IsClass()) { | 41 if (obj.IsType() || obj.IsClass()) { |
44 const Class& cls = (obj.IsType()) | 42 const Class& cls = (obj.IsType()) |
45 ? Class::Handle(Z, Type::Cast(obj).type_class()) | 43 ? Class::Handle(Z, Type::Cast(obj).type_class()) |
46 : Class::Cast(obj); | 44 : Class::Cast(obj); |
47 const char* str = cls.ToCString(); | 45 const char* str = cls.ToCString(); |
48 if (str == NULL) { | 46 if (str == NULL) { |
49 RETURN_NULL_ERROR(str); | 47 RETURN_NULL_ERROR(str); |
50 } | 48 } |
51 CHECK_CALLBACK_STATE(T); | 49 CHECK_CALLBACK_STATE(T); |
52 return Api::NewHandle(T, String::New(str)); | 50 return Api::NewHandle(T, String::New(str)); |
53 } else { | 51 } else { |
54 RETURN_TYPE_ERROR(Z, object, Class / Type); | 52 RETURN_TYPE_ERROR(Z, object, Class / Type); |
55 } | 53 } |
56 } | 54 } |
57 | 55 |
58 | |
59 // --- Function and Variable Reflection --- | 56 // --- Function and Variable Reflection --- |
60 | 57 |
61 // Outside of the vm, we expose setter names with a trailing '='. | 58 // Outside of the vm, we expose setter names with a trailing '='. |
62 static bool HasExternalSetterSuffix(const String& name) { | 59 static bool HasExternalSetterSuffix(const String& name) { |
63 return name.CharAt(name.Length() - 1) == '='; | 60 return name.CharAt(name.Length() - 1) == '='; |
64 } | 61 } |
65 | 62 |
66 | |
67 static RawString* RemoveExternalSetterSuffix(const String& name) { | 63 static RawString* RemoveExternalSetterSuffix(const String& name) { |
68 ASSERT(HasExternalSetterSuffix(name)); | 64 ASSERT(HasExternalSetterSuffix(name)); |
69 return String::SubString(name, 0, name.Length() - 1); | 65 return String::SubString(name, 0, name.Length() - 1); |
70 } | 66 } |
71 | 67 |
72 | |
73 DART_EXPORT Dart_Handle Dart_GetFunctionNames(Dart_Handle target) { | 68 DART_EXPORT Dart_Handle Dart_GetFunctionNames(Dart_Handle target) { |
74 DARTSCOPE(Thread::Current()); | 69 DARTSCOPE(Thread::Current()); |
75 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(target)); | 70 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(target)); |
76 if (obj.IsError()) { | 71 if (obj.IsError()) { |
77 return target; | 72 return target; |
78 } | 73 } |
79 | 74 |
80 const GrowableObjectArray& names = | 75 const GrowableObjectArray& names = |
81 GrowableObjectArray::Handle(Z, GrowableObjectArray::New()); | 76 GrowableObjectArray::Handle(Z, GrowableObjectArray::New()); |
82 Function& func = Function::Handle(Z); | 77 Function& func = Function::Handle(Z); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 names.Add(name); | 115 names.Add(name); |
121 } | 116 } |
122 } | 117 } |
123 } else { | 118 } else { |
124 return Api::NewError( | 119 return Api::NewError( |
125 "%s expects argument 'target' to be a class or library.", CURRENT_FUNC); | 120 "%s expects argument 'target' to be a class or library.", CURRENT_FUNC); |
126 } | 121 } |
127 return Api::NewHandle(T, Array::MakeFixedLength(names)); | 122 return Api::NewHandle(T, Array::MakeFixedLength(names)); |
128 } | 123 } |
129 | 124 |
130 | |
131 DART_EXPORT Dart_Handle Dart_LookupFunction(Dart_Handle target, | 125 DART_EXPORT Dart_Handle Dart_LookupFunction(Dart_Handle target, |
132 Dart_Handle function_name) { | 126 Dart_Handle function_name) { |
133 DARTSCOPE(Thread::Current()); | 127 DARTSCOPE(Thread::Current()); |
134 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(target)); | 128 const Object& obj = Object::Handle(Z, Api::UnwrapHandle(target)); |
135 if (obj.IsError()) { | 129 if (obj.IsError()) { |
136 return target; | 130 return target; |
137 } | 131 } |
138 const String& func_name = Api::UnwrapStringHandle(Z, function_name); | 132 const String& func_name = Api::UnwrapStringHandle(Z, function_name); |
139 if (func_name.IsNull()) { | 133 if (func_name.IsNull()) { |
140 RETURN_TYPE_ERROR(Z, function_name, String); | 134 RETURN_TYPE_ERROR(Z, function_name, String); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 RawFunction::Kind func_kind = func.kind(); | 194 RawFunction::Kind func_kind = func.kind(); |
201 ASSERT(func_kind == RawFunction::kRegularFunction || | 195 ASSERT(func_kind == RawFunction::kRegularFunction || |
202 func_kind == RawFunction::kGetterFunction || | 196 func_kind == RawFunction::kGetterFunction || |
203 func_kind == RawFunction::kSetterFunction || | 197 func_kind == RawFunction::kSetterFunction || |
204 func_kind == RawFunction::kConstructor); | 198 func_kind == RawFunction::kConstructor); |
205 } | 199 } |
206 #endif | 200 #endif |
207 return Api::NewHandle(T, func.raw()); | 201 return Api::NewHandle(T, func.raw()); |
208 } | 202 } |
209 | 203 |
210 | |
211 DART_EXPORT Dart_Handle Dart_FunctionName(Dart_Handle function) { | 204 DART_EXPORT Dart_Handle Dart_FunctionName(Dart_Handle function) { |
212 DARTSCOPE(Thread::Current()); | 205 DARTSCOPE(Thread::Current()); |
213 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 206 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
214 if (func.IsNull()) { | 207 if (func.IsNull()) { |
215 RETURN_TYPE_ERROR(Z, function, Function); | 208 RETURN_TYPE_ERROR(Z, function, Function); |
216 } | 209 } |
217 return Api::NewHandle(T, func.UserVisibleName()); | 210 return Api::NewHandle(T, func.UserVisibleName()); |
218 } | 211 } |
219 | 212 |
220 | |
221 DART_EXPORT Dart_Handle Dart_FunctionOwner(Dart_Handle function) { | 213 DART_EXPORT Dart_Handle Dart_FunctionOwner(Dart_Handle function) { |
222 DARTSCOPE(Thread::Current()); | 214 DARTSCOPE(Thread::Current()); |
223 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 215 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
224 if (func.IsNull()) { | 216 if (func.IsNull()) { |
225 RETURN_TYPE_ERROR(Z, function, Function); | 217 RETURN_TYPE_ERROR(Z, function, Function); |
226 } | 218 } |
227 if (func.IsNonImplicitClosureFunction()) { | 219 if (func.IsNonImplicitClosureFunction()) { |
228 RawFunction* parent_function = func.parent_function(); | 220 RawFunction* parent_function = func.parent_function(); |
229 return Api::NewHandle(T, parent_function); | 221 return Api::NewHandle(T, parent_function); |
230 } | 222 } |
231 const Class& owner = Class::Handle(Z, func.Owner()); | 223 const Class& owner = Class::Handle(Z, func.Owner()); |
232 ASSERT(!owner.IsNull()); | 224 ASSERT(!owner.IsNull()); |
233 if (owner.IsTopLevel()) { | 225 if (owner.IsTopLevel()) { |
234 // Top-level functions are implemented as members of a hidden class. We hide | 226 // Top-level functions are implemented as members of a hidden class. We hide |
235 // that class here and instead answer the library. | 227 // that class here and instead answer the library. |
236 #if defined(DEBUG) | 228 #if defined(DEBUG) |
237 const Library& lib = Library::Handle(Z, owner.library()); | 229 const Library& lib = Library::Handle(Z, owner.library()); |
238 if (lib.IsNull()) { | 230 if (lib.IsNull()) { |
239 ASSERT(owner.IsDynamicClass() || owner.IsVoidClass()); | 231 ASSERT(owner.IsDynamicClass() || owner.IsVoidClass()); |
240 } | 232 } |
241 #endif | 233 #endif |
242 return Api::NewHandle(T, owner.library()); | 234 return Api::NewHandle(T, owner.library()); |
243 } else { | 235 } else { |
244 return Api::NewHandle(T, owner.RareType()); | 236 return Api::NewHandle(T, owner.RareType()); |
245 } | 237 } |
246 } | 238 } |
247 | 239 |
248 | |
249 DART_EXPORT Dart_Handle Dart_FunctionIsStatic(Dart_Handle function, | 240 DART_EXPORT Dart_Handle Dart_FunctionIsStatic(Dart_Handle function, |
250 bool* is_static) { | 241 bool* is_static) { |
251 DARTSCOPE(Thread::Current()); | 242 DARTSCOPE(Thread::Current()); |
252 if (is_static == NULL) { | 243 if (is_static == NULL) { |
253 RETURN_NULL_ERROR(is_static); | 244 RETURN_NULL_ERROR(is_static); |
254 } | 245 } |
255 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 246 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
256 if (func.IsNull()) { | 247 if (func.IsNull()) { |
257 RETURN_TYPE_ERROR(Z, function, Function); | 248 RETURN_TYPE_ERROR(Z, function, Function); |
258 } | 249 } |
259 *is_static = func.is_static(); | 250 *is_static = func.is_static(); |
260 return Api::Success(); | 251 return Api::Success(); |
261 } | 252 } |
262 | 253 |
263 | |
264 DART_EXPORT Dart_Handle Dart_FunctionIsConstructor(Dart_Handle function, | 254 DART_EXPORT Dart_Handle Dart_FunctionIsConstructor(Dart_Handle function, |
265 bool* is_constructor) { | 255 bool* is_constructor) { |
266 DARTSCOPE(Thread::Current()); | 256 DARTSCOPE(Thread::Current()); |
267 if (is_constructor == NULL) { | 257 if (is_constructor == NULL) { |
268 RETURN_NULL_ERROR(is_constructor); | 258 RETURN_NULL_ERROR(is_constructor); |
269 } | 259 } |
270 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 260 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
271 if (func.IsNull()) { | 261 if (func.IsNull()) { |
272 RETURN_TYPE_ERROR(Z, function, Function); | 262 RETURN_TYPE_ERROR(Z, function, Function); |
273 } | 263 } |
274 *is_constructor = func.kind() == RawFunction::kConstructor; | 264 *is_constructor = func.kind() == RawFunction::kConstructor; |
275 return Api::Success(); | 265 return Api::Success(); |
276 } | 266 } |
277 | 267 |
278 | |
279 DART_EXPORT Dart_Handle Dart_FunctionIsGetter(Dart_Handle function, | 268 DART_EXPORT Dart_Handle Dart_FunctionIsGetter(Dart_Handle function, |
280 bool* is_getter) { | 269 bool* is_getter) { |
281 DARTSCOPE(Thread::Current()); | 270 DARTSCOPE(Thread::Current()); |
282 if (is_getter == NULL) { | 271 if (is_getter == NULL) { |
283 RETURN_NULL_ERROR(is_getter); | 272 RETURN_NULL_ERROR(is_getter); |
284 } | 273 } |
285 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 274 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
286 if (func.IsNull()) { | 275 if (func.IsNull()) { |
287 RETURN_TYPE_ERROR(Z, function, Function); | 276 RETURN_TYPE_ERROR(Z, function, Function); |
288 } | 277 } |
289 *is_getter = func.IsGetterFunction(); | 278 *is_getter = func.IsGetterFunction(); |
290 return Api::Success(); | 279 return Api::Success(); |
291 } | 280 } |
292 | 281 |
293 | |
294 DART_EXPORT Dart_Handle Dart_FunctionIsSetter(Dart_Handle function, | 282 DART_EXPORT Dart_Handle Dart_FunctionIsSetter(Dart_Handle function, |
295 bool* is_setter) { | 283 bool* is_setter) { |
296 DARTSCOPE(Thread::Current()); | 284 DARTSCOPE(Thread::Current()); |
297 if (is_setter == NULL) { | 285 if (is_setter == NULL) { |
298 RETURN_NULL_ERROR(is_setter); | 286 RETURN_NULL_ERROR(is_setter); |
299 } | 287 } |
300 const Function& func = Api::UnwrapFunctionHandle(Z, function); | 288 const Function& func = Api::UnwrapFunctionHandle(Z, function); |
301 if (func.IsNull()) { | 289 if (func.IsNull()) { |
302 RETURN_TYPE_ERROR(Z, function, Function); | 290 RETURN_TYPE_ERROR(Z, function, Function); |
303 } | 291 } |
304 *is_setter = (func.kind() == RawFunction::kSetterFunction); | 292 *is_setter = (func.kind() == RawFunction::kSetterFunction); |
305 return Api::Success(); | 293 return Api::Success(); |
306 } | 294 } |
307 | 295 |
308 | |
309 // --- Libraries Reflection --- | 296 // --- Libraries Reflection --- |
310 | 297 |
311 DART_EXPORT Dart_Handle Dart_LibraryName(Dart_Handle library) { | 298 DART_EXPORT Dart_Handle Dart_LibraryName(Dart_Handle library) { |
312 DARTSCOPE(Thread::Current()); | 299 DARTSCOPE(Thread::Current()); |
313 const Library& lib = Api::UnwrapLibraryHandle(Z, library); | 300 const Library& lib = Api::UnwrapLibraryHandle(Z, library); |
314 if (lib.IsNull()) { | 301 if (lib.IsNull()) { |
315 RETURN_TYPE_ERROR(Z, library, Library); | 302 RETURN_TYPE_ERROR(Z, library, Library); |
316 } | 303 } |
317 const String& name = String::Handle(Z, lib.name()); | 304 const String& name = String::Handle(Z, lib.name()); |
318 ASSERT(!name.IsNull()); | 305 ASSERT(!name.IsNull()); |
(...skipping 13 matching lines...) Expand all Loading... |
332 Class& cls = Class::Handle(Z); | 319 Class& cls = Class::Handle(Z); |
333 String& name = String::Handle(Z); | 320 String& name = String::Handle(Z); |
334 while (it.HasNext()) { | 321 while (it.HasNext()) { |
335 cls = it.GetNextClass(); | 322 cls = it.GetNextClass(); |
336 name = cls.UserVisibleName(); | 323 name = cls.UserVisibleName(); |
337 names.Add(name); | 324 names.Add(name); |
338 } | 325 } |
339 return Api::NewHandle(T, Array::MakeFixedLength(names)); | 326 return Api::NewHandle(T, Array::MakeFixedLength(names)); |
340 } | 327 } |
341 | 328 |
342 | |
343 // --- Closures Reflection --- | 329 // --- Closures Reflection --- |
344 | 330 |
345 DART_EXPORT Dart_Handle Dart_ClosureFunction(Dart_Handle closure) { | 331 DART_EXPORT Dart_Handle Dart_ClosureFunction(Dart_Handle closure) { |
346 DARTSCOPE(Thread::Current()); | 332 DARTSCOPE(Thread::Current()); |
347 const Instance& closure_obj = Api::UnwrapInstanceHandle(Z, closure); | 333 const Instance& closure_obj = Api::UnwrapInstanceHandle(Z, closure); |
348 if (closure_obj.IsNull() || !closure_obj.IsClosure()) { | 334 if (closure_obj.IsNull() || !closure_obj.IsClosure()) { |
349 RETURN_TYPE_ERROR(Z, closure, Instance); | 335 RETURN_TYPE_ERROR(Z, closure, Instance); |
350 } | 336 } |
351 | 337 |
352 ASSERT(ClassFinalizer::AllClassesFinalized()); | 338 ASSERT(ClassFinalizer::AllClassesFinalized()); |
353 | 339 |
354 RawFunction* rf = Closure::Cast(closure_obj).function(); | 340 RawFunction* rf = Closure::Cast(closure_obj).function(); |
355 return Api::NewHandle(T, rf); | 341 return Api::NewHandle(T, rf); |
356 } | 342 } |
357 | 343 |
358 } // namespace dart | 344 } // namespace dart |
OLD | NEW |