Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * vim9execute.c: execute Vim9 script instructions |
| 12 | */ |
| 13 | |
| 14 | #define USING_FLOAT_STUFF |
| 15 | #include "vim.h" |
| 16 | |
| 17 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 18 | |
| 19 | #ifdef VMS |
| 20 | # include <float.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "vim9.h" |
| 24 | |
| 25 | // Structure put on ec_trystack when ISN_TRY is encountered. |
| 26 | typedef struct { |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 27 | int tcd_frame_idx; // ec_frame_idx at ISN_TRY |
| 28 | int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 29 | int tcd_catch_idx; // instruction of the first :catch or :finally |
| 30 | int tcd_finally_idx; // instruction of the :finally block or zero |
| 31 | int tcd_endtry_idx; // instruction of the :endtry |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 32 | int tcd_caught; // catch block entered |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 33 | int tcd_cont; // :continue encountered, jump here (minus one) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 34 | int tcd_return; // when TRUE return from end of :finally |
| 35 | } trycmd_T; |
| 36 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 37 | // Data local to a function. |
| 38 | // On a function call, if not empty, is saved on the stack and restored when |
| 39 | // returning. |
| 40 | typedef struct { |
| 41 | int floc_restore_cmdmod; |
| 42 | cmdmod_T floc_save_cmdmod; |
| 43 | int floc_restore_cmdmod_stacklen; |
| 44 | } funclocal_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 45 | |
| 46 | // A stack is used to store: |
| 47 | // - arguments passed to a :def function |
| 48 | // - info about the calling function, to use when returning |
| 49 | // - local variables |
| 50 | // - temporary values |
| 51 | // |
| 52 | // In detail (FP == Frame Pointer): |
| 53 | // arg1 first argument from caller (if present) |
| 54 | // arg2 second argument from caller (if present) |
| 55 | // extra_arg1 any missing optional argument default value |
| 56 | // FP -> cur_func calling function |
| 57 | // current previous instruction pointer |
| 58 | // frame_ptr previous Frame Pointer |
| 59 | // var1 space for local variable |
| 60 | // var2 space for local variable |
| 61 | // .... fixed space for max. number of local variables |
| 62 | // temp temporary values |
| 63 | // .... flexible space for temporary values (can grow big) |
| 64 | |
| 65 | /* |
| 66 | * Execution context. |
| 67 | */ |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 68 | struct ectx_S { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 69 | garray_T ec_stack; // stack of typval_T values |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 70 | int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 71 | int ec_initial_frame_idx; // frame index when called |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 72 | |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 73 | outer_T *ec_outer; // outer scope used for closures, allocated |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 74 | funclocal_T ec_funclocal; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 75 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 76 | garray_T ec_trystack; // stack of trycmd_T values |
| 77 | int ec_in_catch; // when TRUE in catch or finally block |
| 78 | |
| 79 | int ec_dfunc_idx; // current function index |
| 80 | isn_T *ec_instr; // array with instructions |
| 81 | int ec_iidx; // index in ec_instr: instruction to execute |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 82 | |
| 83 | garray_T ec_funcrefs; // partials that might be a closure |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 84 | |
| 85 | int ec_did_emsg_before; |
| 86 | int ec_trylevel_at_start; |
| 87 | where_T ec_where; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 88 | }; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 89 | |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 90 | #ifdef FEAT_PROFILE |
| 91 | // stack of profinfo_T used when profiling. |
| 92 | static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL}; |
| 93 | #endif |
| 94 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 95 | // Get pointer to item relative to the bottom of the stack, -1 is the last one. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 96 | #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 97 | |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 98 | void |
| 99 | to_string_error(vartype_T vartype) |
| 100 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 101 | semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype)); |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 104 | /* |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 105 | * Return the number of arguments, including optional arguments and any vararg. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 106 | */ |
| 107 | static int |
| 108 | ufunc_argcount(ufunc_T *ufunc) |
| 109 | { |
| 110 | return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 111 | } |
| 112 | |
| 113 | /* |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 114 | * Create a new list from "count" items at the bottom of the stack. |
| 115 | * When "count" is zero an empty list is added to the stack. |
| 116 | */ |
| 117 | static int |
| 118 | exe_newlist(int count, ectx_T *ectx) |
| 119 | { |
| 120 | list_T *list = list_alloc_with_items(count); |
| 121 | int idx; |
| 122 | typval_T *tv; |
| 123 | |
| 124 | if (list == NULL) |
| 125 | return FAIL; |
| 126 | for (idx = 0; idx < count; ++idx) |
| 127 | list_set_item(list, idx, STACK_TV_BOT(idx - count)); |
| 128 | |
| 129 | if (count > 0) |
| 130 | ectx->ec_stack.ga_len -= count - 1; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 131 | else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 132 | return FAIL; |
| 133 | else |
| 134 | ++ectx->ec_stack.ga_len; |
| 135 | tv = STACK_TV_BOT(-1); |
| 136 | tv->v_type = VAR_LIST; |
| 137 | tv->vval.v_list = list; |
| 138 | ++list->lv_refcount; |
| 139 | return OK; |
| 140 | } |
| 141 | |
| 142 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 143 | * Call compiled function "cdf_idx" from compiled code. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 144 | * This adds a stack frame and sets the instruction pointer to the start of the |
| 145 | * called function. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 146 | * If "pt" is not null use "pt->pt_outer" for ec_outer. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 147 | * |
| 148 | * Stack has: |
| 149 | * - current arguments (already there) |
| 150 | * - omitted optional argument (default values) added here |
| 151 | * - stack frame: |
| 152 | * - pointer to calling function |
| 153 | * - Index of next instruction in calling function |
| 154 | * - previous frame pointer |
| 155 | * - reserved space for local variables |
| 156 | */ |
| 157 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 158 | call_dfunc( |
| 159 | int cdf_idx, |
| 160 | partial_T *pt, |
| 161 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 162 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 163 | { |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 164 | int argcount = argcount_arg; |
| 165 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; |
| 166 | ufunc_T *ufunc = dfunc->df_ufunc; |
| 167 | int arg_to_add; |
| 168 | int vararg_count = 0; |
| 169 | int varcount; |
| 170 | int idx; |
| 171 | estack_T *entry; |
| 172 | funclocal_T *floc = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 173 | |
| 174 | if (dfunc->df_deleted) |
| 175 | { |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 176 | // don't use ufunc->uf_name, it may have been freed |
| 177 | emsg_funcname(e_func_deleted, |
| 178 | dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 179 | return FAIL; |
| 180 | } |
| 181 | |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 182 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 183 | if (do_profiling == PROF_YES) |
| 184 | { |
| 185 | if (ga_grow(&profile_info_ga, 1) == OK) |
| 186 | { |
| 187 | profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data) |
| 188 | + profile_info_ga.ga_len; |
| 189 | ++profile_info_ga.ga_len; |
| 190 | CLEAR_POINTER(info); |
| 191 | profile_may_start_func(info, ufunc, |
| 192 | (((dfunc_T *)def_functions.ga_data) |
| 193 | + ectx->ec_dfunc_idx)->df_ufunc); |
| 194 | } |
| 195 | |
| 196 | // Profiling might be enabled/disabled along the way. This should not |
| 197 | // fail, since the function was compiled before and toggling profiling |
| 198 | // doesn't change any errors. |
| 199 | if (func_needs_compiling(ufunc, PROFILING(ufunc)) |
| 200 | && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL) |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 201 | == FAIL) |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 202 | return FAIL; |
| 203 | } |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 204 | #endif |
| 205 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 206 | if (ufunc->uf_va_name != NULL) |
| 207 | { |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 208 | // Need to make a list out of the vararg arguments. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 209 | // Stack at time of call with 2 varargs: |
| 210 | // normal_arg |
| 211 | // optional_arg |
| 212 | // vararg_1 |
| 213 | // vararg_2 |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 214 | // After creating the list: |
| 215 | // normal_arg |
| 216 | // optional_arg |
| 217 | // vararg-list |
| 218 | // With missing optional arguments we get: |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 219 | // normal_arg |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 220 | // After creating the list |
| 221 | // normal_arg |
| 222 | // (space for optional_arg) |
| 223 | // vararg-list |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 224 | vararg_count = argcount - ufunc->uf_args.ga_len; |
| 225 | if (vararg_count < 0) |
| 226 | vararg_count = 0; |
| 227 | else |
| 228 | argcount -= vararg_count; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 229 | if (exe_newlist(vararg_count, ectx) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 230 | return FAIL; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 231 | |
| 232 | vararg_count = 1; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 235 | arg_to_add = ufunc->uf_args.ga_len - argcount; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 236 | if (arg_to_add < 0) |
| 237 | { |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 238 | if (arg_to_add == -1) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 239 | emsg(_(e_one_argument_too_many)); |
Bram Moolenaar | 79e8db9 | 2020-08-14 22:16:33 +0200 | [diff] [blame] | 240 | else |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 241 | semsg(_(e_nr_arguments_too_many), -arg_to_add); |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 242 | return FAIL; |
| 243 | } |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 244 | |
| 245 | // Reserve space for: |
| 246 | // - missing arguments |
| 247 | // - stack frame |
| 248 | // - local variables |
| 249 | // - if needed: a counter for number of closures created in |
| 250 | // ectx->ec_funcrefs. |
| 251 | varcount = dfunc->df_varcount + dfunc->df_has_closure; |
| 252 | if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount) |
| 253 | == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 254 | return FAIL; |
| 255 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 256 | // If depth of calling is getting too high, don't execute the function. |
| 257 | if (funcdepth_increment() == FAIL) |
| 258 | return FAIL; |
| 259 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 260 | // Only make a copy of funclocal if it contains something to restore. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 261 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 262 | { |
| 263 | floc = ALLOC_ONE(funclocal_T); |
| 264 | if (floc == NULL) |
| 265 | return FAIL; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 266 | *floc = ectx->ec_funclocal; |
| 267 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 270 | // Move the vararg-list to below the missing optional arguments. |
| 271 | if (vararg_count > 0 && arg_to_add > 0) |
| 272 | *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 273 | |
| 274 | // Reserve space for omitted optional arguments, filled in soon. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 275 | for (idx = 0; idx < arg_to_add; ++idx) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 276 | STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 277 | ectx->ec_stack.ga_len += arg_to_add; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 278 | |
| 279 | // Store current execution state in stack frame for ISN_RETURN. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 280 | STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx; |
| 281 | STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx; |
Bram Moolenaar | 5930ddc | 2021-04-26 20:32:59 +0200 | [diff] [blame] | 282 | STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 283 | STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 284 | STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 285 | STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 286 | ectx->ec_frame_idx = ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 287 | |
| 288 | // Initialize local variables |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 289 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 290 | STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 291 | if (dfunc->df_has_closure) |
| 292 | { |
| 293 | typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount); |
| 294 | |
| 295 | tv->v_type = VAR_NUMBER; |
| 296 | tv->vval.v_number = 0; |
| 297 | } |
| 298 | ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 299 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 300 | if (pt != NULL || ufunc->uf_partial != NULL |
| 301 | || (ufunc->uf_flags & FC_CLOSURE)) |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 302 | { |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 303 | outer_T *outer = ALLOC_CLEAR_ONE(outer_T); |
| 304 | |
| 305 | if (outer == NULL) |
| 306 | return FAIL; |
| 307 | if (pt != NULL) |
| 308 | { |
| 309 | *outer = pt->pt_outer; |
| 310 | outer->out_up_is_copy = TRUE; |
| 311 | } |
| 312 | else if (ufunc->uf_partial != NULL) |
| 313 | { |
| 314 | *outer = ufunc->uf_partial->pt_outer; |
| 315 | outer->out_up_is_copy = TRUE; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | outer->out_stack = &ectx->ec_stack; |
| 320 | outer->out_frame_idx = ectx->ec_frame_idx; |
| 321 | outer->out_up = ectx->ec_outer; |
| 322 | } |
| 323 | ectx->ec_outer = outer; |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 324 | } |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 325 | else |
| 326 | ectx->ec_outer = NULL; |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 327 | |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 328 | ++ufunc->uf_calls; |
| 329 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 330 | // Set execution state to the start of the called function. |
| 331 | ectx->ec_dfunc_idx = cdf_idx; |
Bram Moolenaar | e5ea346 | 2021-01-25 21:01:48 +0100 | [diff] [blame] | 332 | ectx->ec_instr = INSTRUCTIONS(dfunc); |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 333 | entry = estack_push_ufunc(ufunc, 1); |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 334 | if (entry != NULL) |
| 335 | { |
| 336 | // Set the script context to the script where the function was defined. |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 337 | // Save the current context so it can be restored on return. |
| 338 | entry->es_save_sctx = current_sctx; |
| 339 | current_sctx = ufunc->uf_script_ctx; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 340 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 341 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 342 | // Start execution at the first instruction. |
| 343 | ectx->ec_iidx = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 344 | |
| 345 | return OK; |
| 346 | } |
| 347 | |
| 348 | // Get pointer to item in the stack. |
| 349 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
| 350 | |
| 351 | /* |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 352 | * Used when returning from a function: Check if any closure is still |
| 353 | * referenced. If so then move the arguments and variables to a separate piece |
| 354 | * of stack to be used when the closure is called. |
| 355 | * When "free_arguments" is TRUE the arguments are to be freed. |
| 356 | * Returns FAIL when out of memory. |
| 357 | */ |
| 358 | static int |
| 359 | handle_closure_in_use(ectx_T *ectx, int free_arguments) |
| 360 | { |
| 361 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 362 | + ectx->ec_dfunc_idx; |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 363 | int argcount; |
| 364 | int top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 365 | int idx; |
| 366 | typval_T *tv; |
| 367 | int closure_in_use = FALSE; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 368 | garray_T *gap = &ectx->ec_funcrefs; |
| 369 | varnumber_T closure_count; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 370 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 371 | if (dfunc->df_ufunc == NULL) |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 372 | return OK; // function was freed |
| 373 | if (dfunc->df_has_closure == 0) |
| 374 | return OK; // no closures |
| 375 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount); |
| 376 | closure_count = tv->vval.v_number; |
| 377 | if (closure_count == 0) |
| 378 | return OK; // no funcrefs created |
| 379 | |
Bram Moolenaar | fdeab65 | 2020-09-19 15:16:50 +0200 | [diff] [blame] | 380 | argcount = ufunc_argcount(dfunc->df_ufunc); |
| 381 | top = ectx->ec_frame_idx - argcount; |
| 382 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 383 | // Check if any created closure is still in use. |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 384 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 385 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 386 | partial_T *pt; |
| 387 | int off = gap->ga_len - closure_count + idx; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 388 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 389 | if (off < 0) |
| 390 | continue; // count is off or already done |
| 391 | pt = ((partial_T **)gap->ga_data)[off]; |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 392 | if (pt->pt_refcount > 1) |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 393 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 394 | int refcount = pt->pt_refcount; |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 395 | int i; |
| 396 | |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 397 | // A Reference in a local variables doesn't count, it gets |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 398 | // unreferenced on return. |
| 399 | for (i = 0; i < dfunc->df_varcount; ++i) |
| 400 | { |
| 401 | typval_T *stv = STACK_TV(ectx->ec_frame_idx |
| 402 | + STACK_FRAME_SIZE + i); |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 403 | if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial) |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 404 | --refcount; |
| 405 | } |
| 406 | if (refcount > 1) |
| 407 | { |
| 408 | closure_in_use = TRUE; |
| 409 | break; |
| 410 | } |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 411 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | if (closure_in_use) |
| 415 | { |
| 416 | funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); |
| 417 | typval_T *stack; |
| 418 | |
| 419 | // A closure is using the arguments and/or local variables. |
| 420 | // Move them to the called function. |
| 421 | if (funcstack == NULL) |
| 422 | return FAIL; |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 423 | funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE; |
| 424 | funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 425 | stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); |
| 426 | funcstack->fs_ga.ga_data = stack; |
| 427 | if (stack == NULL) |
| 428 | { |
| 429 | vim_free(funcstack); |
| 430 | return FAIL; |
| 431 | } |
| 432 | |
| 433 | // Move or copy the arguments. |
| 434 | for (idx = 0; idx < argcount; ++idx) |
| 435 | { |
| 436 | tv = STACK_TV(top + idx); |
| 437 | if (free_arguments) |
| 438 | { |
| 439 | *(stack + idx) = *tv; |
| 440 | tv->v_type = VAR_UNKNOWN; |
| 441 | } |
| 442 | else |
| 443 | copy_tv(tv, stack + idx); |
| 444 | } |
| 445 | // Move the local variables. |
| 446 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 447 | { |
| 448 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 449 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 450 | // A partial created for a local function, that is also used as a |
| 451 | // local variable, has a reference count for the variable, thus |
| 452 | // will never go down to zero. When all these refcounts are one |
| 453 | // then the funcstack is unused. We need to count how many we have |
| 454 | // so we need when to check. |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 455 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) |
| 456 | { |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 457 | int i; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 458 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 459 | for (i = 0; i < closure_count; ++i) |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 460 | if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[ |
| 461 | gap->ga_len - closure_count + i]) |
| 462 | ++funcstack->fs_min_refcount; |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 465 | *(stack + funcstack->fs_var_offset + idx) = *tv; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 466 | tv->v_type = VAR_UNKNOWN; |
| 467 | } |
| 468 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 469 | for (idx = 0; idx < closure_count; ++idx) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 470 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 471 | partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len |
| 472 | - closure_count + idx]; |
| 473 | if (pt->pt_refcount > 1) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 474 | { |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 475 | ++funcstack->fs_refcount; |
| 476 | pt->pt_funcstack = funcstack; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 477 | pt->pt_outer.out_stack = &funcstack->fs_ga; |
| 478 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top; |
| 479 | pt->pt_outer.out_up = ectx->ec_outer; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
Bram Moolenaar | 148ce7a | 2020-09-23 21:57:23 +0200 | [diff] [blame] | 484 | for (idx = 0; idx < closure_count; ++idx) |
| 485 | partial_unref(((partial_T **)gap->ga_data)[gap->ga_len |
| 486 | - closure_count + idx]); |
| 487 | gap->ga_len -= closure_count; |
| 488 | if (gap->ga_len == 0) |
| 489 | ga_clear(gap); |
| 490 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 491 | return OK; |
| 492 | } |
| 493 | |
| 494 | /* |
Bram Moolenaar | 85d5e2b | 2020-10-10 14:13:01 +0200 | [diff] [blame] | 495 | * Called when a partial is freed or its reference count goes down to one. The |
| 496 | * funcstack may be the only reference to the partials in the local variables. |
| 497 | * Go over all of them, the funcref and can be freed if all partials |
| 498 | * referencing the funcstack have a reference count of one. |
| 499 | */ |
| 500 | void |
| 501 | funcstack_check_refcount(funcstack_T *funcstack) |
| 502 | { |
| 503 | int i; |
| 504 | garray_T *gap = &funcstack->fs_ga; |
| 505 | int done = 0; |
| 506 | |
| 507 | if (funcstack->fs_refcount > funcstack->fs_min_refcount) |
| 508 | return; |
| 509 | for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i) |
| 510 | { |
| 511 | typval_T *tv = ((typval_T *)gap->ga_data) + i; |
| 512 | |
| 513 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL |
| 514 | && tv->vval.v_partial->pt_funcstack == funcstack |
| 515 | && tv->vval.v_partial->pt_refcount == 1) |
| 516 | ++done; |
| 517 | } |
| 518 | if (done == funcstack->fs_min_refcount) |
| 519 | { |
| 520 | typval_T *stack = gap->ga_data; |
| 521 | |
| 522 | // All partials referencing the funcstack have a reference count of |
| 523 | // one, thus the funcstack is no longer of use. |
| 524 | for (i = 0; i < gap->ga_len; ++i) |
| 525 | clear_tv(stack + i); |
| 526 | vim_free(stack); |
| 527 | vim_free(funcstack); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 532 | * Return from the current function. |
| 533 | */ |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 534 | static int |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 535 | func_return(ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 536 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 537 | int idx; |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 538 | int ret_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 539 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 540 | + ectx->ec_dfunc_idx; |
| 541 | int argcount = ufunc_argcount(dfunc->df_ufunc); |
| 542 | int top = ectx->ec_frame_idx - argcount; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 543 | estack_T *entry; |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 544 | int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx |
| 545 | + STACK_FRAME_FUNC_OFF)->vval.v_number; |
Bram Moolenaar | b06b50d | 2021-04-26 21:39:25 +0200 | [diff] [blame] | 546 | funclocal_T *floc; |
| 547 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 548 | dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 549 | + prev_dfunc_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 550 | |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 551 | if (do_profiling == PROF_YES) |
| 552 | { |
| 553 | ufunc_T *caller = prev_dfunc->df_ufunc; |
| 554 | |
| 555 | if (dfunc->df_ufunc->uf_profiling |
| 556 | || (caller != NULL && caller->uf_profiling)) |
| 557 | { |
| 558 | profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data) |
| 559 | + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller); |
| 560 | --profile_info_ga.ga_len; |
| 561 | } |
| 562 | } |
| 563 | #endif |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 564 | // TODO: when is it safe to delete the function when it is no longer used? |
| 565 | --dfunc->df_ufunc->uf_calls; |
| 566 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 567 | // execution context goes one level up |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 568 | entry = estack_pop(); |
| 569 | if (entry != NULL) |
Bram Moolenaar | c70fe46 | 2021-04-17 17:59:19 +0200 | [diff] [blame] | 570 | current_sctx = entry->es_save_sctx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 571 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 572 | if (handle_closure_in_use(ectx, TRUE) == FAIL) |
| 573 | return FAIL; |
| 574 | |
| 575 | // Clear the arguments. |
| 576 | for (idx = top; idx < ectx->ec_frame_idx; ++idx) |
| 577 | clear_tv(STACK_TV(idx)); |
| 578 | |
| 579 | // Clear local variables and temp values, but not the return value. |
| 580 | for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 581 | idx < ectx->ec_stack.ga_len - 1; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 582 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 583 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 584 | // The return value should be on top of the stack. However, when aborting |
| 585 | // it may not be there and ec_frame_idx is the top of the stack. |
| 586 | ret_idx = ectx->ec_stack.ga_len - 1; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 587 | if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF) |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 588 | ret_idx = 0; |
| 589 | |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 590 | vim_free(ectx->ec_outer); |
| 591 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 592 | // Restore the previous frame. |
Bram Moolenaar | 12d2653 | 2021-02-19 19:13:21 +0100 | [diff] [blame] | 593 | ectx->ec_dfunc_idx = prev_dfunc_idx; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 594 | ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx |
| 595 | + STACK_FRAME_IIDX_OFF)->vval.v_number; |
Bram Moolenaar | 5930ddc | 2021-04-26 20:32:59 +0200 | [diff] [blame] | 596 | ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx |
| 597 | + STACK_FRAME_INSTR_OFF)->vval.v_string; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 598 | ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx |
| 599 | + STACK_FRAME_OUTER_OFF)->vval.v_string; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 600 | floc = (void *)STACK_TV(ectx->ec_frame_idx |
| 601 | + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string; |
Bram Moolenaar | 5366e1a | 2020-10-01 13:01:34 +0200 | [diff] [blame] | 602 | // restoring ec_frame_idx must be last |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 603 | ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx |
| 604 | + STACK_FRAME_IDX_OFF)->vval.v_number; |
Bram Moolenaar | d386e92 | 2021-04-25 14:48:49 +0200 | [diff] [blame] | 605 | |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 606 | if (floc == NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 607 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 608 | else |
| 609 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 610 | ectx->ec_funclocal = *floc; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 611 | vim_free(floc); |
| 612 | } |
| 613 | |
Bram Moolenaar | 34c54eb | 2020-11-25 19:15:19 +0100 | [diff] [blame] | 614 | if (ret_idx > 0) |
| 615 | { |
| 616 | // Reset the stack to the position before the call, with a spot for the |
| 617 | // return value, moved there from above the frame. |
| 618 | ectx->ec_stack.ga_len = top + 1; |
| 619 | *STACK_TV_BOT(-1) = *STACK_TV(ret_idx); |
| 620 | } |
| 621 | else |
| 622 | // Reset the stack to the position before the call. |
| 623 | ectx->ec_stack.ga_len = top; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 624 | |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 625 | funcdepth_decrement(); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 626 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | #undef STACK_TV |
| 630 | |
| 631 | /* |
| 632 | * Prepare arguments and rettv for calling a builtin or user function. |
| 633 | */ |
| 634 | static int |
| 635 | call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) |
| 636 | { |
| 637 | int idx; |
| 638 | typval_T *tv; |
| 639 | |
| 640 | // Move arguments from bottom of the stack to argvars[] and add terminator. |
| 641 | for (idx = 0; idx < argcount; ++idx) |
| 642 | argvars[idx] = *STACK_TV_BOT(idx - argcount); |
| 643 | argvars[argcount].v_type = VAR_UNKNOWN; |
| 644 | |
| 645 | // Result replaces the arguments on the stack. |
| 646 | if (argcount > 0) |
| 647 | ectx->ec_stack.ga_len -= argcount - 1; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 648 | else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 649 | return FAIL; |
| 650 | else |
| 651 | ++ectx->ec_stack.ga_len; |
| 652 | |
| 653 | // Default return value is zero. |
| 654 | tv = STACK_TV_BOT(-1); |
| 655 | tv->v_type = VAR_NUMBER; |
| 656 | tv->vval.v_number = 0; |
| 657 | |
| 658 | return OK; |
| 659 | } |
| 660 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 661 | // Ugly global to avoid passing the execution context around through many |
| 662 | // layers. |
| 663 | static ectx_T *current_ectx = NULL; |
| 664 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 665 | /* |
| 666 | * Call a builtin function by index. |
| 667 | */ |
| 668 | static int |
| 669 | call_bfunc(int func_idx, int argcount, ectx_T *ectx) |
| 670 | { |
| 671 | typval_T argvars[MAX_FUNC_ARGS]; |
| 672 | int idx; |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 673 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 674 | ectx_T *prev_ectx = current_ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 675 | |
| 676 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 677 | return FAIL; |
| 678 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 679 | // Call the builtin function. Set "current_ectx" so that when it |
| 680 | // recursively invokes call_def_function() a closure context can be set. |
| 681 | current_ectx = ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 682 | call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 683 | current_ectx = prev_ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 684 | |
| 685 | // Clear the arguments. |
| 686 | for (idx = 0; idx < argcount; ++idx) |
| 687 | clear_tv(&argvars[idx]); |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 688 | |
Bram Moolenaar | 57f799e | 2020-12-12 20:42:19 +0100 | [diff] [blame] | 689 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 690 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 691 | return OK; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Execute a user defined function. |
Bram Moolenaar | ab36052 | 2021-01-10 14:02:28 +0100 | [diff] [blame] | 696 | * If the function is compiled this will add a stack frame and set the |
| 697 | * instruction pointer at the start of the function. |
| 698 | * Otherwise the function is called here. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 699 | * If "pt" is not null use "pt->pt_outer" for ec_outer. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 700 | * "iptr" can be used to replace the instruction with a more efficient one. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 701 | */ |
| 702 | static int |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 703 | call_ufunc( |
| 704 | ufunc_T *ufunc, |
| 705 | partial_T *pt, |
| 706 | int argcount, |
| 707 | ectx_T *ectx, |
| 708 | isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 709 | { |
| 710 | typval_T argvars[MAX_FUNC_ARGS]; |
| 711 | funcexe_T funcexe; |
| 712 | int error; |
| 713 | int idx; |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 714 | int did_emsg_before = did_emsg; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 715 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 716 | int profiling = do_profiling == PROF_YES && ufunc->uf_profiling; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 717 | #else |
| 718 | # define profiling FALSE |
| 719 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 720 | |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 721 | if (func_needs_compiling(ufunc, profiling) |
| 722 | && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 723 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 724 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 725 | { |
Bram Moolenaar | 52c124d | 2020-12-20 21:43:35 +0100 | [diff] [blame] | 726 | error = check_user_func_argcount(ufunc, argcount); |
Bram Moolenaar | 5082471 | 2020-12-20 21:10:17 +0100 | [diff] [blame] | 727 | if (error != FCERR_UNKNOWN) |
| 728 | { |
| 729 | if (error == FCERR_TOOMANY) |
| 730 | semsg(_(e_toomanyarg), ufunc->uf_name); |
| 731 | else |
| 732 | semsg(_(e_toofewarg), ufunc->uf_name); |
| 733 | return FAIL; |
| 734 | } |
| 735 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 736 | // The function has been compiled, can call it quickly. For a function |
| 737 | // that was defined later: we can call it directly next time. |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 738 | // TODO: what if the function was deleted and then defined again? |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 739 | if (iptr != NULL) |
| 740 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 741 | delete_instr(iptr); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 742 | iptr->isn_type = ISN_DCALL; |
| 743 | iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 744 | iptr->isn_arg.dfunc.cdf_argcount = argcount; |
| 745 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 746 | return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 747 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 748 | |
| 749 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 750 | return FAIL; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 751 | CLEAR_FIELD(funcexe); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 752 | funcexe.evaluate = TRUE; |
| 753 | |
| 754 | // Call the user function. Result goes in last position on the stack. |
| 755 | // TODO: add selfdict if there is one |
| 756 | error = call_user_func_check(ufunc, argcount, argvars, |
| 757 | STACK_TV_BOT(-1), &funcexe, NULL); |
| 758 | |
| 759 | // Clear the arguments. |
| 760 | for (idx = 0; idx < argcount; ++idx) |
| 761 | clear_tv(&argvars[idx]); |
| 762 | |
| 763 | if (error != FCERR_NONE) |
| 764 | { |
| 765 | user_func_error(error, ufunc->uf_name); |
| 766 | return FAIL; |
| 767 | } |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 768 | if (did_emsg > did_emsg_before) |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 769 | // Error other than from calling the function itself. |
| 770 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 771 | return OK; |
| 772 | } |
| 773 | |
| 774 | /* |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 775 | * If command modifiers were applied restore them. |
| 776 | */ |
| 777 | static void |
| 778 | may_restore_cmdmod(funclocal_T *funclocal) |
| 779 | { |
| 780 | if (funclocal->floc_restore_cmdmod) |
| 781 | { |
| 782 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 783 | undo_cmdmod(&cmdmod); |
| 784 | cmdmod = funclocal->floc_save_cmdmod; |
| 785 | funclocal->floc_restore_cmdmod = FALSE; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 790 | * Return TRUE if an error was given or CTRL-C was pressed. |
| 791 | */ |
| 792 | static int |
| 793 | vim9_aborting(int prev_called_emsg) |
| 794 | { |
| 795 | return called_emsg > prev_called_emsg || got_int || did_throw; |
| 796 | } |
| 797 | |
| 798 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 799 | * Execute a function by "name". |
| 800 | * This can be a builtin function or a user function. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 801 | * "iptr" can be used to replace the instruction with a more efficient one. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 802 | * Returns FAIL if not found without an error message. |
| 803 | */ |
| 804 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 805 | call_by_name( |
| 806 | char_u *name, |
| 807 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 808 | ectx_T *ectx, |
| 809 | isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 810 | { |
| 811 | ufunc_T *ufunc; |
| 812 | |
| 813 | if (builtin_function(name, -1)) |
| 814 | { |
| 815 | int func_idx = find_internal_func(name); |
| 816 | |
| 817 | if (func_idx < 0) |
| 818 | return FAIL; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 819 | if (check_internal_func(func_idx, argcount) < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 820 | return FAIL; |
| 821 | return call_bfunc(func_idx, argcount, ectx); |
| 822 | } |
| 823 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 824 | ufunc = find_func(name, FALSE, NULL); |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 825 | |
| 826 | if (ufunc == NULL) |
| 827 | { |
| 828 | int called_emsg_before = called_emsg; |
| 829 | |
| 830 | if (script_autoload(name, TRUE)) |
| 831 | // loaded a package, search for the function again |
| 832 | ufunc = find_func(name, FALSE, NULL); |
| 833 | if (vim9_aborting(called_emsg_before)) |
| 834 | return FAIL; // bail out if loading the script caused an error |
| 835 | } |
| 836 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 837 | if (ufunc != NULL) |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 838 | { |
| 839 | if (ufunc->uf_arg_types != NULL) |
| 840 | { |
| 841 | int i; |
| 842 | typval_T *argv = STACK_TV_BOT(0) - argcount; |
| 843 | |
| 844 | // The function can change at runtime, check that the argument |
| 845 | // types are correct. |
| 846 | for (i = 0; i < argcount; ++i) |
| 847 | { |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 848 | type_T *type = NULL; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 849 | |
Bram Moolenaar | e3ffcd9 | 2021-03-08 21:47:13 +0100 | [diff] [blame] | 850 | if (i < ufunc->uf_args.ga_len) |
| 851 | type = ufunc->uf_arg_types[i]; |
| 852 | else if (ufunc->uf_va_type != NULL) |
| 853 | type = ufunc->uf_va_type->tt_member; |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 854 | if (type != NULL && check_typval_arg_type(type, |
| 855 | &argv[i], i + 1) == FAIL) |
| 856 | return FAIL; |
| 857 | } |
| 858 | } |
| 859 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 860 | return call_ufunc(ufunc, NULL, argcount, ectx, iptr); |
Bram Moolenaar | 04947cc | 2021-03-06 19:26:46 +0100 | [diff] [blame] | 861 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 862 | |
| 863 | return FAIL; |
| 864 | } |
| 865 | |
| 866 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 867 | call_partial( |
| 868 | typval_T *tv, |
| 869 | int argcount_arg, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 870 | ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 871 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 872 | int argcount = argcount_arg; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 873 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 874 | int called_emsg_before = called_emsg; |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 875 | int res = FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 876 | |
| 877 | if (tv->v_type == VAR_PARTIAL) |
| 878 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 879 | partial_T *pt = tv->vval.v_partial; |
| 880 | int i; |
| 881 | |
| 882 | if (pt->pt_argc > 0) |
| 883 | { |
| 884 | // Make space for arguments from the partial, shift the "argcount" |
| 885 | // arguments up. |
| 886 | if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL) |
| 887 | return FAIL; |
| 888 | for (i = 1; i <= argcount; ++i) |
| 889 | *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); |
| 890 | ectx->ec_stack.ga_len += pt->pt_argc; |
| 891 | argcount += pt->pt_argc; |
| 892 | |
| 893 | // copy the arguments from the partial onto the stack |
| 894 | for (i = 0; i < pt->pt_argc; ++i) |
| 895 | copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); |
| 896 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 897 | |
| 898 | if (pt->pt_func != NULL) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 899 | return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL); |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 900 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 901 | name = pt->pt_name; |
| 902 | } |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 903 | else if (tv->v_type == VAR_FUNC) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 904 | name = tv->vval.v_string; |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 905 | if (name != NULL) |
| 906 | { |
| 907 | char_u fname_buf[FLEN_FIXED + 1]; |
| 908 | char_u *tofree = NULL; |
| 909 | int error = FCERR_NONE; |
| 910 | char_u *fname; |
| 911 | |
| 912 | // May need to translate <SNR>123_ to K_SNR. |
| 913 | fname = fname_trans_sid(name, fname_buf, &tofree, &error); |
| 914 | if (error != FCERR_NONE) |
| 915 | res = FAIL; |
| 916 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 917 | res = call_by_name(fname, argcount, ectx, NULL); |
Bram Moolenaar | 95006e3 | 2020-08-29 17:47:08 +0200 | [diff] [blame] | 918 | vim_free(tofree); |
| 919 | } |
| 920 | |
Bram Moolenaar | cb6cbf2 | 2021-01-12 17:17:01 +0100 | [diff] [blame] | 921 | if (res == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 922 | { |
| 923 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 924 | semsg(_(e_unknownfunc), |
| 925 | name == NULL ? (char_u *)"[unknown]" : name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 926 | return FAIL; |
| 927 | } |
| 928 | return OK; |
| 929 | } |
| 930 | |
| 931 | /* |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 932 | * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return |
| 933 | * TRUE. |
| 934 | */ |
| 935 | static int |
| 936 | error_if_locked(int lock, char *error) |
| 937 | { |
| 938 | if (lock & (VAR_LOCKED | VAR_FIXED)) |
| 939 | { |
| 940 | emsg(_(error)); |
| 941 | return TRUE; |
| 942 | } |
| 943 | return FALSE; |
| 944 | } |
| 945 | |
| 946 | /* |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 947 | * Give an error if "tv" is not a number and return FAIL. |
| 948 | */ |
| 949 | static int |
| 950 | check_for_number(typval_T *tv) |
| 951 | { |
| 952 | if (tv->v_type != VAR_NUMBER) |
| 953 | { |
| 954 | semsg(_(e_expected_str_but_got_str), |
| 955 | vartype_name(VAR_NUMBER), vartype_name(tv->v_type)); |
| 956 | return FAIL; |
| 957 | } |
| 958 | return OK; |
| 959 | } |
| 960 | |
| 961 | /* |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 962 | * Store "tv" in variable "name". |
| 963 | * This is for s: and g: variables. |
| 964 | */ |
| 965 | static void |
| 966 | store_var(char_u *name, typval_T *tv) |
| 967 | { |
| 968 | funccal_entry_T entry; |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 969 | int flags = ASSIGN_DECL; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 970 | |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 971 | if (tv->v_lock) |
| 972 | flags |= ASSIGN_CONST; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 973 | save_funccal(&entry); |
Bram Moolenaar | d877a57 | 2021-04-01 19:42:48 +0200 | [diff] [blame] | 974 | set_var_const(name, NULL, tv, FALSE, flags, 0); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 975 | restore_funccal(); |
| 976 | } |
| 977 | |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 978 | /* |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 979 | * Convert "tv" to a string. |
| 980 | * Return FAIL if not allowed. |
| 981 | */ |
| 982 | static int |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 983 | do_2string(typval_T *tv, int is_2string_any, int tolerant) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 984 | { |
| 985 | if (tv->v_type != VAR_STRING) |
| 986 | { |
| 987 | char_u *str; |
| 988 | |
| 989 | if (is_2string_any) |
| 990 | { |
| 991 | switch (tv->v_type) |
| 992 | { |
| 993 | case VAR_SPECIAL: |
| 994 | case VAR_BOOL: |
| 995 | case VAR_NUMBER: |
| 996 | case VAR_FLOAT: |
| 997 | case VAR_BLOB: break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 998 | |
| 999 | case VAR_LIST: |
| 1000 | if (tolerant) |
| 1001 | { |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1002 | char_u *s, *e, *p; |
| 1003 | garray_T ga; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1004 | |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1005 | ga_init2(&ga, sizeof(char_u *), 1); |
| 1006 | |
| 1007 | // Convert to NL separated items, then |
| 1008 | // escape the items and replace the NL with |
| 1009 | // a space. |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1010 | str = typval2string(tv, TRUE); |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1011 | if (str == NULL) |
| 1012 | return FAIL; |
| 1013 | s = str; |
| 1014 | while ((e = vim_strchr(s, '\n')) != NULL) |
| 1015 | { |
| 1016 | *e = NUL; |
| 1017 | p = vim_strsave_fnameescape(s, FALSE); |
| 1018 | if (p != NULL) |
| 1019 | { |
| 1020 | ga_concat(&ga, p); |
| 1021 | ga_concat(&ga, (char_u *)" "); |
| 1022 | vim_free(p); |
| 1023 | } |
| 1024 | s = e + 1; |
| 1025 | } |
| 1026 | vim_free(str); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1027 | clear_tv(tv); |
| 1028 | tv->v_type = VAR_STRING; |
Bram Moolenaar | b288ba9 | 2021-06-05 17:10:55 +0200 | [diff] [blame] | 1029 | tv->vval.v_string = ga.ga_data; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 1030 | return OK; |
| 1031 | } |
| 1032 | // FALLTHROUGH |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1033 | default: to_string_error(tv->v_type); |
| 1034 | return FAIL; |
| 1035 | } |
| 1036 | } |
Bram Moolenaar | 3445320 | 2021-01-31 13:08:38 +0100 | [diff] [blame] | 1037 | str = typval_tostring(tv, TRUE); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 1038 | clear_tv(tv); |
| 1039 | tv->v_type = VAR_STRING; |
| 1040 | tv->vval.v_string = str; |
| 1041 | } |
| 1042 | return OK; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1046 | * When the value of "sv" is a null list of dict, allocate it. |
| 1047 | */ |
| 1048 | static void |
| 1049 | allocate_if_null(typval_T *tv) |
| 1050 | { |
| 1051 | switch (tv->v_type) |
| 1052 | { |
| 1053 | case VAR_LIST: |
| 1054 | if (tv->vval.v_list == NULL) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1055 | (void)rettv_list_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1056 | break; |
| 1057 | case VAR_DICT: |
| 1058 | if (tv->vval.v_dict == NULL) |
Bram Moolenaar | fef8064 | 2021-02-03 20:01:19 +0100 | [diff] [blame] | 1059 | (void)rettv_dict_alloc(tv); |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1060 | break; |
Bram Moolenaar | b7c21af | 2021-04-18 14:12:31 +0200 | [diff] [blame] | 1061 | case VAR_BLOB: |
| 1062 | if (tv->vval.v_blob == NULL) |
| 1063 | (void)rettv_blob_alloc(tv); |
| 1064 | break; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1065 | default: |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1069 | |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1070 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1071 | * Return the character "str[index]" where "index" is the character index, |
| 1072 | * including composing characters. |
| 1073 | * If "index" is out of range NULL is returned. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1074 | */ |
| 1075 | char_u * |
| 1076 | char_from_string(char_u *str, varnumber_T index) |
| 1077 | { |
| 1078 | size_t nbyte = 0; |
| 1079 | varnumber_T nchar = index; |
| 1080 | size_t slen; |
| 1081 | |
| 1082 | if (str == NULL) |
| 1083 | return NULL; |
| 1084 | slen = STRLEN(str); |
| 1085 | |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1086 | // Do the same as for a list: a negative index counts from the end. |
| 1087 | // Optimization to check the first byte to be below 0x80 (and no composing |
| 1088 | // character follows) makes this a lot faster. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1089 | if (index < 0) |
| 1090 | { |
| 1091 | int clen = 0; |
| 1092 | |
| 1093 | for (nbyte = 0; nbyte < slen; ++clen) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1094 | { |
| 1095 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1096 | ++nbyte; |
| 1097 | else if (enc_utf8) |
| 1098 | nbyte += utfc_ptr2len(str + nbyte); |
| 1099 | else |
| 1100 | nbyte += mb_ptr2len(str + nbyte); |
| 1101 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1102 | nchar = clen + index; |
| 1103 | if (nchar < 0) |
| 1104 | // unlike list: index out of range results in empty string |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
| 1108 | for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar) |
Bram Moolenaar | ff87140 | 2021-03-26 13:34:05 +0100 | [diff] [blame] | 1109 | { |
| 1110 | if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80) |
| 1111 | ++nbyte; |
| 1112 | else if (enc_utf8) |
| 1113 | nbyte += utfc_ptr2len(str + nbyte); |
| 1114 | else |
| 1115 | nbyte += mb_ptr2len(str + nbyte); |
| 1116 | } |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1117 | if (nbyte >= slen) |
| 1118 | return NULL; |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1119 | return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte)); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * Get the byte index for character index "idx" in string "str" with length |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1124 | * "str_len". Composing characters are included. |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1125 | * If going over the end return "str_len". |
| 1126 | * If "idx" is negative count from the end, -1 is the last character. |
| 1127 | * When going over the start return -1. |
| 1128 | */ |
| 1129 | static long |
| 1130 | char_idx2byte(char_u *str, size_t str_len, varnumber_T idx) |
| 1131 | { |
| 1132 | varnumber_T nchar = idx; |
| 1133 | size_t nbyte = 0; |
| 1134 | |
| 1135 | if (nchar >= 0) |
| 1136 | { |
| 1137 | while (nchar > 0 && nbyte < str_len) |
| 1138 | { |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1139 | nbyte += mb_ptr2len(str + nbyte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1140 | --nchar; |
| 1141 | } |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | nbyte = str_len; |
| 1146 | while (nchar < 0 && nbyte > 0) |
| 1147 | { |
| 1148 | --nbyte; |
| 1149 | nbyte -= mb_head_off(str, str + nbyte); |
| 1150 | ++nchar; |
| 1151 | } |
| 1152 | if (nchar < 0) |
| 1153 | return -1; |
| 1154 | } |
| 1155 | return (long)nbyte; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1159 | * Return the slice "str[first : last]" using character indexes. Composing |
| 1160 | * characters are included. |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1161 | * "exclusive" is TRUE for slice(). |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1162 | * Return NULL when the result is empty. |
| 1163 | */ |
| 1164 | char_u * |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1165 | string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1166 | { |
| 1167 | long start_byte, end_byte; |
| 1168 | size_t slen; |
| 1169 | |
| 1170 | if (str == NULL) |
| 1171 | return NULL; |
| 1172 | slen = STRLEN(str); |
| 1173 | start_byte = char_idx2byte(str, slen, first); |
| 1174 | if (start_byte < 0) |
| 1175 | start_byte = 0; // first index very negative: use zero |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1176 | if ((last == -1 && !exclusive) || last == VARNUM_MAX) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1177 | end_byte = (long)slen; |
| 1178 | else |
| 1179 | { |
| 1180 | end_byte = char_idx2byte(str, slen, last); |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 1181 | if (!exclusive && end_byte >= 0 && end_byte < (long)slen) |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1182 | // end index is inclusive |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 1183 | end_byte += mb_ptr2len(str + end_byte); |
Bram Moolenaar | e7525c5 | 2021-01-09 13:20:37 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (start_byte >= (long)slen || end_byte <= start_byte) |
| 1187 | return NULL; |
| 1188 | return vim_strnsave(str + start_byte, end_byte - start_byte); |
| 1189 | } |
| 1190 | |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1191 | static svar_T * |
| 1192 | get_script_svar(scriptref_T *sref, ectx_T *ectx) |
| 1193 | { |
| 1194 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 1195 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1196 | + ectx->ec_dfunc_idx; |
| 1197 | svar_T *sv; |
| 1198 | |
| 1199 | if (sref->sref_seq != si->sn_script_seq) |
| 1200 | { |
| 1201 | // The script was reloaded after the function was |
| 1202 | // compiled, the script_idx may not be valid. |
| 1203 | semsg(_(e_script_variable_invalid_after_reload_in_function_str), |
| 1204 | dfunc->df_ufunc->uf_name_exp); |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx; |
| 1208 | if (!equal_type(sv->sv_type, sref->sref_type)) |
| 1209 | { |
| 1210 | emsg(_(e_script_variable_type_changed)); |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | return sv; |
| 1214 | } |
| 1215 | |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1216 | /* |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1217 | * Function passed to do_cmdline() for splitting a script joined by NL |
| 1218 | * characters. |
| 1219 | */ |
| 1220 | static char_u * |
| 1221 | get_split_sourceline( |
| 1222 | int c UNUSED, |
| 1223 | void *cookie, |
| 1224 | int indent UNUSED, |
| 1225 | getline_opt_T options UNUSED) |
| 1226 | { |
| 1227 | source_cookie_T *sp = (source_cookie_T *)cookie; |
| 1228 | char_u *p; |
| 1229 | char_u *line; |
| 1230 | |
| 1231 | if (*sp->nextline == NUL) |
| 1232 | return NULL; |
| 1233 | p = vim_strchr(sp->nextline, '\n'); |
| 1234 | if (p == NULL) |
| 1235 | { |
| 1236 | line = vim_strsave(sp->nextline); |
| 1237 | sp->nextline += STRLEN(sp->nextline); |
| 1238 | } |
| 1239 | else |
| 1240 | { |
| 1241 | line = vim_strnsave(sp->nextline, p - sp->nextline); |
| 1242 | sp->nextline = p + 1; |
| 1243 | } |
| 1244 | return line; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1248 | * Execute a function by "name". |
| 1249 | * This can be a builtin function, user function or a funcref. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1250 | * "iptr" can be used to replace the instruction with a more efficient one. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1251 | */ |
| 1252 | static int |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1253 | call_eval_func( |
| 1254 | char_u *name, |
| 1255 | int argcount, |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 1256 | ectx_T *ectx, |
| 1257 | isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1258 | { |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1259 | int called_emsg_before = called_emsg; |
| 1260 | int res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1261 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1262 | res = call_by_name(name, argcount, ectx, iptr); |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1263 | if (res == FAIL && called_emsg == called_emsg_before) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1264 | { |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 1265 | dictitem_T *v; |
| 1266 | |
| 1267 | v = find_var(name, NULL, FALSE); |
| 1268 | if (v == NULL) |
| 1269 | { |
| 1270 | semsg(_(e_unknownfunc), name); |
| 1271 | return FAIL; |
| 1272 | } |
| 1273 | if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) |
| 1274 | { |
| 1275 | semsg(_(e_unknownfunc), name); |
| 1276 | return FAIL; |
| 1277 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1278 | return call_partial(&v->di_tv, argcount, ectx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1279 | } |
Bram Moolenaar | ed677f5 | 2020-08-12 16:38:10 +0200 | [diff] [blame] | 1280 | return res; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | /* |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1284 | * When a function reference is used, fill a partial with the information |
| 1285 | * needed, especially when it is used as a closure. |
| 1286 | */ |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 1287 | int |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1288 | fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx) |
| 1289 | { |
| 1290 | pt->pt_func = ufunc; |
| 1291 | pt->pt_refcount = 1; |
| 1292 | |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1293 | if (ufunc->uf_flags & FC_CLOSURE) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1294 | { |
| 1295 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1296 | + ectx->ec_dfunc_idx; |
| 1297 | |
| 1298 | // The closure needs to find arguments and local |
| 1299 | // variables in the current stack. |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 1300 | pt->pt_outer.out_stack = &ectx->ec_stack; |
| 1301 | pt->pt_outer.out_frame_idx = ectx->ec_frame_idx; |
| 1302 | pt->pt_outer.out_up = ectx->ec_outer; |
| 1303 | pt->pt_outer.out_up_is_copy = TRUE; |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1304 | |
| 1305 | // If this function returns and the closure is still |
| 1306 | // being used, we need to make a copy of the context |
| 1307 | // (arguments and local variables). Store a reference |
| 1308 | // to the partial so we can handle that. |
| 1309 | if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL) |
| 1310 | { |
| 1311 | vim_free(pt); |
| 1312 | return FAIL; |
| 1313 | } |
| 1314 | // Extra variable keeps the count of closures created |
| 1315 | // in the current function call. |
| 1316 | ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx |
| 1317 | + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number; |
| 1318 | |
| 1319 | ((partial_T **)ectx->ec_funcrefs.ga_data) |
| 1320 | [ectx->ec_funcrefs.ga_len] = pt; |
| 1321 | ++pt->pt_refcount; |
| 1322 | ++ectx->ec_funcrefs.ga_len; |
| 1323 | } |
Bram Moolenaar | 0d3de8c | 2021-01-22 20:46:27 +0100 | [diff] [blame] | 1324 | ++ufunc->uf_refcount; |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 1325 | return OK; |
| 1326 | } |
| 1327 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1328 | // used for v_instr of typval of VAR_INSTR |
| 1329 | struct instr_S { |
| 1330 | ectx_T *instr_ectx; |
| 1331 | isn_T *instr_instr; |
| 1332 | }; |
| 1333 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1334 | // used for substitute_instr |
| 1335 | typedef struct subs_expr_S { |
| 1336 | ectx_T *subs_ectx; |
| 1337 | isn_T *subs_instr; |
| 1338 | int subs_status; |
| 1339 | } subs_expr_T; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1340 | |
| 1341 | // Get pointer to item in the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1342 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1343 | |
| 1344 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 1345 | #undef STACK_TV_BOT |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1346 | #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1347 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 1348 | // Get pointer to a local variable on the stack. Negative for arguments. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1349 | #define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1350 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1351 | /* |
| 1352 | * Execute instructions in execution context "ectx". |
| 1353 | * Return OK or FAIL; |
| 1354 | */ |
| 1355 | static int |
| 1356 | exec_instructions(ectx_T *ectx) |
| 1357 | { |
| 1358 | int breakcheck_count = 0; |
| 1359 | typval_T *tv; |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1360 | int ret = FAIL; |
| 1361 | int save_trylevel_at_start = ectx->ec_trylevel_at_start; |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 1362 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 1363 | // Start execution at the first instruction. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1364 | ectx->ec_iidx = 0; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 1365 | |
Bram Moolenaar | ff65288 | 2021-05-16 15:24:49 +0200 | [diff] [blame] | 1366 | // Only catch exceptions in this instruction list. |
| 1367 | ectx->ec_trylevel_at_start = trylevel; |
| 1368 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1369 | for (;;) |
| 1370 | { |
| 1371 | isn_T *iptr; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1372 | |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1373 | if (++breakcheck_count >= 100) |
| 1374 | { |
| 1375 | line_breakcheck(); |
| 1376 | breakcheck_count = 0; |
| 1377 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1378 | if (got_int) |
| 1379 | { |
| 1380 | // Turn CTRL-C into an exception. |
| 1381 | got_int = FALSE; |
Bram Moolenaar | 97acfc7 | 2020-03-22 13:44:28 +0100 | [diff] [blame] | 1382 | if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1383 | goto theend; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1384 | did_throw = TRUE; |
| 1385 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1386 | |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 1387 | if (did_emsg && msg_list != NULL && *msg_list != NULL) |
| 1388 | { |
| 1389 | // Turn an error message into an exception. |
| 1390 | did_emsg = FALSE; |
| 1391 | if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1392 | goto theend; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 1393 | did_throw = TRUE; |
| 1394 | *msg_list = NULL; |
| 1395 | } |
| 1396 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1397 | if (did_throw && !ectx->ec_in_catch) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1398 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1399 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1400 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1401 | |
| 1402 | // An exception jumps to the first catch, finally, or returns from |
| 1403 | // the current function. |
| 1404 | if (trystack->ga_len > 0) |
| 1405 | trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1406 | if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1407 | { |
| 1408 | // jump to ":catch" or ":finally" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1409 | ectx->ec_in_catch = TRUE; |
| 1410 | ectx->ec_iidx = trycmd->tcd_catch_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1411 | } |
| 1412 | else |
| 1413 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 1414 | // Not inside try or need to return from current functions. |
| 1415 | // Push a dummy return value. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1416 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1417 | goto theend; |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 1418 | tv = STACK_TV_BOT(0); |
| 1419 | tv->v_type = VAR_NUMBER; |
| 1420 | tv->vval.v_number = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1421 | ++ectx->ec_stack.ga_len; |
| 1422 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1423 | { |
Bram Moolenaar | cdd70f0 | 2020-08-13 21:40:18 +0200 | [diff] [blame] | 1424 | // At the toplevel we are done. |
Bram Moolenaar | 257cc5e | 2020-02-19 17:06:11 +0100 | [diff] [blame] | 1425 | need_rethrow = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1426 | if (handle_closure_in_use(ectx, FALSE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1427 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1428 | goto done; |
| 1429 | } |
| 1430 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1431 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1432 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1433 | } |
| 1434 | continue; |
| 1435 | } |
| 1436 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1437 | iptr = &ectx->ec_instr[ectx->ec_iidx++]; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1438 | switch (iptr->isn_type) |
| 1439 | { |
| 1440 | // execute Ex command line |
| 1441 | case ISN_EXEC: |
Bram Moolenaar | 631e8f9 | 2020-11-04 15:07:16 +0100 | [diff] [blame] | 1442 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1443 | source_cookie_T cookie; |
| 1444 | |
Bram Moolenaar | 631e8f9 | 2020-11-04 15:07:16 +0100 | [diff] [blame] | 1445 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1446 | // Pass getsourceline to get an error for a missing ":end" |
| 1447 | // command. |
| 1448 | CLEAR_FIELD(cookie); |
| 1449 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1450 | if (do_cmdline(iptr->isn_arg.string, |
| 1451 | getsourceline, &cookie, |
| 1452 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) |
| 1453 | == FAIL |
| 1454 | || did_emsg) |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 1455 | goto on_error; |
Bram Moolenaar | 631e8f9 | 2020-11-04 15:07:16 +0100 | [diff] [blame] | 1456 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1457 | break; |
| 1458 | |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1459 | // execute Ex command line split at NL characters. |
| 1460 | case ISN_EXEC_SPLIT: |
| 1461 | { |
| 1462 | source_cookie_T cookie; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1463 | char_u *line; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1464 | |
| 1465 | SOURCING_LNUM = iptr->isn_lnum; |
| 1466 | CLEAR_FIELD(cookie); |
| 1467 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1468 | cookie.nextline = iptr->isn_arg.string; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1469 | line = get_split_sourceline(0, &cookie, 0, 0); |
| 1470 | if (do_cmdline(line, |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1471 | get_split_sourceline, &cookie, |
| 1472 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) |
| 1473 | == FAIL |
| 1474 | || did_emsg) |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1475 | { |
| 1476 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1477 | goto on_error; |
Bram Moolenaar | 518df27 | 2021-06-06 17:34:13 +0200 | [diff] [blame] | 1478 | } |
| 1479 | vim_free(line); |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 1480 | } |
| 1481 | break; |
| 1482 | |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 1483 | // Evaluate an expression with legacy syntax, push it onto the |
| 1484 | // stack. |
| 1485 | case ISN_LEGACY_EVAL: |
| 1486 | { |
| 1487 | char_u *arg = iptr->isn_arg.string; |
| 1488 | int res; |
| 1489 | int save_flags = cmdmod.cmod_flags; |
| 1490 | |
| 1491 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1492 | goto theend; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 1493 | tv = STACK_TV_BOT(0); |
| 1494 | init_tv(tv); |
| 1495 | cmdmod.cmod_flags |= CMOD_LEGACY; |
| 1496 | res = eval0(arg, tv, NULL, &EVALARG_EVALUATE); |
| 1497 | cmdmod.cmod_flags = save_flags; |
| 1498 | if (res == FAIL) |
| 1499 | goto on_error; |
| 1500 | ++ectx->ec_stack.ga_len; |
| 1501 | } |
| 1502 | break; |
| 1503 | |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1504 | // push typeval VAR_INSTR with instructions to be executed |
| 1505 | case ISN_INSTR: |
| 1506 | { |
| 1507 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1508 | goto theend; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 1509 | tv = STACK_TV_BOT(0); |
| 1510 | tv->vval.v_instr = ALLOC_ONE(instr_T); |
| 1511 | if (tv->vval.v_instr == NULL) |
| 1512 | goto on_error; |
| 1513 | ++ectx->ec_stack.ga_len; |
| 1514 | |
| 1515 | tv->v_type = VAR_INSTR; |
| 1516 | tv->vval.v_instr->instr_ectx = ectx; |
| 1517 | tv->vval.v_instr->instr_instr = iptr->isn_arg.instr; |
| 1518 | } |
| 1519 | break; |
| 1520 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1521 | // execute :substitute with an expression |
| 1522 | case ISN_SUBSTITUTE: |
| 1523 | { |
| 1524 | subs_T *subs = &iptr->isn_arg.subs; |
| 1525 | source_cookie_T cookie; |
| 1526 | struct subs_expr_S *save_instr = substitute_instr; |
| 1527 | struct subs_expr_S subs_instr; |
| 1528 | int res; |
| 1529 | |
| 1530 | subs_instr.subs_ectx = ectx; |
| 1531 | subs_instr.subs_instr = subs->subs_instr; |
| 1532 | subs_instr.subs_status = OK; |
| 1533 | substitute_instr = &subs_instr; |
| 1534 | |
| 1535 | SOURCING_LNUM = iptr->isn_lnum; |
| 1536 | // This is very much like ISN_EXEC |
| 1537 | CLEAR_FIELD(cookie); |
| 1538 | cookie.sourcing_lnum = iptr->isn_lnum - 1; |
| 1539 | res = do_cmdline(subs->subs_cmd, |
| 1540 | getsourceline, &cookie, |
| 1541 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 1542 | substitute_instr = save_instr; |
| 1543 | |
| 1544 | if (res == FAIL || did_emsg |
| 1545 | || subs_instr.subs_status == FAIL) |
| 1546 | goto on_error; |
| 1547 | } |
| 1548 | break; |
| 1549 | |
| 1550 | case ISN_FINISH: |
| 1551 | goto done; |
| 1552 | |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 1553 | case ISN_REDIRSTART: |
| 1554 | // create a dummy entry for var_redir_str() |
| 1555 | if (alloc_redir_lval() == FAIL) |
| 1556 | goto on_error; |
| 1557 | |
| 1558 | // The output is stored in growarray "redir_ga" until |
| 1559 | // redirection ends. |
| 1560 | init_redir_ga(); |
| 1561 | redir_vname = 1; |
| 1562 | break; |
| 1563 | |
| 1564 | case ISN_REDIREND: |
| 1565 | { |
| 1566 | char_u *res = get_clear_redir_ga(); |
| 1567 | |
| 1568 | // End redirection, put redirected text on the stack. |
| 1569 | clear_redir_lval(); |
| 1570 | redir_vname = 0; |
| 1571 | |
| 1572 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
| 1573 | { |
| 1574 | vim_free(res); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1575 | goto theend; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 1576 | } |
| 1577 | tv = STACK_TV_BOT(0); |
| 1578 | tv->v_type = VAR_STRING; |
| 1579 | tv->vval.v_string = res; |
| 1580 | ++ectx->ec_stack.ga_len; |
| 1581 | } |
| 1582 | break; |
| 1583 | |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1584 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1585 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1586 | if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL) |
| 1587 | goto on_error; |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1588 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1589 | break; |
| 1590 | |
| 1591 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1592 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1593 | { |
| 1594 | exarg_T ea; |
| 1595 | int res; |
| 1596 | |
| 1597 | CLEAR_FIELD(ea); |
| 1598 | ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx; |
| 1599 | ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit; |
| 1600 | ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline; |
| 1601 | --ectx->ec_stack.ga_len; |
| 1602 | tv = STACK_TV_BOT(0); |
| 1603 | res = cexpr_core(&ea, tv); |
| 1604 | clear_tv(tv); |
| 1605 | if (res == FAIL) |
| 1606 | goto on_error; |
| 1607 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 1608 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 1609 | break; |
| 1610 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1611 | // execute Ex command from pieces on the stack |
| 1612 | case ISN_EXECCONCAT: |
| 1613 | { |
| 1614 | int count = iptr->isn_arg.number; |
Bram Moolenaar | 7f6f56f | 2020-04-30 20:21:43 +0200 | [diff] [blame] | 1615 | size_t len = 0; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1616 | int pass; |
| 1617 | int i; |
| 1618 | char_u *cmd = NULL; |
| 1619 | char_u *str; |
| 1620 | |
| 1621 | for (pass = 1; pass <= 2; ++pass) |
| 1622 | { |
| 1623 | for (i = 0; i < count; ++i) |
| 1624 | { |
| 1625 | tv = STACK_TV_BOT(i - count); |
| 1626 | str = tv->vval.v_string; |
| 1627 | if (str != NULL && *str != NUL) |
| 1628 | { |
| 1629 | if (pass == 2) |
| 1630 | STRCPY(cmd + len, str); |
| 1631 | len += STRLEN(str); |
| 1632 | } |
| 1633 | if (pass == 2) |
| 1634 | clear_tv(tv); |
| 1635 | } |
| 1636 | if (pass == 1) |
| 1637 | { |
| 1638 | cmd = alloc(len + 1); |
| 1639 | if (cmd == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1640 | goto theend; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1641 | len = 0; |
| 1642 | } |
| 1643 | } |
| 1644 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 1645 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 1646 | do_cmdline_cmd(cmd); |
| 1647 | vim_free(cmd); |
| 1648 | } |
| 1649 | break; |
| 1650 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1651 | // execute :echo {string} ... |
| 1652 | case ISN_ECHO: |
| 1653 | { |
| 1654 | int count = iptr->isn_arg.echo.echo_count; |
| 1655 | int atstart = TRUE; |
| 1656 | int needclr = TRUE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1657 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1658 | |
| 1659 | for (idx = 0; idx < count; ++idx) |
| 1660 | { |
| 1661 | tv = STACK_TV_BOT(idx - count); |
| 1662 | echo_one(tv, iptr->isn_arg.echo.echo_with_white, |
| 1663 | &atstart, &needclr); |
| 1664 | clear_tv(tv); |
| 1665 | } |
Bram Moolenaar | e0807ea | 2020-02-20 22:18:06 +0100 | [diff] [blame] | 1666 | if (needclr) |
| 1667 | msg_clr_eos(); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1668 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1669 | } |
| 1670 | break; |
| 1671 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1672 | // :execute {string} ... |
| 1673 | // :echomsg {string} ... |
| 1674 | // :echoerr {string} ... |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1675 | case ISN_EXECUTE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1676 | case ISN_ECHOMSG: |
| 1677 | case ISN_ECHOERR: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1678 | { |
| 1679 | int count = iptr->isn_arg.number; |
| 1680 | garray_T ga; |
| 1681 | char_u buf[NUMBUFLEN]; |
| 1682 | char_u *p; |
| 1683 | int len; |
| 1684 | int failed = FALSE; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1685 | int idx; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1686 | |
| 1687 | ga_init2(&ga, 1, 80); |
| 1688 | for (idx = 0; idx < count; ++idx) |
| 1689 | { |
| 1690 | tv = STACK_TV_BOT(idx - count); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1691 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1692 | { |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1693 | if (tv->v_type == VAR_CHANNEL |
| 1694 | || tv->v_type == VAR_JOB) |
| 1695 | { |
| 1696 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 68db996 | 2021-05-09 23:19:22 +0200 | [diff] [blame] | 1697 | semsg(_(e_using_invalid_value_as_string_str), |
| 1698 | vartype_name(tv->v_type)); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1699 | break; |
| 1700 | } |
| 1701 | else |
| 1702 | p = tv_get_string_buf(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1703 | } |
| 1704 | else |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1705 | p = tv_stringify(tv, buf); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1706 | |
| 1707 | len = (int)STRLEN(p); |
| 1708 | if (ga_grow(&ga, len + 2) == FAIL) |
| 1709 | failed = TRUE; |
| 1710 | else |
| 1711 | { |
| 1712 | if (ga.ga_len > 0) |
| 1713 | ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; |
| 1714 | STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); |
| 1715 | ga.ga_len += len; |
| 1716 | } |
| 1717 | clear_tv(tv); |
| 1718 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1719 | ectx->ec_stack.ga_len -= count; |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1720 | if (failed) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 1721 | { |
| 1722 | ga_clear(&ga); |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1723 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 1724 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1725 | |
Bram Moolenaar | e5abf7a | 2020-08-16 18:29:35 +0200 | [diff] [blame] | 1726 | if (ga.ga_data != NULL) |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1727 | { |
| 1728 | if (iptr->isn_type == ISN_EXECUTE) |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 1729 | { |
| 1730 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1731 | do_cmdline_cmd((char_u *)ga.ga_data); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 1732 | if (did_emsg) |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 1733 | { |
| 1734 | ga_clear(&ga); |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 1735 | goto on_error; |
Bram Moolenaar | c71ee82 | 2020-11-21 11:45:50 +0100 | [diff] [blame] | 1736 | } |
Bram Moolenaar | 430deb1 | 2020-08-23 16:29:11 +0200 | [diff] [blame] | 1737 | } |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1738 | else |
| 1739 | { |
| 1740 | msg_sb_eol(); |
| 1741 | if (iptr->isn_type == ISN_ECHOMSG) |
| 1742 | { |
| 1743 | msg_attr(ga.ga_data, echo_attr); |
| 1744 | out_flush(); |
| 1745 | } |
| 1746 | else |
| 1747 | { |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1748 | SOURCING_LNUM = iptr->isn_lnum; |
| 1749 | emsg(ga.ga_data); |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1753 | ga_clear(&ga); |
| 1754 | } |
| 1755 | break; |
| 1756 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1757 | // load local variable or argument |
| 1758 | case ISN_LOAD: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1759 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1760 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1761 | copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1762 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1763 | break; |
| 1764 | |
| 1765 | // load v: variable |
| 1766 | case ISN_LOADV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1767 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1768 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1769 | copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1770 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1771 | break; |
| 1772 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1773 | // load s: variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1774 | case ISN_LOADSCRIPT: |
| 1775 | { |
Bram Moolenaar | 4aab88d | 2020-12-24 21:56:41 +0100 | [diff] [blame] | 1776 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1777 | svar_T *sv; |
| 1778 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1779 | sv = get_script_svar(sref, ectx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1780 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1781 | goto theend; |
Bram Moolenaar | 3beaf9c | 2020-12-18 17:23:14 +0100 | [diff] [blame] | 1782 | allocate_if_null(sv->sv_tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1783 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1784 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1785 | copy_tv(sv->sv_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1786 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1787 | } |
| 1788 | break; |
| 1789 | |
| 1790 | // load s: variable in old script |
| 1791 | case ISN_LOADS: |
| 1792 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1793 | hashtab_T *ht = &SCRIPT_VARS( |
| 1794 | iptr->isn_arg.loadstore.ls_sid); |
| 1795 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1796 | dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1797 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1798 | if (di == NULL) |
| 1799 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 1800 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1801 | semsg(_(e_undefined_variable_str), name); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 1802 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1803 | } |
| 1804 | else |
| 1805 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1806 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1807 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1808 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1809 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | break; |
| 1813 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1814 | // load g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1815 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1816 | case ISN_LOADB: |
| 1817 | case ISN_LOADW: |
| 1818 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1819 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1820 | dictitem_T *di = NULL; |
| 1821 | hashtab_T *ht = NULL; |
| 1822 | char namespace; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1823 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1824 | switch (iptr->isn_type) |
| 1825 | { |
| 1826 | case ISN_LOADG: |
| 1827 | ht = get_globvar_ht(); |
| 1828 | namespace = 'g'; |
| 1829 | break; |
| 1830 | case ISN_LOADB: |
| 1831 | ht = &curbuf->b_vars->dv_hashtab; |
| 1832 | namespace = 'b'; |
| 1833 | break; |
| 1834 | case ISN_LOADW: |
| 1835 | ht = &curwin->w_vars->dv_hashtab; |
| 1836 | namespace = 'w'; |
| 1837 | break; |
| 1838 | case ISN_LOADT: |
| 1839 | ht = &curtab->tp_vars->dv_hashtab; |
| 1840 | namespace = 't'; |
| 1841 | break; |
| 1842 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1843 | goto theend; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1844 | } |
| 1845 | di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1846 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1847 | if (di == NULL) |
| 1848 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 1849 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 1850 | semsg(_(e_undefined_variable_char_str), |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1851 | namespace, iptr->isn_arg.string); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 1852 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1853 | } |
| 1854 | else |
| 1855 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1856 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1857 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1858 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1859 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | break; |
| 1863 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 1864 | // load autoload variable |
| 1865 | case ISN_LOADAUTO: |
| 1866 | { |
| 1867 | char_u *name = iptr->isn_arg.string; |
| 1868 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1869 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1870 | goto theend; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 1871 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 38a434f | 2021-01-02 12:45:45 +0100 | [diff] [blame] | 1872 | if (eval_variable(name, (int)STRLEN(name), |
Bram Moolenaar | cb4e80f | 2021-03-13 20:57:19 +0100 | [diff] [blame] | 1873 | STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL) |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 1874 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1875 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 1876 | } |
| 1877 | break; |
| 1878 | |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1879 | // load g:/b:/w:/t: namespace |
| 1880 | case ISN_LOADGDICT: |
| 1881 | case ISN_LOADBDICT: |
| 1882 | case ISN_LOADWDICT: |
| 1883 | case ISN_LOADTDICT: |
| 1884 | { |
| 1885 | dict_T *d = NULL; |
| 1886 | |
| 1887 | switch (iptr->isn_type) |
| 1888 | { |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 1889 | case ISN_LOADGDICT: d = get_globvar_dict(); break; |
| 1890 | case ISN_LOADBDICT: d = curbuf->b_vars; break; |
| 1891 | case ISN_LOADWDICT: d = curwin->w_vars; break; |
| 1892 | case ISN_LOADTDICT: d = curtab->tp_vars; break; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1893 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1894 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1895 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1896 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1897 | goto theend; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1898 | tv = STACK_TV_BOT(0); |
| 1899 | tv->v_type = VAR_DICT; |
| 1900 | tv->v_lock = 0; |
| 1901 | tv->vval.v_dict = d; |
Bram Moolenaar | 1bd3cb2 | 2021-02-24 12:27:31 +0100 | [diff] [blame] | 1902 | ++d->dv_refcount; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1903 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1904 | } |
| 1905 | break; |
| 1906 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1907 | // load &option |
| 1908 | case ISN_LOADOPT: |
| 1909 | { |
| 1910 | typval_T optval; |
| 1911 | char_u *name = iptr->isn_arg.string; |
| 1912 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1913 | // This is not expected to fail, name is checked during |
| 1914 | // compilation: don't set SOURCING_LNUM. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1915 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1916 | goto theend; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1917 | if (eval_option(&name, &optval, TRUE) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1918 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1919 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1920 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1921 | } |
| 1922 | break; |
| 1923 | |
| 1924 | // load $ENV |
| 1925 | case ISN_LOADENV: |
| 1926 | { |
| 1927 | typval_T optval; |
| 1928 | char_u *name = iptr->isn_arg.string; |
| 1929 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1930 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1931 | goto theend; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1932 | // name is always valid, checked when compiling |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1933 | (void)eval_env_var(&name, &optval, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1934 | *STACK_TV_BOT(0) = optval; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1935 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1936 | } |
| 1937 | break; |
| 1938 | |
| 1939 | // load @register |
| 1940 | case ISN_LOADREG: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1941 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1942 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1943 | tv = STACK_TV_BOT(0); |
| 1944 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 1945 | tv->v_lock = 0; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 1946 | // This may result in NULL, which should be equivalent to an |
| 1947 | // empty string. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1948 | tv->vval.v_string = get_reg_contents( |
| 1949 | iptr->isn_arg.number, GREG_EXPR_SRC); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1950 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1951 | break; |
| 1952 | |
| 1953 | // store local variable |
| 1954 | case ISN_STORE: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1955 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1956 | tv = STACK_TV_VAR(iptr->isn_arg.number); |
| 1957 | clear_tv(tv); |
| 1958 | *tv = *STACK_TV_BOT(0); |
| 1959 | break; |
| 1960 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1961 | // store s: variable in old script |
| 1962 | case ISN_STORES: |
| 1963 | { |
| 1964 | hashtab_T *ht = &SCRIPT_VARS( |
| 1965 | iptr->isn_arg.loadstore.ls_sid); |
| 1966 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1967 | dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1968 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1969 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1970 | if (di == NULL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1971 | store_var(name, STACK_TV_BOT(0)); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1972 | else |
| 1973 | { |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 1974 | SOURCING_LNUM = iptr->isn_lnum; |
| 1975 | if (var_check_permission(di, name) == FAIL) |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 1976 | { |
| 1977 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | dcf29ac | 2021-04-02 14:44:02 +0200 | [diff] [blame] | 1978 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 1979 | } |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1980 | clear_tv(&di->di_tv); |
| 1981 | di->di_tv = *STACK_TV_BOT(0); |
| 1982 | } |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1983 | } |
| 1984 | break; |
| 1985 | |
| 1986 | // store script-local variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1987 | case ISN_STORESCRIPT: |
| 1988 | { |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1989 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 1990 | svar_T *sv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1991 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1992 | sv = get_script_svar(sref, ectx); |
Bram Moolenaar | 07a65d2 | 2020-12-26 20:09:15 +0100 | [diff] [blame] | 1993 | if (sv == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 1994 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 1995 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 1996 | |
| 1997 | // "const" and "final" are checked at compile time, locking |
| 1998 | // the value needs to be checked here. |
| 1999 | SOURCING_LNUM = iptr->isn_lnum; |
| 2000 | if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE)) |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2001 | { |
| 2002 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2003 | goto on_error; |
Bram Moolenaar | 6e50ec2 | 2021-04-03 19:32:44 +0200 | [diff] [blame] | 2004 | } |
Bram Moolenaar | f5906aa | 2021-04-02 14:35:15 +0200 | [diff] [blame] | 2005 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2006 | clear_tv(sv->sv_tv); |
| 2007 | *sv->sv_tv = *STACK_TV_BOT(0); |
| 2008 | } |
| 2009 | break; |
| 2010 | |
| 2011 | // store option |
| 2012 | case ISN_STOREOPT: |
| 2013 | { |
| 2014 | long n = 0; |
| 2015 | char_u *s = NULL; |
| 2016 | char *msg; |
| 2017 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2018 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2019 | tv = STACK_TV_BOT(0); |
| 2020 | if (tv->v_type == VAR_STRING) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 2021 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2022 | s = tv->vval.v_string; |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 2023 | if (s == NULL) |
| 2024 | s = (char_u *)""; |
| 2025 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2026 | else |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 2027 | // must be VAR_NUMBER, CHECKTYPE makes sure |
| 2028 | n = tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2029 | msg = set_option_value(iptr->isn_arg.storeopt.so_name, |
| 2030 | n, s, iptr->isn_arg.storeopt.so_flags); |
Bram Moolenaar | e75ba26 | 2020-05-16 15:43:31 +0200 | [diff] [blame] | 2031 | clear_tv(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2032 | if (msg != NULL) |
| 2033 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 2034 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2035 | emsg(_(msg)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2036 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2037 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2038 | } |
| 2039 | break; |
| 2040 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2041 | // store $ENV |
| 2042 | case ISN_STOREENV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2043 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2044 | tv = STACK_TV_BOT(0); |
| 2045 | vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); |
| 2046 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2047 | break; |
| 2048 | |
| 2049 | // store @r |
| 2050 | case ISN_STOREREG: |
| 2051 | { |
| 2052 | int reg = iptr->isn_arg.number; |
| 2053 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2054 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 2055 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2056 | write_reg_contents(reg == '@' ? '"' : reg, |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 2057 | tv_get_string(tv), -1, FALSE); |
| 2058 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2059 | } |
| 2060 | break; |
| 2061 | |
| 2062 | // store v: variable |
| 2063 | case ISN_STOREV: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2064 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2065 | if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) |
| 2066 | == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2067 | // should not happen, type is checked when compiling |
| 2068 | goto on_error; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2069 | break; |
| 2070 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2071 | // store g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2072 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2073 | case ISN_STOREB: |
| 2074 | case ISN_STOREW: |
| 2075 | case ISN_STORET: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2076 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 2077 | dictitem_T *di; |
| 2078 | hashtab_T *ht; |
| 2079 | char_u *name = iptr->isn_arg.string + 2; |
| 2080 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2081 | switch (iptr->isn_type) |
| 2082 | { |
| 2083 | case ISN_STOREG: |
| 2084 | ht = get_globvar_ht(); |
| 2085 | break; |
| 2086 | case ISN_STOREB: |
| 2087 | ht = &curbuf->b_vars->dv_hashtab; |
| 2088 | break; |
| 2089 | case ISN_STOREW: |
| 2090 | ht = &curwin->w_vars->dv_hashtab; |
| 2091 | break; |
| 2092 | case ISN_STORET: |
| 2093 | ht = &curtab->tp_vars->dv_hashtab; |
| 2094 | break; |
| 2095 | default: // Cannot reach here |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2096 | goto theend; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2097 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2098 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2099 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 2100 | di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2101 | if (di == NULL) |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2102 | store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2103 | else |
| 2104 | { |
Bram Moolenaar | 3bdc90b | 2020-12-22 20:35:40 +0100 | [diff] [blame] | 2105 | SOURCING_LNUM = iptr->isn_lnum; |
| 2106 | if (var_check_permission(di, name) == FAIL) |
| 2107 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2108 | clear_tv(&di->di_tv); |
| 2109 | di->di_tv = *STACK_TV_BOT(0); |
| 2110 | } |
| 2111 | } |
| 2112 | break; |
| 2113 | |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2114 | // store an autoload variable |
| 2115 | case ISN_STOREAUTO: |
| 2116 | SOURCING_LNUM = iptr->isn_lnum; |
| 2117 | set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE); |
| 2118 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2119 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 03290b8 | 2020-12-19 16:30:44 +0100 | [diff] [blame] | 2120 | break; |
| 2121 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2122 | // store number in local variable |
| 2123 | case ISN_STORENR: |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 2124 | tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2125 | clear_tv(tv); |
| 2126 | tv->v_type = VAR_NUMBER; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 2127 | tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2128 | break; |
| 2129 | |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2130 | // store value in list or dict variable |
| 2131 | case ISN_STOREINDEX: |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2132 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2133 | vartype_T dest_type = iptr->isn_arg.vartype; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2134 | typval_T *tv_idx = STACK_TV_BOT(-2); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2135 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2136 | int status = OK; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2137 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2138 | // Stack contains: |
| 2139 | // -3 value to be stored |
| 2140 | // -2 index |
| 2141 | // -1 dict or list |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2142 | tv = STACK_TV_BOT(-3); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2143 | SOURCING_LNUM = iptr->isn_lnum; |
| 2144 | if (dest_type == VAR_ANY) |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2145 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2146 | dest_type = tv_dest->v_type; |
| 2147 | if (dest_type == VAR_DICT) |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 2148 | status = do_2string(tv_idx, TRUE, FALSE); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2149 | else if (dest_type == VAR_LIST |
| 2150 | && tv_idx->v_type != VAR_NUMBER) |
| 2151 | { |
| 2152 | emsg(_(e_number_exp)); |
| 2153 | status = FAIL; |
| 2154 | } |
| 2155 | } |
| 2156 | else if (dest_type != tv_dest->v_type) |
| 2157 | { |
| 2158 | // just in case, should be OK |
| 2159 | semsg(_(e_expected_str_but_got_str), |
| 2160 | vartype_name(dest_type), |
| 2161 | vartype_name(tv_dest->v_type)); |
| 2162 | status = FAIL; |
| 2163 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2164 | |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2165 | if (status == OK && dest_type == VAR_LIST) |
| 2166 | { |
Bram Moolenaar | 239f8d9 | 2021-01-17 13:21:20 +0100 | [diff] [blame] | 2167 | long lidx = (long)tv_idx->vval.v_number; |
| 2168 | list_T *list = tv_dest->vval.v_list; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2169 | |
| 2170 | if (list == NULL) |
| 2171 | { |
| 2172 | emsg(_(e_list_not_set)); |
| 2173 | goto on_error; |
| 2174 | } |
| 2175 | if (lidx < 0 && list->lv_len + lidx >= 0) |
| 2176 | // negative index is relative to the end |
| 2177 | lidx = list->lv_len + lidx; |
| 2178 | if (lidx < 0 || lidx > list->lv_len) |
| 2179 | { |
| 2180 | semsg(_(e_listidx), lidx); |
| 2181 | goto on_error; |
| 2182 | } |
| 2183 | if (lidx < list->lv_len) |
| 2184 | { |
| 2185 | listitem_T *li = list_find(list, lidx); |
| 2186 | |
| 2187 | if (error_if_locked(li->li_tv.v_lock, |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 2188 | e_cannot_change_list_item)) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2189 | goto on_error; |
| 2190 | // overwrite existing list item |
| 2191 | clear_tv(&li->li_tv); |
| 2192 | li->li_tv = *tv; |
| 2193 | } |
| 2194 | else |
| 2195 | { |
| 2196 | if (error_if_locked(list->lv_lock, |
| 2197 | e_cannot_change_list)) |
| 2198 | goto on_error; |
| 2199 | // append to list, only fails when out of memory |
| 2200 | if (list_append_tv(list, tv) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2201 | goto theend; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2202 | clear_tv(tv); |
| 2203 | } |
| 2204 | } |
| 2205 | else if (status == OK && dest_type == VAR_DICT) |
| 2206 | { |
| 2207 | char_u *key = tv_idx->vval.v_string; |
| 2208 | dict_T *dict = tv_dest->vval.v_dict; |
| 2209 | dictitem_T *di; |
| 2210 | |
| 2211 | SOURCING_LNUM = iptr->isn_lnum; |
| 2212 | if (dict == NULL) |
| 2213 | { |
| 2214 | emsg(_(e_dictionary_not_set)); |
| 2215 | goto on_error; |
| 2216 | } |
| 2217 | if (key == NULL) |
| 2218 | key = (char_u *)""; |
| 2219 | di = dict_find(dict, key, -1); |
| 2220 | if (di != NULL) |
| 2221 | { |
| 2222 | if (error_if_locked(di->di_tv.v_lock, |
| 2223 | e_cannot_change_dict_item)) |
| 2224 | goto on_error; |
| 2225 | // overwrite existing value |
| 2226 | clear_tv(&di->di_tv); |
| 2227 | di->di_tv = *tv; |
| 2228 | } |
| 2229 | else |
| 2230 | { |
| 2231 | if (error_if_locked(dict->dv_lock, |
| 2232 | e_cannot_change_dict)) |
| 2233 | goto on_error; |
| 2234 | // add to dict, only fails when out of memory |
| 2235 | if (dict_add_tv(dict, (char *)key, tv) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2236 | goto theend; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2237 | clear_tv(tv); |
| 2238 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2239 | } |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2240 | else if (status == OK && dest_type == VAR_BLOB) |
| 2241 | { |
Bram Moolenaar | 51e9332 | 2021-04-17 20:44:56 +0200 | [diff] [blame] | 2242 | long lidx = (long)tv_idx->vval.v_number; |
| 2243 | blob_T *blob = tv_dest->vval.v_blob; |
| 2244 | varnumber_T nr; |
| 2245 | int error = FALSE; |
| 2246 | int len; |
| 2247 | |
| 2248 | if (blob == NULL) |
| 2249 | { |
| 2250 | emsg(_(e_blob_not_set)); |
| 2251 | goto on_error; |
| 2252 | } |
| 2253 | len = blob_len(blob); |
| 2254 | if (lidx < 0 && len + lidx >= 0) |
| 2255 | // negative index is relative to the end |
| 2256 | lidx = len + lidx; |
| 2257 | |
| 2258 | // Can add one byte at the end. |
| 2259 | if (lidx < 0 || lidx > len) |
| 2260 | { |
| 2261 | semsg(_(e_blobidx), lidx); |
| 2262 | goto on_error; |
| 2263 | } |
| 2264 | if (value_check_lock(blob->bv_lock, |
| 2265 | (char_u *)"blob", FALSE)) |
| 2266 | goto on_error; |
| 2267 | nr = tv_get_number_chk(tv, &error); |
| 2268 | if (error) |
| 2269 | goto on_error; |
| 2270 | blob_set_append(blob, lidx, nr); |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2271 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2272 | else |
| 2273 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2274 | status = FAIL; |
| 2275 | semsg(_(e_cannot_index_str), vartype_name(dest_type)); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2276 | } |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2277 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2278 | clear_tv(tv_idx); |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2279 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2280 | ectx->ec_stack.ga_len -= 3; |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2281 | if (status == FAIL) |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 2282 | { |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 2283 | clear_tv(tv); |
Bram Moolenaar | 8e4c8c8 | 2020-08-01 15:38:38 +0200 | [diff] [blame] | 2284 | goto on_error; |
| 2285 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2286 | } |
| 2287 | break; |
| 2288 | |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2289 | // store value in blob range |
| 2290 | case ISN_STORERANGE: |
| 2291 | { |
| 2292 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2293 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2294 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2295 | int status = OK; |
| 2296 | |
| 2297 | // Stack contains: |
| 2298 | // -4 value to be stored |
| 2299 | // -3 first index or "none" |
| 2300 | // -2 second index or "none" |
| 2301 | // -1 destination blob |
| 2302 | tv = STACK_TV_BOT(-4); |
| 2303 | if (tv_dest->v_type != VAR_BLOB) |
| 2304 | { |
| 2305 | status = FAIL; |
| 2306 | emsg(_(e_blob_required)); |
| 2307 | } |
| 2308 | else |
| 2309 | { |
| 2310 | varnumber_T n1; |
| 2311 | varnumber_T n2; |
| 2312 | int error = FALSE; |
| 2313 | |
| 2314 | n1 = tv_get_number_chk(tv_idx1, &error); |
| 2315 | if (error) |
| 2316 | status = FAIL; |
| 2317 | else |
| 2318 | { |
| 2319 | if (tv_idx2->v_type == VAR_SPECIAL |
| 2320 | && tv_idx2->vval.v_number == VVAL_NONE) |
| 2321 | n2 = blob_len(tv_dest->vval.v_blob) - 1; |
| 2322 | else |
| 2323 | n2 = tv_get_number_chk(tv_idx2, &error); |
| 2324 | if (error) |
| 2325 | status = FAIL; |
| 2326 | else |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 2327 | { |
| 2328 | long bloblen = blob_len(tv_dest->vval.v_blob); |
| 2329 | |
| 2330 | if (check_blob_index(bloblen, |
Bram Moolenaar | bd6406f | 2021-04-14 21:30:06 +0200 | [diff] [blame] | 2331 | n1, FALSE) == FAIL |
Bram Moolenaar | 0e3ff19 | 2021-04-14 20:35:23 +0200 | [diff] [blame] | 2332 | || check_blob_range(bloblen, |
| 2333 | n1, n2, FALSE) == FAIL) |
| 2334 | status = FAIL; |
| 2335 | else |
| 2336 | status = blob_set_range( |
| 2337 | tv_dest->vval.v_blob, n1, n2, tv); |
| 2338 | } |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | clear_tv(tv_idx1); |
| 2343 | clear_tv(tv_idx2); |
| 2344 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2345 | ectx->ec_stack.ga_len -= 4; |
Bram Moolenaar | 6845217 | 2021-04-12 21:21:02 +0200 | [diff] [blame] | 2346 | clear_tv(tv); |
| 2347 | |
| 2348 | if (status == FAIL) |
| 2349 | goto on_error; |
| 2350 | } |
| 2351 | break; |
| 2352 | |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2353 | // load or store variable or argument from outer scope |
| 2354 | case ISN_LOADOUTER: |
| 2355 | case ISN_STOREOUTER: |
| 2356 | { |
| 2357 | int depth = iptr->isn_arg.outer.outer_depth; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2358 | outer_T *outer = ectx->ec_outer; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2359 | |
| 2360 | while (depth > 1 && outer != NULL) |
| 2361 | { |
| 2362 | outer = outer->out_up; |
| 2363 | --depth; |
| 2364 | } |
| 2365 | if (outer == NULL) |
| 2366 | { |
| 2367 | SOURCING_LNUM = iptr->isn_lnum; |
| 2368 | iemsg("LOADOUTER depth more than scope levels"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2369 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2370 | } |
| 2371 | tv = ((typval_T *)outer->out_stack->ga_data) |
| 2372 | + outer->out_frame_idx + STACK_FRAME_SIZE |
| 2373 | + iptr->isn_arg.outer.outer_idx; |
| 2374 | if (iptr->isn_type == ISN_LOADOUTER) |
| 2375 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2376 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2377 | goto theend; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2378 | copy_tv(tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2379 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2380 | } |
| 2381 | else |
| 2382 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2383 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 2384 | clear_tv(tv); |
| 2385 | *tv = *STACK_TV_BOT(0); |
| 2386 | } |
| 2387 | } |
| 2388 | break; |
| 2389 | |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2390 | // unlet item in list or dict variable |
| 2391 | case ISN_UNLETINDEX: |
| 2392 | { |
| 2393 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 2394 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2395 | int status = OK; |
| 2396 | |
| 2397 | // Stack contains: |
| 2398 | // -2 index |
| 2399 | // -1 dict or list |
| 2400 | if (tv_dest->v_type == VAR_DICT) |
| 2401 | { |
| 2402 | // unlet a dict item, index must be a string |
| 2403 | if (tv_idx->v_type != VAR_STRING) |
| 2404 | { |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 2405 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2406 | semsg(_(e_expected_str_but_got_str), |
| 2407 | vartype_name(VAR_STRING), |
| 2408 | vartype_name(tv_idx->v_type)); |
| 2409 | status = FAIL; |
| 2410 | } |
| 2411 | else |
| 2412 | { |
| 2413 | dict_T *d = tv_dest->vval.v_dict; |
| 2414 | char_u *key = tv_idx->vval.v_string; |
| 2415 | dictitem_T *di = NULL; |
| 2416 | |
| 2417 | if (key == NULL) |
| 2418 | key = (char_u *)""; |
| 2419 | if (d != NULL) |
| 2420 | di = dict_find(d, key, (int)STRLEN(key)); |
| 2421 | if (di == NULL) |
| 2422 | { |
| 2423 | // NULL dict is equivalent to empty dict |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 2424 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2425 | semsg(_(e_dictkey), key); |
| 2426 | status = FAIL; |
| 2427 | } |
| 2428 | else |
| 2429 | { |
| 2430 | // TODO: check for dict or item locked |
| 2431 | dictitem_remove(d, di); |
| 2432 | } |
| 2433 | } |
| 2434 | } |
| 2435 | else if (tv_dest->v_type == VAR_LIST) |
| 2436 | { |
| 2437 | // unlet a List item, index must be a number |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2438 | SOURCING_LNUM = iptr->isn_lnum; |
| 2439 | if (check_for_number(tv_idx) == FAIL) |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2440 | { |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2441 | status = FAIL; |
| 2442 | } |
| 2443 | else |
| 2444 | { |
| 2445 | list_T *l = tv_dest->vval.v_list; |
Bram Moolenaar | 239f8d9 | 2021-01-17 13:21:20 +0100 | [diff] [blame] | 2446 | long n = (long)tv_idx->vval.v_number; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2447 | listitem_T *li = NULL; |
| 2448 | |
| 2449 | li = list_find(l, n); |
| 2450 | if (li == NULL) |
| 2451 | { |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 2452 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2453 | semsg(_(e_listidx), n); |
| 2454 | status = FAIL; |
| 2455 | } |
| 2456 | else |
| 2457 | // TODO: check for list or item locked |
| 2458 | listitem_remove(l, li); |
| 2459 | } |
| 2460 | } |
| 2461 | else |
| 2462 | { |
| 2463 | status = FAIL; |
| 2464 | semsg(_(e_cannot_index_str), |
| 2465 | vartype_name(tv_dest->v_type)); |
| 2466 | } |
| 2467 | |
| 2468 | clear_tv(tv_idx); |
| 2469 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2470 | ectx->ec_stack.ga_len -= 2; |
Bram Moolenaar | 752fc69 | 2021-01-04 21:57:11 +0100 | [diff] [blame] | 2471 | if (status == FAIL) |
| 2472 | goto on_error; |
| 2473 | } |
| 2474 | break; |
| 2475 | |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2476 | // unlet range of items in list variable |
| 2477 | case ISN_UNLETRANGE: |
| 2478 | { |
| 2479 | // Stack contains: |
| 2480 | // -3 index1 |
| 2481 | // -2 index2 |
| 2482 | // -1 dict or list |
| 2483 | typval_T *tv_idx1 = STACK_TV_BOT(-3); |
| 2484 | typval_T *tv_idx2 = STACK_TV_BOT(-2); |
| 2485 | typval_T *tv_dest = STACK_TV_BOT(-1); |
| 2486 | int status = OK; |
| 2487 | |
| 2488 | if (tv_dest->v_type == VAR_LIST) |
| 2489 | { |
| 2490 | // indexes must be a number |
| 2491 | SOURCING_LNUM = iptr->isn_lnum; |
| 2492 | if (check_for_number(tv_idx1) == FAIL |
| 2493 | || check_for_number(tv_idx2) == FAIL) |
| 2494 | { |
| 2495 | status = FAIL; |
| 2496 | } |
| 2497 | else |
| 2498 | { |
| 2499 | list_T *l = tv_dest->vval.v_list; |
| 2500 | long n1 = (long)tv_idx1->vval.v_number; |
| 2501 | long n2 = (long)tv_idx2->vval.v_number; |
| 2502 | listitem_T *li; |
| 2503 | |
| 2504 | li = list_find_index(l, &n1); |
| 2505 | if (li == NULL |
| 2506 | || list_unlet_range(l, li, NULL, n1, |
| 2507 | TRUE, n2) == FAIL) |
| 2508 | status = FAIL; |
| 2509 | } |
| 2510 | } |
| 2511 | else |
| 2512 | { |
| 2513 | status = FAIL; |
| 2514 | SOURCING_LNUM = iptr->isn_lnum; |
| 2515 | semsg(_(e_cannot_index_str), |
| 2516 | vartype_name(tv_dest->v_type)); |
| 2517 | } |
| 2518 | |
| 2519 | clear_tv(tv_idx1); |
| 2520 | clear_tv(tv_idx2); |
| 2521 | clear_tv(tv_dest); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2522 | ectx->ec_stack.ga_len -= 3; |
Bram Moolenaar | 5b5ae29 | 2021-02-20 17:04:02 +0100 | [diff] [blame] | 2523 | if (status == FAIL) |
| 2524 | goto on_error; |
| 2525 | } |
| 2526 | break; |
| 2527 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2528 | // push constant |
| 2529 | case ISN_PUSHNR: |
| 2530 | case ISN_PUSHBOOL: |
| 2531 | case ISN_PUSHSPEC: |
| 2532 | case ISN_PUSHF: |
| 2533 | case ISN_PUSHS: |
| 2534 | case ISN_PUSHBLOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2535 | case ISN_PUSHFUNC: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2536 | case ISN_PUSHCHANNEL: |
| 2537 | case ISN_PUSHJOB: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2538 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2539 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2540 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2541 | tv->v_lock = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2542 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2543 | switch (iptr->isn_type) |
| 2544 | { |
| 2545 | case ISN_PUSHNR: |
| 2546 | tv->v_type = VAR_NUMBER; |
| 2547 | tv->vval.v_number = iptr->isn_arg.number; |
| 2548 | break; |
| 2549 | case ISN_PUSHBOOL: |
| 2550 | tv->v_type = VAR_BOOL; |
| 2551 | tv->vval.v_number = iptr->isn_arg.number; |
| 2552 | break; |
| 2553 | case ISN_PUSHSPEC: |
| 2554 | tv->v_type = VAR_SPECIAL; |
| 2555 | tv->vval.v_number = iptr->isn_arg.number; |
| 2556 | break; |
| 2557 | #ifdef FEAT_FLOAT |
| 2558 | case ISN_PUSHF: |
| 2559 | tv->v_type = VAR_FLOAT; |
| 2560 | tv->vval.v_float = iptr->isn_arg.fnumber; |
| 2561 | break; |
| 2562 | #endif |
| 2563 | case ISN_PUSHBLOB: |
| 2564 | blob_copy(iptr->isn_arg.blob, tv); |
| 2565 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2566 | case ISN_PUSHFUNC: |
| 2567 | tv->v_type = VAR_FUNC; |
Bram Moolenaar | 087d2e1 | 2020-03-01 15:36:42 +0100 | [diff] [blame] | 2568 | if (iptr->isn_arg.string == NULL) |
| 2569 | tv->vval.v_string = NULL; |
| 2570 | else |
| 2571 | tv->vval.v_string = |
| 2572 | vim_strsave(iptr->isn_arg.string); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2573 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2574 | case ISN_PUSHCHANNEL: |
| 2575 | #ifdef FEAT_JOB_CHANNEL |
| 2576 | tv->v_type = VAR_CHANNEL; |
| 2577 | tv->vval.v_channel = iptr->isn_arg.channel; |
| 2578 | if (tv->vval.v_channel != NULL) |
| 2579 | ++tv->vval.v_channel->ch_refcount; |
| 2580 | #endif |
| 2581 | break; |
| 2582 | case ISN_PUSHJOB: |
| 2583 | #ifdef FEAT_JOB_CHANNEL |
| 2584 | tv->v_type = VAR_JOB; |
| 2585 | tv->vval.v_job = iptr->isn_arg.job; |
| 2586 | if (tv->vval.v_job != NULL) |
| 2587 | ++tv->vval.v_job->jv_refcount; |
| 2588 | #endif |
| 2589 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2590 | default: |
| 2591 | tv->v_type = VAR_STRING; |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 2592 | tv->vval.v_string = vim_strsave( |
| 2593 | iptr->isn_arg.string == NULL |
| 2594 | ? (char_u *)"" : iptr->isn_arg.string); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2595 | } |
| 2596 | break; |
| 2597 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 2598 | case ISN_UNLET: |
| 2599 | if (do_unlet(iptr->isn_arg.unlet.ul_name, |
| 2600 | iptr->isn_arg.unlet.ul_forceit) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2601 | goto on_error; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 2602 | break; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 2603 | case ISN_UNLETENV: |
| 2604 | vim_unsetenv(iptr->isn_arg.unlet.ul_name); |
| 2605 | break; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 2606 | |
Bram Moolenaar | 0b4c66c | 2020-09-14 21:39:44 +0200 | [diff] [blame] | 2607 | case ISN_LOCKCONST: |
| 2608 | item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE); |
| 2609 | break; |
| 2610 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2611 | // create a list from items on the stack; uses a single allocation |
| 2612 | // for the list header and the items |
| 2613 | case ISN_NEWLIST: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2614 | if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2615 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2616 | break; |
| 2617 | |
| 2618 | // create a dict from items on the stack |
| 2619 | case ISN_NEWDICT: |
| 2620 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2621 | int count = iptr->isn_arg.number; |
| 2622 | dict_T *dict = dict_alloc(); |
| 2623 | dictitem_T *item; |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 2624 | char_u *key; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2625 | int idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2626 | |
| 2627 | if (dict == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2628 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2629 | for (idx = 0; idx < count; ++idx) |
| 2630 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2631 | // have already checked key type is VAR_STRING |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2632 | tv = STACK_TV_BOT(2 * (idx - count)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2633 | // check key is unique |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 2634 | key = tv->vval.v_string == NULL |
| 2635 | ? (char_u *)"" : tv->vval.v_string; |
| 2636 | item = dict_find(dict, key, -1); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2637 | if (item != NULL) |
| 2638 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 2639 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 2640 | semsg(_(e_duplicate_key), key); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2641 | dict_unref(dict); |
| 2642 | goto on_error; |
| 2643 | } |
Bram Moolenaar | c7f7f6d | 2020-11-04 13:38:28 +0100 | [diff] [blame] | 2644 | item = dictitem_alloc(key); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2645 | clear_tv(tv); |
| 2646 | if (item == NULL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2647 | { |
| 2648 | dict_unref(dict); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2649 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2650 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2651 | item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); |
| 2652 | item->di_tv.v_lock = 0; |
| 2653 | if (dict_add(dict, item) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2654 | { |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 2655 | // can this ever happen? |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2656 | dict_unref(dict); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2657 | goto theend; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2658 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | if (count > 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2662 | ectx->ec_stack.ga_len -= 2 * count - 1; |
| 2663 | else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2664 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2665 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2666 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2667 | tv = STACK_TV_BOT(-1); |
| 2668 | tv->v_type = VAR_DICT; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2669 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2670 | tv->vval.v_dict = dict; |
| 2671 | ++dict->dv_refcount; |
| 2672 | } |
| 2673 | break; |
| 2674 | |
| 2675 | // call a :def function |
| 2676 | case ISN_DCALL: |
Bram Moolenaar | dfa3d55 | 2020-09-10 22:05:08 +0200 | [diff] [blame] | 2677 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 2678 | if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, |
| 2679 | NULL, |
| 2680 | iptr->isn_arg.dfunc.cdf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2681 | ectx) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 2682 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2683 | break; |
| 2684 | |
| 2685 | // call a builtin function |
| 2686 | case ISN_BCALL: |
| 2687 | SOURCING_LNUM = iptr->isn_lnum; |
| 2688 | if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, |
| 2689 | iptr->isn_arg.bfunc.cbf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2690 | ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 2691 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2692 | break; |
| 2693 | |
| 2694 | // call a funcref or partial |
| 2695 | case ISN_PCALL: |
| 2696 | { |
| 2697 | cpfunc_T *pfunc = &iptr->isn_arg.pfunc; |
| 2698 | int r; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 2699 | typval_T partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2700 | |
| 2701 | SOURCING_LNUM = iptr->isn_lnum; |
| 2702 | if (pfunc->cpf_top) |
| 2703 | { |
| 2704 | // funcref is above the arguments |
| 2705 | tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); |
| 2706 | } |
| 2707 | else |
| 2708 | { |
| 2709 | // Get the funcref from the stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2710 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 2711 | partial_tv = *STACK_TV_BOT(0); |
| 2712 | tv = &partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2713 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2714 | r = call_partial(tv, pfunc->cpf_argcount, ectx); |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 2715 | if (tv == &partial_tv) |
| 2716 | clear_tv(&partial_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2717 | if (r == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 2718 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2719 | } |
| 2720 | break; |
| 2721 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 2722 | case ISN_PCALL_END: |
| 2723 | // PCALL finished, arguments have been consumed and replaced by |
| 2724 | // the return value. Now clear the funcref from the stack, |
| 2725 | // and move the return value in its place. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2726 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 2727 | clear_tv(STACK_TV_BOT(-1)); |
| 2728 | *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); |
| 2729 | break; |
| 2730 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2731 | // call a user defined function or funcref/partial |
| 2732 | case ISN_UCALL: |
| 2733 | { |
| 2734 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 2735 | |
| 2736 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 2fecb53 | 2021-03-24 22:00:56 +0100 | [diff] [blame] | 2737 | if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2738 | ectx, iptr) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 2739 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2740 | } |
| 2741 | break; |
| 2742 | |
| 2743 | // return from a :def function call |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 2744 | case ISN_RETURN_ZERO: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2745 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2746 | goto theend; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 2747 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2748 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 299f303 | 2021-01-08 20:53:09 +0100 | [diff] [blame] | 2749 | tv->v_type = VAR_NUMBER; |
| 2750 | tv->vval.v_number = 0; |
| 2751 | tv->v_lock = 0; |
| 2752 | // FALLTHROUGH |
| 2753 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2754 | case ISN_RETURN: |
| 2755 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2756 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2757 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8cbd6df | 2020-01-28 22:59:45 +0100 | [diff] [blame] | 2758 | |
| 2759 | if (trystack->ga_len > 0) |
| 2760 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 2761 | + trystack->ga_len - 1; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2762 | if (trycmd != NULL |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2763 | && trycmd->tcd_frame_idx == ectx->ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2764 | { |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 2765 | // jump to ":finally" or ":endtry" |
| 2766 | if (trycmd->tcd_finally_idx != 0) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2767 | ectx->ec_iidx = trycmd->tcd_finally_idx; |
Bram Moolenaar | 9cb577a | 2021-02-22 22:45:10 +0100 | [diff] [blame] | 2768 | else |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2769 | ectx->ec_iidx = trycmd->tcd_endtry_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2770 | trycmd->tcd_return = TRUE; |
| 2771 | } |
| 2772 | else |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 2773 | goto func_return; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2774 | } |
| 2775 | break; |
| 2776 | |
| 2777 | // push a function reference to a compiled function |
| 2778 | case ISN_FUNCREF: |
| 2779 | { |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 2780 | partial_T *pt = ALLOC_CLEAR_ONE(partial_T); |
| 2781 | dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 2782 | + iptr->isn_arg.funcref.fr_func; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2783 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2784 | if (pt == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2785 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2786 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2787 | { |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2788 | vim_free(pt); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2789 | goto theend; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2790 | } |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 2791 | if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2792 | ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2793 | goto theend; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2794 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2795 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2796 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2797 | tv->vval.v_partial = pt; |
| 2798 | tv->v_type = VAR_PARTIAL; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 2799 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2800 | } |
| 2801 | break; |
| 2802 | |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 2803 | // Create a global function from a lambda. |
| 2804 | case ISN_NEWFUNC: |
| 2805 | { |
| 2806 | newfunc_T *newfunc = &iptr->isn_arg.newfunc; |
| 2807 | |
Bram Moolenaar | cd45ed0 | 2020-12-22 17:35:54 +0100 | [diff] [blame] | 2808 | if (copy_func(newfunc->nf_lambda, newfunc->nf_global, |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2809 | ectx) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2810 | goto theend; |
Bram Moolenaar | 38ddf33 | 2020-07-31 22:05:04 +0200 | [diff] [blame] | 2811 | } |
| 2812 | break; |
| 2813 | |
Bram Moolenaar | 6abdcf8 | 2020-11-22 18:15:44 +0100 | [diff] [blame] | 2814 | // List functions |
| 2815 | case ISN_DEF: |
| 2816 | if (iptr->isn_arg.string == NULL) |
| 2817 | list_functions(NULL); |
| 2818 | else |
| 2819 | { |
| 2820 | exarg_T ea; |
| 2821 | |
| 2822 | CLEAR_FIELD(ea); |
| 2823 | ea.cmd = ea.arg = iptr->isn_arg.string; |
| 2824 | define_function(&ea, NULL); |
| 2825 | } |
| 2826 | break; |
| 2827 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2828 | // jump if a condition is met |
| 2829 | case ISN_JUMP: |
| 2830 | { |
| 2831 | jumpwhen_T when = iptr->isn_arg.jump.jump_when; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 2832 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2833 | int jump = TRUE; |
| 2834 | |
| 2835 | if (when != JUMP_ALWAYS) |
| 2836 | { |
| 2837 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 2838 | if (when == JUMP_IF_COND_FALSE |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 2839 | || when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 2840 | || when == JUMP_IF_COND_TRUE) |
| 2841 | { |
| 2842 | SOURCING_LNUM = iptr->isn_lnum; |
| 2843 | jump = tv_get_bool_chk(tv, &error); |
| 2844 | if (error) |
| 2845 | goto on_error; |
| 2846 | } |
| 2847 | else |
| 2848 | jump = tv2bool(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2849 | if (when == JUMP_IF_FALSE |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 2850 | || when == JUMP_AND_KEEP_IF_FALSE |
| 2851 | || when == JUMP_IF_COND_FALSE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2852 | jump = !jump; |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 2853 | if (when == JUMP_IF_FALSE || !jump) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2854 | { |
| 2855 | // drop the value from the stack |
| 2856 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2857 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2858 | } |
| 2859 | } |
| 2860 | if (jump) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2861 | ectx->ec_iidx = iptr->isn_arg.jump.jump_where; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2862 | } |
| 2863 | break; |
| 2864 | |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 2865 | // Jump if an argument with a default value was already set and not |
| 2866 | // v:none. |
| 2867 | case ISN_JUMP_IF_ARG_SET: |
| 2868 | tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off); |
| 2869 | if (tv->v_type != VAR_UNKNOWN |
| 2870 | && !(tv->v_type == VAR_SPECIAL |
| 2871 | && tv->vval.v_number == VVAL_NONE)) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2872 | ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where; |
Bram Moolenaar | 38a3bfa | 2021-03-29 22:14:55 +0200 | [diff] [blame] | 2873 | break; |
| 2874 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2875 | // top of a for loop |
| 2876 | case ISN_FOR: |
| 2877 | { |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2878 | typval_T *ltv = STACK_TV_BOT(-1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2879 | typval_T *idxtv = |
| 2880 | STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); |
| 2881 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2882 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2883 | goto theend; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2884 | if (ltv->v_type == VAR_LIST) |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 2885 | { |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2886 | list_T *list = ltv->vval.v_list; |
| 2887 | |
| 2888 | // push the next item from the list |
| 2889 | ++idxtv->vval.v_number; |
| 2890 | if (list == NULL |
| 2891 | || idxtv->vval.v_number >= list->lv_len) |
| 2892 | { |
| 2893 | // past the end of the list, jump to "endfor" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2894 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2895 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2896 | } |
| 2897 | else if (list->lv_first == &range_list_item) |
| 2898 | { |
| 2899 | // non-materialized range() list |
| 2900 | tv = STACK_TV_BOT(0); |
| 2901 | tv->v_type = VAR_NUMBER; |
| 2902 | tv->v_lock = 0; |
| 2903 | tv->vval.v_number = list_find_nr( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2904 | list, idxtv->vval.v_number, NULL); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2905 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2906 | } |
| 2907 | else |
| 2908 | { |
| 2909 | listitem_T *li = list_find(list, |
| 2910 | idxtv->vval.v_number); |
| 2911 | |
| 2912 | copy_tv(&li->li_tv, STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2913 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2914 | } |
| 2915 | } |
| 2916 | else if (ltv->v_type == VAR_STRING) |
| 2917 | { |
| 2918 | char_u *str = ltv->vval.v_string; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2919 | |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 2920 | // The index is for the last byte of the previous |
| 2921 | // character. |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2922 | ++idxtv->vval.v_number; |
Bram Moolenaar | 175a41c | 2021-04-08 18:05:03 +0200 | [diff] [blame] | 2923 | if (str == NULL || str[idxtv->vval.v_number] == NUL) |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2924 | { |
| 2925 | // past the end of the string, jump to "endfor" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2926 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2927 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2928 | } |
| 2929 | else |
| 2930 | { |
| 2931 | int clen = mb_ptr2len(str + idxtv->vval.v_number); |
| 2932 | |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 2933 | // Push the next character from the string. |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2934 | tv = STACK_TV_BOT(0); |
| 2935 | tv->v_type = VAR_STRING; |
| 2936 | tv->vval.v_string = vim_strnsave( |
| 2937 | str + idxtv->vval.v_number, clen); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2938 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2939 | idxtv->vval.v_number += clen - 1; |
| 2940 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2941 | } |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 2942 | else if (ltv->v_type == VAR_BLOB) |
| 2943 | { |
| 2944 | blob_T *blob = ltv->vval.v_blob; |
| 2945 | |
| 2946 | // When we get here the first time make a copy of the |
| 2947 | // blob, so that the iteration still works when it is |
| 2948 | // changed. |
| 2949 | if (idxtv->vval.v_number == -1 && blob != NULL) |
| 2950 | { |
| 2951 | blob_copy(blob, ltv); |
| 2952 | blob_unref(blob); |
| 2953 | blob = ltv->vval.v_blob; |
| 2954 | } |
| 2955 | |
| 2956 | // The index is for the previous byte. |
| 2957 | ++idxtv->vval.v_number; |
| 2958 | if (blob == NULL |
| 2959 | || idxtv->vval.v_number >= blob_len(blob)) |
| 2960 | { |
| 2961 | // past the end of the blob, jump to "endfor" |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2962 | ectx->ec_iidx = iptr->isn_arg.forloop.for_end; |
| 2963 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 2964 | } |
| 2965 | else |
| 2966 | { |
| 2967 | // Push the next byte from the blob. |
| 2968 | tv = STACK_TV_BOT(0); |
| 2969 | tv->v_type = VAR_NUMBER; |
| 2970 | tv->vval.v_number = blob_get(blob, |
| 2971 | idxtv->vval.v_number); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2972 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | d551d6c | 2021-04-18 13:15:58 +0200 | [diff] [blame] | 2973 | } |
| 2974 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2975 | else |
| 2976 | { |
Bram Moolenaar | 74e54fc | 2021-03-26 20:41:29 +0100 | [diff] [blame] | 2977 | semsg(_(e_for_loop_on_str_not_supported), |
| 2978 | vartype_name(ltv->v_type)); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2979 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2980 | } |
| 2981 | } |
| 2982 | break; |
| 2983 | |
| 2984 | // start of ":try" block |
| 2985 | case ISN_TRY: |
| 2986 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2987 | trycmd_T *trycmd = NULL; |
| 2988 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2989 | if (GA_GROW(&ectx->ec_trystack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 2990 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2991 | trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data) |
| 2992 | + ectx->ec_trystack.ga_len; |
| 2993 | ++ectx->ec_trystack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2994 | ++trylevel; |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 2995 | CLEAR_POINTER(trycmd); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 2996 | trycmd->tcd_frame_idx = ectx->ec_frame_idx; |
| 2997 | trycmd->tcd_stack_len = ectx->ec_stack.ga_len; |
Bram Moolenaar | a91a713 | 2021-03-25 21:12:15 +0100 | [diff] [blame] | 2998 | trycmd->tcd_catch_idx = |
| 2999 | iptr->isn_arg.try.try_ref->try_catch; |
| 3000 | trycmd->tcd_finally_idx = |
| 3001 | iptr->isn_arg.try.try_ref->try_finally; |
| 3002 | trycmd->tcd_endtry_idx = |
| 3003 | iptr->isn_arg.try.try_ref->try_endtry; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3004 | } |
| 3005 | break; |
| 3006 | |
| 3007 | case ISN_PUSHEXC: |
| 3008 | if (current_exception == NULL) |
| 3009 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3010 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3011 | iemsg("Evaluating catch while current_exception is NULL"); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3012 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3013 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3014 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3015 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3016 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3017 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3018 | tv->v_type = VAR_STRING; |
Bram Moolenaar | 2f8ce0a | 2020-07-19 19:47:35 +0200 | [diff] [blame] | 3019 | tv->v_lock = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3020 | tv->vval.v_string = vim_strsave( |
| 3021 | (char_u *)current_exception->value); |
| 3022 | break; |
| 3023 | |
| 3024 | case ISN_CATCH: |
| 3025 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3026 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3027 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3028 | may_restore_cmdmod(&ectx->ec_funclocal); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3029 | if (trystack->ga_len > 0) |
| 3030 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3031 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3032 | + trystack->ga_len - 1; |
| 3033 | trycmd->tcd_caught = TRUE; |
| 3034 | } |
| 3035 | did_emsg = got_int = did_throw = FALSE; |
Bram Moolenaar | 1430cee | 2021-01-17 19:20:32 +0100 | [diff] [blame] | 3036 | force_abort = need_rethrow = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3037 | catch_exception(current_exception); |
| 3038 | } |
| 3039 | break; |
| 3040 | |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3041 | case ISN_TRYCONT: |
| 3042 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3043 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3044 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 3045 | int i; |
| 3046 | trycmd_T *trycmd; |
| 3047 | int iidx = trycont->tct_where; |
| 3048 | |
| 3049 | if (trystack->ga_len < trycont->tct_levels) |
| 3050 | { |
| 3051 | siemsg("TRYCONT: expected %d levels, found %d", |
| 3052 | trycont->tct_levels, trystack->ga_len); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3053 | goto theend; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3054 | } |
| 3055 | // Make :endtry jump to any outer try block and the last |
| 3056 | // :endtry inside the loop to the loop start. |
| 3057 | for (i = trycont->tct_levels; i > 0; --i) |
| 3058 | { |
| 3059 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3060 | + trystack->ga_len - i; |
Bram Moolenaar | 2e34c34 | 2021-03-14 12:13:33 +0100 | [diff] [blame] | 3061 | // Add one to tcd_cont to be able to jump to |
| 3062 | // instruction with index zero. |
| 3063 | trycmd->tcd_cont = iidx + 1; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3064 | iidx = trycmd->tcd_finally_idx == 0 |
| 3065 | ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3066 | } |
| 3067 | // jump to :finally or :endtry of current try statement |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3068 | ectx->ec_iidx = iidx; |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3069 | } |
| 3070 | break; |
| 3071 | |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3072 | case ISN_FINALLY: |
| 3073 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3074 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 7e82c5f | 2021-02-21 21:32:45 +0100 | [diff] [blame] | 3075 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 3076 | + trystack->ga_len - 1; |
| 3077 | |
| 3078 | // Reset the index to avoid a return statement jumps here |
| 3079 | // again. |
| 3080 | trycmd->tcd_finally_idx = 0; |
| 3081 | break; |
| 3082 | } |
| 3083 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3084 | // end of ":try" block |
| 3085 | case ISN_ENDTRY: |
| 3086 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3087 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3088 | |
| 3089 | if (trystack->ga_len > 0) |
| 3090 | { |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3091 | trycmd_T *trycmd; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 3092 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3093 | --trystack->ga_len; |
| 3094 | --trylevel; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3095 | ectx->ec_in_catch = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3096 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 3097 | + trystack->ga_len; |
Bram Moolenaar | f575adf | 2020-02-20 20:41:06 +0100 | [diff] [blame] | 3098 | if (trycmd->tcd_caught && current_exception != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3099 | { |
| 3100 | // discard the exception |
| 3101 | if (caught_stack == current_exception) |
| 3102 | caught_stack = caught_stack->caught; |
| 3103 | discard_current_exception(); |
| 3104 | } |
| 3105 | |
| 3106 | if (trycmd->tcd_return) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3107 | goto func_return; |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3108 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3109 | while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len) |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3110 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3111 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | d9d7789 | 2021-02-12 21:32:47 +0100 | [diff] [blame] | 3112 | clear_tv(STACK_TV_BOT(0)); |
| 3113 | } |
Bram Moolenaar | 8d4be89 | 2021-02-13 18:33:02 +0100 | [diff] [blame] | 3114 | if (trycmd->tcd_cont != 0) |
Bram Moolenaar | c150c09 | 2021-02-13 15:02:46 +0100 | [diff] [blame] | 3115 | // handling :continue: jump to outer try block or |
| 3116 | // start of the loop |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3117 | ectx->ec_iidx = trycmd->tcd_cont - 1; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3118 | } |
| 3119 | } |
| 3120 | break; |
| 3121 | |
| 3122 | case ISN_THROW: |
Bram Moolenaar | 8f81b22 | 2021-01-14 21:47:06 +0100 | [diff] [blame] | 3123 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3124 | garray_T *trystack = &ectx->ec_trystack; |
Bram Moolenaar | 1e021e6 | 2020-10-16 20:25:23 +0200 | [diff] [blame] | 3125 | |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3126 | if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent) |
| 3127 | { |
| 3128 | // throwing an exception while using "silent!" causes |
| 3129 | // the function to abort but not display an error. |
| 3130 | tv = STACK_TV_BOT(-1); |
| 3131 | clear_tv(tv); |
| 3132 | tv->v_type = VAR_NUMBER; |
| 3133 | tv->vval.v_number = 0; |
| 3134 | goto done; |
| 3135 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3136 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3137 | tv = STACK_TV_BOT(0); |
| 3138 | if (tv->vval.v_string == NULL |
| 3139 | || *skipwhite(tv->vval.v_string) == NUL) |
| 3140 | { |
| 3141 | vim_free(tv->vval.v_string); |
| 3142 | SOURCING_LNUM = iptr->isn_lnum; |
| 3143 | emsg(_(e_throw_with_empty_string)); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3144 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | // Inside a "catch" we need to first discard the caught |
| 3148 | // exception. |
| 3149 | if (trystack->ga_len > 0) |
| 3150 | { |
| 3151 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
| 3152 | + trystack->ga_len - 1; |
| 3153 | if (trycmd->tcd_caught && current_exception != NULL) |
| 3154 | { |
| 3155 | // discard the exception |
| 3156 | if (caught_stack == current_exception) |
| 3157 | caught_stack = caught_stack->caught; |
| 3158 | discard_current_exception(); |
| 3159 | trycmd->tcd_caught = FALSE; |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | if (throw_exception(tv->vval.v_string, ET_USER, NULL) |
| 3164 | == FAIL) |
| 3165 | { |
| 3166 | vim_free(tv->vval.v_string); |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3167 | goto theend; |
Bram Moolenaar | 107e9ce | 2021-01-24 20:52:00 +0100 | [diff] [blame] | 3168 | } |
| 3169 | did_throw = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3170 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3171 | break; |
| 3172 | |
| 3173 | // compare with special values |
| 3174 | case ISN_COMPAREBOOL: |
| 3175 | case ISN_COMPARESPECIAL: |
| 3176 | { |
| 3177 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3178 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3179 | varnumber_T arg1 = tv1->vval.v_number; |
| 3180 | varnumber_T arg2 = tv2->vval.v_number; |
| 3181 | int res; |
| 3182 | |
| 3183 | switch (iptr->isn_arg.op.op_type) |
| 3184 | { |
| 3185 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 3186 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 3187 | default: res = 0; break; |
| 3188 | } |
| 3189 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3190 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3191 | tv1->v_type = VAR_BOOL; |
| 3192 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3193 | } |
| 3194 | break; |
| 3195 | |
| 3196 | // Operation with two number arguments |
| 3197 | case ISN_OPNR: |
| 3198 | case ISN_COMPARENR: |
| 3199 | { |
| 3200 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3201 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3202 | varnumber_T arg1 = tv1->vval.v_number; |
| 3203 | varnumber_T arg2 = tv2->vval.v_number; |
| 3204 | varnumber_T res; |
| 3205 | |
| 3206 | switch (iptr->isn_arg.op.op_type) |
| 3207 | { |
| 3208 | case EXPR_MULT: res = arg1 * arg2; break; |
| 3209 | case EXPR_DIV: res = arg1 / arg2; break; |
| 3210 | case EXPR_REM: res = arg1 % arg2; break; |
| 3211 | case EXPR_SUB: res = arg1 - arg2; break; |
| 3212 | case EXPR_ADD: res = arg1 + arg2; break; |
| 3213 | |
| 3214 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 3215 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 3216 | case EXPR_GREATER: res = arg1 > arg2; break; |
| 3217 | case EXPR_GEQUAL: res = arg1 >= arg2; break; |
| 3218 | case EXPR_SMALLER: res = arg1 < arg2; break; |
| 3219 | case EXPR_SEQUAL: res = arg1 <= arg2; break; |
| 3220 | default: res = 0; break; |
| 3221 | } |
| 3222 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3223 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3224 | if (iptr->isn_type == ISN_COMPARENR) |
| 3225 | { |
| 3226 | tv1->v_type = VAR_BOOL; |
| 3227 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 3228 | } |
| 3229 | else |
| 3230 | tv1->vval.v_number = res; |
| 3231 | } |
| 3232 | break; |
| 3233 | |
| 3234 | // Computation with two float arguments |
| 3235 | case ISN_OPFLOAT: |
| 3236 | case ISN_COMPAREFLOAT: |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 3237 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3238 | { |
| 3239 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3240 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3241 | float_T arg1 = tv1->vval.v_float; |
| 3242 | float_T arg2 = tv2->vval.v_float; |
| 3243 | float_T res = 0; |
| 3244 | int cmp = FALSE; |
| 3245 | |
| 3246 | switch (iptr->isn_arg.op.op_type) |
| 3247 | { |
| 3248 | case EXPR_MULT: res = arg1 * arg2; break; |
| 3249 | case EXPR_DIV: res = arg1 / arg2; break; |
| 3250 | case EXPR_SUB: res = arg1 - arg2; break; |
| 3251 | case EXPR_ADD: res = arg1 + arg2; break; |
| 3252 | |
| 3253 | case EXPR_EQUAL: cmp = arg1 == arg2; break; |
| 3254 | case EXPR_NEQUAL: cmp = arg1 != arg2; break; |
| 3255 | case EXPR_GREATER: cmp = arg1 > arg2; break; |
| 3256 | case EXPR_GEQUAL: cmp = arg1 >= arg2; break; |
| 3257 | case EXPR_SMALLER: cmp = arg1 < arg2; break; |
| 3258 | case EXPR_SEQUAL: cmp = arg1 <= arg2; break; |
| 3259 | default: cmp = 0; break; |
| 3260 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3261 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3262 | if (iptr->isn_type == ISN_COMPAREFLOAT) |
| 3263 | { |
| 3264 | tv1->v_type = VAR_BOOL; |
| 3265 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3266 | } |
| 3267 | else |
| 3268 | tv1->vval.v_float = res; |
| 3269 | } |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 3270 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3271 | break; |
| 3272 | |
| 3273 | case ISN_COMPARELIST: |
| 3274 | { |
| 3275 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3276 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3277 | list_T *arg1 = tv1->vval.v_list; |
| 3278 | list_T *arg2 = tv2->vval.v_list; |
| 3279 | int cmp = FALSE; |
| 3280 | int ic = iptr->isn_arg.op.op_ic; |
| 3281 | |
| 3282 | switch (iptr->isn_arg.op.op_type) |
| 3283 | { |
| 3284 | case EXPR_EQUAL: cmp = |
| 3285 | list_equal(arg1, arg2, ic, FALSE); break; |
| 3286 | case EXPR_NEQUAL: cmp = |
| 3287 | !list_equal(arg1, arg2, ic, FALSE); break; |
| 3288 | case EXPR_IS: cmp = arg1 == arg2; break; |
| 3289 | case EXPR_ISNOT: cmp = arg1 != arg2; break; |
| 3290 | default: cmp = 0; break; |
| 3291 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3292 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3293 | clear_tv(tv1); |
| 3294 | clear_tv(tv2); |
| 3295 | tv1->v_type = VAR_BOOL; |
| 3296 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3297 | } |
| 3298 | break; |
| 3299 | |
| 3300 | case ISN_COMPAREBLOB: |
| 3301 | { |
| 3302 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3303 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3304 | blob_T *arg1 = tv1->vval.v_blob; |
| 3305 | blob_T *arg2 = tv2->vval.v_blob; |
| 3306 | int cmp = FALSE; |
| 3307 | |
| 3308 | switch (iptr->isn_arg.op.op_type) |
| 3309 | { |
| 3310 | case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; |
| 3311 | case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; |
| 3312 | case EXPR_IS: cmp = arg1 == arg2; break; |
| 3313 | case EXPR_ISNOT: cmp = arg1 != arg2; break; |
| 3314 | default: cmp = 0; break; |
| 3315 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3316 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3317 | clear_tv(tv1); |
| 3318 | clear_tv(tv2); |
| 3319 | tv1->v_type = VAR_BOOL; |
| 3320 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 3321 | } |
| 3322 | break; |
| 3323 | |
| 3324 | // TODO: handle separately |
| 3325 | case ISN_COMPARESTRING: |
| 3326 | case ISN_COMPAREDICT: |
| 3327 | case ISN_COMPAREFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3328 | case ISN_COMPAREANY: |
| 3329 | { |
| 3330 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3331 | typval_T *tv2 = STACK_TV_BOT(-1); |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3332 | exprtype_T exprtype = iptr->isn_arg.op.op_type; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3333 | int ic = iptr->isn_arg.op.op_ic; |
| 3334 | |
Bram Moolenaar | eb26f43 | 2020-09-14 16:50:05 +0200 | [diff] [blame] | 3335 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 657137c | 2021-01-09 15:45:23 +0100 | [diff] [blame] | 3336 | typval_compare(tv1, tv2, exprtype, ic); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3337 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3338 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3339 | } |
| 3340 | break; |
| 3341 | |
| 3342 | case ISN_ADDLIST: |
| 3343 | case ISN_ADDBLOB: |
| 3344 | { |
| 3345 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3346 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3347 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 3348 | // add two lists or blobs |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3349 | if (iptr->isn_type == ISN_ADDLIST) |
| 3350 | eval_addlist(tv1, tv2); |
| 3351 | else |
| 3352 | eval_addblob(tv1, tv2); |
| 3353 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3354 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3355 | } |
| 3356 | break; |
| 3357 | |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 3358 | case ISN_LISTAPPEND: |
| 3359 | { |
| 3360 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3361 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3362 | list_T *l = tv1->vval.v_list; |
| 3363 | |
| 3364 | // add an item to a list |
| 3365 | if (l == NULL) |
| 3366 | { |
| 3367 | SOURCING_LNUM = iptr->isn_lnum; |
| 3368 | emsg(_(e_cannot_add_to_null_list)); |
| 3369 | goto on_error; |
| 3370 | } |
| 3371 | if (list_append_tv(l, tv2) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3372 | goto theend; |
Bram Moolenaar | 955347c | 2020-10-19 23:01:46 +0200 | [diff] [blame] | 3373 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3374 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 1dcae59 | 2020-10-19 19:02:42 +0200 | [diff] [blame] | 3375 | } |
| 3376 | break; |
| 3377 | |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 3378 | case ISN_BLOBAPPEND: |
| 3379 | { |
| 3380 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3381 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3382 | blob_T *b = tv1->vval.v_blob; |
| 3383 | int error = FALSE; |
| 3384 | varnumber_T n; |
| 3385 | |
| 3386 | // add a number to a blob |
| 3387 | if (b == NULL) |
| 3388 | { |
| 3389 | SOURCING_LNUM = iptr->isn_lnum; |
| 3390 | emsg(_(e_cannot_add_to_null_blob)); |
| 3391 | goto on_error; |
| 3392 | } |
| 3393 | n = tv_get_number_chk(tv2, &error); |
| 3394 | if (error) |
| 3395 | goto on_error; |
| 3396 | ga_append(&b->bv_ga, (int)n); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3397 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 80b0e5e | 2020-10-19 20:45:36 +0200 | [diff] [blame] | 3398 | } |
| 3399 | break; |
| 3400 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3401 | // Computation with two arguments of unknown type |
| 3402 | case ISN_OPANY: |
| 3403 | { |
| 3404 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 3405 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 3406 | varnumber_T n1, n2; |
| 3407 | #ifdef FEAT_FLOAT |
| 3408 | float_T f1 = 0, f2 = 0; |
| 3409 | #endif |
| 3410 | int error = FALSE; |
| 3411 | |
| 3412 | if (iptr->isn_arg.op.op_type == EXPR_ADD) |
| 3413 | { |
| 3414 | if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) |
| 3415 | { |
| 3416 | eval_addlist(tv1, tv2); |
| 3417 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3418 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3419 | break; |
| 3420 | } |
| 3421 | else if (tv1->v_type == VAR_BLOB |
| 3422 | && tv2->v_type == VAR_BLOB) |
| 3423 | { |
| 3424 | eval_addblob(tv1, tv2); |
| 3425 | clear_tv(tv2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3426 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3427 | break; |
| 3428 | } |
| 3429 | } |
| 3430 | #ifdef FEAT_FLOAT |
| 3431 | if (tv1->v_type == VAR_FLOAT) |
| 3432 | { |
| 3433 | f1 = tv1->vval.v_float; |
| 3434 | n1 = 0; |
| 3435 | } |
| 3436 | else |
| 3437 | #endif |
| 3438 | { |
Bram Moolenaar | f665e97 | 2020-12-05 19:17:16 +0100 | [diff] [blame] | 3439 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3440 | n1 = tv_get_number_chk(tv1, &error); |
| 3441 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3442 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3443 | #ifdef FEAT_FLOAT |
| 3444 | if (tv2->v_type == VAR_FLOAT) |
| 3445 | f1 = n1; |
| 3446 | #endif |
| 3447 | } |
| 3448 | #ifdef FEAT_FLOAT |
| 3449 | if (tv2->v_type == VAR_FLOAT) |
| 3450 | { |
| 3451 | f2 = tv2->vval.v_float; |
| 3452 | n2 = 0; |
| 3453 | } |
| 3454 | else |
| 3455 | #endif |
| 3456 | { |
| 3457 | n2 = tv_get_number_chk(tv2, &error); |
| 3458 | if (error) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3459 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3460 | #ifdef FEAT_FLOAT |
| 3461 | if (tv1->v_type == VAR_FLOAT) |
| 3462 | f2 = n2; |
| 3463 | #endif |
| 3464 | } |
| 3465 | #ifdef FEAT_FLOAT |
| 3466 | // if there is a float on either side the result is a float |
| 3467 | if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) |
| 3468 | { |
| 3469 | switch (iptr->isn_arg.op.op_type) |
| 3470 | { |
| 3471 | case EXPR_MULT: f1 = f1 * f2; break; |
| 3472 | case EXPR_DIV: f1 = f1 / f2; break; |
| 3473 | case EXPR_SUB: f1 = f1 - f2; break; |
| 3474 | case EXPR_ADD: f1 = f1 + f2; break; |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3475 | default: SOURCING_LNUM = iptr->isn_lnum; |
| 3476 | emsg(_(e_modulus)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3477 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3478 | } |
| 3479 | clear_tv(tv1); |
| 3480 | clear_tv(tv2); |
| 3481 | tv1->v_type = VAR_FLOAT; |
| 3482 | tv1->vval.v_float = f1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3483 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3484 | } |
| 3485 | else |
| 3486 | #endif |
| 3487 | { |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 3488 | int failed = FALSE; |
| 3489 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3490 | switch (iptr->isn_arg.op.op_type) |
| 3491 | { |
| 3492 | case EXPR_MULT: n1 = n1 * n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 3493 | case EXPR_DIV: n1 = num_divide(n1, n2, &failed); |
| 3494 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 3495 | goto on_error; |
| 3496 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3497 | case EXPR_SUB: n1 = n1 - n2; break; |
| 3498 | case EXPR_ADD: n1 = n1 + n2; break; |
Bram Moolenaar | b1f2857 | 2021-01-21 13:03:20 +0100 | [diff] [blame] | 3499 | default: n1 = num_modulus(n1, n2, &failed); |
| 3500 | if (failed) |
Bram Moolenaar | 99880f9 | 2021-01-20 21:23:14 +0100 | [diff] [blame] | 3501 | goto on_error; |
| 3502 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3503 | } |
| 3504 | clear_tv(tv1); |
| 3505 | clear_tv(tv2); |
| 3506 | tv1->v_type = VAR_NUMBER; |
| 3507 | tv1->vval.v_number = n1; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3508 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3509 | } |
| 3510 | } |
| 3511 | break; |
| 3512 | |
| 3513 | case ISN_CONCAT: |
| 3514 | { |
| 3515 | char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; |
| 3516 | char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; |
| 3517 | char_u *res; |
| 3518 | |
| 3519 | res = concat_str(str1, str2); |
| 3520 | clear_tv(STACK_TV_BOT(-2)); |
| 3521 | clear_tv(STACK_TV_BOT(-1)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3522 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3523 | STACK_TV_BOT(-1)->vval.v_string = res; |
| 3524 | } |
| 3525 | break; |
| 3526 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3527 | case ISN_STRINDEX: |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3528 | case ISN_STRSLICE: |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3529 | { |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3530 | int is_slice = iptr->isn_type == ISN_STRSLICE; |
| 3531 | varnumber_T n1 = 0, n2; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3532 | char_u *res; |
| 3533 | |
| 3534 | // string index: string is at stack-2, index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3535 | // string slice: string is at stack-3, first index at |
| 3536 | // stack-2, second index at stack-1 |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3537 | if (is_slice) |
| 3538 | { |
| 3539 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3540 | n1 = tv->vval.v_number; |
| 3541 | } |
| 3542 | |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3543 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3544 | n2 = tv->vval.v_number; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3545 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3546 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3547 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3548 | if (is_slice) |
| 3549 | // Slice: Select the characters from the string |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 3550 | res = string_slice(tv->vval.v_string, n1, n2, FALSE); |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3551 | else |
| 3552 | // Index: The resulting variable is a string of a |
Bram Moolenaar | 0289a09 | 2021-03-14 18:40:19 +0100 | [diff] [blame] | 3553 | // single character (including composing characters). |
| 3554 | // If the index is too big or negative the result is |
| 3555 | // empty. |
Bram Moolenaar | 11107ba | 2020-08-15 21:10:16 +0200 | [diff] [blame] | 3556 | res = char_from_string(tv->vval.v_string, n2); |
Bram Moolenaar | bf9d8c3 | 2020-07-19 17:55:44 +0200 | [diff] [blame] | 3557 | vim_free(tv->vval.v_string); |
| 3558 | tv->vval.v_string = res; |
| 3559 | } |
| 3560 | break; |
| 3561 | |
| 3562 | case ISN_LISTINDEX: |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3563 | case ISN_LISTSLICE: |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 3564 | case ISN_BLOBINDEX: |
| 3565 | case ISN_BLOBSLICE: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3566 | { |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 3567 | int is_slice = iptr->isn_type == ISN_LISTSLICE |
| 3568 | || iptr->isn_type == ISN_BLOBSLICE; |
| 3569 | int is_blob = iptr->isn_type == ISN_BLOBINDEX |
| 3570 | || iptr->isn_type == ISN_BLOBSLICE; |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3571 | varnumber_T n1, n2; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 3572 | typval_T *val_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3573 | |
| 3574 | // list index: list is at stack-2, index at stack-1 |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3575 | // list slice: list is at stack-3, indexes at stack-2 and |
| 3576 | // stack-1 |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 3577 | // Same for blob. |
| 3578 | val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3579 | |
| 3580 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3581 | n1 = n2 = tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3582 | clear_tv(tv); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3583 | |
| 3584 | if (is_slice) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3585 | { |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3586 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3587 | n1 = tv->vval.v_number; |
| 3588 | clear_tv(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3589 | } |
Bram Moolenaar | ed59187 | 2020-08-15 22:14:53 +0200 | [diff] [blame] | 3590 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3591 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 3592 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 1d63454 | 2020-08-18 13:41:50 +0200 | [diff] [blame] | 3593 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfc3023 | 2021-04-11 20:26:34 +0200 | [diff] [blame] | 3594 | if (is_blob) |
| 3595 | { |
| 3596 | if (blob_slice_or_index(val_tv->vval.v_blob, is_slice, |
| 3597 | n1, n2, FALSE, tv) == FAIL) |
| 3598 | goto on_error; |
| 3599 | } |
| 3600 | else |
| 3601 | { |
| 3602 | if (list_slice_or_index(val_tv->vval.v_list, is_slice, |
| 3603 | n1, n2, FALSE, tv, TRUE) == FAIL) |
| 3604 | goto on_error; |
| 3605 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3606 | } |
| 3607 | break; |
| 3608 | |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3609 | case ISN_ANYINDEX: |
| 3610 | case ISN_ANYSLICE: |
| 3611 | { |
| 3612 | int is_slice = iptr->isn_type == ISN_ANYSLICE; |
| 3613 | typval_T *var1, *var2; |
| 3614 | int res; |
| 3615 | |
| 3616 | // index: composite is at stack-2, index at stack-1 |
| 3617 | // slice: composite is at stack-3, indexes at stack-2 and |
| 3618 | // stack-1 |
| 3619 | tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 3620 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3621 | if (check_can_index(tv, TRUE, TRUE) == FAIL) |
| 3622 | goto on_error; |
| 3623 | var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1); |
| 3624 | var2 = is_slice ? STACK_TV_BOT(-1) : NULL; |
Bram Moolenaar | 6601b62 | 2021-01-13 21:47:15 +0100 | [diff] [blame] | 3625 | res = eval_index_inner(tv, is_slice, var1, var2, |
| 3626 | FALSE, NULL, -1, TRUE); |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3627 | clear_tv(var1); |
| 3628 | if (is_slice) |
| 3629 | clear_tv(var2); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3630 | ectx->ec_stack.ga_len -= is_slice ? 2 : 1; |
Bram Moolenaar | cc673e7 | 2020-08-16 17:33:35 +0200 | [diff] [blame] | 3631 | if (res == FAIL) |
| 3632 | goto on_error; |
| 3633 | } |
| 3634 | break; |
| 3635 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 3636 | case ISN_SLICE: |
| 3637 | { |
| 3638 | list_T *list; |
| 3639 | int count = iptr->isn_arg.number; |
| 3640 | |
Bram Moolenaar | c5b1c20 | 2020-06-18 22:43:27 +0200 | [diff] [blame] | 3641 | // type will have been checked to be a list |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 3642 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 3643 | list = tv->vval.v_list; |
| 3644 | |
| 3645 | // no error for short list, expect it to be checked earlier |
| 3646 | if (list != NULL && list->lv_len >= count) |
| 3647 | { |
| 3648 | list_T *newlist = list_slice(list, |
| 3649 | count, list->lv_len - 1); |
| 3650 | |
| 3651 | if (newlist != NULL) |
| 3652 | { |
| 3653 | list_unref(list); |
| 3654 | tv->vval.v_list = newlist; |
| 3655 | ++newlist->lv_refcount; |
| 3656 | } |
| 3657 | } |
| 3658 | } |
| 3659 | break; |
| 3660 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 3661 | case ISN_GETITEM: |
| 3662 | { |
| 3663 | listitem_T *li; |
| 3664 | int index = iptr->isn_arg.number; |
| 3665 | |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 3666 | // Get list item: list is at stack-1, push item. |
| 3667 | // List type and length is checked for when compiling. |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 3668 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 3669 | li = list_find(tv->vval.v_list, index); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 3670 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3671 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3672 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3673 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 3674 | copy_tv(&li->li_tv, STACK_TV_BOT(-1)); |
Bram Moolenaar | f785aa1 | 2021-02-11 21:19:34 +0100 | [diff] [blame] | 3675 | |
| 3676 | // Useful when used in unpack assignment. Reset at |
| 3677 | // ISN_DROP. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3678 | ectx->ec_where.wt_index = index + 1; |
| 3679 | ectx->ec_where.wt_variable = TRUE; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 3680 | } |
| 3681 | break; |
| 3682 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3683 | case ISN_MEMBER: |
| 3684 | { |
| 3685 | dict_T *dict; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3686 | char_u *key; |
| 3687 | dictitem_T *di; |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 3688 | typval_T temp_tv; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3689 | |
| 3690 | // dict member: dict is at stack-2, key at stack-1 |
| 3691 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 3692 | // no need to check for VAR_DICT, CHECKTYPE will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3693 | dict = tv->vval.v_dict; |
| 3694 | |
| 3695 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 3696 | // no need to check for VAR_STRING, 2STRING will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3697 | key = tv->vval.v_string; |
Bram Moolenaar | 086fc9a | 2020-10-30 18:33:02 +0100 | [diff] [blame] | 3698 | if (key == NULL) |
| 3699 | key = (char_u *)""; |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 3700 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3701 | if ((di = dict_find(dict, key, -1)) == NULL) |
| 3702 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3703 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3704 | semsg(_(e_dictkey), key); |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 3705 | |
| 3706 | // If :silent! is used we will continue, make sure the |
| 3707 | // stack contents makes sense. |
| 3708 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3709 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 4029cab | 2020-12-05 18:13:27 +0100 | [diff] [blame] | 3710 | tv = STACK_TV_BOT(-1); |
| 3711 | clear_tv(tv); |
| 3712 | tv->v_type = VAR_NUMBER; |
| 3713 | tv->vval.v_number = 0; |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 3714 | goto on_fatal_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3715 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3716 | clear_tv(tv); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3717 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 3718 | // Clear the dict only after getting the item, to avoid |
| 3719 | // that it makes the item invalid. |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 3720 | tv = STACK_TV_BOT(-1); |
| 3721 | temp_tv = *tv; |
| 3722 | copy_tv(&di->di_tv, tv); |
| 3723 | clear_tv(&temp_tv); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 3724 | } |
| 3725 | break; |
| 3726 | |
| 3727 | // dict member with string key |
| 3728 | case ISN_STRINGMEMBER: |
| 3729 | { |
| 3730 | dict_T *dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3731 | dictitem_T *di; |
Bram Moolenaar | fb9d5c5 | 2020-07-04 19:19:43 +0200 | [diff] [blame] | 3732 | typval_T temp_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3733 | |
| 3734 | tv = STACK_TV_BOT(-1); |
| 3735 | if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) |
| 3736 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3737 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3738 | emsg(_(e_dictreq)); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3739 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3740 | } |
| 3741 | dict = tv->vval.v_dict; |
| 3742 | |
| 3743 | if ((di = dict_find(dict, iptr->isn_arg.string, -1)) |
| 3744 | == NULL) |
| 3745 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3746 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3747 | semsg(_(e_dictkey), iptr->isn_arg.string); |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 3748 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3749 | } |
Bram Moolenaar | fb9d5c5 | 2020-07-04 19:19:43 +0200 | [diff] [blame] | 3750 | // Clear the dict after getting the item, to avoid that it |
| 3751 | // make the item invalid. |
| 3752 | temp_tv = *tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3753 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | fb9d5c5 | 2020-07-04 19:19:43 +0200 | [diff] [blame] | 3754 | clear_tv(&temp_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3755 | } |
| 3756 | break; |
| 3757 | |
| 3758 | case ISN_NEGATENR: |
| 3759 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 3760 | if (tv->v_type != VAR_NUMBER |
| 3761 | #ifdef FEAT_FLOAT |
| 3762 | && tv->v_type != VAR_FLOAT |
| 3763 | #endif |
| 3764 | ) |
| 3765 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3766 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 3767 | emsg(_(e_number_exp)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3768 | goto on_error; |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 3769 | } |
| 3770 | #ifdef FEAT_FLOAT |
| 3771 | if (tv->v_type == VAR_FLOAT) |
| 3772 | tv->vval.v_float = -tv->vval.v_float; |
| 3773 | else |
| 3774 | #endif |
| 3775 | tv->vval.v_number = -tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3776 | break; |
| 3777 | |
| 3778 | case ISN_CHECKNR: |
| 3779 | { |
| 3780 | int error = FALSE; |
| 3781 | |
| 3782 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 3affe7a | 2020-08-18 20:34:13 +0200 | [diff] [blame] | 3783 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3784 | if (check_not_string(tv) == FAIL) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3785 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3786 | (void)tv_get_number_chk(tv, &error); |
| 3787 | if (error) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3788 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3789 | } |
| 3790 | break; |
| 3791 | |
| 3792 | case ISN_CHECKTYPE: |
| 3793 | { |
| 3794 | checktype_T *ct = &iptr->isn_arg.type; |
| 3795 | |
Bram Moolenaar | b3005ce | 2021-01-22 17:51:06 +0100 | [diff] [blame] | 3796 | tv = STACK_TV_BOT((int)ct->ct_off); |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 3797 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3798 | if (!ectx->ec_where.wt_variable) |
| 3799 | ectx->ec_where.wt_index = ct->ct_arg_idx; |
| 3800 | if (check_typval_type(ct->ct_type, tv, ectx->ec_where) |
| 3801 | == FAIL) |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 3802 | goto on_error; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3803 | if (!ectx->ec_where.wt_variable) |
| 3804 | ectx->ec_where.wt_index = 0; |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 3805 | |
| 3806 | // number 0 is FALSE, number 1 is TRUE |
| 3807 | if (tv->v_type == VAR_NUMBER |
| 3808 | && ct->ct_type->tt_type == VAR_BOOL |
| 3809 | && (tv->vval.v_number == 0 |
| 3810 | || tv->vval.v_number == 1)) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3811 | { |
Bram Moolenaar | 5e65423 | 2020-09-16 15:22:00 +0200 | [diff] [blame] | 3812 | tv->v_type = VAR_BOOL; |
| 3813 | tv->vval.v_number = tv->vval.v_number |
Bram Moolenaar | dadaddd | 2020-09-12 19:11:23 +0200 | [diff] [blame] | 3814 | ? VVAL_TRUE : VVAL_FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3815 | } |
| 3816 | } |
| 3817 | break; |
| 3818 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 3819 | case ISN_CHECKLEN: |
| 3820 | { |
| 3821 | int min_len = iptr->isn_arg.checklen.cl_min_len; |
| 3822 | list_T *list = NULL; |
| 3823 | |
| 3824 | tv = STACK_TV_BOT(-1); |
| 3825 | if (tv->v_type == VAR_LIST) |
| 3826 | list = tv->vval.v_list; |
| 3827 | if (list == NULL || list->lv_len < min_len |
| 3828 | || (list->lv_len > min_len |
| 3829 | && !iptr->isn_arg.checklen.cl_more_OK)) |
| 3830 | { |
Bram Moolenaar | 7517ffd | 2020-08-14 18:35:07 +0200 | [diff] [blame] | 3831 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 3832 | semsg(_(e_expected_nr_items_but_got_nr), |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 3833 | min_len, list == NULL ? 0 : list->lv_len); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 3834 | goto on_error; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | break; |
| 3838 | |
Bram Moolenaar | aa210a3 | 2021-01-02 15:41:03 +0100 | [diff] [blame] | 3839 | case ISN_SETTYPE: |
| 3840 | { |
| 3841 | checktype_T *ct = &iptr->isn_arg.type; |
| 3842 | |
| 3843 | tv = STACK_TV_BOT(-1); |
| 3844 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 3845 | { |
| 3846 | free_type(tv->vval.v_dict->dv_type); |
| 3847 | tv->vval.v_dict->dv_type = alloc_type(ct->ct_type); |
| 3848 | } |
| 3849 | else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL) |
| 3850 | { |
| 3851 | free_type(tv->vval.v_list->lv_type); |
| 3852 | tv->vval.v_list->lv_type = alloc_type(ct->ct_type); |
| 3853 | } |
| 3854 | } |
| 3855 | break; |
| 3856 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3857 | case ISN_2BOOL: |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3858 | case ISN_COND2BOOL: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3859 | { |
| 3860 | int n; |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3861 | int error = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3862 | |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3863 | if (iptr->isn_type == ISN_2BOOL) |
| 3864 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 3865 | tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3866 | n = tv2bool(tv); |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 3867 | if (iptr->isn_arg.tobool.invert) |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3868 | n = !n; |
| 3869 | } |
| 3870 | else |
| 3871 | { |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 3872 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 2bb2658 | 2020-10-03 22:52:39 +0200 | [diff] [blame] | 3873 | SOURCING_LNUM = iptr->isn_lnum; |
| 3874 | n = tv_get_bool_chk(tv, &error); |
| 3875 | if (error) |
| 3876 | goto on_error; |
| 3877 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3878 | clear_tv(tv); |
| 3879 | tv->v_type = VAR_BOOL; |
| 3880 | tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; |
| 3881 | } |
| 3882 | break; |
| 3883 | |
| 3884 | case ISN_2STRING: |
Bram Moolenaar | 418f1df | 2020-08-12 21:34:49 +0200 | [diff] [blame] | 3885 | case ISN_2STRING_ANY: |
Bram Moolenaar | 0acbf5a | 2021-01-05 20:58:25 +0100 | [diff] [blame] | 3886 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 3887 | if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset), |
| 3888 | iptr->isn_type == ISN_2STRING_ANY, |
| 3889 | iptr->isn_arg.tostring.tolerant) == FAIL) |
Bram Moolenaar | 4f5e397 | 2020-12-21 17:30:50 +0100 | [diff] [blame] | 3890 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3891 | break; |
| 3892 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 3893 | case ISN_RANGE: |
| 3894 | { |
| 3895 | exarg_T ea; |
| 3896 | char *errormsg; |
| 3897 | |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 3898 | ea.line2 = 0; |
Bram Moolenaar | d1510ee | 2021-01-04 16:15:58 +0100 | [diff] [blame] | 3899 | ea.addr_count = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 3900 | ea.addr_type = ADDR_LINES; |
| 3901 | ea.cmd = iptr->isn_arg.string; |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 3902 | ea.skip = FALSE; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 3903 | if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | ece0b87 | 2021-01-08 20:40:45 +0100 | [diff] [blame] | 3904 | goto on_error; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 3905 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3906 | if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 3907 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3908 | ++ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 3909 | tv = STACK_TV_BOT(-1); |
| 3910 | tv->v_type = VAR_NUMBER; |
| 3911 | tv->v_lock = 0; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 3912 | if (ea.addr_count == 0) |
| 3913 | tv->vval.v_number = curwin->w_cursor.lnum; |
| 3914 | else |
| 3915 | tv->vval.v_number = ea.line2; |
| 3916 | } |
| 3917 | break; |
| 3918 | |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 3919 | case ISN_PUT: |
| 3920 | { |
| 3921 | int regname = iptr->isn_arg.put.put_regname; |
| 3922 | linenr_T lnum = iptr->isn_arg.put.put_lnum; |
| 3923 | char_u *expr = NULL; |
| 3924 | int dir = FORWARD; |
| 3925 | |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 3926 | if (lnum < -2) |
| 3927 | { |
| 3928 | // line number was put on the stack by ISN_RANGE |
| 3929 | tv = STACK_TV_BOT(-1); |
| 3930 | curwin->w_cursor.lnum = tv->vval.v_number; |
| 3931 | if (lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 3932 | dir = BACKWARD; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3933 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 0859787 | 2020-12-10 19:43:40 +0100 | [diff] [blame] | 3934 | } |
| 3935 | else if (lnum == -2) |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 3936 | // :put! above cursor |
| 3937 | dir = BACKWARD; |
| 3938 | else if (lnum >= 0) |
| 3939 | curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 3940 | |
| 3941 | if (regname == '=') |
| 3942 | { |
| 3943 | tv = STACK_TV_BOT(-1); |
| 3944 | if (tv->v_type == VAR_STRING) |
| 3945 | expr = tv->vval.v_string; |
| 3946 | else |
| 3947 | { |
| 3948 | expr = typval2string(tv, TRUE); // allocates value |
| 3949 | clear_tv(tv); |
| 3950 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3951 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | a28639e | 2021-01-19 22:48:09 +0100 | [diff] [blame] | 3952 | } |
Bram Moolenaar | c3516f7 | 2020-09-08 22:45:35 +0200 | [diff] [blame] | 3953 | check_cursor(); |
| 3954 | do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE); |
| 3955 | vim_free(expr); |
| 3956 | } |
| 3957 | break; |
| 3958 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 3959 | case ISN_CMDMOD: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3960 | ectx->ec_funclocal.floc_save_cmdmod = cmdmod; |
| 3961 | ectx->ec_funclocal.floc_restore_cmdmod = TRUE; |
| 3962 | ectx->ec_funclocal.floc_restore_cmdmod_stacklen = |
| 3963 | ectx->ec_stack.ga_len; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 3964 | cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod; |
| 3965 | apply_cmdmod(&cmdmod); |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 3966 | break; |
| 3967 | |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 3968 | case ISN_CMDMOD_REV: |
| 3969 | // filter regprog is owned by the instruction, don't free it |
| 3970 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 3971 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 3972 | cmdmod = ectx->ec_funclocal.floc_save_cmdmod; |
| 3973 | ectx->ec_funclocal.floc_restore_cmdmod = FALSE; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 3974 | break; |
| 3975 | |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 3976 | case ISN_UNPACK: |
| 3977 | { |
| 3978 | int count = iptr->isn_arg.unpack.unp_count; |
| 3979 | int semicolon = iptr->isn_arg.unpack.unp_semicolon; |
| 3980 | list_T *l; |
| 3981 | listitem_T *li; |
| 3982 | int i; |
| 3983 | |
| 3984 | // Check there is a valid list to unpack. |
| 3985 | tv = STACK_TV_BOT(-1); |
| 3986 | if (tv->v_type != VAR_LIST) |
| 3987 | { |
| 3988 | SOURCING_LNUM = iptr->isn_lnum; |
| 3989 | emsg(_(e_for_argument_must_be_sequence_of_lists)); |
| 3990 | goto on_error; |
| 3991 | } |
| 3992 | l = tv->vval.v_list; |
| 3993 | if (l == NULL |
| 3994 | || l->lv_len < (semicolon ? count - 1 : count)) |
| 3995 | { |
| 3996 | SOURCING_LNUM = iptr->isn_lnum; |
| 3997 | emsg(_(e_list_value_does_not_have_enough_items)); |
| 3998 | goto on_error; |
| 3999 | } |
| 4000 | else if (!semicolon && l->lv_len > count) |
| 4001 | { |
| 4002 | SOURCING_LNUM = iptr->isn_lnum; |
| 4003 | emsg(_(e_list_value_has_more_items_than_targets)); |
| 4004 | goto on_error; |
| 4005 | } |
| 4006 | |
| 4007 | CHECK_LIST_MATERIALIZE(l); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4008 | if (GA_GROW(&ectx->ec_stack, count - 1) == FAIL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4009 | goto theend; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4010 | ectx->ec_stack.ga_len += count - 1; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4011 | |
| 4012 | // Variable after semicolon gets a list with the remaining |
| 4013 | // items. |
| 4014 | if (semicolon) |
| 4015 | { |
| 4016 | list_T *rem_list = |
| 4017 | list_alloc_with_items(l->lv_len - count + 1); |
| 4018 | |
| 4019 | if (rem_list == NULL) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4020 | goto theend; |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4021 | tv = STACK_TV_BOT(-count); |
| 4022 | tv->vval.v_list = rem_list; |
| 4023 | ++rem_list->lv_refcount; |
| 4024 | tv->v_lock = 0; |
| 4025 | li = l->lv_first; |
| 4026 | for (i = 0; i < count - 1; ++i) |
| 4027 | li = li->li_next; |
| 4028 | for (i = 0; li != NULL; ++i) |
| 4029 | { |
| 4030 | list_set_item(rem_list, i, &li->li_tv); |
| 4031 | li = li->li_next; |
| 4032 | } |
| 4033 | --count; |
| 4034 | } |
| 4035 | |
| 4036 | // Produce the values in reverse order, first item last. |
| 4037 | li = l->lv_first; |
| 4038 | for (i = 0; i < count; ++i) |
| 4039 | { |
| 4040 | tv = STACK_TV_BOT(-i - 1); |
| 4041 | copy_tv(&li->li_tv, tv); |
| 4042 | li = li->li_next; |
| 4043 | } |
| 4044 | |
| 4045 | list_unref(l); |
| 4046 | } |
| 4047 | break; |
| 4048 | |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4049 | case ISN_PROF_START: |
| 4050 | case ISN_PROF_END: |
| 4051 | { |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 4052 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4053 | funccall_T cookie; |
| 4054 | ufunc_T *cur_ufunc = |
| 4055 | (((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4056 | + ectx->ec_dfunc_idx)->df_ufunc; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4057 | |
| 4058 | cookie.func = cur_ufunc; |
| 4059 | if (iptr->isn_type == ISN_PROF_START) |
| 4060 | { |
| 4061 | func_line_start(&cookie, iptr->isn_lnum); |
| 4062 | // if we get here the instruction is executed |
| 4063 | func_line_exec(&cookie); |
| 4064 | } |
| 4065 | else |
| 4066 | func_line_end(&cookie); |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 4067 | #endif |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 4068 | } |
| 4069 | break; |
| 4070 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 4071 | case ISN_SHUFFLE: |
| 4072 | { |
Bram Moolenaar | 792f786 | 2020-11-23 08:31:18 +0100 | [diff] [blame] | 4073 | typval_T tmp_tv; |
| 4074 | int item = iptr->isn_arg.shuffle.shfl_item; |
| 4075 | int up = iptr->isn_arg.shuffle.shfl_up; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 4076 | |
| 4077 | tmp_tv = *STACK_TV_BOT(-item); |
| 4078 | for ( ; up > 0 && item > 1; --up) |
| 4079 | { |
| 4080 | *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); |
| 4081 | --item; |
| 4082 | } |
| 4083 | *STACK_TV_BOT(-item) = tmp_tv; |
| 4084 | } |
| 4085 | break; |
| 4086 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4087 | case ISN_DROP: |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4088 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4089 | clear_tv(STACK_TV_BOT(0)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4090 | ectx->ec_where.wt_index = 0; |
| 4091 | ectx->ec_where.wt_variable = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4092 | break; |
| 4093 | } |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4094 | continue; |
| 4095 | |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4096 | func_return: |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 4097 | // Restore previous function. If the frame pointer is where we started |
| 4098 | // then there is none and we are done. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4099 | if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4100 | goto done; |
Bram Moolenaar | 7cbfaa5 | 2020-09-18 21:25:32 +0200 | [diff] [blame] | 4101 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4102 | if (func_return(ectx) == FAIL) |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4103 | // only fails when out of memory |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4104 | goto theend; |
Bram Moolenaar | c7db577 | 2020-07-21 20:55:50 +0200 | [diff] [blame] | 4105 | continue; |
Bram Moolenaar | d032f34 | 2020-07-18 18:13:02 +0200 | [diff] [blame] | 4106 | |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 4107 | on_error: |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4108 | // Jump here for an error that does not require aborting execution. |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4109 | // If "emsg_silent" is set then ignore the error, unless it was set |
| 4110 | // when calling the function. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4111 | if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4112 | && emsg_silent && did_emsg_def == 0) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4113 | { |
| 4114 | // If a sequence of instructions causes an error while ":silent!" |
| 4115 | // was used, restore the stack length and jump ahead to restoring |
| 4116 | // the cmdmod. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4117 | if (ectx->ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4118 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4119 | while (ectx->ec_stack.ga_len |
| 4120 | > ectx->ec_funclocal.floc_restore_cmdmod_stacklen) |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4121 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4122 | --ectx->ec_stack.ga_len; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4123 | clear_tv(STACK_TV_BOT(0)); |
| 4124 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4125 | while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV) |
| 4126 | ++ectx->ec_iidx; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4127 | } |
Bram Moolenaar | cd030c4 | 2020-10-30 21:49:40 +0100 | [diff] [blame] | 4128 | continue; |
Bram Moolenaar | f904133 | 2021-01-21 19:41:16 +0100 | [diff] [blame] | 4129 | } |
Bram Moolenaar | af0df47 | 2020-12-02 20:51:22 +0100 | [diff] [blame] | 4130 | on_fatal_error: |
| 4131 | // Jump here for an error that messes up the stack. |
Bram Moolenaar | 171fb92 | 2020-10-28 16:54:47 +0100 | [diff] [blame] | 4132 | // If we are not inside a try-catch started here, abort execution. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4133 | if (trylevel <= ectx->ec_trylevel_at_start) |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4134 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | done: |
Bram Moolenaar | cbe178e | 2021-05-18 17:49:59 +0200 | [diff] [blame] | 4138 | ret = OK; |
| 4139 | theend: |
| 4140 | ectx->ec_trylevel_at_start = save_trylevel_at_start; |
| 4141 | return ret; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /* |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 4145 | * Execute the instructions from a VAR_INSTR typeval and put the result in |
| 4146 | * "rettv". |
| 4147 | * Return OK or FAIL. |
| 4148 | */ |
| 4149 | int |
| 4150 | exe_typval_instr(typval_T *tv, typval_T *rettv) |
| 4151 | { |
| 4152 | ectx_T *ectx = tv->vval.v_instr->instr_ectx; |
| 4153 | isn_T *save_instr = ectx->ec_instr; |
| 4154 | int save_iidx = ectx->ec_iidx; |
| 4155 | int res; |
| 4156 | |
| 4157 | ectx->ec_instr = tv->vval.v_instr->instr_instr; |
| 4158 | res = exec_instructions(ectx); |
| 4159 | if (res == OK) |
| 4160 | { |
| 4161 | *rettv = *STACK_TV_BOT(-1); |
| 4162 | --ectx->ec_stack.ga_len; |
| 4163 | } |
| 4164 | |
| 4165 | ectx->ec_instr = save_instr; |
| 4166 | ectx->ec_iidx = save_iidx; |
| 4167 | |
| 4168 | return res; |
| 4169 | } |
| 4170 | |
| 4171 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4172 | * Execute the instructions from an ISN_SUBSTITUTE command, which are in |
| 4173 | * "substitute_instr". |
| 4174 | */ |
| 4175 | char_u * |
| 4176 | exe_substitute_instr(void) |
| 4177 | { |
| 4178 | ectx_T *ectx = substitute_instr->subs_ectx; |
| 4179 | isn_T *save_instr = ectx->ec_instr; |
| 4180 | int save_iidx = ectx->ec_iidx; |
| 4181 | char_u *res; |
| 4182 | |
| 4183 | ectx->ec_instr = substitute_instr->subs_instr; |
| 4184 | if (exec_instructions(ectx) == OK) |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4185 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4186 | typval_T *tv = STACK_TV_BOT(-1); |
| 4187 | |
Bram Moolenaar | 2752360 | 2021-06-05 21:36:19 +0200 | [diff] [blame] | 4188 | res = typval2string(tv, TRUE); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4189 | --ectx->ec_stack.ga_len; |
| 4190 | clear_tv(tv); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4191 | } |
| 4192 | else |
| 4193 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4194 | substitute_instr->subs_status = FAIL; |
| 4195 | res = vim_strsave((char_u *)""); |
Bram Moolenaar | 90193e6 | 2021-04-04 20:49:50 +0200 | [diff] [blame] | 4196 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4197 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4198 | ectx->ec_instr = save_instr; |
| 4199 | ectx->ec_iidx = save_iidx; |
| 4200 | |
| 4201 | return res; |
| 4202 | } |
| 4203 | |
| 4204 | /* |
| 4205 | * Call a "def" function from old Vim script. |
| 4206 | * Return OK or FAIL. |
| 4207 | */ |
| 4208 | int |
| 4209 | call_def_function( |
| 4210 | ufunc_T *ufunc, |
| 4211 | int argc_arg, // nr of arguments |
| 4212 | typval_T *argv, // arguments |
| 4213 | partial_T *partial, // optional partial for context |
| 4214 | typval_T *rettv) // return value |
| 4215 | { |
| 4216 | ectx_T ectx; // execution context |
| 4217 | int argc = argc_arg; |
| 4218 | typval_T *tv; |
| 4219 | int idx; |
| 4220 | int ret = FAIL; |
| 4221 | int defcount = ufunc->uf_args.ga_len - argc; |
| 4222 | sctx_T save_current_sctx = current_sctx; |
| 4223 | int did_emsg_before = did_emsg_cumul + did_emsg; |
| 4224 | int save_suppress_errthrow = suppress_errthrow; |
| 4225 | msglist_T **saved_msg_list = NULL; |
| 4226 | msglist_T *private_msg_list = NULL; |
| 4227 | int save_emsg_silent_def = emsg_silent_def; |
| 4228 | int save_did_emsg_def = did_emsg_def; |
| 4229 | int orig_funcdepth; |
| 4230 | |
| 4231 | // Get pointer to item in the stack. |
| 4232 | #undef STACK_TV |
| 4233 | #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) |
| 4234 | |
| 4235 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 4236 | #undef STACK_TV_BOT |
| 4237 | #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) |
| 4238 | |
| 4239 | // Get pointer to a local variable on the stack. Negative for arguments. |
| 4240 | #undef STACK_TV_VAR |
| 4241 | #define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx) |
| 4242 | |
| 4243 | if (ufunc->uf_def_status == UF_NOT_COMPILED |
| 4244 | || ufunc->uf_def_status == UF_COMPILE_ERROR |
| 4245 | || (func_needs_compiling(ufunc, PROFILING(ufunc)) |
| 4246 | && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL) |
| 4247 | == FAIL)) |
| 4248 | { |
| 4249 | if (did_emsg_cumul + did_emsg == did_emsg_before) |
| 4250 | semsg(_(e_function_is_not_compiled_str), |
| 4251 | printable_func_name(ufunc)); |
| 4252 | return FAIL; |
| 4253 | } |
| 4254 | |
| 4255 | { |
| 4256 | // Check the function was really compiled. |
| 4257 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4258 | + ufunc->uf_dfunc_idx; |
| 4259 | if (INSTRUCTIONS(dfunc) == NULL) |
| 4260 | { |
| 4261 | iemsg("using call_def_function() on not compiled function"); |
| 4262 | return FAIL; |
| 4263 | } |
| 4264 | } |
| 4265 | |
| 4266 | // If depth of calling is getting too high, don't execute the function. |
| 4267 | orig_funcdepth = funcdepth_get(); |
| 4268 | if (funcdepth_increment() == FAIL) |
| 4269 | return FAIL; |
| 4270 | |
| 4271 | CLEAR_FIELD(ectx); |
| 4272 | ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; |
| 4273 | ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); |
| 4274 | if (ga_grow(&ectx.ec_stack, 20) == FAIL) |
| 4275 | { |
| 4276 | funcdepth_decrement(); |
| 4277 | return FAIL; |
| 4278 | } |
| 4279 | ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); |
| 4280 | ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10); |
| 4281 | ectx.ec_did_emsg_before = did_emsg_before; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4282 | |
| 4283 | idx = argc - ufunc->uf_args.ga_len; |
| 4284 | if (idx > 0 && ufunc->uf_va_name == NULL) |
| 4285 | { |
| 4286 | if (idx == 1) |
| 4287 | emsg(_(e_one_argument_too_many)); |
| 4288 | else |
| 4289 | semsg(_(e_nr_arguments_too_many), idx); |
| 4290 | goto failed_early; |
| 4291 | } |
Bram Moolenaar | c6d7153 | 2021-06-05 18:49:38 +0200 | [diff] [blame] | 4292 | idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len; |
| 4293 | if (idx < 0) |
Bram Moolenaar | 8da6d6d | 2021-06-05 18:15:09 +0200 | [diff] [blame] | 4294 | { |
| 4295 | if (idx == -1) |
| 4296 | emsg(_(e_one_argument_too_few)); |
| 4297 | else |
| 4298 | semsg(_(e_nr_arguments_too_few), -idx); |
| 4299 | goto failed_early; |
| 4300 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4301 | |
| 4302 | // Put arguments on the stack, but no more than what the function expects. |
| 4303 | // A lambda can be called with more arguments than it uses. |
| 4304 | for (idx = 0; idx < argc |
| 4305 | && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len); |
| 4306 | ++idx) |
| 4307 | { |
| 4308 | if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len |
| 4309 | && argv[idx].v_type == VAR_SPECIAL |
| 4310 | && argv[idx].vval.v_number == VVAL_NONE) |
| 4311 | { |
| 4312 | // Use the default value. |
| 4313 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 4314 | } |
| 4315 | else |
| 4316 | { |
| 4317 | if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len |
| 4318 | && check_typval_arg_type( |
| 4319 | ufunc->uf_arg_types[idx], &argv[idx], idx + 1) == FAIL) |
| 4320 | goto failed_early; |
| 4321 | copy_tv(&argv[idx], STACK_TV_BOT(0)); |
| 4322 | } |
| 4323 | ++ectx.ec_stack.ga_len; |
| 4324 | } |
| 4325 | |
| 4326 | // Turn varargs into a list. Empty list if no args. |
| 4327 | if (ufunc->uf_va_name != NULL) |
| 4328 | { |
| 4329 | int vararg_count = argc - ufunc->uf_args.ga_len; |
| 4330 | |
| 4331 | if (vararg_count < 0) |
| 4332 | vararg_count = 0; |
| 4333 | else |
| 4334 | argc -= vararg_count; |
| 4335 | if (exe_newlist(vararg_count, &ectx) == FAIL) |
| 4336 | goto failed_early; |
| 4337 | |
| 4338 | // Check the type of the list items. |
| 4339 | tv = STACK_TV_BOT(-1); |
| 4340 | if (ufunc->uf_va_type != NULL |
| 4341 | && ufunc->uf_va_type != &t_list_any |
| 4342 | && ufunc->uf_va_type->tt_member != &t_any |
| 4343 | && tv->vval.v_list != NULL) |
| 4344 | { |
| 4345 | type_T *expected = ufunc->uf_va_type->tt_member; |
| 4346 | listitem_T *li = tv->vval.v_list->lv_first; |
| 4347 | |
| 4348 | for (idx = 0; idx < vararg_count; ++idx) |
| 4349 | { |
| 4350 | if (check_typval_arg_type(expected, &li->li_tv, |
| 4351 | argc + idx + 1) == FAIL) |
| 4352 | goto failed_early; |
| 4353 | li = li->li_next; |
| 4354 | } |
| 4355 | } |
| 4356 | |
| 4357 | if (defcount > 0) |
| 4358 | // Move varargs list to below missing default arguments. |
| 4359 | *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1); |
| 4360 | --ectx.ec_stack.ga_len; |
| 4361 | } |
| 4362 | |
| 4363 | // Make space for omitted arguments, will store default value below. |
| 4364 | // Any varargs list goes after them. |
| 4365 | if (defcount > 0) |
| 4366 | for (idx = 0; idx < defcount; ++idx) |
| 4367 | { |
| 4368 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 4369 | ++ectx.ec_stack.ga_len; |
| 4370 | } |
| 4371 | if (ufunc->uf_va_name != NULL) |
| 4372 | ++ectx.ec_stack.ga_len; |
| 4373 | |
| 4374 | // Frame pointer points to just after arguments. |
| 4375 | ectx.ec_frame_idx = ectx.ec_stack.ga_len; |
| 4376 | ectx.ec_initial_frame_idx = ectx.ec_frame_idx; |
| 4377 | |
| 4378 | { |
| 4379 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4380 | + ufunc->uf_dfunc_idx; |
| 4381 | ufunc_T *base_ufunc = dfunc->df_ufunc; |
| 4382 | |
| 4383 | // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done |
| 4384 | // by copy_func(). |
| 4385 | if (partial != NULL || base_ufunc->uf_partial != NULL) |
| 4386 | { |
| 4387 | ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T); |
| 4388 | if (ectx.ec_outer == NULL) |
| 4389 | goto failed_early; |
| 4390 | if (partial != NULL) |
| 4391 | { |
| 4392 | if (partial->pt_outer.out_stack == NULL && current_ectx != NULL) |
| 4393 | { |
| 4394 | if (current_ectx->ec_outer != NULL) |
| 4395 | *ectx.ec_outer = *current_ectx->ec_outer; |
| 4396 | } |
| 4397 | else |
| 4398 | *ectx.ec_outer = partial->pt_outer; |
| 4399 | } |
| 4400 | else |
| 4401 | *ectx.ec_outer = base_ufunc->uf_partial->pt_outer; |
| 4402 | ectx.ec_outer->out_up_is_copy = TRUE; |
| 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | // dummy frame entries |
| 4407 | for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) |
| 4408 | { |
| 4409 | STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; |
| 4410 | ++ectx.ec_stack.ga_len; |
| 4411 | } |
| 4412 | |
| 4413 | { |
| 4414 | // Reserve space for local variables and any closure reference count. |
| 4415 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 4416 | + ufunc->uf_dfunc_idx; |
| 4417 | |
| 4418 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 4419 | STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; |
| 4420 | ectx.ec_stack.ga_len += dfunc->df_varcount; |
| 4421 | if (dfunc->df_has_closure) |
| 4422 | { |
| 4423 | STACK_TV_VAR(idx)->v_type = VAR_NUMBER; |
| 4424 | STACK_TV_VAR(idx)->vval.v_number = 0; |
| 4425 | ++ectx.ec_stack.ga_len; |
| 4426 | } |
| 4427 | |
| 4428 | ectx.ec_instr = INSTRUCTIONS(dfunc); |
| 4429 | } |
| 4430 | |
| 4431 | // Following errors are in the function, not the caller. |
| 4432 | // Commands behave like vim9script. |
| 4433 | estack_push_ufunc(ufunc, 1); |
| 4434 | current_sctx = ufunc->uf_script_ctx; |
| 4435 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 4436 | |
| 4437 | // Use a specific location for storing error messages to be converted to an |
| 4438 | // exception. |
| 4439 | saved_msg_list = msg_list; |
| 4440 | msg_list = &private_msg_list; |
| 4441 | |
| 4442 | // Do turn errors into exceptions. |
| 4443 | suppress_errthrow = FALSE; |
| 4444 | |
| 4445 | // Do not delete the function while executing it. |
| 4446 | ++ufunc->uf_calls; |
| 4447 | |
| 4448 | // When ":silent!" was used before calling then we still abort the |
| 4449 | // function. If ":silent!" is used in the function then we don't. |
| 4450 | emsg_silent_def = emsg_silent; |
| 4451 | did_emsg_def = 0; |
| 4452 | |
| 4453 | ectx.ec_where.wt_index = 0; |
| 4454 | ectx.ec_where.wt_variable = FALSE; |
| 4455 | |
| 4456 | // Execute the instructions until done. |
| 4457 | ret = exec_instructions(&ectx); |
| 4458 | if (ret == OK) |
| 4459 | { |
| 4460 | // function finished, get result from the stack. |
| 4461 | if (ufunc->uf_ret_type == &t_void) |
| 4462 | { |
| 4463 | rettv->v_type = VAR_VOID; |
| 4464 | } |
| 4465 | else |
| 4466 | { |
| 4467 | tv = STACK_TV_BOT(-1); |
| 4468 | *rettv = *tv; |
| 4469 | tv->v_type = VAR_UNKNOWN; |
| 4470 | } |
| 4471 | } |
| 4472 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 4473 | // When failed need to unwind the call stack. |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4474 | while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx) |
| 4475 | func_return(&ectx); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 4476 | |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 4477 | // Deal with any remaining closures, they may be in use somewhere. |
| 4478 | if (ectx.ec_funcrefs.ga_len > 0) |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 4479 | { |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 4480 | handle_closure_in_use(&ectx, FALSE); |
Bram Moolenaar | f112f30 | 2020-12-20 17:47:52 +0100 | [diff] [blame] | 4481 | ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed? |
| 4482 | } |
Bram Moolenaar | c70bdab | 2020-09-26 19:59:38 +0200 | [diff] [blame] | 4483 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 4484 | estack_pop(); |
| 4485 | current_sctx = save_current_sctx; |
| 4486 | |
Bram Moolenaar | c970e42 | 2021-03-17 15:03:04 +0100 | [diff] [blame] | 4487 | // TODO: when is it safe to delete the function if it is no longer used? |
| 4488 | --ufunc->uf_calls; |
| 4489 | |
Bram Moolenaar | 352134b | 2020-10-17 22:04:08 +0200 | [diff] [blame] | 4490 | if (*msg_list != NULL && saved_msg_list != NULL) |
| 4491 | { |
| 4492 | msglist_T **plist = saved_msg_list; |
| 4493 | |
| 4494 | // Append entries from the current msg_list (uncaught exceptions) to |
| 4495 | // the saved msg_list. |
| 4496 | while (*plist != NULL) |
| 4497 | plist = &(*plist)->next; |
| 4498 | |
| 4499 | *plist = *msg_list; |
| 4500 | } |
| 4501 | msg_list = saved_msg_list; |
| 4502 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4503 | if (ectx.ec_funclocal.floc_restore_cmdmod) |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4504 | { |
| 4505 | cmdmod.cmod_filter_regmatch.regprog = NULL; |
| 4506 | undo_cmdmod(&cmdmod); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4507 | cmdmod = ectx.ec_funclocal.floc_save_cmdmod; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 4508 | } |
Bram Moolenaar | 56602ba | 2020-12-05 21:22:08 +0100 | [diff] [blame] | 4509 | emsg_silent_def = save_emsg_silent_def; |
| 4510 | did_emsg_def += save_did_emsg_def; |
Bram Moolenaar | f4c6e1e | 2020-10-23 18:02:32 +0200 | [diff] [blame] | 4511 | |
Bram Moolenaar | ee8580e | 2020-08-28 17:19:07 +0200 | [diff] [blame] | 4512 | failed_early: |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 4513 | // Free all local variables, but not arguments. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4514 | for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) |
| 4515 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 4516 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4517 | vim_free(ectx.ec_stack.ga_data); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 4518 | vim_free(ectx.ec_trystack.ga_data); |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 4519 | |
Bram Moolenaar | 0186e58 | 2021-01-10 18:33:11 +0100 | [diff] [blame] | 4520 | while (ectx.ec_outer != NULL) |
| 4521 | { |
| 4522 | outer_T *up = ectx.ec_outer->out_up_is_copy |
| 4523 | ? NULL : ectx.ec_outer->out_up; |
| 4524 | |
| 4525 | vim_free(ectx.ec_outer); |
| 4526 | ectx.ec_outer = up; |
| 4527 | } |
| 4528 | |
Bram Moolenaar | 77e5dcc | 2020-09-17 21:29:03 +0200 | [diff] [blame] | 4529 | // Not sure if this is necessary. |
| 4530 | suppress_errthrow = save_suppress_errthrow; |
| 4531 | |
Bram Moolenaar | eeece9e | 2020-11-20 19:26:48 +0100 | [diff] [blame] | 4532 | if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before) |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 4533 | semsg(_(e_unknown_error_while_executing_str), |
Bram Moolenaar | 682d0a1 | 2020-07-19 20:48:59 +0200 | [diff] [blame] | 4534 | printable_func_name(ufunc)); |
Bram Moolenaar | 0ba48e8 | 2020-11-17 18:23:19 +0100 | [diff] [blame] | 4535 | funcdepth_restore(orig_funcdepth); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4536 | return ret; |
| 4537 | } |
| 4538 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 4539 | /* |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4540 | * List instructions "instr" up to "instr_count" or until ISN_FINISH. |
| 4541 | * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE. |
| 4542 | * "pfx" is prefixed to every line. |
| 4543 | */ |
| 4544 | static void |
| 4545 | list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc) |
| 4546 | { |
| 4547 | int line_idx = 0; |
| 4548 | int prev_current = 0; |
| 4549 | int current; |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 4550 | int def_arg_idx = 0; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4551 | |
| 4552 | for (current = 0; current < instr_count; ++current) |
| 4553 | { |
| 4554 | isn_T *iptr = &instr[current]; |
| 4555 | char *line; |
| 4556 | |
| 4557 | if (ufunc != NULL) |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 4558 | { |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4559 | while (line_idx < iptr->isn_lnum |
| 4560 | && line_idx < ufunc->uf_lines.ga_len) |
| 4561 | { |
| 4562 | if (current > prev_current) |
| 4563 | { |
| 4564 | msg_puts("\n\n"); |
| 4565 | prev_current = current; |
| 4566 | } |
| 4567 | line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; |
| 4568 | if (line != NULL) |
| 4569 | msg(line); |
| 4570 | } |
Bram Moolenaar | 9ce47ec | 2021-04-20 22:16:39 +0200 | [diff] [blame] | 4571 | if (iptr->isn_type == ISN_JUMP_IF_ARG_SET) |
| 4572 | { |
| 4573 | int first_def_arg = ufunc->uf_args.ga_len |
| 4574 | - ufunc->uf_def_args.ga_len; |
| 4575 | |
| 4576 | if (def_arg_idx > 0) |
| 4577 | msg_puts("\n\n"); |
| 4578 | msg_start(); |
| 4579 | msg_puts(" "); |
| 4580 | msg_puts(((char **)(ufunc->uf_args.ga_data))[ |
| 4581 | first_def_arg + def_arg_idx]); |
| 4582 | msg_puts(" = "); |
| 4583 | msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]); |
| 4584 | msg_clr_eos(); |
| 4585 | msg_end(); |
| 4586 | } |
| 4587 | } |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4588 | |
| 4589 | switch (iptr->isn_type) |
| 4590 | { |
| 4591 | case ISN_EXEC: |
| 4592 | smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string); |
| 4593 | break; |
Bram Moolenaar | 2067733 | 2021-06-06 17:02:53 +0200 | [diff] [blame] | 4594 | case ISN_EXEC_SPLIT: |
| 4595 | smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string); |
| 4596 | break; |
Bram Moolenaar | 3b1373b | 2021-05-17 00:01:42 +0200 | [diff] [blame] | 4597 | case ISN_LEGACY_EVAL: |
| 4598 | smsg("%s%4d EVAL legacy %s", pfx, current, |
| 4599 | iptr->isn_arg.string); |
| 4600 | break; |
Bram Moolenaar | 2d1c57e | 2021-04-19 20:50:03 +0200 | [diff] [blame] | 4601 | case ISN_REDIRSTART: |
| 4602 | smsg("%s%4d REDIR", pfx, current); |
| 4603 | break; |
| 4604 | case ISN_REDIREND: |
| 4605 | smsg("%s%4d REDIR END%s", pfx, current, |
| 4606 | iptr->isn_arg.number ? " append" : ""); |
| 4607 | break; |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 4608 | case ISN_CEXPR_AUCMD: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 4609 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 4610 | smsg("%s%4d CEXPR pre %s", pfx, current, |
| 4611 | cexpr_get_auname(iptr->isn_arg.number)); |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 4612 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 4613 | break; |
| 4614 | case ISN_CEXPR_CORE: |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 4615 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 4616 | { |
| 4617 | cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref; |
| 4618 | |
| 4619 | smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current, |
| 4620 | cexpr_get_auname(cer->cer_cmdidx), |
| 4621 | cer->cer_forceit ? "!" : "", |
| 4622 | cer->cer_cmdline); |
| 4623 | } |
Bram Moolenaar | b7c9781 | 2021-05-05 22:51:39 +0200 | [diff] [blame] | 4624 | #endif |
Bram Moolenaar | 5f7d4c0 | 2021-05-05 21:31:39 +0200 | [diff] [blame] | 4625 | break; |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 4626 | case ISN_INSTR: |
| 4627 | { |
| 4628 | smsg("%s%4d INSTR", pfx, current); |
| 4629 | list_instructions(" ", iptr->isn_arg.instr, |
| 4630 | INT_MAX, NULL); |
| 4631 | msg(" -------------"); |
| 4632 | } |
| 4633 | break; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4634 | case ISN_SUBSTITUTE: |
| 4635 | { |
| 4636 | subs_T *subs = &iptr->isn_arg.subs; |
| 4637 | |
| 4638 | smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd); |
| 4639 | list_instructions(" ", subs->subs_instr, INT_MAX, NULL); |
| 4640 | msg(" -------------"); |
| 4641 | } |
| 4642 | break; |
| 4643 | case ISN_EXECCONCAT: |
| 4644 | smsg("%s%4d EXECCONCAT %lld", pfx, current, |
| 4645 | (varnumber_T)iptr->isn_arg.number); |
| 4646 | break; |
| 4647 | case ISN_ECHO: |
| 4648 | { |
| 4649 | echo_T *echo = &iptr->isn_arg.echo; |
| 4650 | |
| 4651 | smsg("%s%4d %s %d", pfx, current, |
| 4652 | echo->echo_with_white ? "ECHO" : "ECHON", |
| 4653 | echo->echo_count); |
| 4654 | } |
| 4655 | break; |
| 4656 | case ISN_EXECUTE: |
| 4657 | smsg("%s%4d EXECUTE %lld", pfx, current, |
| 4658 | (varnumber_T)(iptr->isn_arg.number)); |
| 4659 | break; |
| 4660 | case ISN_ECHOMSG: |
| 4661 | smsg("%s%4d ECHOMSG %lld", pfx, current, |
| 4662 | (varnumber_T)(iptr->isn_arg.number)); |
| 4663 | break; |
| 4664 | case ISN_ECHOERR: |
| 4665 | smsg("%s%4d ECHOERR %lld", pfx, current, |
| 4666 | (varnumber_T)(iptr->isn_arg.number)); |
| 4667 | break; |
| 4668 | case ISN_LOAD: |
| 4669 | { |
| 4670 | if (iptr->isn_arg.number < 0) |
| 4671 | smsg("%s%4d LOAD arg[%lld]", pfx, current, |
| 4672 | (varnumber_T)(iptr->isn_arg.number |
| 4673 | + STACK_FRAME_SIZE)); |
| 4674 | else |
| 4675 | smsg("%s%4d LOAD $%lld", pfx, current, |
| 4676 | (varnumber_T)(iptr->isn_arg.number)); |
| 4677 | } |
| 4678 | break; |
| 4679 | case ISN_LOADOUTER: |
| 4680 | { |
| 4681 | if (iptr->isn_arg.number < 0) |
| 4682 | smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current, |
| 4683 | iptr->isn_arg.outer.outer_depth, |
| 4684 | iptr->isn_arg.outer.outer_idx |
| 4685 | + STACK_FRAME_SIZE); |
| 4686 | else |
| 4687 | smsg("%s%4d LOADOUTER level %d $%d", pfx, current, |
| 4688 | iptr->isn_arg.outer.outer_depth, |
| 4689 | iptr->isn_arg.outer.outer_idx); |
| 4690 | } |
| 4691 | break; |
| 4692 | case ISN_LOADV: |
| 4693 | smsg("%s%4d LOADV v:%s", pfx, current, |
| 4694 | get_vim_var_name(iptr->isn_arg.number)); |
| 4695 | break; |
| 4696 | case ISN_LOADSCRIPT: |
| 4697 | { |
| 4698 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 4699 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 4700 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 4701 | + sref->sref_idx; |
| 4702 | |
| 4703 | smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current, |
| 4704 | sv->sv_name, |
| 4705 | sref->sref_idx, |
| 4706 | si->sn_name); |
| 4707 | } |
| 4708 | break; |
| 4709 | case ISN_LOADS: |
| 4710 | { |
| 4711 | scriptitem_T *si = SCRIPT_ITEM( |
| 4712 | iptr->isn_arg.loadstore.ls_sid); |
| 4713 | |
| 4714 | smsg("%s%4d LOADS s:%s from %s", pfx, current, |
| 4715 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 4716 | } |
| 4717 | break; |
| 4718 | case ISN_LOADAUTO: |
| 4719 | smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string); |
| 4720 | break; |
| 4721 | case ISN_LOADG: |
| 4722 | smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string); |
| 4723 | break; |
| 4724 | case ISN_LOADB: |
| 4725 | smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string); |
| 4726 | break; |
| 4727 | case ISN_LOADW: |
| 4728 | smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string); |
| 4729 | break; |
| 4730 | case ISN_LOADT: |
| 4731 | smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string); |
| 4732 | break; |
| 4733 | case ISN_LOADGDICT: |
| 4734 | smsg("%s%4d LOAD g:", pfx, current); |
| 4735 | break; |
| 4736 | case ISN_LOADBDICT: |
| 4737 | smsg("%s%4d LOAD b:", pfx, current); |
| 4738 | break; |
| 4739 | case ISN_LOADWDICT: |
| 4740 | smsg("%s%4d LOAD w:", pfx, current); |
| 4741 | break; |
| 4742 | case ISN_LOADTDICT: |
| 4743 | smsg("%s%4d LOAD t:", pfx, current); |
| 4744 | break; |
| 4745 | case ISN_LOADOPT: |
| 4746 | smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string); |
| 4747 | break; |
| 4748 | case ISN_LOADENV: |
| 4749 | smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string); |
| 4750 | break; |
| 4751 | case ISN_LOADREG: |
| 4752 | smsg("%s%4d LOADREG @%c", pfx, current, (int)(iptr->isn_arg.number)); |
| 4753 | break; |
| 4754 | |
| 4755 | case ISN_STORE: |
| 4756 | if (iptr->isn_arg.number < 0) |
| 4757 | smsg("%s%4d STORE arg[%lld]", pfx, current, |
| 4758 | iptr->isn_arg.number + STACK_FRAME_SIZE); |
| 4759 | else |
| 4760 | smsg("%s%4d STORE $%lld", pfx, current, iptr->isn_arg.number); |
| 4761 | break; |
| 4762 | case ISN_STOREOUTER: |
| 4763 | { |
| 4764 | if (iptr->isn_arg.number < 0) |
| 4765 | smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current, |
| 4766 | iptr->isn_arg.outer.outer_depth, |
| 4767 | iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE); |
| 4768 | else |
| 4769 | smsg("%s%4d STOREOUTER level %d $%d", pfx, current, |
| 4770 | iptr->isn_arg.outer.outer_depth, |
| 4771 | iptr->isn_arg.outer.outer_idx); |
| 4772 | } |
| 4773 | break; |
| 4774 | case ISN_STOREV: |
| 4775 | smsg("%s%4d STOREV v:%s", pfx, current, |
| 4776 | get_vim_var_name(iptr->isn_arg.number)); |
| 4777 | break; |
| 4778 | case ISN_STOREAUTO: |
| 4779 | smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string); |
| 4780 | break; |
| 4781 | case ISN_STOREG: |
| 4782 | smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string); |
| 4783 | break; |
| 4784 | case ISN_STOREB: |
| 4785 | smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string); |
| 4786 | break; |
| 4787 | case ISN_STOREW: |
| 4788 | smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string); |
| 4789 | break; |
| 4790 | case ISN_STORET: |
| 4791 | smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string); |
| 4792 | break; |
| 4793 | case ISN_STORES: |
| 4794 | { |
| 4795 | scriptitem_T *si = SCRIPT_ITEM( |
| 4796 | iptr->isn_arg.loadstore.ls_sid); |
| 4797 | |
| 4798 | smsg("%s%4d STORES %s in %s", pfx, current, |
| 4799 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
| 4800 | } |
| 4801 | break; |
| 4802 | case ISN_STORESCRIPT: |
| 4803 | { |
| 4804 | scriptref_T *sref = iptr->isn_arg.script.scriptref; |
| 4805 | scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid); |
| 4806 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 4807 | + sref->sref_idx; |
| 4808 | |
| 4809 | smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current, |
| 4810 | sv->sv_name, |
| 4811 | sref->sref_idx, |
| 4812 | si->sn_name); |
| 4813 | } |
| 4814 | break; |
| 4815 | case ISN_STOREOPT: |
| 4816 | smsg("%s%4d STOREOPT &%s", pfx, current, |
| 4817 | iptr->isn_arg.storeopt.so_name); |
| 4818 | break; |
| 4819 | case ISN_STOREENV: |
| 4820 | smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string); |
| 4821 | break; |
| 4822 | case ISN_STOREREG: |
| 4823 | smsg("%s%4d STOREREG @%c", pfx, current, (int)iptr->isn_arg.number); |
| 4824 | break; |
| 4825 | case ISN_STORENR: |
| 4826 | smsg("%s%4d STORE %lld in $%d", pfx, current, |
| 4827 | iptr->isn_arg.storenr.stnr_val, |
| 4828 | iptr->isn_arg.storenr.stnr_idx); |
| 4829 | break; |
| 4830 | |
| 4831 | case ISN_STOREINDEX: |
| 4832 | smsg("%s%4d STOREINDEX %s", pfx, current, |
| 4833 | vartype_name(iptr->isn_arg.vartype)); |
| 4834 | break; |
| 4835 | |
| 4836 | case ISN_STORERANGE: |
| 4837 | smsg("%s%4d STORERANGE", pfx, current); |
| 4838 | break; |
| 4839 | |
| 4840 | // constants |
| 4841 | case ISN_PUSHNR: |
| 4842 | smsg("%s%4d PUSHNR %lld", pfx, current, |
| 4843 | (varnumber_T)(iptr->isn_arg.number)); |
| 4844 | break; |
| 4845 | case ISN_PUSHBOOL: |
| 4846 | case ISN_PUSHSPEC: |
| 4847 | smsg("%s%4d PUSH %s", pfx, current, |
| 4848 | get_var_special_name(iptr->isn_arg.number)); |
| 4849 | break; |
| 4850 | case ISN_PUSHF: |
| 4851 | #ifdef FEAT_FLOAT |
| 4852 | smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber); |
| 4853 | #endif |
| 4854 | break; |
| 4855 | case ISN_PUSHS: |
| 4856 | smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string); |
| 4857 | break; |
| 4858 | case ISN_PUSHBLOB: |
| 4859 | { |
| 4860 | char_u *r; |
| 4861 | char_u numbuf[NUMBUFLEN]; |
| 4862 | char_u *tofree; |
| 4863 | |
| 4864 | r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); |
| 4865 | smsg("%s%4d PUSHBLOB %s", pfx, current, r); |
| 4866 | vim_free(tofree); |
| 4867 | } |
| 4868 | break; |
| 4869 | case ISN_PUSHFUNC: |
| 4870 | { |
| 4871 | char *name = (char *)iptr->isn_arg.string; |
| 4872 | |
| 4873 | smsg("%s%4d PUSHFUNC \"%s\"", pfx, current, |
| 4874 | name == NULL ? "[none]" : name); |
| 4875 | } |
| 4876 | break; |
| 4877 | case ISN_PUSHCHANNEL: |
| 4878 | #ifdef FEAT_JOB_CHANNEL |
| 4879 | { |
| 4880 | channel_T *channel = iptr->isn_arg.channel; |
| 4881 | |
| 4882 | smsg("%s%4d PUSHCHANNEL %d", pfx, current, |
| 4883 | channel == NULL ? 0 : channel->ch_id); |
| 4884 | } |
| 4885 | #endif |
| 4886 | break; |
| 4887 | case ISN_PUSHJOB: |
| 4888 | #ifdef FEAT_JOB_CHANNEL |
| 4889 | { |
| 4890 | typval_T tv; |
| 4891 | char_u *name; |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 4892 | char_u buf[NUMBUFLEN]; |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4893 | |
| 4894 | tv.v_type = VAR_JOB; |
| 4895 | tv.vval.v_job = iptr->isn_arg.job; |
Bram Moolenaar | 1328bde | 2021-06-05 20:51:38 +0200 | [diff] [blame] | 4896 | name = job_to_string_buf(&tv, buf); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 4897 | smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name); |
| 4898 | } |
| 4899 | #endif |
| 4900 | break; |
| 4901 | case ISN_PUSHEXC: |
| 4902 | smsg("%s%4d PUSH v:exception", pfx, current); |
| 4903 | break; |
| 4904 | case ISN_UNLET: |
| 4905 | smsg("%s%4d UNLET%s %s", pfx, current, |
| 4906 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 4907 | iptr->isn_arg.unlet.ul_name); |
| 4908 | break; |
| 4909 | case ISN_UNLETENV: |
| 4910 | smsg("%s%4d UNLETENV%s $%s", pfx, current, |
| 4911 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 4912 | iptr->isn_arg.unlet.ul_name); |
| 4913 | break; |
| 4914 | case ISN_UNLETINDEX: |
| 4915 | smsg("%s%4d UNLETINDEX", pfx, current); |
| 4916 | break; |
| 4917 | case ISN_UNLETRANGE: |
| 4918 | smsg("%s%4d UNLETRANGE", pfx, current); |
| 4919 | break; |
| 4920 | case ISN_LOCKCONST: |
| 4921 | smsg("%s%4d LOCKCONST", pfx, current); |
| 4922 | break; |
| 4923 | case ISN_NEWLIST: |
| 4924 | smsg("%s%4d NEWLIST size %lld", pfx, current, |
| 4925 | (varnumber_T)(iptr->isn_arg.number)); |
| 4926 | break; |
| 4927 | case ISN_NEWDICT: |
| 4928 | smsg("%s%4d NEWDICT size %lld", pfx, current, |
| 4929 | (varnumber_T)(iptr->isn_arg.number)); |
| 4930 | break; |
| 4931 | |
| 4932 | // function call |
| 4933 | case ISN_BCALL: |
| 4934 | { |
| 4935 | cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; |
| 4936 | |
| 4937 | smsg("%s%4d BCALL %s(argc %d)", pfx, current, |
| 4938 | internal_func_name(cbfunc->cbf_idx), |
| 4939 | cbfunc->cbf_argcount); |
| 4940 | } |
| 4941 | break; |
| 4942 | case ISN_DCALL: |
| 4943 | { |
| 4944 | cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; |
| 4945 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 4946 | + cdfunc->cdf_idx; |
| 4947 | |
| 4948 | smsg("%s%4d DCALL %s(argc %d)", pfx, current, |
| 4949 | df->df_ufunc->uf_name_exp != NULL |
| 4950 | ? df->df_ufunc->uf_name_exp |
| 4951 | : df->df_ufunc->uf_name, cdfunc->cdf_argcount); |
| 4952 | } |
| 4953 | break; |
| 4954 | case ISN_UCALL: |
| 4955 | { |
| 4956 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 4957 | |
| 4958 | smsg("%s%4d UCALL %s(argc %d)", pfx, current, |
| 4959 | cufunc->cuf_name, cufunc->cuf_argcount); |
| 4960 | } |
| 4961 | break; |
| 4962 | case ISN_PCALL: |
| 4963 | { |
| 4964 | cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; |
| 4965 | |
| 4966 | smsg("%s%4d PCALL%s (argc %d)", pfx, current, |
| 4967 | cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); |
| 4968 | } |
| 4969 | break; |
| 4970 | case ISN_PCALL_END: |
| 4971 | smsg("%s%4d PCALL end", pfx, current); |
| 4972 | break; |
| 4973 | case ISN_RETURN: |
| 4974 | smsg("%s%4d RETURN", pfx, current); |
| 4975 | break; |
| 4976 | case ISN_RETURN_ZERO: |
| 4977 | smsg("%s%4d RETURN 0", pfx, current); |
| 4978 | break; |
| 4979 | case ISN_FUNCREF: |
| 4980 | { |
| 4981 | funcref_T *funcref = &iptr->isn_arg.funcref; |
| 4982 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 4983 | + funcref->fr_func; |
| 4984 | |
| 4985 | smsg("%s%4d FUNCREF %s", pfx, current, df->df_ufunc->uf_name); |
| 4986 | } |
| 4987 | break; |
| 4988 | |
| 4989 | case ISN_NEWFUNC: |
| 4990 | { |
| 4991 | newfunc_T *newfunc = &iptr->isn_arg.newfunc; |
| 4992 | |
| 4993 | smsg("%s%4d NEWFUNC %s %s", pfx, current, |
| 4994 | newfunc->nf_lambda, newfunc->nf_global); |
| 4995 | } |
| 4996 | break; |
| 4997 | |
| 4998 | case ISN_DEF: |
| 4999 | { |
| 5000 | char_u *name = iptr->isn_arg.string; |
| 5001 | |
| 5002 | smsg("%s%4d DEF %s", pfx, current, |
| 5003 | name == NULL ? (char_u *)"" : name); |
| 5004 | } |
| 5005 | break; |
| 5006 | |
| 5007 | case ISN_JUMP: |
| 5008 | { |
| 5009 | char *when = "?"; |
| 5010 | |
| 5011 | switch (iptr->isn_arg.jump.jump_when) |
| 5012 | { |
| 5013 | case JUMP_ALWAYS: |
| 5014 | when = "JUMP"; |
| 5015 | break; |
| 5016 | case JUMP_AND_KEEP_IF_TRUE: |
| 5017 | when = "JUMP_AND_KEEP_IF_TRUE"; |
| 5018 | break; |
| 5019 | case JUMP_IF_FALSE: |
| 5020 | when = "JUMP_IF_FALSE"; |
| 5021 | break; |
| 5022 | case JUMP_AND_KEEP_IF_FALSE: |
| 5023 | when = "JUMP_AND_KEEP_IF_FALSE"; |
| 5024 | break; |
| 5025 | case JUMP_IF_COND_FALSE: |
| 5026 | when = "JUMP_IF_COND_FALSE"; |
| 5027 | break; |
| 5028 | case JUMP_IF_COND_TRUE: |
| 5029 | when = "JUMP_IF_COND_TRUE"; |
| 5030 | break; |
| 5031 | } |
| 5032 | smsg("%s%4d %s -> %d", pfx, current, when, |
| 5033 | iptr->isn_arg.jump.jump_where); |
| 5034 | } |
| 5035 | break; |
| 5036 | |
| 5037 | case ISN_JUMP_IF_ARG_SET: |
| 5038 | smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current, |
| 5039 | iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE, |
| 5040 | iptr->isn_arg.jump.jump_where); |
| 5041 | break; |
| 5042 | |
| 5043 | case ISN_FOR: |
| 5044 | { |
| 5045 | forloop_T *forloop = &iptr->isn_arg.forloop; |
| 5046 | |
| 5047 | smsg("%s%4d FOR $%d -> %d", pfx, current, |
| 5048 | forloop->for_idx, forloop->for_end); |
| 5049 | } |
| 5050 | break; |
| 5051 | |
| 5052 | case ISN_TRY: |
| 5053 | { |
| 5054 | try_T *try = &iptr->isn_arg.try; |
| 5055 | |
| 5056 | if (try->try_ref->try_finally == 0) |
| 5057 | smsg("%s%4d TRY catch -> %d, endtry -> %d", |
| 5058 | pfx, current, |
| 5059 | try->try_ref->try_catch, |
| 5060 | try->try_ref->try_endtry); |
| 5061 | else |
| 5062 | smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d", |
| 5063 | pfx, current, |
| 5064 | try->try_ref->try_catch, |
| 5065 | try->try_ref->try_finally, |
| 5066 | try->try_ref->try_endtry); |
| 5067 | } |
| 5068 | break; |
| 5069 | case ISN_CATCH: |
| 5070 | // TODO |
| 5071 | smsg("%s%4d CATCH", pfx, current); |
| 5072 | break; |
| 5073 | case ISN_TRYCONT: |
| 5074 | { |
| 5075 | trycont_T *trycont = &iptr->isn_arg.trycont; |
| 5076 | |
| 5077 | smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current, |
| 5078 | trycont->tct_levels, |
| 5079 | trycont->tct_levels == 1 ? "" : "s", |
| 5080 | trycont->tct_where); |
| 5081 | } |
| 5082 | break; |
| 5083 | case ISN_FINALLY: |
| 5084 | smsg("%s%4d FINALLY", pfx, current); |
| 5085 | break; |
| 5086 | case ISN_ENDTRY: |
| 5087 | smsg("%s%4d ENDTRY", pfx, current); |
| 5088 | break; |
| 5089 | case ISN_THROW: |
| 5090 | smsg("%s%4d THROW", pfx, current); |
| 5091 | break; |
| 5092 | |
| 5093 | // expression operations on number |
| 5094 | case ISN_OPNR: |
| 5095 | case ISN_OPFLOAT: |
| 5096 | case ISN_OPANY: |
| 5097 | { |
| 5098 | char *what; |
| 5099 | char *ins; |
| 5100 | |
| 5101 | switch (iptr->isn_arg.op.op_type) |
| 5102 | { |
| 5103 | case EXPR_MULT: what = "*"; break; |
| 5104 | case EXPR_DIV: what = "/"; break; |
| 5105 | case EXPR_REM: what = "%"; break; |
| 5106 | case EXPR_SUB: what = "-"; break; |
| 5107 | case EXPR_ADD: what = "+"; break; |
| 5108 | default: what = "???"; break; |
| 5109 | } |
| 5110 | switch (iptr->isn_type) |
| 5111 | { |
| 5112 | case ISN_OPNR: ins = "OPNR"; break; |
| 5113 | case ISN_OPFLOAT: ins = "OPFLOAT"; break; |
| 5114 | case ISN_OPANY: ins = "OPANY"; break; |
| 5115 | default: ins = "???"; break; |
| 5116 | } |
| 5117 | smsg("%s%4d %s %s", pfx, current, ins, what); |
| 5118 | } |
| 5119 | break; |
| 5120 | |
| 5121 | case ISN_COMPAREBOOL: |
| 5122 | case ISN_COMPARESPECIAL: |
| 5123 | case ISN_COMPARENR: |
| 5124 | case ISN_COMPAREFLOAT: |
| 5125 | case ISN_COMPARESTRING: |
| 5126 | case ISN_COMPAREBLOB: |
| 5127 | case ISN_COMPARELIST: |
| 5128 | case ISN_COMPAREDICT: |
| 5129 | case ISN_COMPAREFUNC: |
| 5130 | case ISN_COMPAREANY: |
| 5131 | { |
| 5132 | char *p; |
| 5133 | char buf[10]; |
| 5134 | char *type; |
| 5135 | |
| 5136 | switch (iptr->isn_arg.op.op_type) |
| 5137 | { |
| 5138 | case EXPR_EQUAL: p = "=="; break; |
| 5139 | case EXPR_NEQUAL: p = "!="; break; |
| 5140 | case EXPR_GREATER: p = ">"; break; |
| 5141 | case EXPR_GEQUAL: p = ">="; break; |
| 5142 | case EXPR_SMALLER: p = "<"; break; |
| 5143 | case EXPR_SEQUAL: p = "<="; break; |
| 5144 | case EXPR_MATCH: p = "=~"; break; |
| 5145 | case EXPR_IS: p = "is"; break; |
| 5146 | case EXPR_ISNOT: p = "isnot"; break; |
| 5147 | case EXPR_NOMATCH: p = "!~"; break; |
| 5148 | default: p = "???"; break; |
| 5149 | } |
| 5150 | STRCPY(buf, p); |
| 5151 | if (iptr->isn_arg.op.op_ic == TRUE) |
| 5152 | strcat(buf, "?"); |
| 5153 | switch(iptr->isn_type) |
| 5154 | { |
| 5155 | case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; |
| 5156 | case ISN_COMPARESPECIAL: |
| 5157 | type = "COMPARESPECIAL"; break; |
| 5158 | case ISN_COMPARENR: type = "COMPARENR"; break; |
| 5159 | case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; |
| 5160 | case ISN_COMPARESTRING: |
| 5161 | type = "COMPARESTRING"; break; |
| 5162 | case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; |
| 5163 | case ISN_COMPARELIST: type = "COMPARELIST"; break; |
| 5164 | case ISN_COMPAREDICT: type = "COMPAREDICT"; break; |
| 5165 | case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; |
| 5166 | case ISN_COMPAREANY: type = "COMPAREANY"; break; |
| 5167 | default: type = "???"; break; |
| 5168 | } |
| 5169 | |
| 5170 | smsg("%s%4d %s %s", pfx, current, type, buf); |
| 5171 | } |
| 5172 | break; |
| 5173 | |
| 5174 | case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break; |
| 5175 | case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break; |
| 5176 | |
| 5177 | // expression operations |
| 5178 | case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break; |
| 5179 | case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break; |
| 5180 | case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break; |
| 5181 | case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break; |
| 5182 | case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break; |
| 5183 | case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break; |
| 5184 | case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break; |
| 5185 | case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break; |
| 5186 | case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break; |
| 5187 | case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break; |
| 5188 | case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break; |
| 5189 | case ISN_SLICE: smsg("%s%4d SLICE %lld", |
| 5190 | pfx, current, iptr->isn_arg.number); break; |
| 5191 | case ISN_GETITEM: smsg("%s%4d ITEM %lld", |
| 5192 | pfx, current, iptr->isn_arg.number); break; |
| 5193 | case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break; |
| 5194 | case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current, |
| 5195 | iptr->isn_arg.string); break; |
| 5196 | case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break; |
| 5197 | |
| 5198 | case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break; |
| 5199 | case ISN_CHECKTYPE: |
| 5200 | { |
| 5201 | checktype_T *ct = &iptr->isn_arg.type; |
| 5202 | char *tofree; |
| 5203 | |
| 5204 | if (ct->ct_arg_idx == 0) |
| 5205 | smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current, |
| 5206 | type_name(ct->ct_type, &tofree), |
| 5207 | (int)ct->ct_off); |
| 5208 | else |
| 5209 | smsg("%s%4d CHECKTYPE %s stack[%d] arg %d", pfx, current, |
| 5210 | type_name(ct->ct_type, &tofree), |
| 5211 | (int)ct->ct_off, |
| 5212 | (int)ct->ct_arg_idx); |
| 5213 | vim_free(tofree); |
| 5214 | break; |
| 5215 | } |
| 5216 | case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current, |
| 5217 | iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", |
| 5218 | iptr->isn_arg.checklen.cl_min_len); |
| 5219 | break; |
| 5220 | case ISN_SETTYPE: |
| 5221 | { |
| 5222 | char *tofree; |
| 5223 | |
| 5224 | smsg("%s%4d SETTYPE %s", pfx, current, |
| 5225 | type_name(iptr->isn_arg.type.ct_type, &tofree)); |
| 5226 | vim_free(tofree); |
| 5227 | break; |
| 5228 | } |
| 5229 | case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5230 | case ISN_2BOOL: if (iptr->isn_arg.tobool.invert) |
| 5231 | smsg("%s%4d INVERT %d (!val)", pfx, current, |
| 5232 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5233 | else |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5234 | smsg("%s%4d 2BOOL %d (!!val)", pfx, current, |
| 5235 | iptr->isn_arg.tobool.offset); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5236 | break; |
| 5237 | case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current, |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5238 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5239 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5240 | case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]", |
| 5241 | pfx, current, |
| 5242 | (varnumber_T)(iptr->isn_arg.tostring.offset)); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5243 | break; |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5244 | case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current, |
| 5245 | iptr->isn_arg.string); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5246 | break; |
| 5247 | case ISN_PUT: |
| 5248 | if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE) |
| 5249 | smsg("%s%4d PUT %c above range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5250 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5251 | else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE) |
| 5252 | smsg("%s%4d PUT %c range", |
Bram Moolenaar | 5fa9b24 | 2021-06-04 21:00:32 +0200 | [diff] [blame] | 5253 | pfx, current, iptr->isn_arg.put.put_regname); |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5254 | else |
| 5255 | smsg("%s%4d PUT %c %ld", pfx, current, |
| 5256 | iptr->isn_arg.put.put_regname, |
| 5257 | (long)iptr->isn_arg.put.put_lnum); |
| 5258 | break; |
| 5259 | |
| 5260 | // TODO: summarize modifiers |
| 5261 | case ISN_CMDMOD: |
| 5262 | { |
| 5263 | char_u *buf; |
| 5264 | size_t len = produce_cmdmods( |
| 5265 | NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 5266 | |
| 5267 | buf = alloc(len + 1); |
| 5268 | if (buf != NULL) |
| 5269 | { |
| 5270 | (void)produce_cmdmods( |
| 5271 | buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE); |
| 5272 | smsg("%s%4d CMDMOD %s", pfx, current, buf); |
| 5273 | vim_free(buf); |
| 5274 | } |
| 5275 | break; |
| 5276 | } |
| 5277 | case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break; |
| 5278 | |
| 5279 | case ISN_PROF_START: |
| 5280 | smsg("%s%4d PROFILE START line %d", pfx, current, iptr->isn_lnum); |
| 5281 | break; |
| 5282 | |
| 5283 | case ISN_PROF_END: |
| 5284 | smsg("%s%4d PROFILE END", pfx, current); |
| 5285 | break; |
| 5286 | |
| 5287 | case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current, |
| 5288 | iptr->isn_arg.unpack.unp_count, |
| 5289 | iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : ""); |
| 5290 | break; |
| 5291 | case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current, |
| 5292 | iptr->isn_arg.shuffle.shfl_item, |
| 5293 | iptr->isn_arg.shuffle.shfl_up); |
| 5294 | break; |
| 5295 | case ISN_DROP: smsg("%s%4d DROP", pfx, current); break; |
| 5296 | |
| 5297 | case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE. |
| 5298 | return; |
| 5299 | } |
| 5300 | |
| 5301 | out_flush(); // output one line at a time |
| 5302 | ui_breakcheck(); |
| 5303 | if (got_int) |
| 5304 | break; |
| 5305 | } |
| 5306 | } |
| 5307 | |
| 5308 | /* |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 5309 | * ":disassemble". |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 5310 | * We don't really need this at runtime, but we do have tests that require it, |
| 5311 | * so always include this. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5312 | */ |
| 5313 | void |
| 5314 | ex_disassemble(exarg_T *eap) |
| 5315 | { |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 5316 | char_u *arg = eap->arg; |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 5317 | char_u *fname; |
| 5318 | ufunc_T *ufunc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5319 | dfunc_T *dfunc; |
| 5320 | isn_T *instr; |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5321 | int instr_count; |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 5322 | int is_global = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5323 | |
Bram Moolenaar | bfd6558 | 2020-07-13 18:18:00 +0200 | [diff] [blame] | 5324 | if (STRNCMP(arg, "<lambda>", 8) == 0) |
| 5325 | { |
| 5326 | arg += 8; |
| 5327 | (void)getdigits(&arg); |
| 5328 | fname = vim_strnsave(eap->arg, arg - eap->arg); |
| 5329 | } |
| 5330 | else |
| 5331 | fname = trans_function_name(&arg, &is_global, FALSE, |
Bram Moolenaar | 32b3f82 | 2021-01-06 21:59:39 +0100 | [diff] [blame] | 5332 | TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL); |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 5333 | if (fname == NULL) |
| 5334 | { |
| 5335 | semsg(_(e_invarg2), eap->arg); |
| 5336 | return; |
| 5337 | } |
| 5338 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 5339 | ufunc = find_func(fname, is_global, NULL); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 5340 | if (ufunc == NULL) |
| 5341 | { |
| 5342 | char_u *p = untrans_function_name(fname); |
| 5343 | |
| 5344 | if (p != NULL) |
| 5345 | // Try again without making it script-local. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 5346 | ufunc = find_func(p, FALSE, NULL); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 5347 | } |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 5348 | vim_free(fname); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5349 | if (ufunc == NULL) |
| 5350 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5351 | semsg(_(e_cannot_find_function_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5352 | return; |
| 5353 | } |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5354 | if (func_needs_compiling(ufunc, eap->forceit) |
| 5355 | && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL) |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 5356 | return; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 5357 | if (ufunc->uf_def_status != UF_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5358 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 5359 | semsg(_(e_function_is_not_compiled_str), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5360 | return; |
| 5361 | } |
| 5362 | if (ufunc->uf_name_exp != NULL) |
| 5363 | msg((char *)ufunc->uf_name_exp); |
| 5364 | else |
| 5365 | msg((char *)ufunc->uf_name); |
| 5366 | |
| 5367 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 5368 | #ifdef FEAT_PROFILE |
Bram Moolenaar | b204990 | 2021-01-24 12:53:53 +0100 | [diff] [blame] | 5369 | instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr; |
| 5370 | instr_count = eap->forceit ? dfunc->df_instr_prof_count |
| 5371 | : dfunc->df_instr_count; |
Bram Moolenaar | f002a41 | 2021-01-24 13:34:18 +0100 | [diff] [blame] | 5372 | #else |
| 5373 | instr = dfunc->df_instr; |
| 5374 | instr_count = dfunc->df_instr_count; |
| 5375 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5376 | |
Bram Moolenaar | 4c13721 | 2021-04-19 16:48:48 +0200 | [diff] [blame] | 5377 | list_instructions("", instr, instr_count, ufunc); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | /* |
Bram Moolenaar | 1310660 | 2020-10-04 16:06:05 +0200 | [diff] [blame] | 5381 | * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5382 | * list, etc. Mostly like what JavaScript does, except that empty list and |
| 5383 | * empty dictionary are FALSE. |
| 5384 | */ |
| 5385 | int |
| 5386 | tv2bool(typval_T *tv) |
| 5387 | { |
| 5388 | switch (tv->v_type) |
| 5389 | { |
| 5390 | case VAR_NUMBER: |
| 5391 | return tv->vval.v_number != 0; |
| 5392 | case VAR_FLOAT: |
| 5393 | #ifdef FEAT_FLOAT |
| 5394 | return tv->vval.v_float != 0.0; |
| 5395 | #else |
| 5396 | break; |
| 5397 | #endif |
| 5398 | case VAR_PARTIAL: |
| 5399 | return tv->vval.v_partial != NULL; |
| 5400 | case VAR_FUNC: |
| 5401 | case VAR_STRING: |
| 5402 | return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; |
| 5403 | case VAR_LIST: |
| 5404 | return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; |
| 5405 | case VAR_DICT: |
| 5406 | return tv->vval.v_dict != NULL |
| 5407 | && tv->vval.v_dict->dv_hashtab.ht_used > 0; |
| 5408 | case VAR_BOOL: |
| 5409 | case VAR_SPECIAL: |
| 5410 | return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; |
| 5411 | case VAR_JOB: |
| 5412 | #ifdef FEAT_JOB_CHANNEL |
| 5413 | return tv->vval.v_job != NULL; |
| 5414 | #else |
| 5415 | break; |
| 5416 | #endif |
| 5417 | case VAR_CHANNEL: |
| 5418 | #ifdef FEAT_JOB_CHANNEL |
| 5419 | return tv->vval.v_channel != NULL; |
| 5420 | #else |
| 5421 | break; |
| 5422 | #endif |
| 5423 | case VAR_BLOB: |
| 5424 | return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; |
| 5425 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 5426 | case VAR_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5427 | case VAR_VOID: |
Bram Moolenaar | f18332f | 2021-05-07 17:55:55 +0200 | [diff] [blame] | 5428 | case VAR_INSTR: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5429 | break; |
| 5430 | } |
| 5431 | return FALSE; |
| 5432 | } |
| 5433 | |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 5434 | void |
| 5435 | emsg_using_string_as(typval_T *tv, int as_number) |
| 5436 | { |
| 5437 | semsg(_(as_number ? e_using_string_as_number_str |
| 5438 | : e_using_string_as_bool_str), |
| 5439 | tv->vval.v_string == NULL |
| 5440 | ? (char_u *)"" : tv->vval.v_string); |
| 5441 | } |
| 5442 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5443 | /* |
| 5444 | * If "tv" is a string give an error and return FAIL. |
| 5445 | */ |
| 5446 | int |
| 5447 | check_not_string(typval_T *tv) |
| 5448 | { |
| 5449 | if (tv->v_type == VAR_STRING) |
| 5450 | { |
Bram Moolenaar | ea2d407 | 2020-11-12 12:08:51 +0100 | [diff] [blame] | 5451 | emsg_using_string_as(tv, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5452 | clear_tv(tv); |
| 5453 | return FAIL; |
| 5454 | } |
| 5455 | return OK; |
| 5456 | } |
| 5457 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 5458 | |
| 5459 | #endif // FEAT_EVAL |