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