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