Skip to content

Commit 0b639b7

Browse files
authored
Berry faster type (#24175)
* Remove tab from json * Berry faster `type()` function
1 parent e61586e commit 0b639b7

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

lib/libesp32/berry/src/be_baselib.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,15 @@ int be_baselib_super(bvm *vm)
201201
int be_baselib_type(bvm *vm)
202202
{
203203
if (be_top(vm)) {
204+
#if BE_USE_PRECOMPILED_OBJECT
205+
bvalue *v = be_indexof(vm, 1);
206+
bstring *s = be_vtype2bstring(v);
207+
bvalue *reg = be_incrtop(vm);
208+
be_assert(reg < vm->stacktop);
209+
var_setstr(reg, s);
210+
#else
204211
be_pushstring(vm, be_typename(vm, 1));
212+
#endif
205213
be_return(vm);
206214
}
207215
be_return_nil(vm);

lib/libesp32/berry/src/be_object.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "be_mem.h"
1111
#include "be_gc.h"
1212
#include "be_vm.h"
13+
#include "be_string.h"
14+
#include "be_const_strtab.h"
1315

1416
#define cast_comobj(o) gc_cast(o, BE_COMOBJ, bcommomobj)
1517

@@ -35,6 +37,33 @@ const char* be_vtype2str(bvalue *v)
3537
}
3638
}
3739

40+
41+
bstring* be_vtype2bstring(bvalue *v)
42+
{
43+
#if BE_USE_PRECOMPILED_OBJECT
44+
switch(var_primetype(v)) {
45+
case BE_NIL: return (bstring*) &be_const_str_nil;
46+
case BE_INT: return (bstring*) &be_const_str_int;
47+
case BE_REAL: return (bstring*) &be_const_str_real;
48+
case BE_BOOL: return (bstring*) &be_const_str_bool;
49+
case BE_CLOSURE: case BE_NTVCLOS: case BE_CTYPE_FUNC:
50+
case BE_NTVFUNC: return (bstring*) &be_const_str_function;
51+
case BE_PROTO: return (bstring*) &be_const_str_proto;
52+
case BE_CLASS: return (bstring*) &be_const_str_class;
53+
case BE_STRING: return (bstring*) &be_const_str_string;
54+
case BE_LIST: return (bstring*) &be_const_str_list;
55+
case BE_MAP: return (bstring*) &be_const_str_map;
56+
case BE_INSTANCE: return (bstring*) &be_const_str_instance;
57+
case BE_MODULE: return (bstring*) &be_const_str_module;
58+
case BE_INDEX: return (bstring*) &be_const_str_var;
59+
case BE_COMPTR: return (bstring*) &be_const_str_ptr;
60+
default: return (bstring*) &be_const_str_invalid_type;
61+
}
62+
#else
63+
return be_newstr(vm, be_vtype2str(v));
64+
#endif
65+
}
66+
3867
bvalue* be_indexof(bvm *vm, int idx)
3968
{
4069
if (idx > 0) { /* absolute index */

lib/libesp32/berry/src/be_object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ typedef const char* (*breader)(struct blexer*, void*, size_t*);
261261
#define var_toidx(_v) cast_int(var_toint(_v))
262262

263263
const char* be_vtype2str(bvalue *v);
264+
bstring* be_vtype2bstring(bvalue *v);
264265
bvalue* be_indexof(bvm *vm, int idx);
265266
void be_commonobj_delete(bvm *vm, bgcobject *obj);
266267
int be_commonobj_destroy_generic(bvm* vm);

lib/libesp32/berry/tools/plugins/vscode/skiars.berry-1.2.0/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Berry Script Language",
44
"description": "A small embedded script language.",
55
"version": "1.2.0",
6-
"icon": "berry-icon.png",
6+
"icon": "berry-icon.png",
77
"publisher": "skiars",
88
"engines": {
99
"vscode": "^1.15.1"

0 commit comments

Comments
 (0)