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