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