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