From 0feba56d8e63d56005712aff3db4105717735033 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sat, 15 Apr 2023 19:34:31 +0200 Subject: [PATCH] Berry `instrospect.name()` to get names of functions, modules and classes (#18422) --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_introspectlib.c | 29 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8c2628de..1d1cc31d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. - Berry `webserver.html_escape()` reusing the internal HTML escaping function - Support for GDK101 gamma radiation sensor by Petr Novacek (#18390) - Matter support in now stabilized for Apple and Google (not tested with Alexa) +- Berry `instrospect.name()` to get names of functions, modules and classes ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_introspectlib.c b/lib/libesp32/berry/src/be_introspectlib.c index 28f506d18..047815558 100644 --- a/lib/libesp32/berry/src/be_introspectlib.c +++ b/lib/libesp32/berry/src/be_introspectlib.c @@ -188,6 +188,31 @@ static int m_ismethod(bvm *vm) be_return_nil(vm); } +static int m_name(bvm *vm) +{ + int top = be_top(vm); + if (top >= 1) { + bvalue *v = be_indexof(vm, 1); + const char* name = NULL; + switch (var_type(v)) { + case BE_CLOSURE: + name = str(((bclosure*) var_toobj(v))->proto->name); + break; + case BE_CLASS: + name = str(((bclass*) var_toobj(v))->name); + break; + case BE_MODULE: + name = be_module_name(var_toobj(v)); + break; + } + if (name) { + be_pushstring(vm, name); + be_return(vm); + } + } + be_return_nil(vm); +} + #if !BE_USE_PRECOMPILED_OBJECT be_native_module_attr_table(introspect) { be_native_module_function("members", m_attrlist), @@ -201,6 +226,8 @@ be_native_module_attr_table(introspect) { be_native_module_function("toptr", m_toptr), be_native_module_function("fromptr", m_fromptr), + be_native_module_function("name", m_name), + be_native_module_function("ismethod", m_ismethod), }; @@ -219,6 +246,8 @@ module introspect (scope: global, depend: BE_USE_INTROSPECT_MODULE) { toptr, func(m_toptr) fromptr, func(m_fromptr) + name, func(m_name) + ismethod, func(m_ismethod) } @const_object_info_end */