refactor(api): use keydict and arena for more api return values

Implement api_keydict_to_dict as the complement to api_dict_to_keydict

Fix a conversion error when nvim_get_win_config gets called from lua,
where Float values "x" and "y" didn't get converted to lua numbers.
This commit is contained in:
bfredl
2024-01-31 22:02:06 +01:00
parent 24d26b4cd1
commit f9d81c43d2
10 changed files with 252 additions and 115 deletions

View File

@@ -1014,6 +1014,54 @@ bool api_dict_to_keydict(void *retval, FieldHashfn hashy, Dictionary dict, Error
return true;
}
Dictionary api_keydict_to_dict(void *value, KeySetLink *table, size_t max_size, Arena *arena)
{
Dictionary rv = arena_dict(arena, max_size);
for (size_t i = 0; table[i].str; i++) {
KeySetLink *field = &table[i];
bool is_set = true;
if (field->opt_index >= 0) {
OptKeySet *ks = (OptKeySet *)value;
is_set = ks->is_set_ & (1ULL << field->opt_index);
}
if (!is_set) {
continue;
}
char *mem = ((char *)value + field->ptr_off);
Object val = NIL;
if (field->type == kObjectTypeNil) {
val = *(Object *)mem;
} else if (field->type == kObjectTypeInteger) {
val = INTEGER_OBJ(*(Integer *)mem);
} else if (field->type == kObjectTypeFloat) {
val = FLOAT_OBJ(*(Float *)mem);
} else if (field->type == kObjectTypeBoolean) {
val = BOOLEAN_OBJ(*(Boolean *)mem);
} else if (field->type == kObjectTypeString) {
val = STRING_OBJ(*(String *)mem);
} else if (field->type == kObjectTypeArray) {
val = ARRAY_OBJ(*(Array *)mem);
} else if (field->type == kObjectTypeDictionary) {
val = DICTIONARY_OBJ(*(Dictionary *)mem);
} else if (field->type == kObjectTypeBuffer || field->type == kObjectTypeWindow
|| field->type == kObjectTypeTabpage) {
val.data.integer = *(Integer *)mem;
val.type = field->type;
} else if (field->type == kObjectTypeLuaRef) {
// do nothing
} else {
abort();
}
PUT_C(rv, field->str, val);
}
return rv;
}
void api_free_keydict(void *dict, KeySetLink *table)
{
for (size_t i = 0; table[i].str; i++) {