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