Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry added introspect.set() for class attributes #20339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.
- Display of active drivers using command ``status 4``
- ESP32 used UART information
- HASPmota added `haspmota.page_show()` to change page
- Berry added `introspect.set()` for class attributes

### Breaking Changed
- Refactoring of Berry `animate` module for WS2812 Leds (#20236)
Expand Down
4 changes: 4 additions & 0 deletions lib/libesp32/berry/src/be_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ BERRY_API bbool be_setmember(bvm *vm, int index, const char *k)
bstring *key = be_newstr(vm, k);
bmodule *mod = var_toobj(o);
return be_module_setmember(vm, mod, key, v);
} else if (var_isclass(o)) {
bstring *key = be_newstr(vm, k);
bclass *cl = var_toobj(o);
return be_class_setmember(vm, cl, key, v);
}
return bfalse;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/libesp32/berry/src/be_introspectlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static int m_findmember(bvm *vm)
static int m_setmember(bvm *vm)
{
int top = be_top(vm);
if (top >= 3 && (be_isinstance(vm, 1) || be_ismodule(vm, 1)) && be_isstring(vm, 2)) {
if (top >= 3 && (be_isinstance(vm, 1) || be_ismodule(vm, 1) || be_isclass(vm, 1)) && be_isstring(vm, 2)) {
be_setmember(vm, 1, be_tostring(vm, 2));
be_return(vm);
}
Expand Down
22 changes: 22 additions & 0 deletions lib/libesp32/berry/tests/introspect.be
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ assert(introspect.name(A) == 'A')
assert(introspect.name(A.a) == 'a')
assert(introspect.name(A.b) == 'b')
assert(introspect.name(A.c) == nil)

# test introspect get and set
# class and instance
class A
static var a
var b
end

a = A()
introspect.set(A, "a", 10)
assert(A.a == 10)
assert(introspect.get(A, "a") == 10)

introspect.set(a, "b", 20)
assert(a.b == 20)
assert(introspect.get(a, "b") == 20)

# module
m = module('m')
introspect.set(m, 'c', 30)
assert(m.c == 30)
assert(introspect.get(m, 'c') == 30)