Expose krk_isSubClass

This commit is contained in:
K. Lange 2023-02-12 15:09:29 +09:00
parent ca1a490bbb
commit 3f5693cf43
3 changed files with 7 additions and 15 deletions

View File

@ -949,22 +949,14 @@ KRK_Function(isinstance) {
}
}
static int _isSubClass(KrkClass * cls, KrkClass * base) {
while (cls) {
if (cls == base) return 1;
cls = cls->base;
}
return 0;
}
KRK_Function(issubclass) {
FUNCTION_TAKES_EXACTLY(2);
CHECK_ARG(0,class,KrkClass*,cls);
if (IS_CLASS(argv[1])) {
return BOOLEAN_VAL(_isSubClass(cls, AS_CLASS(argv[1])));
return BOOLEAN_VAL(krk_isSubClass(cls, AS_CLASS(argv[1])));
} else if (IS_TUPLE(argv[1])) {
for (size_t i = 0; i < AS_TUPLE(argv[1])->values.count; ++i) {
if (IS_CLASS(AS_TUPLE(argv[1])->values.values[i]) && _isSubClass(cls, AS_CLASS(AS_TUPLE(argv[1])->values.values[i]))) {
if (IS_CLASS(AS_TUPLE(argv[1])->values.values[i]) && krk_isSubClass(cls, AS_CLASS(AS_TUPLE(argv[1])->values.values[i]))) {
return BOOLEAN_VAL(1);
}
}
@ -1281,9 +1273,9 @@ KRK_Function(__build_class__) {
if (IS_CLASS(metaclass)) {
KrkClass * basemeta = base->_class ? base->_class : vm.baseClasses->typeClass;
if (_isSubClass(AS_CLASS(metaclass), basemeta)) {
if (krk_isSubClass(AS_CLASS(metaclass), basemeta)) {
/* good to go */
} else if (_isSubClass(basemeta, AS_CLASS(metaclass))) {
} else if (krk_isSubClass(basemeta, AS_CLASS(metaclass))) {
/* take the more derived one */
metaclass = OBJECT_VAL(basemeta);
} else {

View File

@ -352,4 +352,4 @@ extern int krk_pushStringBuilderFormatV(struct StringBuilder * sb, const char *
extern int krk_pushStringBuilderFormat(struct StringBuilder * sb, const char * fmt, ...);
extern KrkValue krk_stringFromFormat(const char * fmt, ...);
extern int krk_long_to_int(KrkValue val, char size, void * out);
extern int krk_isSubClass(const KrkClass * cls, const KrkClass * base);

View File

@ -2063,7 +2063,7 @@ static inline void commonMethodInvoke(size_t methodOffset, int args, const char
}
}
static int _isSubClass(KrkClass * cls, KrkClass * base) {
int krk_isSubClass(const KrkClass * cls, const KrkClass * base) {
while (cls) {
if (cls == base) return 1;
cls = cls->base;
@ -2738,7 +2738,7 @@ _finishReturn: (void)0;
KrkClass * obj_type;
KrkValue obj = krk_peek(0);
if (IS_CLASS(obj) && _isSubClass(AS_CLASS(obj),AS_CLASS(baseClass))) {
if (IS_CLASS(obj) && krk_isSubClass(AS_CLASS(obj),AS_CLASS(baseClass))) {
/* Class method call */
obj_type = AS_CLASS(obj);
krk_pop();