interp.c 48.3 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
    vbdisp_t *vbthis;
36

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

41
    dynamic_var_t *dynamic_vars;
42
    heap_pool_t heap;
43

44 45
    BOOL resume_next;

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

    VARIANT ret_val;
51 52 53 54
} exec_ctx_t;

typedef HRESULT (*instr_func_t)(exec_ctx_t*);

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

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

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

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

        var = var->next;
    }

    return FALSE;
}

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

106 107
    static const WCHAR errW[] = {'e','r','r',0};

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

116 117 118 119 120 121 122 123
    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;
        }
    }

124 125 126 127 128 129 130 131
    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;
        }
    }

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

135
    if(ctx->func->type != FUNC_GLOBAL) {
136 137 138 139 140 141 142 143 144 145 146
        if(ctx->vbthis) {
            /* FIXME: Bind such identifier while generating bytecode. */
            for(i=0; i < ctx->vbthis->desc->prop_cnt; i++) {
                if(!strcmpiW(ctx->vbthis->desc->props[i].name, name)) {
                    ref->type = REF_VAR;
                    ref->u.v = ctx->vbthis->props+i;
                    return S_OK;
                }
            }
        }

147 148 149 150 151 152 153
        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;
        }
154 155
    }

156
    if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
157 158
        return S_OK;

159 160 161 162 163 164 165 166
    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;
        }
    }

167 168 169 170 171 172 173 174 175 176 177 178 179 180
    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;
    }

181
    LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        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;
        }
204 205
    }

206 207 208 209 210 211 212 213 214 215 216 217
    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;
            }
        }
    }

218 219 220 221
    ref->type = REF_NONE;
    return S_OK;
}

222
static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
223
        BOOL is_const, VARIANT **out_var)
224 225
{
    dynamic_var_t *new_var;
226
    heap_pool_t *heap;
227 228 229 230 231
    WCHAR *str;
    unsigned size;

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

232
    new_var = heap_pool_alloc(heap, sizeof(*new_var));
233 234 235 236
    if(!new_var)
        return E_OUTOFMEMORY;

    size = (strlenW(name)+1)*sizeof(WCHAR);
237
    str = heap_pool_alloc(heap, size);
238 239 240 241
    if(!str)
        return E_OUTOFMEMORY;
    memcpy(str, name, size);
    new_var->name = str;
242
    new_var->is_const = is_const;
243
    V_VT(&new_var->v) = VT_EMPTY;
244 245 246 247 248 249 250 251 252

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

253
    *out_var = &new_var->v;
254 255 256
    return S_OK;
}

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

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

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

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

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

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

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

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

301
static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
302
{
303
    VARIANT *v;
304

305 306 307 308
    v = stack_pop(ctx);
    if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
        r->owned = FALSE;
        r->v = V_VARIANTREF(v);
309
    }else {
310 311
        r->owned = TRUE;
        r->v = v;
312
    }
313 314 315 316 317 318 319 320 321 322 323
}

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

325
    if(V_VT(r->v) == VT_DISPATCH) {
326 327
        HRESULT hres;

328
        hres = get_disp_value(ctx->script, V_DISPATCH(r->v), &r->store);
329 330
        if(r->owned)
            IDispatch_Release(V_DISPATCH(r->v));
331 332 333
        if(FAILED(hres))
            return hres;

334 335
        r->owned = TRUE;
        r->v = &r->store;
336 337 338 339 340
    }

    return S_OK;
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
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);
359
        hres = get_disp_value(ctx->script, disp, v);
360 361 362 363 364 365 366 367
        IDispatch_Release(disp);
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}

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

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

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

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

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

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

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

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
{
    unsigned cell_off = 0, dim_size = 1, i;
    unsigned argc = arg_cnt(dp);
    VARIANT *data;
    LONG idx;
    HRESULT hres;

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

    if(array->cDims != argc) {
        FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
        return E_FAIL;
    }

    for(i=0; i < argc; i++) {
        hres = to_int(get_arg(dp, i), &idx);
        if(FAILED(hres))
            return hres;

        idx -= array->rgsabound[i].lLbound;
        if(idx >= array->rgsabound[i].cElements) {
            FIXME("out of bound element %d in dim %d of size %d\n", idx, i+1, array->rgsabound[i].cElements);
            return E_FAIL;
        }

        cell_off += idx*dim_size;
        dim_size *= array->rgsabound[i].cElements;
    }

    hres = SafeArrayAccessData(array, (void**)&data);
    if(FAILED(hres))
        return hres;

    *ret = data+cell_off;

    SafeArrayUnaccessData(array);
    return S_OK;
}

525
static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
526
{
527
    BSTR identifier = ctx->instr->arg1.bstr;
528
    const unsigned arg_cnt = ctx->instr->arg2.uint;
529
    DISPPARAMS dp;
530
    ref_t ref;
531 532
    HRESULT hres;

533
    hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
534 535 536 537
    if(FAILED(hres))
        return hres;

    switch(ref.type) {
538
    case REF_VAR:
539 540 541
    case REF_CONST: {
        VARIANT *v;

542 543 544 545 546
        if(!res) {
            FIXME("REF_VAR no res\n");
            return E_NOTIMPL;
        }

547 548
        v = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;

549
        if(arg_cnt) {
550
            SAFEARRAY *array = NULL;
551 552 553 554 555 556 557 558

            switch(V_VT(v)) {
            case VT_ARRAY|VT_BYREF|VT_VARIANT:
                array = *V_ARRAYREF(ref.u.v);
                break;
            case VT_ARRAY|VT_VARIANT:
                array = V_ARRAY(ref.u.v);
                break;
559 560 561 562 563 564
            case VT_DISPATCH:
                vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
                hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
                if(FAILED(hres))
                    return hres;
                break;
565 566 567 568 569
            default:
                FIXME("arguments not implemented\n");
                return E_NOTIMPL;
            }

570 571 572
            if(!array)
                break;

573 574 575 576
            vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
            hres = array_access(ctx, array, &dp, &v);
            if(FAILED(hres))
                return hres;
577 578 579
        }

        V_VT(res) = VT_BYREF|VT_VARIANT;
580
        V_BYREF(res) = v;
581
        break;
582
    }
583
    case REF_DISP:
584
        vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
585
        hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
586 587 588
        if(FAILED(hres))
            return hres;
        break;
589
    case REF_FUNC:
590
        vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
591
        hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
592 593 594
        if(FAILED(hres))
            return hres;
        break;
595 596 597 598 599 600 601 602 603 604 605 606
    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;
607
    case REF_NONE:
608
        if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
609 610
            VARIANT *new;
            hres = add_dynamic_var(ctx, identifier, FALSE, &new);
611 612 613 614 615 616
            if(FAILED(hres))
                return hres;
            V_VT(res) = VT_BYREF|VT_VARIANT;
            V_BYREF(res) = new;
            break;
        }
617 618 619 620
        FIXME("%s not found\n", debugstr_w(identifier));
        return DISP_E_UNKNOWNNAME;
    }

621
    stack_popn(ctx, arg_cnt);
622
    return S_OK;
623 624
}

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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);
}

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
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;
    }

663
    vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
664

665
    hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    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)
{
692 693 694
    TRACE("\n");

    return do_mcall(ctx, NULL);
695 696
}

697 698 699 700 701 702 703 704 705 706 707
static HRESULT assign_value(exec_ctx_t *ctx, VARIANT *dst, VARIANT *src, WORD flags)
{
    HRESULT hres;

    hres = VariantCopyInd(dst, src);
    if(FAILED(hres))
        return hres;

    if(V_VT(dst) == VT_DISPATCH && !(flags & DISPATCH_PROPERTYPUTREF)) {
        VARIANT value;

708
        hres = get_disp_value(ctx->script, V_DISPATCH(dst), &value);
709 710 711 712 713 714 715 716 717 718
        IDispatch_Release(V_DISPATCH(dst));
        if(FAILED(hres))
            return hres;

        *dst = value;
    }

    return S_OK;
}

719
static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, WORD flags, DISPPARAMS *dp)
720 721 722 723
{
    ref_t ref;
    HRESULT hres;

724
    hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
725 726 727 728
    if(FAILED(hres))
        return hres;

    switch(ref.type) {
729 730 731
    case REF_VAR: {
        VARIANT *v = ref.u.v;

732 733 734
        if(V_VT(v) == (VT_VARIANT|VT_BYREF))
            v = V_VARIANTREF(v);

735
        if(arg_cnt(dp)) {
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
            SAFEARRAY *array;

            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:
751
                FIXME("Unsupported array type %x\n", V_VT(v));
752 753 754 755 756 757 758 759 760 761 762 763 764
                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");
765 766 767
            return E_NOTIMPL;
        }

768
        hres = assign_value(ctx, v, dp->rgvarg, flags);
769
        break;
770
    }
771
    case REF_DISP:
772
        hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, flags, dp);
773
        break;
774 775 776
    case REF_FUNC:
        FIXME("functions not implemented\n");
        return E_NOTIMPL;
777 778 779
    case REF_OBJ:
        FIXME("REF_OBJ\n");
        return E_NOTIMPL;
780 781 782
    case REF_CONST:
        FIXME("REF_CONST\n");
        return E_NOTIMPL;
783
    case REF_NONE:
784 785
        if(ctx->func->code_ctx->option_explicit) {
            FIXME("throw exception\n");
786
            hres = E_FAIL;
787
        }else {
788 789
            VARIANT *new_var;

790 791 792 793 794
            if(arg_cnt(dp)) {
                FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
                return E_NOTIMPL;
            }

795
            TRACE("creating variable %s\n", debugstr_w(name));
796 797
            hres = add_dynamic_var(ctx, name, FALSE, &new_var);
            if(SUCCEEDED(hres))
798
                hres = assign_value(ctx, new_var, dp->rgvarg, flags);
799
        }
800 801 802 803 804
    }

    return hres;
}

805 806
static HRESULT interp_assign_ident(exec_ctx_t *ctx)
{
807
    const BSTR arg = ctx->instr->arg1.bstr;
808
    const unsigned arg_cnt = ctx->instr->arg2.uint;
809
    DISPPARAMS dp;
810 811 812 813
    HRESULT hres;

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

814
    vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
815
    hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUT, &dp);
816 817 818
    if(FAILED(hres))
        return hres;

819 820
    stack_popn(ctx, arg_cnt+1);
    return S_OK;
821 822
}

823 824 825
static HRESULT interp_set_ident(exec_ctx_t *ctx)
{
    const BSTR arg = ctx->instr->arg1.bstr;
826
    const unsigned arg_cnt = ctx->instr->arg2.uint;
827
    DISPPARAMS dp;
828 829 830 831
    HRESULT hres;

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

832 833 834 835 836
    if(arg_cnt) {
        FIXME("arguments not supported\n");
        return E_NOTIMPL;
    }

837 838 839 840 841
    hres = stack_assume_disp(ctx, 0, NULL);
    if(FAILED(hres))
        return hres;

    vbstack_to_dp(ctx, 0, TRUE, &dp);
842
    hres = assign_ident(ctx, ctx->instr->arg1.bstr, DISPATCH_PROPERTYPUTREF, &dp);
843 844 845
    if(FAILED(hres))
        return hres;

846 847
    stack_popn(ctx, 1);
    return S_OK;
848 849
}

850 851
static HRESULT interp_assign_member(exec_ctx_t *ctx)
{
852
    BSTR identifier = ctx->instr->arg1.bstr;
853
    const unsigned arg_cnt = ctx->instr->arg2.uint;
854
    IDispatch *obj;
855
    DISPPARAMS dp;
856 857 858 859 860
    DISPID id;
    HRESULT hres;

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

861
    hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
862 863 864 865 866 867 868 869
    if(FAILED(hres))
        return hres;

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

870
    hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
871 872
    if(SUCCEEDED(hres)) {
        vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
873
        hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUT, &dp);
874
    }
875 876 877
    if(FAILED(hres))
        return hres;

878
    stack_popn(ctx, arg_cnt+2);
879
    return S_OK;
880 881
}

882 883 884
static HRESULT interp_set_member(exec_ctx_t *ctx)
{
    BSTR identifier = ctx->instr->arg1.bstr;
885
    const unsigned arg_cnt = ctx->instr->arg2.uint;
886
    IDispatch *obj;
887
    DISPPARAMS dp;
888 889 890 891 892
    DISPID id;
    HRESULT hres;

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

893 894 895 896 897
    if(arg_cnt) {
        FIXME("arguments not supported\n");
        return E_NOTIMPL;
    }

898
    hres = stack_assume_disp(ctx, 1, &obj);
899 900 901 902 903 904 905 906
    if(FAILED(hres))
        return hres;

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

907 908
    hres = stack_assume_disp(ctx, 0, NULL);
    if(FAILED(hres))
909 910 911
        return hres;

    hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
912 913
    if(SUCCEEDED(hres)) {
        vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
914
        hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUTREF, &dp);
915
    }
916 917
    if(FAILED(hres))
        return hres;
918

919 920
    stack_popn(ctx, 2);
    return S_OK;
921 922
}

923 924 925
static HRESULT interp_const(exec_ctx_t *ctx)
{
    BSTR arg = ctx->instr->arg1.bstr;
926
    VARIANT *v;
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
    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;
    }

943
    hres = stack_assume_val(ctx, 0);
944 945 946
    if(FAILED(hres))
        return hres;

947 948 949 950 951 952
    hres = add_dynamic_var(ctx, arg, TRUE, &v);
    if(FAILED(hres))
        return hres;

    *v = *stack_pop(ctx);
    return S_OK;
953 954
}

955 956
static HRESULT interp_val(exec_ctx_t *ctx)
{
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
    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);
975 976 977 978 979 980 981 982 983 984 985 986
}

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

987 988 989
static HRESULT interp_new(exec_ctx_t *ctx)
{
    const WCHAR *arg = ctx->instr->arg1.bstr;
990 991 992 993 994
    class_desc_t *class_desc;
    vbdisp_t *obj;
    VARIANT v;
    HRESULT hres;

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

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

999 1000 1001 1002 1003 1004 1005 1006 1007
    if(!strcmpiW(arg, regexpW)) {
        V_VT(&v) = VT_DISPATCH;
        hres = create_regexp(&V_DISPATCH(&v));
        if(FAILED(hres))
            return hres;

        return stack_push(ctx, &v);
    }

1008 1009 1010 1011 1012 1013 1014 1015 1016
    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;
    }

1017
    hres = create_vbdisp(class_desc, &obj);
1018 1019 1020 1021 1022 1023
    if(FAILED(hres))
        return hres;

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

1026 1027 1028 1029
static HRESULT interp_dim(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg1.bstr;
    const unsigned array_id = ctx->instr->arg2.uint;
1030
    const array_desc_t *array_desc;
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    ref_t ref;
    HRESULT hres;

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

    assert(array_id < ctx->func->array_cnt);
    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;
    }
1048

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

1054 1055 1056 1057
    if(ctx->arrays[array_id]) {
        FIXME("Array already initialized\n");
        return E_FAIL;
    }
1058

1059 1060 1061 1062 1063
    array_desc = ctx->func->array_descs + array_id;
    if(array_desc->dim_cnt) {
        ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
        if(!ctx->arrays[array_id])
            return E_OUTOFMEMORY;
1064 1065 1066 1067 1068
    }

    V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
    V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
    return S_OK;
1069 1070
}

1071 1072 1073
static HRESULT interp_step(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg2.bstr;
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    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;

1102
    if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1103
        ctx->instr++;
1104 1105
    }else {
        stack_popn(ctx, 2);
1106
        instr_jmp(ctx, ctx->instr->arg1.uint);
1107
    }
1108
    return S_OK;
1109 1110
}

1111 1112
static HRESULT interp_newenum(exec_ctx_t *ctx)
{
1113
    variant_val_t v;
1114
    VARIANT *r;
1115 1116 1117 1118
    HRESULT hres;

    TRACE("\n");

1119
    stack_pop_deref(ctx, &v);
1120 1121
    assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
    r = stack_top(ctx, 0);
1122 1123

    switch(V_VT(v.v)) {
1124
    case VT_DISPATCH|VT_BYREF:
1125 1126 1127 1128 1129
    case VT_DISPATCH: {
        IEnumVARIANT *iter;
        DISPPARAMS dp = {0};
        VARIANT iterv;

1130 1131
        hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
        release_val(&v);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
        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;
        }

1148 1149
        V_VT(r) = VT_UNKNOWN;
        V_UNKNOWN(r) = (IUnknown*)iter;
1150 1151 1152
        break;
    }
    default:
1153 1154
        FIXME("Unsupported for %s\n", debugstr_variant(v.v));
        release_val(&v);
1155 1156 1157
        return E_NOTIMPL;
    }

1158
    return S_OK;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
}

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

1173 1174 1175 1176 1177
    if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
        FIXME("uninitialized\n");
        return E_FAIL;
    }

1178 1179 1180 1181 1182 1183 1184 1185 1186
    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;
1187
    hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    VariantClear(&v);
    if(FAILED(hres))
        return hres;

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

1201 1202
static HRESULT interp_jmp(exec_ctx_t *ctx)
{
1203 1204 1205 1206 1207 1208
    const unsigned arg = ctx->instr->arg1.uint;

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

    instr_jmp(ctx, arg);
    return S_OK;
1209 1210 1211 1212
}

static HRESULT interp_jmp_false(exec_ctx_t *ctx)
{
1213 1214
    const unsigned arg = ctx->instr->arg1.uint;
    HRESULT hres;
1215
    BOOL b;
1216 1217 1218

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

1219
    hres = stack_pop_bool(ctx, &b);
1220 1221 1222
    if(FAILED(hres))
        return hres;

1223
    if(b)
1224 1225 1226 1227
        ctx->instr++;
    else
        instr_jmp(ctx, ctx->instr->arg1.uint);
    return S_OK;
1228 1229
}

1230 1231 1232 1233
static HRESULT interp_jmp_true(exec_ctx_t *ctx)
{
    const unsigned arg = ctx->instr->arg1.uint;
    HRESULT hres;
1234
    BOOL b;
1235 1236 1237

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

1238
    hres = stack_pop_bool(ctx, &b);
1239 1240 1241
    if(FAILED(hres))
        return hres;

1242
    if(b)
1243 1244 1245 1246 1247 1248
        instr_jmp(ctx, ctx->instr->arg1.uint);
    else
        ctx->instr++;
    return S_OK;
}

1249 1250 1251 1252 1253 1254 1255 1256
static HRESULT interp_ret(exec_ctx_t *ctx)
{
    TRACE("\n");

    ctx->instr = NULL;
    return S_OK;
}

1257 1258 1259 1260 1261 1262 1263 1264
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;
}

1265 1266
static HRESULT interp_me(exec_ctx_t *ctx)
{
1267 1268 1269 1270 1271 1272 1273 1274
    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);
1275 1276
}

1277 1278
static HRESULT interp_bool(exec_ctx_t *ctx)
{
1279 1280 1281 1282 1283 1284 1285 1286
    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);
1287 1288
}

1289 1290 1291
static HRESULT interp_errmode(exec_ctx_t *ctx)
{
    const int err_mode = ctx->instr->arg1.uint;
1292 1293 1294 1295

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

    ctx->resume_next = err_mode;
1296
    ctx->script->err_number = S_OK;
1297
    return S_OK;
1298 1299
}

1300 1301
static HRESULT interp_string(exec_ctx_t *ctx)
{
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
    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);
1312 1313
}

1314 1315
static HRESULT interp_long(exec_ctx_t *ctx)
{
1316 1317 1318 1319 1320 1321 1322 1323
    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);
1324 1325 1326 1327
}

static HRESULT interp_short(exec_ctx_t *ctx)
{
1328 1329 1330 1331 1332 1333 1334 1335
    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);
1336 1337 1338 1339
}

static HRESULT interp_double(exec_ctx_t *ctx)
{
1340 1341 1342 1343 1344 1345 1346 1347
    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);
1348 1349
}

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
static HRESULT interp_empty(exec_ctx_t *ctx)
{
    VARIANT v;

    TRACE("\n");

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

1360 1361 1362
static HRESULT interp_null(exec_ctx_t *ctx)
{
    TRACE("\n");
1363
    return stack_push_null(ctx);
1364 1365
}

1366 1367
static HRESULT interp_nothing(exec_ctx_t *ctx)
{
1368 1369 1370 1371 1372 1373 1374
    VARIANT v;

    TRACE("\n");

    V_VT(&v) = VT_DISPATCH;
    V_DISPATCH(&v) = NULL;
    return stack_push(ctx, &v);
1375 1376
}

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
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);
}

1389 1390
static HRESULT interp_not(exec_ctx_t *ctx)
{
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
    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);
1407 1408
}

1409 1410
static HRESULT interp_and(exec_ctx_t *ctx)
{
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    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);
1431 1432
}

1433 1434
static HRESULT interp_or(exec_ctx_t *ctx)
{
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    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);
1455 1456
}

1457 1458
static HRESULT interp_xor(exec_ctx_t *ctx)
{
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
    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);
1479 1480 1481 1482
}

static HRESULT interp_eqv(exec_ctx_t *ctx)
{
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    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);
1503 1504 1505 1506
}

static HRESULT interp_imp(exec_ctx_t *ctx)
{
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
    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);
1527 1528
}

1529 1530 1531 1532 1533 1534 1535 1536 1537
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);
 }

1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
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);
1548
    if(SUCCEEDED(hres)) {
1549 1550
        hres = var_cmp(ctx, l.v, r.v);
        release_val(&l);
1551
    }
1552 1553 1554 1555 1556

    release_val(&r);
    return hres;
}

1557 1558
static HRESULT interp_equal(exec_ctx_t *ctx)
{
1559 1560 1561 1562 1563 1564 1565 1566
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1567 1568
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1569 1570 1571 1572

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

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
static HRESULT interp_nequal(exec_ctx_t *ctx)
{
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1585 1586
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1587 1588 1589 1590 1591 1592

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

1593 1594
static HRESULT interp_gt(exec_ctx_t *ctx)
{
1595 1596 1597 1598 1599 1600 1601 1602
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1603 1604
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1605 1606 1607 1608

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1609 1610 1611 1612
}

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

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1621 1622
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1623 1624 1625 1626

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1627 1628 1629 1630
}

static HRESULT interp_lt(exec_ctx_t *ctx)
{
1631 1632 1633 1634 1635 1636 1637 1638
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1639 1640
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1641 1642 1643 1644

    V_VT(&v) = VT_BOOL;
    V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
    return stack_push(ctx, &v);
1645 1646 1647 1648
}

static HRESULT interp_lteq(exec_ctx_t *ctx)
{
1649 1650 1651 1652 1653 1654 1655 1656
    VARIANT v;
    HRESULT hres;

    TRACE("\n");

    hres = cmp_oper(ctx);
    if(FAILED(hres))
        return hres;
1657 1658
    if(hres == VARCMP_NULL)
        return stack_push_null(ctx);
1659 1660 1661 1662

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

1665 1666
static HRESULT interp_case(exec_ctx_t *ctx)
{
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
    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;
1690 1691
}

1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
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);
}

1763 1764
static HRESULT interp_concat(exec_ctx_t *ctx)
{
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
    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);
1785 1786
}

1787 1788
static HRESULT interp_add(exec_ctx_t *ctx)
{
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
    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);
1809 1810 1811 1812
}

static HRESULT interp_sub(exec_ctx_t *ctx)
{
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
    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);
1833 1834
}

1835 1836
static HRESULT interp_mod(exec_ctx_t *ctx)
{
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    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);
1857 1858
}

1859 1860
static HRESULT interp_idiv(exec_ctx_t *ctx)
{
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    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);
1881 1882
}

1883 1884
static HRESULT interp_div(exec_ctx_t *ctx)
{
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
    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);
1905 1906 1907 1908
}

static HRESULT interp_mul(exec_ctx_t *ctx)
{
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
    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);
1929 1930
}

1931 1932
static HRESULT interp_exp(exec_ctx_t *ctx)
{
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
    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);
1953 1954
}

1955 1956
static HRESULT interp_neg(exec_ctx_t *ctx)
{
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
    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);
1971 1972
}

1973 1974 1975
static HRESULT interp_incc(exec_ctx_t *ctx)
{
    const BSTR ident = ctx->instr->arg1.bstr;
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
    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;
1998 1999
}

2000 2001 2002 2003 2004 2005
static HRESULT interp_catch(exec_ctx_t *ctx)
{
    /* Nothing to do here, the OP is for unwinding only. */
    return S_OK;
}

2006
static const instr_func_t op_funcs[] = {
2007
#define X(x,n,a,b) interp_ ## x,
2008 2009 2010 2011 2012
OP_LIST
#undef X
};

static const unsigned op_move[] = {
2013
#define X(x,n,a,b) n,
2014 2015 2016 2017
OP_LIST
#undef X
};

2018 2019 2020 2021 2022 2023 2024 2025
void release_dynamic_vars(dynamic_var_t *var)
{
    while(var) {
        VariantClear(&var->v);
        var = var->next;
    }
}

2026 2027
static void release_exec(exec_ctx_t *ctx)
{
2028
    unsigned i;
2029

2030
    VariantClear(&ctx->ret_val);
2031
    release_dynamic_vars(ctx->dynamic_vars);
2032

2033 2034 2035
    if(ctx->this_obj)
        IDispatch_Release(ctx->this_obj);

2036
    if(ctx->args) {
2037 2038 2039 2040
        for(i=0; i < ctx->func->arg_cnt; i++)
            VariantClear(ctx->args+i);
    }

2041 2042 2043 2044 2045
    if(ctx->vars) {
        for(i=0; i < ctx->func->var_cnt; i++)
            VariantClear(ctx->vars+i);
    }

2046 2047 2048 2049 2050 2051 2052 2053
    if(ctx->arrays) {
        for(i=0; i < ctx->func->var_cnt; i++) {
            if(ctx->arrays[i])
                SafeArrayDestroy(ctx->arrays[i]);
        }
        heap_free(ctx->arrays);
    }

2054
    heap_pool_free(&ctx->heap);
2055
    heap_free(ctx->args);
2056
    heap_free(ctx->vars);
2057 2058 2059
    heap_free(ctx->stack);
}

2060
HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2061
{
2062
    exec_ctx_t exec = {func->code_ctx};
2063 2064 2065
    vbsop_t op;
    HRESULT hres = S_OK;

2066 2067 2068 2069 2070 2071 2072
    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;
    }

2073
    heap_pool_init(&exec.heap);
2074

2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
    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
2091
                    hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2092
            }else {
2093
                hres = VariantCopyInd(exec.args+i, v);
2094 2095 2096 2097 2098 2099 2100 2101
            }
            if(FAILED(hres)) {
                release_exec(&exec);
                return hres;
            }
        }
    }else {
        exec.args = NULL;
2102 2103
    }

2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
    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;
    }

2114 2115 2116
    exec.stack_size = 16;
    exec.top = 0;
    exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2117 2118
    if(!exec.stack) {
        release_exec(&exec);
2119
        return E_OUTOFMEMORY;
2120
    }
2121

2122 2123 2124 2125
    if(vbthis) {
        exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
        exec.vbthis = vbthis;
    }else if (ctx->host_global) {
2126
        exec.this_obj = ctx->host_global;
2127
    }else {
2128
        exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2129
    }
2130 2131
    IDispatch_AddRef(exec.this_obj);

2132
    exec.instr = exec.code->instrs + func->code_off;
2133
    exec.script = ctx;
2134
    exec.func = func;
2135 2136 2137 2138 2139

    while(exec.instr) {
        op = exec.instr->op;
        hres = op_funcs[op](&exec);
        if(FAILED(hres)) {
2140
            ctx->err_number = hres = map_hres(hres);
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157

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

                stack_off = exec.instr->arg2.uint;
2158
                instr_jmp(&exec, exec.instr->arg1.uint);
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174

                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 {
2175
                WARN("Failed %08x\n", hres);
2176 2177 2178
                stack_popn(&exec, exec.top);
                break;
            }
2179 2180 2181 2182 2183
        }

        exec.instr += op_move[op];
    }

2184
    assert(!exec.top);
2185
    if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2186 2187 2188 2189 2190 2191 2192
        assert(V_VT(&exec.ret_val) == VT_EMPTY);

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

2193
    release_exec(&exec);
2194 2195
    return hres;
}