Skip to content

Commit

Permalink
Berry classof extended to class methods (#21615)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger committed Jun 12, 2024
1 parent 87148ae commit bd47d99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions lib/libesp32/berry/src/be_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 16 additions & 1 deletion lib/libesp32/berry/tests/class.be
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit bd47d99

Please sign in to comment.