Berry faster type (#24175)

* Remove tab from json

* Berry faster `type()` function
This commit is contained in:
s-hadinger 2025-11-29 17:40:12 +01:00 committed by GitHub
parent e61586e9ea
commit 0b639b7ab7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 1 deletions

View File

@ -201,7 +201,15 @@ int be_baselib_super(bvm *vm)
int be_baselib_type(bvm *vm)
{
if (be_top(vm)) {
#if BE_USE_PRECOMPILED_OBJECT
bvalue *v = be_indexof(vm, 1);
bstring *s = be_vtype2bstring(v);
bvalue *reg = be_incrtop(vm);
be_assert(reg < vm->stacktop);
var_setstr(reg, s);
#else
be_pushstring(vm, be_typename(vm, 1));
#endif
be_return(vm);
}
be_return_nil(vm);

View File

@ -10,6 +10,8 @@
#include "be_mem.h"
#include "be_gc.h"
#include "be_vm.h"
#include "be_string.h"
#include "be_const_strtab.h"
#define cast_comobj(o) gc_cast(o, BE_COMOBJ, bcommomobj)
@ -35,6 +37,33 @@ const char* be_vtype2str(bvalue *v)
}
}
bstring* be_vtype2bstring(bvalue *v)
{
#if BE_USE_PRECOMPILED_OBJECT
switch(var_primetype(v)) {
case BE_NIL: return (bstring*) &be_const_str_nil;
case BE_INT: return (bstring*) &be_const_str_int;
case BE_REAL: return (bstring*) &be_const_str_real;
case BE_BOOL: return (bstring*) &be_const_str_bool;
case BE_CLOSURE: case BE_NTVCLOS: case BE_CTYPE_FUNC:
case BE_NTVFUNC: return (bstring*) &be_const_str_function;
case BE_PROTO: return (bstring*) &be_const_str_proto;
case BE_CLASS: return (bstring*) &be_const_str_class;
case BE_STRING: return (bstring*) &be_const_str_string;
case BE_LIST: return (bstring*) &be_const_str_list;
case BE_MAP: return (bstring*) &be_const_str_map;
case BE_INSTANCE: return (bstring*) &be_const_str_instance;
case BE_MODULE: return (bstring*) &be_const_str_module;
case BE_INDEX: return (bstring*) &be_const_str_var;
case BE_COMPTR: return (bstring*) &be_const_str_ptr;
default: return (bstring*) &be_const_str_invalid_type;
}
#else
return be_newstr(vm, be_vtype2str(v));
#endif
}
bvalue* be_indexof(bvm *vm, int idx)
{
if (idx > 0) { /* absolute index */

View File

@ -261,6 +261,7 @@ typedef const char* (*breader)(struct blexer*, void*, size_t*);
#define var_toidx(_v) cast_int(var_toint(_v))
const char* be_vtype2str(bvalue *v);
bstring* be_vtype2bstring(bvalue *v);
bvalue* be_indexof(bvm *vm, int idx);
void be_commonobj_delete(bvm *vm, bgcobject *obj);
int be_commonobj_destroy_generic(bvm* vm);