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 | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 27 | int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 28 | int tcd_catch_idx; // instruction of the first catch |
| 29 | int tcd_finally_idx; // instruction of the finally block |
| 30 | int tcd_caught; // catch block entered |
| 31 | int tcd_return; // when TRUE return from end of :finally |
| 32 | } trycmd_T; |
| 33 | |
| 34 | |
| 35 | // A stack is used to store: |
| 36 | // - arguments passed to a :def function |
| 37 | // - info about the calling function, to use when returning |
| 38 | // - local variables |
| 39 | // - temporary values |
| 40 | // |
| 41 | // In detail (FP == Frame Pointer): |
| 42 | // arg1 first argument from caller (if present) |
| 43 | // arg2 second argument from caller (if present) |
| 44 | // extra_arg1 any missing optional argument default value |
| 45 | // FP -> cur_func calling function |
| 46 | // current previous instruction pointer |
| 47 | // frame_ptr previous Frame Pointer |
| 48 | // var1 space for local variable |
| 49 | // var2 space for local variable |
| 50 | // .... fixed space for max. number of local variables |
| 51 | // temp temporary values |
| 52 | // .... flexible space for temporary values (can grow big) |
| 53 | |
| 54 | /* |
| 55 | * Execution context. |
| 56 | */ |
| 57 | typedef struct { |
| 58 | garray_T ec_stack; // stack of typval_T values |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 59 | int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 60 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 61 | garray_T *ec_outer_stack; // stack used for closures |
| 62 | int ec_outer_frame; // stack frame in ec_outer_stack |
| 63 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 64 | garray_T ec_trystack; // stack of trycmd_T values |
| 65 | int ec_in_catch; // when TRUE in catch or finally block |
| 66 | |
| 67 | int ec_dfunc_idx; // current function index |
| 68 | isn_T *ec_instr; // array with instructions |
| 69 | int ec_iidx; // index in ec_instr: instruction to execute |
| 70 | } ectx_T; |
| 71 | |
| 72 | // Get pointer to item relative to the bottom of the stack, -1 is the last one. |
| 73 | #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx) |
| 74 | |
| 75 | /* |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 76 | * Return the number of arguments, including optional arguments and any vararg. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 77 | */ |
| 78 | static int |
| 79 | ufunc_argcount(ufunc_T *ufunc) |
| 80 | { |
| 81 | return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0); |
| 82 | } |
| 83 | |
| 84 | /* |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 85 | * Set the instruction index, depending on omitted arguments, where the default |
| 86 | * values are to be computed. If all optional arguments are present, start |
| 87 | * with the function body. |
| 88 | * The expression evaluation is at the start of the instructions: |
| 89 | * 0 -> EVAL default1 |
| 90 | * STORE arg[-2] |
| 91 | * 1 -> EVAL default2 |
| 92 | * STORE arg[-1] |
| 93 | * 2 -> function body |
| 94 | */ |
| 95 | static void |
| 96 | init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx) |
| 97 | { |
| 98 | if (ufunc->uf_def_args.ga_len == 0) |
| 99 | ectx->ec_iidx = 0; |
| 100 | else |
| 101 | { |
| 102 | int defcount = ufunc->uf_args.ga_len - argcount; |
| 103 | |
| 104 | // If there is a varargs argument defcount can be negative, no defaults |
| 105 | // to evaluate then. |
| 106 | if (defcount < 0) |
| 107 | defcount = 0; |
| 108 | ectx->ec_iidx = ufunc->uf_def_arg_idx[ |
| 109 | ufunc->uf_def_args.ga_len - defcount]; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 114 | * Create a new list from "count" items at the bottom of the stack. |
| 115 | * When "count" is zero an empty list is added to the stack. |
| 116 | */ |
| 117 | static int |
| 118 | exe_newlist(int count, ectx_T *ectx) |
| 119 | { |
| 120 | list_T *list = list_alloc_with_items(count); |
| 121 | int idx; |
| 122 | typval_T *tv; |
| 123 | |
| 124 | if (list == NULL) |
| 125 | return FAIL; |
| 126 | for (idx = 0; idx < count; ++idx) |
| 127 | list_set_item(list, idx, STACK_TV_BOT(idx - count)); |
| 128 | |
| 129 | if (count > 0) |
| 130 | ectx->ec_stack.ga_len -= count - 1; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 131 | else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 132 | return FAIL; |
| 133 | else |
| 134 | ++ectx->ec_stack.ga_len; |
| 135 | tv = STACK_TV_BOT(-1); |
| 136 | tv->v_type = VAR_LIST; |
| 137 | tv->vval.v_list = list; |
| 138 | ++list->lv_refcount; |
| 139 | return OK; |
| 140 | } |
| 141 | |
| 142 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 143 | * Call compiled function "cdf_idx" from compiled code. |
| 144 | * |
| 145 | * Stack has: |
| 146 | * - current arguments (already there) |
| 147 | * - omitted optional argument (default values) added here |
| 148 | * - stack frame: |
| 149 | * - pointer to calling function |
| 150 | * - Index of next instruction in calling function |
| 151 | * - previous frame pointer |
| 152 | * - reserved space for local variables |
| 153 | */ |
| 154 | static int |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 155 | call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 156 | { |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 157 | int argcount = argcount_arg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 158 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx; |
| 159 | ufunc_T *ufunc = dfunc->df_ufunc; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 160 | int arg_to_add; |
| 161 | int vararg_count = 0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 162 | int idx; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 163 | estack_T *entry; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 164 | |
| 165 | if (dfunc->df_deleted) |
| 166 | { |
| 167 | emsg_funcname(e_func_deleted, ufunc->uf_name); |
| 168 | return FAIL; |
| 169 | } |
| 170 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 171 | if (ufunc->uf_va_name != NULL) |
| 172 | { |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 173 | // Need to make a list out of the vararg arguments. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 174 | // Stack at time of call with 2 varargs: |
| 175 | // normal_arg |
| 176 | // optional_arg |
| 177 | // vararg_1 |
| 178 | // vararg_2 |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 179 | // After creating the list: |
| 180 | // normal_arg |
| 181 | // optional_arg |
| 182 | // vararg-list |
| 183 | // With missing optional arguments we get: |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 184 | // normal_arg |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 185 | // After creating the list |
| 186 | // normal_arg |
| 187 | // (space for optional_arg) |
| 188 | // vararg-list |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 189 | vararg_count = argcount - ufunc->uf_args.ga_len; |
| 190 | if (vararg_count < 0) |
| 191 | vararg_count = 0; |
| 192 | else |
| 193 | argcount -= vararg_count; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 194 | if (exe_newlist(vararg_count, ectx) == FAIL) |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 195 | return FAIL; |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 196 | |
| 197 | vararg_count = 1; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 200 | arg_to_add = ufunc->uf_args.ga_len - argcount; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 201 | if (arg_to_add < 0) |
| 202 | { |
| 203 | iemsg("Argument count wrong?"); |
| 204 | return FAIL; |
| 205 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 206 | if (ga_grow(&ectx->ec_stack, arg_to_add + 3 |
| 207 | + dfunc->df_varcount + dfunc->df_closure_count) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 208 | return FAIL; |
| 209 | |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 210 | // Move the vararg-list to below the missing optional arguments. |
| 211 | if (vararg_count > 0 && arg_to_add > 0) |
| 212 | *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 213 | |
| 214 | // Reserve space for omitted optional arguments, filled in soon. |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 215 | for (idx = 0; idx < arg_to_add; ++idx) |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 216 | STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 217 | ectx->ec_stack.ga_len += arg_to_add; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 218 | |
| 219 | // Store current execution state in stack frame for ISN_RETURN. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 220 | STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx; |
| 221 | STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 222 | STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx; |
| 223 | ectx->ec_frame_idx = ectx->ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 224 | |
| 225 | // Initialize local variables |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 226 | for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 227 | STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 228 | ectx->ec_stack.ga_len += STACK_FRAME_SIZE |
| 229 | + dfunc->df_varcount + dfunc->df_closure_count; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 230 | |
| 231 | // Set execution state to the start of the called function. |
| 232 | ectx->ec_dfunc_idx = cdf_idx; |
| 233 | ectx->ec_instr = dfunc->df_instr; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 234 | entry = estack_push_ufunc(dfunc->df_ufunc, 1); |
| 235 | if (entry != NULL) |
| 236 | { |
| 237 | // Set the script context to the script where the function was defined. |
| 238 | // TODO: save more than the SID? |
| 239 | entry->es_save_sid = current_sctx.sc_sid; |
| 240 | current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid; |
| 241 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 242 | |
| 243 | // Decide where to start execution, handles optional arguments. |
| 244 | init_instr_idx(ufunc, argcount, ectx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 245 | |
| 246 | return OK; |
| 247 | } |
| 248 | |
| 249 | // Get pointer to item in the stack. |
| 250 | #define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx) |
| 251 | |
| 252 | /* |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 253 | * Used when returning from a function: Check if any closure is still |
| 254 | * referenced. If so then move the arguments and variables to a separate piece |
| 255 | * of stack to be used when the closure is called. |
| 256 | * When "free_arguments" is TRUE the arguments are to be freed. |
| 257 | * Returns FAIL when out of memory. |
| 258 | */ |
| 259 | static int |
| 260 | handle_closure_in_use(ectx_T *ectx, int free_arguments) |
| 261 | { |
| 262 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 263 | + ectx->ec_dfunc_idx; |
| 264 | int argcount = ufunc_argcount(dfunc->df_ufunc); |
| 265 | int top = ectx->ec_frame_idx - argcount; |
| 266 | int idx; |
| 267 | typval_T *tv; |
| 268 | int closure_in_use = FALSE; |
| 269 | |
| 270 | // Check if any created closure is still in use. |
| 271 | for (idx = 0; idx < dfunc->df_closure_count; ++idx) |
| 272 | { |
| 273 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE |
| 274 | + dfunc->df_varcount + idx); |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 275 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL |
| 276 | && tv->vval.v_partial->pt_refcount > 1) |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 277 | { |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 278 | int refcount = tv->vval.v_partial->pt_refcount; |
| 279 | int i; |
| 280 | |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 281 | // A Reference in a local variables doesn't count, it gets |
Bram Moolenaar | 221fcc7 | 2020-05-05 19:46:20 +0200 | [diff] [blame] | 282 | // unreferenced on return. |
| 283 | for (i = 0; i < dfunc->df_varcount; ++i) |
| 284 | { |
| 285 | typval_T *stv = STACK_TV(ectx->ec_frame_idx |
| 286 | + STACK_FRAME_SIZE + i); |
| 287 | if (stv->v_type == VAR_PARTIAL |
| 288 | && tv->vval.v_partial == stv->vval.v_partial) |
| 289 | --refcount; |
| 290 | } |
| 291 | if (refcount > 1) |
| 292 | { |
| 293 | closure_in_use = TRUE; |
| 294 | break; |
| 295 | } |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 296 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (closure_in_use) |
| 300 | { |
| 301 | funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T); |
| 302 | typval_T *stack; |
| 303 | |
| 304 | // A closure is using the arguments and/or local variables. |
| 305 | // Move them to the called function. |
| 306 | if (funcstack == NULL) |
| 307 | return FAIL; |
| 308 | funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE |
| 309 | + dfunc->df_varcount; |
| 310 | stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len); |
| 311 | funcstack->fs_ga.ga_data = stack; |
| 312 | if (stack == NULL) |
| 313 | { |
| 314 | vim_free(funcstack); |
| 315 | return FAIL; |
| 316 | } |
| 317 | |
| 318 | // Move or copy the arguments. |
| 319 | for (idx = 0; idx < argcount; ++idx) |
| 320 | { |
| 321 | tv = STACK_TV(top + idx); |
| 322 | if (free_arguments) |
| 323 | { |
| 324 | *(stack + idx) = *tv; |
| 325 | tv->v_type = VAR_UNKNOWN; |
| 326 | } |
| 327 | else |
| 328 | copy_tv(tv, stack + idx); |
| 329 | } |
| 330 | // Move the local variables. |
| 331 | for (idx = 0; idx < dfunc->df_varcount; ++idx) |
| 332 | { |
| 333 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx); |
Bram Moolenaar | f821dda | 2020-05-06 22:18:17 +0200 | [diff] [blame] | 334 | |
| 335 | // Do not copy a partial created for a local function. |
| 336 | // TODO: this won't work if the closure actually uses it. But when |
| 337 | // keeping it it gets complicated: it will create a reference cycle |
| 338 | // inside the partial, thus needs special handling for garbage |
| 339 | // collection. |
| 340 | if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL) |
| 341 | { |
| 342 | int i; |
| 343 | typval_T *ctv; |
| 344 | |
| 345 | for (i = 0; i < dfunc->df_closure_count; ++i) |
| 346 | { |
| 347 | ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE |
| 348 | + dfunc->df_varcount + i); |
| 349 | if (tv->vval.v_partial == ctv->vval.v_partial) |
| 350 | break; |
| 351 | } |
| 352 | if (i < dfunc->df_closure_count) |
| 353 | { |
| 354 | (stack + argcount + STACK_FRAME_SIZE + idx)->v_type = |
| 355 | VAR_UNKNOWN; |
| 356 | continue; |
| 357 | } |
| 358 | } |
| 359 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 360 | *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv; |
| 361 | tv->v_type = VAR_UNKNOWN; |
| 362 | } |
| 363 | |
| 364 | for (idx = 0; idx < dfunc->df_closure_count; ++idx) |
| 365 | { |
| 366 | tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE |
| 367 | + dfunc->df_varcount + idx); |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 368 | if (tv->v_type == VAR_PARTIAL) |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 369 | { |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 370 | partial_T *partial = tv->vval.v_partial; |
| 371 | |
| 372 | if (partial->pt_refcount > 1) |
| 373 | { |
| 374 | ++funcstack->fs_refcount; |
| 375 | partial->pt_funcstack = funcstack; |
| 376 | partial->pt_ectx_stack = &funcstack->fs_ga; |
| 377 | partial->pt_ectx_frame = ectx->ec_frame_idx - top; |
| 378 | } |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return OK; |
| 384 | } |
| 385 | |
| 386 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 387 | * Return from the current function. |
| 388 | */ |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 389 | static int |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 390 | func_return(ectx_T *ectx) |
| 391 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 392 | int idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 393 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 394 | + ectx->ec_dfunc_idx; |
| 395 | int argcount = ufunc_argcount(dfunc->df_ufunc); |
| 396 | int top = ectx->ec_frame_idx - argcount; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 397 | estack_T *entry; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 398 | |
| 399 | // execution context goes one level up |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 400 | entry = estack_pop(); |
| 401 | if (entry != NULL) |
| 402 | current_sctx.sc_sid = entry->es_save_sid; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 403 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 404 | if (handle_closure_in_use(ectx, TRUE) == FAIL) |
| 405 | return FAIL; |
| 406 | |
| 407 | // Clear the arguments. |
| 408 | for (idx = top; idx < ectx->ec_frame_idx; ++idx) |
| 409 | clear_tv(STACK_TV(idx)); |
| 410 | |
| 411 | // Clear local variables and temp values, but not the return value. |
| 412 | for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 413 | idx < ectx->ec_stack.ga_len - 1; ++idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 414 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 415 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 416 | // Restore the previous frame. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 417 | ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number; |
| 418 | ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number; |
| 419 | ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 420 | dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx; |
| 421 | ectx->ec_instr = dfunc->df_instr; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 422 | |
| 423 | // Reset the stack to the position before the call, move the return value |
| 424 | // to the top of the stack. |
| 425 | idx = ectx->ec_stack.ga_len - 1; |
| 426 | ectx->ec_stack.ga_len = top + 1; |
| 427 | *STACK_TV_BOT(-1) = *STACK_TV(idx); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 428 | |
| 429 | return OK; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | #undef STACK_TV |
| 433 | |
| 434 | /* |
| 435 | * Prepare arguments and rettv for calling a builtin or user function. |
| 436 | */ |
| 437 | static int |
| 438 | call_prepare(int argcount, typval_T *argvars, ectx_T *ectx) |
| 439 | { |
| 440 | int idx; |
| 441 | typval_T *tv; |
| 442 | |
| 443 | // Move arguments from bottom of the stack to argvars[] and add terminator. |
| 444 | for (idx = 0; idx < argcount; ++idx) |
| 445 | argvars[idx] = *STACK_TV_BOT(idx - argcount); |
| 446 | argvars[argcount].v_type = VAR_UNKNOWN; |
| 447 | |
| 448 | // Result replaces the arguments on the stack. |
| 449 | if (argcount > 0) |
| 450 | ectx->ec_stack.ga_len -= argcount - 1; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 451 | else if (GA_GROW(&ectx->ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 452 | return FAIL; |
| 453 | else |
| 454 | ++ectx->ec_stack.ga_len; |
| 455 | |
| 456 | // Default return value is zero. |
| 457 | tv = STACK_TV_BOT(-1); |
| 458 | tv->v_type = VAR_NUMBER; |
| 459 | tv->vval.v_number = 0; |
| 460 | |
| 461 | return OK; |
| 462 | } |
| 463 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 464 | // Ugly global to avoid passing the execution context around through many |
| 465 | // layers. |
| 466 | static ectx_T *current_ectx = NULL; |
| 467 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 468 | /* |
| 469 | * Call a builtin function by index. |
| 470 | */ |
| 471 | static int |
| 472 | call_bfunc(int func_idx, int argcount, ectx_T *ectx) |
| 473 | { |
| 474 | typval_T argvars[MAX_FUNC_ARGS]; |
| 475 | int idx; |
Bram Moolenaar | 8a1c101 | 2020-05-07 14:07:25 +0200 | [diff] [blame] | 476 | int did_emsg_before = did_emsg; |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 477 | ectx_T *prev_ectx = current_ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 478 | |
| 479 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 480 | return FAIL; |
| 481 | |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 482 | // Call the builtin function. Set "current_ectx" so that when it |
| 483 | // recursively invokes call_def_function() a closure context can be set. |
| 484 | current_ectx = ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 485 | call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1)); |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 486 | current_ectx = prev_ectx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 487 | |
| 488 | // Clear the arguments. |
| 489 | for (idx = 0; idx < argcount; ++idx) |
| 490 | clear_tv(&argvars[idx]); |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 491 | |
Bram Moolenaar | 8a1c101 | 2020-05-07 14:07:25 +0200 | [diff] [blame] | 492 | if (did_emsg != did_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 493 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 494 | return OK; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Execute a user defined function. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 499 | * "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] | 500 | */ |
| 501 | static int |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 502 | call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 503 | { |
| 504 | typval_T argvars[MAX_FUNC_ARGS]; |
| 505 | funcexe_T funcexe; |
| 506 | int error; |
| 507 | int idx; |
| 508 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 509 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 510 | && compile_def_function(ufunc, FALSE, NULL) == FAIL) |
| 511 | return FAIL; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 512 | if (ufunc->uf_def_status == UF_COMPILED) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 513 | { |
| 514 | // The function has been compiled, can call it quickly. For a function |
| 515 | // that was defined later: we can call it directly next time. |
| 516 | if (iptr != NULL) |
| 517 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 518 | delete_instr(iptr); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 519 | iptr->isn_type = ISN_DCALL; |
| 520 | iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 521 | iptr->isn_arg.dfunc.cdf_argcount = argcount; |
| 522 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 523 | return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx); |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 524 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 525 | |
| 526 | if (call_prepare(argcount, argvars, ectx) == FAIL) |
| 527 | return FAIL; |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 528 | CLEAR_FIELD(funcexe); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 529 | funcexe.evaluate = TRUE; |
| 530 | |
| 531 | // Call the user function. Result goes in last position on the stack. |
| 532 | // TODO: add selfdict if there is one |
| 533 | error = call_user_func_check(ufunc, argcount, argvars, |
| 534 | STACK_TV_BOT(-1), &funcexe, NULL); |
| 535 | |
| 536 | // Clear the arguments. |
| 537 | for (idx = 0; idx < argcount; ++idx) |
| 538 | clear_tv(&argvars[idx]); |
| 539 | |
| 540 | if (error != FCERR_NONE) |
| 541 | { |
| 542 | user_func_error(error, ufunc->uf_name); |
| 543 | return FAIL; |
| 544 | } |
| 545 | return OK; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Execute a function by "name". |
| 550 | * This can be a builtin function or a user function. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 551 | * "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] | 552 | * Returns FAIL if not found without an error message. |
| 553 | */ |
| 554 | static int |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 555 | call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 556 | { |
| 557 | ufunc_T *ufunc; |
| 558 | |
| 559 | if (builtin_function(name, -1)) |
| 560 | { |
| 561 | int func_idx = find_internal_func(name); |
| 562 | |
| 563 | if (func_idx < 0) |
| 564 | return FAIL; |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 565 | if (check_internal_func(func_idx, argcount) < 0) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 566 | return FAIL; |
| 567 | return call_bfunc(func_idx, argcount, ectx); |
| 568 | } |
| 569 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 570 | ufunc = find_func(name, FALSE, NULL); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 571 | if (ufunc != NULL) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 572 | return call_ufunc(ufunc, argcount, ectx, iptr); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 573 | |
| 574 | return FAIL; |
| 575 | } |
| 576 | |
| 577 | static int |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 578 | call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 579 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 580 | int argcount = argcount_arg; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 581 | char_u *name = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 582 | int called_emsg_before = called_emsg; |
| 583 | |
| 584 | if (tv->v_type == VAR_PARTIAL) |
| 585 | { |
Bram Moolenaar | a90afb9 | 2020-07-15 22:38:56 +0200 | [diff] [blame] | 586 | partial_T *pt = tv->vval.v_partial; |
| 587 | int i; |
| 588 | |
| 589 | if (pt->pt_argc > 0) |
| 590 | { |
| 591 | // Make space for arguments from the partial, shift the "argcount" |
| 592 | // arguments up. |
| 593 | if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL) |
| 594 | return FAIL; |
| 595 | for (i = 1; i <= argcount; ++i) |
| 596 | *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i); |
| 597 | ectx->ec_stack.ga_len += pt->pt_argc; |
| 598 | argcount += pt->pt_argc; |
| 599 | |
| 600 | // copy the arguments from the partial onto the stack |
| 601 | for (i = 0; i < pt->pt_argc; ++i) |
| 602 | copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i)); |
| 603 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 604 | |
| 605 | if (pt->pt_func != NULL) |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 606 | { |
| 607 | int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL); |
| 608 | |
| 609 | // closure may need the function context where it was defined |
| 610 | ectx->ec_outer_stack = pt->pt_ectx_stack; |
| 611 | ectx->ec_outer_frame = pt->pt_ectx_frame; |
| 612 | |
| 613 | return ret; |
| 614 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 615 | name = pt->pt_name; |
| 616 | } |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 617 | else if (tv->v_type == VAR_FUNC) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 618 | name = tv->vval.v_string; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 619 | if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 620 | { |
| 621 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | 015f426 | 2020-05-05 21:25:22 +0200 | [diff] [blame] | 622 | semsg(_(e_unknownfunc), |
| 623 | name == NULL ? (char_u *)"[unknown]" : name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 624 | return FAIL; |
| 625 | } |
| 626 | return OK; |
| 627 | } |
| 628 | |
| 629 | /* |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 630 | * Store "tv" in variable "name". |
| 631 | * This is for s: and g: variables. |
| 632 | */ |
| 633 | static void |
| 634 | store_var(char_u *name, typval_T *tv) |
| 635 | { |
| 636 | funccal_entry_T entry; |
| 637 | |
| 638 | save_funccal(&entry); |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 639 | set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 640 | restore_funccal(); |
| 641 | } |
| 642 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 643 | |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 644 | /* |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 645 | * Execute a function by "name". |
| 646 | * This can be a builtin function, user function or a funcref. |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 647 | * "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] | 648 | */ |
| 649 | static int |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 650 | call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 651 | { |
| 652 | int called_emsg_before = called_emsg; |
| 653 | |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 654 | if (call_by_name(name, argcount, ectx, iptr) == FAIL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 655 | && called_emsg == called_emsg_before) |
| 656 | { |
Bram Moolenaar | 1df8b3f | 2020-04-23 18:13:23 +0200 | [diff] [blame] | 657 | dictitem_T *v; |
| 658 | |
| 659 | v = find_var(name, NULL, FALSE); |
| 660 | if (v == NULL) |
| 661 | { |
| 662 | semsg(_(e_unknownfunc), name); |
| 663 | return FAIL; |
| 664 | } |
| 665 | if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC) |
| 666 | { |
| 667 | semsg(_(e_unknownfunc), name); |
| 668 | return FAIL; |
| 669 | } |
| 670 | return call_partial(&v->di_tv, argcount, ectx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 671 | } |
| 672 | return OK; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Call a "def" function from old Vim script. |
| 677 | * Return OK or FAIL. |
| 678 | */ |
| 679 | int |
| 680 | call_def_function( |
| 681 | ufunc_T *ufunc, |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 682 | int argc_arg, // nr of arguments |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 683 | typval_T *argv, // arguments |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 684 | partial_T *partial, // optional partial for context |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 685 | typval_T *rettv) // return value |
| 686 | { |
| 687 | ectx_T ectx; // execution context |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 688 | int argc = argc_arg; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 689 | int initial_frame_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 690 | typval_T *tv; |
| 691 | int idx; |
| 692 | int ret = FAIL; |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 693 | int defcount = ufunc->uf_args.ga_len - argc; |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 694 | int save_sc_version = current_sctx.sc_version; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 695 | int breakcheck_count = 0; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 696 | int called_emsg_before = called_emsg; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 697 | |
| 698 | // Get pointer to item in the stack. |
| 699 | #define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx) |
| 700 | |
| 701 | // Get pointer to item at the bottom of the stack, -1 is the bottom. |
| 702 | #undef STACK_TV_BOT |
| 703 | #define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx) |
| 704 | |
Bram Moolenaar | 1378fbc | 2020-04-11 20:50:33 +0200 | [diff] [blame] | 705 | // Get pointer to a local variable on the stack. Negative for arguments. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 706 | #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] | 707 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 708 | // Like STACK_TV_VAR but use the outer scope |
| 709 | #define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx) |
| 710 | |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 711 | if (ufunc->uf_def_status == UF_NOT_COMPILED |
| 712 | || (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 713 | && compile_def_function(ufunc, FALSE, NULL) == FAIL)) |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 714 | { |
| 715 | if (called_emsg == called_emsg_before) |
| 716 | semsg(_("E1091: Function is not compiled: %s"), |
| 717 | ufunc->uf_name_exp == NULL |
| 718 | ? ufunc->uf_name : ufunc->uf_name_exp); |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 719 | return FAIL; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 720 | } |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 721 | |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 722 | { |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 723 | // Check the function was really compiled. |
Bram Moolenaar | 09689a0 | 2020-05-09 22:50:08 +0200 | [diff] [blame] | 724 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 725 | + ufunc->uf_dfunc_idx; |
| 726 | if (dfunc->df_instr == NULL) |
| 727 | return FAIL; |
| 728 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 729 | |
Bram Moolenaar | 3b6a6eb | 2020-05-09 23:20:20 +0200 | [diff] [blame] | 730 | CLEAR_FIELD(ectx); |
| 731 | ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx; |
| 732 | ga_init2(&ectx.ec_stack, sizeof(typval_T), 500); |
| 733 | if (ga_grow(&ectx.ec_stack, 20) == FAIL) |
| 734 | return FAIL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 735 | ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10); |
| 736 | |
| 737 | // Put arguments on the stack. |
| 738 | for (idx = 0; idx < argc; ++idx) |
| 739 | { |
| 740 | copy_tv(&argv[idx], STACK_TV_BOT(0)); |
| 741 | ++ectx.ec_stack.ga_len; |
| 742 | } |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 743 | |
| 744 | // Turn varargs into a list. Empty list if no args. |
| 745 | if (ufunc->uf_va_name != NULL) |
| 746 | { |
| 747 | int vararg_count = argc - ufunc->uf_args.ga_len; |
| 748 | |
| 749 | if (vararg_count < 0) |
| 750 | vararg_count = 0; |
| 751 | else |
| 752 | argc -= vararg_count; |
| 753 | if (exe_newlist(vararg_count, &ectx) == FAIL) |
Bram Moolenaar | 1a2f4bf | 2020-04-12 23:09:25 +0200 | [diff] [blame] | 754 | goto failed_early; |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 755 | if (defcount > 0) |
| 756 | // Move varargs list to below missing default arguments. |
| 757 | *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1); |
| 758 | --ectx.ec_stack.ga_len; |
| 759 | } |
| 760 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 761 | // Make space for omitted arguments, will store default value below. |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 762 | // Any varargs list goes after them. |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 763 | if (defcount > 0) |
| 764 | for (idx = 0; idx < defcount; ++idx) |
| 765 | { |
| 766 | STACK_TV_BOT(0)->v_type = VAR_UNKNOWN; |
| 767 | ++ectx.ec_stack.ga_len; |
| 768 | } |
Bram Moolenaar | 23e0325 | 2020-04-12 22:22:31 +0200 | [diff] [blame] | 769 | if (ufunc->uf_va_name != NULL) |
| 770 | ++ectx.ec_stack.ga_len; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 771 | |
| 772 | // Frame pointer points to just after arguments. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 773 | ectx.ec_frame_idx = ectx.ec_stack.ga_len; |
| 774 | initial_frame_idx = ectx.ec_frame_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 775 | |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 776 | if (partial != NULL) |
| 777 | { |
Bram Moolenaar | 08f7a41 | 2020-07-13 20:41:08 +0200 | [diff] [blame] | 778 | if (partial->pt_ectx_stack == NULL && current_ectx != NULL) |
| 779 | { |
| 780 | // TODO: is this always the right way? |
| 781 | ectx.ec_outer_stack = ¤t_ectx->ec_stack; |
| 782 | ectx.ec_outer_frame = current_ectx->ec_frame_idx; |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | ectx.ec_outer_stack = partial->pt_ectx_stack; |
| 787 | ectx.ec_outer_frame = partial->pt_ectx_frame; |
| 788 | } |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 791 | // dummy frame entries |
| 792 | for (idx = 0; idx < STACK_FRAME_SIZE; ++idx) |
| 793 | { |
| 794 | STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN; |
| 795 | ++ectx.ec_stack.ga_len; |
| 796 | } |
| 797 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 798 | { |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 799 | // Reserve space for local variables and closure references. |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 800 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 801 | + ufunc->uf_dfunc_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 802 | int count = dfunc->df_varcount + dfunc->df_closure_count; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 803 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 804 | for (idx = 0; idx < count; ++idx) |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 805 | STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 806 | ectx.ec_stack.ga_len += count; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 807 | |
| 808 | ectx.ec_instr = dfunc->df_instr; |
| 809 | } |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 810 | |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 811 | // Commands behave like vim9script. |
| 812 | current_sctx.sc_version = SCRIPT_VERSION_VIM9; |
| 813 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 814 | // Decide where to start execution, handles optional arguments. |
| 815 | init_instr_idx(ufunc, argc, &ectx); |
| 816 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 817 | for (;;) |
| 818 | { |
| 819 | isn_T *iptr; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 820 | |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 821 | if (++breakcheck_count >= 100) |
| 822 | { |
| 823 | line_breakcheck(); |
| 824 | breakcheck_count = 0; |
| 825 | } |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 826 | if (got_int) |
| 827 | { |
| 828 | // Turn CTRL-C into an exception. |
| 829 | got_int = FALSE; |
Bram Moolenaar | 97acfc7 | 2020-03-22 13:44:28 +0100 | [diff] [blame] | 830 | if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL) |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 831 | goto failed; |
| 832 | did_throw = TRUE; |
| 833 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 834 | |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 835 | if (did_emsg && msg_list != NULL && *msg_list != NULL) |
| 836 | { |
| 837 | // Turn an error message into an exception. |
| 838 | did_emsg = FALSE; |
| 839 | if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL) |
| 840 | goto failed; |
| 841 | did_throw = TRUE; |
| 842 | *msg_list = NULL; |
| 843 | } |
| 844 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 845 | if (did_throw && !ectx.ec_in_catch) |
| 846 | { |
| 847 | garray_T *trystack = &ectx.ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 848 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 849 | |
| 850 | // An exception jumps to the first catch, finally, or returns from |
| 851 | // the current function. |
| 852 | if (trystack->ga_len > 0) |
| 853 | trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 854 | if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 855 | { |
| 856 | // jump to ":catch" or ":finally" |
| 857 | ectx.ec_in_catch = TRUE; |
| 858 | ectx.ec_iidx = trycmd->tcd_catch_idx; |
| 859 | } |
| 860 | else |
| 861 | { |
| 862 | // not inside try or need to return from current functions. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 863 | if (ectx.ec_frame_idx == initial_frame_idx) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 864 | { |
| 865 | // At the toplevel we are done. Push a dummy return value. |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 866 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 867 | goto failed; |
| 868 | tv = STACK_TV_BOT(0); |
| 869 | tv->v_type = VAR_NUMBER; |
| 870 | tv->vval.v_number = 0; |
| 871 | ++ectx.ec_stack.ga_len; |
Bram Moolenaar | 257cc5e | 2020-02-19 17:06:11 +0100 | [diff] [blame] | 872 | need_rethrow = TRUE; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 873 | if (handle_closure_in_use(&ectx, FALSE) == FAIL) |
| 874 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 875 | goto done; |
| 876 | } |
| 877 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 878 | if (func_return(&ectx) == FAIL) |
| 879 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 880 | } |
| 881 | continue; |
| 882 | } |
| 883 | |
| 884 | iptr = &ectx.ec_instr[ectx.ec_iidx++]; |
| 885 | switch (iptr->isn_type) |
| 886 | { |
| 887 | // execute Ex command line |
| 888 | case ISN_EXEC: |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 889 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 890 | do_cmdline_cmd(iptr->isn_arg.string); |
| 891 | break; |
| 892 | |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 893 | // execute Ex command from pieces on the stack |
| 894 | case ISN_EXECCONCAT: |
| 895 | { |
| 896 | int count = iptr->isn_arg.number; |
Bram Moolenaar | 7f6f56f | 2020-04-30 20:21:43 +0200 | [diff] [blame] | 897 | size_t len = 0; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 898 | int pass; |
| 899 | int i; |
| 900 | char_u *cmd = NULL; |
| 901 | char_u *str; |
| 902 | |
| 903 | for (pass = 1; pass <= 2; ++pass) |
| 904 | { |
| 905 | for (i = 0; i < count; ++i) |
| 906 | { |
| 907 | tv = STACK_TV_BOT(i - count); |
| 908 | str = tv->vval.v_string; |
| 909 | if (str != NULL && *str != NUL) |
| 910 | { |
| 911 | if (pass == 2) |
| 912 | STRCPY(cmd + len, str); |
| 913 | len += STRLEN(str); |
| 914 | } |
| 915 | if (pass == 2) |
| 916 | clear_tv(tv); |
| 917 | } |
| 918 | if (pass == 1) |
| 919 | { |
| 920 | cmd = alloc(len + 1); |
| 921 | if (cmd == NULL) |
| 922 | goto failed; |
| 923 | len = 0; |
| 924 | } |
| 925 | } |
| 926 | |
Bram Moolenaar | 6378c4f | 2020-04-26 13:50:41 +0200 | [diff] [blame] | 927 | SOURCING_LNUM = iptr->isn_lnum; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 928 | do_cmdline_cmd(cmd); |
| 929 | vim_free(cmd); |
| 930 | } |
| 931 | break; |
| 932 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 933 | // execute :echo {string} ... |
| 934 | case ISN_ECHO: |
| 935 | { |
| 936 | int count = iptr->isn_arg.echo.echo_count; |
| 937 | int atstart = TRUE; |
| 938 | int needclr = TRUE; |
| 939 | |
| 940 | for (idx = 0; idx < count; ++idx) |
| 941 | { |
| 942 | tv = STACK_TV_BOT(idx - count); |
| 943 | echo_one(tv, iptr->isn_arg.echo.echo_with_white, |
| 944 | &atstart, &needclr); |
| 945 | clear_tv(tv); |
| 946 | } |
Bram Moolenaar | e0807ea | 2020-02-20 22:18:06 +0100 | [diff] [blame] | 947 | if (needclr) |
| 948 | msg_clr_eos(); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 949 | ectx.ec_stack.ga_len -= count; |
| 950 | } |
| 951 | break; |
| 952 | |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 953 | // :execute {string} ... |
| 954 | // :echomsg {string} ... |
| 955 | // :echoerr {string} ... |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 956 | case ISN_EXECUTE: |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 957 | case ISN_ECHOMSG: |
| 958 | case ISN_ECHOERR: |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 959 | { |
| 960 | int count = iptr->isn_arg.number; |
| 961 | garray_T ga; |
| 962 | char_u buf[NUMBUFLEN]; |
| 963 | char_u *p; |
| 964 | int len; |
| 965 | int failed = FALSE; |
| 966 | |
| 967 | ga_init2(&ga, 1, 80); |
| 968 | for (idx = 0; idx < count; ++idx) |
| 969 | { |
| 970 | tv = STACK_TV_BOT(idx - count); |
| 971 | if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB) |
| 972 | { |
| 973 | emsg(_(e_inval_string)); |
| 974 | break; |
| 975 | } |
| 976 | else |
| 977 | p = tv_get_string_buf(tv, buf); |
| 978 | |
| 979 | len = (int)STRLEN(p); |
| 980 | if (ga_grow(&ga, len + 2) == FAIL) |
| 981 | failed = TRUE; |
| 982 | else |
| 983 | { |
| 984 | if (ga.ga_len > 0) |
| 985 | ((char_u *)(ga.ga_data))[ga.ga_len++] = ' '; |
| 986 | STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p); |
| 987 | ga.ga_len += len; |
| 988 | } |
| 989 | clear_tv(tv); |
| 990 | } |
| 991 | ectx.ec_stack.ga_len -= count; |
| 992 | |
| 993 | if (!failed && ga.ga_data != NULL) |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 994 | { |
| 995 | if (iptr->isn_type == ISN_EXECUTE) |
| 996 | do_cmdline_cmd((char_u *)ga.ga_data); |
| 997 | else |
| 998 | { |
| 999 | msg_sb_eol(); |
| 1000 | if (iptr->isn_type == ISN_ECHOMSG) |
| 1001 | { |
| 1002 | msg_attr(ga.ga_data, echo_attr); |
| 1003 | out_flush(); |
| 1004 | } |
| 1005 | else |
| 1006 | { |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1007 | SOURCING_LNUM = iptr->isn_lnum; |
| 1008 | emsg(ga.ga_data); |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | } |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 1012 | ga_clear(&ga); |
| 1013 | } |
| 1014 | break; |
| 1015 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1016 | // load local variable or argument |
| 1017 | case ISN_LOAD: |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1018 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1019 | goto failed; |
| 1020 | copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0)); |
| 1021 | ++ectx.ec_stack.ga_len; |
| 1022 | break; |
| 1023 | |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1024 | // load variable or argument from outer scope |
| 1025 | case ISN_LOADOUTER: |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1026 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1027 | goto failed; |
| 1028 | copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number), |
| 1029 | STACK_TV_BOT(0)); |
| 1030 | ++ectx.ec_stack.ga_len; |
| 1031 | break; |
| 1032 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1033 | // load v: variable |
| 1034 | case ISN_LOADV: |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1035 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1036 | goto failed; |
| 1037 | copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0)); |
| 1038 | ++ectx.ec_stack.ga_len; |
| 1039 | break; |
| 1040 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1041 | // load s: variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1042 | case ISN_LOADSCRIPT: |
| 1043 | { |
| 1044 | scriptitem_T *si = |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1045 | SCRIPT_ITEM(iptr->isn_arg.script.script_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1046 | svar_T *sv; |
| 1047 | |
| 1048 | sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 1049 | + iptr->isn_arg.script.script_idx; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1050 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1051 | goto failed; |
| 1052 | copy_tv(sv->sv_tv, STACK_TV_BOT(0)); |
| 1053 | ++ectx.ec_stack.ga_len; |
| 1054 | } |
| 1055 | break; |
| 1056 | |
| 1057 | // load s: variable in old script |
| 1058 | case ISN_LOADS: |
| 1059 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1060 | hashtab_T *ht = &SCRIPT_VARS( |
| 1061 | iptr->isn_arg.loadstore.ls_sid); |
| 1062 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1063 | dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1064 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1065 | if (di == NULL) |
| 1066 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1067 | semsg(_(e_undefvar), name); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 1068 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1069 | } |
| 1070 | else |
| 1071 | { |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1072 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1073 | goto failed; |
| 1074 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
| 1075 | ++ectx.ec_stack.ga_len; |
| 1076 | } |
| 1077 | } |
| 1078 | break; |
| 1079 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1080 | // load g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1081 | case ISN_LOADG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1082 | case ISN_LOADB: |
| 1083 | case ISN_LOADW: |
| 1084 | case ISN_LOADT: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1085 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1086 | dictitem_T *di = NULL; |
| 1087 | hashtab_T *ht = NULL; |
| 1088 | char namespace; |
| 1089 | switch (iptr->isn_type) |
| 1090 | { |
| 1091 | case ISN_LOADG: |
| 1092 | ht = get_globvar_ht(); |
| 1093 | namespace = 'g'; |
| 1094 | break; |
| 1095 | case ISN_LOADB: |
| 1096 | ht = &curbuf->b_vars->dv_hashtab; |
| 1097 | namespace = 'b'; |
| 1098 | break; |
| 1099 | case ISN_LOADW: |
| 1100 | ht = &curwin->w_vars->dv_hashtab; |
| 1101 | namespace = 'w'; |
| 1102 | break; |
| 1103 | case ISN_LOADT: |
| 1104 | ht = &curtab->tp_vars->dv_hashtab; |
| 1105 | namespace = 't'; |
| 1106 | break; |
| 1107 | default: // Cannot reach here |
| 1108 | goto failed; |
| 1109 | } |
| 1110 | di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1111 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1112 | if (di == NULL) |
| 1113 | { |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1114 | semsg(_("E121: Undefined variable: %c:%s"), |
| 1115 | namespace, iptr->isn_arg.string); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 1116 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1117 | } |
| 1118 | else |
| 1119 | { |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1120 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1121 | goto failed; |
| 1122 | copy_tv(&di->di_tv, STACK_TV_BOT(0)); |
| 1123 | ++ectx.ec_stack.ga_len; |
| 1124 | } |
| 1125 | } |
| 1126 | break; |
| 1127 | |
| 1128 | // load &option |
| 1129 | case ISN_LOADOPT: |
| 1130 | { |
| 1131 | typval_T optval; |
| 1132 | char_u *name = iptr->isn_arg.string; |
| 1133 | |
Bram Moolenaar | a8c1770 | 2020-04-01 21:17:24 +0200 | [diff] [blame] | 1134 | // This is not expected to fail, name is checked during |
| 1135 | // compilation: don't set SOURCING_LNUM. |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1136 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1137 | goto failed; |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1138 | if (eval_option(&name, &optval, TRUE) == FAIL) |
Bram Moolenaar | 58ceca5 | 2020-01-28 22:46:22 +0100 | [diff] [blame] | 1139 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1140 | *STACK_TV_BOT(0) = optval; |
| 1141 | ++ectx.ec_stack.ga_len; |
| 1142 | } |
| 1143 | break; |
| 1144 | |
| 1145 | // load $ENV |
| 1146 | case ISN_LOADENV: |
| 1147 | { |
| 1148 | typval_T optval; |
| 1149 | char_u *name = iptr->isn_arg.string; |
| 1150 | |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1151 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1152 | goto failed; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1153 | // name is always valid, checked when compiling |
Bram Moolenaar | 9a78e6d | 2020-07-01 18:29:55 +0200 | [diff] [blame] | 1154 | (void)eval_env_var(&name, &optval, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1155 | *STACK_TV_BOT(0) = optval; |
| 1156 | ++ectx.ec_stack.ga_len; |
| 1157 | } |
| 1158 | break; |
| 1159 | |
| 1160 | // load @register |
| 1161 | case ISN_LOADREG: |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1162 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1163 | goto failed; |
| 1164 | tv = STACK_TV_BOT(0); |
| 1165 | tv->v_type = VAR_STRING; |
| 1166 | tv->vval.v_string = get_reg_contents( |
| 1167 | iptr->isn_arg.number, GREG_EXPR_SRC); |
| 1168 | ++ectx.ec_stack.ga_len; |
| 1169 | break; |
| 1170 | |
| 1171 | // store local variable |
| 1172 | case ISN_STORE: |
| 1173 | --ectx.ec_stack.ga_len; |
| 1174 | tv = STACK_TV_VAR(iptr->isn_arg.number); |
| 1175 | clear_tv(tv); |
| 1176 | *tv = *STACK_TV_BOT(0); |
| 1177 | break; |
| 1178 | |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 1179 | // store variable or argument in outer scope |
| 1180 | case ISN_STOREOUTER: |
| 1181 | --ectx.ec_stack.ga_len; |
| 1182 | tv = STACK_OUT_TV_VAR(iptr->isn_arg.number); |
| 1183 | clear_tv(tv); |
| 1184 | *tv = *STACK_TV_BOT(0); |
| 1185 | break; |
| 1186 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1187 | // store s: variable in old script |
| 1188 | case ISN_STORES: |
| 1189 | { |
| 1190 | hashtab_T *ht = &SCRIPT_VARS( |
| 1191 | iptr->isn_arg.loadstore.ls_sid); |
| 1192 | char_u *name = iptr->isn_arg.loadstore.ls_name; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1193 | dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1194 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1195 | --ectx.ec_stack.ga_len; |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1196 | if (di == NULL) |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 1197 | store_var(name, STACK_TV_BOT(0)); |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1198 | else |
| 1199 | { |
| 1200 | clear_tv(&di->di_tv); |
| 1201 | di->di_tv = *STACK_TV_BOT(0); |
| 1202 | } |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1203 | } |
| 1204 | break; |
| 1205 | |
| 1206 | // store script-local variable in Vim9 script |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1207 | case ISN_STORESCRIPT: |
| 1208 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1209 | scriptitem_T *si = SCRIPT_ITEM( |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1210 | iptr->isn_arg.script.script_sid); |
| 1211 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 1212 | + iptr->isn_arg.script.script_idx; |
| 1213 | |
| 1214 | --ectx.ec_stack.ga_len; |
| 1215 | clear_tv(sv->sv_tv); |
| 1216 | *sv->sv_tv = *STACK_TV_BOT(0); |
| 1217 | } |
| 1218 | break; |
| 1219 | |
| 1220 | // store option |
| 1221 | case ISN_STOREOPT: |
| 1222 | { |
| 1223 | long n = 0; |
| 1224 | char_u *s = NULL; |
| 1225 | char *msg; |
| 1226 | |
| 1227 | --ectx.ec_stack.ga_len; |
| 1228 | tv = STACK_TV_BOT(0); |
| 1229 | if (tv->v_type == VAR_STRING) |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 1230 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1231 | s = tv->vval.v_string; |
Bram Moolenaar | 97a2af3 | 2020-01-28 22:52:48 +0100 | [diff] [blame] | 1232 | if (s == NULL) |
| 1233 | s = (char_u *)""; |
| 1234 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1235 | else |
Bram Moolenaar | a6e67e4 | 2020-05-15 23:36:40 +0200 | [diff] [blame] | 1236 | // must be VAR_NUMBER, CHECKTYPE makes sure |
| 1237 | n = tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1238 | msg = set_option_value(iptr->isn_arg.storeopt.so_name, |
| 1239 | n, s, iptr->isn_arg.storeopt.so_flags); |
Bram Moolenaar | e75ba26 | 2020-05-16 15:43:31 +0200 | [diff] [blame] | 1240 | clear_tv(tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1241 | if (msg != NULL) |
| 1242 | { |
| 1243 | emsg(_(msg)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1244 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1245 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1246 | } |
| 1247 | break; |
| 1248 | |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1249 | // store $ENV |
| 1250 | case ISN_STOREENV: |
| 1251 | --ectx.ec_stack.ga_len; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1252 | tv = STACK_TV_BOT(0); |
| 1253 | vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv)); |
| 1254 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1255 | break; |
| 1256 | |
| 1257 | // store @r |
| 1258 | case ISN_STOREREG: |
| 1259 | { |
| 1260 | int reg = iptr->isn_arg.number; |
| 1261 | |
| 1262 | --ectx.ec_stack.ga_len; |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 1263 | tv = STACK_TV_BOT(0); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1264 | write_reg_contents(reg == '@' ? '"' : reg, |
Bram Moolenaar | 401d9ff | 2020-02-19 18:14:44 +0100 | [diff] [blame] | 1265 | tv_get_string(tv), -1, FALSE); |
| 1266 | clear_tv(tv); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1267 | } |
| 1268 | break; |
| 1269 | |
| 1270 | // store v: variable |
| 1271 | case ISN_STOREV: |
| 1272 | --ectx.ec_stack.ga_len; |
| 1273 | if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0)) |
| 1274 | == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1275 | // should not happen, type is checked when compiling |
| 1276 | goto on_error; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 1277 | break; |
| 1278 | |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1279 | // store g:/b:/w:/t: variable |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1280 | case ISN_STOREG: |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1281 | case ISN_STOREB: |
| 1282 | case ISN_STOREW: |
| 1283 | case ISN_STORET: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1284 | { |
| 1285 | dictitem_T *di; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 1286 | hashtab_T *ht; |
| 1287 | switch (iptr->isn_type) |
| 1288 | { |
| 1289 | case ISN_STOREG: |
| 1290 | ht = get_globvar_ht(); |
| 1291 | break; |
| 1292 | case ISN_STOREB: |
| 1293 | ht = &curbuf->b_vars->dv_hashtab; |
| 1294 | break; |
| 1295 | case ISN_STOREW: |
| 1296 | ht = &curwin->w_vars->dv_hashtab; |
| 1297 | break; |
| 1298 | case ISN_STORET: |
| 1299 | ht = &curtab->tp_vars->dv_hashtab; |
| 1300 | break; |
| 1301 | default: // Cannot reach here |
| 1302 | goto failed; |
| 1303 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1304 | |
| 1305 | --ectx.ec_stack.ga_len; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1306 | di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1307 | if (di == NULL) |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 1308 | store_var(iptr->isn_arg.string, STACK_TV_BOT(0)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1309 | else |
| 1310 | { |
| 1311 | clear_tv(&di->di_tv); |
| 1312 | di->di_tv = *STACK_TV_BOT(0); |
| 1313 | } |
| 1314 | } |
| 1315 | break; |
| 1316 | |
| 1317 | // store number in local variable |
| 1318 | case ISN_STORENR: |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1319 | tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1320 | clear_tv(tv); |
| 1321 | tv->v_type = VAR_NUMBER; |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 1322 | tv->vval.v_number = iptr->isn_arg.storenr.stnr_val; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1323 | break; |
| 1324 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1325 | // store value in list variable |
| 1326 | case ISN_STORELIST: |
| 1327 | { |
| 1328 | typval_T *tv_idx = STACK_TV_BOT(-2); |
| 1329 | varnumber_T lidx = tv_idx->vval.v_number; |
| 1330 | typval_T *tv_list = STACK_TV_BOT(-1); |
| 1331 | list_T *list = tv_list->vval.v_list; |
| 1332 | |
| 1333 | if (lidx < 0 && list->lv_len + lidx >= 0) |
| 1334 | // negative index is relative to the end |
| 1335 | lidx = list->lv_len + lidx; |
| 1336 | if (lidx < 0 || lidx > list->lv_len) |
| 1337 | { |
| 1338 | semsg(_(e_listidx), lidx); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1339 | goto on_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1340 | } |
| 1341 | tv = STACK_TV_BOT(-3); |
| 1342 | if (lidx < list->lv_len) |
| 1343 | { |
| 1344 | listitem_T *li = list_find(list, lidx); |
| 1345 | |
| 1346 | // overwrite existing list item |
| 1347 | clear_tv(&li->li_tv); |
| 1348 | li->li_tv = *tv; |
| 1349 | } |
| 1350 | else |
| 1351 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1352 | // append to list, only fails when out of memory |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1353 | if (list_append_tv(list, tv) == FAIL) |
| 1354 | goto failed; |
| 1355 | clear_tv(tv); |
| 1356 | } |
| 1357 | clear_tv(tv_idx); |
| 1358 | clear_tv(tv_list); |
Bram Moolenaar | f163bd5 | 2020-05-10 21:47:43 +0200 | [diff] [blame] | 1359 | ectx.ec_stack.ga_len -= 3; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1360 | } |
| 1361 | break; |
| 1362 | |
| 1363 | // store value in dict variable |
| 1364 | case ISN_STOREDICT: |
| 1365 | { |
| 1366 | typval_T *tv_key = STACK_TV_BOT(-2); |
| 1367 | char_u *key = tv_key->vval.v_string; |
| 1368 | typval_T *tv_dict = STACK_TV_BOT(-1); |
| 1369 | dict_T *dict = tv_dict->vval.v_dict; |
| 1370 | dictitem_T *di; |
| 1371 | |
| 1372 | if (key == NULL || *key == NUL) |
| 1373 | { |
| 1374 | emsg(_(e_emptykey)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1375 | goto on_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1376 | } |
| 1377 | tv = STACK_TV_BOT(-3); |
| 1378 | di = dict_find(dict, key, -1); |
| 1379 | if (di != NULL) |
| 1380 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1381 | // overwrite existing value |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1382 | clear_tv(&di->di_tv); |
| 1383 | di->di_tv = *tv; |
| 1384 | } |
| 1385 | else |
| 1386 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1387 | // add to dict, only fails when out of memory |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1388 | if (dict_add_tv(dict, (char *)key, tv) == FAIL) |
| 1389 | goto failed; |
| 1390 | clear_tv(tv); |
| 1391 | } |
| 1392 | clear_tv(tv_key); |
| 1393 | clear_tv(tv_dict); |
Bram Moolenaar | f163bd5 | 2020-05-10 21:47:43 +0200 | [diff] [blame] | 1394 | ectx.ec_stack.ga_len -= 3; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 1395 | } |
| 1396 | break; |
| 1397 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1398 | // push constant |
| 1399 | case ISN_PUSHNR: |
| 1400 | case ISN_PUSHBOOL: |
| 1401 | case ISN_PUSHSPEC: |
| 1402 | case ISN_PUSHF: |
| 1403 | case ISN_PUSHS: |
| 1404 | case ISN_PUSHBLOB: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1405 | case ISN_PUSHFUNC: |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1406 | case ISN_PUSHCHANNEL: |
| 1407 | case ISN_PUSHJOB: |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1408 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1409 | goto failed; |
| 1410 | tv = STACK_TV_BOT(0); |
| 1411 | ++ectx.ec_stack.ga_len; |
| 1412 | switch (iptr->isn_type) |
| 1413 | { |
| 1414 | case ISN_PUSHNR: |
| 1415 | tv->v_type = VAR_NUMBER; |
| 1416 | tv->vval.v_number = iptr->isn_arg.number; |
| 1417 | break; |
| 1418 | case ISN_PUSHBOOL: |
| 1419 | tv->v_type = VAR_BOOL; |
| 1420 | tv->vval.v_number = iptr->isn_arg.number; |
| 1421 | break; |
| 1422 | case ISN_PUSHSPEC: |
| 1423 | tv->v_type = VAR_SPECIAL; |
| 1424 | tv->vval.v_number = iptr->isn_arg.number; |
| 1425 | break; |
| 1426 | #ifdef FEAT_FLOAT |
| 1427 | case ISN_PUSHF: |
| 1428 | tv->v_type = VAR_FLOAT; |
| 1429 | tv->vval.v_float = iptr->isn_arg.fnumber; |
| 1430 | break; |
| 1431 | #endif |
| 1432 | case ISN_PUSHBLOB: |
| 1433 | blob_copy(iptr->isn_arg.blob, tv); |
| 1434 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1435 | case ISN_PUSHFUNC: |
| 1436 | tv->v_type = VAR_FUNC; |
Bram Moolenaar | 087d2e1 | 2020-03-01 15:36:42 +0100 | [diff] [blame] | 1437 | if (iptr->isn_arg.string == NULL) |
| 1438 | tv->vval.v_string = NULL; |
| 1439 | else |
| 1440 | tv->vval.v_string = |
| 1441 | vim_strsave(iptr->isn_arg.string); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1442 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 1443 | case ISN_PUSHCHANNEL: |
| 1444 | #ifdef FEAT_JOB_CHANNEL |
| 1445 | tv->v_type = VAR_CHANNEL; |
| 1446 | tv->vval.v_channel = iptr->isn_arg.channel; |
| 1447 | if (tv->vval.v_channel != NULL) |
| 1448 | ++tv->vval.v_channel->ch_refcount; |
| 1449 | #endif |
| 1450 | break; |
| 1451 | case ISN_PUSHJOB: |
| 1452 | #ifdef FEAT_JOB_CHANNEL |
| 1453 | tv->v_type = VAR_JOB; |
| 1454 | tv->vval.v_job = iptr->isn_arg.job; |
| 1455 | if (tv->vval.v_job != NULL) |
| 1456 | ++tv->vval.v_job->jv_refcount; |
| 1457 | #endif |
| 1458 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1459 | default: |
| 1460 | tv->v_type = VAR_STRING; |
Bram Moolenaar | e69f6d0 | 2020-04-01 22:11:01 +0200 | [diff] [blame] | 1461 | tv->vval.v_string = vim_strsave( |
| 1462 | iptr->isn_arg.string == NULL |
| 1463 | ? (char_u *)"" : iptr->isn_arg.string); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1464 | } |
| 1465 | break; |
| 1466 | |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1467 | case ISN_UNLET: |
| 1468 | if (do_unlet(iptr->isn_arg.unlet.ul_name, |
| 1469 | iptr->isn_arg.unlet.ul_forceit) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1470 | goto on_error; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1471 | break; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 1472 | case ISN_UNLETENV: |
| 1473 | vim_unsetenv(iptr->isn_arg.unlet.ul_name); |
| 1474 | break; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 1475 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1476 | // create a list from items on the stack; uses a single allocation |
| 1477 | // for the list header and the items |
| 1478 | case ISN_NEWLIST: |
Bram Moolenaar | fe27081 | 2020-04-11 22:31:27 +0200 | [diff] [blame] | 1479 | if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL) |
| 1480 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1481 | break; |
| 1482 | |
| 1483 | // create a dict from items on the stack |
| 1484 | case ISN_NEWDICT: |
| 1485 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1486 | int count = iptr->isn_arg.number; |
| 1487 | dict_T *dict = dict_alloc(); |
| 1488 | dictitem_T *item; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1489 | |
| 1490 | if (dict == NULL) |
| 1491 | goto failed; |
| 1492 | for (idx = 0; idx < count; ++idx) |
| 1493 | { |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1494 | // have already checked key type is VAR_STRING |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1495 | tv = STACK_TV_BOT(2 * (idx - count)); |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1496 | // check key is unique |
| 1497 | item = dict_find(dict, tv->vval.v_string, -1); |
| 1498 | if (item != NULL) |
| 1499 | { |
| 1500 | semsg(_(e_duplicate_key), tv->vval.v_string); |
| 1501 | dict_unref(dict); |
| 1502 | goto on_error; |
| 1503 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1504 | item = dictitem_alloc(tv->vval.v_string); |
| 1505 | clear_tv(tv); |
| 1506 | if (item == NULL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1507 | { |
| 1508 | dict_unref(dict); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1509 | goto failed; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1510 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1511 | item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1); |
| 1512 | item->di_tv.v_lock = 0; |
| 1513 | if (dict_add(dict, item) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1514 | { |
| 1515 | dict_unref(dict); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1516 | goto failed; |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1517 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | if (count > 0) |
| 1521 | ectx.ec_stack.ga_len -= 2 * count - 1; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1522 | else if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1523 | goto failed; |
| 1524 | else |
| 1525 | ++ectx.ec_stack.ga_len; |
| 1526 | tv = STACK_TV_BOT(-1); |
| 1527 | tv->v_type = VAR_DICT; |
| 1528 | tv->vval.v_dict = dict; |
| 1529 | ++dict->dv_refcount; |
| 1530 | } |
| 1531 | break; |
| 1532 | |
| 1533 | // call a :def function |
| 1534 | case ISN_DCALL: |
| 1535 | if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, |
| 1536 | iptr->isn_arg.dfunc.cdf_argcount, |
| 1537 | &ectx) == FAIL) |
Bram Moolenaar | e859312 | 2020-07-18 15:17:02 +0200 | [diff] [blame] | 1538 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1539 | break; |
| 1540 | |
| 1541 | // call a builtin function |
| 1542 | case ISN_BCALL: |
| 1543 | SOURCING_LNUM = iptr->isn_lnum; |
| 1544 | if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx, |
| 1545 | iptr->isn_arg.bfunc.cbf_argcount, |
| 1546 | &ectx) == FAIL) |
| 1547 | goto failed; |
| 1548 | break; |
| 1549 | |
| 1550 | // call a funcref or partial |
| 1551 | case ISN_PCALL: |
| 1552 | { |
| 1553 | cpfunc_T *pfunc = &iptr->isn_arg.pfunc; |
| 1554 | int r; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 1555 | typval_T partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1556 | |
| 1557 | SOURCING_LNUM = iptr->isn_lnum; |
| 1558 | if (pfunc->cpf_top) |
| 1559 | { |
| 1560 | // funcref is above the arguments |
| 1561 | tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1); |
| 1562 | } |
| 1563 | else |
| 1564 | { |
| 1565 | // Get the funcref from the stack. |
| 1566 | --ectx.ec_stack.ga_len; |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 1567 | partial_tv = *STACK_TV_BOT(0); |
| 1568 | tv = &partial_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1569 | } |
| 1570 | r = call_partial(tv, pfunc->cpf_argcount, &ectx); |
Bram Moolenaar | 6f5b6df | 2020-05-16 21:20:12 +0200 | [diff] [blame] | 1571 | if (tv == &partial_tv) |
| 1572 | clear_tv(&partial_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1573 | if (r == FAIL) |
| 1574 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1575 | } |
| 1576 | break; |
| 1577 | |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 1578 | case ISN_PCALL_END: |
| 1579 | // PCALL finished, arguments have been consumed and replaced by |
| 1580 | // the return value. Now clear the funcref from the stack, |
| 1581 | // and move the return value in its place. |
| 1582 | --ectx.ec_stack.ga_len; |
| 1583 | clear_tv(STACK_TV_BOT(-1)); |
| 1584 | *STACK_TV_BOT(-1) = *STACK_TV_BOT(0); |
| 1585 | break; |
| 1586 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1587 | // call a user defined function or funcref/partial |
| 1588 | case ISN_UCALL: |
| 1589 | { |
| 1590 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 1591 | |
| 1592 | SOURCING_LNUM = iptr->isn_lnum; |
| 1593 | if (call_eval_func(cufunc->cuf_name, |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 1594 | cufunc->cuf_argcount, &ectx, iptr) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1595 | goto failed; |
| 1596 | } |
| 1597 | break; |
| 1598 | |
| 1599 | // return from a :def function call |
| 1600 | case ISN_RETURN: |
| 1601 | { |
Bram Moolenaar | 8cbd6df | 2020-01-28 22:59:45 +0100 | [diff] [blame] | 1602 | garray_T *trystack = &ectx.ec_trystack; |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1603 | trycmd_T *trycmd = NULL; |
Bram Moolenaar | 8cbd6df | 2020-01-28 22:59:45 +0100 | [diff] [blame] | 1604 | |
| 1605 | if (trystack->ga_len > 0) |
| 1606 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 1607 | + trystack->ga_len - 1; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1608 | if (trycmd != NULL |
| 1609 | && trycmd->tcd_frame_idx == ectx.ec_frame_idx |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1610 | && trycmd->tcd_finally_idx != 0) |
| 1611 | { |
| 1612 | // jump to ":finally" |
| 1613 | ectx.ec_iidx = trycmd->tcd_finally_idx; |
| 1614 | trycmd->tcd_return = TRUE; |
| 1615 | } |
| 1616 | else |
| 1617 | { |
| 1618 | // Restore previous function. If the frame pointer |
| 1619 | // is zero then there is none and we are done. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1620 | if (ectx.ec_frame_idx == initial_frame_idx) |
| 1621 | { |
| 1622 | if (handle_closure_in_use(&ectx, FALSE) == FAIL) |
| 1623 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1624 | goto done; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1625 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1626 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1627 | if (func_return(&ectx) == FAIL) |
| 1628 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | break; |
| 1632 | |
| 1633 | // push a function reference to a compiled function |
| 1634 | case ISN_FUNCREF: |
| 1635 | { |
| 1636 | partial_T *pt = NULL; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1637 | dfunc_T *pt_dfunc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1638 | |
| 1639 | pt = ALLOC_CLEAR_ONE(partial_T); |
| 1640 | if (pt == NULL) |
| 1641 | goto failed; |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1642 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1643 | { |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1644 | vim_free(pt); |
| 1645 | goto failed; |
| 1646 | } |
| 1647 | pt_dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1648 | + iptr->isn_arg.funcref.fr_func; |
| 1649 | pt->pt_func = pt_dfunc->df_ufunc; |
| 1650 | pt->pt_refcount = 1; |
| 1651 | ++pt_dfunc->df_ufunc->uf_refcount; |
| 1652 | |
| 1653 | if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE) |
| 1654 | { |
| 1655 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 1656 | + ectx.ec_dfunc_idx; |
| 1657 | |
| 1658 | // The closure needs to find arguments and local |
| 1659 | // variables in the current stack. |
Bram Moolenaar | f7779c6 | 2020-05-03 15:38:16 +0200 | [diff] [blame] | 1660 | pt->pt_ectx_stack = &ectx.ec_stack; |
| 1661 | pt->pt_ectx_frame = ectx.ec_frame_idx; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1662 | |
| 1663 | // If this function returns and the closure is still |
| 1664 | // used, we need to make a copy of the context |
| 1665 | // (arguments and local variables). Store a reference |
| 1666 | // to the partial so we can handle that. |
| 1667 | ++pt->pt_refcount; |
| 1668 | tv = STACK_TV_VAR(dfunc->df_varcount |
| 1669 | + iptr->isn_arg.funcref.fr_var_idx); |
| 1670 | if (tv->v_type == VAR_PARTIAL) |
| 1671 | { |
| 1672 | // TODO: use a garray_T on ectx. |
| 1673 | emsg("Multiple closures not supported yet"); |
| 1674 | goto failed; |
| 1675 | } |
| 1676 | tv->v_type = VAR_PARTIAL; |
| 1677 | tv->vval.v_partial = pt; |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1680 | tv = STACK_TV_BOT(0); |
| 1681 | ++ectx.ec_stack.ga_len; |
| 1682 | tv->vval.v_partial = pt; |
| 1683 | tv->v_type = VAR_PARTIAL; |
| 1684 | } |
| 1685 | break; |
| 1686 | |
| 1687 | // jump if a condition is met |
| 1688 | case ISN_JUMP: |
| 1689 | { |
| 1690 | jumpwhen_T when = iptr->isn_arg.jump.jump_when; |
| 1691 | int jump = TRUE; |
| 1692 | |
| 1693 | if (when != JUMP_ALWAYS) |
| 1694 | { |
| 1695 | tv = STACK_TV_BOT(-1); |
| 1696 | jump = tv2bool(tv); |
| 1697 | if (when == JUMP_IF_FALSE |
| 1698 | || when == JUMP_AND_KEEP_IF_FALSE) |
| 1699 | jump = !jump; |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 1700 | if (when == JUMP_IF_FALSE || !jump) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1701 | { |
| 1702 | // drop the value from the stack |
| 1703 | clear_tv(tv); |
| 1704 | --ectx.ec_stack.ga_len; |
| 1705 | } |
| 1706 | } |
| 1707 | if (jump) |
| 1708 | ectx.ec_iidx = iptr->isn_arg.jump.jump_where; |
| 1709 | } |
| 1710 | break; |
| 1711 | |
| 1712 | // top of a for loop |
| 1713 | case ISN_FOR: |
| 1714 | { |
| 1715 | list_T *list = STACK_TV_BOT(-1)->vval.v_list; |
| 1716 | typval_T *idxtv = |
| 1717 | STACK_TV_VAR(iptr->isn_arg.forloop.for_idx); |
| 1718 | |
| 1719 | // push the next item from the list |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1720 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1721 | goto failed; |
| 1722 | if (++idxtv->vval.v_number >= list->lv_len) |
| 1723 | // past the end of the list, jump to "endfor" |
| 1724 | ectx.ec_iidx = iptr->isn_arg.forloop.for_end; |
| 1725 | else if (list->lv_first == &range_list_item) |
| 1726 | { |
| 1727 | // non-materialized range() list |
| 1728 | tv = STACK_TV_BOT(0); |
| 1729 | tv->v_type = VAR_NUMBER; |
| 1730 | tv->vval.v_number = list_find_nr( |
| 1731 | list, idxtv->vval.v_number, NULL); |
| 1732 | ++ectx.ec_stack.ga_len; |
| 1733 | } |
| 1734 | else |
| 1735 | { |
| 1736 | listitem_T *li = list_find(list, idxtv->vval.v_number); |
| 1737 | |
| 1738 | if (li == NULL) |
| 1739 | goto failed; |
| 1740 | copy_tv(&li->li_tv, STACK_TV_BOT(0)); |
| 1741 | ++ectx.ec_stack.ga_len; |
| 1742 | } |
| 1743 | } |
| 1744 | break; |
| 1745 | |
| 1746 | // start of ":try" block |
| 1747 | case ISN_TRY: |
| 1748 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1749 | trycmd_T *trycmd = NULL; |
| 1750 | |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1751 | if (GA_GROW(&ectx.ec_trystack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1752 | goto failed; |
| 1753 | trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data) |
| 1754 | + ectx.ec_trystack.ga_len; |
| 1755 | ++ectx.ec_trystack.ga_len; |
| 1756 | ++trylevel; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1757 | trycmd->tcd_frame_idx = ectx.ec_frame_idx; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1758 | trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch; |
| 1759 | trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally; |
Bram Moolenaar | f575adf | 2020-02-20 20:41:06 +0100 | [diff] [blame] | 1760 | trycmd->tcd_caught = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1761 | } |
| 1762 | break; |
| 1763 | |
| 1764 | case ISN_PUSHEXC: |
| 1765 | if (current_exception == NULL) |
| 1766 | { |
| 1767 | iemsg("Evaluating catch while current_exception is NULL"); |
| 1768 | goto failed; |
| 1769 | } |
Bram Moolenaar | 270d038 | 2020-05-15 21:42:53 +0200 | [diff] [blame] | 1770 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1771 | goto failed; |
| 1772 | tv = STACK_TV_BOT(0); |
| 1773 | ++ectx.ec_stack.ga_len; |
| 1774 | tv->v_type = VAR_STRING; |
| 1775 | tv->vval.v_string = vim_strsave( |
| 1776 | (char_u *)current_exception->value); |
| 1777 | break; |
| 1778 | |
| 1779 | case ISN_CATCH: |
| 1780 | { |
| 1781 | garray_T *trystack = &ectx.ec_trystack; |
| 1782 | |
| 1783 | if (trystack->ga_len > 0) |
| 1784 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1785 | trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1786 | + trystack->ga_len - 1; |
| 1787 | trycmd->tcd_caught = TRUE; |
| 1788 | } |
| 1789 | did_emsg = got_int = did_throw = FALSE; |
| 1790 | catch_exception(current_exception); |
| 1791 | } |
| 1792 | break; |
| 1793 | |
| 1794 | // end of ":try" block |
| 1795 | case ISN_ENDTRY: |
| 1796 | { |
| 1797 | garray_T *trystack = &ectx.ec_trystack; |
| 1798 | |
| 1799 | if (trystack->ga_len > 0) |
| 1800 | { |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 1801 | trycmd_T *trycmd = NULL; |
| 1802 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1803 | --trystack->ga_len; |
| 1804 | --trylevel; |
Bram Moolenaar | 68d130c | 2020-07-17 22:06:44 +0200 | [diff] [blame] | 1805 | ectx.ec_in_catch = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1806 | trycmd = ((trycmd_T *)trystack->ga_data) |
| 1807 | + trystack->ga_len; |
Bram Moolenaar | f575adf | 2020-02-20 20:41:06 +0100 | [diff] [blame] | 1808 | if (trycmd->tcd_caught && current_exception != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1809 | { |
| 1810 | // discard the exception |
| 1811 | if (caught_stack == current_exception) |
| 1812 | caught_stack = caught_stack->caught; |
| 1813 | discard_current_exception(); |
| 1814 | } |
| 1815 | |
| 1816 | if (trycmd->tcd_return) |
| 1817 | { |
| 1818 | // Restore previous function. If the frame pointer |
| 1819 | // is zero then there is none and we are done. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1820 | if (ectx.ec_frame_idx == initial_frame_idx) |
| 1821 | { |
| 1822 | if (handle_closure_in_use(&ectx, FALSE) == FAIL) |
| 1823 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1824 | goto done; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1825 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1826 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 1827 | if (func_return(&ectx) == FAIL) |
| 1828 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | break; |
| 1833 | |
| 1834 | case ISN_THROW: |
| 1835 | --ectx.ec_stack.ga_len; |
| 1836 | tv = STACK_TV_BOT(0); |
| 1837 | if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL) |
| 1838 | { |
| 1839 | vim_free(tv->vval.v_string); |
| 1840 | goto failed; |
| 1841 | } |
| 1842 | did_throw = TRUE; |
| 1843 | break; |
| 1844 | |
| 1845 | // compare with special values |
| 1846 | case ISN_COMPAREBOOL: |
| 1847 | case ISN_COMPARESPECIAL: |
| 1848 | { |
| 1849 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 1850 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 1851 | varnumber_T arg1 = tv1->vval.v_number; |
| 1852 | varnumber_T arg2 = tv2->vval.v_number; |
| 1853 | int res; |
| 1854 | |
| 1855 | switch (iptr->isn_arg.op.op_type) |
| 1856 | { |
| 1857 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 1858 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 1859 | default: res = 0; break; |
| 1860 | } |
| 1861 | |
| 1862 | --ectx.ec_stack.ga_len; |
| 1863 | tv1->v_type = VAR_BOOL; |
| 1864 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 1865 | } |
| 1866 | break; |
| 1867 | |
| 1868 | // Operation with two number arguments |
| 1869 | case ISN_OPNR: |
| 1870 | case ISN_COMPARENR: |
| 1871 | { |
| 1872 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 1873 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 1874 | varnumber_T arg1 = tv1->vval.v_number; |
| 1875 | varnumber_T arg2 = tv2->vval.v_number; |
| 1876 | varnumber_T res; |
| 1877 | |
| 1878 | switch (iptr->isn_arg.op.op_type) |
| 1879 | { |
| 1880 | case EXPR_MULT: res = arg1 * arg2; break; |
| 1881 | case EXPR_DIV: res = arg1 / arg2; break; |
| 1882 | case EXPR_REM: res = arg1 % arg2; break; |
| 1883 | case EXPR_SUB: res = arg1 - arg2; break; |
| 1884 | case EXPR_ADD: res = arg1 + arg2; break; |
| 1885 | |
| 1886 | case EXPR_EQUAL: res = arg1 == arg2; break; |
| 1887 | case EXPR_NEQUAL: res = arg1 != arg2; break; |
| 1888 | case EXPR_GREATER: res = arg1 > arg2; break; |
| 1889 | case EXPR_GEQUAL: res = arg1 >= arg2; break; |
| 1890 | case EXPR_SMALLER: res = arg1 < arg2; break; |
| 1891 | case EXPR_SEQUAL: res = arg1 <= arg2; break; |
| 1892 | default: res = 0; break; |
| 1893 | } |
| 1894 | |
| 1895 | --ectx.ec_stack.ga_len; |
| 1896 | if (iptr->isn_type == ISN_COMPARENR) |
| 1897 | { |
| 1898 | tv1->v_type = VAR_BOOL; |
| 1899 | tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE; |
| 1900 | } |
| 1901 | else |
| 1902 | tv1->vval.v_number = res; |
| 1903 | } |
| 1904 | break; |
| 1905 | |
| 1906 | // Computation with two float arguments |
| 1907 | case ISN_OPFLOAT: |
| 1908 | case ISN_COMPAREFLOAT: |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1909 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1910 | { |
| 1911 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 1912 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 1913 | float_T arg1 = tv1->vval.v_float; |
| 1914 | float_T arg2 = tv2->vval.v_float; |
| 1915 | float_T res = 0; |
| 1916 | int cmp = FALSE; |
| 1917 | |
| 1918 | switch (iptr->isn_arg.op.op_type) |
| 1919 | { |
| 1920 | case EXPR_MULT: res = arg1 * arg2; break; |
| 1921 | case EXPR_DIV: res = arg1 / arg2; break; |
| 1922 | case EXPR_SUB: res = arg1 - arg2; break; |
| 1923 | case EXPR_ADD: res = arg1 + arg2; break; |
| 1924 | |
| 1925 | case EXPR_EQUAL: cmp = arg1 == arg2; break; |
| 1926 | case EXPR_NEQUAL: cmp = arg1 != arg2; break; |
| 1927 | case EXPR_GREATER: cmp = arg1 > arg2; break; |
| 1928 | case EXPR_GEQUAL: cmp = arg1 >= arg2; break; |
| 1929 | case EXPR_SMALLER: cmp = arg1 < arg2; break; |
| 1930 | case EXPR_SEQUAL: cmp = arg1 <= arg2; break; |
| 1931 | default: cmp = 0; break; |
| 1932 | } |
| 1933 | --ectx.ec_stack.ga_len; |
| 1934 | if (iptr->isn_type == ISN_COMPAREFLOAT) |
| 1935 | { |
| 1936 | tv1->v_type = VAR_BOOL; |
| 1937 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 1938 | } |
| 1939 | else |
| 1940 | tv1->vval.v_float = res; |
| 1941 | } |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 1942 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1943 | break; |
| 1944 | |
| 1945 | case ISN_COMPARELIST: |
| 1946 | { |
| 1947 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 1948 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 1949 | list_T *arg1 = tv1->vval.v_list; |
| 1950 | list_T *arg2 = tv2->vval.v_list; |
| 1951 | int cmp = FALSE; |
| 1952 | int ic = iptr->isn_arg.op.op_ic; |
| 1953 | |
| 1954 | switch (iptr->isn_arg.op.op_type) |
| 1955 | { |
| 1956 | case EXPR_EQUAL: cmp = |
| 1957 | list_equal(arg1, arg2, ic, FALSE); break; |
| 1958 | case EXPR_NEQUAL: cmp = |
| 1959 | !list_equal(arg1, arg2, ic, FALSE); break; |
| 1960 | case EXPR_IS: cmp = arg1 == arg2; break; |
| 1961 | case EXPR_ISNOT: cmp = arg1 != arg2; break; |
| 1962 | default: cmp = 0; break; |
| 1963 | } |
| 1964 | --ectx.ec_stack.ga_len; |
| 1965 | clear_tv(tv1); |
| 1966 | clear_tv(tv2); |
| 1967 | tv1->v_type = VAR_BOOL; |
| 1968 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 1969 | } |
| 1970 | break; |
| 1971 | |
| 1972 | case ISN_COMPAREBLOB: |
| 1973 | { |
| 1974 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 1975 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 1976 | blob_T *arg1 = tv1->vval.v_blob; |
| 1977 | blob_T *arg2 = tv2->vval.v_blob; |
| 1978 | int cmp = FALSE; |
| 1979 | |
| 1980 | switch (iptr->isn_arg.op.op_type) |
| 1981 | { |
| 1982 | case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break; |
| 1983 | case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break; |
| 1984 | case EXPR_IS: cmp = arg1 == arg2; break; |
| 1985 | case EXPR_ISNOT: cmp = arg1 != arg2; break; |
| 1986 | default: cmp = 0; break; |
| 1987 | } |
| 1988 | --ectx.ec_stack.ga_len; |
| 1989 | clear_tv(tv1); |
| 1990 | clear_tv(tv2); |
| 1991 | tv1->v_type = VAR_BOOL; |
| 1992 | tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE; |
| 1993 | } |
| 1994 | break; |
| 1995 | |
| 1996 | // TODO: handle separately |
| 1997 | case ISN_COMPARESTRING: |
| 1998 | case ISN_COMPAREDICT: |
| 1999 | case ISN_COMPAREFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2000 | case ISN_COMPAREANY: |
| 2001 | { |
| 2002 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 2003 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 2004 | exptype_T exptype = iptr->isn_arg.op.op_type; |
| 2005 | int ic = iptr->isn_arg.op.op_ic; |
| 2006 | |
| 2007 | typval_compare(tv1, tv2, exptype, ic); |
| 2008 | clear_tv(tv2); |
| 2009 | tv1->v_type = VAR_BOOL; |
| 2010 | tv1->vval.v_number = tv1->vval.v_number |
| 2011 | ? VVAL_TRUE : VVAL_FALSE; |
| 2012 | --ectx.ec_stack.ga_len; |
| 2013 | } |
| 2014 | break; |
| 2015 | |
| 2016 | case ISN_ADDLIST: |
| 2017 | case ISN_ADDBLOB: |
| 2018 | { |
| 2019 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 2020 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 2021 | |
| 2022 | if (iptr->isn_type == ISN_ADDLIST) |
| 2023 | eval_addlist(tv1, tv2); |
| 2024 | else |
| 2025 | eval_addblob(tv1, tv2); |
| 2026 | clear_tv(tv2); |
| 2027 | --ectx.ec_stack.ga_len; |
| 2028 | } |
| 2029 | break; |
| 2030 | |
| 2031 | // Computation with two arguments of unknown type |
| 2032 | case ISN_OPANY: |
| 2033 | { |
| 2034 | typval_T *tv1 = STACK_TV_BOT(-2); |
| 2035 | typval_T *tv2 = STACK_TV_BOT(-1); |
| 2036 | varnumber_T n1, n2; |
| 2037 | #ifdef FEAT_FLOAT |
| 2038 | float_T f1 = 0, f2 = 0; |
| 2039 | #endif |
| 2040 | int error = FALSE; |
| 2041 | |
| 2042 | if (iptr->isn_arg.op.op_type == EXPR_ADD) |
| 2043 | { |
| 2044 | if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST) |
| 2045 | { |
| 2046 | eval_addlist(tv1, tv2); |
| 2047 | clear_tv(tv2); |
| 2048 | --ectx.ec_stack.ga_len; |
| 2049 | break; |
| 2050 | } |
| 2051 | else if (tv1->v_type == VAR_BLOB |
| 2052 | && tv2->v_type == VAR_BLOB) |
| 2053 | { |
| 2054 | eval_addblob(tv1, tv2); |
| 2055 | clear_tv(tv2); |
| 2056 | --ectx.ec_stack.ga_len; |
| 2057 | break; |
| 2058 | } |
| 2059 | } |
| 2060 | #ifdef FEAT_FLOAT |
| 2061 | if (tv1->v_type == VAR_FLOAT) |
| 2062 | { |
| 2063 | f1 = tv1->vval.v_float; |
| 2064 | n1 = 0; |
| 2065 | } |
| 2066 | else |
| 2067 | #endif |
| 2068 | { |
| 2069 | n1 = tv_get_number_chk(tv1, &error); |
| 2070 | if (error) |
| 2071 | goto failed; |
| 2072 | #ifdef FEAT_FLOAT |
| 2073 | if (tv2->v_type == VAR_FLOAT) |
| 2074 | f1 = n1; |
| 2075 | #endif |
| 2076 | } |
| 2077 | #ifdef FEAT_FLOAT |
| 2078 | if (tv2->v_type == VAR_FLOAT) |
| 2079 | { |
| 2080 | f2 = tv2->vval.v_float; |
| 2081 | n2 = 0; |
| 2082 | } |
| 2083 | else |
| 2084 | #endif |
| 2085 | { |
| 2086 | n2 = tv_get_number_chk(tv2, &error); |
| 2087 | if (error) |
| 2088 | goto failed; |
| 2089 | #ifdef FEAT_FLOAT |
| 2090 | if (tv1->v_type == VAR_FLOAT) |
| 2091 | f2 = n2; |
| 2092 | #endif |
| 2093 | } |
| 2094 | #ifdef FEAT_FLOAT |
| 2095 | // if there is a float on either side the result is a float |
| 2096 | if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT) |
| 2097 | { |
| 2098 | switch (iptr->isn_arg.op.op_type) |
| 2099 | { |
| 2100 | case EXPR_MULT: f1 = f1 * f2; break; |
| 2101 | case EXPR_DIV: f1 = f1 / f2; break; |
| 2102 | case EXPR_SUB: f1 = f1 - f2; break; |
| 2103 | case EXPR_ADD: f1 = f1 + f2; break; |
Bram Moolenaar | 68d130c | 2020-07-17 22:06:44 +0200 | [diff] [blame] | 2104 | default: emsg(_(e_modulus)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2105 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2106 | } |
| 2107 | clear_tv(tv1); |
| 2108 | clear_tv(tv2); |
| 2109 | tv1->v_type = VAR_FLOAT; |
| 2110 | tv1->vval.v_float = f1; |
| 2111 | --ectx.ec_stack.ga_len; |
| 2112 | } |
| 2113 | else |
| 2114 | #endif |
| 2115 | { |
| 2116 | switch (iptr->isn_arg.op.op_type) |
| 2117 | { |
| 2118 | case EXPR_MULT: n1 = n1 * n2; break; |
| 2119 | case EXPR_DIV: n1 = num_divide(n1, n2); break; |
| 2120 | case EXPR_SUB: n1 = n1 - n2; break; |
| 2121 | case EXPR_ADD: n1 = n1 + n2; break; |
| 2122 | default: n1 = num_modulus(n1, n2); break; |
| 2123 | } |
| 2124 | clear_tv(tv1); |
| 2125 | clear_tv(tv2); |
| 2126 | tv1->v_type = VAR_NUMBER; |
| 2127 | tv1->vval.v_number = n1; |
| 2128 | --ectx.ec_stack.ga_len; |
| 2129 | } |
| 2130 | } |
| 2131 | break; |
| 2132 | |
| 2133 | case ISN_CONCAT: |
| 2134 | { |
| 2135 | char_u *str1 = STACK_TV_BOT(-2)->vval.v_string; |
| 2136 | char_u *str2 = STACK_TV_BOT(-1)->vval.v_string; |
| 2137 | char_u *res; |
| 2138 | |
| 2139 | res = concat_str(str1, str2); |
| 2140 | clear_tv(STACK_TV_BOT(-2)); |
| 2141 | clear_tv(STACK_TV_BOT(-1)); |
| 2142 | --ectx.ec_stack.ga_len; |
| 2143 | STACK_TV_BOT(-1)->vval.v_string = res; |
| 2144 | } |
| 2145 | break; |
| 2146 | |
| 2147 | case ISN_INDEX: |
| 2148 | { |
| 2149 | list_T *list; |
| 2150 | varnumber_T n; |
| 2151 | listitem_T *li; |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 2152 | typval_T temp_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2153 | |
| 2154 | // list index: list is at stack-2, index at stack-1 |
| 2155 | tv = STACK_TV_BOT(-2); |
| 2156 | if (tv->v_type != VAR_LIST) |
| 2157 | { |
| 2158 | emsg(_(e_listreq)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2159 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2160 | } |
| 2161 | list = tv->vval.v_list; |
| 2162 | |
| 2163 | tv = STACK_TV_BOT(-1); |
| 2164 | if (tv->v_type != VAR_NUMBER) |
| 2165 | { |
| 2166 | emsg(_(e_number_exp)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2167 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2168 | } |
| 2169 | n = tv->vval.v_number; |
| 2170 | clear_tv(tv); |
| 2171 | if ((li = list_find(list, n)) == NULL) |
| 2172 | { |
| 2173 | semsg(_(e_listidx), n); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2174 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2175 | } |
| 2176 | --ectx.ec_stack.ga_len; |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 2177 | // Clear the list after getting the item, to avoid that it |
Bram Moolenaar | 68d130c | 2020-07-17 22:06:44 +0200 | [diff] [blame] | 2178 | // makes the item invalid. |
Bram Moolenaar | 435d897 | 2020-07-05 16:42:13 +0200 | [diff] [blame] | 2179 | tv = STACK_TV_BOT(-1); |
| 2180 | temp_tv = *tv; |
| 2181 | copy_tv(&li->li_tv, tv); |
| 2182 | clear_tv(&temp_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2183 | } |
| 2184 | break; |
| 2185 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2186 | case ISN_SLICE: |
| 2187 | { |
| 2188 | list_T *list; |
| 2189 | int count = iptr->isn_arg.number; |
| 2190 | |
Bram Moolenaar | c5b1c20 | 2020-06-18 22:43:27 +0200 | [diff] [blame] | 2191 | // type will have been checked to be a list |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2192 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2193 | list = tv->vval.v_list; |
| 2194 | |
| 2195 | // no error for short list, expect it to be checked earlier |
| 2196 | if (list != NULL && list->lv_len >= count) |
| 2197 | { |
| 2198 | list_T *newlist = list_slice(list, |
| 2199 | count, list->lv_len - 1); |
| 2200 | |
| 2201 | if (newlist != NULL) |
| 2202 | { |
| 2203 | list_unref(list); |
| 2204 | tv->vval.v_list = newlist; |
| 2205 | ++newlist->lv_refcount; |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | break; |
| 2210 | |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 2211 | case ISN_GETITEM: |
| 2212 | { |
| 2213 | listitem_T *li; |
| 2214 | int index = iptr->isn_arg.number; |
| 2215 | |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 2216 | // Get list item: list is at stack-1, push item. |
| 2217 | // List type and length is checked for when compiling. |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 2218 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | c785b9a | 2020-06-19 18:34:15 +0200 | [diff] [blame] | 2219 | li = list_find(tv->vval.v_list, index); |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 2220 | |
| 2221 | if (GA_GROW(&ectx.ec_stack, 1) == FAIL) |
| 2222 | goto failed; |
| 2223 | ++ectx.ec_stack.ga_len; |
| 2224 | copy_tv(&li->li_tv, STACK_TV_BOT(-1)); |
| 2225 | } |
| 2226 | break; |
| 2227 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2228 | case ISN_MEMBER: |
| 2229 | { |
| 2230 | dict_T *dict; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2231 | char_u *key; |
| 2232 | dictitem_T *di; |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 2233 | typval_T temp_tv; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2234 | |
| 2235 | // dict member: dict is at stack-2, key at stack-1 |
| 2236 | tv = STACK_TV_BOT(-2); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 2237 | // no need to check for VAR_DICT, CHECKTYPE will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2238 | dict = tv->vval.v_dict; |
| 2239 | |
| 2240 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 2241 | // no need to check for VAR_STRING, 2STRING will check. |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2242 | key = tv->vval.v_string; |
Bram Moolenaar | 4dac32c | 2020-05-15 21:44:19 +0200 | [diff] [blame] | 2243 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2244 | if ((di = dict_find(dict, key, -1)) == NULL) |
| 2245 | { |
| 2246 | semsg(_(e_dictkey), key); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2247 | goto on_error; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2248 | } |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2249 | clear_tv(tv); |
Bram Moolenaar | 50788ef | 2020-07-05 16:51:26 +0200 | [diff] [blame] | 2250 | --ectx.ec_stack.ga_len; |
| 2251 | // Clear the dict after getting the item, to avoid that it |
| 2252 | // make the item invalid. |
| 2253 | tv = STACK_TV_BOT(-1); |
| 2254 | temp_tv = *tv; |
| 2255 | copy_tv(&di->di_tv, tv); |
| 2256 | clear_tv(&temp_tv); |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2257 | } |
| 2258 | break; |
| 2259 | |
| 2260 | // dict member with string key |
| 2261 | case ISN_STRINGMEMBER: |
| 2262 | { |
| 2263 | dict_T *dict; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2264 | dictitem_T *di; |
Bram Moolenaar | fb9d5c5 | 2020-07-04 19:19:43 +0200 | [diff] [blame] | 2265 | typval_T temp_tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2266 | |
| 2267 | tv = STACK_TV_BOT(-1); |
| 2268 | if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL) |
| 2269 | { |
| 2270 | emsg(_(e_dictreq)); |
| 2271 | goto failed; |
| 2272 | } |
| 2273 | dict = tv->vval.v_dict; |
| 2274 | |
| 2275 | if ((di = dict_find(dict, iptr->isn_arg.string, -1)) |
| 2276 | == NULL) |
| 2277 | { |
| 2278 | semsg(_(e_dictkey), iptr->isn_arg.string); |
| 2279 | goto failed; |
| 2280 | } |
Bram Moolenaar | fb9d5c5 | 2020-07-04 19:19:43 +0200 | [diff] [blame] | 2281 | // Clear the dict after getting the item, to avoid that it |
| 2282 | // make the item invalid. |
| 2283 | temp_tv = *tv; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2284 | copy_tv(&di->di_tv, tv); |
Bram Moolenaar | fb9d5c5 | 2020-07-04 19:19:43 +0200 | [diff] [blame] | 2285 | clear_tv(&temp_tv); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2286 | } |
| 2287 | break; |
| 2288 | |
| 2289 | case ISN_NEGATENR: |
| 2290 | tv = STACK_TV_BOT(-1); |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 2291 | if (tv->v_type != VAR_NUMBER |
| 2292 | #ifdef FEAT_FLOAT |
| 2293 | && tv->v_type != VAR_FLOAT |
| 2294 | #endif |
| 2295 | ) |
| 2296 | { |
| 2297 | emsg(_(e_number_exp)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2298 | goto on_error; |
Bram Moolenaar | c58164c | 2020-03-29 18:40:30 +0200 | [diff] [blame] | 2299 | } |
| 2300 | #ifdef FEAT_FLOAT |
| 2301 | if (tv->v_type == VAR_FLOAT) |
| 2302 | tv->vval.v_float = -tv->vval.v_float; |
| 2303 | else |
| 2304 | #endif |
| 2305 | tv->vval.v_number = -tv->vval.v_number; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2306 | break; |
| 2307 | |
| 2308 | case ISN_CHECKNR: |
| 2309 | { |
| 2310 | int error = FALSE; |
| 2311 | |
| 2312 | tv = STACK_TV_BOT(-1); |
| 2313 | if (check_not_string(tv) == FAIL) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2314 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2315 | (void)tv_get_number_chk(tv, &error); |
| 2316 | if (error) |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2317 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2318 | } |
| 2319 | break; |
| 2320 | |
| 2321 | case ISN_CHECKTYPE: |
| 2322 | { |
| 2323 | checktype_T *ct = &iptr->isn_arg.type; |
| 2324 | |
| 2325 | tv = STACK_TV_BOT(ct->ct_off); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2326 | // TODO: better type comparison |
| 2327 | if (tv->v_type != ct->ct_type |
| 2328 | && !((tv->v_type == VAR_PARTIAL |
| 2329 | && ct->ct_type == VAR_FUNC) |
| 2330 | || (tv->v_type == VAR_FUNC |
| 2331 | && ct->ct_type == VAR_PARTIAL))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2332 | { |
| 2333 | semsg(_("E1029: Expected %s but got %s"), |
| 2334 | vartype_name(ct->ct_type), |
| 2335 | vartype_name(tv->v_type)); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2336 | goto on_error; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2337 | } |
| 2338 | } |
| 2339 | break; |
| 2340 | |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2341 | case ISN_CHECKLEN: |
| 2342 | { |
| 2343 | int min_len = iptr->isn_arg.checklen.cl_min_len; |
| 2344 | list_T *list = NULL; |
| 2345 | |
| 2346 | tv = STACK_TV_BOT(-1); |
| 2347 | if (tv->v_type == VAR_LIST) |
| 2348 | list = tv->vval.v_list; |
| 2349 | if (list == NULL || list->lv_len < min_len |
| 2350 | || (list->lv_len > min_len |
| 2351 | && !iptr->isn_arg.checklen.cl_more_OK)) |
| 2352 | { |
| 2353 | semsg(_("E1093: Expected %d items but got %d"), |
| 2354 | min_len, list == NULL ? 0 : list->lv_len); |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2355 | goto on_error; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2356 | } |
| 2357 | } |
| 2358 | break; |
| 2359 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2360 | case ISN_2BOOL: |
| 2361 | { |
| 2362 | int n; |
| 2363 | |
| 2364 | tv = STACK_TV_BOT(-1); |
| 2365 | n = tv2bool(tv); |
| 2366 | if (iptr->isn_arg.number) // invert |
| 2367 | n = !n; |
| 2368 | clear_tv(tv); |
| 2369 | tv->v_type = VAR_BOOL; |
| 2370 | tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE; |
| 2371 | } |
| 2372 | break; |
| 2373 | |
| 2374 | case ISN_2STRING: |
| 2375 | { |
| 2376 | char_u *str; |
| 2377 | |
| 2378 | tv = STACK_TV_BOT(iptr->isn_arg.number); |
| 2379 | if (tv->v_type != VAR_STRING) |
| 2380 | { |
| 2381 | str = typval_tostring(tv); |
| 2382 | clear_tv(tv); |
| 2383 | tv->v_type = VAR_STRING; |
| 2384 | tv->vval.v_string = str; |
| 2385 | } |
| 2386 | } |
| 2387 | break; |
| 2388 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2389 | case ISN_SHUFFLE: |
| 2390 | { |
| 2391 | typval_T tmp_tv; |
| 2392 | int item = iptr->isn_arg.shuffle.shfl_item; |
| 2393 | int up = iptr->isn_arg.shuffle.shfl_up; |
| 2394 | |
| 2395 | tmp_tv = *STACK_TV_BOT(-item); |
| 2396 | for ( ; up > 0 && item > 1; --up) |
| 2397 | { |
| 2398 | *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1); |
| 2399 | --item; |
| 2400 | } |
| 2401 | *STACK_TV_BOT(-item) = tmp_tv; |
| 2402 | } |
| 2403 | break; |
| 2404 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2405 | case ISN_DROP: |
| 2406 | --ectx.ec_stack.ga_len; |
| 2407 | clear_tv(STACK_TV_BOT(0)); |
| 2408 | break; |
| 2409 | } |
Bram Moolenaar | f0b9f43 | 2020-07-17 23:03:17 +0200 | [diff] [blame] | 2410 | continue; |
| 2411 | |
| 2412 | on_error: |
| 2413 | if (trylevel == 0) |
| 2414 | goto failed; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | done: |
| 2418 | // function finished, get result from the stack. |
| 2419 | tv = STACK_TV_BOT(-1); |
| 2420 | *rettv = *tv; |
| 2421 | tv->v_type = VAR_UNKNOWN; |
| 2422 | ret = OK; |
| 2423 | |
| 2424 | failed: |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 2425 | // When failed need to unwind the call stack. |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2426 | while (ectx.ec_frame_idx != initial_frame_idx) |
Bram Moolenaar | 7eeefd4 | 2020-02-26 21:24:23 +0100 | [diff] [blame] | 2427 | func_return(&ectx); |
Bram Moolenaar | 1a2f4bf | 2020-04-12 23:09:25 +0200 | [diff] [blame] | 2428 | failed_early: |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2429 | current_sctx.sc_version = save_sc_version; |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2430 | |
| 2431 | // Free all local variables, but not arguments. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2432 | for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx) |
| 2433 | clear_tv(STACK_TV(idx)); |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2434 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2435 | vim_free(ectx.ec_stack.ga_data); |
Bram Moolenaar | 20431c9 | 2020-03-20 18:39:46 +0100 | [diff] [blame] | 2436 | vim_free(ectx.ec_trystack.ga_data); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2437 | return ret; |
| 2438 | } |
| 2439 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2440 | /* |
| 2441 | * ":dissassemble". |
Bram Moolenaar | 777770f | 2020-02-06 21:27:08 +0100 | [diff] [blame] | 2442 | * We don't really need this at runtime, but we do have tests that require it, |
| 2443 | * so always include this. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2444 | */ |
| 2445 | void |
| 2446 | ex_disassemble(exarg_T *eap) |
| 2447 | { |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 2448 | char_u *arg = eap->arg; |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 2449 | char_u *fname; |
| 2450 | ufunc_T *ufunc; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2451 | dfunc_T *dfunc; |
| 2452 | isn_T *instr; |
| 2453 | int current; |
| 2454 | int line_idx = 0; |
| 2455 | int prev_current = 0; |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2456 | int is_global = FALSE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2457 | |
Bram Moolenaar | bfd6558 | 2020-07-13 18:18:00 +0200 | [diff] [blame] | 2458 | if (STRNCMP(arg, "<lambda>", 8) == 0) |
| 2459 | { |
| 2460 | arg += 8; |
| 2461 | (void)getdigits(&arg); |
| 2462 | fname = vim_strnsave(eap->arg, arg - eap->arg); |
| 2463 | } |
| 2464 | else |
| 2465 | fname = trans_function_name(&arg, &is_global, FALSE, |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 2466 | TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL); |
Bram Moolenaar | 21456cd | 2020-02-13 21:29:32 +0100 | [diff] [blame] | 2467 | if (fname == NULL) |
| 2468 | { |
| 2469 | semsg(_(e_invarg2), eap->arg); |
| 2470 | return; |
| 2471 | } |
| 2472 | |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2473 | ufunc = find_func(fname, is_global, NULL); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2474 | if (ufunc == NULL) |
| 2475 | { |
| 2476 | char_u *p = untrans_function_name(fname); |
| 2477 | |
| 2478 | if (p != NULL) |
| 2479 | // Try again without making it script-local. |
Bram Moolenaar | 4c17ad9 | 2020-04-27 22:47:51 +0200 | [diff] [blame] | 2480 | ufunc = find_func(p, FALSE, NULL); |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 2481 | } |
Bram Moolenaar | 0f18b6d | 2020-02-02 17:22:27 +0100 | [diff] [blame] | 2482 | vim_free(fname); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2483 | if (ufunc == NULL) |
| 2484 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 2485 | semsg(_("E1061: Cannot find function %s"), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2486 | return; |
| 2487 | } |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2488 | if (ufunc->uf_def_status == UF_TO_BE_COMPILED |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 2489 | && compile_def_function(ufunc, FALSE, NULL) == FAIL) |
| 2490 | return; |
Bram Moolenaar | 0cb5bcf | 2020-06-20 18:19:09 +0200 | [diff] [blame] | 2491 | if (ufunc->uf_def_status != UF_COMPILED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2492 | { |
Bram Moolenaar | df2ecdd | 2020-02-16 15:03:48 +0100 | [diff] [blame] | 2493 | semsg(_("E1062: Function %s is not compiled"), eap->arg); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2494 | return; |
| 2495 | } |
| 2496 | if (ufunc->uf_name_exp != NULL) |
| 2497 | msg((char *)ufunc->uf_name_exp); |
| 2498 | else |
| 2499 | msg((char *)ufunc->uf_name); |
| 2500 | |
| 2501 | dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx; |
| 2502 | instr = dfunc->df_instr; |
| 2503 | for (current = 0; current < dfunc->df_instr_count; ++current) |
| 2504 | { |
| 2505 | isn_T *iptr = &instr[current]; |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 2506 | char *line; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2507 | |
| 2508 | while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len) |
| 2509 | { |
| 2510 | if (current > prev_current) |
| 2511 | { |
| 2512 | msg_puts("\n\n"); |
| 2513 | prev_current = current; |
| 2514 | } |
Bram Moolenaar | f2460a3 | 2020-02-07 22:09:54 +0100 | [diff] [blame] | 2515 | line = ((char **)ufunc->uf_lines.ga_data)[line_idx++]; |
| 2516 | if (line != NULL) |
| 2517 | msg(line); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | switch (iptr->isn_type) |
| 2521 | { |
| 2522 | case ISN_EXEC: |
| 2523 | smsg("%4d EXEC %s", current, iptr->isn_arg.string); |
| 2524 | break; |
Bram Moolenaar | cfe435d | 2020-04-25 20:02:55 +0200 | [diff] [blame] | 2525 | case ISN_EXECCONCAT: |
| 2526 | smsg("%4d EXECCONCAT %lld", current, |
| 2527 | (long long)iptr->isn_arg.number); |
| 2528 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2529 | case ISN_ECHO: |
| 2530 | { |
| 2531 | echo_T *echo = &iptr->isn_arg.echo; |
| 2532 | |
| 2533 | smsg("%4d %s %d", current, |
| 2534 | echo->echo_with_white ? "ECHO" : "ECHON", |
| 2535 | echo->echo_count); |
| 2536 | } |
| 2537 | break; |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2538 | case ISN_EXECUTE: |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2539 | smsg("%4d EXECUTE %lld", current, |
| 2540 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | ad39c09 | 2020-02-26 18:23:43 +0100 | [diff] [blame] | 2541 | break; |
Bram Moolenaar | f93c7fe | 2020-04-23 22:16:53 +0200 | [diff] [blame] | 2542 | case ISN_ECHOMSG: |
| 2543 | smsg("%4d ECHOMSG %lld", current, |
| 2544 | (long long)(iptr->isn_arg.number)); |
| 2545 | break; |
| 2546 | case ISN_ECHOERR: |
| 2547 | smsg("%4d ECHOERR %lld", current, |
| 2548 | (long long)(iptr->isn_arg.number)); |
| 2549 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2550 | case ISN_LOAD: |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2551 | case ISN_LOADOUTER: |
| 2552 | { |
| 2553 | char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER"; |
| 2554 | |
| 2555 | if (iptr->isn_arg.number < 0) |
| 2556 | smsg("%4d LOAD%s arg[%lld]", current, add, |
| 2557 | (long long)(iptr->isn_arg.number |
| 2558 | + STACK_FRAME_SIZE)); |
| 2559 | else |
| 2560 | smsg("%4d LOAD%s $%lld", current, add, |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2561 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | c8cd2b3 | 2020-05-01 19:29:08 +0200 | [diff] [blame] | 2562 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2563 | break; |
| 2564 | case ISN_LOADV: |
| 2565 | smsg("%4d LOADV v:%s", current, |
| 2566 | get_vim_var_name(iptr->isn_arg.number)); |
| 2567 | break; |
| 2568 | case ISN_LOADSCRIPT: |
| 2569 | { |
| 2570 | scriptitem_T *si = |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2571 | SCRIPT_ITEM(iptr->isn_arg.script.script_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2572 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 2573 | + iptr->isn_arg.script.script_idx; |
| 2574 | |
| 2575 | smsg("%4d LOADSCRIPT %s from %s", current, |
| 2576 | sv->sv_name, si->sn_name); |
| 2577 | } |
| 2578 | break; |
| 2579 | case ISN_LOADS: |
| 2580 | { |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2581 | scriptitem_T *si = SCRIPT_ITEM( |
| 2582 | iptr->isn_arg.loadstore.ls_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2583 | |
| 2584 | smsg("%4d LOADS s:%s from %s", current, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2585 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2586 | } |
| 2587 | break; |
| 2588 | case ISN_LOADG: |
| 2589 | smsg("%4d LOADG g:%s", current, iptr->isn_arg.string); |
| 2590 | break; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2591 | case ISN_LOADB: |
| 2592 | smsg("%4d LOADB b:%s", current, iptr->isn_arg.string); |
| 2593 | break; |
| 2594 | case ISN_LOADW: |
| 2595 | smsg("%4d LOADW w:%s", current, iptr->isn_arg.string); |
| 2596 | break; |
| 2597 | case ISN_LOADT: |
| 2598 | smsg("%4d LOADT t:%s", current, iptr->isn_arg.string); |
| 2599 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2600 | case ISN_LOADOPT: |
| 2601 | smsg("%4d LOADOPT %s", current, iptr->isn_arg.string); |
| 2602 | break; |
| 2603 | case ISN_LOADENV: |
| 2604 | smsg("%4d LOADENV %s", current, iptr->isn_arg.string); |
| 2605 | break; |
| 2606 | case ISN_LOADREG: |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2607 | smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2608 | break; |
| 2609 | |
| 2610 | case ISN_STORE: |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 2611 | case ISN_STOREOUTER: |
| 2612 | { |
| 2613 | char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER"; |
| 2614 | |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 2615 | if (iptr->isn_arg.number < 0) |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 2616 | smsg("%4d STORE%s arg[%lld]", current, add, |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2617 | (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE)); |
Bram Moolenaar | 170fcfc | 2020-02-06 17:51:35 +0100 | [diff] [blame] | 2618 | else |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 2619 | smsg("%4d STORE%s $%lld", current, add, |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2620 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | b68b346 | 2020-05-06 21:06:30 +0200 | [diff] [blame] | 2621 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2622 | break; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2623 | case ISN_STOREV: |
| 2624 | smsg("%4d STOREV v:%s", current, |
| 2625 | get_vim_var_name(iptr->isn_arg.number)); |
| 2626 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2627 | case ISN_STOREG: |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2628 | smsg("%4d STOREG %s", current, iptr->isn_arg.string); |
| 2629 | break; |
Bram Moolenaar | d3aac29 | 2020-04-19 14:32:17 +0200 | [diff] [blame] | 2630 | case ISN_STOREB: |
| 2631 | smsg("%4d STOREB %s", current, iptr->isn_arg.string); |
| 2632 | break; |
| 2633 | case ISN_STOREW: |
| 2634 | smsg("%4d STOREW %s", current, iptr->isn_arg.string); |
| 2635 | break; |
| 2636 | case ISN_STORET: |
| 2637 | smsg("%4d STORET %s", current, iptr->isn_arg.string); |
| 2638 | break; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2639 | case ISN_STORES: |
| 2640 | { |
| 2641 | scriptitem_T *si = SCRIPT_ITEM( |
| 2642 | iptr->isn_arg.loadstore.ls_sid); |
| 2643 | |
Bram Moolenaar | 0bbf722 | 2020-02-19 22:31:48 +0100 | [diff] [blame] | 2644 | smsg("%4d STORES %s in %s", current, |
Bram Moolenaar | 5deeb3f | 2020-04-05 17:08:17 +0200 | [diff] [blame] | 2645 | iptr->isn_arg.loadstore.ls_name, si->sn_name); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2646 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2647 | break; |
| 2648 | case ISN_STORESCRIPT: |
| 2649 | { |
| 2650 | scriptitem_T *si = |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2651 | SCRIPT_ITEM(iptr->isn_arg.script.script_sid); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2652 | svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) |
| 2653 | + iptr->isn_arg.script.script_idx; |
| 2654 | |
| 2655 | smsg("%4d STORESCRIPT %s in %s", current, |
| 2656 | sv->sv_name, si->sn_name); |
| 2657 | } |
| 2658 | break; |
| 2659 | case ISN_STOREOPT: |
| 2660 | smsg("%4d STOREOPT &%s", current, |
| 2661 | iptr->isn_arg.storeopt.so_name); |
| 2662 | break; |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2663 | case ISN_STOREENV: |
| 2664 | smsg("%4d STOREENV $%s", current, iptr->isn_arg.string); |
| 2665 | break; |
| 2666 | case ISN_STOREREG: |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2667 | smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number); |
Bram Moolenaar | b283a8a | 2020-02-02 22:24:04 +0100 | [diff] [blame] | 2668 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2669 | case ISN_STORENR: |
| 2670 | smsg("%4d STORE %lld in $%d", current, |
Bram Moolenaar | a471eea | 2020-03-04 22:20:26 +0100 | [diff] [blame] | 2671 | iptr->isn_arg.storenr.stnr_val, |
| 2672 | iptr->isn_arg.storenr.stnr_idx); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2673 | break; |
| 2674 | |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2675 | case ISN_STORELIST: |
| 2676 | smsg("%4d STORELIST", current); |
| 2677 | break; |
| 2678 | |
| 2679 | case ISN_STOREDICT: |
| 2680 | smsg("%4d STOREDICT", current); |
| 2681 | break; |
| 2682 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2683 | // constants |
| 2684 | case ISN_PUSHNR: |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2685 | smsg("%4d PUSHNR %lld", current, |
| 2686 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2687 | break; |
| 2688 | case ISN_PUSHBOOL: |
| 2689 | case ISN_PUSHSPEC: |
| 2690 | smsg("%4d PUSH %s", current, |
| 2691 | get_var_special_name(iptr->isn_arg.number)); |
| 2692 | break; |
| 2693 | case ISN_PUSHF: |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2694 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2695 | smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber); |
Bram Moolenaar | a5d5953 | 2020-01-26 21:42:03 +0100 | [diff] [blame] | 2696 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2697 | break; |
| 2698 | case ISN_PUSHS: |
| 2699 | smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string); |
| 2700 | break; |
| 2701 | case ISN_PUSHBLOB: |
| 2702 | { |
| 2703 | char_u *r; |
| 2704 | char_u numbuf[NUMBUFLEN]; |
| 2705 | char_u *tofree; |
| 2706 | |
| 2707 | r = blob2string(iptr->isn_arg.blob, &tofree, numbuf); |
Bram Moolenaar | ff80cb6 | 2020-02-05 22:10:05 +0100 | [diff] [blame] | 2708 | smsg("%4d PUSHBLOB %s", current, r); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2709 | vim_free(tofree); |
| 2710 | } |
| 2711 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2712 | case ISN_PUSHFUNC: |
Bram Moolenaar | 087d2e1 | 2020-03-01 15:36:42 +0100 | [diff] [blame] | 2713 | { |
| 2714 | char *name = (char *)iptr->isn_arg.string; |
| 2715 | |
| 2716 | smsg("%4d PUSHFUNC \"%s\"", current, |
| 2717 | name == NULL ? "[none]" : name); |
| 2718 | } |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2719 | break; |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2720 | case ISN_PUSHCHANNEL: |
| 2721 | #ifdef FEAT_JOB_CHANNEL |
| 2722 | { |
| 2723 | channel_T *channel = iptr->isn_arg.channel; |
| 2724 | |
| 2725 | smsg("%4d PUSHCHANNEL %d", current, |
| 2726 | channel == NULL ? 0 : channel->ch_id); |
| 2727 | } |
| 2728 | #endif |
| 2729 | break; |
| 2730 | case ISN_PUSHJOB: |
| 2731 | #ifdef FEAT_JOB_CHANNEL |
| 2732 | { |
| 2733 | typval_T tv; |
| 2734 | char_u *name; |
| 2735 | |
| 2736 | tv.v_type = VAR_JOB; |
| 2737 | tv.vval.v_job = iptr->isn_arg.job; |
| 2738 | name = tv_get_string(&tv); |
Bram Moolenaar | f51cb4e | 2020-03-01 17:55:14 +0100 | [diff] [blame] | 2739 | smsg("%4d PUSHJOB \"%s\"", current, name); |
Bram Moolenaar | 42a480b | 2020-02-29 23:23:47 +0100 | [diff] [blame] | 2740 | } |
| 2741 | #endif |
| 2742 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2743 | case ISN_PUSHEXC: |
| 2744 | smsg("%4d PUSH v:exception", current); |
| 2745 | break; |
Bram Moolenaar | d72c1bf | 2020-04-19 16:28:59 +0200 | [diff] [blame] | 2746 | case ISN_UNLET: |
| 2747 | smsg("%4d UNLET%s %s", current, |
| 2748 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 2749 | iptr->isn_arg.unlet.ul_name); |
| 2750 | break; |
Bram Moolenaar | 7bdaea6 | 2020-04-19 18:27:26 +0200 | [diff] [blame] | 2751 | case ISN_UNLETENV: |
| 2752 | smsg("%4d UNLETENV%s $%s", current, |
| 2753 | iptr->isn_arg.unlet.ul_forceit ? "!" : "", |
| 2754 | iptr->isn_arg.unlet.ul_name); |
| 2755 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2756 | case ISN_NEWLIST: |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2757 | smsg("%4d NEWLIST size %lld", current, |
| 2758 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2759 | break; |
| 2760 | case ISN_NEWDICT: |
Bram Moolenaar | 1082772 | 2020-03-23 22:53:22 +0100 | [diff] [blame] | 2761 | smsg("%4d NEWDICT size %lld", current, |
| 2762 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2763 | break; |
| 2764 | |
| 2765 | // function call |
| 2766 | case ISN_BCALL: |
| 2767 | { |
| 2768 | cbfunc_T *cbfunc = &iptr->isn_arg.bfunc; |
| 2769 | |
| 2770 | smsg("%4d BCALL %s(argc %d)", current, |
| 2771 | internal_func_name(cbfunc->cbf_idx), |
| 2772 | cbfunc->cbf_argcount); |
| 2773 | } |
| 2774 | break; |
| 2775 | case ISN_DCALL: |
| 2776 | { |
| 2777 | cdfunc_T *cdfunc = &iptr->isn_arg.dfunc; |
| 2778 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
| 2779 | + cdfunc->cdf_idx; |
| 2780 | |
| 2781 | smsg("%4d DCALL %s(argc %d)", current, |
| 2782 | df->df_ufunc->uf_name_exp != NULL |
| 2783 | ? df->df_ufunc->uf_name_exp |
| 2784 | : df->df_ufunc->uf_name, cdfunc->cdf_argcount); |
| 2785 | } |
| 2786 | break; |
| 2787 | case ISN_UCALL: |
| 2788 | { |
| 2789 | cufunc_T *cufunc = &iptr->isn_arg.ufunc; |
| 2790 | |
| 2791 | smsg("%4d UCALL %s(argc %d)", current, |
| 2792 | cufunc->cuf_name, cufunc->cuf_argcount); |
| 2793 | } |
| 2794 | break; |
| 2795 | case ISN_PCALL: |
| 2796 | { |
| 2797 | cpfunc_T *cpfunc = &iptr->isn_arg.pfunc; |
| 2798 | |
| 2799 | smsg("%4d PCALL%s (argc %d)", current, |
| 2800 | cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount); |
| 2801 | } |
| 2802 | break; |
Bram Moolenaar | bd5da37 | 2020-03-31 23:13:10 +0200 | [diff] [blame] | 2803 | case ISN_PCALL_END: |
| 2804 | smsg("%4d PCALL end", current); |
| 2805 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2806 | case ISN_RETURN: |
| 2807 | smsg("%4d RETURN", current); |
| 2808 | break; |
| 2809 | case ISN_FUNCREF: |
| 2810 | { |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 2811 | funcref_T *funcref = &iptr->isn_arg.funcref; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2812 | dfunc_T *df = ((dfunc_T *)def_functions.ga_data) |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 2813 | + funcref->fr_func; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2814 | |
Bram Moolenaar | bf67ea1 | 2020-05-02 17:52:42 +0200 | [diff] [blame] | 2815 | smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name, |
Bram Moolenaar | 5adc55c | 2020-05-02 23:12:58 +0200 | [diff] [blame] | 2816 | funcref->fr_var_idx + dfunc->df_varcount); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2817 | } |
| 2818 | break; |
| 2819 | |
| 2820 | case ISN_JUMP: |
| 2821 | { |
| 2822 | char *when = "?"; |
| 2823 | |
| 2824 | switch (iptr->isn_arg.jump.jump_when) |
| 2825 | { |
| 2826 | case JUMP_ALWAYS: |
| 2827 | when = "JUMP"; |
| 2828 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2829 | case JUMP_AND_KEEP_IF_TRUE: |
| 2830 | when = "JUMP_AND_KEEP_IF_TRUE"; |
| 2831 | break; |
| 2832 | case JUMP_IF_FALSE: |
| 2833 | when = "JUMP_IF_FALSE"; |
| 2834 | break; |
| 2835 | case JUMP_AND_KEEP_IF_FALSE: |
| 2836 | when = "JUMP_AND_KEEP_IF_FALSE"; |
| 2837 | break; |
| 2838 | } |
Bram Moolenaar | 8a677a3 | 2020-03-12 19:15:45 +0100 | [diff] [blame] | 2839 | smsg("%4d %s -> %d", current, when, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2840 | iptr->isn_arg.jump.jump_where); |
| 2841 | } |
| 2842 | break; |
| 2843 | |
| 2844 | case ISN_FOR: |
| 2845 | { |
| 2846 | forloop_T *forloop = &iptr->isn_arg.forloop; |
| 2847 | |
| 2848 | smsg("%4d FOR $%d -> %d", current, |
| 2849 | forloop->for_idx, forloop->for_end); |
| 2850 | } |
| 2851 | break; |
| 2852 | |
| 2853 | case ISN_TRY: |
| 2854 | { |
| 2855 | try_T *try = &iptr->isn_arg.try; |
| 2856 | |
| 2857 | smsg("%4d TRY catch -> %d, finally -> %d", current, |
| 2858 | try->try_catch, try->try_finally); |
| 2859 | } |
| 2860 | break; |
| 2861 | case ISN_CATCH: |
| 2862 | // TODO |
| 2863 | smsg("%4d CATCH", current); |
| 2864 | break; |
| 2865 | case ISN_ENDTRY: |
| 2866 | smsg("%4d ENDTRY", current); |
| 2867 | break; |
| 2868 | case ISN_THROW: |
| 2869 | smsg("%4d THROW", current); |
| 2870 | break; |
| 2871 | |
| 2872 | // expression operations on number |
| 2873 | case ISN_OPNR: |
| 2874 | case ISN_OPFLOAT: |
| 2875 | case ISN_OPANY: |
| 2876 | { |
| 2877 | char *what; |
| 2878 | char *ins; |
| 2879 | |
| 2880 | switch (iptr->isn_arg.op.op_type) |
| 2881 | { |
| 2882 | case EXPR_MULT: what = "*"; break; |
| 2883 | case EXPR_DIV: what = "/"; break; |
| 2884 | case EXPR_REM: what = "%"; break; |
| 2885 | case EXPR_SUB: what = "-"; break; |
| 2886 | case EXPR_ADD: what = "+"; break; |
| 2887 | default: what = "???"; break; |
| 2888 | } |
| 2889 | switch (iptr->isn_type) |
| 2890 | { |
| 2891 | case ISN_OPNR: ins = "OPNR"; break; |
| 2892 | case ISN_OPFLOAT: ins = "OPFLOAT"; break; |
| 2893 | case ISN_OPANY: ins = "OPANY"; break; |
| 2894 | default: ins = "???"; break; |
| 2895 | } |
| 2896 | smsg("%4d %s %s", current, ins, what); |
| 2897 | } |
| 2898 | break; |
| 2899 | |
| 2900 | case ISN_COMPAREBOOL: |
| 2901 | case ISN_COMPARESPECIAL: |
| 2902 | case ISN_COMPARENR: |
| 2903 | case ISN_COMPAREFLOAT: |
| 2904 | case ISN_COMPARESTRING: |
| 2905 | case ISN_COMPAREBLOB: |
| 2906 | case ISN_COMPARELIST: |
| 2907 | case ISN_COMPAREDICT: |
| 2908 | case ISN_COMPAREFUNC: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2909 | case ISN_COMPAREANY: |
| 2910 | { |
| 2911 | char *p; |
| 2912 | char buf[10]; |
| 2913 | char *type; |
| 2914 | |
| 2915 | switch (iptr->isn_arg.op.op_type) |
| 2916 | { |
| 2917 | case EXPR_EQUAL: p = "=="; break; |
| 2918 | case EXPR_NEQUAL: p = "!="; break; |
| 2919 | case EXPR_GREATER: p = ">"; break; |
| 2920 | case EXPR_GEQUAL: p = ">="; break; |
| 2921 | case EXPR_SMALLER: p = "<"; break; |
| 2922 | case EXPR_SEQUAL: p = "<="; break; |
| 2923 | case EXPR_MATCH: p = "=~"; break; |
| 2924 | case EXPR_IS: p = "is"; break; |
| 2925 | case EXPR_ISNOT: p = "isnot"; break; |
| 2926 | case EXPR_NOMATCH: p = "!~"; break; |
| 2927 | default: p = "???"; break; |
| 2928 | } |
| 2929 | STRCPY(buf, p); |
| 2930 | if (iptr->isn_arg.op.op_ic == TRUE) |
| 2931 | strcat(buf, "?"); |
| 2932 | switch(iptr->isn_type) |
| 2933 | { |
| 2934 | case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break; |
| 2935 | case ISN_COMPARESPECIAL: |
| 2936 | type = "COMPARESPECIAL"; break; |
| 2937 | case ISN_COMPARENR: type = "COMPARENR"; break; |
| 2938 | case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break; |
| 2939 | case ISN_COMPARESTRING: |
| 2940 | type = "COMPARESTRING"; break; |
| 2941 | case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break; |
| 2942 | case ISN_COMPARELIST: type = "COMPARELIST"; break; |
| 2943 | case ISN_COMPAREDICT: type = "COMPAREDICT"; break; |
| 2944 | case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2945 | case ISN_COMPAREANY: type = "COMPAREANY"; break; |
| 2946 | default: type = "???"; break; |
| 2947 | } |
| 2948 | |
| 2949 | smsg("%4d %s %s", current, type, buf); |
| 2950 | } |
| 2951 | break; |
| 2952 | |
| 2953 | case ISN_ADDLIST: smsg("%4d ADDLIST", current); break; |
| 2954 | case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break; |
| 2955 | |
| 2956 | // expression operations |
| 2957 | case ISN_CONCAT: smsg("%4d CONCAT", current); break; |
| 2958 | case ISN_INDEX: smsg("%4d INDEX", current); break; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2959 | case ISN_SLICE: smsg("%4d SLICE %lld", |
| 2960 | current, iptr->isn_arg.number); break; |
Bram Moolenaar | 47a519a | 2020-06-14 23:05:10 +0200 | [diff] [blame] | 2961 | case ISN_GETITEM: smsg("%4d ITEM %lld", |
| 2962 | current, iptr->isn_arg.number); break; |
Bram Moolenaar | 1cc2a94 | 2020-05-10 19:10:31 +0200 | [diff] [blame] | 2963 | case ISN_MEMBER: smsg("%4d MEMBER", current); break; |
| 2964 | case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current, |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2965 | iptr->isn_arg.string); break; |
| 2966 | case ISN_NEGATENR: smsg("%4d NEGATENR", current); break; |
| 2967 | |
| 2968 | case ISN_CHECKNR: smsg("%4d CHECKNR", current); break; |
| 2969 | case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current, |
| 2970 | vartype_name(iptr->isn_arg.type.ct_type), |
| 2971 | iptr->isn_arg.type.ct_off); |
| 2972 | break; |
Bram Moolenaar | 9af7876 | 2020-06-16 11:34:42 +0200 | [diff] [blame] | 2973 | case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current, |
| 2974 | iptr->isn_arg.checklen.cl_more_OK ? ">= " : "", |
| 2975 | iptr->isn_arg.checklen.cl_min_len); |
| 2976 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2977 | case ISN_2BOOL: if (iptr->isn_arg.number) |
| 2978 | smsg("%4d INVERT (!val)", current); |
| 2979 | else |
| 2980 | smsg("%4d 2BOOL (!!val)", current); |
| 2981 | break; |
Bram Moolenaar | db99f9f | 2020-03-23 22:12:22 +0100 | [diff] [blame] | 2982 | case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current, |
| 2983 | (long long)(iptr->isn_arg.number)); |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2984 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2985 | |
Bram Moolenaar | 389df25 | 2020-07-09 21:20:47 +0200 | [diff] [blame] | 2986 | case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current, |
| 2987 | iptr->isn_arg.shuffle.shfl_item, |
| 2988 | iptr->isn_arg.shuffle.shfl_up); |
| 2989 | break; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2990 | case ISN_DROP: smsg("%4d DROP", current); break; |
| 2991 | } |
| 2992 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* |
| 2996 | * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty |
| 2997 | * list, etc. Mostly like what JavaScript does, except that empty list and |
| 2998 | * empty dictionary are FALSE. |
| 2999 | */ |
| 3000 | int |
| 3001 | tv2bool(typval_T *tv) |
| 3002 | { |
| 3003 | switch (tv->v_type) |
| 3004 | { |
| 3005 | case VAR_NUMBER: |
| 3006 | return tv->vval.v_number != 0; |
| 3007 | case VAR_FLOAT: |
| 3008 | #ifdef FEAT_FLOAT |
| 3009 | return tv->vval.v_float != 0.0; |
| 3010 | #else |
| 3011 | break; |
| 3012 | #endif |
| 3013 | case VAR_PARTIAL: |
| 3014 | return tv->vval.v_partial != NULL; |
| 3015 | case VAR_FUNC: |
| 3016 | case VAR_STRING: |
| 3017 | return tv->vval.v_string != NULL && *tv->vval.v_string != NUL; |
| 3018 | case VAR_LIST: |
| 3019 | return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0; |
| 3020 | case VAR_DICT: |
| 3021 | return tv->vval.v_dict != NULL |
| 3022 | && tv->vval.v_dict->dv_hashtab.ht_used > 0; |
| 3023 | case VAR_BOOL: |
| 3024 | case VAR_SPECIAL: |
| 3025 | return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE; |
| 3026 | case VAR_JOB: |
| 3027 | #ifdef FEAT_JOB_CHANNEL |
| 3028 | return tv->vval.v_job != NULL; |
| 3029 | #else |
| 3030 | break; |
| 3031 | #endif |
| 3032 | case VAR_CHANNEL: |
| 3033 | #ifdef FEAT_JOB_CHANNEL |
| 3034 | return tv->vval.v_channel != NULL; |
| 3035 | #else |
| 3036 | break; |
| 3037 | #endif |
| 3038 | case VAR_BLOB: |
| 3039 | return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0; |
| 3040 | case VAR_UNKNOWN: |
Bram Moolenaar | 4c68375 | 2020-04-05 21:38:23 +0200 | [diff] [blame] | 3041 | case VAR_ANY: |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3042 | case VAR_VOID: |
| 3043 | break; |
| 3044 | } |
| 3045 | return FALSE; |
| 3046 | } |
| 3047 | |
| 3048 | /* |
| 3049 | * If "tv" is a string give an error and return FAIL. |
| 3050 | */ |
| 3051 | int |
| 3052 | check_not_string(typval_T *tv) |
| 3053 | { |
| 3054 | if (tv->v_type == VAR_STRING) |
| 3055 | { |
| 3056 | emsg(_("E1030: Using a String as a Number")); |
| 3057 | clear_tv(tv); |
| 3058 | return FAIL; |
| 3059 | } |
| 3060 | return OK; |
| 3061 | } |
| 3062 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3063 | |
| 3064 | #endif // FEAT_EVAL |