Lichen

Annotated templates/native/introspection.c

919:326570523de5
2021-06-22 Paul Boddie Merged changes from the default branch. trailing-data
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@664 29
__attr __fn_native_introspection_object_getattr(__attr __self, __attr obj, __attr name, __attr _default)
paul@354 30
{
paul@664 31
    /* name interpreted as string */
paul@763 32
    __attr key = __load_via_object(__VALUE(name), __key__);
paul@487 33
    __attr out;
paul@354 34
paul@360 35
    if ((key.code == 0) && (key.pos == 0))
paul@664 36
        return _default;
paul@487 37
paul@487 38
    /* Attempt to get the attribute from the object. */
paul@487 39
paul@763 40
    out = __check_and_load_via_object_null(__VALUE(obj), key.pos, key.code);
paul@758 41
    if (__ISNULL(out))
paul@487 42
    {
paul@487 43
        /* Inspect the object's class if this failed. */
paul@487 44
paul@763 45
        out = __check_and_load_via_class__(__VALUE(obj), key.pos, key.code);
paul@758 46
        if (__ISNULL(out))
paul@664 47
            return _default;
paul@487 48
paul@487 49
        /* Update the context to the object if it is a method. */
paul@487 50
paul@763 51
        return __update_context(obj, out);
paul@487 52
    }
paul@487 53
paul@487 54
    return out;
paul@354 55
}
paul@354 56
paul@664 57
__attr __fn_native_introspection_isinstance(__attr __self, __attr obj, __attr cls)
paul@354 58
{
paul@354 59
    /* cls must be a class. */
paul@763 60
    if (__is_instance_subclass(__VALUE(obj), cls))
paul@354 61
        return __builtins___boolean_True;
paul@354 62
    else
paul@354 63
        return __builtins___boolean_False;
paul@354 64
}
paul@354 65
paul@664 66
__attr __fn_native_introspection_issubclass(__attr __self, __attr obj, __attr cls)
paul@354 67
{
paul@354 68
    /* obj and cls must be classes. */
paul@763 69
    if (__is_subclass(__VALUE(obj), cls))
paul@354 70
        return __builtins___boolean_True;
paul@354 71
    else
paul@354 72
        return __builtins___boolean_False;
paul@354 73
}
paul@354 74
paul@354 75
/* Module initialisation. */
paul@354 76
paul@354 77
void __main_native_introspection()
paul@354 78
{
paul@354 79
}