interp.c 54.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright 2011 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <assert.h>

#include "vbscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(vbscript);

27 28
static DISPID propput_dispid = DISPID_PROPERTYPUT;

29 30 31
typedef struct {
    vbscode_t *code;
    instr_t *instr;
32
    script_ctx_t *script;
33
    function_t *func;
34
    vbdisp_t *vbthis;
35

36
    VARIANT *args;
37
    VARIANT *vars;
38
    SAFEARRAY **arrays;
39

40
    dynamic_var_t *dynamic_vars;
41
    heap_pool_t heap;
42

43 44
    BOOL resume_next;

45 46 47
    unsigned stack_size;
    unsigned top;
    VARIANT *stack;
48 49

    VARIANT ret_val;
50 51 52 53
} exec_ctx_t;

typedef HRESULT (*instr_func_t)(exec_ctx_t*);

54 55
typedef enum {
    REF_NONE,
56
    REF_DISP,
57
    REF_VAR,
58
    REF_OBJ,
59
    REF_CONST,
60
    REF_FUNC
61 62 63 64 65 66 67 68 69
} ref_type_t;

typedef struct {
    ref_type_t type;
    union {
        struct {
            IDispatch *disp;
            DISPID id;
        } d;
70
        VARIANT *v;
71
        function_t *f;
72
        IDispatch *obj;
73 74 75
    } u;
} ref_t;

76 77 78 79 80 81
typedef struct {
    VARIANT *v;
    VARIANT store;
    BOOL owned;
} variant_val_t;

82 83 84
static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
{
    while(var) {
85
        if(!wcsicmp(var->name, name)) {
86
            ref->type = var->is_const ? REF_CONST : REF_VAR;
87 88 89 90 91 92 93 94 95 96
            ref->u.v = &var->v;
            return TRUE;
        }

        var = var->next;
    }

    return FALSE;
}

97
static BOOL lookup_global_vars(ScriptDisp *script, const WCHAR *name, ref_t *ref)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
    dynamic_var_t **vars = script->global_vars;
    size_t i, cnt = script->global_vars_cnt;

    for(i = 0; i < cnt; i++) {
        if(!wcsicmp(vars[i]->name, name)) {
            ref->type = vars[i]->is_const ? REF_CONST : REF_VAR;
            ref->u.v = &vars[i]->v;
            return TRUE;
        }
    }

    return FALSE;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static BOOL lookup_global_funcs(ScriptDisp *script, const WCHAR *name, ref_t *ref)
{
    function_t **funcs = script->global_funcs;
    size_t i, cnt = script->global_funcs_cnt;

    for(i = 0; i < cnt; i++) {
        if(!wcsicmp(funcs[i]->name, name)) {
            ref->type = REF_FUNC;
            ref->u.f = funcs[i];
            return TRUE;
        }
    }

    return FALSE;
}

129
static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
130
{
131
    ScriptDisp *script_obj = ctx->script->script_obj;
132
    named_item_t *item;
133
    unsigned i;
134 135 136
    DISPID id;
    HRESULT hres;

137 138
    if((ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
       && !wcsicmp(name, ctx->func->name)) {
139 140 141 142 143
        ref->type = REF_VAR;
        ref->u.v = &ctx->ret_val;
        return S_OK;
    }

144 145 146 147 148 149 150
    if(ctx->func->type != FUNC_GLOBAL) {
        for(i=0; i < ctx->func->var_cnt; i++) {
            if(!wcsicmp(ctx->func->vars[i].name, name)) {
                ref->type = REF_VAR;
                ref->u.v = ctx->vars+i;
                return TRUE;
            }
151 152
        }

153 154 155 156 157 158
        for(i=0; i < ctx->func->arg_cnt; i++) {
            if(!wcsicmp(ctx->func->args[i].name, name)) {
                ref->type = REF_VAR;
                ref->u.v = ctx->args+i;
                return S_OK;
            }
159 160
        }

161 162
        if(lookup_dynamic_vars(ctx->dynamic_vars, name, ref))
            return S_OK;
163

164 165 166
        if(ctx->vbthis) {
            /* FIXME: Bind such identifier while generating bytecode. */
            for(i=0; i < ctx->vbthis->desc->prop_cnt; i++) {
167
                if(!wcsicmp(ctx->vbthis->desc->props[i].name, name)) {
168 169 170 171 172 173
                    ref->type = REF_VAR;
                    ref->u.v = ctx->vbthis->props+i;
                    return S_OK;
                }
            }

174 175 176 177 178 179 180
            hres = vbdisp_get_id(ctx->vbthis, name, invoke_type, TRUE, &id);
            if(SUCCEEDED(hres)) {
                ref->type = REF_DISP;
                ref->u.d.disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
                ref->u.d.id = id;
                return S_OK;
            }
181 182 183 184 185 186 187 188 189
        }
    }

    if(ctx->func->code_ctx->context) {
        hres = disp_get_id(ctx->func->code_ctx->context, name, invoke_type, TRUE, &id);
        if(SUCCEEDED(hres)) {
            ref->type = REF_DISP;
            ref->u.d.disp = ctx->func->code_ctx->context;
            ref->u.d.id = id;
190 191
            return S_OK;
        }
192 193
    }

194
    if(lookup_global_vars(script_obj, name, ref))
195
        return S_OK;
196 197
    if(lookup_global_funcs(script_obj, name, ref))
        return S_OK;
198

199
    hres = get_builtin_id(ctx->script->global_obj, name, &id);
200 201
    if(SUCCEEDED(hres)) {
        ref->type = REF_DISP;
202
        ref->u.d.disp = &ctx->script->global_obj->IDispatch_iface;
203 204 205 206
        ref->u.d.id = id;
        return S_OK;
    }

207 208
    item = lookup_named_item(ctx->script, name, SCRIPTITEM_ISVISIBLE);
    if(item && item->disp) {
209
        ref->type = REF_OBJ;
210
        ref->u.obj = item->disp;
211
        return S_OK;
212 213
    }

214 215 216 217 218 219 220 221 222 223 224 225
    LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
        if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
            hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
            if(SUCCEEDED(hres)) {
                ref->type = REF_DISP;
                ref->u.d.disp = item->disp;
                ref->u.d.id = id;
                return S_OK;
            }
        }
    }

226 227 228 229
    ref->type = REF_NONE;
    return S_OK;
}

230
static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
231
        BOOL is_const, VARIANT **out_var)
232
{
233
    ScriptDisp *script_obj = ctx->script->script_obj;
234
    dynamic_var_t *new_var;
235
    heap_pool_t *heap;
236 237 238
    WCHAR *str;
    unsigned size;

239
    heap = ctx->func->type == FUNC_GLOBAL ? &script_obj->heap : &ctx->heap;
240

241
    new_var = heap_pool_alloc(heap, sizeof(*new_var));
242 243 244
    if(!new_var)
        return E_OUTOFMEMORY;

245
    size = (lstrlenW(name)+1)*sizeof(WCHAR);
246
    str = heap_pool_alloc(heap, size);
247 248 249 250
    if(!str)
        return E_OUTOFMEMORY;
    memcpy(str, name, size);
    new_var->name = str;
251
    new_var->is_const = is_const;
252
    new_var->array = NULL;
253
    V_VT(&new_var->v) = VT_EMPTY;
254 255

    if(ctx->func->type == FUNC_GLOBAL) {
256 257
        size_t cnt = script_obj->global_vars_cnt + 1;
        if(cnt > script_obj->global_vars_size) {
258
            dynamic_var_t **new_vars;
259 260
            if(script_obj->global_vars)
                new_vars = heap_realloc(script_obj->global_vars, cnt * 2 * sizeof(*new_vars));
261 262 263 264
            else
                new_vars = heap_alloc(cnt * 2 * sizeof(*new_vars));
            if(!new_vars)
                return E_OUTOFMEMORY;
265 266
            script_obj->global_vars = new_vars;
            script_obj->global_vars_size = cnt * 2;
267
        }
268
        script_obj->global_vars[script_obj->global_vars_cnt++] = new_var;
269 270 271 272 273
    }else {
        new_var->next = ctx->dynamic_vars;
        ctx->dynamic_vars = new_var;
    }

274
    *out_var = &new_var->v;
275 276 277
    return S_OK;
}

278 279 280 281 282 283 284 285
void clear_ei(EXCEPINFO *ei)
{
    SysFreeString(ei->bstrSource);
    SysFreeString(ei->bstrDescription);
    SysFreeString(ei->bstrHelpFile);
    memset(ei, 0, sizeof(*ei));
}

286 287 288 289 290 291 292 293
static void clear_error_loc(script_ctx_t *ctx)
{
    if(ctx->error_loc_code) {
        release_vbscode(ctx->error_loc_code);
        ctx->error_loc_code = NULL;
    }
}

294 295 296 297 298 299
static inline VARIANT *stack_pop(exec_ctx_t *ctx)
{
    assert(ctx->top);
    return ctx->stack + --ctx->top;
}

300 301 302 303 304 305
static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
{
    assert(ctx->top >= n);
    return ctx->stack + (ctx->top-n-1);
}

306 307 308 309 310
static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
{
    if(ctx->stack_size == ctx->top) {
        VARIANT *new_stack;

311
        new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
312 313 314 315 316 317 318 319 320 321 322 323 324
        if(!new_stack) {
            VariantClear(v);
            return E_OUTOFMEMORY;
        }

        ctx->stack = new_stack;
        ctx->stack_size *= 2;
    }

    ctx->stack[ctx->top++] = *v;
    return S_OK;
}

325 326 327 328 329 330 331
static inline HRESULT stack_push_null(exec_ctx_t *ctx)
{
    VARIANT v;
    V_VT(&v) = VT_NULL;
    return stack_push(ctx, &v);
}

332 333 334 335 336 337
static void stack_popn(exec_ctx_t *ctx, unsigned n)
{
    while(n--)
        VariantClear(stack_pop(ctx));
}

338
static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
339
{
340
    VARIANT *v;
341

342 343 344 345
    v = stack_pop(ctx);
    if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
        r->owned = FALSE;
        r->v = V_VARIANTREF(v);
346
    }else {
347 348
        r->owned = TRUE;
        r->v = v;
349
    }
350 351 352 353 354 355 356 357 358 359 360
}

static inline void release_val(variant_val_t *v)
{
    if(v->owned)
        VariantClear(v->v);
}

static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *r)
{
    stack_pop_deref(ctx, r);
361

362
    if(V_VT(r->v) == VT_DISPATCH) {
363 364
        HRESULT hres;

365
        hres = get_disp_value(ctx->script, V_DISPATCH(r->v), &r->store);
366
        if(r->owned && V_DISPATCH(r->v))
367
            IDispatch_Release(V_DISPATCH(r->v));
368 369 370
        if(FAILED(hres))
            return hres;

371 372
        r->owned = TRUE;
        r->v = &r->store;
373 374 375 376 377
    }

    return S_OK;
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
{
    VARIANT *v = stack_top(ctx, n);
    HRESULT hres;

    if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
        VARIANT *ref = V_VARIANTREF(v);

        V_VT(v) = VT_EMPTY;
        hres = VariantCopy(v, ref);
        if(FAILED(hres))
            return hres;
    }

    if(V_VT(v) == VT_DISPATCH) {
        IDispatch *disp;

        disp = V_DISPATCH(v);
396
        hres = get_disp_value(ctx->script, disp, v);
397 398
        if(disp)
            IDispatch_Release(disp);
399 400 401 402 403 404 405
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419
static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
{
    variant_val_t val;
    HRESULT hres;

    hres = stack_pop_val(ctx, &val);
    if(FAILED(hres))
        return hres;

    switch (V_VT(val.v))
    {
    case VT_BOOL:
        *b = V_BOOL(val.v);
        break;
420
    case VT_NULL:
421
    case VT_EMPTY:
422 423
        *b = FALSE;
        break;
424 425 426 427 428 429 430 431 432 433 434 435 436 437
    case VT_I2:
        *b = V_I2(val.v);
        break;
    case VT_I4:
        *b = V_I4(val.v);
        break;
    default:
        FIXME("unsupported for %s\n", debugstr_variant(val.v));
        release_val(&val);
        return E_NOTIMPL;
    }
    return S_OK;
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
{
    VARIANT *v = stack_pop(ctx);

    if(V_VT(v) == VT_DISPATCH) {
        *ret = V_DISPATCH(v);
        return S_OK;
    }

    if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
        FIXME("not supported type: %s\n", debugstr_variant(v));
        VariantClear(v);
        return E_FAIL;
    }

    v = V_BYREF(v);
    if(V_VT(v) != VT_DISPATCH) {
        FIXME("not disp %s\n", debugstr_variant(v));
        return E_FAIL;
    }

    if(V_DISPATCH(v))
        IDispatch_AddRef(V_DISPATCH(v));
    *ret = V_DISPATCH(v);
    return S_OK;
}

465 466 467 468
static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
{
    VARIANT *v = stack_top(ctx, n), *ref;

469
    if(V_VT(v) != VT_DISPATCH && (disp || V_VT(v) != VT_UNKNOWN)) {
470 471 472 473 474 475
        if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
            FIXME("not supported type: %s\n", debugstr_variant(v));
            return E_FAIL;
        }

        ref = V_VARIANTREF(v);
476
        if(V_VT(ref) != VT_DISPATCH && (disp || V_VT(ref) != VT_UNKNOWN)) {
477 478 479 480
            FIXME("not disp %s\n", debugstr_variant(ref));
            return E_FAIL;
        }

481 482 483 484
        V_VT(v) = V_VT(ref);
        V_UNKNOWN(v) = V_UNKNOWN(ref);
        if(V_UNKNOWN(v))
            IUnknown_AddRef(V_UNKNOWN(v));
485 486 487 488 489 490 491
    }

    if(disp)
        *disp = V_DISPATCH(v);
    return S_OK;
}

492 493 494 495 496
static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
{
    ctx->instr = ctx->code->instrs + addr;
}

497
static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
498
{
499 500 501
    dp->cNamedArgs = is_propput ? 1 : 0;
    dp->cArgs = arg_cnt + dp->cNamedArgs;
    dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
502 503 504 505 506 507 508 509 510 511 512 513 514

    if(arg_cnt) {
        VARIANT tmp;
        unsigned i;

        assert(ctx->top >= arg_cnt);

        for(i=1; i*2 <= arg_cnt; i++) {
            tmp = ctx->stack[ctx->top-i];
            ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
            ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
        }

515
        dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
516
    }else {
517
        dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
518 519 520
    }
}

521 522
static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
{
523 524
    unsigned i, argc = arg_cnt(dp);
    LONG *indices;
525 526 527 528 529 530 531
    HRESULT hres;

    if(!array) {
        FIXME("NULL array\n");
        return E_FAIL;
    }

532 533 534 535
    hres = SafeArrayLock(array);
    if(FAILED(hres))
        return hres;

536 537
    if(array->cDims != argc) {
        FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
538
        SafeArrayUnlock(array);
539 540 541
        return E_FAIL;
    }

542 543 544 545 546
    indices = heap_alloc(sizeof(*indices) * argc);
    if(!indices) {
        SafeArrayUnlock(array);
        return E_OUTOFMEMORY;
    }
547

548 549 550 551 552 553
    for(i=0; i<argc; i++) {
        hres = to_int(get_arg(dp, i), indices+i);
        if(FAILED(hres)) {
            heap_free(indices);
            SafeArrayUnlock(array);
            return hres;
554 555 556
        }
    }

557 558 559 560
    hres = SafeArrayPtrOfIndex(array, indices, (void**)ret);
    SafeArrayUnlock(array);
    heap_free(indices);
    return hres;
561 562
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
static HRESULT variant_call(exec_ctx_t *ctx, VARIANT *v, unsigned arg_cnt, VARIANT *res)
{
    SAFEARRAY *array = NULL;
    DISPPARAMS dp;
    HRESULT hres;

    TRACE("%s\n", debugstr_variant(v));

    if(V_VT(v) == (VT_VARIANT|VT_BYREF))
        v = V_VARIANTREF(v);

    switch(V_VT(v)) {
    case VT_ARRAY|VT_BYREF|VT_VARIANT:
        array = *V_ARRAYREF(v);
        break;
    case VT_ARRAY|VT_VARIANT:
        array = V_ARRAY(v);
        break;
    case VT_DISPATCH:
        vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
        hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
        break;
    default:
        FIXME("unsupported on %s\n", debugstr_variant(v));
        return E_NOTIMPL;
    }

    if(array) {
        if(!res) {
            FIXME("no res\n");
            return E_NOTIMPL;
        }

        vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
        hres = array_access(ctx, array, &dp, &v);
        if(FAILED(hres))
            return hres;

        V_VT(res) = VT_BYREF|VT_VARIANT;
        V_BYREF(res) = v;
    }

    stack_popn(ctx, arg_cnt);
    return S_OK;
}

609
static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
610
{
611
    BSTR identifier = ctx->instr->arg1.bstr;
612
    const unsigned arg_cnt = ctx->instr->arg2.uint;
613
    DISPPARAMS dp;
614
    ref_t ref;
615 616
    HRESULT hres;

617 618
    TRACE("%s %u\n", debugstr_w(identifier), arg_cnt);

619
    hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
620 621 622 623
    if(FAILED(hres))
        return hres;

    switch(ref.type) {
624
    case REF_VAR:
625 626 627
    case REF_CONST:
        if(arg_cnt)
            return variant_call(ctx, ref.u.v, arg_cnt, res);
628

629 630 631 632 633 634
        if(!res) {
            FIXME("REF_VAR no res\n");
            return E_NOTIMPL;
        }

        V_VT(res) = VT_BYREF|VT_VARIANT;
635
        V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
636
        break;
637
    case REF_DISP:
638
        vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
639
        hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
640 641 642
        if(FAILED(hres))
            return hres;
        break;
643
    case REF_FUNC:
644
        vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
645
        hres = exec_script(ctx->script, FALSE, ref.u.f, NULL, &dp, res);
646 647 648
        if(FAILED(hres))
            return hres;
        break;
649 650 651 652 653 654 655 656 657 658 659 660
    case REF_OBJ:
        if(arg_cnt) {
            FIXME("arguments on object\n");
            return E_NOTIMPL;
        }

        if(res) {
            IDispatch_AddRef(ref.u.obj);
            V_VT(res) = VT_DISPATCH;
            V_DISPATCH(res) = ref.u.obj;
        }
        break;
661
    case REF_NONE:
662
        if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
663 664
            VARIANT *new;
            hres = add_dynamic_var(ctx, identifier, FALSE, &new);
665 666 667 668 669 670
            if(FAILED(hres))
                return hres;
            V_VT(res) = VT_BYREF|VT_VARIANT;
            V_BYREF(res) = new;
            break;
        }
671 672 673 674
        FIXME("%s not found\n", debugstr_w(identifier));
        return DISP_E_UNKNOWNNAME;
    }

675
    stack_popn(ctx, arg_cnt);
676
    return S_OK;
677 678
}

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
static HRESULT interp_icall(exec_ctx_t *ctx)
{
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = do_icall(ctx, &v);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
}

static HRESULT interp_icallv(exec_ctx_t *ctx)
{
    TRACE("\n");
    return do_icall(ctx, NULL);
}

699 700
static HRESULT interp_vcall(exec_ctx_t *ctx)
{
701 702 703 704 705 706 707 708 709 710 711 712 713
    const unsigned arg_cnt = ctx->instr->arg1.uint;
    VARIANT res, *v;
    HRESULT hres;

    TRACE("\n");

    v = stack_pop(ctx);
    hres = variant_call(ctx, v, arg_cnt, &res);
    VariantClear(v);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &res);
714 715 716 717
}

static HRESULT interp_vcallv(exec_ctx_t *ctx)
{
718 719 720 721 722 723 724 725 726 727
    const unsigned arg_cnt = ctx->instr->arg1.uint;
    VARIANT *v;
    HRESULT hres;

    TRACE("\n");

    v = stack_pop(ctx);
    hres = variant_call(ctx, v, arg_cnt, NULL);
    VariantClear(v);
    return hres;
728 729
}

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
{
    const BSTR identifier = ctx->instr->arg1.bstr;
    const unsigned arg_cnt = ctx->instr->arg2.uint;
    IDispatch *obj;
    DISPPARAMS dp;
    DISPID id;
    HRESULT hres;

    hres = stack_pop_disp(ctx, &obj);
    if(FAILED(hres))
        return hres;

    if(!obj) {
        FIXME("NULL obj\n");
        return E_FAIL;
    }

748
    vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
749

750
    hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    if(SUCCEEDED(hres))
        hres = disp_call(ctx->script, obj, id, &dp, res);
    IDispatch_Release(obj);
    if(FAILED(hres))
        return hres;

    stack_popn(ctx, arg_cnt);
    return S_OK;
}

static HRESULT interp_mcall(exec_ctx_t *ctx)
{
    VARIANT res;
    HRESULT hres;

    TRACE("\n");

    hres = do_mcall(ctx, &res);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &res);
}

static HRESULT interp_mcallv(exec_ctx_t *ctx)
{
777 778 779
    TRACE("\n");

    return do_mcall(ctx, NULL);
780 781
}

782 783
static HRESULT assign_value(exec_ctx_t *ctx, VARIANT *dst, VARIANT *src, WORD flags)
{
784
    VARIANT value;
785 786
    HRESULT hres;

787 788
    V_VT(&value) = VT_EMPTY;
    hres = VariantCopyInd(&value, src);
789 790 791
    if(FAILED(hres))
        return hres;

792 793
    if(V_VT(&value) == VT_DISPATCH && !(flags & DISPATCH_PROPERTYPUTREF)) {
        IDispatch *disp = V_DISPATCH(&value);
794

795 796 797 798
        V_VT(&value) = VT_EMPTY;
        hres = get_disp_value(ctx->script, disp, &value);
        if(disp)
            IDispatch_Release(disp);
799 800 801 802
        if(FAILED(hres))
            return hres;
    }

803 804
    VariantClear(dst);
    *dst = value;
805 806 807
    return S_OK;
}

808
static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, WORD flags, DISPPARAMS *dp)
809 810 811 812
{
    ref_t ref;
    HRESULT hres;

813
    hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
814 815 816 817
    if(FAILED(hres))
        return hres;

    switch(ref.type) {
818 819 820
    case REF_VAR: {
        VARIANT *v = ref.u.v;

821 822 823
        if(V_VT(v) == (VT_VARIANT|VT_BYREF))
            v = V_VARIANTREF(v);

824
        if(arg_cnt(dp)) {
825 826
            SAFEARRAY *array;

827 828 829 830 831
            if(V_VT(v) == VT_DISPATCH) {
                hres = disp_propput(ctx->script, V_DISPATCH(v), DISPID_VALUE, flags, dp);
                break;
            }

832 833 834 835 836 837 838 839 840 841 842 843 844
            if(!(V_VT(v) & VT_ARRAY)) {
                FIXME("array assign on type %d\n", V_VT(v));
                return E_FAIL;
            }

            switch(V_VT(v)) {
            case VT_ARRAY|VT_BYREF|VT_VARIANT:
                array = *V_ARRAYREF(v);
                break;
            case VT_ARRAY|VT_VARIANT:
                array = V_ARRAY(v);
                break;
            default:
845
                FIXME("Unsupported array type %x\n", V_VT(v));
846 847 848 849 850 851 852 853 854 855 856 857 858
                return E_NOTIMPL;
            }

            if(!array) {
                FIXME("null array\n");
                return E_FAIL;
            }

            hres = array_access(ctx, array, dp, &v);
            if(FAILED(hres))
                return hres;
        }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
            FIXME("non-array assign\n");
859 860 861
            return E_NOTIMPL;
        }

862
        hres = assign_value(ctx, v, dp->rgvarg, flags);
863
        break;
864
    }
865
    case REF_DISP:
866
        hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, flags, dp);
867
        break;
868 869 870
    case REF_FUNC:
        FIXME("functions not implemented\n");
        return E_NOTIMPL;
871 872 873
    case REF_OBJ:
        FIXME("REF_OBJ\n");
        return E_NOTIMPL;
874 875 876
    case REF_CONST:
        FIXME("REF_CONST\n");
        return E_NOTIMPL;
877
    case REF_NONE:
878 879
        if(ctx->func->code_ctx->option_explicit) {
            FIXME("throw exception\n");
880
            hres = E_FAIL;
881
        }else {
882 883
            VARIANT *new_var;

884 885 886 887 888
            if(arg_cnt(dp)) {
                FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
                return E_NOTIMPL;
            }

889
            TRACE("creating variable %s\n", debugstr_w(name));
890 891
            hres = add_dynamic_var(ctx, name, FALSE, &new_var);
            if(SUCCEEDED(hres))
892
                hres = assign_value(ctx, new_var, dp->rgvarg, flags);
893
        }
894 895 896 897 898
    }

    return hres;
}

899 900
static HRESULT interp_assign_ident(exec_ctx_t *ctx)
{
901
    const BSTR arg = ctx->instr->arg1.bstr;
902
    const unsigned arg_cnt = ctx->instr->arg2.uint;
903
    DISPPARAMS dp;
904 905 906 907
    HRESULT hres;

    TRACE("%s\n", debugstr_w(arg));

908
    vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
909
    hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUT, &dp);
910 911 912
    if(FAILED(hres))
        return hres;

913 914
    stack_popn(ctx, arg_cnt+1);
    return S_OK;
915 916
}

917 918 919
static HRESULT interp_set_ident(exec_ctx_t *ctx)
{
    const BSTR arg = ctx->instr->arg1.bstr;
920
    const unsigned arg_cnt = ctx->instr->arg2.uint;
921
    DISPPARAMS dp;
922 923
    HRESULT hres;

924
    TRACE("%s %u\n", debugstr_w(arg), arg_cnt);
925

926
    hres = stack_assume_disp(ctx, arg_cnt, NULL);
927 928 929
    if(FAILED(hres))
        return hres;

930 931
    vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
    hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUTREF, &dp);
932 933 934
    if(FAILED(hres))
        return hres;

935
    stack_popn(ctx, arg_cnt + 1);
936
    return S_OK;
937 938
}

939 940
static HRESULT interp_assign_member(exec_ctx_t *ctx)
{
941
    BSTR identifier = ctx->instr->arg1.bstr;
942
    const unsigned arg_cnt = ctx->instr->arg2.uint;
943
    IDispatch *obj;
944
    DISPPARAMS dp;
945 946 947 948 949
    DISPID id;
    HRESULT hres;

    TRACE("%s\n", debugstr_w(identifier));

950
    hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
951 952 953 954 955 956 957 958
    if(FAILED(hres))
        return hres;

    if(!obj) {
        FIXME("NULL obj\n");
        return E_FAIL;
    }

959
    hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
960 961
    if(SUCCEEDED(hres)) {
        vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
962
        hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUT, &dp);
963
    }
964 965 966
    if(FAILED(hres))
        return hres;

967
    stack_popn(ctx, arg_cnt+2);
968
    return S_OK;
969 970
}

971 972 973
static HRESULT interp_set_member(exec_ctx_t *ctx)
{
    BSTR identifier = ctx->instr->arg1.bstr;
974
    const unsigned arg_cnt = ctx->instr->arg2.uint;
975
    IDispatch *obj;
976
    DISPPARAMS dp;
977 978 979 980 981
    DISPID id;
    HRESULT hres;

    TRACE("%s\n", debugstr_w(identifier));

982 983 984 985 986
    if(arg_cnt) {
        FIXME("arguments not supported\n");
        return E_NOTIMPL;
    }

987
    hres = stack_assume_disp(ctx, 1, &obj);
988 989 990 991 992 993 994 995
    if(FAILED(hres))
        return hres;

    if(!obj) {
        FIXME("NULL obj\n");
        return E_FAIL;
    }

996 997
    hres = stack_assume_disp(ctx, 0, NULL);
    if(FAILED(hres))
998 999 1000
        return hres;

    hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
1001 1002
    if(SUCCEEDED(hres)) {
        vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
1003
        hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUTREF, &dp);
1004
    }
1005 1006
    if(FAILED(hres))
        return hres;
1007

1008 1009
    stack_popn(ctx, 2);
    return S_OK;
1010 1011
}

1012 1013 1014
static HRESULT interp_const(exec_ctx_t *ctx)
{
    BSTR arg = ctx->instr->arg1.bstr;
1015
    VARIANT *v;
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    ref_t ref;
    HRESULT hres;

    TRACE("%s\n", debugstr_w(arg));

    assert(ctx->func->type == FUNC_GLOBAL);

    hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
    if(FAILED(hres))
        return hres;

    if(ref.type != REF_NONE) {
        FIXME("%s already defined\n", debugstr_w(arg));
        return E_FAIL;
    }

1032
    hres = stack_assume_val(ctx, 0);
1033 1034 1035
    if(FAILED(hres))
        return hres;

1036 1037 1038 1039 1040 1041
    hres = add_dynamic_var(ctx, arg, TRUE, &v);
    if(FAILED(hres))
        return hres;

    *v = *stack_pop(ctx);
    return S_OK;
1042 1043
}

1044 1045
static HRESULT interp_val(exec_ctx_t *ctx)
{
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    variant_val_t val;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &val);
    if(FAILED(hres))
        return hres;

    if(!val.owned) {
        V_VT(&v) = VT_EMPTY;
        hres = VariantCopy(&v, val.v);
        if(FAILED(hres))
            return hres;
    }

    return stack_push(ctx, val.owned ? val.v : &v);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
}

static HRESULT interp_pop(exec_ctx_t *ctx)
{
    const unsigned n = ctx->instr->arg1.uint;

    TRACE("%u\n", n);

    stack_popn(ctx, n);
    return S_OK;
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
static HRESULT interp_stack(exec_ctx_t *ctx)
{
    const unsigned n = ctx->instr->arg1.uint;
    VARIANT v;
    HRESULT hres;

    TRACE("%#x\n", n);

    if(n == ~0)
        return MAKE_VBSERROR(505);
    assert(n < ctx->top);

    V_VT(&v) = VT_EMPTY;
    hres = VariantCopy(&v, ctx->stack + n);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
}

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
static HRESULT interp_deref(exec_ctx_t *ctx)
{
    VARIANT copy, *v = stack_top(ctx, 0);
    HRESULT hres;

    TRACE("%s\n", debugstr_variant(v));

    if(V_VT(v) != (VT_BYREF|VT_VARIANT))
        return S_OK;

    V_VT(&copy) = VT_EMPTY;
    hres = VariantCopy(&copy, V_VARIANTREF(v));
    if(SUCCEEDED(hres))
        *v = copy;
    return hres;
}

1113 1114 1115
static HRESULT interp_new(exec_ctx_t *ctx)
{
    const WCHAR *arg = ctx->instr->arg1.bstr;
1116 1117 1118 1119 1120
    class_desc_t *class_desc;
    vbdisp_t *obj;
    VARIANT v;
    HRESULT hres;

1121 1122
    static const WCHAR regexpW[] = {'r','e','g','e','x','p',0};

1123 1124
    TRACE("%s\n", debugstr_w(arg));

1125
    if(!wcsicmp(arg, regexpW)) {
1126 1127 1128 1129 1130 1131 1132 1133
        V_VT(&v) = VT_DISPATCH;
        hres = create_regexp(&V_DISPATCH(&v));
        if(FAILED(hres))
            return hres;

        return stack_push(ctx, &v);
    }

1134
    for(class_desc = ctx->script->script_obj->classes; class_desc; class_desc = class_desc->next) {
1135
        if(!wcsicmp(class_desc->name, arg))
1136 1137 1138 1139 1140 1141 1142
            break;
    }
    if(!class_desc) {
        FIXME("Class %s not found\n", debugstr_w(arg));
        return E_FAIL;
    }

1143
    hres = create_vbdisp(class_desc, &obj);
1144 1145 1146 1147 1148 1149
    if(FAILED(hres))
        return hres;

    V_VT(&v) = VT_DISPATCH;
    V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
    return stack_push(ctx, &v);
1150 1151
}

1152 1153
static HRESULT interp_dim(exec_ctx_t *ctx)
{
1154
    ScriptDisp *script_obj = ctx->script->script_obj;
1155 1156
    const BSTR ident = ctx->instr->arg1.bstr;
    const unsigned array_id = ctx->instr->arg2.uint;
1157
    const array_desc_t *array_desc;
1158 1159
    SAFEARRAY **array_ref;
    VARIANT *v;
1160 1161 1162 1163 1164 1165
    HRESULT hres;

    TRACE("%s\n", debugstr_w(ident));

    assert(array_id < ctx->func->array_cnt);

1166
    if(ctx->func->type == FUNC_GLOBAL) {
1167
        unsigned i;
1168 1169
        for(i = 0; i < script_obj->global_vars_cnt; i++) {
            if(!wcsicmp(script_obj->global_vars[i]->name, ident))
1170 1171
                break;
        }
1172 1173 1174
        assert(i < script_obj->global_vars_cnt);
        v = &script_obj->global_vars[i]->v;
        array_ref = &script_obj->global_vars[i]->array;
1175 1176
    }else {
        ref_t ref;
1177

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
        if(!ctx->arrays) {
            ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
            if(!ctx->arrays)
                return E_OUTOFMEMORY;
        }

        hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
        if(FAILED(hres)) {
            FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
            return hres;
        }

        if(ref.type != REF_VAR) {
            FIXME("got ref.type = %d\n", ref.type);
            return E_FAIL;
        }

        v = ref.u.v;
        array_ref = ctx->arrays + array_id;
1197 1198
    }

1199
    if(*array_ref) {
1200 1201 1202
        FIXME("Array already initialized\n");
        return E_FAIL;
    }
1203

1204 1205
    array_desc = ctx->func->array_descs + array_id;
    if(array_desc->dim_cnt) {
1206 1207
        *array_ref = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
        if(!*array_ref)
1208
            return E_OUTOFMEMORY;
1209 1210
    }

1211 1212
    V_VT(v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
    V_ARRAYREF(v) = array_ref;
1213
    return S_OK;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
}

static HRESULT array_bounds_from_stack(exec_ctx_t *ctx, unsigned dim_cnt, SAFEARRAYBOUND **ret)
{
    SAFEARRAYBOUND *bounds;
    unsigned i;
    int dim;
    HRESULT hres;

    if(!(bounds = heap_alloc(dim_cnt * sizeof(*bounds))))
        return E_OUTOFMEMORY;

    for(i = 0; i < dim_cnt; i++) {
        hres = to_int(stack_top(ctx, dim_cnt - i - 1), &dim);
        if(FAILED(hres)) {
            heap_free(bounds);
            return hres;
        }

        bounds[i].cElements = dim + 1;
        bounds[i].lLbound = 0;
    }

    stack_popn(ctx, dim_cnt);
    *ret = bounds;
    return S_OK;
}

static HRESULT interp_redim(exec_ctx_t *ctx)
{
    BSTR identifier = ctx->instr->arg1.bstr;
    const unsigned dim_cnt = ctx->instr->arg2.uint;
    SAFEARRAYBOUND *bounds;
    SAFEARRAY *array;
    ref_t ref;
    HRESULT hres;

    TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);

    hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
    if(FAILED(hres)) {
        FIXME("lookup %s failed: %08x\n", debugstr_w(identifier), hres);
        return hres;
    }

    if(ref.type != REF_VAR) {
        FIXME("got ref.type = %d\n", ref.type);
        return E_FAIL;
    }

    hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
    if(FAILED(hres))
        return hres;

    array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
    heap_free(bounds);
    if(!array)
        return E_OUTOFMEMORY;

    /* FIXME: We should check if we're not modifying an existing static array here */

    VariantClear(ref.u.v);
    V_VT(ref.u.v) = VT_ARRAY|VT_VARIANT;
    V_ARRAY(ref.u.v) = array;
    return S_OK;
1279 1280
}

1281 1282 1283
static HRESULT interp_step(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg2.bstr;
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
    BOOL gteq_zero;
    VARIANT zero;
    ref_t ref;
    HRESULT hres;

    TRACE("%s\n", debugstr_w(ident));

    V_VT(&zero) = VT_I2;
    V_I2(&zero) = 0;
    hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
    if(FAILED(hres))
        return hres;

    gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;

    hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
    if(FAILED(hres))
        return hres;

    if(ref.type != REF_VAR) {
        FIXME("%s is not REF_VAR\n", debugstr_w(ident));
        return E_FAIL;
    }

    hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
    if(FAILED(hres))
        return hres;

1312
    if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1313
        ctx->instr++;
1314 1315
    }else {
        stack_popn(ctx, 2);
1316
        instr_jmp(ctx, ctx->instr->arg1.uint);
1317
    }
1318
    return S_OK;
1319 1320
}

1321 1322
static HRESULT interp_newenum(exec_ctx_t *ctx)
{
1323
    variant_val_t v;
1324
    VARIANT *r;
1325 1326 1327 1328
    HRESULT hres;

    TRACE("\n");

1329
    stack_pop_deref(ctx, &v);
1330 1331
    assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
    r = stack_top(ctx, 0);
1332 1333

    switch(V_VT(v.v)) {
1334
    case VT_DISPATCH|VT_BYREF:
1335 1336 1337 1338 1339
    case VT_DISPATCH: {
        IEnumVARIANT *iter;
        DISPPARAMS dp = {0};
        VARIANT iterv;

1340 1341
        hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
        release_val(&v);
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
        if(FAILED(hres))
            return hres;

        if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
            FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
            VariantClear(&iterv);
            return hres;
        }

        hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
        IUnknown_Release(V_UNKNOWN(&iterv));
        if(FAILED(hres)) {
            FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
            return hres;
        }

1358 1359
        V_VT(r) = VT_UNKNOWN;
        V_UNKNOWN(r) = (IUnknown*)iter;
1360 1361
        break;
    }
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    case VT_VARIANT|VT_ARRAY:
    case VT_VARIANT|VT_ARRAY|VT_BYREF: {
        IEnumVARIANT *iter;

        hres = create_safearray_iter(V_ISBYREF(v.v) ? *V_ARRAYREF(v.v) : V_ARRAY(v.v), &iter);
        if(FAILED(hres))
            return hres;

        V_VT(r) = VT_UNKNOWN;
        V_UNKNOWN(r) = (IUnknown*)iter;
        break;
    }
1374
    default:
1375 1376
        FIXME("Unsupported for %s\n", debugstr_variant(v.v));
        release_val(&v);
1377 1378 1379
        return E_NOTIMPL;
    }

1380
    return S_OK;
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
}

static HRESULT interp_enumnext(exec_ctx_t *ctx)
{
    const unsigned loop_end = ctx->instr->arg1.uint;
    const BSTR ident = ctx->instr->arg2.bstr;
    VARIANT v;
    DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
    IEnumVARIANT *iter;
    BOOL do_continue;
    HRESULT hres;

    TRACE("\n");

1395 1396 1397 1398 1399
    if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
        FIXME("uninitialized\n");
        return E_FAIL;
    }

1400 1401 1402 1403 1404 1405 1406 1407 1408
    assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
    iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));

    V_VT(&v) = VT_EMPTY;
    hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
    if(FAILED(hres))
        return hres;

    do_continue = hres == S_OK;
1409
    hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1410 1411 1412 1413 1414 1415 1416
    VariantClear(&v);
    if(FAILED(hres))
        return hres;

    if(do_continue) {
        ctx->instr++;
    }else {
1417
        stack_popn(ctx, 1);
1418 1419 1420 1421 1422
        instr_jmp(ctx, loop_end);
    }
    return S_OK;
}

1423 1424
static HRESULT interp_jmp(exec_ctx_t *ctx)
{
1425 1426 1427 1428 1429 1430
    const unsigned arg = ctx->instr->arg1.uint;

    TRACE("%u\n", arg);

    instr_jmp(ctx, arg);
    return S_OK;
1431 1432 1433 1434
}

static HRESULT interp_jmp_false(exec_ctx_t *ctx)
{
1435 1436
    const unsigned arg = ctx->instr->arg1.uint;
    HRESULT hres;
1437
    BOOL b;
1438 1439 1440

    TRACE("%u\n", arg);

1441
    hres = stack_pop_bool(ctx, &b);
1442 1443 1444
    if(FAILED(hres))
        return hres;

1445
    if(b)
1446 1447 1448 1449
        ctx->instr++;
    else
        instr_jmp(ctx, ctx->instr->arg1.uint);
    return S_OK;
1450 1451
}

1452 1453 1454 1455
static HRESULT interp_jmp_true(exec_ctx_t *ctx)
{
    const unsigned arg = ctx->instr->arg1.uint;
    HRESULT hres;
1456
    BOOL b;
1457 1458 1459

    TRACE("%u\n", arg);

1460
    hres = stack_pop_bool(ctx, &b);
1461 1462 1463
    if(FAILED(hres))
        return hres;

1464
    if(b)
1465 1466 1467 1468 1469 1470
        instr_jmp(ctx, ctx->instr->arg1.uint);
    else
        ctx->instr++;
    return S_OK;
}

1471 1472 1473 1474 1475 1476 1477 1478
static HRESULT interp_ret(exec_ctx_t *ctx)
{
    TRACE("\n");

    ctx->instr = NULL;
    return S_OK;
}

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
static HRESULT interp_retval(exec_ctx_t *ctx)
{
    variant_val_t val;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &val);
    if(FAILED(hres))
        return hres;

    if(val.owned) {
        VariantClear(&ctx->ret_val);
        ctx->ret_val = *val.v;
    }
    else {
        hres = VariantCopy(&ctx->ret_val, val.v);
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}

1503 1504 1505 1506 1507 1508 1509 1510
static HRESULT interp_stop(exec_ctx_t *ctx)
{
    WARN("\n");

    /* NOTE: this should have effect in debugging mode (that we don't support yet) */
    return S_OK;
}

1511 1512
static HRESULT interp_me(exec_ctx_t *ctx)
{
1513
    IDispatch *disp;
1514 1515 1516 1517
    VARIANT v;

    TRACE("\n");

1518 1519 1520 1521 1522 1523 1524 1525
    if(ctx->vbthis)
        disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
    else if(ctx->script->host_global)
        disp = ctx->script->host_global;
    else
        disp = (IDispatch*)&ctx->script->script_obj->IDispatchEx_iface;

    IDispatch_AddRef(disp);
1526
    V_VT(&v) = VT_DISPATCH;
1527
    V_DISPATCH(&v) = disp;
1528
    return stack_push(ctx, &v);
1529 1530
}

1531 1532
static HRESULT interp_bool(exec_ctx_t *ctx)
{
1533 1534 1535 1536 1537 1538 1539 1540
    const VARIANT_BOOL arg = ctx->instr->arg1.lng;
    VARIANT v;

    TRACE("%s\n", arg ? "true" : "false");

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = arg;
    return stack_push(ctx, &v);
1541 1542
}

1543 1544 1545
static HRESULT interp_errmode(exec_ctx_t *ctx)
{
    const int err_mode = ctx->instr->arg1.uint;
1546 1547 1548 1549

    TRACE("%d\n", err_mode);

    ctx->resume_next = err_mode;
1550
    clear_ei(&ctx->script->ei);
1551
    return S_OK;
1552 1553
}

1554 1555
static HRESULT interp_string(exec_ctx_t *ctx)
{
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
    VARIANT v;

    TRACE("\n");

    V_VT(&v) = VT_BSTR;
    V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
    if(!V_BSTR(&v))
        return E_OUTOFMEMORY;

    return stack_push(ctx, &v);
1566 1567
}

1568
static HRESULT interp_int(exec_ctx_t *ctx)
1569
{
1570 1571 1572 1573 1574
    const LONG arg = ctx->instr->arg1.lng;
    VARIANT v;

    TRACE("%d\n", arg);

1575 1576 1577 1578 1579 1580 1581
    if(arg == (INT16)arg) {
        V_VT(&v) = VT_I2;
        V_I2(&v) = arg;
    }else {
        V_VT(&v) = VT_I4;
        V_I4(&v) = arg;
    }
1582
    return stack_push(ctx, &v);
1583 1584 1585 1586
}

static HRESULT interp_double(exec_ctx_t *ctx)
{
1587 1588 1589 1590 1591 1592 1593 1594
    const DOUBLE *arg = ctx->instr->arg1.dbl;
    VARIANT v;

    TRACE("%lf\n", *arg);

    V_VT(&v) = VT_R8;
    V_R8(&v) = *arg;
    return stack_push(ctx, &v);
1595 1596
}

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
static HRESULT interp_empty(exec_ctx_t *ctx)
{
    VARIANT v;

    TRACE("\n");

    V_VT(&v) = VT_EMPTY;
    return stack_push(ctx, &v);
}

1607 1608 1609
static HRESULT interp_null(exec_ctx_t *ctx)
{
    TRACE("\n");
1610
    return stack_push_null(ctx);
1611 1612
}

1613 1614
static HRESULT interp_nothing(exec_ctx_t *ctx)
{
1615 1616 1617 1618 1619 1620 1621
    VARIANT v;

    TRACE("\n");

    V_VT(&v) = VT_DISPATCH;
    V_DISPATCH(&v) = NULL;
    return stack_push(ctx, &v);
1622 1623
}

1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
static HRESULT interp_hres(exec_ctx_t *ctx)
{
    const unsigned arg = ctx->instr->arg1.uint;
    VARIANT v;

    TRACE("%d\n", arg);

    V_VT(&v) = VT_ERROR;
    V_ERROR(&v) = arg;
    return stack_push(ctx, &v);
}

1636 1637
static HRESULT interp_not(exec_ctx_t *ctx)
{
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
    variant_val_t val;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &val);
    if(FAILED(hres))
        return hres;

    hres = VarNot(val.v, &v);
    release_val(&val);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
1654 1655
}

1656 1657
static HRESULT interp_and(exec_ctx_t *ctx)
{
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarAnd(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
1678 1679
}

1680 1681
static HRESULT interp_or(exec_ctx_t *ctx)
{
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarOr(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
1702 1703
}

1704 1705
static HRESULT interp_xor(exec_ctx_t *ctx)
{
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarXor(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
1726 1727 1728 1729
}

static HRESULT interp_eqv(exec_ctx_t *ctx)
{
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarEqv(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
1750 1751 1752 1753
}

static HRESULT interp_imp(exec_ctx_t *ctx)
{
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarImp(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
1774 1775
}

1776 1777 1778 1779 1780 1781 1782 1783 1784
static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
{
    TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));

    /* FIXME: Fix comparing string to number */

    return VarCmp(l, r, ctx->script->lcid, 0);
 }

1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
static HRESULT cmp_oper(exec_ctx_t *ctx)
{
    variant_val_t l, r;
    HRESULT hres;

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
1795
    if(SUCCEEDED(hres)) {
1796 1797
        hres = var_cmp(ctx, l.v, r.v);
        release_val(&l);
1798
    }
1799 1800 1801 1802 1803

    release_val(&r);
    return hres;
}

1804 1805
static HRESULT interp_equal(exec_ctx_t *ctx)
{
1806 1807 1808 1809 1810 1811 1812 1813
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1814 1815
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1816 1817 1818 1819

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1820 1821
}

1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
static HRESULT interp_nequal(exec_ctx_t *ctx)
{
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1832 1833
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1834 1835 1836 1837 1838 1839

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
}

1840 1841
static HRESULT interp_gt(exec_ctx_t *ctx)
{
1842 1843 1844 1845 1846 1847 1848 1849
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1850 1851
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1852 1853 1854 1855

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1856 1857 1858 1859
}

static HRESULT interp_gteq(exec_ctx_t *ctx)
{
1860 1861 1862 1863 1864 1865 1866 1867
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1868 1869
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1870 1871 1872 1873

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1874 1875 1876 1877
}

static HRESULT interp_lt(exec_ctx_t *ctx)
{
1878 1879 1880 1881 1882 1883 1884 1885
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1886 1887
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1888 1889 1890 1891

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1892 1893 1894 1895
}

static HRESULT interp_lteq(exec_ctx_t *ctx)
{
1896 1897 1898 1899 1900 1901 1902 1903
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1904 1905
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1906 1907 1908 1909

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1910 1911
}

1912 1913
static HRESULT interp_case(exec_ctx_t *ctx)
{
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
    const unsigned arg = ctx->instr->arg1.uint;
    variant_val_t v;
    HRESULT hres;

    TRACE("%d\n", arg);

    hres = stack_pop_val(ctx, &v);
    if(FAILED(hres))
        return hres;

    hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
    release_val(&v);
    if(FAILED(hres))
        return hres;

    if(hres == VARCMP_EQ) {
        stack_popn(ctx, 1);
        instr_jmp(ctx, arg);
    }else {
        ctx->instr++;
    }

    return S_OK;
1937 1938
}

1939
static HRESULT interp_is(exec_ctx_t *ctx)
1940
{
1941 1942 1943
    IUnknown *l = NULL, *r = NULL;
    variant_val_t v;
    HRESULT hres = S_OK;
1944

1945
    TRACE("\n");
1946

1947 1948 1949 1950 1951 1952
    stack_pop_deref(ctx, &v);
    if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
        FIXME("Unhandled type %s\n", debugstr_variant(v.v));
        hres = E_NOTIMPL;
    }else if(V_UNKNOWN(v.v)) {
        hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&r);
1953
    }
1954
    if(v.owned) VariantClear(v.v);
1955 1956 1957
    if(FAILED(hres))
        return hres;

1958 1959 1960 1961 1962 1963
    stack_pop_deref(ctx, &v);
    if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
        FIXME("Unhandled type %s\n", debugstr_variant(v.v));
        hres = E_NOTIMPL;
    }else if(V_UNKNOWN(v.v)) {
        hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&l);
1964
    }
1965
    if(v.owned) VariantClear(v.v);
1966 1967

    if(SUCCEEDED(hres)) {
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
        VARIANT res;
        V_VT(&res) = VT_BOOL;
        if(r == l)
            V_BOOL(&res) = VARIANT_TRUE;
        else if(!r || !l)
            V_BOOL(&res) = VARIANT_FALSE;
        else {
            IObjectIdentity *identity;
            hres = IUnknown_QueryInterface(l, &IID_IObjectIdentity, (void**)&identity);
            if(SUCCEEDED(hres)) {
                hres = IObjectIdentity_IsEqualObject(identity, r);
                IObjectIdentity_Release(identity);
            }
            V_BOOL(&res) = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
        }
        hres = stack_push(ctx, &res);
1984 1985
    }
    if(r)
1986 1987 1988 1989
        IUnknown_Release(r);
    if(l)
        IUnknown_Release(l);
    return hres;
1990 1991
}

1992 1993
static HRESULT interp_concat(exec_ctx_t *ctx)
{
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarCat(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2014 2015
}

2016 2017
static HRESULT interp_add(exec_ctx_t *ctx)
{
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarAdd(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2038 2039 2040 2041
}

static HRESULT interp_sub(exec_ctx_t *ctx)
{
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarSub(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2062 2063
}

2064 2065
static HRESULT interp_mod(exec_ctx_t *ctx)
{
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarMod(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2086 2087
}

2088 2089
static HRESULT interp_idiv(exec_ctx_t *ctx)
{
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarIdiv(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2110 2111
}

2112 2113
static HRESULT interp_div(exec_ctx_t *ctx)
{
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarDiv(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2134 2135 2136 2137
}

static HRESULT interp_mul(exec_ctx_t *ctx)
{
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarMul(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2158 2159
}

2160 2161
static HRESULT interp_exp(exec_ctx_t *ctx)
{
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
    variant_val_t r, l;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = stack_pop_val(ctx, &r);
    if(FAILED(hres))
        return hres;

    hres = stack_pop_val(ctx, &l);
    if(SUCCEEDED(hres)) {
        hres = VarPow(l.v, r.v, &v);
        release_val(&l);
    }
    release_val(&r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2182 2183
}

2184 2185
static HRESULT interp_neg(exec_ctx_t *ctx)
{
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    variant_val_t val;
    VARIANT v;
    HRESULT hres;

    hres = stack_pop_val(ctx, &val);
    if(FAILED(hres))
        return hres;

    hres = VarNeg(val.v, &v);
    release_val(&val);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
2200 2201
}

2202 2203 2204
static HRESULT interp_incc(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg1.bstr;
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
    VARIANT v;
    ref_t ref;
    HRESULT hres;

    TRACE("\n");

    hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
    if(FAILED(hres))
        return hres;

    if(ref.type != REF_VAR) {
        FIXME("ref.type is not REF_VAR\n");
        return E_FAIL;
    }

    hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
    if(FAILED(hres))
        return hres;

    VariantClear(ref.u.v);
    *ref.u.v = v;
    return S_OK;
2227 2228
}

2229 2230 2231 2232 2233 2234
static HRESULT interp_catch(exec_ctx_t *ctx)
{
    /* Nothing to do here, the OP is for unwinding only. */
    return S_OK;
}

2235
static const instr_func_t op_funcs[] = {
2236
#define X(x,n,a,b) interp_ ## x,
2237 2238 2239 2240 2241
OP_LIST
#undef X
};

static const unsigned op_move[] = {
2242
#define X(x,n,a,b) n,
2243 2244 2245 2246
OP_LIST
#undef X
};

2247
void release_dynamic_var(dynamic_var_t *var)
2248
{
2249 2250 2251
    VariantClear(&var->v);
    if(var->array)
        SafeArrayDestroy(var->array);
2252 2253
}

2254 2255
static void release_exec(exec_ctx_t *ctx)
{
2256
    dynamic_var_t *var;
2257
    unsigned i;
2258

2259
    VariantClear(&ctx->ret_val);
2260 2261 2262

    for(var = ctx->dynamic_vars; var; var = var->next)
        release_dynamic_var(var);
2263

2264 2265
    if(ctx->vbthis)
        IDispatchEx_Release(&ctx->vbthis->IDispatchEx_iface);
2266

2267
    if(ctx->args) {
2268 2269 2270 2271
        for(i=0; i < ctx->func->arg_cnt; i++)
            VariantClear(ctx->args+i);
    }

2272 2273 2274 2275 2276
    if(ctx->vars) {
        for(i=0; i < ctx->func->var_cnt; i++)
            VariantClear(ctx->vars+i);
    }

2277
    if(ctx->arrays) {
2278
        for(i=0; i < ctx->func->array_cnt; i++) {
2279 2280 2281 2282 2283 2284
            if(ctx->arrays[i])
                SafeArrayDestroy(ctx->arrays[i]);
        }
        heap_free(ctx->arrays);
    }

2285
    heap_pool_free(&ctx->heap);
2286
    heap_free(ctx->args);
2287
    heap_free(ctx->vars);
2288 2289 2290
    heap_free(ctx->stack);
}

2291
HRESULT exec_script(script_ctx_t *ctx, BOOL extern_caller, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2292
{
2293
    exec_ctx_t exec = {func->code_ctx};
2294 2295 2296
    vbsop_t op;
    HRESULT hres = S_OK;

2297 2298 2299 2300 2301 2302 2303
    exec.code = func->code_ctx;

    if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
        FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
        return E_FAIL;
    }

2304
    heap_pool_init(&exec.heap);
2305

2306
    TRACE("%s(", debugstr_w(func->name));
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
    if(func->arg_cnt) {
        VARIANT *v;
        unsigned i;

        exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
        if(!exec.args) {
            release_exec(&exec);
            return E_OUTOFMEMORY;
        }

        for(i=0; i < func->arg_cnt; i++) {
            v = get_arg(dp, i);
2319
            TRACE("%s%s", i ? ", " : "", debugstr_variant(v));
2320 2321 2322 2323
            if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
                if(func->args[i].by_ref)
                    exec.args[i] = *v;
                else
2324
                    hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2325
            }else {
2326
                hres = VariantCopyInd(exec.args+i, v);
2327 2328 2329 2330 2331 2332 2333 2334
            }
            if(FAILED(hres)) {
                release_exec(&exec);
                return hres;
            }
        }
    }else {
        exec.args = NULL;
2335
    }
2336
    TRACE(")\n");
2337

2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
    if(func->var_cnt) {
        exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
        if(!exec.vars) {
            release_exec(&exec);
            return E_OUTOFMEMORY;
        }
    }else {
        exec.vars = NULL;
    }

2348 2349 2350
    exec.stack_size = 16;
    exec.top = 0;
    exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2351 2352
    if(!exec.stack) {
        release_exec(&exec);
2353
        return E_OUTOFMEMORY;
2354
    }
2355

2356 2357 2358
    if(extern_caller)
        IActiveScriptSite_OnEnterScript(ctx->site);

2359
    if(vbthis) {
2360
        IDispatchEx_AddRef(&vbthis->IDispatchEx_iface);
2361 2362
        exec.vbthis = vbthis;
    }
2363

2364
    exec.instr = exec.code->instrs + func->code_off;
2365
    exec.script = ctx;
2366
    exec.func = func;
2367 2368 2369 2370 2371

    while(exec.instr) {
        op = exec.instr->op;
        hres = op_funcs[op](&exec);
        if(FAILED(hres)) {
2372 2373 2374 2375
            if(hres != SCRIPT_E_RECORDED) {
                clear_ei(&ctx->ei);

                ctx->ei.scode = hres = map_hres(hres);
2376
                ctx->ei.bstrSource = get_vbscript_string(VBS_RUNTIME_ERROR);
2377 2378 2379 2380
                ctx->ei.bstrDescription = get_vbscript_error_string(hres);
            }else {
                hres = ctx->ei.scode;
            }
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396

            if(exec.resume_next) {
                unsigned stack_off;

                WARN("Failed %08x in resume next mode\n", hres);

                /*
                 * Unwinding here is simple. We need to find the next OP_catch, which contains
                 * information about expected stack size and jump offset on error. Generated
                 * bytecode needs to guarantee, that simple jump and stack adjustment will
                 * guarantee proper execution continuation.
                 */
                while((++exec.instr)->op != OP_catch);

                TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);

2397
                clear_error_loc(ctx);
2398
                stack_off = exec.instr->arg2.uint;
2399
                instr_jmp(&exec, exec.instr->arg1.uint);
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415

                if(exec.top > stack_off) {
                    stack_popn(&exec, exec.top-stack_off);
                }else if(exec.top < stack_off) {
                    VARIANT v;

                    V_VT(&v) = VT_EMPTY;
                    while(exec.top < stack_off) {
                        hres = stack_push(&exec, &v);
                        if(FAILED(hres))
                            break;
                    }
                }

                continue;
            }else {
2416 2417 2418 2419 2420
                if(!ctx->error_loc_code) {
                    grab_vbscode(exec.code);
                    ctx->error_loc_code = exec.code;
                    ctx->error_loc_offset = exec.instr->loc;
                }
2421 2422 2423
                stack_popn(&exec, exec.top);
                break;
            }
2424 2425 2426 2427 2428
        }

        exec.instr += op_move[op];
    }

2429
    assert(!exec.top);
2430

2431 2432 2433 2434
    if(extern_caller) {
        if(FAILED(hres)) {
            if(!ctx->ei.scode)
                ctx->ei.scode = hres;
2435 2436
            hres = report_script_error(ctx, ctx->error_loc_code, ctx->error_loc_offset);
            clear_error_loc(ctx);
2437
        }
2438
        IActiveScriptSite_OnLeaveScript(ctx->site);
2439
    }
2440

2441 2442 2443 2444 2445
    if(SUCCEEDED(hres) && res) {
        *res = exec.ret_val;
        V_VT(&exec.ret_val) = VT_EMPTY;
    }

2446
    release_exec(&exec);
2447 2448
    return hres;
}