Berry yet another fix 2

This commit is contained in:
Stephan Hadinger 2021-12-15 23:32:49 +01:00
parent a92f2cb941
commit c152838e25
8 changed files with 1005 additions and 1072 deletions

View File

@ -554,7 +554,6 @@ extern const bcstring be_const_str_readline;
extern const bcstring be_const_str_real;
extern const bcstring be_const_str_reapply;
extern const bcstring be_const_str_redirect;
extern const bcstring be_const_str_reduce;
extern const bcstring be_const_str_refr_size;
extern const bcstring be_const_str_register_obj;
extern const bcstring be_const_str_remove;

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,23 @@
#include "be_constobj.h"
static be_define_const_map_slots(be_class_map_map) {
{ be_const_key(setitem, -1), be_const_func(m_setitem) },
{ be_const_key(remove, 8), be_const_func(m_remove) },
{ be_const_key(insert, -1), be_const_func(m_insert) },
{ be_const_key(tostring, 4), be_const_func(m_tostring) },
{ be_const_key(has, -1), be_const_func(m_contains) },
{ be_const_key(init, -1), be_const_func(m_init) },
{ be_const_key(contains, 9), be_const_func(m_contains) },
{ be_const_key(_X2Ep, 13), be_const_var(0) },
{ be_const_key(reduce, -1), be_const_func(m_reduce) },
{ be_const_key(size, -1), be_const_func(m_size) },
{ be_const_key(find, -1), be_const_func(m_find) },
{ be_const_key(tostring, -1), be_const_func(m_tostring) },
{ be_const_key(insert, -1), be_const_func(m_insert) },
{ be_const_key(init, -1), be_const_func(m_init) },
{ be_const_key(contains, -1), be_const_func(m_contains) },
{ be_const_key(keys, -1), be_const_func(m_keys) },
{ be_const_key(item, -1), be_const_func(m_item) },
{ be_const_key(iter, -1), be_const_func(m_iter) },
{ be_const_key(_X2Ep, -1), be_const_var(0) },
{ be_const_key(iter, 6), be_const_func(m_iter) },
{ be_const_key(setitem, 2), be_const_func(m_setitem) },
{ be_const_key(remove, -1), be_const_func(m_remove) },
{ be_const_key(item, 11), be_const_func(m_item) },
{ be_const_key(find, -1), be_const_func(m_find) },
};
static be_define_const_map(
be_class_map_map,
14
12
);
BE_EXPORT_VARIABLE be_define_const_class(

View File

@ -1271,38 +1271,38 @@ be_local_closure(getbits, /* name */
&be_const_str_getbits,
&be_const_str_solidified,
( &(const binstruction[32]) { /* code */
0x180C0500, // 0000 LE R3 R2 K0
0x740E0002, // 0001 JMPT R3 #0005
0x540E001F, // 0002 LDINT R3 32
0x240C0403, // 0003 GT R3 R2 R3
0x780E0000, // 0004 JMPF R3 #0006
0xB0060302, // 0005 RAISE 1 K1 K2
0x580C0000, // 0006 LDCONST R3 K0
0x3C100303, // 0007 SHR R4 R1 K3
0x54160007, // 0008 LDINT R5 8
0x10040205, // 0009 MOD R1 R1 R5
0x58140000, // 000A LDCONST R5 K0
0x24180500, // 000B GT R6 R2 K0
0x781A0011, // 000C JMPF R6 #001F
0x541A0007, // 000D LDINT R6 8
0x04180C01, // 000E SUB R6 R6 R1
0x241C0C02, // 000F GT R7 R6 R2
0x781E0000, // 0010 JMPF R7 #0012
0x5C180400, // 0011 MOVE R6 R2
0x381E0806, // 0012 SHL R7 K4 R6
0x041C0F04, // 0013 SUB R7 R7 K4
0x381C0E01, // 0014 SHL R7 R7 R1
0x94200004, // 0015 GETIDX R8 R0 R4
0x2C201007, // 0016 AND R8 R8 R7
0x3C201001, // 0017 SHR R8 R8 R1
0x38201005, // 0018 SHL R8 R8 R5
0x300C0608, // 0019 OR R3 R3 R8
0x00140A06, // 001A ADD R5 R5 R6
0x04080406, // 001B SUB R2 R2 R6
0x58040000, // 001C LDCONST R1 K0
0x00100904, // 001D ADD R4 R4 K4
0x7001FFEB, // 001E JMP #000B
0x80040600, // 001F RET 1 R3
0x180C0500, // 0000 LE R3 R2 K0
0x740E0002, // 0001 JMPT R3 #0005
0x540E001F, // 0002 LDINT R3 32
0x240C0403, // 0003 GT R3 R2 R3
0x780E0000, // 0004 JMPF R3 #0006
0xB0060302, // 0005 RAISE 1 K1 K2
0x580C0000, // 0006 LDCONST R3 K0
0x3C100303, // 0007 SHR R4 R1 K3
0x54160007, // 0008 LDINT R5 8
0x10040205, // 0009 MOD R1 R1 R5
0x58140000, // 000A LDCONST R5 K0
0x24180500, // 000B GT R6 R2 K0
0x781A0011, // 000C JMPF R6 #001F
0x541A0007, // 000D LDINT R6 8
0x04180C01, // 000E SUB R6 R6 R1
0x241C0C02, // 000F GT R7 R6 R2
0x781E0000, // 0010 JMPF R7 #0012
0x5C180400, // 0011 MOVE R6 R2
0x381E0806, // 0012 SHL R7 K4 R6
0x041C0F04, // 0013 SUB R7 R7 K4
0x381C0E01, // 0014 SHL R7 R7 R1
0x94200004, // 0015 GETIDX R8 R0 R4
0x2C201007, // 0016 AND R8 R8 R7
0x3C201001, // 0017 SHR R8 R8 R1
0x38201005, // 0018 SHL R8 R8 R5
0x300C0608, // 0019 OR R3 R3 R8
0x00140A06, // 001A ADD R5 R5 R6
0x04080406, // 001B SUB R2 R2 R6
0x58040000, // 001C LDCONST R1 K0
0x00100904, // 001D ADD R4 R4 K4
0x7001FFEB, // 001E JMP #000B
0x80040600, // 001F RET 1 R3
})
)
);
@ -1331,43 +1331,43 @@ be_local_closure(setbits, /* name */
&be_const_str_setbits,
&be_const_str_solidified,
( &(const binstruction[37]) { /* code */
0x14100500, // 0000 LT R4 R2 K0
0x74120002, // 0001 JMPT R4 #0005
0x5412001F, // 0002 LDINT R4 32
0x24100404, // 0003 GT R4 R2 R4
0x78120000, // 0004 JMPF R4 #0006
0xB0060302, // 0005 RAISE 1 K1 K2
0x60100009, // 0006 GETGBL R4 G9
0x5C140600, // 0007 MOVE R5 R3
0x7C100200, // 0008 CALL R4 1
0x5C0C0800, // 0009 MOVE R3 R4
0x3C100303, // 000A SHR R4 R1 K3
0x54160007, // 000B LDINT R5 8
0x10040205, // 000C MOD R1 R1 R5
0x24140500, // 000D GT R5 R2 K0
0x78160014, // 000E JMPF R5 #0024
0x54160007, // 000F LDINT R5 8
0x04140A01, // 0010 SUB R5 R5 R1
0x24180A02, // 0011 GT R6 R5 R2
0x781A0000, // 0012 JMPF R6 #0014
0x5C140400, // 0013 MOVE R5 R2
0x381A0805, // 0014 SHL R6 K4 R5
0x04180D04, // 0015 SUB R6 R6 K4
0x541E00FE, // 0016 LDINT R7 255
0x38200C01, // 0017 SHL R8 R6 R1
0x041C0E08, // 0018 SUB R7 R7 R8
0x94200004, // 0019 GETIDX R8 R0 R4
0x2C201007, // 001A AND R8 R8 R7
0x2C240606, // 001B AND R9 R3 R6
0x38241201, // 001C SHL R9 R9 R1
0x30201009, // 001D OR R8 R8 R9
0x98000808, // 001E SETIDX R0 R4 R8
0x3C0C0605, // 001F SHR R3 R3 R5
0x04080405, // 0020 SUB R2 R2 R5
0x58040000, // 0021 LDCONST R1 K0
0x00100904, // 0022 ADD R4 R4 K4
0x7001FFE8, // 0023 JMP #000D
0x80040000, // 0024 RET 1 R0
0x14100500, // 0000 LT R4 R2 K0
0x74120002, // 0001 JMPT R4 #0005
0x5412001F, // 0002 LDINT R4 32
0x24100404, // 0003 GT R4 R2 R4
0x78120000, // 0004 JMPF R4 #0006
0xB0060302, // 0005 RAISE 1 K1 K2
0x60100009, // 0006 GETGBL R4 G9
0x5C140600, // 0007 MOVE R5 R3
0x7C100200, // 0008 CALL R4 1
0x5C0C0800, // 0009 MOVE R3 R4
0x3C100303, // 000A SHR R4 R1 K3
0x54160007, // 000B LDINT R5 8
0x10040205, // 000C MOD R1 R1 R5
0x24140500, // 000D GT R5 R2 K0
0x78160014, // 000E JMPF R5 #0024
0x54160007, // 000F LDINT R5 8
0x04140A01, // 0010 SUB R5 R5 R1
0x24180A02, // 0011 GT R6 R5 R2
0x781A0000, // 0012 JMPF R6 #0014
0x5C140400, // 0013 MOVE R5 R2
0x381A0805, // 0014 SHL R6 K4 R5
0x04180D04, // 0015 SUB R6 R6 K4
0x541E00FE, // 0016 LDINT R7 255
0x38200C01, // 0017 SHL R8 R6 R1
0x041C0E08, // 0018 SUB R7 R7 R8
0x94200004, // 0019 GETIDX R8 R0 R4
0x2C201007, // 001A AND R8 R8 R7
0x2C240606, // 001B AND R9 R3 R6
0x38241201, // 001C SHL R9 R9 R1
0x30201009, // 001D OR R8 R8 R9
0x98000808, // 001E SETIDX R0 R4 R8
0x3C0C0605, // 001F SHR R3 R3 R5
0x04080405, // 0020 SUB R2 R2 R5
0x58040000, // 0021 LDCONST R1 K0
0x00100904, // 0022 ADD R4 R4 K4
0x7001FFE8, // 0023 JMP #000D
0x80040000, // 0024 RET 1 R0
})
)
);

View File

@ -201,15 +201,6 @@ const bntvmodule be_native_module(_module) = { \
.members = _members \
}
// #define be_local_instance(_name, _class_ptr, _members) \
// static const binstance i_##_name = { \
// be_const_header(BE_INSTANCE), \
// .super = NULL, \
// .sub = NULL, \
// ._class = (bclass*) _class_ptr, \
// .members = _members \
// }
#define be_nested_map(_size, _slots) \
& (const bmap) { \
be_const_header(BE_MAP), \
@ -233,13 +224,6 @@ const bntvmodule be_native_module(_module) = { \
BE_STRING \
}
#define be_nested_str_literal(_str) \
{ \
{ .s=(be_nested_const_str(_str, 0, sizeof(_str)-1 )) \
}, \
BE_STRING \
}
#define be_str_literal(_str) \
be_nested_const_str(_str, 0, sizeof(_str)-1 )

View File

@ -561,5 +561,9 @@ void be_gc_collect(bvm *vm)
reset_fixedlist(vm);
/* step 5: calculate the next GC threshold */
vm->gc.threshold = next_threshold(vm->gc);
#if BE_USE_PERF_COUNTERS
if (vm->obshook != NULL) (*vm->obshook)(vm, BE_OBS_GC_END, vm->gc.usage, vm->counter_gc_kept, vm->counter_gc_freed);
#else
if (vm->obshook != NULL) (*vm->obshook)(vm, BE_OBS_GC_END, vm->gc.usage);
#endif
}

View File

@ -212,62 +212,6 @@ static int m_keys(bvm *vm)
be_return(vm);
}
/* apply a function/closure to each element of a map */
/* `map.reduce(f:function [, initializer:any]) -> any` */
/* Calls for each element `f(key, value, acc) -> any` */
/* `acc` is initialized with `initilizer` if present or `nil` */
/* the return value of the function becomes the next value passed in arg `acc` */
static int m_reduce(bvm *vm)
{
int argc = be_top(vm);
if (argc > 1 && be_isfunction(vm, 2)) {
bbool has_initializer = (argc > 2);
/* get map internal object */
be_getmember(vm, 1, ".p");
bvalue *v = be_indexof(vm, -1);
bmap *map = cast(bmap*, var_toobj(v));
/* get the number of slots if any */
int slots_initial = be_map_size(map);
/* place-holder for on-going value and return value */
if (has_initializer) {
be_pushvalue(vm, 3);
} else {
be_pushnil(vm); /* if no initializer use `nil` */
}
for (int i = 0; i < slots_initial; i++) {
bmapnode * node = map->slots + i;
if (!var_isnil(&node->key)) { /* is the key present in this slot? */
be_pushvalue(vm, 2); /* push function */
bvalue kv; /* push key on stack */
kv.type = node->key.type;
kv.v = node->key.v;
bvalue *reg = vm->top;
var_setval(reg, &kv);
be_incrtop(vm);
reg = vm->top; /* push value on stack */
var_setval(reg, &node->value);
be_incrtop(vm);
be_pushvalue(vm, -4);
be_call(vm, 3);
be_pop(vm, 3); /* pop args, keep return value */
be_remove(vm, -2); /* remove previous accumulator, keep return value from function */
}
/* check if the map has been resized during the call */
if (be_map_size(map) != slots_initial) {
be_raise(vm, "stop_iteration", "map resized within apply");
break; /* abort */
}
}
be_return(vm);
}
be_raise(vm, "value_error", "needs function as first argument");
be_return_nil(vm);
}
#if !BE_USE_PRECOMPILED_OBJECT
void be_load_maplib(bvm *vm)
{
@ -280,12 +224,10 @@ void be_load_maplib(bvm *vm)
{ "setitem", m_setitem },
{ "find", m_find },
{ "contains", m_contains },
{ "has", m_contains }, /* deprecated */
{ "size", m_size },
{ "insert", m_insert },
{ "iter", m_iter },
{ "keys", m_keys },
{ "reduce", m_reduce },
{ NULL, NULL }
};
be_regclass(vm, "map", members);
@ -301,12 +243,10 @@ class be_class_map (scope: global, name: map) {
setitem, func(m_setitem)
find, func(m_find)
contains, func(m_contains)
has, func(m_contains)
size, func(m_size)
insert, func(m_insert)
iter, func(m_iter)
keys, func(m_keys)
reduce, func(m_reduce)
}
@const_object_info_end */
#include "../generate/be_fixed_be_class_map.h"

View File

@ -38,11 +38,11 @@ extern const bclass be_class_map;
be_writestring(__lbuf); \
} while (0)
/********************************************************************
/********************************************************************\
* Encode string to identifiers
*
* `_X` is used as an escape marker
/********************************************************************/
\********************************************************************/
static unsigned toidentifier_length(const char *s)
{
unsigned len = 1;
@ -115,7 +115,6 @@ static void m_solidify_map(bvm *vm, bmap * map, const char *class_name)
be_raise(vm, "internal_error", error);
}
int key_next = node->key.next;
size_t len = strlen(str(node->key.v.s));
if (0xFFFFFF == key_next) {
key_next = -1; /* more readable */
}
@ -192,10 +191,16 @@ static void m_solidify_bvalue(bvm *vm, bvalue * value, const char *classname, co
}
break;
case BE_CLOSURE:
logfmt("be_const_%sclosure(%s%s%s_closure)",
func_isstatic(value) ? "static_" : "",
classname ? classname : "", classname ? "_" : "",
str(((bclosure*) var_toobj(value))->proto->name));
{
const char * func_name = str(((bclosure*) var_toobj(value))->proto->name);
size_t id_len = toidentifier_length(func_name);
char func_name_id[id_len];
toidentifier(func_name_id, func_name);
logfmt("be_const_%sclosure(%s%s%s_closure)",
func_isstatic(value) ? "static_" : "",
classname ? classname : "", classname ? "_" : "",
func_name_id);
}
break;
case BE_CLASS:
logfmt("be_const_class(be_class_%s)", str(((bclass*) var_toobj(value))->name));
@ -365,9 +370,14 @@ static void m_solidify_closure(bvm *vm, bclosure *cl, const char * classname, in
logfmt("** Solidified function: %s\n", func_name);
logfmt("********************************************************************/\n");
logfmt("be_local_closure(%s%s%s, /* name */\n",
classname ? classname : "", classname ? "_" : "",
func_name);
{
size_t id_len = toidentifier_length(func_name);
char func_name_id[id_len];
toidentifier(func_name_id, func_name);
logfmt("be_local_closure(%s%s%s, /* name */\n",
classname ? classname : "", classname ? "_" : "",
func_name_id);
}
m_solidify_proto(vm, pr, func_name, builtins, indent);
logfmt("\n");