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