diff options
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r-- | src/nvim/eval.c | 245 |
1 files changed, 240 insertions, 5 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 8c8881b398..420a712e3e 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -79,6 +79,7 @@ #include "nvim/event/pty_process.h" #include "nvim/event/rstream.h" #include "nvim/event/wstream.h" +#include "nvim/event/time.h" #include "nvim/os/time.h" #include "nvim/msgpack_rpc/channel.h" #include "nvim/msgpack_rpc/server.h" @@ -428,6 +429,14 @@ typedef struct { int status; } JobEvent; +typedef struct { + TimeWatcher tw; + int timer_id; + int repeat_count; + bool stopped; + ufunc_T *callback; +} timer_T; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "eval.c.generated.h" #endif @@ -438,6 +447,9 @@ typedef struct { static uint64_t current_job_id = 1; static PMap(uint64_t) *jobs = NULL; +static uint64_t last_timer_id = 0; +static PMap(uint64_t) *timers = NULL; + static const char *const msgpack_type_names[] = { [kMPNil] = "nil", [kMPBoolean] = "boolean", @@ -469,6 +481,7 @@ void eval_init(void) vimvars[VV_VERSION].vv_nr = VIM_VERSION_100; jobs = pmap_new(uint64_t)(); + timers = pmap_new(uint64_t)(); struct vimvar *p; init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE); @@ -6667,6 +6680,7 @@ static struct fst { { "asin", 1, 1, f_asin }, // WJMc { "assert_equal", 2, 3, f_assert_equal }, { "assert_exception", 1, 2, f_assert_exception }, + { "assert_fails", 1, 2, f_assert_fails }, { "assert_false", 1, 2, f_assert_false }, { "assert_true", 1, 2, f_assert_true }, { "atan", 1, 1, f_atan }, @@ -6877,6 +6891,7 @@ static struct fst { { "setbufvar", 3, 3, f_setbufvar }, { "setcharsearch", 1, 1, f_setcharsearch }, { "setcmdpos", 1, 1, f_setcmdpos }, + { "setfperm", 2, 2, f_setfperm }, { "setline", 2, 2, f_setline }, { "setloclist", 2, 4, f_setloclist }, { "setmatches", 1, 1, f_setmatches }, @@ -6929,6 +6944,8 @@ static struct fst { { "tempname", 0, 0, f_tempname }, { "termopen", 1, 2, f_termopen }, { "test", 1, 1, f_test }, + { "timer_start", 2, 3, f_timer_start }, + { "timer_stop", 1, 1, f_timer_stop }, { "tolower", 1, 1, f_tolower }, { "toupper", 1, 1, f_toupper }, { "tr", 3, 3, f_tr }, @@ -7651,6 +7668,43 @@ static void f_assert_exception(typval_T *argvars, typval_T *rettv) } } +/// "assert_fails(cmd [, error])" function +static void f_assert_fails(typval_T *argvars, typval_T *rettv) +{ + char_u *cmd = get_tv_string_chk(&argvars[0]); + garray_T ga; + + called_emsg = false; + suppress_errthrow = true; + emsg_silent = true; + do_cmdline_cmd((char *)cmd); + if (!called_emsg) { + prepare_assert_error(&ga); + ga_concat(&ga, (char_u *)"command did not fail: "); + ga_concat(&ga, cmd); + assert_error(&ga); + ga_clear(&ga); + } else if (argvars[1].v_type != VAR_UNKNOWN) { + char_u buf[NUMBUFLEN]; + char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf); + + if (error == NULL + || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) { + prepare_assert_error(&ga); + fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], + &vimvars[VV_ERRMSG].vv_tv); + assert_error(&ga); + ga_clear(&ga); + } + } + + called_emsg = false; + suppress_errthrow = false; + emsg_silent = false; + emsg_on_display = false; + set_vim_var_string(VV_ERRMSG, NULL, 0); +} + // Common for assert_true() and assert_false(). static void assert_bool(typval_T *argvars, bool is_true) { @@ -10687,6 +10741,7 @@ static void f_has(typval_T *argvars, typval_T *rettv) "termguicolors", "termresponse", "textobjects", + "timers", "title", "user-commands", /* was accidentally included in 5.4 */ "user_commands", @@ -14446,6 +14501,38 @@ static void f_setcmdpos(typval_T *argvars, typval_T *rettv) rettv->vval.v_number = set_cmdline_pos(pos); } + +/// "setfperm({fname}, {mode})" function +static void f_setfperm(typval_T *argvars, typval_T *rettv) +{ + rettv->vval.v_number = 0; + + char_u *fname = get_tv_string_chk(&argvars[0]); + if (fname == NULL) { + return; + } + + char_u modebuf[NUMBUFLEN]; + char_u *mode_str = get_tv_string_buf_chk(&argvars[1], modebuf); + if (mode_str == NULL) { + return; + } + if (STRLEN(mode_str) != 9) { + EMSG2(_(e_invarg2), mode_str); + return; + } + + int mask = 1; + int mode = 0; + for (int i = 8; i >= 0; i--) { + if (mode_str[i] != '-') { + mode |= mask; + } + mask = mask << 1; + } + rettv->vval.v_number = os_setperm(fname, mode) == OK; +} + /* * "setline()" function */ @@ -16408,6 +16495,119 @@ static void f_tanh(typval_T *argvars, typval_T *rettv) float_op_wrapper(argvars, rettv, &tanh); } + +/// "timer_start(timeout, callback, opts)" function +static void f_timer_start(typval_T *argvars, typval_T *rettv) +{ + long timeout = get_tv_number(&argvars[0]); + timer_T *timer; + int repeat = 1; + dict_T *dict; + + rettv->vval.v_number = -1; + + if (argvars[2].v_type != VAR_UNKNOWN) { + if (argvars[2].v_type != VAR_DICT + || (dict = argvars[2].vval.v_dict) == NULL) { + EMSG2(_(e_invarg2), get_tv_string(&argvars[2])); + return; + } + if (dict_find(dict, (char_u *)"repeat", -1) != NULL) { + repeat = get_dict_number(dict, (char_u *)"repeat"); + } + } + + if (argvars[1].v_type != VAR_FUNC && argvars[1].v_type != VAR_STRING) { + EMSG2(e_invarg2, "funcref"); + return; + } + ufunc_T *func = find_ufunc(argvars[1].vval.v_string); + if (!func) { + // Invalid function name. Error already reported by `find_ufunc`. + return; + } + func->uf_refcount++; + + timer = xmalloc(sizeof *timer); + timer->stopped = false; + timer->repeat_count = repeat; + timer->timer_id = last_timer_id++; + timer->callback = func; + + time_watcher_init(&loop, &timer->tw, timer); + timer->tw.events = queue_new_child(loop.events); + // if main loop is blocked, don't queue up multiple events + timer->tw.blockable = true; + time_watcher_start(&timer->tw, timer_due_cb, timeout, + timeout * (repeat != 1)); + + pmap_put(uint64_t)(timers, timer->timer_id, timer); + rettv->vval.v_number = timer->timer_id; +} + + +// "timer_stop(timerid)" function +static void f_timer_stop(typval_T *argvars, typval_T *rettv) +{ + if (argvars[0].v_type != VAR_NUMBER) { + EMSG(_(e_number_exp)); + return; + } + + timer_T *timer = pmap_get(uint64_t)(timers, get_tv_number(&argvars[0])); + + if (timer == NULL) { + return; + } + + timer_stop(timer); +} + +// invoked on the main loop +static void timer_due_cb(TimeWatcher *tw, void *data) +{ + timer_T *timer = (timer_T *)data; + if (timer->stopped) { + return; + } + // if repeat was negative repeat forever + if (timer->repeat_count >= 0 && --timer->repeat_count == 0) { + timer_stop(timer); + } + + typval_T argv[1]; + init_tv(argv); + argv[0].v_type = VAR_NUMBER; + argv[0].vval.v_number = timer->timer_id; + typval_T rettv; + + init_tv(&rettv); + call_user_func(timer->callback, ARRAY_SIZE(argv), argv, &rettv, + curwin->w_cursor.lnum, curwin->w_cursor.lnum, NULL); + clear_tv(&rettv); +} + +static void timer_stop(timer_T *timer) +{ + if (timer->stopped) { + // avoid double free + return; + } + timer->stopped = true; + time_watcher_stop(&timer->tw); + time_watcher_close(&timer->tw, timer_free_cb); +} + +// invoked on next event loop tick, so queue is empty +static void timer_free_cb(TimeWatcher *tw, void *data) +{ + timer_T *timer = (timer_T *)data; + queue_free(timer->tw.events); + user_func_unref(timer->callback); + pmap_del(uint64_t)(timers, timer->timer_id); + xfree(timer); +} + /* * "tolower(string)" function */ @@ -18123,6 +18323,25 @@ static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, in return HI2DI(hi); } +// Get function call environment based on backtrace debug level +static funccall_T *get_funccal(void) +{ + funccall_T *funccal = current_funccal; + if (debug_backtrace_level > 0) { + for (int i = 0; i < debug_backtrace_level; i++) { + funccall_T *temp_funccal = funccal->caller; + if (temp_funccal) { + funccal = temp_funccal; + } else { + // backtrace level overflow. reset to max + debug_backtrace_level = i; + } + } + } + + return funccal; +} + // Find the dict and hashtable used for a variable name. Set "varname" to the // start of name without ':'. static hashtab_T *find_var_ht_dict(char_u *name, uint8_t **varname, dict_T **d) @@ -18147,7 +18366,11 @@ static hashtab_T *find_var_ht_dict(char_u *name, uint8_t **varname, dict_T **d) return &compat_hashtab; } - *d = current_funccal ? ¤t_funccal->l_vars : &globvardict; + if (current_funccal == NULL) { + *d = &globvardict; + } else { + *d = &get_funccal()->l_vars; // l: variable + } goto end; } @@ -18169,9 +18392,9 @@ static hashtab_T *find_var_ht_dict(char_u *name, uint8_t **varname, dict_T **d) } else if (*name == 'v') { // v: variable *d = &vimvardict; } else if (*name == 'a' && current_funccal != NULL) { // function argument - *d = ¤t_funccal->l_avars; + *d = &get_funccal()->l_avars; } else if (*name == 'l' && current_funccal != NULL) { // local variable - *d = ¤t_funccal->l_vars; + *d = &get_funccal()->l_vars; } else if (*name == 's' // script variable && current_SID > 0 && current_SID <= ga_scripts.ga_len) { *d = &SCRIPT_SV(current_SID)->sv_dict; @@ -21701,6 +21924,18 @@ static void term_resize(uint16_t width, uint16_t height, void *d) pty_process_resize(&data->proc.pty, width, height); } +static inline void term_delayed_free(void **argv) +{ + TerminalJobData *j = argv[0]; + if (j->in.pending_reqs || j->out.pending_reqs || j->err.pending_reqs) { + queue_put(j->events, term_delayed_free, 1, j); + return; + } + + terminal_destroy(j->term); + term_job_data_decref(j); +} + static void term_close(void *d) { TerminalJobData *data = d; @@ -21708,8 +21943,7 @@ static void term_close(void *d) data->exited = true; process_stop((Process *)&data->proc); } - terminal_destroy(data->term); - term_job_data_decref(d); + queue_put(data->events, term_delayed_free, 1, data); } static void term_job_data_decref(TerminalJobData *data) @@ -21784,6 +22018,7 @@ static void script_host_eval(char *name, typval_T *argvars, typval_T *rettv) if (argvars[0].v_type != VAR_STRING) { EMSG(_(e_invarg)); + return; } list_T *args = list_alloc(); |