From 7ce3ba376c2e507b13ba9d6b0486c7d8e146782b Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Tue, 20 May 2025 22:39:42 +0200 Subject: [PATCH] Berry 'introspect.module' option to not cache module entry (#23451) --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_introspectlib.c | 6 +++++- lib/libesp32/berry/src/be_module.c | 16 ++++++++++++---- lib/libesp32/berry/src/be_module.h | 1 + 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d0f035cc..95ede8f9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - HASPmota auto-dimming when no touch (#23425) - Provide serial upload port from VSC to PIO (#23436) - Berry support for `sortedmap` (#23441) +- Berry `introspect.module` option to not cache module entry ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_introspectlib.c b/lib/libesp32/berry/src/be_introspectlib.c index 3d91e1ad9..50c9e7329 100644 --- a/lib/libesp32/berry/src/be_introspectlib.c +++ b/lib/libesp32/berry/src/be_introspectlib.c @@ -182,7 +182,11 @@ static int m_getmodule(bvm *vm) if (top >= 1) { bvalue *v = be_indexof(vm, 1); if (var_isstr(v)) { - int ret = be_module_load(vm, var_tostr(v)); + bbool no_cache = bfalse; + if (top >= 2) { + no_cache = be_tobool(vm, 2); + } + int ret = be_module_load_nocache(vm, var_tostr(v), no_cache); if (ret == BE_OK) { be_return(vm); } diff --git a/lib/libesp32/berry/src/be_module.c b/lib/libesp32/berry/src/be_module.c index 7b638b4c5..87c290e85 100644 --- a/lib/libesp32/berry/src/be_module.c +++ b/lib/libesp32/berry/src/be_module.c @@ -278,8 +278,8 @@ static void module_init(bvm *vm) { } } -/* load module to vm->top */ -int be_module_load(bvm *vm, bstring *path) +/* load module to vm->top, option to cache or not */ +int be_module_load_nocache(bvm *vm, bstring *path, bbool nocache) { int res = BE_OK; if (!load_cached(vm, path)) { @@ -287,14 +287,22 @@ int be_module_load(bvm *vm, bstring *path) if (res == BE_IO_ERROR) res = load_package(vm, path); if (res == BE_OK) { - /* on first load of the module, try running the '()' function */ + /* on first load of the module, try running the 'init' function */ module_init(vm); - be_cache_module(vm, path); + if (!nocache) { /* cache the module if it is loaded successfully */ + be_cache_module(vm, path); + } } } return res; } +/* load module to vm->top */ +int be_module_load(bvm *vm, bstring *path) +{ + return be_module_load_nocache(vm, path, btrue); +} + BERRY_API bbool be_getmodule(bvm *vm, const char *k) { int res = be_module_load(vm, be_newstr(vm, k)); diff --git a/lib/libesp32/berry/src/be_module.h b/lib/libesp32/berry/src/be_module.h index 89ae682ae..66f39b25d 100644 --- a/lib/libesp32/berry/src/be_module.h +++ b/lib/libesp32/berry/src/be_module.h @@ -34,6 +34,7 @@ typedef struct bmodule { bmodule* be_module_new(bvm *vm); void be_module_delete(bvm *vm, bmodule *module); int be_module_load(bvm *vm, bstring *path); +int be_module_load_nocache(bvm *vm, bstring *path, bbool cache); void be_cache_module(bvm *vm, bstring *name); int be_module_attr(bvm *vm, bmodule *module, bstring *attr, bvalue *dst); bbool be_module_setmember(bvm *vm, bmodule *module, bstring *attr, bvalue *src);