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