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