global.c 82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
 */

19
#include <assert.h>
20
#include <math.h>
21

22 23 24
#include "vbscript.h"
#include "vbscript_defs.h"

25 26
#include "mshtmhst.h"
#include "objsafe.h"
27
#include "wchar.h"
28

29 30 31 32
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(vbscript);

33
#define VB_E_CANNOT_CREATE_OBJ 0x800a01ad
34
#define VB_E_MK_PARSE_ERROR    0x800a01b0
35 36 37 38 39

/* Defined as extern in urlmon.idl, but not exported by uuid.lib */
const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
    {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};

40 41 42 43 44 45 46 47 48
#define BP_GET      1
#define BP_GETPUT   2

typedef struct {
    UINT16 len;
    WCHAR buf[7];
} string_constant_t;

struct _builtin_prop_t {
49
    const WCHAR *name;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    HRESULT (*proc)(BuiltinDisp*,VARIANT*,unsigned,VARIANT*);
    DWORD flags;
    unsigned min_args;
    UINT_PTR max_args;
};

static inline BuiltinDisp *impl_from_IDispatch(IDispatch *iface)
{
    return CONTAINING_RECORD(iface, BuiltinDisp, IDispatch_iface);
}

static HRESULT WINAPI Builtin_QueryInterface(IDispatch *iface, REFIID riid, void **ppv)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IDispatch_iface;
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
        *ppv = &This->IDispatch_iface;
    }else {
72 73
        if(!IsEqualGUID(riid, &IID_IDispatchEx))
            WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI Builtin_AddRef(IDispatch *iface)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI Builtin_Release(IDispatch *iface)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    if(!ref) {
        assert(!This->ctx);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI Builtin_GetTypeInfoCount(IDispatch *iface, UINT *pctinfo)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 0;
    return S_OK;
}

static HRESULT WINAPI Builtin_GetTypeInfo(IDispatch *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);
    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
    return DISP_E_BADINDEX;
}

122 123
HRESULT get_builtin_id(BuiltinDisp *disp, const WCHAR *name, DISPID *id)
{
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    size_t min = 1, max = disp->member_cnt - 1, i;
    int r;

    while(min <= max) {
        i = (min + max) / 2;
        r = wcsicmp(disp->members[i].name, name);
        if(!r) {
            *id = i;
            return S_OK;
        }
        if(r < 0)
            min = i+1;
        else
            max = i-1;
    }

    return DISP_E_MEMBERNOTFOUND;

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}

static HRESULT WINAPI Builtin_GetIDsOfNames(IDispatch *iface, REFIID riid, LPOLESTR *names, UINT name_cnt,
                                            LCID lcid, DISPID *ids)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);
    unsigned i;
    HRESULT hres;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), names, name_cnt, lcid, ids);

    if(!This->ctx) {
        FIXME("NULL context\n");
        return E_UNEXPECTED;
    }

    for(i = 0; i < name_cnt; i++) {
        hres = get_builtin_id(This, names[i], &ids[i]);
        if(FAILED(hres))
            return hres;
    }

    return S_OK;
}

static HRESULT WINAPI Builtin_Invoke(IDispatch *iface, DISPID id, REFIID riid, LCID lcid, WORD flags,
                                     DISPPARAMS *dp, VARIANT *res, EXCEPINFO *ei, UINT *err)
{
    BuiltinDisp *This = impl_from_IDispatch(iface);
    const builtin_prop_t *prop;
172
    VARIANT args_buf[8], *args;
173
    unsigned argn, i;
174
    HRESULT hres;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, id, debugstr_guid(riid), lcid, flags, dp, res, ei, err);

    if(!This->ctx) {
        FIXME("NULL context\n");
        return E_UNEXPECTED;
    }

    if(id >= This->member_cnt || (!This->members[id].proc && !This->members[id].flags))
        return DISP_E_MEMBERNOTFOUND;
    prop = This->members + id;

    switch(flags) {
    case DISPATCH_PROPERTYGET:
        if(!(prop->flags & (BP_GET|BP_GETPUT))) {
            FIXME("property does not support DISPATCH_PROPERTYGET\n");
            return E_FAIL;
        }
        break;
    case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
        if(!prop->proc && prop->flags == BP_GET) {
            const int vt = prop->min_args, val = prop->max_args;
            switch(vt) {
            case VT_I2:
                V_VT(res) = VT_I2;
                V_I2(res) = val;
                break;
            case VT_I4:
                V_VT(res) = VT_I4;
                V_I4(res) = val;
                break;
            case VT_BSTR: {
                const string_constant_t *str = (const string_constant_t*)prop->max_args;
                BSTR ret;

                ret = SysAllocStringLen(str->buf, str->len);
                if(!ret)
                    return E_OUTOFMEMORY;

                V_VT(res) = VT_BSTR;
                V_BSTR(res) = ret;
                break;
            }
            DEFAULT_UNREACHABLE;
            }
            return S_OK;
        }
        break;
    case DISPATCH_METHOD:
        if(prop->flags & (BP_GET|BP_GETPUT)) {
            FIXME("Call on property\n");
            return E_FAIL;
        }
        break;
    case DISPATCH_PROPERTYPUT:
        if(!(prop->flags & BP_GETPUT)) {
            FIXME("property does not support DISPATCH_PROPERTYPUT\n");
            return E_FAIL;
        }

        FIXME("call put\n");
        return E_NOTIMPL;
    default:
        FIXME("unsupported flags %x\n", flags);
        return E_NOTIMPL;
    }

    argn = arg_cnt(dp);

    if(argn < prop->min_args || argn > (prop->max_args ? prop->max_args : prop->min_args)) {
        WARN("invalid number of arguments\n");
        return MAKE_VBSERROR(VBSE_FUNC_ARITY_MISMATCH);
    }

249 250 251 252 253 254 255
    if(argn <= ARRAY_SIZE(args_buf)) {
        args = args_buf;
    }else {
        args = heap_alloc(argn * sizeof(*args));
        if(!args)
            return E_OUTOFMEMORY;
    }
256 257 258 259 260 261 262 263

    for(i=0; i < argn; i++) {
        if(V_VT(dp->rgvarg+dp->cArgs-i-1) == (VT_BYREF|VT_VARIANT))
            args[i] = *V_VARIANTREF(dp->rgvarg+dp->cArgs-i-1);
        else
            args[i] = dp->rgvarg[dp->cArgs-i-1];
    }

264 265 266 267
    hres = prop->proc(This, args, dp->cArgs, res);
    if(args != args_buf)
        heap_free(args);
    return hres;
268 269 270 271 272 273 274 275 276 277 278 279
}

static const IDispatchVtbl BuiltinDispVtbl = {
    Builtin_QueryInterface,
    Builtin_AddRef,
    Builtin_Release,
    Builtin_GetTypeInfoCount,
    Builtin_GetTypeInfo,
    Builtin_GetIDsOfNames,
    Builtin_Invoke
};

280
static HRESULT create_builtin_dispatch(script_ctx_t *ctx, const builtin_prop_t *members, size_t member_cnt, BuiltinDisp **ret)
281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
    BuiltinDisp *disp;

    if(!(disp = heap_alloc(sizeof(*disp))))
        return E_OUTOFMEMORY;

    disp->IDispatch_iface.lpVtbl = &BuiltinDispVtbl;
    disp->ref = 1;
    disp->members = members;
    disp->member_cnt = member_cnt;
    disp->ctx = ctx;

    *ret = disp;
    return S_OK;
295 296
}

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
static IInternetHostSecurityManager *get_sec_mgr(script_ctx_t *ctx)
{
    IInternetHostSecurityManager *secmgr;
    IServiceProvider *sp;
    HRESULT hres;

    if(!ctx->site)
        return NULL;

    if(ctx->secmgr)
        return ctx->secmgr;

    hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
    if(FAILED(hres))
        return NULL;

    hres = IServiceProvider_QueryService(sp, &SID_SInternetHostSecurityManager, &IID_IInternetHostSecurityManager,
            (void**)&secmgr);
    IServiceProvider_Release(sp);
    if(FAILED(hres))
        return NULL;

    return ctx->secmgr = secmgr;
}

322
static HRESULT return_string(VARIANT *res, const WCHAR *str)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
{
    BSTR ret;

    if(!res)
        return S_OK;

    ret = SysAllocString(str);
    if(!ret)
        return E_OUTOFMEMORY;

    V_VT(res) = VT_BSTR;
    V_BSTR(res) = ret;
    return S_OK;
}

338 339 340 341 342 343 344 345 346 347 348
static HRESULT return_bstr(VARIANT *res, BSTR str)
{
    if(res) {
        V_VT(res) = VT_BSTR;
        V_BSTR(res) = str;
    }else {
        SysFreeString(str);
    }
    return S_OK;
}

349 350 351 352 353 354 355 356 357
static HRESULT return_bool(VARIANT *res, BOOL val)
{
    if(res) {
        V_VT(res) = VT_BOOL;
        V_BOOL(res) = val ? VARIANT_TRUE : VARIANT_FALSE;
    }
    return S_OK;
}

358 359 360 361 362 363 364 365 366 367
static HRESULT return_short(VARIANT *res, short val)
{
    if(res) {
        V_VT(res) = VT_I2;
        V_I2(res) = val;
    }

    return S_OK;
}

368 369 370 371 372 373 374 375 376 377
static HRESULT return_int(VARIANT *res, int val)
{
    if(res) {
        V_VT(res) = VT_I4;
        V_I4(res) = val;
    }

    return S_OK;
}

378 379 380 381 382 383 384 385 386 387
static inline HRESULT return_double(VARIANT *res, double val)
{
    if(res) {
        V_VT(res) = VT_R8;
        V_R8(res) = val;
    }

    return S_OK;
}

388 389 390 391 392 393 394 395 396 397
static inline HRESULT return_float(VARIANT *res, float val)
{
    if(res) {
        V_VT(res) = VT_R4;
        V_R4(res) = val;
    }

    return S_OK;
}

398 399 400 401 402 403 404
static inline HRESULT return_null(VARIANT *res)
{
    if(res)
        V_VT(res) = VT_NULL;
    return S_OK;
}

405 406 407 408 409 410 411 412 413
static inline HRESULT return_date(VARIANT *res, double date)
{
    if(res) {
        V_VT(res) = VT_DATE;
        V_DATE(res) = date;
    }
    return S_OK;
}

414
HRESULT to_int(VARIANT *v, int *ret)
415
{
416 417
    VARIANT r;
    HRESULT hres;
418

419 420 421 422
    V_VT(&r) = VT_EMPTY;
    hres = VariantChangeType(&r, v, 0, VT_I4);
    if(FAILED(hres))
        return hres;
423

424
    *ret = V_I4(&r);
425 426 427
    return S_OK;
}

428 429
static HRESULT to_double(VARIANT *v, double *ret)
{
Shuai Meng's avatar
Shuai Meng committed
430 431
    VARIANT dst;
    HRESULT hres;
432

Shuai Meng's avatar
Shuai Meng committed
433 434 435 436
    V_VT(&dst) = VT_EMPTY;
    hres = VariantChangeType(&dst, v, 0, VT_R8);
    if(FAILED(hres))
        return hres;
437

Shuai Meng's avatar
Shuai Meng committed
438
    *ret = V_R8(&dst);
439 440 441
    return S_OK;
}

442 443
static HRESULT to_string(VARIANT *v, BSTR *ret)
{
444 445
    VARIANT dst;
    HRESULT hres;
446

447 448 449 450 451 452 453
    V_VT(&dst) = VT_EMPTY;
    hres = VariantChangeType(&dst, v, VARIANT_LOCALBOOL, VT_BSTR);
    if(FAILED(hres))
        return hres;

    *ret = V_BSTR(&dst);
    return S_OK;
454 455
}

456 457 458 459 460 461 462 463 464 465 466 467 468
static HRESULT to_system_time(VARIANT *v, SYSTEMTIME *st)
{
    VARIANT date;
    HRESULT hres;

    V_VT(&date) = VT_EMPTY;
    hres = VariantChangeType(&date, v, 0, VT_DATE);
    if(FAILED(hres))
        return hres;

    return VariantTimeToSystemTime(V_DATE(&date), st);
}

469 470 471 472 473 474 475 476 477 478 479
static HRESULT set_object_site(script_ctx_t *ctx, IUnknown *obj)
{
    IObjectWithSite *obj_site;
    IUnknown *ax_site;
    HRESULT hres;

    hres = IUnknown_QueryInterface(obj, &IID_IObjectWithSite, (void**)&obj_site);
    if(FAILED(hres))
        return S_OK;

    ax_site = create_ax_site(ctx);
480
    if(ax_site) {
481
        hres = IObjectWithSite_SetSite(obj_site, ax_site);
482 483
        IUnknown_Release(ax_site);
    }
484 485 486 487 488 489
    else
        hres = E_OUTOFMEMORY;
    IObjectWithSite_Release(obj_site);
    return hres;
}

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 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static IUnknown *create_object(script_ctx_t *ctx, const WCHAR *progid)
{
    IInternetHostSecurityManager *secmgr = NULL;
    struct CONFIRMSAFETY cs;
    IClassFactoryEx *cfex;
    IClassFactory *cf;
    DWORD policy_size;
    BYTE *bpolicy;
    IUnknown *obj;
    DWORD policy;
    GUID guid;
    HRESULT hres;

    hres = CLSIDFromProgID(progid, &guid);
    if(FAILED(hres))
        return NULL;

    TRACE("GUID %s\n", debugstr_guid(&guid));

    if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
        secmgr = get_sec_mgr(ctx);
        if(!secmgr)
            return NULL;

        policy = 0;
        hres = IInternetHostSecurityManager_ProcessUrlAction(secmgr, URLACTION_ACTIVEX_RUN,
                (BYTE*)&policy, sizeof(policy), (BYTE*)&guid, sizeof(GUID), 0, 0);
        if(FAILED(hres) || policy != URLPOLICY_ALLOW)
            return NULL;
    }

    hres = CoGetClassObject(&guid, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, NULL, &IID_IClassFactory, (void**)&cf);
    if(FAILED(hres))
        return NULL;

    hres = IClassFactory_QueryInterface(cf, &IID_IClassFactoryEx, (void**)&cfex);
    if(SUCCEEDED(hres)) {
        FIXME("Use IClassFactoryEx\n");
        IClassFactoryEx_Release(cfex);
    }

    hres = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (void**)&obj);
    if(FAILED(hres))
        return NULL;

    if(secmgr) {
        cs.clsid = guid;
        cs.pUnk = obj;
        cs.dwFlags = 0;
        hres = IInternetHostSecurityManager_QueryCustomPolicy(secmgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
                &bpolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
        if(SUCCEEDED(hres)) {
            policy = policy_size >= sizeof(DWORD) ? *(DWORD*)bpolicy : URLPOLICY_DISALLOW;
            CoTaskMemFree(bpolicy);
        }

        if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
            IUnknown_Release(obj);
            return NULL;
        }
    }

552 553 554 555
    hres = set_object_site(ctx, obj);
    if(FAILED(hres)) {
        IUnknown_Release(obj);
        return NULL;
556 557 558 559 560
    }

    return obj;
}

561
static HRESULT show_msgbox(script_ctx_t *ctx, BSTR prompt, unsigned type, BSTR orig_title, VARIANT *res)
562 563 564 565
{
    SCRIPTUICHANDLING uic_handling = SCRIPTUICHANDLING_ALLOW;
    IActiveScriptSiteUIControl *ui_control;
    IActiveScriptSiteWindow *acts_window;
566
    WCHAR *title_buf = NULL;
567 568
    const WCHAR *title;
    HWND hwnd = NULL;
569
    int ret = 0;
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    HRESULT hres;

    hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IActiveScriptSiteUIControl, (void**)&ui_control);
    if(SUCCEEDED(hres)) {
        hres = IActiveScriptSiteUIControl_GetUIBehavior(ui_control, SCRIPTUICITEM_MSGBOX, &uic_handling);
        IActiveScriptSiteUIControl_Release(ui_control);
        if(FAILED(hres))
            uic_handling = SCRIPTUICHANDLING_ALLOW;
    }

    switch(uic_handling) {
    case SCRIPTUICHANDLING_ALLOW:
        break;
    case SCRIPTUICHANDLING_NOUIDEFAULT:
        return return_short(res, 0);
    default:
        FIXME("blocked\n");
        return E_FAIL;
    }

    hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IActiveScriptSiteWindow, (void**)&acts_window);
    if(FAILED(hres)) {
        FIXME("No IActiveScriptSiteWindow\n");
        return hres;
    }

596 597 598 599
    if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
        if(orig_title && *orig_title) {
            WCHAR *ptr;

600
            title = title_buf = heap_alloc(sizeof(L"VBScript") + (lstrlenW(orig_title)+2)*sizeof(WCHAR));
601 602 603
            if(!title)
                return E_OUTOFMEMORY;

604 605
            memcpy(title_buf, L"VBScript", sizeof(L"VBScript"));
            ptr = title_buf + ARRAY_SIZE(L"VBScript")-1;
606 607 608

            *ptr++ = ':';
            *ptr++ = ' ';
609
            lstrcpyW(ptr, orig_title);
610
        }else {
611
            title = L"VBScript";
612 613
        }
    }else {
614
        title = orig_title ? orig_title : L"";
615 616
    }

617 618 619 620
    hres = IActiveScriptSiteWindow_GetWindow(acts_window, &hwnd);
    if(SUCCEEDED(hres)) {
        hres = IActiveScriptSiteWindow_EnableModeless(acts_window, FALSE);
        if(SUCCEEDED(hres)) {
621
            ret = MessageBoxW(hwnd, prompt, title, type);
622 623 624 625
            hres = IActiveScriptSiteWindow_EnableModeless(acts_window, TRUE);
        }
    }

626
    heap_free(title_buf);
627 628 629 630 631 632 633 634 635
    IActiveScriptSiteWindow_Release(acts_window);
    if(FAILED(hres)) {
        FIXME("failed: %08x\n", hres);
        return hres;
    }

    return return_short(res, ret);
}

636
static HRESULT Global_CCur(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
637
{
Shuai Meng's avatar
Shuai Meng committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    VARIANT v;
    HRESULT hres;

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

    assert(args_cnt == 1);

    V_VT(&v) = VT_EMPTY;
    hres = VariantChangeType(&v, arg, 0, VT_CY);
    if(FAILED(hres))
        return hres;

    if(!res) {
        VariantClear(&v);
        return DISP_E_BADVARTYPE;
    }

    *res = v;
    return S_OK;
657 658
}

659
static HRESULT Global_CInt(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
660
{
Shuai Meng's avatar
Shuai Meng committed
661
    VARIANT v;
662 663 664 665 666 667
    HRESULT hres;

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

    assert(args_cnt == 1);

Shuai Meng's avatar
Shuai Meng committed
668 669
    V_VT(&v) = VT_EMPTY;
    hres = VariantChangeType(&v, arg, 0, VT_I2);
670 671 672
    if(FAILED(hres))
        return hres;

Shuai Meng's avatar
Shuai Meng committed
673 674 675 676 677 678
    if(!res)
        return DISP_E_BADVARTYPE;
    else {
        *res = v;
        return S_OK;
    }
679 680
}

681
static HRESULT Global_CLng(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
682
{
683
    int i;
Shuai Meng's avatar
Shuai Meng committed
684 685 686 687 688 689
    HRESULT hres;

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

    assert(args_cnt == 1);

690
    hres = to_int(arg, &i);
Shuai Meng's avatar
Shuai Meng committed
691 692 693 694
    if(FAILED(hres))
        return hres;
    if(!res)
        return DISP_E_BADVARTYPE;
695

696
    return return_int(res, i);
697 698
}

699
static HRESULT Global_CBool(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
700
{
701 702 703
    VARIANT v;
    HRESULT hres;

704 705 706 707
    TRACE("%s\n", debugstr_variant(arg));

    assert(args_cnt == 1);

708 709 710 711
    V_VT(&v) = VT_EMPTY;
    hres = VariantChangeType(&v, arg, VARIANT_LOCALBOOL, VT_BOOL);
    if(FAILED(hres))
        return hres;
712

713 714 715 716 717
    if(res)
        *res = v;
    else
        VariantClear(&v);
    return S_OK;
718 719
}

720
static HRESULT Global_CByte(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
721
{
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
    VARIANT v;
    HRESULT hres;

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

    assert(args_cnt == 1);

    V_VT(&v) = VT_EMPTY;
    hres = VariantChangeType(&v, arg, VARIANT_LOCALBOOL, VT_UI1);
    if(FAILED(hres))
        return hres;

    if(!res) {
        VariantClear(&v);
        return DISP_E_BADVARTYPE;
    }

    *res = v;
    return S_OK;
741 742
}

743
static HRESULT Global_CDate(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
744 745 746 747 748
{
    FIXME("\n");
    return E_NOTIMPL;
}

749
static HRESULT Global_CDbl(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
750
{
Shuai Meng's avatar
Shuai Meng committed
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
    VARIANT v;
    HRESULT hres;

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

    assert(args_cnt == 1);

    V_VT(&v) = VT_EMPTY;
    hres = VariantChangeType(&v, arg, 0, VT_R8);
    if(FAILED(hres))
        return hres;

    if(!res)
        return DISP_E_BADVARTYPE;
    else {
        *res = v;
        return S_OK;
    }
769 770
}

771
static HRESULT Global_CSng(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
772
{
Shuai Meng's avatar
Shuai Meng committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    VARIANT v;
    HRESULT hres;

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

    assert(args_cnt == 1);

    V_VT(&v) = VT_EMPTY;
    hres = VariantChangeType(&v, arg, 0, VT_R4);
    if(FAILED(hres))
        return hres;

    if(!res)
        return DISP_E_BADVARTYPE;

   *res = v;
   return S_OK;
790 791
}

792
static HRESULT Global_CStr(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
793
{
794 795 796 797 798
    BSTR str;
    HRESULT hres;

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

799 800 801
    if(V_VT(arg) == VT_NULL)
        return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);

802 803 804 805 806
    hres = to_string(arg, &str);
    if(FAILED(hres))
        return hres;

    return return_bstr(res, str);
807 808
}

809 810 811 812 813
static inline WCHAR hex_char(unsigned n)
{
    return n < 10 ? '0'+n : 'A'+n-10;
}

814
static HRESULT Global_Hex(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
815
{
816 817
    WCHAR buf[17], *ptr;
    DWORD n;
Shuai Meng's avatar
Shuai Meng committed
818 819
    HRESULT hres;
    int ret;
820 821 822 823 824 825 826 827 828 829 830 831

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

    switch(V_VT(arg)) {
    case VT_I2:
        n = (WORD)V_I2(arg);
        break;
    case VT_NULL:
        if(res)
            V_VT(res) = VT_NULL;
        return S_OK;
    default:
Shuai Meng's avatar
Shuai Meng committed
832 833 834 835 836
        hres = to_int(arg, &ret);
        if(FAILED(hres))
            return hres;
        else
            n = ret;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    }

    buf[16] = 0;
    ptr = buf+15;

    if(n) {
        do {
            *ptr-- = hex_char(n & 0xf);
            n >>= 4;
        }while(n);
        ptr++;
    }else {
        *ptr = '0';
    }

852
    return return_string(res, ptr);
853 854
}

855
static HRESULT Global_Oct(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
856
{
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
    HRESULT hres;
    WCHAR buf[23], *ptr;
    DWORD n;
    int ret;

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

    switch(V_VT(arg)) {
    case VT_I2:
        n = (WORD)V_I2(arg);
        break;
    case VT_NULL:
        if(res)
            V_VT(res) = VT_NULL;
        return S_OK;
    default:
        hres = to_int(arg, &ret);
        if(FAILED(hres))
            return hres;
        else
            n = ret;
    }

    buf[22] = 0;
    ptr = buf + 21;

    if(n) {
        do {
            *ptr-- = '0' + (n & 0x7);
            n >>= 3;
        }while(n);
        ptr++;
    }else {
        *ptr = '0';
    }

    return return_string(res, ptr);
894 895
}

896
static HRESULT Global_VarType(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
897
{
898 899
    VARTYPE vt;

900 901 902 903
    TRACE("(%s)\n", debugstr_variant(arg));

    assert(args_cnt == 1);

904 905
    vt = V_VT(arg) & ~VT_BYREF;
    if(vt & ~(VT_TYPEMASK | VT_ARRAY)) {
906 907 908 909
        FIXME("not supported %s\n", debugstr_variant(arg));
        return E_NOTIMPL;
    }

910
    return return_short(res, vt);
911 912
}

913
static HRESULT Global_IsDate(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
914 915 916 917 918
{
    FIXME("\n");
    return E_NOTIMPL;
}

919
static HRESULT Global_IsEmpty(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
920
{
921 922 923 924 925 926 927 928 929
    TRACE("(%s)\n", debugstr_variant(arg));

    assert(args_cnt == 1);

    if(res) {
        V_VT(res) = VT_BOOL;
        V_BOOL(res) = V_VT(arg) == VT_EMPTY ? VARIANT_TRUE : VARIANT_FALSE;
    }
    return S_OK;
930 931
}

932
static HRESULT Global_IsNull(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
933
{
934 935 936 937 938 939 940 941 942
    TRACE("(%s)\n", debugstr_variant(arg));

    assert(args_cnt == 1);

    if(res) {
        V_VT(res) = VT_BOOL;
        V_BOOL(res) = V_VT(arg) == VT_NULL ? VARIANT_TRUE : VARIANT_FALSE;
    }
    return S_OK;
943 944
}

945
static HRESULT Global_IsNumeric(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
946
{
947 948 949 950 951 952 953 954 955 956
    HRESULT hres;
    double d;

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

    assert(args_cnt == 1);

    hres = to_double(arg, &d);

    return return_bool(res, SUCCEEDED(hres));
957 958
}

959
static HRESULT Global_IsArray(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
960
{
961 962 963 964 965
    TRACE("(%s)\n", debugstr_variant(arg));

    assert(args_cnt == 1);

    return return_bool(res, V_ISARRAY(arg));
966 967
}

968
static HRESULT Global_IsObject(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
969
{
970 971
    TRACE("(%s)\n", debugstr_variant(arg));

972
    assert(args_cnt == 1);
973 974 975 976 977 978

    if(res) {
        V_VT(res) = VT_BOOL;
        V_BOOL(res) = V_VT(arg) == VT_DISPATCH ? VARIANT_TRUE : VARIANT_FALSE;
    }
    return S_OK;
979 980
}

981
static HRESULT Global_Atn(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
982
{
Shuai Meng's avatar
Shuai Meng committed
983 984 985 986 987 988 989 990
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    return return_double(res, atan(d));
991 992
}

993
static HRESULT Global_Cos(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
994
{
Shuai Meng's avatar
Shuai Meng committed
995 996 997 998 999 1000 1001 1002
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    return return_double(res, cos(d));
1003 1004
}

1005
static HRESULT Global_Sin(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1006
{
Shuai Meng's avatar
Shuai Meng committed
1007 1008 1009 1010 1011 1012 1013 1014
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    return return_double(res, sin(d));
1015 1016
}

1017
static HRESULT Global_Tan(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1018
{
Shuai Meng's avatar
Shuai Meng committed
1019 1020 1021 1022 1023 1024 1025 1026
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    return return_double(res, tan(d));
1027 1028
}

1029
static HRESULT Global_Exp(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1030
{
Shuai Meng's avatar
Shuai Meng committed
1031 1032 1033 1034 1035 1036 1037 1038
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    return return_double(res, exp(d));
1039 1040
}

1041
static HRESULT Global_Log(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1042
{
Shuai Meng's avatar
Shuai Meng committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    if(d <= 0)
        return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
    else
        return return_double(res, log(d));
1054 1055
}

1056
static HRESULT Global_Sqr(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1057
{
Shuai Meng's avatar
Shuai Meng committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    HRESULT hres;
    double d;

    hres = to_double(arg, &d);
    if(FAILED(hres))
        return hres;

    if(d < 0)
        return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
    else
        return return_double(res, sqrt(d));
1069 1070
}

1071
static HRESULT Global_Randomize(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1072 1073 1074 1075 1076
{
    FIXME("\n");
    return E_NOTIMPL;
}

1077
static HRESULT Global_Rnd(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1078 1079 1080 1081 1082
{
    FIXME("\n");
    return E_NOTIMPL;
}

1083
static HRESULT Global_Timer(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1084
{
1085 1086 1087 1088 1089 1090 1091
    SYSTEMTIME lt;
    double sec;

    GetLocalTime(&lt);
    sec = lt.wHour * 3600 + lt.wMinute * 60 + lt.wSecond + lt.wMilliseconds / 1000.0;
    return return_float(res, sec);

1092 1093
}

1094
static HRESULT Global_LBound(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1095
{
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    SAFEARRAY *sa;
    HRESULT hres;
    LONG ubound;
    int dim;

    assert(args_cnt == 1 || args_cnt == 2);

    TRACE("%s %s\n", debugstr_variant(arg), args_cnt == 2 ? debugstr_variant(arg + 1) : "1");

    switch(V_VT(arg)) {
    case VT_VARIANT|VT_ARRAY:
        sa = V_ARRAY(arg);
        break;
    case VT_VARIANT|VT_ARRAY|VT_BYREF:
        sa = *V_ARRAYREF(arg);
        break;
    default:
        FIXME("arg %s not supported\n", debugstr_variant(arg));
        return E_NOTIMPL;
    }

    if(args_cnt == 2) {
        hres = to_int(arg + 1, &dim);
        if(FAILED(hres))
            return hres;
    }else {
        dim = 1;
    }

    hres = SafeArrayGetLBound(sa, dim, &ubound);
    if(FAILED(hres))
        return hres;

    return return_int(res, ubound);
1130 1131
}

1132
static HRESULT Global_UBound(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1133
{
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    SAFEARRAY *sa;
    HRESULT hres;
    LONG ubound;
    int dim;

    assert(args_cnt == 1 || args_cnt == 2);

    TRACE("%s %s\n", debugstr_variant(arg), args_cnt == 2 ? debugstr_variant(arg + 1) : "1");

    switch(V_VT(arg)) {
    case VT_VARIANT|VT_ARRAY:
        sa = V_ARRAY(arg);
        break;
    case VT_VARIANT|VT_ARRAY|VT_BYREF:
        sa = *V_ARRAYREF(arg);
        break;
    default:
        FIXME("arg %s not supported\n", debugstr_variant(arg));
        return E_NOTIMPL;
    }

    if(args_cnt == 2) {
        hres = to_int(arg + 1, &dim);
        if(FAILED(hres))
            return hres;
    }else {
        dim = 1;
    }

    hres = SafeArrayGetUBound(sa, dim, &ubound);
    if(FAILED(hres))
        return hres;

    return return_int(res, ubound);
1168 1169
}

1170
static HRESULT Global_RGB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1171
{
Shuai Meng's avatar
Shuai Meng committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    HRESULT hres;
    int i, color[3];

    TRACE("%s %s %s\n", debugstr_variant(arg), debugstr_variant(arg + 1), debugstr_variant(arg + 2));

    assert(args_cnt == 3);

    for(i = 0; i < 3; i++) {
        hres = to_int(arg + i, color + i);
        if(FAILED(hres))
            return hres;
        if(color[i] > 255)
            color[i] = 255;
        if(color[i] < 0)
            return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
    }

    return return_int(res, RGB(color[0], color[1], color[2]));
1190 1191
}

1192
static HRESULT Global_Len(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1193
{
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    DWORD len;
    HRESULT hres;

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

    if(V_VT(arg) == VT_NULL)
        return return_null(res);

    if(V_VT(arg) != VT_BSTR) {
        BSTR str;

        hres = to_string(arg, &str);
        if(FAILED(hres))
            return hres;

        len = SysStringLen(str);
        SysFreeString(str);
    }else {
        len = SysStringLen(V_BSTR(arg));
    }

    return return_int(res, len);
1216 1217
}

1218
static HRESULT Global_LenB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1219 1220 1221 1222 1223
{
    FIXME("\n");
    return E_NOTIMPL;
}

1224
static HRESULT Global_Left(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1225
{
1226 1227 1228 1229 1230 1231
    BSTR str, ret, conv_str = NULL;
    int len, str_len;
    HRESULT hres;

    TRACE("(%s %s)\n", debugstr_variant(args+1), debugstr_variant(args));

1232 1233
    if(V_VT(args) == VT_BSTR) {
        str = V_BSTR(args);
1234
    }else {
1235
        hres = to_string(args, &conv_str);
1236 1237 1238 1239 1240
        if(FAILED(hres))
            return hres;
        str = conv_str;
    }

1241
    hres = to_int(args+1, &len);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    if(FAILED(hres))
        return hres;

    if(len < 0) {
        FIXME("len = %d\n", len);
        return E_FAIL;
    }

    str_len = SysStringLen(str);
    if(len > str_len)
        len = str_len;

    ret = SysAllocStringLen(str, len);
    SysFreeString(conv_str);
    if(!ret)
        return E_OUTOFMEMORY;

    return return_bstr(res, ret);
1260 1261
}

1262
static HRESULT Global_LeftB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1263 1264 1265 1266 1267
{
    FIXME("\n");
    return E_NOTIMPL;
}

1268
static HRESULT Global_Right(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1269
{
1270 1271 1272 1273
    BSTR str, ret, conv_str = NULL;
    int len, str_len;
    HRESULT hres;

1274
    TRACE("(%s %s)\n", debugstr_variant(args), debugstr_variant(args+1));
1275 1276

    if(V_VT(args+1) == VT_BSTR) {
1277
        str = V_BSTR(args);
1278
    }else {
1279
        hres = to_string(args, &conv_str);
1280 1281 1282 1283 1284
        if(FAILED(hres))
            return hres;
        str = conv_str;
    }

1285
    hres = to_int(args+1, &len);
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    if(FAILED(hres))
        return hres;

    if(len < 0) {
        FIXME("len = %d\n", len);
        return E_FAIL;
    }

    str_len = SysStringLen(str);
    if(len > str_len)
        len = str_len;

    ret = SysAllocStringLen(str+str_len-len, len);
    SysFreeString(conv_str);
    if(!ret)
        return E_OUTOFMEMORY;

    return return_bstr(res, ret);
1304 1305
}

1306
static HRESULT Global_RightB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1307 1308 1309 1310 1311
{
    FIXME("\n");
    return E_NOTIMPL;
}

1312
static HRESULT Global_Mid(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1313
{
1314 1315 1316 1317
    int len = -1, start, str_len;
    BSTR str;
    HRESULT hres;

1318
    TRACE("(%s %s ...)\n", debugstr_variant(args), debugstr_variant(args+1));
1319

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    assert(args_cnt == 2 || args_cnt == 3);

    if(V_VT(args) != VT_BSTR) {
        FIXME("args[0] = %s\n", debugstr_variant(args));
        return E_NOTIMPL;
    }

    str = V_BSTR(args);

    hres = to_int(args+1, &start);
    if(FAILED(hres))
        return hres;

    if(args_cnt == 3) {
        hres = to_int(args+2, &len);
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
        if(FAILED(hres))
            return hres;

        if(len < 0) {
            FIXME("len = %d\n", len);
            return E_FAIL;
        }
    }


    str_len = SysStringLen(str);
    start--;
    if(start > str_len)
        start = str_len;

    if(len == -1)
        len = str_len-start;
    else if(len > str_len-start)
        len = str_len-start;

    if(res) {
        V_VT(res) = VT_BSTR;
        V_BSTR(res) = SysAllocStringLen(str+start, len);
        if(!V_BSTR(res))
            return E_OUTOFMEMORY;
    }

    return S_OK;
1363 1364
}

1365
static HRESULT Global_MidB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1366 1367 1368 1369 1370
{
    FIXME("\n");
    return E_NOTIMPL;
}

1371
static HRESULT Global_StrComp(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1372
{
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
    BSTR left, right;
    int mode, ret;
    HRESULT hres;
    short val;

    TRACE("(%s %s ...)\n", debugstr_variant(args), debugstr_variant(args+1));

    assert(args_cnt == 2 || args_cnt == 3);

    if (args_cnt == 3) {
        hres = to_int(args+2, &mode);
        if(FAILED(hres))
            return hres;

        if (mode != 0 && mode != 1) {
            FIXME("unknown compare mode = %d\n", mode);
            return E_FAIL;
        }
    }
    else
        mode = 0;

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
    hres = to_string(args, &left);
    if(FAILED(hres))
        return hres;

    hres = to_string(args+1, &right);
    if(FAILED(hres))
    {
        SysFreeString(left);
        return hres;
    }
1405

1406
    ret = mode ? wcsicmp(left, right) : wcscmp(left, right);
1407
    val = ret < 0 ? -1 : (ret > 0 ? 1 : 0);
1408 1409 1410

    SysFreeString(left);
    SysFreeString(right);
1411
    return return_short(res, val);
1412 1413
}

1414
static HRESULT Global_LCase(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1415
{
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    BSTR str;
    HRESULT hres;

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

    if(V_VT(arg) == VT_NULL) {
        if(res)
            V_VT(res) = VT_NULL;
        return S_OK;
    }

    hres = to_string(arg, &str);
    if(FAILED(hres))
        return hres;

    if(res) {
        WCHAR *ptr;

        for(ptr = str; *ptr; ptr++)
1435
            *ptr = towlower(*ptr);
1436 1437 1438 1439 1440 1441 1442

        V_VT(res) = VT_BSTR;
        V_BSTR(res) = str;
    }else {
        SysFreeString(str);
    }
    return S_OK;
1443 1444
}

1445
static HRESULT Global_UCase(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1446
{
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    BSTR str;
    HRESULT hres;

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

    if(V_VT(arg) == VT_NULL) {
        if(res)
            V_VT(res) = VT_NULL;
        return S_OK;
    }

    hres = to_string(arg, &str);
    if(FAILED(hres))
        return hres;

    if(res) {
        WCHAR *ptr;

        for(ptr = str; *ptr; ptr++)
1466
            *ptr = towupper(*ptr);
1467 1468 1469 1470 1471 1472 1473

        V_VT(res) = VT_BSTR;
        V_BSTR(res) = str;
    }else {
        SysFreeString(str);
    }
    return S_OK;
1474 1475
}

1476
static HRESULT Global_LTrim(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1477
{
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
    BSTR str, conv_str = NULL;
    WCHAR *ptr;
    HRESULT hres;

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

    if(V_VT(arg) == VT_BSTR) {
        str = V_BSTR(arg);
    }else {
        hres = to_string(arg, &conv_str);
        if(FAILED(hres))
            return hres;
        str = conv_str;
    }

1493
    for(ptr = str; *ptr && iswspace(*ptr); ptr++);
1494 1495 1496 1497 1498 1499 1500

    str = SysAllocString(ptr);
    SysFreeString(conv_str);
    if(!str)
        return E_OUTOFMEMORY;

    return return_bstr(res, str);
1501 1502
}

1503
static HRESULT Global_RTrim(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1504
{
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
    BSTR str, conv_str = NULL;
    WCHAR *ptr;
    HRESULT hres;

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

    if(V_VT(arg) == VT_BSTR) {
        str = V_BSTR(arg);
    }else {
        hres = to_string(arg, &conv_str);
        if(FAILED(hres))
            return hres;
        str = conv_str;
    }

1520
    for(ptr = str+SysStringLen(str); ptr-1 > str && iswspace(*(ptr-1)); ptr--);
1521 1522 1523 1524 1525 1526 1527

    str = SysAllocStringLen(str, ptr-str);
    SysFreeString(conv_str);
    if(!str)
        return E_OUTOFMEMORY;

    return return_bstr(res, str);
1528 1529
}

1530
static HRESULT Global_Trim(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1531
{
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
    BSTR str, conv_str = NULL;
    WCHAR *begin_ptr, *end_ptr;
    HRESULT hres;

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

    if(V_VT(arg) == VT_BSTR) {
        str = V_BSTR(arg);
    }else {
        hres = to_string(arg, &conv_str);
        if(FAILED(hres))
            return hres;
        str = conv_str;
    }

1547 1548
    for(begin_ptr = str; *begin_ptr && iswspace(*begin_ptr); begin_ptr++);
    for(end_ptr = str+SysStringLen(str); end_ptr-1 > begin_ptr && iswspace(*(end_ptr-1)); end_ptr--);
1549 1550 1551 1552 1553 1554 1555

    str = SysAllocStringLen(begin_ptr, end_ptr-begin_ptr);
    SysFreeString(conv_str);
    if(!str)
        return E_OUTOFMEMORY;

    return return_bstr(res, str);
1556 1557
}

1558
static HRESULT Global_Space(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1559
{
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    BSTR str;
    int n, i;
    HRESULT hres;

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

    hres = to_int(arg, &n);
    if(FAILED(hres))
        return hres;

    if(n < 0) {
        FIXME("n = %d\n", n);
        return E_NOTIMPL;
    }

    if(!res)
        return S_OK;

    str = SysAllocStringLen(NULL, n);
    if(!str)
        return E_OUTOFMEMORY;

    for(i=0; i<n; i++)
        str[i] = ' ';

    V_VT(res) = VT_BSTR;
    V_BSTR(res) = str;
    return S_OK;
1588 1589
}

1590
static HRESULT Global_String(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1591
{
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
    WCHAR ch;
    int cnt;
    HRESULT hres;

    TRACE("%s %s\n", debugstr_variant(args), debugstr_variant(args + 1));

    hres = to_int(args, &cnt);
    if(FAILED(hres))
        return hres;
    if(cnt < 0)
        return E_INVALIDARG;

    if(V_VT(args + 1) != VT_BSTR) {
        FIXME("Unsupported argument %s\n", debugstr_variant(args+1));
        return E_NOTIMPL;
    }
    if(!SysStringLen(V_BSTR(args + 1)))
        return E_INVALIDARG;
    ch = V_BSTR(args + 1)[0];

    if(res) {
        BSTR str = SysAllocStringLen(NULL, cnt);
        if(!str)
            return E_OUTOFMEMORY;
        wmemset(str, ch, cnt);
        V_VT(res) = VT_BSTR;
        V_BSTR(res) = str;
    }
    return S_OK;
1621 1622
}

1623
static HRESULT Global_InStr(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1624
{
1625 1626
    VARIANT *startv, *str1v, *str2v;
    BSTR str1, str2;
1627
    int ret = -1, start = 0, mode = 0;
1628
    HRESULT hres;
1629

1630
    TRACE("args_cnt=%u\n", args_cnt);
1631

1632 1633
    assert(2 <= args_cnt && args_cnt <= 4);

1634 1635 1636
    switch(args_cnt) {
    case 2:
        startv = NULL;
1637 1638
        str1v = args;
        str2v = args+1;
1639 1640
        break;
    case 3:
1641
        startv = args;
1642
        str1v = args+1;
1643
        str2v = args+2;
1644 1645
        break;
    case 4:
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
        startv = args;
        str1v = args+1;
        str2v = args+2;

        if(V_VT(args+3) == VT_NULL)
            return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);

        hres = to_int(args+3, &mode);
        if(FAILED(hres))
            return hres;

        if (mode != 0 && mode != 1)
            return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);

        break;
1661
    DEFAULT_UNREACHABLE;
1662 1663 1664
    }

    if(startv) {
1665 1666 1667
       if(V_VT(startv) == VT_NULL)
            return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);

1668 1669 1670
        hres = to_int(startv, &start);
        if(FAILED(hres))
            return hres;
1671 1672
        if(--start < 0)
            return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
1673 1674 1675 1676 1677 1678
    }

    if(V_VT(str1v) == VT_NULL || V_VT(str2v) == VT_NULL)
        return return_null(res);

    if(V_VT(str1v) != VT_BSTR) {
1679 1680 1681
        hres = to_string(str1v, &str1);
        if(FAILED(hres))
            return hres;
1682
    }
1683 1684
    else
        str1 = V_BSTR(str1v);
1685 1686

    if(V_VT(str2v) != VT_BSTR) {
1687 1688 1689 1690 1691 1692
        hres = to_string(str2v, &str2);
        if(FAILED(hres)){
            if(V_VT(str1v) != VT_BSTR)
                SysFreeString(str1);
            return hres;
        }
1693
    }
1694 1695
    else
        str2 = V_BSTR(str2v);
1696

1697 1698 1699 1700
    if(start < SysStringLen(str1)) {
        ret = FindStringOrdinal(FIND_FROMSTART, str1 + start, SysStringLen(str1)-start,
                                str2, SysStringLen(str2), mode);
    }
1701

1702 1703 1704 1705
    if(V_VT(str1v) != VT_BSTR)
        SysFreeString(str1);
    if(V_VT(str2v) != VT_BSTR)
        SysFreeString(str2);
1706
    return return_int(res, ++ret ? ret+start : 0);
1707 1708
}

1709
static HRESULT Global_InStrB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1710 1711 1712 1713 1714
{
    FIXME("\n");
    return E_NOTIMPL;
}

1715
static HRESULT Global_AscB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1716 1717 1718 1719 1720
{
    FIXME("\n");
    return E_NOTIMPL;
}

1721
static HRESULT Global_ChrB(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1722 1723 1724 1725 1726
{
    FIXME("\n");
    return E_NOTIMPL;
}

1727
static HRESULT Global_Asc(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1728
{
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
    BSTR conv_str = NULL, str;
    HRESULT hres = S_OK;

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

    switch(V_VT(arg)) {
    case VT_NULL:
        return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);
    case VT_EMPTY:
        return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
    case VT_BSTR:
        str = V_BSTR(arg);
        break;
    default:
        hres = to_string(arg, &conv_str);
        if(FAILED(hres))
            return hres;
        str = conv_str;
    }

1749
    if(!SysStringLen(str))
1750
        hres = MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
    else {
        unsigned char buf[2];
        short val = 0;
        int n = WideCharToMultiByte(CP_ACP, 0, str, 1, (char*)buf, sizeof(buf), NULL, NULL);
        switch(n) {
        case 1:
            val = buf[0];
            break;
        case 2:
            val = (buf[0] << 8) | buf[1];
            break;
        default:
            WARN("Failed to convert %x\n", *str);
            hres = MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
        }
        if(SUCCEEDED(hres))
            hres = return_short(res, val);
    }

1770 1771
    SysFreeString(conv_str);
    return hres;
1772 1773
}

1774 1775 1776
/* The function supports only single-byte and double-byte character sets. It
 * ignores language specified by IActiveScriptSite::GetLCID. The argument needs
 * to be in range of short or unsigned short. */
1777
static HRESULT Global_Chr(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1778
{
1779 1780 1781 1782
    int cp, c, len = 0;
    CPINFO cpi;
    WCHAR ch;
    char buf[2];
1783 1784 1785 1786 1787 1788 1789 1790
    HRESULT hres;

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

    hres = to_int(arg, &c);
    if(FAILED(hres))
        return hres;

1791 1792 1793 1794 1795 1796
    cp = GetACP();
    if(!GetCPInfo(cp, &cpi))
        cpi.MaxCharSize = 1;

    if((c!=(short)c && c!=(unsigned short)c) ||
            (unsigned short)c>=(cpi.MaxCharSize>1 ? 0x10000 : 0x100)) {
1797 1798
        WARN("invalid arg %d\n", c);
        return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
1799 1800
    }

1801 1802 1803 1804
    if(c>>8)
        buf[len++] = c>>8;
    if(!len || IsDBCSLeadByteEx(cp, buf[0]))
        buf[len++] = c;
1805
    if(!MultiByteToWideChar(CP_ACP, 0, buf, len, &ch, 1)) {
1806 1807 1808
        WARN("invalid arg %d, cp %d\n", c, cp);
        return E_FAIL;
    }
1809

1810
    if(res) {
1811 1812 1813 1814 1815 1816
        V_VT(res) = VT_BSTR;
        V_BSTR(res) = SysAllocStringLen(&ch, 1);
        if(!V_BSTR(res))
            return E_OUTOFMEMORY;
    }
    return S_OK;
1817 1818
}

1819
static HRESULT Global_AscW(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1820 1821 1822 1823 1824
{
    FIXME("\n");
    return E_NOTIMPL;
}

1825
static HRESULT Global_ChrW(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1826 1827 1828 1829 1830
{
    FIXME("\n");
    return E_NOTIMPL;
}

1831
static HRESULT Global_Abs(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1832
{
Shuai Meng's avatar
Shuai Meng committed
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
    HRESULT hres;
    VARIANT dst;

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

    assert(args_cnt == 1);

    hres = VarAbs(arg, &dst);
    if(FAILED(hres))
        return hres;

    if (res)
        *res = dst;
    else
        VariantClear(&dst);

    return S_OK;
1850 1851
}

1852
static HRESULT Global_Fix(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1853
{
Shuai Meng's avatar
Shuai Meng committed
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
    HRESULT hres;
    VARIANT dst;

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

    assert(args_cnt == 1);

    hres = VarFix(arg, &dst);
    if(FAILED(hres))
        return hres;

    if (res)
        *res = dst;
    else
        VariantClear(&dst);

    return S_OK;
1871 1872
}

1873
static HRESULT Global_Int(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1874
{
Shuai Meng's avatar
Shuai Meng committed
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
    HRESULT hres;
    VARIANT dst;

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

    assert(args_cnt == 1);

    hres = VarInt(arg, &dst);
    if(FAILED(hres))
        return hres;

    if (res)
        *res = dst;
    else
        VariantClear(&dst);

    return S_OK;
1892 1893
}

1894
static HRESULT Global_Sgn(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1895
{
Shuai Meng's avatar
Shuai Meng committed
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
    double v;
    short val;
    HRESULT hres;

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

    assert(args_cnt == 1);

    if(V_VT(arg) == VT_NULL)
        return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);

    hres = to_double(arg, &v);
    if (FAILED(hres))
        return hres;

    val = v == 0 ? 0 : (v > 0 ? 1 : -1);
    return return_short(res, val);
1913 1914
}

1915
static HRESULT Global_Now(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1916
{
1917 1918 1919 1920 1921 1922 1923 1924
    SYSTEMTIME lt;
    double date;

    TRACE("\n");

    GetLocalTime(&lt);
    SystemTimeToVariantTime(&lt, &date);
    return return_date(res, date);
1925 1926
}

1927
static HRESULT Global_Date(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1928
{
Shuai Meng's avatar
Shuai Meng committed
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
    SYSTEMTIME lt;
    UDATE ud;
    DATE date;
    HRESULT hres;

    TRACE("\n");

    GetLocalTime(&lt);
    ud.st = lt;
    ud.wDayOfYear = 0;
    hres = VarDateFromUdateEx(&ud, 0, VAR_DATEVALUEONLY, &date);
    if(FAILED(hres))
        return hres;
    return return_date(res, date);
1943 1944
}

1945
static HRESULT Global_Time(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1946
{
Shuai Meng's avatar
Shuai Meng committed
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
    SYSTEMTIME lt;
    UDATE ud;
    DATE time;
    HRESULT hres;

    TRACE("\n");

    GetLocalTime(&lt);
    ud.st = lt;
    ud.wDayOfYear = 0;
    hres = VarDateFromUdateEx(&ud, 0, VAR_TIMEVALUEONLY, &time);
    if(FAILED(hres))
        return hres;
    return return_date(res, time);
1961 1962
}

1963
static HRESULT Global_Day(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1964
{
1965 1966 1967 1968 1969 1970 1971
    SYSTEMTIME st;
    HRESULT hres;

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

    hres = to_system_time(arg, &st);
    return FAILED(hres) ? hres : return_short(res, st.wDay);
1972 1973
}

1974
static HRESULT Global_Month(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1975
{
1976 1977 1978 1979 1980 1981 1982
    SYSTEMTIME st;
    HRESULT hres;

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

    hres = to_system_time(arg, &st);
    return FAILED(hres) ? hres : return_short(res, st.wMonth);
1983 1984
}

1985
static HRESULT Global_Weekday(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1986 1987 1988 1989 1990
{
    FIXME("\n");
    return E_NOTIMPL;
}

1991
static HRESULT Global_Year(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1992
{
1993 1994 1995 1996 1997 1998 1999
    SYSTEMTIME st;
    HRESULT hres;

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

    hres = to_system_time(arg, &st);
    return FAILED(hres) ? hres : return_short(res, st.wYear);
2000 2001
}

2002
static HRESULT Global_Hour(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2003
{
2004 2005 2006 2007 2008 2009 2010
    SYSTEMTIME st;
    HRESULT hres;

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

    hres = to_system_time(arg, &st);
    return FAILED(hres) ? hres : return_short(res, st.wHour);
2011 2012
}

2013
static HRESULT Global_Minute(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2014
{
2015 2016 2017 2018 2019 2020 2021
    SYSTEMTIME st;
    HRESULT hres;

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

    hres = to_system_time(arg, &st);
    return FAILED(hres) ? hres : return_short(res, st.wMinute);
2022 2023
}

2024
static HRESULT Global_Second(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2025
{
2026 2027 2028 2029 2030 2031 2032
    SYSTEMTIME st;
    HRESULT hres;

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

    hres = to_system_time(arg, &st);
    return FAILED(hres) ? hres : return_short(res, st.wSecond);
2033 2034
}

2035
static HRESULT Global_DateValue(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2036 2037 2038 2039 2040
{
    FIXME("\n");
    return E_NOTIMPL;
}

2041
static HRESULT Global_TimeValue(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2042 2043 2044 2045 2046
{
    FIXME("\n");
    return E_NOTIMPL;
}

2047
static HRESULT Global_DateSerial(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2048 2049 2050 2051 2052
{
    FIXME("\n");
    return E_NOTIMPL;
}

2053
static HRESULT Global_TimeSerial(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2054 2055 2056 2057 2058
{
    FIXME("\n");
    return E_NOTIMPL;
}

2059
static HRESULT Global_InputBox(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2060 2061 2062 2063 2064
{
    FIXME("\n");
    return E_NOTIMPL;
}

2065
static HRESULT Global_MsgBox(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2066
{
2067 2068
    BSTR prompt, title = NULL;
    int type = MB_OK;
2069 2070 2071 2072
    HRESULT hres;

    TRACE("\n");

2073
    assert(1 <= args_cnt && args_cnt <= 5);
2074

2075
    hres = to_string(args, &prompt);
2076 2077 2078
    if(FAILED(hres))
        return hres;

2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
    if(args_cnt > 1)
        hres = to_int(args+1, &type);

    if(SUCCEEDED(hres) && args_cnt > 2)
        hres = to_string(args+2, &title);

    if(SUCCEEDED(hres) && args_cnt > 3) {
        FIXME("unsupported arg_cnt %d\n", args_cnt);
        hres = E_NOTIMPL;
    }

    if(SUCCEEDED(hres))
2091
        hres = show_msgbox(This->ctx, prompt, type, title, res);
2092

2093
    SysFreeString(prompt);
2094
    SysFreeString(title);
2095
    return hres;
2096 2097
}

2098
static HRESULT Global_CreateObject(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2099
{
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    IUnknown *obj;
    HRESULT hres;

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

    if(V_VT(arg) != VT_BSTR) {
        FIXME("non-bstr arg\n");
        return E_INVALIDARG;
    }

2110
    obj = create_object(This->ctx, V_BSTR(arg));
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
    if(!obj)
        return VB_E_CANNOT_CREATE_OBJ;

    if(res) {
        hres = IUnknown_QueryInterface(obj, &IID_IDispatch, (void**)&V_DISPATCH(res));
        if(FAILED(hres))
            return hres;

        V_VT(res) = VT_DISPATCH;
    }

    IUnknown_Release(obj);
    return S_OK;
2124 2125
}

2126
static HRESULT Global_GetObject(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2127
{
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
    IBindCtx *bind_ctx;
    IUnknown *obj_unk;
    IDispatch *disp;
    ULONG eaten = 0;
    IMoniker *mon;
    HRESULT hres;

    TRACE("%s %s\n", args_cnt ? debugstr_variant(args) : "", args_cnt > 1 ? debugstr_variant(args+1) : "");

    if(args_cnt != 1 || V_VT(args) != VT_BSTR) {
        FIXME("unsupported args\n");
        return E_NOTIMPL;
    }

2142
    if(This->ctx->safeopt & (INTERFACE_USES_SECURITY_MANAGER|INTERFACESAFE_FOR_UNTRUSTED_DATA)) {
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
        WARN("blocked in current safety mode\n");
        return VB_E_CANNOT_CREATE_OBJ;
    }

    hres = CreateBindCtx(0, &bind_ctx);
    if(FAILED(hres))
        return hres;

    hres = MkParseDisplayName(bind_ctx, V_BSTR(args), &eaten, &mon);
    if(SUCCEEDED(hres)) {
        hres = IMoniker_BindToObject(mon, bind_ctx, NULL, &IID_IUnknown, (void**)&obj_unk);
        IMoniker_Release(mon);
    }else {
        hres = MK_E_SYNTAX;
    }
    IBindCtx_Release(bind_ctx);
    if(FAILED(hres))
        return hres;

2162
    hres = set_object_site(This->ctx, obj_unk);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
    if(FAILED(hres)) {
        IUnknown_Release(obj_unk);
        return hres;
    }

    hres = IUnknown_QueryInterface(obj_unk, &IID_IDispatch, (void**)&disp);
    if(SUCCEEDED(hres)) {
        if(res) {
            V_VT(res) = VT_DISPATCH;
            V_DISPATCH(res) = disp;
        }else {
            IDispatch_Release(disp);
        }
    }else {
        FIXME("object does not support IDispatch\n");
    }

    return hres;
2181 2182
}

2183
static HRESULT Global_DateAdd(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2184 2185 2186 2187 2188
{
    FIXME("\n");
    return E_NOTIMPL;
}

2189
static HRESULT Global_DateDiff(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2190 2191 2192 2193 2194
{
    FIXME("\n");
    return E_NOTIMPL;
}

2195
static HRESULT Global_DatePart(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2196 2197 2198 2199 2200
{
    FIXME("\n");
    return E_NOTIMPL;
}

2201
static HRESULT Global_TypeName(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2202
{
2203 2204 2205 2206
    TRACE("(%s)\n", debugstr_variant(arg));

    assert(args_cnt == 1);

2207 2208 2209
    if (V_ISARRAY(arg))
        return return_string(res, L"Variant()");

2210 2211
    switch(V_VT(arg)) {
        case VT_UI1:
2212
            return return_string(res, L"Byte");
2213
        case VT_I2:
2214
            return return_string(res, L"Integer");
2215
        case VT_I4:
2216
            return return_string(res, L"Long");
2217
        case VT_R4:
2218
            return return_string(res, L"Single");
2219
        case VT_R8:
2220
            return return_string(res, L"Double");
2221
        case VT_CY:
2222
            return return_string(res, L"Currency");
2223
        case VT_DECIMAL:
2224
            return return_string(res, L"Decimal");
2225
        case VT_DATE:
2226
            return return_string(res, L"Date");
2227
        case VT_BSTR:
2228
            return return_string(res, L"String");
2229
        case VT_BOOL:
2230
            return return_string(res, L"Boolean");
2231
        case VT_EMPTY:
2232
            return return_string(res, L"Empty");
2233
        case VT_NULL:
2234
            return return_string(res, L"Null");
2235 2236 2237 2238
        default:
            FIXME("arg %s not supported\n", debugstr_variant(arg));
            return E_NOTIMPL;
        }
2239 2240
}

2241
static HRESULT Global_Array(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2242
{
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
    SAFEARRAYBOUND bounds;
    SAFEARRAY *sa;
    VARIANT *data;
    HRESULT hres;
    unsigned i;

    TRACE("arg_cnt=%u\n", args_cnt);

    bounds.lLbound = 0;
    bounds.cElements = args_cnt;
    sa = SafeArrayCreate(VT_VARIANT, 1, &bounds);
    if(!sa)
        return E_OUTOFMEMORY;

    hres = SafeArrayAccessData(sa, (void**)&data);
    if(FAILED(hres)) {
        SafeArrayDestroy(sa);
        return hres;
    }

    for(i=0; i<args_cnt; i++) {
        hres = VariantCopyInd(data+i, arg+i);
        if(FAILED(hres)) {
            SafeArrayUnaccessData(sa);
            SafeArrayDestroy(sa);
            return hres;
        }
    }
    SafeArrayUnaccessData(sa);

    if(res) {
        V_VT(res) = VT_ARRAY|VT_VARIANT;
        V_ARRAY(res) = sa;
    }else {
        SafeArrayDestroy(sa);
    }

    return S_OK;
2281 2282
}

2283
static HRESULT Global_Erase(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2284 2285 2286 2287 2288
{
    FIXME("\n");
    return E_NOTIMPL;
}

2289
static HRESULT Global_Filter(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2290 2291 2292 2293 2294
{
    FIXME("\n");
    return E_NOTIMPL;
}

2295
static HRESULT Global_Join(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2296 2297 2298 2299 2300
{
    FIXME("\n");
    return E_NOTIMPL;
}

2301
static HRESULT Global_Split(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2302
{
2303 2304
    BSTR str, string, delimiter = NULL;
    int count, max, mode, len, start, end, ret, delimiterlen = 1;
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
    int i,*indices = NULL, indices_max = 8;
    SAFEARRAYBOUND bounds;
    SAFEARRAY *sa = NULL;
    VARIANT *data, var;
    HRESULT hres = S_OK;

    TRACE("%s %u...\n", debugstr_variant(args), args_cnt);

    assert(1 <= args_cnt && args_cnt <= 4);

    if(V_VT(args) == VT_NULL || (args_cnt > 1 && V_VT(args+1) == VT_NULL) || (args_cnt > 2 && V_VT(args+2) == VT_NULL)
       || (args_cnt == 4 && V_VT(args+3) == VT_NULL))
        return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);

    if(V_VT(args) != VT_BSTR) {
        hres = to_string(args, &string);
        if(FAILED(hres))
            return hres;
    }else {
        string = V_BSTR(args);
    }

    if(args_cnt > 1) {
        if(V_VT(args+1) != VT_BSTR) {
2329
            hres = to_string(args+1, &delimiter);
2330 2331 2332
            if(FAILED(hres))
                goto error;
        }else {
2333
            delimiter = V_BSTR(args+1);
2334
        }
2335
        delimiterlen = SysStringLen(delimiter);
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
    }

    if(args_cnt > 2) {
        hres = to_int(args+2, &max);
        if(FAILED(hres))
            goto error;
        if (max < -1) {
            hres = MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
            goto error;
       }
    }else {
        max = -1;
    }

    if(args_cnt == 4) {
        hres = to_int(args+3, &mode);
        if(FAILED(hres))
            goto error;
        if (mode != 0 && mode != 1) {
            hres = MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
            goto error;
        }
    }else {
        mode = 0;
    }

    start = 0;

    len = SysStringLen(string);
    count = 0;

    indices = heap_alloc( indices_max * sizeof(int));
    if(!indices) {
        hres = E_OUTOFMEMORY;
        goto error;
    }

    while(1) {
        ret = -1;
2375
        if (delimiterlen) {
2376
            ret = FindStringOrdinal(FIND_FROMSTART, string + start, len - start,
2377
                                    delimiter ? delimiter : L" ", delimiterlen, mode);
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
        }

        if (ret == -1) {
            end = len;
        }else {
            end = start + ret;
        }

        if (count == indices_max) {
            indices_max *= 2;
            indices = heap_realloc( indices, indices_max * sizeof(int));
            if(!indices) {
                hres = E_OUTOFMEMORY;
                goto error;
            }
        }
        indices[count++] = end;

        if (ret == -1 || count == max) break;
2397
        start = start + ret + delimiterlen;
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
        if (start > len) break;
    }

    bounds.lLbound = 0;
    bounds.cElements = count;
    sa = SafeArrayCreate( VT_VARIANT, 1, &bounds);
    if (!sa) {
        hres = E_OUTOFMEMORY;
        goto error;
    }
    hres = SafeArrayAccessData(sa, (void**)&data);
    if(FAILED(hres)) {
        goto error;
    }

    start = 0;
    for (i = 0; i < count; i++) {
        str = SysAllocStringLen(string + start, indices[i] - start);
        if (!str) {
            hres = E_OUTOFMEMORY;
            break;
        }
        V_VT(&var) = VT_BSTR;
        V_BSTR(&var) = str;

        hres = VariantCopyInd(data+i, &var);
        if(FAILED(hres)) {
            SafeArrayUnaccessData(sa);
            goto error;
        }
2428
        start = indices[i]+delimiterlen;
2429 2430 2431 2432 2433 2434 2435 2436
    }
    SafeArrayUnaccessData(sa);

error:
    if(SUCCEEDED(hres) && res) {
        V_VT(res) = VT_ARRAY|VT_VARIANT;
        V_ARRAY(res) = sa;
    }else {
2437
        SafeArrayDestroy(sa);
2438 2439 2440 2441 2442
    }

    heap_free(indices);
    if(V_VT(args) != VT_BSTR)
        SysFreeString(string);
2443
    if(args_cnt > 1 && V_VT(args+1) != VT_BSTR)
2444
        SysFreeString(delimiter);
2445
    return hres;
2446 2447
}

2448
static HRESULT Global_Replace(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2449
{
2450
    BSTR string, find = NULL, replace = NULL, ret;
2451
    int from = 1, cnt = -1, mode = 0;
2452 2453 2454 2455 2456
    HRESULT hres = S_OK;

    TRACE("%s %s %s %u...\n", debugstr_variant(args), debugstr_variant(args+1), debugstr_variant(args+2), args_cnt);

    assert(3 <= args_cnt && args_cnt <= 6);
2457 2458 2459 2460 2461 2462 2463

   if(V_VT(args) == VT_NULL || V_VT(args+1) == VT_NULL || (V_VT(args+2) == VT_NULL)
        || (args_cnt >= 4 && V_VT(args+3) == VT_NULL) || (args_cnt >= 5 && V_VT(args+4) == VT_NULL)
        || (args_cnt == 6 && V_VT(args+5) == VT_NULL))
        return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);


2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
    if(V_VT(args) != VT_BSTR) {
        hres = to_string(args, &string);
        if(FAILED(hres))
            return hres;
    }else {
        string = V_BSTR(args);
    }

    if(V_VT(args+1) != VT_BSTR) {
        hres = to_string(args+1, &find);
        if(FAILED(hres))
            goto error;
    }else {
        find = V_BSTR(args+1);
    }

    if(V_VT(args+2) != VT_BSTR) {
        hres = to_string(args+2, &replace);
        if(FAILED(hres))
            goto error;
    }else {
        replace = V_BSTR(args+2);
    }

    if(args_cnt >= 4) {
        hres = to_int(args+3, &from);
        if(FAILED(hres))
            goto error;
        if(from < 1) {
            hres = E_INVALIDARG;
            goto error;
        }
    }

    if(args_cnt >= 5) {
        hres = to_int(args+4, &cnt);
        if(FAILED(hres))
            goto error;
        if(cnt < -1) {
            hres = E_INVALIDARG;
            goto error;
        }
    }

2508 2509 2510 2511 2512 2513 2514 2515 2516
    if(args_cnt == 6) {
        hres = to_int(args+5, &mode);
        if(FAILED(hres))
            goto error;
        if (mode != 0 && mode != 1) {
            hres = MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
            goto error;
        }
    }
2517

2518
    ret = string_replace(string, find, replace, from - 1, cnt, mode);
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
    if(!ret) {
        hres = E_OUTOFMEMORY;
    }else if(res) {
        V_VT(res) = VT_BSTR;
        V_BSTR(res) = ret;
    }else {
        SysFreeString(ret);
    }

error:
    if(V_VT(args) != VT_BSTR)
        SysFreeString(string);
    if(V_VT(args+1) != VT_BSTR)
        SysFreeString(find);
    if(V_VT(args+2) != VT_BSTR)
        SysFreeString(replace);
    return hres;
2536 2537
}

2538
static HRESULT Global_StrReverse(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2539
{
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
    WCHAR *ptr1, *ptr2, ch;
    BSTR ret;
    HRESULT hres;

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

    hres = to_string(arg, &ret);
    if(FAILED(hres))
        return hres;

    ptr1 = ret;
    ptr2 = ret + SysStringLen(ret)-1;
    while(ptr1 < ptr2) {
        ch = *ptr1;
        *ptr1++ = *ptr2;
        *ptr2-- = ch;
    }

    return return_bstr(res, ret);
2559 2560
}

2561
static HRESULT Global_InStrRev(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2562
{
2563
    int start = -1, ret = -1, mode = 0;
2564
    BSTR str1, str2;
2565
    size_t len1, len2;
2566 2567 2568 2569 2570 2571
    HRESULT hres;

    TRACE("%s %s arg_cnt=%u\n", debugstr_variant(args), debugstr_variant(args+1), args_cnt);

    assert(2 <= args_cnt && args_cnt <= 4);

2572 2573
    if(V_VT(args) == VT_NULL || V_VT(args+1) == VT_NULL || (args_cnt > 2 && V_VT(args+2) == VT_NULL)
       || (args_cnt == 4 && V_VT(args+3) == VT_NULL))
2574 2575
        return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);

2576 2577 2578 2579 2580 2581 2582
    if(args_cnt == 4) {
        hres = to_int(args+3, &mode);
        if(FAILED(hres))
            return hres;
        if (mode != 0 && mode != 1)
            return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
    }
2583

2584 2585 2586 2587 2588 2589
    if(args_cnt >= 3) {
        hres = to_int(args+2, &start);
        if(FAILED(hres))
            return hres;
        if(!start || start < -1)
            return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
2590 2591
    }

2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
    if(V_VT(args) != VT_BSTR) {
        hres = to_string(args, &str1);
        if(FAILED(hres))
            return hres;
    }
    else
        str1 = V_BSTR(args);

    if(V_VT(args+1) != VT_BSTR) {
        hres = to_string(args+1, &str2);
        if(FAILED(hres)) {
            if(V_VT(args) != VT_BSTR)
                SysFreeString(str1);
            return hres;
2606 2607
        }
    }
2608 2609
    else
        str2 = V_BSTR(args+1);
2610

2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
    len1 = SysStringLen(str1);
    if(!len1) {
        ret = 0;
        goto end;
    }

    if(start == -1)
        start = len1;

    len2 = SysStringLen(str2);
    if(!len2) {
        ret = start;
        goto end;
    }

    if(start >= len2 && start <= len1) {
        ret = FindStringOrdinal(FIND_FROMEND, str1, start,
                                str2, len2, mode);
    }
    ret++;
2631

2632 2633 2634 2635 2636
end:
    if(V_VT(args) != VT_BSTR)
        SysFreeString(str1);
    if(V_VT(args+1) != VT_BSTR)
        SysFreeString(str2);
2637
    return return_int(res, ret);
2638 2639
}

2640
static HRESULT Global_LoadPicture(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2641 2642 2643 2644 2645
{
    FIXME("\n");
    return E_NOTIMPL;
}

2646
static HRESULT Global_ScriptEngine(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2647
{
2648 2649 2650 2651
    TRACE("%s\n", debugstr_variant(arg));

    assert(args_cnt == 0);

2652
    return return_string(res, L"VBScript");
2653 2654
}

2655
static HRESULT Global_ScriptEngineMajorVersion(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2656
{
2657 2658 2659 2660
    TRACE("%s\n", debugstr_variant(arg));

    assert(args_cnt == 0);

2661
    return return_int(res, VBSCRIPT_MAJOR_VERSION);
2662 2663
}

2664
static HRESULT Global_ScriptEngineMinorVersion(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2665
{
2666 2667 2668 2669
    TRACE("%s\n", debugstr_variant(arg));

    assert(args_cnt == 0);

2670
    return return_int(res, VBSCRIPT_MINOR_VERSION);
2671 2672
}

2673
static HRESULT Global_ScriptEngineBuildVersion(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2674
{
2675 2676 2677 2678
    TRACE("%s\n", debugstr_variant(arg));

    assert(args_cnt == 0);

2679
    return return_int(res, VBSCRIPT_BUILD_VERSION);
2680 2681
}

2682
static HRESULT Global_FormatNumber(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2683 2684 2685 2686 2687
{
    FIXME("\n");
    return E_NOTIMPL;
}

2688
static HRESULT Global_FormatCurrency(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2689 2690 2691 2692 2693
{
    FIXME("\n");
    return E_NOTIMPL;
}

2694
static HRESULT Global_FormatPercent(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2695 2696 2697 2698 2699
{
    FIXME("\n");
    return E_NOTIMPL;
}

2700
static HRESULT Global_FormatDateTime(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2701 2702 2703 2704 2705
{
    FIXME("\n");
    return E_NOTIMPL;
}

2706
static HRESULT Global_WeekdayName(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2707
{
2708
    int weekday, first_day = 1, abbrev = 0;
2709 2710 2711 2712 2713 2714 2715
    BSTR ret;
    HRESULT hres;

    TRACE("\n");

    assert(1 <= args_cnt && args_cnt <= 3);

2716
    hres = to_int(args, &weekday);
2717 2718 2719 2720
    if(FAILED(hres))
        return hres;

    if(args_cnt > 1) {
2721
        hres = to_int(args+1, &abbrev);
2722 2723 2724 2725
        if(FAILED(hres))
            return hres;

        if(args_cnt == 3) {
2726
            hres = to_int(args+2, &first_day);
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
            if(FAILED(hres))
                return hres;
        }
    }

    hres = VarWeekdayName(weekday, abbrev, first_day, 0, &ret);
    if(FAILED(hres))
        return hres;

    return return_bstr(res, ret);
2737 2738
}

2739
static HRESULT Global_MonthName(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2740
{
2741 2742 2743 2744 2745 2746 2747 2748
    int month, abbrev = 0;
    BSTR ret;
    HRESULT hres;

    TRACE("\n");

    assert(args_cnt == 1 || args_cnt == 2);

2749
    hres = to_int(args, &month);
2750 2751 2752 2753
    if(FAILED(hres))
        return hres;

    if(args_cnt == 2) {
2754
        hres = to_int(args+1, &abbrev);
2755 2756 2757 2758 2759 2760 2761 2762 2763
        if(FAILED(hres))
            return hres;
    }

    hres = VarMonthName(month, abbrev, 0, &ret);
    if(FAILED(hres))
        return hres;

    return return_bstr(res, ret);
2764 2765
}

2766
static HRESULT Global_Round(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2767
{
2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
    double n;
    HRESULT hres;

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

    if(!res)
        return S_OK;

    switch(V_VT(arg)) {
    case VT_I2:
    case VT_I4:
    case VT_BOOL:
        *res = *arg;
        return S_OK;
    case VT_R8:
        n = V_R8(arg);
        break;
    default:
        hres = to_double(arg, &n);
        if(FAILED(hres))
            return hres;
    }

    return return_double(res, round(n));
2792 2793
}

2794
static HRESULT Global_Escape(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2795 2796 2797 2798 2799
{
    FIXME("\n");
    return E_NOTIMPL;
}

2800
static HRESULT Global_Unescape(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2801 2802 2803 2804 2805
{
    FIXME("\n");
    return E_NOTIMPL;
}

2806
static HRESULT Global_Eval(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2807 2808 2809 2810 2811
{
    FIXME("\n");
    return E_NOTIMPL;
}

2812
static HRESULT Global_Execute(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2813 2814 2815 2816 2817
{
    FIXME("\n");
    return E_NOTIMPL;
}

2818
static HRESULT Global_ExecuteGlobal(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2819 2820 2821 2822 2823
{
    FIXME("\n");
    return E_NOTIMPL;
}

2824
static HRESULT Global_GetRef(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2825 2826 2827 2828 2829
{
    FIXME("\n");
    return E_NOTIMPL;
}

2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
static HRESULT Global_Err(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
{
    TRACE("\n");

    if(args_cnt) {
        FIXME("Setter not supported\n");
        return E_NOTIMPL;
    }

    V_VT(res) = VT_DISPATCH;
    V_DISPATCH(res) = &This->ctx->err_obj->IDispatch_iface;
    IDispatch_AddRef(V_DISPATCH(res));
    return S_OK;
}

2845 2846 2847 2848 2849 2850 2851 2852 2853
static const string_constant_t vbCr          = {1, {'\r'}};
static const string_constant_t vbCrLf        = {2, {'\r','\n'}};
static const string_constant_t vbNewLine     = {2, {'\r','\n'}};
static const string_constant_t vbFormFeed    = {1, {0xc}};
static const string_constant_t vbLf          = {1, {'\n'}};
static const string_constant_t vbNullChar    = {1};
static const string_constant_t vbNullString  = {0};
static const string_constant_t vbTab         = {1, {'\t'}};
static const string_constant_t vbVerticalTab = {1, {0xb}};
2854

2855
static const builtin_prop_t global_props[] = {
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
    {NULL}, /* no default value */
    {L"Abs",                       Global_Abs, 0, 1},
    {L"Array",                     Global_Array, 0, 0, MAXDWORD},
    {L"Asc",                       Global_Asc, 0, 1},
    {L"AscB",                      Global_AscB, 0, 1},
    {L"AscW",                      Global_AscW, 0, 1},
    {L"Atn",                       Global_Atn, 0, 1},
    {L"CBool",                     Global_CBool, 0, 1},
    {L"CByte",                     Global_CByte, 0, 1},
    {L"CCur",                      Global_CCur, 0, 1},
    {L"CDate",                     Global_CDate, 0, 1},
    {L"CDbl",                      Global_CDbl, 0, 1},
    {L"Chr",                       Global_Chr, 0, 1},
    {L"ChrB",                      Global_ChrB, 0, 1},
    {L"ChrW",                      Global_ChrW, 0, 1},
    {L"CInt",                      Global_CInt, 0, 1},
    {L"CLng",                      Global_CLng, 0, 1},
    {L"Cos",                       Global_Cos, 0, 1},
    {L"CreateObject",              Global_CreateObject, 0, 1},
    {L"CSng",                      Global_CSng, 0, 1},
    {L"CStr",                      Global_CStr, 0, 1},
    {L"Date",                      Global_Date, 0, 0},
    {L"DateAdd",                   Global_DateAdd, 0, 3},
    {L"DateDiff",                  Global_DateDiff, 0, 3, 5},
    {L"DatePart",                  Global_DatePart, 0, 2, 4},
    {L"DateSerial",                Global_DateSerial, 0, 3},
    {L"DateValue",                 Global_DateValue, 0, 1},
    {L"Day",                       Global_Day, 0, 1},
    {L"Erase",                     Global_Erase, 0, 1},
2885
    {L"Err",                       Global_Err, BP_GETPUT},
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912
    {L"Escape",                    Global_Escape, 0, 1},
    {L"Eval",                      Global_Eval, 0, 1},
    {L"Execute",                   Global_Execute, 0, 1},
    {L"ExecuteGlobal",             Global_ExecuteGlobal, 0, 1},
    {L"Exp",                       Global_Exp, 0, 1},
    {L"Filter",                    Global_Filter, 0, 2, 4},
    {L"Fix",                       Global_Fix, 0, 1},
    {L"FormatCurrency",            Global_FormatCurrency, 0, 1, 5},
    {L"FormatDateTime",            Global_FormatDateTime, 0, 1, 2},
    {L"FormatNumber",              Global_FormatNumber, 0, 1, 5},
    {L"FormatPercent",             Global_FormatPercent, 0, 1, 5},
    {L"GetObject",                 Global_GetObject, 0, 0, 2},
    {L"GetRef",                    Global_GetRef, 0, 1},
    {L"Hex",                       Global_Hex, 0, 1},
    {L"Hour",                      Global_Hour, 0, 1},
    {L"InputBox",                  Global_InputBox, 0, 1, 7},
    {L"InStr",                     Global_InStr, 0, 2, 4},
    {L"InStrB",                    Global_InStrB, 0, 3, 4},
    {L"InStrRev",                  Global_InStrRev, 0, 2, 4},
    {L"Int",                       Global_Int, 0, 1},
    {L"IsArray",                   Global_IsArray, 0, 1},
    {L"IsDate",                    Global_IsDate, 0, 1},
    {L"IsEmpty",                   Global_IsEmpty, 0, 1},
    {L"IsNull",                    Global_IsNull, 0, 1},
    {L"IsNumeric",                 Global_IsNumeric, 0, 1},
    {L"IsObject",                  Global_IsObject, 0, 1},
    {L"Join",                      Global_Join, 0, 1, 2},
2913
    {L"LBound",                    Global_LBound, 0, 1, 2},
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
    {L"LCase",                     Global_LCase, 0, 1},
    {L"Left",                      Global_Left, 0, 2},
    {L"LeftB",                     Global_LeftB, 0, 2},
    {L"Len",                       Global_Len, 0, 1},
    {L"LenB",                      Global_LenB, 0, 1},
    {L"LoadPicture",               Global_LoadPicture, 0, 1},
    {L"Log",                       Global_Log, 0, 1},
    {L"LTrim",                     Global_LTrim, 0, 1},
    {L"Mid",                       Global_Mid, 0, 2, 3},
    {L"MidB",                      Global_MidB, 0, 2, 3},
    {L"Minute",                    Global_Minute, 0, 1},
    {L"Month",                     Global_Month, 0, 1},
    {L"MonthName",                 Global_MonthName, 0, 1, 2},
    {L"MsgBox",                    Global_MsgBox, 0, 1, 5},
    {L"Now",                       Global_Now, 0, 0},
    {L"Oct",                       Global_Oct, 0, 1},
    {L"Randomize",                 Global_Randomize, 0, 1},
    {L"Replace",                   Global_Replace, 0, 3, 6},
    {L"RGB",                       Global_RGB, 0, 3},
    {L"Right",                     Global_Right, 0, 2},
    {L"RightB",                    Global_RightB, 0, 2},
    {L"Rnd",                       Global_Rnd, 0, 1},
    {L"Round",                     Global_Round, 0, 1, 2},
    {L"RTrim",                     Global_RTrim, 0, 1},
    {L"ScriptEngine",              Global_ScriptEngine, 0, 0},
    {L"ScriptEngineBuildVersion",  Global_ScriptEngineBuildVersion, 0, 0},
    {L"ScriptEngineMajorVersion",  Global_ScriptEngineMajorVersion, 0, 0},
    {L"ScriptEngineMinorVersion",  Global_ScriptEngineMinorVersion, 0, 0},
    {L"Second",                    Global_Second, 0, 1},
    {L"Sgn",                       Global_Sgn, 0, 1},
    {L"Sin",                       Global_Sin, 0, 1},
    {L"Space",                     Global_Space, 0, 1},
    {L"Split",                     Global_Split, 0, 1, 4},
    {L"Sqr",                       Global_Sqr, 0, 1},
    {L"StrComp",                   Global_StrComp, 0, 2, 3},
    {L"String",                    Global_String, 0, 0, 2},
    {L"StrReverse",                Global_StrReverse, 0, 1},
    {L"Tan",                       Global_Tan, 0, 1},
    {L"Time",                      Global_Time, 0, 0},
    {L"Timer",                     Global_Timer, 0, 0},
    {L"TimeSerial",                Global_TimeSerial, 0, 3},
    {L"TimeValue",                 Global_TimeValue, 0, 1},
    {L"Trim",                      Global_Trim, 0, 1},
    {L"TypeName",                  Global_TypeName, 0, 1},
    {L"UBound",                    Global_UBound, 0, 1, 2},
    {L"UCase",                     Global_UCase, 0, 1},
    {L"Unescape",                  Global_Unescape, 0, 1},
    {L"VarType",                   Global_VarType, 0, 1},
    {L"vbAbort",                   NULL, BP_GET, VT_I2, IDABORT},
    {L"vbAbortRetryIgnore",        NULL, BP_GET, VT_I2, MB_ABORTRETRYIGNORE},
    {L"vbApplicationModal",        NULL, BP_GET, VT_I2, MB_APPLMODAL},
    {L"vbArray",                   NULL, BP_GET, VT_I2, VT_ARRAY},
    {L"vbBinaryCompare",           NULL, BP_GET, VT_I2, 0},
    {L"vbBlack",                   NULL, BP_GET, VT_I4, 0x000000},
    {L"vbBlue",                    NULL, BP_GET, VT_I4, 0xff0000},
    {L"vbBoolean",                 NULL, BP_GET, VT_I2, VT_BOOL},
    {L"vbByte",                    NULL, BP_GET, VT_I2, VT_UI1},
    {L"vbCancel",                  NULL, BP_GET, VT_I2, IDCANCEL},
    {L"vbCr",                      NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbCr},
    {L"vbCritical",                NULL, BP_GET, VT_I2, MB_ICONHAND},
    {L"vbCrLf",                    NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbCrLf},
    {L"vbCurrency",                NULL, BP_GET, VT_I2, VT_CY},
    {L"vbCyan",                    NULL, BP_GET, VT_I4, 0xffff00},
    {L"vbDatabaseCompare",         NULL, BP_GET, VT_I2, 2},
    {L"vbDataObject",              NULL, BP_GET, VT_I2, VT_UNKNOWN},
    {L"vbDate",                    NULL, BP_GET, VT_I2, VT_DATE},
    {L"vbDecimal",                 NULL, BP_GET, VT_I2, VT_DECIMAL},
    {L"vbDefaultButton1",          NULL, BP_GET, VT_I2, MB_DEFBUTTON1},
    {L"vbDefaultButton2",          NULL, BP_GET, VT_I2, MB_DEFBUTTON2},
    {L"vbDefaultButton3",          NULL, BP_GET, VT_I2, MB_DEFBUTTON3},
    {L"vbDefaultButton4",          NULL, BP_GET, VT_I2, MB_DEFBUTTON4},
    {L"vbDouble",                  NULL, BP_GET, VT_I2, VT_R8},
    {L"vbEmpty",                   NULL, BP_GET, VT_I2, VT_EMPTY},
    {L"vbError",                   NULL, BP_GET, VT_I2, VT_ERROR},
    {L"vbExclamation",             NULL, BP_GET, VT_I2, MB_ICONEXCLAMATION},
    {L"vbFalse",                   NULL, BP_GET, VT_I2, VARIANT_FALSE},
    {L"vbFirstFourDays",           NULL, BP_GET, VT_I2, 2},
    {L"vbFirstFullWeek",           NULL, BP_GET, VT_I2, 3},
    {L"vbFirstJan1",               NULL, BP_GET, VT_I2, 1},
    {L"vbFormFeed",                NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbFormFeed},
    {L"vbFriday",                  NULL, BP_GET, VT_I2, 6},
    {L"vbGeneralDate",             NULL, BP_GET, VT_I2, 0},
2996
    {L"vbGreen",                   NULL, BP_GET, VT_I4, 0x00ff00},
2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
    {L"vbIgnore",                  NULL, BP_GET, VT_I2, IDIGNORE},
    {L"vbInformation",             NULL, BP_GET, VT_I2, MB_ICONASTERISK},
    {L"vbInteger",                 NULL, BP_GET, VT_I2, VT_I2},
    {L"vbLf",                      NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbLf},
    {L"vbLong",                    NULL, BP_GET, VT_I2, VT_I4},
    {L"vbLongDate",                NULL, BP_GET, VT_I2, 1},
    {L"vbLongTime",                NULL, BP_GET, VT_I2, 3},
    {L"vbMagenta",                 NULL, BP_GET, VT_I4, 0xff00ff},
    {L"vbMonday",                  NULL, BP_GET, VT_I2, 2},
    {L"vbMsgBoxHelpButton",        NULL, BP_GET, VT_I4, MB_HELP},
    {L"vbMsgBoxRight",             NULL, BP_GET, VT_I4, MB_RIGHT},
    {L"vbMsgBoxRtlReading",        NULL, BP_GET, VT_I4, MB_RTLREADING},
    {L"vbMsgBoxSetForeground",     NULL, BP_GET, VT_I4, MB_SETFOREGROUND},
    {L"vbNewLine",                 NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNewLine},
    {L"vbNo",                      NULL, BP_GET, VT_I2, IDNO},
    {L"vbNull",                    NULL, BP_GET, VT_I2, VT_NULL},
    {L"vbNullChar",                NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNullChar},
    {L"vbNullString",              NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNullString},
    {L"vbObject",                  NULL, BP_GET, VT_I2, VT_DISPATCH},
    {L"vbObjectError",             NULL, BP_GET, VT_I4, 0x80040000},
    {L"vbOK",                      NULL, BP_GET, VT_I2, IDOK},
    {L"vbOKCancel",                NULL, BP_GET, VT_I2, MB_OKCANCEL},
    {L"vbOKOnly",                  NULL, BP_GET, VT_I2, MB_OK},
    {L"vbQuestion",                NULL, BP_GET, VT_I2, MB_ICONQUESTION},
    {L"vbRed",                     NULL, BP_GET, VT_I4, 0x0000ff},
    {L"vbRetry",                   NULL, BP_GET, VT_I2, IDRETRY},
    {L"vbRetryCancel",             NULL, BP_GET, VT_I2, MB_RETRYCANCEL},
    {L"vbSaturday",                NULL, BP_GET, VT_I2, 7},
    {L"vbShortDate",               NULL, BP_GET, VT_I2, 2},
    {L"vbShortTime",               NULL, BP_GET, VT_I2, 4},
    {L"vbSingle",                  NULL, BP_GET, VT_I2, VT_R4},
    {L"vbString",                  NULL, BP_GET, VT_I2, VT_BSTR},
    {L"vbSunday",                  NULL, BP_GET, VT_I2, 1},
    {L"vbSystemModal",             NULL, BP_GET, VT_I2, MB_SYSTEMMODAL},
    {L"vbTab",                     NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbTab},
    {L"vbTextCompare",             NULL, BP_GET, VT_I2, 1},
    {L"vbThursday",                NULL, BP_GET, VT_I2, 5},
    {L"vbTrue",                    NULL, BP_GET, VT_I2, VARIANT_TRUE},
    {L"vbTuesday",                 NULL, BP_GET, VT_I2, 3},
    {L"vbUseDefault",              NULL, BP_GET, VT_I2, -2},
    {L"vbUseSystem",               NULL, BP_GET, VT_I2, 0},
    {L"vbUseSystemDayOfWeek",      NULL, BP_GET, VT_I2, 0},
    {L"vbVariant",                 NULL, BP_GET, VT_I2, VT_VARIANT},
    {L"vbVerticalTab",             NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbVerticalTab},
    {L"vbWednesday",               NULL, BP_GET, VT_I2, 4},
    {L"vbWhite",                   NULL, BP_GET, VT_I4, 0xffffff},
3043
    {L"vbYellow",                  NULL, BP_GET, VT_I4, 0x00ffff},
3044 3045 3046 3047 3048 3049
    {L"vbYes",                     NULL, BP_GET, VT_I2, IDYES},
    {L"vbYesNo",                   NULL, BP_GET, VT_I2, MB_YESNO},
    {L"vbYesNoCancel",             NULL, BP_GET, VT_I2, MB_YESNOCANCEL},
    {L"Weekday",                   Global_Weekday, 0, 1, 2},
    {L"WeekdayName",               Global_WeekdayName, 0, 1, 3},
    {L"Year",                      Global_Year, 0, 1}
3050 3051
};

3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
static HRESULT err_string_prop(BSTR *prop, VARIANT *args, unsigned args_cnt, VARIANT *res)
{
    BSTR str;
    HRESULT hres;

    if(!args_cnt)
        return return_string(res, *prop ? *prop : L"");

    hres = to_string(args, &str);
    if(FAILED(hres))
        return hres;

    SysFreeString(*prop);
    *prop = str;
    return S_OK;
}

3069
static HRESULT Err_Description(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3070
{
3071
    TRACE("\n");
3072
    return err_string_prop(&This->ctx->ei.bstrDescription, args, args_cnt, res);
3073 3074
}

3075
static HRESULT Err_HelpContext(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3076
{
3077 3078 3079 3080 3081 3082 3083
    TRACE("\n");

    if(args_cnt) {
        FIXME("setter not implemented\n");
        return E_NOTIMPL;
    }

3084
    return return_int(res, This->ctx->ei.dwHelpContext);
3085 3086
}

3087
static HRESULT Err_HelpFile(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3088
{
3089
    TRACE("\n");
3090
    return err_string_prop(&This->ctx->ei.bstrHelpFile, args, args_cnt, res);
3091 3092
}

3093
static HRESULT Err_Number(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3094
{
3095 3096
    HRESULT hres;

3097 3098 3099 3100 3101 3102 3103
    TRACE("\n");

    if(args_cnt) {
        FIXME("setter not implemented\n");
        return E_NOTIMPL;
    }

3104
    hres = This->ctx->ei.scode;
3105
    return return_int(res, HRESULT_FACILITY(hres) == FACILITY_VBS ? HRESULT_CODE(hres) : hres);
3106 3107
}

3108
static HRESULT Err_Source(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3109
{
3110
    TRACE("\n");
3111
    return err_string_prop(&This->ctx->ei.bstrSource, args, args_cnt, res);
3112 3113
}

3114
static HRESULT Err_Clear(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3115
{
3116 3117
    TRACE("\n");

3118
    clear_ei(&This->ctx->ei);
3119
    return S_OK;
3120 3121
}

3122
static HRESULT Err_Raise(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
3123
{
3124 3125 3126 3127 3128 3129 3130 3131 3132
    BSTR source = NULL, description = NULL, helpfile = NULL;
    int code,  helpcontext = 0;
    HRESULT hres, error;

    TRACE("%s %u...\n", debugstr_variant(args), args_cnt);

    hres = to_int(args, &code);
    if(FAILED(hres))
        return hres;
3133
    if(code == 0 || code > 0xffff)
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
        return E_INVALIDARG;

    if(args_cnt >= 2)
        hres = to_string(args + 1, &source);
    if(args_cnt >= 3 && SUCCEEDED(hres))
        hres = to_string(args + 2, &description);
    if(args_cnt >= 4 && SUCCEEDED(hres))
        hres = to_string(args + 3, &helpfile);
    if(args_cnt >= 5 && SUCCEEDED(hres))
        hres = to_int(args + 4, &helpcontext);

3145 3146
    if(SUCCEEDED(hres)) {
        script_ctx_t *ctx = This->ctx;
3147

3148
        error = (code & ~0xffff) ? map_hres(code) : MAKE_VBSERROR(code);
3149 3150

        if(source) {
3151
            SysFreeString(ctx->ei.bstrSource);
3152 3153 3154 3155 3156
            ctx->ei.bstrSource = source;
        }
        if(!ctx->ei.bstrSource)
            ctx->ei.bstrSource = get_vbscript_string(VBS_RUNTIME_ERROR);
        if(description) {
3157
            SysFreeString(ctx->ei.bstrDescription);
3158 3159 3160 3161 3162
            ctx->ei.bstrDescription = description;
        }
        if(!ctx->ei.bstrDescription)
            ctx->ei.bstrDescription = get_vbscript_error_string(error);
        if(helpfile) {
3163
            SysFreeString(ctx->ei.bstrHelpFile);
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
            ctx->ei.bstrHelpFile = helpfile;
        }
        if(args_cnt >= 5)
            ctx->ei.dwHelpContext = helpcontext;

        ctx->ei.scode = error;
        hres = SCRIPT_E_RECORDED;
    }else {
        SysFreeString(source);
        SysFreeString(description);
        SysFreeString(helpfile);
    }

    return hres;
3178 3179 3180
}

static const builtin_prop_t err_props[] = {
3181 3182 3183 3184 3185 3186 3187 3188
    {NULL,            Err_Number, BP_GETPUT},
    {L"Clear",        Err_Clear},
    {L"Description",  Err_Description, BP_GETPUT},
    {L"HelpContext",  Err_HelpContext, BP_GETPUT},
    {L"HelpFile",     Err_HelpFile, BP_GETPUT},
    {L"Number",       Err_Number, BP_GETPUT},
    {L"Raise",        Err_Raise, 0, 1, 5},
    {L"Source",       Err_Source, BP_GETPUT}
3189 3190
};

3191 3192 3193
void detach_global_objects(script_ctx_t *ctx)
{
    if(ctx->err_obj) {
3194 3195
        ctx->err_obj->ctx = NULL;
        IDispatch_Release(&ctx->err_obj->IDispatch_iface);
3196 3197 3198 3199
        ctx->err_obj = NULL;
    }

    if(ctx->global_obj) {
3200 3201
        ctx->global_obj->ctx = NULL;
        IDispatch_Release(&ctx->global_obj->IDispatch_iface);
3202 3203 3204 3205
        ctx->global_obj = NULL;
    }
}

3206 3207 3208 3209
HRESULT init_global(script_ctx_t *ctx)
{
    HRESULT hres;

3210
    hres = create_builtin_dispatch(ctx, global_props, ARRAY_SIZE(global_props), &ctx->global_obj);
3211 3212 3213
    if(FAILED(hres))
        return hres;

3214
    return create_builtin_dispatch(ctx, err_props, ARRAY_SIZE(err_props), &ctx->err_obj);
3215
}