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