eval/decode: Replace INIT_SPECIAL_DICT macros with inline function

This commit is contained in:
ZyX
2016-02-03 19:56:37 +03:00
parent 700b32a2b3
commit ed6756563c

View File

@@ -25,6 +25,34 @@ typedef kvec_t(ContainerStackItem) ContainerStack;
# include "eval/decode.c.generated.h" # include "eval/decode.c.generated.h"
#endif #endif
/// Create special dictionary
///
/// @param[out] rettv Location where created dictionary will be saved.
/// @param[in] type Type of the dictionary.
/// @param[in] val Value associated with the _VAL key.
static inline void create_special_dict(typval_T *const rettv,
const MessagePackType type,
typval_T val)
FUNC_ATTR_NONNULL_ALL
{
dict_T *const dict = dict_alloc();
dictitem_T *const type_di = dictitem_alloc((char_u *) "_TYPE");
type_di->di_tv.v_type = VAR_LIST;
type_di->di_tv.v_lock = 0;
type_di->di_tv.vval.v_list = (list_T *) eval_msgpack_type_lists[type];
type_di->di_tv.vval.v_list->lv_refcount++;
dict_add(dict, type_di);
dictitem_T *const val_di = dictitem_alloc((char_u *) "_VAL");
val_di->di_tv = val;
dict_add(dict, val_di);
dict->dv_refcount++;
*rettv = (typval_T) {
.v_type = VAR_DICT,
.v_lock = VAR_UNLOCKED,
.vval = { .v_dict = dict },
};
}
/// Helper function used for working with stack vectors used by JSON decoder /// Helper function used for working with stack vectors used by JSON decoder
/// ///
/// @param[in] obj New object. /// @param[in] obj New object.
@@ -521,25 +549,9 @@ json_decode_string_ret:
int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv) int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
#define INIT_SPECIAL_DICT(tv, type, val) \
do { \
dict_T *const dict = dict_alloc(); \
dictitem_T *const type_di = dictitem_alloc((char_u *) "_TYPE"); \
type_di->di_tv.v_type = VAR_LIST; \
type_di->di_tv.v_lock = 0; \
type_di->di_tv.vval.v_list = (list_T *) eval_msgpack_type_lists[type]; \
type_di->di_tv.vval.v_list->lv_refcount++; \
dict_add(dict, type_di); \
dictitem_T *const val_di = dictitem_alloc((char_u *) "_VAL"); \
val_di->di_tv = val; \
dict_add(dict, val_di); \
tv->v_type = VAR_DICT; \
dict->dv_refcount++; \
tv->vval.v_dict = dict; \
} while (0)
switch (mobj.type) { switch (mobj.type) {
case MSGPACK_OBJECT_NIL: { case MSGPACK_OBJECT_NIL: {
INIT_SPECIAL_DICT(rettv, kMPNil, ((typval_T) { create_special_dict(rettv, kMPNil, ((typval_T) {
.v_type = VAR_NUMBER, .v_type = VAR_NUMBER,
.v_lock = 0, .v_lock = 0,
.vval = { .v_number = 0 }, .vval = { .v_number = 0 },
@@ -547,8 +559,7 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
break; break;
} }
case MSGPACK_OBJECT_BOOLEAN: { case MSGPACK_OBJECT_BOOLEAN: {
INIT_SPECIAL_DICT(rettv, kMPBoolean, create_special_dict(rettv, kMPBoolean, ((typval_T) {
((typval_T) {
.v_type = VAR_NUMBER, .v_type = VAR_NUMBER,
.v_lock = 0, .v_lock = 0,
.vval = { .vval = {
@@ -567,8 +578,7 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
} else { } else {
list_T *const list = list_alloc(); list_T *const list = list_alloc();
list->lv_refcount++; list->lv_refcount++;
INIT_SPECIAL_DICT(rettv, kMPInteger, create_special_dict(rettv, kMPInteger, ((typval_T) {
((typval_T) {
.v_type = VAR_LIST, .v_type = VAR_LIST,
.v_lock = 0, .v_lock = 0,
.vval = { .v_list = list }, .vval = { .v_list = list },
@@ -591,8 +601,7 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
} else { } else {
list_T *const list = list_alloc(); list_T *const list = list_alloc();
list->lv_refcount++; list->lv_refcount++;
INIT_SPECIAL_DICT(rettv, kMPInteger, create_special_dict(rettv, kMPInteger, ((typval_T) {
((typval_T) {
.v_type = VAR_LIST, .v_type = VAR_LIST,
.v_lock = 0, .v_lock = 0,
.vval = { .v_list = list }, .vval = { .v_list = list },
@@ -616,8 +625,7 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
case MSGPACK_OBJECT_STR: { case MSGPACK_OBJECT_STR: {
list_T *const list = list_alloc(); list_T *const list = list_alloc();
list->lv_refcount++; list->lv_refcount++;
INIT_SPECIAL_DICT(rettv, kMPString, create_special_dict(rettv, kMPString, ((typval_T) {
((typval_T) {
.v_type = VAR_LIST, .v_type = VAR_LIST,
.v_lock = 0, .v_lock = 0,
.vval = { .v_list = list }, .vval = { .v_list = list },
@@ -639,8 +647,7 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
} }
list_T *const list = list_alloc(); list_T *const list = list_alloc();
list->lv_refcount++; list->lv_refcount++;
INIT_SPECIAL_DICT(rettv, kMPBinary, create_special_dict(rettv, kMPBinary, ((typval_T) {
((typval_T) {
.v_type = VAR_LIST, .v_type = VAR_LIST,
.v_lock = 0, .v_lock = 0,
.vval = { .v_list = list }, .vval = { .v_list = list },
@@ -705,8 +712,7 @@ int msgpack_to_vim(const msgpack_object mobj, typval_T *const rettv)
msgpack_to_vim_generic_map: {} msgpack_to_vim_generic_map: {}
list_T *const list = list_alloc(); list_T *const list = list_alloc();
list->lv_refcount++; list->lv_refcount++;
INIT_SPECIAL_DICT(rettv, kMPMap, create_special_dict(rettv, kMPMap, ((typval_T) {
((typval_T) {
.v_type = VAR_LIST, .v_type = VAR_LIST,
.v_lock = 0, .v_lock = 0,
.vval = { .v_list = list }, .vval = { .v_list = list },
@@ -735,8 +741,7 @@ msgpack_to_vim_generic_map: {}
list_append_number(list, mobj.via.ext.type); list_append_number(list, mobj.via.ext.type);
list_T *const ext_val_list = list_alloc(); list_T *const ext_val_list = list_alloc();
list_append_list(list, ext_val_list); list_append_list(list, ext_val_list);
INIT_SPECIAL_DICT(rettv, kMPExt, create_special_dict(rettv, kMPExt, ((typval_T) {
((typval_T) {
.v_type = VAR_LIST, .v_type = VAR_LIST,
.v_lock = 0, .v_lock = 0,
.vval = { .v_list = list }, .vval = { .v_list = list },
@@ -748,6 +753,5 @@ msgpack_to_vim_generic_map: {}
break; break;
} }
} }
#undef INIT_SPECIAL_DICT
return OK; return OK;
} }