Berry instrospect.name() to get names of functions, modules and classes (#18422)

This commit is contained in:
s-hadinger 2023-04-15 19:34:31 +02:00 committed by GitHub
parent 21c3812826
commit 0feba56d8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -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

View File

@ -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 */