Berry add module `introspect` (#12728)

* Berry add module ``introspect``

* Rename `members`
This commit is contained in:
s-hadinger 2021-07-22 22:36:18 +02:00 committed by GitHub
parent fdee77d333
commit 392d580a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1744 additions and 1609 deletions

View File

@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Command ``SetSensor1..127 0|1`` to globally disable individual sensor driver
- Support for CAN bus and Freedom Won Battery Management System by Marius Bezuidenhout (#12651)
- Berry ESP32 support for I2S audio mp3 playback
- Berry add module ``introspect``
### Changed
- ESP32 core library from v1.0.7.1 to v1.0.7.3

View File

@ -20,6 +20,7 @@ be_extern_native_module(sys);
be_extern_native_module(debug);
be_extern_native_module(gc);
be_extern_native_module(solidify);
be_extern_native_module(introspect);
/* Tasmota specific */
be_extern_native_module(light);
@ -68,6 +69,9 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = {
#endif
#if BE_USE_SOLIDIFY_MODULE
&be_native_module(solidify),
#endif
#if BE_USE_INTROSPECT_MODULE
&be_native_module(introspect),
#endif
/* user-defined modules register start */

View File

@ -158,6 +158,7 @@
#define BE_USE_DEBUG_MODULE 1
#define BE_USE_GC_MODULE 1
#define BE_USE_SOLIDIFY_MODULE 1
#define BE_USE_INTROSPECT_MODULE 1
/* Macro: BE_EXPLICIT_XXX
* If these macros are defined, the corresponding function will

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
#include "be_constobj.h"
static be_define_const_map_slots(m_libintrospect_map) {
{ be_const_key(members, -1), 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) },
};
static be_define_const_map(
m_libintrospect_map,
3
);
static be_define_const_module(
m_libintrospect,
"introspect"
);
BE_EXPORT_VARIABLE be_define_const_native_module(introspect, NULL);

View File

@ -208,6 +208,12 @@ BERRY_API bbool be_isinstance(bvm *vm, int index)
return var_isinstance(v);
}
BERRY_API bbool be_ismodule(bvm *vm, int index)
{
bvalue *v = be_indexof(vm, index);
return var_ismodule(v);
}
BERRY_API bbool be_islist(bvm *vm, int index)
{
bvalue *v = be_indexof(vm, index);

View File

@ -0,0 +1,100 @@
/********************************************************************
** Copyright (c) 2018-2020 Guan Wenliang
** This file is part of the Berry default interpreter.
** skiars@qq.com, https://github.com/Skiars/berry
** See Copyright Notice in the LICENSE file or at
** https://github.com/Skiars/berry/blob/master/LICENSE
********************************************************************/
#include "be_object.h"
#include "be_module.h"
#include "be_string.h"
#include "be_vector.h"
#include "be_class.h"
#include "be_debug.h"
#include "be_map.h"
#include "be_vm.h"
#include <string.h>
#if BE_USE_INTROSPECT_MODULE
#define global(vm) ((vm)->gbldesc.global)
#define builtin(vm) ((vm)->gbldesc.builtin)
static void dump_map_keys(bvm *vm, bmap *map)
{
if (!map) { return; } /* protect agains potential null pointer */
bmapnode *node;
bmapiter iter = be_map_iter();
while ((node = be_map_next(map, &iter)) != NULL) {
if (var_isstr(&node->key)) {
bstring *s = var_tostr(&node->key);
be_pushstring(vm, str(s));
be_data_push(vm, -2);
be_pop(vm, 1);
}
}
}
static int m_attrlist(bvm *vm)
{
int top = be_top(vm);
be_newobject(vm, "list");
if (top >= 1) {
bvalue *v = be_indexof(vm, 1);
void *obj = var_toobj(v);
switch (var_type(v)) {
case BE_NIL: dump_map_keys(vm, global(vm).vtab); break;
case BE_MODULE: dump_map_keys(vm, ((bmodule*)obj)->table); break;
case BE_CLASS: dump_map_keys(vm, ((bclass*)obj)->members); break;
case BE_INSTANCE: dump_map_keys(vm, ((binstance*)obj)->_class->members); break;
default: break;
}
} else { /* if no parameter, then dump globals */
dump_map_keys(vm, global(vm).vtab);
}
be_pop(vm, 1);
be_return(vm);
}
static int m_findmember(bvm *vm)
{
int top = be_top(vm);
if (top >= 2 && (be_isinstance(vm, 1) || be_ismodule(vm, 1) || be_isclass(vm, 1)) && be_isstring(vm, 2)) {
be_getmember(vm, 1, be_tostring(vm, 2));
be_return(vm);
}
be_return_nil(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)) {
be_setmember(vm, 1, be_tostring(vm, 2));
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),
be_native_module_function("get", m_findmember),
be_native_module_function("set", m_setmember),
};
be_define_native_module(introspect, NULL);
#else
/* @const_object_info_begin
module introspect (scope: global, depend: BE_USE_INTROSPECT_MODULE) {
members, func(m_attrlist)
get, func(m_findmember)
set, func(m_setmember)
}
@const_object_info_end */
#include "../generate/be_fixed_introspect.h"
#endif
#endif /* BE_USE_INTROSPECT_MODULE */

View File

@ -438,6 +438,7 @@ BERRY_API bbool be_isfunction(bvm *vm, int index);
BERRY_API bbool be_isproto(bvm *vm, int index);
BERRY_API bbool be_isclass(bvm *vm, int index);
BERRY_API bbool be_isinstance(bvm *vm, int index);
BERRY_API bbool be_ismodule(bvm *vm, int index);
BERRY_API bbool be_islist(bvm *vm, int index);
BERRY_API bbool be_ismap(bvm *vm, int index);
BERRY_API bbool be_iscomptr(bvm *vm, int index);