Berry `classof` extended to class methods (#21615)

This commit is contained in:
s-hadinger 2024-06-12 10:48:38 +02:00 committed by GitHub
parent 87148ae0c1
commit bd47d99ceb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 1 deletions

View File

@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## [14.1.0.2]
### Added
- Berry `classof` extended to class methods
### Breaking Changed

View File

@ -544,6 +544,22 @@ BERRY_API bbool be_classof(bvm *vm, int index)
binstance *ins = var_toobj(v);
var_setclass(top, be_instance_class(ins));
return btrue;
} else if (var_isclosure(v)) {
bclosure *cl = var_toobj(v);
bproto *pr = cl->proto;
if (pr != NULL) {
bclass *cla;
if (pr->nproto > 0) {
cla = (bclass*) pr->ptab[pr->nproto];
} else {
cla = (bclass*) pr->ptab;
}
if (cla && var_basetype(cla) == BE_CLASS) {
bvalue *top = be_incrtop(vm);
var_setclass(top, cla);
return btrue;
}
}
}
return bfalse;
}

View File

@ -57,4 +57,19 @@ c4 = Test_class()
assert(type(c4.c) == 'class')
c5 = c4.c()
assert(type(c5) == 'instance')
assert(classname(c5) == 'map')
assert(classname(c5) == 'map')
#- classof now gets back the class of Berry methods -#
class A
def f() end
static def g() end
end
class B : A
def h() end
end
assert(classof(A.f) == A)
assert(classof(A.g) == A)
assert(classof(B.h) == B)
#- returns nil if native function of not in class -#
assert(classof(int) == nil)
assert(classof(def () end) == nil)