mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 10:46:31 +00:00
Berry introspect.module
This commit is contained in:
parent
1373e415c9
commit
4df75cd54b
@ -1,17 +1,18 @@
|
||||
#include "be_constobj.h"
|
||||
|
||||
static be_define_const_map_slots(m_libintrospect_map) {
|
||||
{ be_const_key(members, 3), be_const_func(m_attrlist) },
|
||||
{ be_const_key(set, -1), be_const_func(m_setmember) },
|
||||
{ be_const_key(get, -1), be_const_func(m_findmember) },
|
||||
{ be_const_key(toptr, -1), be_const_func(m_toptr) },
|
||||
{ be_const_key(ismethod, -1), be_const_func(m_ismethod) },
|
||||
{ be_const_key(fromptr, 2), be_const_func(m_fromptr) },
|
||||
{ be_const_key(toptr, -1), be_const_func(m_toptr) },
|
||||
{ be_const_key(get, -1), be_const_func(m_findmember) },
|
||||
{ be_const_key(module, 5), be_const_func(m_getmodule) },
|
||||
{ be_const_key(fromptr, 1), be_const_func(m_fromptr) },
|
||||
{ be_const_key(members, -1), be_const_func(m_attrlist) },
|
||||
{ be_const_key(set, 3), be_const_func(m_setmember) },
|
||||
};
|
||||
|
||||
static be_define_const_map(
|
||||
m_libintrospect_map,
|
||||
6
|
||||
7
|
||||
);
|
||||
|
||||
static be_define_const_module(
|
||||
|
@ -127,6 +127,22 @@ static int m_fromptr(bvm *vm)
|
||||
be_return_nil(vm);
|
||||
}
|
||||
|
||||
/* load module by name, like `import` would do. But don't create a global variable from it. */
|
||||
static int m_getmodule(bvm *vm)
|
||||
{
|
||||
int top = be_top(vm);
|
||||
if (top >= 1) {
|
||||
bvalue *v = be_indexof(vm, 1);
|
||||
if (var_isstr(v)) {
|
||||
int ret = be_module_load(vm, var_tostr(v));
|
||||
if (ret == BE_OK) {
|
||||
be_return(vm);
|
||||
}
|
||||
}
|
||||
}
|
||||
be_return_nil(vm);
|
||||
}
|
||||
|
||||
/* checks if the function (berry bytecode bproto only) is hinted as a method */
|
||||
static int m_ismethod(bvm *vm)
|
||||
{
|
||||
@ -150,6 +166,8 @@ be_native_module_attr_table(introspect) {
|
||||
be_native_module_function("get", m_findmember),
|
||||
be_native_module_function("set", m_setmember),
|
||||
|
||||
be_native_module_function("module", m_getmodule),
|
||||
|
||||
be_native_module_function("toptr", m_toptr),
|
||||
be_native_module_function("fromptr", m_fromptr),
|
||||
|
||||
@ -165,6 +183,8 @@ module introspect (scope: global, depend: BE_USE_INTROSPECT_MODULE) {
|
||||
get, func(m_findmember)
|
||||
set, func(m_setmember)
|
||||
|
||||
module, func(m_getmodule)
|
||||
|
||||
toptr, func(m_toptr)
|
||||
fromptr, func(m_fromptr)
|
||||
|
||||
|
@ -128,8 +128,11 @@ static char* fixpath(bvm *vm, bstring *path, size_t *size)
|
||||
const char *split, *base;
|
||||
bvalue *func = vm->cf->func;
|
||||
bclosure *cl = var_toobj(func);
|
||||
be_assert(var_isclosure(func));
|
||||
if (var_isclosure(func)) {
|
||||
base = str(cl->proto->source); /* get the source file path */
|
||||
} else {
|
||||
base = "/";
|
||||
}
|
||||
split = be_splitpath(base);
|
||||
*size = split - base + (size_t)str_len(path) + SUFFIX_LEN;
|
||||
buffer = be_malloc(vm, *size);
|
||||
|
Loading…
x
Reference in New Issue
Block a user