refactor: object_to_vim() cannot fail

Since the parent commit, object_to_vim() can't fail, so callers don't
need to check its result.
This commit is contained in:
Justin M. Keyes
2023-12-07 13:01:42 +01:00
parent cca6c4c698
commit a16218d4c6
7 changed files with 15 additions and 46 deletions

View File

@@ -258,9 +258,7 @@ Object vim_to_object(typval_T *obj)
/// @param tv Conversion result is placed here. On failure member v_type is /// @param tv Conversion result is placed here. On failure member v_type is
/// set to VAR_UNKNOWN (no allocation was made for this variable). /// set to VAR_UNKNOWN (no allocation was made for this variable).
/// @param err Error object. /// @param err Error object.
/// void object_to_vim(Object obj, typval_T *tv, Error *err)
/// @returns true if conversion is successful, otherwise false.
bool object_to_vim(Object obj, typval_T *tv, Error *err)
{ {
tv->v_type = VAR_UNKNOWN; tv->v_type = VAR_UNKNOWN;
tv->v_lock = VAR_UNLOCKED; tv->v_lock = VAR_UNLOCKED;
@@ -307,12 +305,7 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
for (uint32_t i = 0; i < obj.data.array.size; i++) { for (uint32_t i = 0; i < obj.data.array.size; i++) {
Object item = obj.data.array.items[i]; Object item = obj.data.array.items[i];
typval_T li_tv; typval_T li_tv;
object_to_vim(item, &li_tv, err);
if (!object_to_vim(item, &li_tv, err)) {
tv_list_free(list);
return false;
}
tv_list_append_owned_tv(list, li_tv); tv_list_append_owned_tv(list, li_tv);
} }
tv_list_ref(list); tv_list_ref(list);
@@ -329,14 +322,7 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
KeyValuePair item = obj.data.dictionary.items[i]; KeyValuePair item = obj.data.dictionary.items[i];
String key = item.key; String key = item.key;
dictitem_T *const di = tv_dict_item_alloc(key.data); dictitem_T *const di = tv_dict_item_alloc(key.data);
object_to_vim(item.value, &di->di_tv, err);
if (!object_to_vim(item.value, &di->di_tv, err)) {
// cleanup
tv_dict_item_free(di);
tv_dict_free(dict);
return false;
}
tv_dict_add(dict, di); tv_dict_add(dict, di);
} }
dict->dv_refcount++; dict->dv_refcount++;
@@ -353,6 +339,4 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
break; break;
} }
} }
return true;
} }

View File

@@ -253,9 +253,7 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, bool retva
typval_T tv; typval_T tv;
// Convert the object to a vimscript type in the temporary variable // Convert the object to a vimscript type in the temporary variable
if (!object_to_vim(value, &tv, err)) { object_to_vim(value, &tv, err);
return rv;
}
typval_T oldtv = TV_INITIAL_VALUE; typval_T oldtv = TV_INITIAL_VALUE;

View File

@@ -207,9 +207,7 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
typval_T vim_args[MAX_FUNC_ARGS + 1]; typval_T vim_args[MAX_FUNC_ARGS + 1];
size_t i = 0; // also used for freeing the variables size_t i = 0; // also used for freeing the variables
for (; i < args.size; i++) { for (; i < args.size; i++) {
if (!object_to_vim(args.items[i], &vim_args[i], err)) { object_to_vim(args.items[i], &vim_args[i], err);
goto free_vim_args;
}
} }
// Initialize `force_abort` and `suppress_errthrow` at the top level. // Initialize `force_abort` and `suppress_errthrow` at the top level.
@@ -243,7 +241,6 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
tv_clear(&rettv); tv_clear(&rettv);
recursive--; recursive--;
free_vim_args:
while (i > 0) { while (i > 0) {
tv_clear(&vim_args[--i]); tv_clear(&vim_args[--i]);
} }
@@ -297,9 +294,7 @@ Object nvim_call_dict_function(Object dict, String fn, Array args, Error *err)
mustfree = true; mustfree = true;
break; break;
case kObjectTypeDictionary: case kObjectTypeDictionary:
if (!object_to_vim(dict, &rettv, err)) { object_to_vim(dict, &rettv, err);
goto end;
}
break; break;
default: default:
api_set_error(err, kErrorTypeValidation, api_set_error(err, kErrorTypeValidation,

View File

@@ -233,7 +233,7 @@ void channel_create_event(Channel *chan, const char *ext_source)
typval_T tv = TV_INITIAL_VALUE; typval_T tv = TV_INITIAL_VALUE;
// TODO(bfredl): do the conversion in one step. Also would be nice // TODO(bfredl): do the conversion in one step. Also would be nice
// to pretty print top level dict in defined order // to pretty print top level dict in defined order
(void)object_to_vim(DICTIONARY_OBJ(info), &tv, NULL); object_to_vim(DICTIONARY_OBJ(info), &tv, NULL);
assert(tv.v_type == VAR_DICT); assert(tv.v_type == VAR_DICT);
char *str = encode_tv2json(&tv, NULL); char *str = encode_tv2json(&tv, NULL);
ILOG("new channel %" PRIu64 " (%s) : %s", chan->id, source, str); ILOG("new channel %" PRIu64 " (%s) : %s", chan->id, source, str);
@@ -865,7 +865,7 @@ static void set_info_event(void **argv)
dict_T *dict = get_v_event(&save_v_event); dict_T *dict = get_v_event(&save_v_event);
Dictionary info = channel_info(chan->id); Dictionary info = channel_info(chan->id);
typval_T retval; typval_T retval;
(void)object_to_vim(DICTIONARY_OBJ(info), &retval, NULL); object_to_vim(DICTIONARY_OBJ(info), &retval, NULL);
assert(retval.v_type == VAR_DICT); assert(retval.v_type == VAR_DICT);
tv_dict_add_dict(dict, S_LEN("info"), retval.vval.v_dict); tv_dict_add_dict(dict, S_LEN("info"), retval.vval.v_dict);
tv_dict_set_keys_readonly(dict); tv_dict_set_keys_readonly(dict);

View File

@@ -326,9 +326,7 @@ static inline msgpack_sbuffer array_to_sbuf(Array array, Error *err)
msgpack_sbuffer_init(&sbuf); msgpack_sbuffer_init(&sbuf);
typval_T list_tv; typval_T list_tv;
if (!object_to_vim(ARRAY_OBJ(array), &list_tv, err)) { object_to_vim(ARRAY_OBJ(array), &list_tv, err);
return sbuf;
}
assert(list_tv.v_type == VAR_LIST); assert(list_tv.v_type == VAR_LIST);
if (!encode_vim_list_to_buf(list_tv.vval.v_list, &sbuf.size, &sbuf.data)) { if (!encode_vim_list_to_buf(list_tv.vval.v_list, &sbuf.size, &sbuf.data)) {

View File

@@ -355,10 +355,7 @@ static void api_wrapper(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
goto end; goto end;
} }
if (!object_to_vim(result, rettv, &err)) { object_to_vim(result, rettv, &err);
assert(ERROR_SET(&err));
semsg(_("Error converting the call result: %s"), err.msg);
}
end: end:
api_free_array(args); api_free_array(args);
@@ -428,7 +425,7 @@ static void f_and(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
static void f_api_info(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) static void f_api_info(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{ {
Dictionary metadata = api_metadata(); Dictionary metadata = api_metadata();
(void)object_to_vim(DICTIONARY_OBJ(metadata), rettv, NULL); object_to_vim(DICTIONARY_OBJ(metadata), rettv, NULL);
} }
/// "atan2()" function /// "atan2()" function
@@ -1023,7 +1020,7 @@ static void f_ctxget(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
Dictionary ctx_dict = ctx_to_dict(ctx); Dictionary ctx_dict = ctx_to_dict(ctx);
Error err = ERROR_INIT; Error err = ERROR_INIT;
(void)object_to_vim(DICTIONARY_OBJ(ctx_dict), rettv, &err); object_to_vim(DICTIONARY_OBJ(ctx_dict), rettv, &err);
api_free_dictionary(ctx_dict); api_free_dictionary(ctx_dict);
api_clear_error(&err); api_clear_error(&err);
} }
@@ -6751,10 +6748,7 @@ static void f_rpcrequest(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
goto end; goto end;
} }
if (!object_to_vim(result, rettv, &err)) { object_to_vim(result, rettv, &err);
assert(ERROR_SET(&err));
semsg(_("Error converting the call result: %s"), err.msg);
}
end: end:
arena_mem_free(res_mem); arena_mem_free(res_mem);

View File

@@ -2199,7 +2199,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Dictionary dict = mapblock_fill_dict(mp, Dictionary dict = mapblock_fill_dict(mp,
did_simplify ? keys_simplified : NULL, did_simplify ? keys_simplified : NULL,
buffer_local, abbr, true); buffer_local, abbr, true);
(void)object_to_vim(DICTIONARY_OBJ(dict), rettv, NULL); object_to_vim(DICTIONARY_OBJ(dict), rettv, NULL);
api_free_dictionary(dict); api_free_dictionary(dict);
} else { } else {
// Return an empty dictionary. // Return an empty dictionary.
@@ -2407,7 +2407,7 @@ void f_maplist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
did_simplify ? keys_buf : NULL, did_simplify ? keys_buf : NULL,
buffer_local, abbr, true); buffer_local, abbr, true);
typval_T d = TV_INITIAL_VALUE; typval_T d = TV_INITIAL_VALUE;
(void)object_to_vim(DICTIONARY_OBJ(dict), &d, NULL); object_to_vim(DICTIONARY_OBJ(dict), &d, NULL);
assert(d.v_type == VAR_DICT); assert(d.v_type == VAR_DICT);
tv_list_append_dict(rettv->vval.v_list, d.vval.v_dict); tv_list_append_dict(rettv->vval.v_list, d.vval.v_dict);
api_free_dictionary(dict); api_free_dictionary(dict);