paul@354 | 1 | /* Native functions for introspection operations. |
paul@354 | 2 | |
paul@523 | 3 | Copyright (C) 2016, 2017 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@758 | 35 | __attr key = __load_via_object(__VALUE(*name), __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@758 | 43 | out = __check_and_load_via_object_null(__VALUE(*obj), key.pos, key.code); |
paul@758 | 44 | if (__ISNULL(out)) |
paul@487 | 45 | { |
paul@487 | 46 | /* Inspect the object's class if this failed. */ |
paul@487 | 47 | |
paul@758 | 48 | out = __check_and_load_via_class__(__VALUE(*obj), key.pos, key.code); |
paul@758 | 49 | if (__ISNULL(out)) |
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@757 | 54 | return __update_context(*obj, out); |
paul@487 | 55 | } |
paul@487 | 56 | |
paul@487 | 57 | return out; |
paul@354 | 58 | } |
paul@354 | 59 | |
paul@354 | 60 | __attr __fn_native_introspection_isinstance(__attr __args[]) |
paul@354 | 61 | { |
paul@354 | 62 | __attr * const obj = &__args[1]; |
paul@354 | 63 | __attr * const cls = &__args[2]; |
paul@354 | 64 | |
paul@354 | 65 | /* cls must be a class. */ |
paul@758 | 66 | if (__is_instance_subclass(__VALUE(*obj), *cls)) |
paul@354 | 67 | return __builtins___boolean_True; |
paul@354 | 68 | else |
paul@354 | 69 | return __builtins___boolean_False; |
paul@354 | 70 | } |
paul@354 | 71 | |
paul@354 | 72 | __attr __fn_native_introspection_issubclass(__attr __args[]) |
paul@354 | 73 | { |
paul@354 | 74 | __attr * const obj = &__args[1]; |
paul@354 | 75 | __attr * const cls = &__args[2]; |
paul@354 | 76 | |
paul@354 | 77 | /* obj and cls must be classes. */ |
paul@758 | 78 | if (__is_subclass(__VALUE(*obj), *cls)) |
paul@354 | 79 | return __builtins___boolean_True; |
paul@354 | 80 | else |
paul@354 | 81 | return __builtins___boolean_False; |
paul@354 | 82 | } |
paul@354 | 83 | |
paul@354 | 84 | /* Module initialisation. */ |
paul@354 | 85 | |
paul@354 | 86 | void __main_native_introspection() |
paul@354 | 87 | { |
paul@354 | 88 | } |