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