interp.c 41.1 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
    IDispatch *this_obj;
35

36
    VARIANT *args;
37
    VARIANT *vars;
38

39 40 41
    dynamic_var_t *dynamic_vars;
    vbsheap_t heap;

42 43
    BOOL resume_next;

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

    VARIANT ret_val;
49 50 51 52
} exec_ctx_t;

typedef HRESULT (*instr_func_t)(exec_ctx_t*);

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

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

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

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

        var = var->next;
    }

    return FALSE;
}

96
static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
97 98
{
    named_item_t *item;
99
    function_t *func;
100
    unsigned i;
101 102 103
    DISPID id;
    HRESULT hres;

104 105
    static const WCHAR errW[] = {'e','r','r',0};

106 107
    if(invoke_type == VBDISP_LET
            && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
108
            && !strcmpiW(name, ctx->func->name)) {
109 110 111 112 113
        ref->type = REF_VAR;
        ref->u.v = &ctx->ret_val;
        return S_OK;
    }

114 115 116 117 118 119 120 121
    for(i=0; i < ctx->func->var_cnt; i++) {
        if(!strcmpiW(ctx->func->vars[i].name, name)) {
            ref->type = REF_VAR;
            ref->u.v = ctx->vars+i;
            return TRUE;
        }
    }

122 123 124 125 126 127 128 129
    for(i=0; i < ctx->func->arg_cnt; i++) {
        if(!strcmpiW(ctx->func->args[i].name, name)) {
            ref->type = REF_VAR;
            ref->u.v = ctx->args+i;
            return S_OK;
        }
    }

130 131 132
    if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
        return S_OK;

133 134 135 136 137 138 139 140
    if(ctx->func->type != FUNC_GLOBAL) {
        hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
        if(SUCCEEDED(hres)) {
            ref->type = REF_DISP;
            ref->u.d.disp = ctx->this_obj;
            ref->u.d.id = id;
            return S_OK;
        }
141 142
    }

143
    if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
144 145
        return S_OK;

146 147 148 149 150 151 152 153
    for(func = ctx->script->global_funcs; func; func = func->next) {
        if(!strcmpiW(func->name, name)) {
            ref->type = REF_FUNC;
            ref->u.f = func;
            return S_OK;
        }
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167
    if(!strcmpiW(name, errW)) {
        ref->type = REF_OBJ;
        ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
        return S_OK;
    }

    hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
    if(SUCCEEDED(hres)) {
        ref->type = REF_DISP;
        ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
        ref->u.d.id = id;
        return S_OK;
    }

168
    LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
            if(!item->disp) {
                IUnknown *unk;

                hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
                if(FAILED(hres)) {
                    WARN("GetItemInfo failed: %08x\n", hres);
                    continue;
                }

                hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
                IUnknown_Release(unk);
                if(FAILED(hres)) {
                    WARN("object does not implement IDispatch\n");
                    continue;
                }
            }

            ref->type = REF_OBJ;
            ref->u.obj = item->disp;
            return S_OK;
        }
191 192
    }

193 194 195 196 197 198 199 200 201 202 203 204
    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;
            }
        }
    }

205 206 207 208
    ref->type = REF_NONE;
    return S_OK;
}

209 210
static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
        BOOL is_const, VARIANT *val, BOOL own_val, VARIANT **out_var)
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
{
    dynamic_var_t *new_var;
    vbsheap_t *heap;
    WCHAR *str;
    unsigned size;
    HRESULT hres;

    heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;

    new_var = vbsheap_alloc(heap, sizeof(*new_var));
    if(!new_var)
        return E_OUTOFMEMORY;

    size = (strlenW(name)+1)*sizeof(WCHAR);
    str = vbsheap_alloc(heap, size);
    if(!str)
        return E_OUTOFMEMORY;
    memcpy(str, name, size);
    new_var->name = str;
230
    new_var->is_const = is_const;
231 232 233 234

    if(own_val) {
        new_var->v = *val;
    }else {
235
        V_VT(&new_var->v) = VT_EMPTY;
236 237 238 239 240 241 242 243 244 245 246 247 248
        hres = VariantCopy(&new_var->v, val);
        if(FAILED(hres))
            return hres;
    }

    if(ctx->func->type == FUNC_GLOBAL) {
        new_var->next = ctx->script->global_vars;
        ctx->script->global_vars = new_var;
    }else {
        new_var->next = ctx->dynamic_vars;
        ctx->dynamic_vars = new_var;
    }

249 250 251
    if(out_var)
        *out_var = &new_var->v;

252 253 254
    return S_OK;
}

255 256 257 258 259 260
static inline VARIANT *stack_pop(exec_ctx_t *ctx)
{
    assert(ctx->top);
    return ctx->stack + --ctx->top;
}

261 262 263 264 265 266
static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
{
    assert(ctx->top >= n);
    return ctx->stack + (ctx->top-n-1);
}

267 268 269 270 271
static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
{
    if(ctx->stack_size == ctx->top) {
        VARIANT *new_stack;

272
        new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
273 274 275 276 277 278 279 280 281 282 283 284 285
        if(!new_stack) {
            VariantClear(v);
            return E_OUTOFMEMORY;
        }

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

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

286 287 288 289 290 291 292
static inline HRESULT stack_push_null(exec_ctx_t *ctx)
{
    VARIANT v;
    V_VT(&v) = VT_NULL;
    return stack_push(ctx, &v);
}

293 294 295 296 297 298
static void stack_popn(exec_ctx_t *ctx, unsigned n)
{
    while(n--)
        VariantClear(stack_pop(ctx));
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312
static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
{
    VARIANT *var;

    var = stack_pop(ctx);

    if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
        v->owned = FALSE;
        var = V_VARIANTREF(var);
    }else {
        v->owned = TRUE;
    }

    if(V_VT(var) == VT_DISPATCH) {
313 314 315 316 317 318 319 320 321 322 323
        DISPPARAMS dp = {0};
        HRESULT hres;

        hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
        if(v->owned)
            IDispatch_Release(V_DISPATCH(var));
        if(FAILED(hres))
            return hres;

        v->owned = TRUE;
        v->v = &v->store;
324 325 326 327 328 329 330
    }else {
        v->v = var;
    }

    return S_OK;
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
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) {
        DISPPARAMS dp = {0};
        IDispatch *disp;

        disp = V_DISPATCH(v);
        V_VT(v) = VT_EMPTY;
        hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
        IDispatch_Release(disp);
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}

360 361 362 363 364 365
static inline void release_val(variant_val_t *v)
{
    if(v->owned)
        VariantClear(v->v);
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379
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;
380 381 382
    case VT_NULL:
        *b = FALSE;
        break;
383 384 385 386 387 388 389 390 391 392 393 394 395 396
    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;
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
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;
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
{
    VARIANT *v = stack_top(ctx, n), *ref;

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

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

        V_VT(v) = VT_DISPATCH;
        V_DISPATCH(v) = V_DISPATCH(ref);
        if(V_DISPATCH(v))
            IDispatch_AddRef(V_DISPATCH(v));
    }

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

451 452 453 454 455
static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
{
    ctx->instr = ctx->code->instrs + addr;
}

456
static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
457
{
458 459 460
    dp->cNamedArgs = is_propput ? 1 : 0;
    dp->cArgs = arg_cnt + dp->cNamedArgs;
    dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
461 462 463 464 465 466 467 468 469 470 471 472 473

    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;
        }

474
        dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
475
    }else {
476
        dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
477 478 479
    }
}

480
static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
481
{
482
    BSTR identifier = ctx->instr->arg1.bstr;
483
    const unsigned arg_cnt = ctx->instr->arg2.uint;
484
    DISPPARAMS dp;
485
    ref_t ref;
486 487
    HRESULT hres;

488
    hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
489 490 491
    if(FAILED(hres))
        return hres;

492
    vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
493

494
    switch(ref.type) {
495
    case REF_VAR:
496
    case REF_CONST:
497 498 499 500 501 502 503 504 505 506 507 508 509
        if(!res) {
            FIXME("REF_VAR no res\n");
            return E_NOTIMPL;
        }

        if(arg_cnt) {
            FIXME("arguments not implemented\n");
            return E_NOTIMPL;
        }

        V_VT(res) = VT_BYREF|VT_VARIANT;
        V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
        break;
510
    case REF_DISP:
511
        hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
512 513 514
        if(FAILED(hres))
            return hres;
        break;
515
    case REF_FUNC:
516
        hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
517 518 519
        if(FAILED(hres))
            return hres;
        break;
520 521 522 523 524 525 526 527 528 529 530 531
    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;
532
    case REF_NONE:
533 534 535 536 537 538 539 540 541 542
        if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
            VARIANT v, *new;
            VariantInit(&v);
            hres = add_dynamic_var(ctx, identifier, FALSE, &v, FALSE, &new);
            if(FAILED(hres))
                return hres;
            V_VT(res) = VT_BYREF|VT_VARIANT;
            V_BYREF(res) = new;
            break;
        }
543 544 545 546
        FIXME("%s not found\n", debugstr_w(identifier));
        return DISP_E_UNKNOWNNAME;
    }

547
    stack_popn(ctx, arg_cnt);
548
    return S_OK;
549 550
}

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
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);
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
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;
    }

589
    vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
590

591
    hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    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)
{
618 619 620
    TRACE("\n");

    return do_mcall(ctx, NULL);
621 622
}

623
static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
624 625 626 627
{
    ref_t ref;
    HRESULT hres;

628
    hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
629 630 631 632
    if(FAILED(hres))
        return hres;

    switch(ref.type) {
633 634 635
    case REF_VAR: {
        VARIANT *v = ref.u.v;

636 637 638 639 640
        if(arg_cnt(dp)) {
            FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
            return E_NOTIMPL;
        }

641 642 643
        if(V_VT(v) == (VT_VARIANT|VT_BYREF))
            v = V_VARIANTREF(v);

644
        hres = VariantCopy(v, dp->rgvarg);
645
        break;
646
    }
647
    case REF_DISP:
648
        hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
649
        break;
650 651 652
    case REF_FUNC:
        FIXME("functions not implemented\n");
        return E_NOTIMPL;
653 654 655
    case REF_OBJ:
        FIXME("REF_OBJ\n");
        return E_NOTIMPL;
656 657 658
    case REF_CONST:
        FIXME("REF_CONST\n");
        return E_NOTIMPL;
659
    case REF_NONE:
660 661
        if(ctx->func->code_ctx->option_explicit) {
            FIXME("throw exception\n");
662
            hres = E_FAIL;
663
        }else {
664 665 666 667 668
            if(arg_cnt(dp)) {
                FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
                return E_NOTIMPL;
            }

669
            TRACE("creating variable %s\n", debugstr_w(name));
670
            hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE, NULL);
671
        }
672 673 674 675 676
    }

    return hres;
}

677 678
static HRESULT interp_assign_ident(exec_ctx_t *ctx)
{
679
    const BSTR arg = ctx->instr->arg1.bstr;
680
    const unsigned arg_cnt = ctx->instr->arg2.uint;
681
    DISPPARAMS dp;
682 683 684 685
    HRESULT hres;

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

686 687 688
    hres = stack_assume_val(ctx, arg_cnt);
    if(FAILED(hres))
        return hres;
689

690 691
    vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
    hres = assign_ident(ctx, arg, &dp);
692 693 694
    if(FAILED(hres))
        return hres;

695 696
    stack_popn(ctx, arg_cnt+1);
    return S_OK;
697 698
}

699 700 701
static HRESULT interp_set_ident(exec_ctx_t *ctx)
{
    const BSTR arg = ctx->instr->arg1.bstr;
702
    const unsigned arg_cnt = ctx->instr->arg2.uint;
703
    DISPPARAMS dp;
704 705 706 707
    HRESULT hres;

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

708 709 710 711 712
    if(arg_cnt) {
        FIXME("arguments not supported\n");
        return E_NOTIMPL;
    }

713 714 715 716 717 718
    hres = stack_assume_disp(ctx, 0, NULL);
    if(FAILED(hres))
        return hres;

    vbstack_to_dp(ctx, 0, TRUE, &dp);
    hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
719 720 721
    if(FAILED(hres))
        return hres;

722 723
    stack_popn(ctx, 1);
    return S_OK;
724 725
}

726 727
static HRESULT interp_assign_member(exec_ctx_t *ctx)
{
728
    BSTR identifier = ctx->instr->arg1.bstr;
729
    const unsigned arg_cnt = ctx->instr->arg2.uint;
730
    IDispatch *obj;
731
    DISPPARAMS dp;
732 733 734 735 736
    DISPID id;
    HRESULT hres;

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

737
    hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
738 739 740 741 742 743 744 745
    if(FAILED(hres))
        return hres;

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

746
    hres = stack_assume_val(ctx, arg_cnt);
747
    if(FAILED(hres))
748 749
        return hres;

750
    hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
751 752 753 754
    if(SUCCEEDED(hres)) {
        vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
        hres = disp_propput(ctx->script, obj, id, &dp);
    }
755 756 757
    if(FAILED(hres))
        return hres;

758
    stack_popn(ctx, arg_cnt+2);
759
    return S_OK;
760 761
}

762 763 764
static HRESULT interp_set_member(exec_ctx_t *ctx)
{
    BSTR identifier = ctx->instr->arg1.bstr;
765
    const unsigned arg_cnt = ctx->instr->arg2.uint;
766
    IDispatch *obj;
767
    DISPPARAMS dp;
768 769 770 771 772
    DISPID id;
    HRESULT hres;

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

773 774 775 776 777
    if(arg_cnt) {
        FIXME("arguments not supported\n");
        return E_NOTIMPL;
    }

778
    hres = stack_assume_disp(ctx, 1, &obj);
779 780 781 782 783 784 785 786
    if(FAILED(hres))
        return hres;

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

787 788
    hres = stack_assume_disp(ctx, 0, NULL);
    if(FAILED(hres))
789 790 791
        return hres;

    hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
792 793 794 795
    if(SUCCEEDED(hres)) {
        vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
        hres = disp_propput(ctx->script, obj, id, &dp);
    }
796 797
    if(FAILED(hres))
        return hres;
798

799 800
    stack_popn(ctx, 2);
    return S_OK;
801 802
}

803 804 805
static HRESULT interp_const(exec_ctx_t *ctx)
{
    BSTR arg = ctx->instr->arg1.bstr;
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    variant_val_t val;
    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;
    }

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

827
    return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned, NULL);
828 829
}

830 831
static HRESULT interp_val(exec_ctx_t *ctx)
{
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    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);
850 851 852 853 854 855 856 857 858 859 860 861
}

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;
}

862 863 864
static HRESULT interp_new(exec_ctx_t *ctx)
{
    const WCHAR *arg = ctx->instr->arg1.bstr;
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    class_desc_t *class_desc;
    vbdisp_t *obj;
    VARIANT v;
    HRESULT hres;

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

    for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
        if(!strcmpiW(class_desc->name, arg))
            break;
    }
    if(!class_desc) {
        FIXME("Class %s not found\n", debugstr_w(arg));
        return E_FAIL;
    }

881
    hres = create_vbdisp(class_desc, &obj);
882 883 884 885 886 887
    if(FAILED(hres))
        return hres;

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

890 891 892
static HRESULT interp_step(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg2.bstr;
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    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;

921
    if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
922
        ctx->instr++;
923 924
    }else {
        stack_popn(ctx, 2);
925
        instr_jmp(ctx, ctx->instr->arg1.uint);
926
    }
927
    return S_OK;
928 929
}

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
static HRESULT interp_newenum(exec_ctx_t *ctx)
{
    VARIANT *v, r;
    HRESULT hres;

    TRACE("\n");

    v = stack_pop(ctx);
    switch(V_VT(v)) {
    case VT_DISPATCH: {
        IEnumVARIANT *iter;
        DISPPARAMS dp = {0};
        VARIANT iterv;

        hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_NEWENUM, &dp, &iterv);
        VariantClear(v);
        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;
        }

        V_VT(&r) = VT_UNKNOWN;
        V_UNKNOWN(&r) = (IUnknown*)iter;
        break;
    }
    default:
        FIXME("Unsupported for %s\n", debugstr_variant(v));
        VariantClear(v);
        return E_NOTIMPL;
    }

    return stack_push(ctx, &r);
}

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");

    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;
    hres = assign_ident(ctx, ident, &dp);
    VariantClear(&v);
    if(FAILED(hres))
        return hres;

    if(do_continue) {
        ctx->instr++;
    }else {
        stack_pop(ctx);
        instr_jmp(ctx, loop_end);
    }
    return S_OK;
}

1010 1011
static HRESULT interp_jmp(exec_ctx_t *ctx)
{
1012 1013 1014 1015 1016 1017
    const unsigned arg = ctx->instr->arg1.uint;

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

    instr_jmp(ctx, arg);
    return S_OK;
1018 1019 1020 1021
}

static HRESULT interp_jmp_false(exec_ctx_t *ctx)
{
1022 1023
    const unsigned arg = ctx->instr->arg1.uint;
    HRESULT hres;
1024
    BOOL b;
1025 1026 1027

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

1028
    hres = stack_pop_bool(ctx, &b);
1029 1030 1031
    if(FAILED(hres))
        return hres;

1032
    if(b)
1033 1034 1035 1036
        ctx->instr++;
    else
        instr_jmp(ctx, ctx->instr->arg1.uint);
    return S_OK;
1037 1038
}

1039 1040 1041 1042
static HRESULT interp_jmp_true(exec_ctx_t *ctx)
{
    const unsigned arg = ctx->instr->arg1.uint;
    HRESULT hres;
1043
    BOOL b;
1044 1045 1046

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

1047
    hres = stack_pop_bool(ctx, &b);
1048 1049 1050
    if(FAILED(hres))
        return hres;

1051
    if(b)
1052 1053 1054 1055 1056 1057
        instr_jmp(ctx, ctx->instr->arg1.uint);
    else
        ctx->instr++;
    return S_OK;
}

1058 1059 1060 1061 1062 1063 1064 1065
static HRESULT interp_ret(exec_ctx_t *ctx)
{
    TRACE("\n");

    ctx->instr = NULL;
    return S_OK;
}

1066 1067 1068 1069 1070 1071 1072 1073
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;
}

1074 1075
static HRESULT interp_me(exec_ctx_t *ctx)
{
1076 1077 1078 1079 1080 1081 1082 1083
    VARIANT v;

    TRACE("\n");

    IDispatch_AddRef(ctx->this_obj);
    V_VT(&v) = VT_DISPATCH;
    V_DISPATCH(&v) = ctx->this_obj;
    return stack_push(ctx, &v);
1084 1085
}

1086 1087
static HRESULT interp_bool(exec_ctx_t *ctx)
{
1088 1089 1090 1091 1092 1093 1094 1095
    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);
1096 1097
}

1098 1099 1100
static HRESULT interp_errmode(exec_ctx_t *ctx)
{
    const int err_mode = ctx->instr->arg1.uint;
1101 1102 1103 1104 1105

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

    ctx->resume_next = err_mode;
    return S_OK;
1106 1107
}

1108 1109
static HRESULT interp_string(exec_ctx_t *ctx)
{
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    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);
1120 1121
}

1122 1123
static HRESULT interp_long(exec_ctx_t *ctx)
{
1124 1125 1126 1127 1128 1129 1130 1131
    const LONG arg = ctx->instr->arg1.lng;
    VARIANT v;

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

    V_VT(&v) = VT_I4;
    V_I4(&v) = arg;
    return stack_push(ctx, &v);
1132 1133 1134 1135
}

static HRESULT interp_short(exec_ctx_t *ctx)
{
1136 1137 1138 1139 1140 1141 1142 1143
    const LONG arg = ctx->instr->arg1.lng;
    VARIANT v;

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

    V_VT(&v) = VT_I2;
    V_I2(&v) = arg;
    return stack_push(ctx, &v);
1144 1145 1146 1147
}

static HRESULT interp_double(exec_ctx_t *ctx)
{
1148 1149 1150 1151 1152 1153 1154 1155
    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);
1156 1157
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
static HRESULT interp_empty(exec_ctx_t *ctx)
{
    VARIANT v;

    TRACE("\n");

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

1168 1169 1170
static HRESULT interp_null(exec_ctx_t *ctx)
{
    TRACE("\n");
1171
    return stack_push_null(ctx);
1172 1173
}

1174 1175
static HRESULT interp_nothing(exec_ctx_t *ctx)
{
1176 1177 1178 1179 1180 1181 1182
    VARIANT v;

    TRACE("\n");

    V_VT(&v) = VT_DISPATCH;
    V_DISPATCH(&v) = NULL;
    return stack_push(ctx, &v);
1183 1184
}

1185 1186
static HRESULT interp_not(exec_ctx_t *ctx)
{
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    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);
1203 1204
}

1205 1206
static HRESULT interp_and(exec_ctx_t *ctx)
{
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    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);
1227 1228
}

1229 1230
static HRESULT interp_or(exec_ctx_t *ctx)
{
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    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);
1251 1252
}

1253 1254
static HRESULT interp_xor(exec_ctx_t *ctx)
{
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    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);
1275 1276 1277 1278
}

static HRESULT interp_eqv(exec_ctx_t *ctx)
{
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
    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);
1299 1300 1301 1302
}

static HRESULT interp_imp(exec_ctx_t *ctx)
{
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    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);
1323 1324
}

1325 1326 1327 1328 1329 1330 1331 1332 1333
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);
 }

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
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);
1344
    if(SUCCEEDED(hres)) {
1345 1346
        hres = var_cmp(ctx, l.v, r.v);
        release_val(&l);
1347
    }
1348 1349 1350 1351 1352

    release_val(&r);
    return hres;
}

1353 1354
static HRESULT interp_equal(exec_ctx_t *ctx)
{
1355 1356 1357 1358 1359 1360 1361 1362
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1363 1364
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1365 1366 1367 1368

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

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
static HRESULT interp_nequal(exec_ctx_t *ctx)
{
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1381 1382
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1383 1384 1385 1386 1387 1388

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

1389 1390
static HRESULT interp_gt(exec_ctx_t *ctx)
{
1391 1392 1393 1394 1395 1396 1397 1398
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1399 1400
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1401 1402 1403 1404

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1405 1406 1407 1408
}

static HRESULT interp_gteq(exec_ctx_t *ctx)
{
1409 1410 1411 1412 1413 1414 1415 1416
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1417 1418
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1419 1420 1421 1422

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1423 1424 1425 1426
}

static HRESULT interp_lt(exec_ctx_t *ctx)
{
1427 1428 1429 1430 1431 1432 1433 1434
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1435 1436
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1437 1438 1439 1440

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1441 1442 1443 1444
}

static HRESULT interp_lteq(exec_ctx_t *ctx)
{
1445 1446 1447 1448 1449 1450 1451 1452
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1453 1454
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1455 1456 1457 1458

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

1461 1462
static HRESULT interp_case(exec_ctx_t *ctx)
{
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
    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;
1486 1487
}

1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
{
    IObjectIdentity *identity;
    IUnknown *unk1, *unk2;
    HRESULT hres;

    if(disp1 == disp2) {
        *ret = VARIANT_TRUE;
        return S_OK;
    }

    if(!disp1 || !disp2) {
        *ret = VARIANT_FALSE;
        return S_OK;
    }

    hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
    if(FAILED(hres))
        return hres;

    hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
    if(FAILED(hres)) {
        IUnknown_Release(unk1);
        return hres;
    }

    if(unk1 == unk2) {
        *ret = VARIANT_TRUE;
    }else {
        hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
        if(SUCCEEDED(hres)) {
            hres = IObjectIdentity_IsEqualObject(identity, unk2);
            IObjectIdentity_Release(identity);
            *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
        }else {
            *ret = VARIANT_FALSE;
        }
    }

    IUnknown_Release(unk1);
    IUnknown_Release(unk2);
    return S_OK;
}

static HRESULT interp_is(exec_ctx_t *ctx)
{
    IDispatch *l, *r;
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

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

    hres = stack_pop_disp(ctx, &l);
    if(SUCCEEDED(hres)) {
        V_VT(&v) = VT_BOOL;
        hres = disp_cmp(l, r, &V_BOOL(&v));
        if(l)
            IDispatch_Release(l);
    }
    if(r)
        IDispatch_Release(r);
    if(FAILED(hres))
        return hres;

    return stack_push(ctx, &v);
}

1559 1560
static HRESULT interp_concat(exec_ctx_t *ctx)
{
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
    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);
1581 1582
}

1583 1584
static HRESULT interp_add(exec_ctx_t *ctx)
{
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
    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);
1605 1606 1607 1608
}

static HRESULT interp_sub(exec_ctx_t *ctx)
{
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
    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);
1629 1630
}

1631 1632
static HRESULT interp_mod(exec_ctx_t *ctx)
{
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
    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);
1653 1654
}

1655 1656
static HRESULT interp_idiv(exec_ctx_t *ctx)
{
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
    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);
1677 1678
}

1679 1680
static HRESULT interp_div(exec_ctx_t *ctx)
{
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
    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);
1701 1702 1703 1704
}

static HRESULT interp_mul(exec_ctx_t *ctx)
{
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
    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);
1725 1726
}

1727 1728
static HRESULT interp_exp(exec_ctx_t *ctx)
{
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
    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);
1749 1750
}

1751 1752
static HRESULT interp_neg(exec_ctx_t *ctx)
{
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
    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);
1767 1768
}

1769 1770 1771
static HRESULT interp_incc(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg1.bstr;
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
    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;
1794 1795
}

1796
static const instr_func_t op_funcs[] = {
1797
#define X(x,n,a,b) interp_ ## x,
1798 1799 1800 1801 1802
OP_LIST
#undef X
};

static const unsigned op_move[] = {
1803
#define X(x,n,a,b) n,
1804 1805 1806 1807
OP_LIST
#undef X
};

1808 1809 1810 1811 1812 1813 1814 1815
void release_dynamic_vars(dynamic_var_t *var)
{
    while(var) {
        VariantClear(&var->v);
        var = var->next;
    }
}

1816 1817
static void release_exec(exec_ctx_t *ctx)
{
1818
    unsigned i;
1819

1820
    VariantClear(&ctx->ret_val);
1821
    release_dynamic_vars(ctx->dynamic_vars);
1822

1823 1824 1825
    if(ctx->this_obj)
        IDispatch_Release(ctx->this_obj);

1826
    if(ctx->args) {
1827 1828 1829 1830
        for(i=0; i < ctx->func->arg_cnt; i++)
            VariantClear(ctx->args+i);
    }

1831 1832 1833 1834 1835
    if(ctx->vars) {
        for(i=0; i < ctx->func->var_cnt; i++)
            VariantClear(ctx->vars+i);
    }

1836
    vbsheap_free(&ctx->heap);
1837
    heap_free(ctx->args);
1838
    heap_free(ctx->vars);
1839 1840 1841
    heap_free(ctx->stack);
}

1842
HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1843
{
1844
    exec_ctx_t exec = {func->code_ctx};
1845 1846 1847
    vbsop_t op;
    HRESULT hres = S_OK;

1848 1849 1850 1851 1852 1853 1854
    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;
    }

1855 1856
    vbsheap_init(&exec.heap);

1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
    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);
            if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
                if(func->args[i].by_ref)
                    exec.args[i] = *v;
                else
                    hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
            }else {
                hres = VariantCopy(exec.args+i, v);
            }
            if(FAILED(hres)) {
                release_exec(&exec);
                return hres;
            }
        }
    }else {
        exec.args = NULL;
1884 1885
    }

1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
    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;
    }

1896 1897 1898
    exec.stack_size = 16;
    exec.top = 0;
    exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1899 1900
    if(!exec.stack) {
        release_exec(&exec);
1901
        return E_OUTOFMEMORY;
1902
    }
1903

1904 1905 1906 1907 1908 1909 1910 1911
    if(this_obj)
        exec.this_obj = this_obj;
    else if (ctx->host_global)
        exec.this_obj = ctx->host_global;
    else
        exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
    IDispatch_AddRef(exec.this_obj);

1912
    exec.instr = exec.code->instrs + func->code_off;
1913
    exec.script = ctx;
1914
    exec.func = func;
1915 1916 1917 1918 1919

    while(exec.instr) {
        op = exec.instr->op;
        hres = op_funcs[op](&exec);
        if(FAILED(hres)) {
1920 1921 1922 1923
            if(exec.resume_next)
                FIXME("Failed %08x in resume next mode\n", hres);
            else
                WARN("Failed %08x\n", hres);
1924
            stack_popn(&exec, exec.top);
1925 1926 1927 1928 1929 1930
            break;
        }

        exec.instr += op_move[op];
    }

1931
    assert(!exec.top);
1932
    if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1933 1934 1935 1936 1937 1938 1939
        assert(V_VT(&exec.ret_val) == VT_EMPTY);

    if(SUCCEEDED(hres) && res) {
        *res = exec.ret_val;
        V_VT(&exec.ret_val) = VT_EMPTY;
    }

1940
    release_exec(&exec);
1941 1942
    return hres;
}