paul@354 | 1 | /* Native functions for introspection operations. |
paul@354 | 2 | |
paul@354 | 3 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@354 | 4 | |
paul@354 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@354 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@354 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@354 | 8 | version. |
paul@354 | 9 | |
paul@354 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@354 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@354 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@354 | 13 | details. |
paul@354 | 14 | |
paul@354 | 15 | You should have received a copy of the GNU General Public License along with |
paul@354 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@354 | 17 | */ |
paul@354 | 18 | |
paul@354 | 19 | #include "types.h" |
paul@354 | 20 | #include "exceptions.h" |
paul@354 | 21 | #include "ops.h" |
paul@354 | 22 | #include "progconsts.h" |
paul@354 | 23 | #include "progops.h" |
paul@354 | 24 | #include "progtypes.h" |
paul@354 | 25 | #include "main.h" |
paul@354 | 26 | |
paul@354 | 27 | /* Introspection. */ |
paul@354 | 28 | |
paul@354 | 29 | __attr __fn_native_introspection_object_getattr(__attr __args[]) |
paul@354 | 30 | { |
paul@354 | 31 | __attr * const obj = &__args[1]; |
paul@354 | 32 | __attr * const name = &__args[2]; |
paul@354 | 33 | __attr * const _default = &__args[3]; |
paul@360 | 34 | /* name.__data__ interpreted as string */ |
paul@360 | 35 | __attr key = __load_via_object(name->value, __pos___key__); |
paul@487 | 36 | __attr out; |
paul@354 | 37 | |
paul@360 | 38 | if ((key.code == 0) && (key.pos == 0)) |
paul@360 | 39 | return *_default; |
paul@487 | 40 | |
paul@487 | 41 | /* Attempt to get the attribute from the object. */ |
paul@487 | 42 | |
paul@487 | 43 | out = __check_and_load_via_object_null(obj->value, key.pos, key.code); |
paul@487 | 44 | if (out.value == 0) |
paul@487 | 45 | { |
paul@487 | 46 | /* Inspect the object's class if this failed. */ |
paul@487 | 47 | |
paul@487 | 48 | out = __check_and_load_via_class(obj->value, key.pos, key.code); |
paul@487 | 49 | if (out.value == 0) |
paul@487 | 50 | return *_default; |
paul@487 | 51 | |
paul@487 | 52 | /* Update the context to the object if it is a method. */ |
paul@487 | 53 | |
paul@487 | 54 | return __replace_context(obj->value, out); |
paul@487 | 55 | } |
paul@487 | 56 | |
paul@487 | 57 | return out; |
paul@354 | 58 | } |
paul@354 | 59 | |
paul@354 | 60 | static int __issubclass(__ref obj, __attr cls) |
paul@354 | 61 | { |
paul@354 | 62 | return (__HASATTR(obj, __TYPEPOS(cls.value), __TYPECODE(cls.value))); |
paul@354 | 63 | } |
paul@354 | 64 | |
paul@354 | 65 | __attr __fn_native_introspection_isinstance(__attr __args[]) |
paul@354 | 66 | { |
paul@354 | 67 | __attr * const obj = &__args[1]; |
paul@354 | 68 | __attr * const cls = &__args[2]; |
paul@354 | 69 | |
paul@354 | 70 | /* cls must be a class. */ |
paul@354 | 71 | if (__is_instance(obj->value) && __issubclass(__get_class(obj->value), *cls)) |
paul@354 | 72 | return __builtins___boolean_True; |
paul@354 | 73 | else |
paul@354 | 74 | return __builtins___boolean_False; |
paul@354 | 75 | } |
paul@354 | 76 | |
paul@354 | 77 | __attr __fn_native_introspection_issubclass(__attr __args[]) |
paul@354 | 78 | { |
paul@354 | 79 | __attr * const obj = &__args[1]; |
paul@354 | 80 | __attr * const cls = &__args[2]; |
paul@354 | 81 | |
paul@354 | 82 | /* obj and cls must be classes. */ |
paul@354 | 83 | if (__issubclass(obj->value, *cls)) |
paul@354 | 84 | return __builtins___boolean_True; |
paul@354 | 85 | else |
paul@354 | 86 | return __builtins___boolean_False; |
paul@354 | 87 | } |
paul@354 | 88 | |
paul@354 | 89 | /* Module initialisation. */ |
paul@354 | 90 | |
paul@354 | 91 | void __main_native_introspection() |
paul@354 | 92 | { |
paul@354 | 93 | } |