Merge pull request #15306 from s-hadinger/berry_map_int

Berry support for int keys in map in solidification
This commit is contained in:
s-hadinger 2022-04-06 19:29:05 +02:00 committed by GitHub
commit a65e6ac0b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 14 deletions

View File

@ -33,6 +33,12 @@ extern "C" {
.next = (uint32_t)(_next) & 0xFFFFFF \
}
#define be_const_key_int(_i, _next) { \
.v.i = _i, \
.type = BE_INT, \
.next = (uint32_t)(_next) & 0xFFFFFF \
}
#define be_const_func(_func) { \
.v.nf = (_func), \
.type = BE_NTVFUNC \
@ -249,6 +255,12 @@ const bntvmodule be_native_module(_module) = { \
uint32_t((_next)&0xFFFFFF) \
}
#define be_const_key_int(_i, _next) { \
bvaldata(i), \
BE_INT, \
uint32_t((_next)&0xFFFFFF) \
}
#define be_const_func(_func) { \
bvaldata(_func), \
BE_NTVFUNC \

View File

@ -109,22 +109,26 @@ static void m_solidify_map(bvm *vm, bmap * map, const char *class_name)
if (node->key.type == BE_NIL) {
continue; /* key not used */
}
if (node->key.type != BE_STRING) {
char error[64];
snprintf(error, sizeof(error), "Unsupported type in key: %i", node->key.type);
be_raise(vm, "internal_error", error);
}
int key_next = node->key.next;
if (0xFFFFFF == key_next) {
key_next = -1; /* more readable */
}
/* convert the string literal to identifier */
const char * key = str(node->key.v.s);
size_t id_len = toidentifier_length(key);
char id_buf[id_len];
toidentifier(id_buf, key);
logfmt(" { be_const_key(%s, %i), ", id_buf, key_next);
m_solidify_bvalue(vm, &node->value, class_name, str(node->key.v.s));
if (node->key.type == BE_STRING) {
/* convert the string literal to identifier */
const char * key = str(node->key.v.s);
size_t id_len = toidentifier_length(key);
char id_buf[id_len];
toidentifier(id_buf, key);
logfmt(" { be_const_key(%s, %i), ", id_buf, key_next);
m_solidify_bvalue(vm, &node->value, class_name, str(node->key.v.s));
} else if (node->key.type == BE_INT) {
logfmt(" { be_const_key_int(%i, %i), ", node->key.v.i, key_next);
m_solidify_bvalue(vm, &node->value, class_name, NULL);
} else {
char error[64];
snprintf(error, sizeof(error), "Unsupported type in key: %i", node->key.type);
be_raise(vm, "internal_error", error);
}
logfmt(" },\n");
}
@ -358,7 +362,8 @@ static void m_solidify_closure(bvm *vm, bclosure *cl, const char * classname, in
const char * func_name = str(pr->name);
if (cl->nupvals > 0) {
be_raise(vm, "internal_error", "Unsupported upvals in closure");
logfmt("--> Unsupported upvals in closure <---");
// be_raise(vm, "internal_error", "Unsupported upvals in closure");
}
int indent = 2;

View File

@ -113,7 +113,7 @@ static void free_sstring(bvm *vm, bstring *str)
static uint32_t str_hash(const char *str, size_t len)
{
uint32_t hash = 2166136261u;
be_assert(str || len);
be_assert(str || !len);
while (len--) {
hash = (hash ^ (unsigned char)*str++) * 16777619u;
}

View File

@ -92,6 +92,7 @@ extern "C" {
int32_t top = be_top(vm); // Get the number of arguments
if (top >= 2 && be_isstring(vm, 2)) { // 1 mandatory string argument
const char * payload = be_tostring(vm, 2);
be_pop(vm, top); // clear the stack before calling, because of re-entrant call to Berry in a Rule
bool handled = XdrvRulesProcess(0, payload);
be_pushbool(vm, handled);
be_return(vm); // Return