refactor: do more in TRY_WRAP

This commit is contained in:
Lewis Russell
2023-03-22 10:09:28 +00:00
committed by GitHub
parent c45b5e2c5b
commit 3285cd6ecc
8 changed files with 78 additions and 85 deletions

View File

@@ -683,11 +683,9 @@ Integer nvim_create_augroup(uint64_t channel_id, String name, Dict(create_augrou
void nvim_del_augroup_by_id(Integer id, Error *err)
FUNC_API_SINCE(9)
{
TRY_WRAP({
try_start();
TRY_WRAP(err, {
char *name = augroup_name((int)id);
augroup_del(name, false);
try_end(err);
});
}
@@ -700,10 +698,8 @@ void nvim_del_augroup_by_id(Integer id, Error *err)
void nvim_del_augroup_by_name(String name, Error *err)
FUNC_API_SINCE(9)
{
TRY_WRAP({
try_start();
TRY_WRAP(err, {
augroup_del(name.data, false);
try_end(err);
});
}

View File

@@ -697,8 +697,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
capture_ga = &capture_local;
}
TRY_WRAP({
try_start();
TRY_WRAP(err, {
if (output) {
msg_silent++;
msg_col = 0; // prevent leading spaces
@@ -714,8 +713,6 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
// Put msg_col back where it was, since nothing should have been written.
msg_col = save_msg_col;
}
try_end(err);
});
if (ERROR_SET(err)) {

View File

@@ -114,10 +114,8 @@ static buf_T *do_ft_buf(char *filetype, aco_save_T *aco, Error *err)
ftbuf->b_p_ft = xstrdup(filetype);
TRY_WRAP({
try_start();
TRY_WRAP(err, {
apply_autocmds(EVENT_FILETYPE, ftbuf->b_p_ft, ftbuf->b_fname, true, ftbuf);
try_end(err);
});
return ftbuf;

View File

@@ -149,13 +149,15 @@ typedef struct {
// which would otherwise be ignored. This pattern is from do_cmdline().
//
// TODO(bfredl): prepare error-handling at "top level" (nv_event).
#define TRY_WRAP(code) \
#define TRY_WRAP(err, code) \
do { \
msglist_T **saved_msg_list = msg_list; \
msglist_T *private_msg_list; \
msg_list = &private_msg_list; \
private_msg_list = NULL; \
try_start(); \
code; \
try_end(err); \
msg_list = saved_msg_list; /* Restore the exception context. */ \
} while (0)

View File

@@ -538,10 +538,8 @@ ArrayOf(String) nvim_get_runtime_file(String name, Boolean all, Error *err)
int flags = DIP_DIRFILE | (all ? DIP_ALL : 0);
TRY_WRAP({
try_start();
TRY_WRAP(err, {
do_in_runtimepath((name.size ? name.data : ""), flags, find_runtime_cb, &rv);
try_end(err);
});
return rv;
}
@@ -1238,14 +1236,12 @@ void nvim_put(ArrayOf(String) lines, String type, Boolean after, Boolean follow,
finish_yankreg_from_object(reg, false);
TRY_WRAP({
try_start();
TRY_WRAP(err, {
bool VIsual_was_active = VIsual_active;
msg_silent++; // Avoid "N more lines" message.
do_put(0, reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0);
msg_silent--;
VIsual_active = VIsual_was_active;
try_end(err);
});
cleanup:

View File

@@ -137,35 +137,37 @@ Object nvim_eval(String expr, Error *err)
static int recursive = 0; // recursion depth
Object rv = OBJECT_INIT;
TRY_WRAP({
// Initialize `force_abort` and `suppress_errthrow` at the top level.
if (!recursive) {
force_abort = false;
suppress_errthrow = false;
did_throw = false;
// `did_emsg` is set by emsg(), which cancels execution.
did_emsg = false;
}
recursive++;
try_start();
// Initialize `force_abort` and `suppress_errthrow` at the top level.
if (!recursive) {
force_abort = false;
suppress_errthrow = false;
did_throw = false;
// `did_emsg` is set by emsg(), which cancels execution.
did_emsg = false;
}
typval_T rettv;
int ok = eval0(expr.data, &rettv, NULL, true);
recursive++;
if (!try_end(err)) {
if (ok == FAIL) {
// Should never happen, try_end() should get the error. #8371
api_set_error(err, kErrorTypeException,
"Failed to evaluate expression: '%.*s'", 256, expr.data);
} else {
rv = vim_to_object(&rettv);
}
}
typval_T rettv;
int ok;
tv_clear(&rettv);
recursive--;
TRY_WRAP(err, {
ok = eval0(expr.data, &rettv, NULL, true);
});
if (!ERROR_SET(err)) {
if (ok == FAIL) {
// Should never happen, try_end() (in TRY_WRAP) should get the error. #8371
api_set_error(err, kErrorTypeException,
"Failed to evaluate expression: '%.*s'", 256, expr.data);
} else {
rv = vim_to_object(&rettv);
}
}
tv_clear(&rettv);
recursive--;
return rv;
}
@@ -196,34 +198,37 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
}
}
TRY_WRAP({
// Initialize `force_abort` and `suppress_errthrow` at the top level.
if (!recursive) {
force_abort = false;
suppress_errthrow = false;
did_throw = false;
// `did_emsg` is set by emsg(), which cancels execution.
did_emsg = false;
}
recursive++;
try_start();
typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT;
funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = true;
funcexe.fe_selfdict = self;
// Initialize `force_abort` and `suppress_errthrow` at the top level.
if (!recursive) {
force_abort = false;
suppress_errthrow = false;
did_throw = false;
// `did_emsg` is set by emsg(), which cancels execution.
did_emsg = false;
}
recursive++;
typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT;
funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = true;
funcexe.fe_selfdict = self;
TRY_WRAP(err, {
// call_func() retval is deceptive, ignore it. Instead we set `msg_list`
// (see above) to capture abort-causing non-exception errors.
(void)call_func(fn.data, (int)fn.size, &rettv, (int)args.size,
vim_args, &funcexe);
if (!try_end(err)) {
rv = vim_to_object(&rettv);
}
tv_clear(&rettv);
recursive--;
});
if (!ERROR_SET(err)) {
rv = vim_to_object(&rettv);
}
tv_clear(&rettv);
recursive--;
free_vim_args:
while (i > 0) {
tv_clear(&vim_args[--i]);

View File

@@ -1157,28 +1157,29 @@ int nlua_call(lua_State *lstate)
}
}
TRY_WRAP({
// TODO(bfredl): this should be simplified in error handling refactor
force_abort = false;
suppress_errthrow = false;
did_throw = false;
did_emsg = false;
// TODO(bfredl): this should be simplified in error handling refactor
force_abort = false;
suppress_errthrow = false;
did_throw = false;
did_emsg = false;
try_start();
typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT;
funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = true;
typval_T rettv;
funcexe_T funcexe = FUNCEXE_INIT;
funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = true;
TRY_WRAP(&err, {
// call_func() retval is deceptive, ignore it. Instead we set `msg_list`
// (TRY_WRAP) to capture abort-causing non-exception errors.
(void)call_func((char *)name, (int)name_len, &rettv, nargs, vim_args, &funcexe);
if (!try_end(&err)) {
nlua_push_typval(lstate, &rettv, false);
}
tv_clear(&rettv);
});
if (!ERROR_SET(&err)) {
nlua_push_typval(lstate, &rettv, false);
}
tv_clear(&rettv);
free_vim_args:
while (i > 0) {
tv_clear(&vim_args[--i]);

View File

@@ -287,10 +287,8 @@ int nlua_regex(lua_State *lstate)
const char *text = luaL_checkstring(lstate, 1);
regprog_T *prog = NULL;
TRY_WRAP({
try_start();
TRY_WRAP(&err, {
prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT);
try_end(&err);
});
if (ERROR_SET(&err)) {