Skip to content

Commit bd47d99

Browse files
authored
Berry classof extended to class methods (#21615)
1 parent 87148ae commit bd47d99

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
## [14.1.0.2]
77
### Added
8+
- Berry `classof` extended to class methods
89

910
### Breaking Changed
1011

lib/libesp32/berry/src/be_api.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,22 @@ BERRY_API bbool be_classof(bvm *vm, int index)
544544
binstance *ins = var_toobj(v);
545545
var_setclass(top, be_instance_class(ins));
546546
return btrue;
547+
} else if (var_isclosure(v)) {
548+
bclosure *cl = var_toobj(v);
549+
bproto *pr = cl->proto;
550+
if (pr != NULL) {
551+
bclass *cla;
552+
if (pr->nproto > 0) {
553+
cla = (bclass*) pr->ptab[pr->nproto];
554+
} else {
555+
cla = (bclass*) pr->ptab;
556+
}
557+
if (cla && var_basetype(cla) == BE_CLASS) {
558+
bvalue *top = be_incrtop(vm);
559+
var_setclass(top, cla);
560+
return btrue;
561+
}
562+
}
547563
}
548564
return bfalse;
549565
}

lib/libesp32/berry/tests/class.be

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,19 @@ c4 = Test_class()
5757
assert(type(c4.c) == 'class')
5858
c5 = c4.c()
5959
assert(type(c5) == 'instance')
60-
assert(classname(c5) == 'map')
60+
assert(classname(c5) == 'map')
61+
62+
#- classof now gets back the class of Berry methods -#
63+
class A
64+
def f() end
65+
static def g() end
66+
end
67+
class B : A
68+
def h() end
69+
end
70+
assert(classof(A.f) == A)
71+
assert(classof(A.g) == A)
72+
assert(classof(B.h) == B)
73+
#- returns nil if native function of not in class -#
74+
assert(classof(int) == nil)
75+
assert(classof(def () end) == nil)

0 commit comments

Comments
 (0)