msscript.c 114 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2015 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 20
#define COBJMACROS

21
#include "windows.h"
22
#include "initguid.h"
23
#include "dispex.h"
24
#include "ole2.h"
25
#include "olectl.h"
26 27
#include "objsafe.h"
#include "activscp.h"
28
#include "rpcproxy.h"
29
#include "msscript.h"
30
DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31 32

#include "wine/debug.h"
33
#include "wine/heap.h"
34
#include "wine/list.h"
35 36 37

WINE_DEFAULT_DEBUG_CHANNEL(msscript);

38 39 40 41 42 43 44 45 46 47 48 49 50 51
#ifdef _WIN64

#define IActiveScriptParse_Release IActiveScriptParse64_Release
#define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
#define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText

#else

#define IActiveScriptParse_Release IActiveScriptParse32_Release
#define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
#define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText

#endif

52 53
struct ScriptControl;
typedef struct ConnectionPoint ConnectionPoint;
54
typedef struct ScriptProcedureCollection ScriptProcedureCollection;
55
typedef struct ScriptHost ScriptHost;
56 57 58 59 60 61 62 63

struct ConnectionPoint {
    IConnectionPoint IConnectionPoint_iface;
    ScriptControl *control;
    const IID *riid;
    ConnectionPoint *next;
};

64 65 66 67 68 69
struct named_item {
    struct list entry;
    BSTR name;
    IDispatch *disp;
};

70 71 72 73 74 75 76 77 78
struct module_enum {
    IEnumVARIANT IEnumVARIANT_iface;
    LONG ref;

    UINT pos;
    ScriptHost *host;
    ScriptControl *control;
};

79 80 81
typedef struct {
    IScriptModule IScriptModule_iface;
    LONG ref;
82

83
    BSTR name;
84
    ScriptHost *host;
85
    IDispatch *script_dispatch;
86
    ITypeInfo *script_typeinfo;
87
    ITypeComp *script_typecomp;
88 89

    ScriptProcedureCollection *procedures;
90 91
} ScriptModule;

92 93 94 95
typedef struct {
    IScriptProcedure IScriptProcedure_iface;
    LONG ref;

96 97 98
    ULONG hash;
    struct list entry;

99
    BSTR name;
100
    USHORT num_args;
101
    VARTYPE ret_type;
102 103
} ScriptProcedure;

104 105 106 107
struct ScriptProcedureCollection {
    IScriptProcedureCollection IScriptProcedureCollection_iface;
    LONG ref;

108
    LONG count;
109
    ScriptModule *module;
110
    struct list hash_table[43];
111 112
};

113 114 115 116 117 118 119 120 121
struct procedure_enum {
    IEnumVARIANT IEnumVARIANT_iface;
    LONG ref;

    WORD pos;
    WORD count;
    ScriptProcedureCollection *procedures;
};

122 123
typedef struct {
    IScriptError IScriptError_iface;
124
    IActiveScriptError *object;
125
    LONG ref;
126 127

    HRESULT number;
128
    BSTR text;
129
    BSTR source;
130
    BSTR desc;
131
    BSTR help_file;
132
    DWORD help_context;
133
    ULONG line;
134
    LONG column;
135 136

    BOOLEAN info_filled;
137
    BOOLEAN text_filled;
138
    BOOLEAN pos_filled;
139 140
} ScriptError;

141
struct ScriptHost {
142
    IActiveScriptSite IActiveScriptSite_iface;
143
    IActiveScriptSiteWindow IActiveScriptSiteWindow_iface;
144
    IServiceProvider IServiceProvider_iface;
145 146 147 148
    LONG ref;

    IActiveScript *script;
    IActiveScriptParse *parse;
149
    ScriptError *error;
150
    HWND site_hwnd;
151
    SCRIPTSTATE script_state;
152
    CLSID clsid;
153

154
    unsigned int module_count;
155
    struct list named_items;
156
};
157

158 159
struct ScriptControl {
    IScriptControl IScriptControl_iface;
160
    IPersistStreamInit IPersistStreamInit_iface;
161
    IOleObject IOleObject_iface;
162
    IOleControl IOleControl_iface;
163
    IQuickActivate IQuickActivate_iface;
164
    IViewObjectEx IViewObjectEx_iface;
165
    IPointerInactive IPointerInactive_iface;
166
    IConnectionPointContainer IConnectionPointContainer_iface;
167
    LONG ref;
168
    IOleClientSite *site;
169
    HWND site_hwnd;
170
    SIZEL extent;
171
    LONG timeout;
172
    VARIANT_BOOL allow_ui;
173
    VARIANT_BOOL use_safe_subset;
174 175 176 177 178

    /* connection points */
    ConnectionPoint *cp_list;
    ConnectionPoint cp_scsource;
    ConnectionPoint cp_propnotif;
179

180 181 182 183
    /* IViewObject sink */
    IAdviseSink *view_sink;
    DWORD view_sink_flags;

184
    /* modules */
185
    ScriptModule **modules;
186 187
    IScriptModuleCollection IScriptModuleCollection_iface;

188
    ScriptHost *host;
189
    ScriptError *error;
190 191
};

192 193
static HINSTANCE msscript_instance;

194 195
typedef enum tid_t {
    IScriptControl_tid,
196
    IScriptError_tid,
197
    IScriptModuleCollection_tid,
198
    IScriptModule_tid,
199
    IScriptProcedureCollection_tid,
200
    IScriptProcedure_tid,
201 202 203 204 205 206 207
    LAST_tid
} tid_t;

static ITypeLib *typelib;
static ITypeInfo *typeinfos[LAST_tid];

static REFIID tid_ids[] = {
208
    &IID_IScriptControl,
209
    &IID_IScriptError,
210
    &IID_IScriptModuleCollection,
211 212
    &IID_IScriptModule,
    &IID_IScriptProcedureCollection,
213
    &IID_IScriptProcedure
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
};

static HRESULT load_typelib(void)
{
    HRESULT hres;
    ITypeLib *tl;

    hres = LoadRegTypeLib(&LIBID_MSScriptControl, 1, 0, LOCALE_SYSTEM_DEFAULT, &tl);
    if(FAILED(hres)) {
        ERR("LoadRegTypeLib failed: %08x\n", hres);
        return hres;
    }

    if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
        ITypeLib_Release(tl);
    return hres;
}

static HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
{
    HRESULT hres;

    if (!typelib)
        hres = load_typelib();
    if (!typelib)
        return hres;

    if(!typeinfos[tid]) {
        ITypeInfo *ti;

        hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
        if(FAILED(hres)) {
            ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
            return hres;
        }

        if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
            ITypeInfo_Release(ti);
    }

    *typeinfo = typeinfos[tid];
    ITypeInfo_AddRef(typeinfos[tid]);
    return S_OK;
}

static void release_typelib(void)
{
    unsigned i;

    if(!typelib)
        return;

266
    for(i = 0; i < ARRAY_SIZE(typeinfos); i++)
267 268 269 270 271 272
        if(typeinfos[i])
            ITypeInfo_Release(typeinfos[i]);

    ITypeLib_Release(typelib);
}

273 274 275 276 277
static inline BOOL is_power_of_2(unsigned x)
{
    return !(x & (x - 1));
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static void clear_named_items(ScriptHost *host)
{
    struct named_item *item, *item1;
    LIST_FOR_EACH_ENTRY_SAFE(item, item1, &host->named_items, struct named_item, entry) {
       list_remove(&item->entry);
       SysFreeString(item->name);
       IDispatch_Release(item->disp);
       heap_free(item);
    }
}

static struct named_item *host_get_named_item(ScriptHost *host, const WCHAR *nameW)
{
    struct named_item *item;

    LIST_FOR_EACH_ENTRY(item, &host->named_items, struct named_item, entry) {
294
        if (!wcscmp(item->name, nameW))
295 296 297 298 299 300
            return item;
    }

    return NULL;
}

301
static HRESULT get_script_dispatch(ScriptModule *module, IDispatch **disp)
302
{
303
    if (!module->script_dispatch)
304
    {
305 306
        HRESULT hr = IActiveScript_GetScriptDispatch(module->host->script,
                                                     module->name, &module->script_dispatch);
307 308
        if (FAILED(hr)) return hr;
    }
309
    *disp = module->script_dispatch;
310 311 312
    return S_OK;
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
static HRESULT get_script_typeinfo(ScriptModule *module, ITypeInfo **typeinfo)
{
    IDispatch *disp;
    HRESULT hr;

    if (!module->script_typeinfo)
    {
        hr = get_script_dispatch(module, &disp);
        if (FAILED(hr)) return hr;

        hr = IDispatch_GetTypeInfo(disp, 0, LOCALE_USER_DEFAULT, &module->script_typeinfo);
        if (FAILED(hr)) return hr;
    }
    *typeinfo = module->script_typeinfo;
    return S_OK;
}

330 331 332 333 334 335 336 337 338 339 340 341 342
static HRESULT get_script_typecomp(ScriptModule *module, ITypeInfo *typeinfo, ITypeComp **typecomp)
{
    HRESULT hr;

    if (!module->script_typecomp)
    {
        hr = ITypeInfo_QueryInterface(typeinfo, &IID_ITypeComp, (void**)&module->script_typecomp);
        if (FAILED(hr)) return hr;
    }
    *typecomp = module->script_typecomp;
    return S_OK;
}

343 344 345 346 347 348 349 350 351 352 353 354
static void uncache_module_objects(ScriptModule *module)
{
    if (module->script_dispatch)
    {
        IDispatch_Release(module->script_dispatch);
        module->script_dispatch = NULL;
    }
    if (module->script_typeinfo)
    {
        ITypeInfo_Release(module->script_typeinfo);
        module->script_typeinfo = NULL;
    }
355 356 357 358 359
    if (module->script_typecomp)
    {
        ITypeComp_Release(module->script_typecomp);
        module->script_typecomp = NULL;
    }
360 361
}

362 363 364 365 366 367 368 369 370 371
static HRESULT set_script_state(ScriptHost *host, SCRIPTSTATE state)
{
    HRESULT hr;

    hr = IActiveScript_SetScriptState(host->script, state);
    if (SUCCEEDED(hr))
        host->script_state = state;
    return hr;
}

372
static HRESULT start_script(ScriptModule *module)
373 374 375
{
    HRESULT hr = S_OK;

376 377 378 379 380
    if (module->host->script_state != SCRIPTSTATE_STARTED)
    {
        hr = set_script_state(module->host, SCRIPTSTATE_STARTED);
        if (SUCCEEDED(hr)) uncache_module_objects(module);
    }
381 382 383 384

    return hr;
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
static HRESULT add_script_object(ScriptHost *host, BSTR name, IDispatch *object, DWORD flags)
{
    struct named_item *item;
    HRESULT hr;

    if (host_get_named_item(host, name))
        return E_INVALIDARG;

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

    item->name = SysAllocString(name);
    if (!item->name)
    {
        heap_free(item);
        return E_OUTOFMEMORY;
    }
    IDispatch_AddRef(item->disp = object);
    list_add_tail(&host->named_items, &item->entry);

    hr = IActiveScript_AddNamedItem(host->script, name, flags);
    if (FAILED(hr))
    {
        list_remove(&item->entry);
        IDispatch_Release(item->disp);
        SysFreeString(item->name);
        heap_free(item);
        return hr;
    }

    return hr;
}

419
static HRESULT parse_script_text(ScriptModule *module, BSTR script_text, DWORD flag, VARIANT *res)
420 421 422 423
{
    EXCEPINFO excepinfo;
    HRESULT hr;

424
    hr = start_script(module);
425 426
    if (FAILED(hr)) return hr;

427
    uncache_module_objects(module);
428 429
    if (module->procedures)
        module->procedures->count = -1;
430

431
    hr = IActiveScriptParse_ParseScriptText(module->host->parse, script_text, module->name,
432 433 434 435 436
                                            NULL, NULL, 0, 1, flag, res, &excepinfo);
    /* FIXME: more error handling */
    return hr;
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
static HRESULT WINAPI sp_caller_QueryInterface(IServiceProvider *iface, REFIID riid, void **obj)
{
    if (IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IServiceProvider, riid))
        *obj = iface;
    else
    {
        FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
        *obj = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI sp_caller_AddRef(IServiceProvider *iface)
{
    return 2;
}

static ULONG WINAPI sp_caller_Release(IServiceProvider *iface)
{
    return 1;
}

static HRESULT WINAPI sp_caller_QueryService(IServiceProvider *iface, REFGUID service, REFIID riid, void **obj)
{
    FIXME("(%p)->(%s %s %p): semi-stub\n", iface, debugstr_guid(service), debugstr_guid(riid), obj);

    *obj = NULL;
    if (IsEqualGUID(&SID_GetCaller, service))
        return S_OK;

    return E_NOINTERFACE;
}

static const IServiceProviderVtbl sp_caller_vtbl = {
    sp_caller_QueryInterface,
    sp_caller_AddRef,
    sp_caller_Release,
    sp_caller_QueryService
};

static IServiceProvider sp_caller = { &sp_caller_vtbl };

482 483 484 485 486 487 488 489 490
static HRESULT run_procedure(ScriptModule *module, BSTR procedure_name, SAFEARRAY *args, VARIANT *res)
{
    IDispatchEx *dispex;
    IDispatch *disp;
    DISPPARAMS dp;
    DISPID dispid;
    HRESULT hr;
    UINT i;

491
    hr = start_script(module);
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
    if (FAILED(hr)) return hr;

    hr = get_script_dispatch(module, &disp);
    if (FAILED(hr)) return hr;

    hr = IDispatch_GetIDsOfNames(disp, &IID_NULL, &procedure_name, 1, LOCALE_USER_DEFAULT, &dispid);
    if (FAILED(hr)) return hr;

    dp.cArgs = args->rgsabound[0].cElements;
    dp.rgdispidNamedArgs = NULL;
    dp.cNamedArgs = 0;
    dp.rgvarg = heap_alloc(dp.cArgs * sizeof(*dp.rgvarg));
    if (!dp.rgvarg) return E_OUTOFMEMORY;

    hr = SafeArrayLock(args);
    if (SUCCEEDED(hr))
    {
        /* The DISPPARAMS are stored in reverse order */
        for (i = 0; i < dp.cArgs; i++)
            dp.rgvarg[i] = *(VARIANT*)((char*)(args->pvData) + (dp.cArgs - i - 1) * args->cbElements);
        SafeArrayUnlock(args);

        hr = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
        if (FAILED(hr))
        {
            hr = IDispatch_Invoke(disp, dispid, &IID_NULL, LOCALE_USER_DEFAULT,
                                  DISPATCH_METHOD, &dp, res, NULL, NULL);
        }
        else
        {
            hr = IDispatchEx_InvokeEx(dispex, dispid, LOCALE_USER_DEFAULT,
523
                                      DISPATCH_METHOD, &dp, res, NULL, &sp_caller);
524 525 526 527 528 529 530 531
            IDispatchEx_Release(dispex);
        }
    }
    heap_free(dp.rgvarg);

    return hr;
}

532 533 534 535 536
static inline ScriptControl *impl_from_IScriptControl(IScriptControl *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IScriptControl_iface);
}

537 538 539 540 541
static inline ScriptControl *impl_from_IOleObject(IOleObject *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IOleObject_iface);
}

542 543 544 545 546
static inline ScriptControl *impl_from_IPersistStreamInit(IPersistStreamInit *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IPersistStreamInit_iface);
}

547 548 549 550 551
static inline ScriptControl *impl_from_IOleControl(IOleControl *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IOleControl_iface);
}

552 553 554 555 556
static inline ScriptControl *impl_from_IQuickActivate(IQuickActivate *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IQuickActivate_iface);
}

557
static inline ScriptControl *impl_from_IViewObjectEx(IViewObjectEx *iface)
558
{
559
    return CONTAINING_RECORD(iface, ScriptControl, IViewObjectEx_iface);
560 561
}

562 563 564 565 566
static inline ScriptControl *impl_from_IPointerInactive(IPointerInactive *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IPointerInactive_iface);
}

567 568 569 570 571
static inline ScriptControl *impl_from_IConnectionPointContainer(IConnectionPointContainer *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IConnectionPointContainer_iface);
}

572 573 574 575 576
static inline ScriptProcedure *impl_from_IScriptProcedure(IScriptProcedure *iface)
{
    return CONTAINING_RECORD(iface, ScriptProcedure, IScriptProcedure_iface);
}

577 578 579 580 581
static inline ScriptProcedureCollection *impl_from_IScriptProcedureCollection(IScriptProcedureCollection *iface)
{
    return CONTAINING_RECORD(iface, ScriptProcedureCollection, IScriptProcedureCollection_iface);
}

582 583 584 585 586
static inline ScriptControl *impl_from_IScriptModuleCollection(IScriptModuleCollection *iface)
{
    return CONTAINING_RECORD(iface, ScriptControl, IScriptModuleCollection_iface);
}

587 588 589 590 591
static inline ScriptModule *impl_from_IScriptModule(IScriptModule *iface)
{
    return CONTAINING_RECORD(iface, ScriptModule, IScriptModule_iface);
}

592 593 594 595 596
static inline ScriptError *impl_from_IScriptError(IScriptError *iface)
{
    return CONTAINING_RECORD(iface, ScriptError, IScriptError_iface);
}

597 598 599 600 601
static inline ConnectionPoint *impl_from_IConnectionPoint(IConnectionPoint *iface)
{
    return CONTAINING_RECORD(iface, ConnectionPoint, IConnectionPoint_iface);
}

602 603 604 605 606
static inline ScriptHost *impl_from_IActiveScriptSite(IActiveScriptSite *iface)
{
    return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSite_iface);
}

607 608 609 610 611
static inline ScriptHost *impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow *iface)
{
    return CONTAINING_RECORD(iface, ScriptHost, IActiveScriptSiteWindow_iface);
}

612 613 614 615 616
static inline ScriptHost *impl_from_IServiceProvider(IServiceProvider *iface)
{
    return CONTAINING_RECORD(iface, ScriptHost, IServiceProvider_iface);
}

617 618 619 620 621
static inline struct module_enum *module_enum_from_IEnumVARIANT(IEnumVARIANT *iface)
{
    return CONTAINING_RECORD(iface, struct module_enum, IEnumVARIANT_iface);
}

622 623 624 625 626
static inline struct procedure_enum *procedure_enum_from_IEnumVARIANT(IEnumVARIANT *iface)
{
    return CONTAINING_RECORD(iface, struct procedure_enum, IEnumVARIANT_iface);
}

627 628 629 630 631 632 633 634 635 636 637
/* IActiveScriptSite */
static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface, REFIID riid, void **ppv)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IActiveScriptSite_iface;
    }else if(IsEqualGUID(&IID_IActiveScriptSite, riid)) {
        TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This, ppv);
        *ppv = &This->IActiveScriptSite_iface;
638 639 640
    }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow, riid)) {
        TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This, ppv);
        *ppv = &This->IActiveScriptSiteWindow_iface;
641 642 643
    }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
        TRACE("(%p)->(IID_IServiceProvider %p)\n", This, ppv);
        *ppv = &This->IServiceProvider_iface;
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    }else {
        FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

671 672
    if(!ref) {
        clear_named_items(This);
673
        heap_free(This);
674
    }
675 676 677 678 679 680 681 682 683 684 685 686 687 688

    return ref;
}

static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *lcid)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    TRACE("(%p, %p)\n", This, lcid);

    *lcid = GetUserDefaultLCID();
    return S_OK;
}

689 690
static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
    IUnknown **unk, ITypeInfo **ti)
691 692
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);
693
    struct named_item *item;
694

695
    TRACE("(%p, %s, %#x, %p, %p)\n", This, debugstr_w(name), mask, unk, ti);
696

697 698 699 700 701 702 703 704 705 706 707 708 709
    item = host_get_named_item(This, name);
    if (!item)
        return TYPE_E_ELEMENTNOTFOUND;

    if (mask != SCRIPTINFO_IUNKNOWN) {
        FIXME("mask %#x is not supported\n", mask);
        return E_NOTIMPL;
    }

    *unk = (IUnknown*)item->disp;
    IUnknown_AddRef(*unk);

    return S_OK;
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
}

static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface, BSTR *version)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    FIXME("(%p, %p)\n", This, version);

    return E_NOTIMPL;
}

static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface, const VARIANT *result,
    const EXCEPINFO *ei)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    FIXME("(%p, %s, %p)\n", This, debugstr_variant(result), ei);

    return E_NOTIMPL;
}

static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface, SCRIPTSTATE state)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    FIXME("(%p, %d)\n", This, state);

    return E_NOTIMPL;
}

static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface, IActiveScriptError *script_error)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

744
    TRACE("(%p, %p)\n", This, script_error);
745

746 747 748 749 750 751 752
    if (This->error)
    {
        IScriptError_Clear(&This->error->IScriptError_iface);
        IActiveScriptError_AddRef(script_error);
        This->error->object = script_error;
    }
    return S_FALSE;
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
}

static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    FIXME("(%p)\n", This);

    return E_NOTIMPL;
}

static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
{
    ScriptHost *This = impl_from_IActiveScriptSite(iface);

    FIXME("(%p)\n", This);

    return E_NOTIMPL;
}

static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
    ActiveScriptSite_QueryInterface,
    ActiveScriptSite_AddRef,
    ActiveScriptSite_Release,
    ActiveScriptSite_GetLCID,
    ActiveScriptSite_GetItemInfo,
    ActiveScriptSite_GetDocVersionString,
    ActiveScriptSite_OnScriptTerminate,
    ActiveScriptSite_OnStateChange,
    ActiveScriptSite_OnScriptError,
    ActiveScriptSite_OnEnterScript,
    ActiveScriptSite_OnLeaveScript
};

787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
/* IActiveScriptSiteWindow */
static HRESULT WINAPI ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow *iface, REFIID riid, void **obj)
{
    ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
    return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, obj);
}

static ULONG WINAPI ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow *iface)
{
    ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
    return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
}

static ULONG WINAPI ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow *iface)
{
    ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);
    return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
}

static HRESULT WINAPI ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow *iface, HWND *hwnd)
{
    ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);

810
    TRACE("(%p, %p)\n", This, hwnd);
811

812 813 814 815 816
    if (!hwnd) return E_POINTER;
    if (This->site_hwnd == ((HWND)-1)) return E_FAIL;

    *hwnd = This->site_hwnd;
    return S_OK;
817 818 819 820 821 822
}

static HRESULT WINAPI ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow *iface, BOOL enable)
{
    ScriptHost *This = impl_from_IActiveScriptSiteWindow(iface);

823
    FIXME("(%p, %d): stub\n", This, enable);
824

825
    return S_OK;
826 827 828 829 830 831 832 833 834 835
}

static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl = {
    ActiveScriptSiteWindow_QueryInterface,
    ActiveScriptSiteWindow_AddRef,
    ActiveScriptSiteWindow_Release,
    ActiveScriptSiteWindow_GetWindow,
    ActiveScriptSiteWindow_EnableModeless
};

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
/* IServiceProvider */
static HRESULT WINAPI ServiceProvider_QueryInterface(IServiceProvider *iface, REFIID riid, void **obj)
{
    ScriptHost *This = impl_from_IServiceProvider(iface);
    return IActiveScriptSite_QueryInterface(&This->IActiveScriptSite_iface, riid, obj);
}

static ULONG WINAPI ServiceProvider_AddRef(IServiceProvider *iface)
{
    ScriptHost *This = impl_from_IServiceProvider(iface);
    return IActiveScriptSite_AddRef(&This->IActiveScriptSite_iface);
}

static ULONG WINAPI ServiceProvider_Release(IServiceProvider *iface)
{
    ScriptHost *This = impl_from_IServiceProvider(iface);
    return IActiveScriptSite_Release(&This->IActiveScriptSite_iface);
}

static HRESULT WINAPI ServiceProvider_QueryService(IServiceProvider *iface, REFGUID service,
    REFIID riid, void **obj)
{
    ScriptHost *This = impl_from_IServiceProvider(iface);

    FIXME("(%p)->(%s %s %p)\n", This, debugstr_guid(service), debugstr_guid(riid), obj);

    return E_NOTIMPL;
}

static const IServiceProviderVtbl ServiceProviderVtbl = {
    ServiceProvider_QueryInterface,
    ServiceProvider_AddRef,
    ServiceProvider_Release,
    ServiceProvider_QueryService
};

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
static HRESULT WINAPI ScriptProcedure_QueryInterface(IScriptProcedure *iface, REFIID riid, void **ppv)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);

    if (IsEqualGUID(&IID_IDispatch, riid) || IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IScriptProcedure, riid))
    {
        *ppv = &This->IScriptProcedure_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ScriptProcedure_AddRef(IScriptProcedure *iface)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI ScriptProcedure_Release(IScriptProcedure *iface)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if (!ref)
    {
911
        list_remove(&This->entry);
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
        SysFreeString(This->name);
        heap_free(This);
    }
    return ref;
}

static HRESULT WINAPI ScriptProcedure_GetTypeInfoCount(IScriptProcedure *iface, UINT *pctinfo)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;
    return S_OK;
}

static HRESULT WINAPI ScriptProcedure_GetTypeInfo(IScriptProcedure *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    return get_typeinfo(IScriptProcedure_tid, ppTInfo);
}

static HRESULT WINAPI ScriptProcedure_GetIDsOfNames(IScriptProcedure *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);

    hr = get_typeinfo(IScriptProcedure_tid, &typeinfo);
    if (SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptProcedure_Invoke(IScriptProcedure *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IScriptProcedure_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags,
                              pDispParams, pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptProcedure_get_Name(IScriptProcedure *iface, BSTR *pbstrName)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);

983
    TRACE("(%p)->(%p)\n", This, pbstrName);
984

985 986 987 988
    if (!pbstrName) return E_POINTER;

    *pbstrName = SysAllocString(This->name);
    return *pbstrName ? S_OK : E_OUTOFMEMORY;
989 990 991 992 993 994
}

static HRESULT WINAPI ScriptProcedure_get_NumArgs(IScriptProcedure *iface, LONG *pcArgs)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);

995
    TRACE("(%p)->(%p)\n", This, pcArgs);
996

997 998 999 1000
    if (!pcArgs) return E_POINTER;

    *pcArgs = This->num_args;
    return S_OK;
1001 1002 1003 1004 1005 1006
}

static HRESULT WINAPI ScriptProcedure_get_HasReturnValue(IScriptProcedure *iface, VARIANT_BOOL *pfHasReturnValue)
{
    ScriptProcedure *This = impl_from_IScriptProcedure(iface);

1007
    TRACE("(%p)->(%p)\n", This, pfHasReturnValue);
1008

1009 1010 1011 1012
    if (!pfHasReturnValue) return E_POINTER;

    *pfHasReturnValue = (This->ret_type == VT_VOID) ? VARIANT_FALSE : VARIANT_TRUE;
    return S_OK;
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
}

static const IScriptProcedureVtbl ScriptProcedureVtbl = {
    ScriptProcedure_QueryInterface,
    ScriptProcedure_AddRef,
    ScriptProcedure_Release,
    ScriptProcedure_GetTypeInfoCount,
    ScriptProcedure_GetTypeInfo,
    ScriptProcedure_GetIDsOfNames,
    ScriptProcedure_Invoke,
    ScriptProcedure_get_Name,
    ScriptProcedure_get_NumArgs,
    ScriptProcedure_get_HasReturnValue
};

/* This function always releases the FUNCDESC passed in */
1029 1030
static HRESULT get_script_procedure(ScriptProcedureCollection *procedures, ITypeInfo *typeinfo,
        FUNCDESC *desc, IScriptProcedure **procedure)
1031
{
1032
    struct list *proc_list;
1033
    ScriptProcedure *proc;
1034
    ULONG hash;
1035 1036 1037 1038 1039 1040 1041
    HRESULT hr;
    BSTR str;
    UINT len;

    hr = ITypeInfo_GetNames(typeinfo, desc->memid, &str, 1, &len);
    if (FAILED(hr)) goto done;

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    len = SysStringLen(str);
    hash = LHashValOfNameSys(sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32, LOCALE_USER_DEFAULT, str);
    proc_list = &procedures->hash_table[hash % ARRAY_SIZE(procedures->hash_table)];

    /* Try to find it in the hash table */
    LIST_FOR_EACH_ENTRY(proc, proc_list, ScriptProcedure, entry)
    {
        if (proc->hash == hash && SysStringLen(proc->name) == len &&
            !memcmp(proc->name, str, len * sizeof(*str)))
        {
            SysFreeString(str);
            IScriptProcedure_AddRef(&proc->IScriptProcedure_iface);
            *procedure = &proc->IScriptProcedure_iface;
            goto done;
        }
    }

1059 1060 1061 1062 1063 1064 1065 1066 1067
    if (!(proc = heap_alloc(sizeof(*proc))))
    {
        hr = E_OUTOFMEMORY;
        SysFreeString(str);
        goto done;
    }

    proc->IScriptProcedure_iface.lpVtbl = &ScriptProcedureVtbl;
    proc->ref = 1;
1068
    proc->hash = hash;
1069
    proc->name = str;
1070
    proc->num_args = desc->cParams + desc->cParamsOpt;
1071
    proc->ret_type = desc->elemdescFunc.tdesc.vt;
1072
    list_add_tail(proc_list, &proc->entry);
1073 1074 1075 1076 1077 1078 1079 1080

    *procedure = &proc->IScriptProcedure_iface;

done:
    ITypeInfo_ReleaseFuncDesc(typeinfo, desc);
    return hr;
}

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 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 1130 1131 1132 1133 1134 1135 1136 1137 1138
static HRESULT WINAPI procedure_enum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);

    if (IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IEnumVARIANT, riid))
    {
        *ppv = &This->IEnumVARIANT_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI procedure_enum_AddRef(IEnumVARIANT *iface)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI procedure_enum_Release(IEnumVARIANT *iface)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if (!ref)
    {
        IScriptProcedureCollection_Release(&This->procedures->IScriptProcedureCollection_iface);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI procedure_enum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);
    FUNCDESC *desc;
    ITypeInfo *ti;
    UINT i, num;
    HRESULT hr;

    TRACE("(%p)->(%u %p %p)\n", This, celt, rgVar, pCeltFetched);

    if (!rgVar) return E_POINTER;
    if (!This->procedures->module->host) return E_FAIL;

1139
    hr = start_script(This->procedures->module);
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 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    if (FAILED(hr)) return hr;

    hr = get_script_typeinfo(This->procedures->module, &ti);
    if (FAILED(hr)) return hr;

    num = min(celt, This->count - This->pos);
    for (i = 0; i < num; i++)
    {
        hr = ITypeInfo_GetFuncDesc(ti, This->pos + i, &desc);
        if (FAILED(hr)) break;

        hr = get_script_procedure(This->procedures, ti, desc, (IScriptProcedure**)&V_DISPATCH(rgVar + i));
        if (FAILED(hr)) break;

        V_VT(rgVar + i) = VT_DISPATCH;
    }

    if (FAILED(hr))
    {
        while (i--)
            VariantClear(rgVar + i);
        if (pCeltFetched) *pCeltFetched = 0;
        return hr;
    }

    This->pos += i;

    if (pCeltFetched) *pCeltFetched = i;
    return i == celt ? S_OK : S_FALSE;
}

static HRESULT WINAPI procedure_enum_Skip(IEnumVARIANT *iface, ULONG celt)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);

    TRACE("(%p)->(%u)\n", This, celt);

    if (This->count - This->pos < celt)
    {
        This->pos = This->count;
        return S_FALSE;
    }
    This->pos += celt;
    return S_OK;
}

static HRESULT WINAPI procedure_enum_Reset(IEnumVARIANT *iface)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);

    TRACE("(%p)\n", This);

    This->pos = 0;
    return S_OK;
}

static HRESULT WINAPI procedure_enum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
{
    struct procedure_enum *This = procedure_enum_from_IEnumVARIANT(iface);
    struct procedure_enum *clone;

    TRACE("(%p)->(%p)\n", This, ppEnum);

    if (!ppEnum) return E_POINTER;

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

    *clone = *This;
    clone->ref = 1;
    IScriptProcedureCollection_AddRef(&This->procedures->IScriptProcedureCollection_iface);

    *ppEnum = &clone->IEnumVARIANT_iface;
    return S_OK;
}

static const IEnumVARIANTVtbl procedure_enum_vtbl = {
    procedure_enum_QueryInterface,
    procedure_enum_AddRef,
    procedure_enum_Release,
    procedure_enum_Next,
    procedure_enum_Skip,
    procedure_enum_Reset,
    procedure_enum_Clone
};

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
static HRESULT WINAPI ScriptProcedureCollection_QueryInterface(IScriptProcedureCollection *iface, REFIID riid, void **ppv)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);

    if (IsEqualGUID(&IID_IDispatch, riid) || IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IScriptProcedureCollection, riid))
    {
        *ppv = &This->IScriptProcedureCollection_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ScriptProcedureCollection_AddRef(IScriptProcedureCollection *iface)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI ScriptProcedureCollection_Release(IScriptProcedureCollection *iface)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
    LONG ref = InterlockedDecrement(&This->ref);
1260
    UINT i;
1261 1262 1263 1264 1265

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

    if (!ref)
    {
1266 1267 1268 1269
        /* Unlink any dangling items from the hash table */
        for (i = 0; i < ARRAY_SIZE(This->hash_table); i++)
            list_remove(&This->hash_table[i]);

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
        This->module->procedures = NULL;
        IScriptModule_Release(&This->module->IScriptModule_iface);
        heap_free(This);
    }
    return ref;
}

static HRESULT WINAPI ScriptProcedureCollection_GetTypeInfoCount(IScriptProcedureCollection *iface, UINT *pctinfo)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;
    return S_OK;
}

static HRESULT WINAPI ScriptProcedureCollection_GetTypeInfo(IScriptProcedureCollection *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    return get_typeinfo(IScriptProcedureCollection_tid, ppTInfo);
}

static HRESULT WINAPI ScriptProcedureCollection_GetIDsOfNames(IScriptProcedureCollection *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);

    hr = get_typeinfo(IScriptProcedureCollection_tid, &typeinfo);
    if (SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptProcedureCollection_Invoke(IScriptProcedureCollection *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IScriptProcedureCollection_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags,
                              pDispParams, pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptProcedureCollection_get__NewEnum(IScriptProcedureCollection *iface, IUnknown **ppenumProcedures)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
1341 1342 1343 1344 1345
    struct procedure_enum *proc_enum;
    TYPEATTR *attr;
    ITypeInfo *ti;
    UINT count;
    HRESULT hr;
1346

1347
    TRACE("(%p)->(%p)\n", This, ppenumProcedures);
1348

1349 1350 1351
    if (!ppenumProcedures) return E_POINTER;
    if (!This->module->host) return E_FAIL;

1352
    hr = start_script(This->module);
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    if (FAILED(hr)) return hr;

    hr = get_script_typeinfo(This->module, &ti);
    if (FAILED(hr)) return hr;

    hr = ITypeInfo_GetTypeAttr(ti, &attr);
    if (FAILED(hr)) return hr;

    count = attr->cFuncs;
    ITypeInfo_ReleaseTypeAttr(ti, attr);

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

    proc_enum->IEnumVARIANT_iface.lpVtbl = &procedure_enum_vtbl;
    proc_enum->ref = 1;
    proc_enum->pos = 0;
    proc_enum->count = count;
    proc_enum->procedures = This;
    IScriptProcedureCollection_AddRef(&This->IScriptProcedureCollection_iface);

    *ppenumProcedures = (IUnknown*)&proc_enum->IEnumVARIANT_iface;
    return S_OK;
1376 1377 1378 1379 1380 1381
}

static HRESULT WINAPI ScriptProcedureCollection_get_Item(IScriptProcedureCollection *iface, VARIANT index,
        IScriptProcedure **ppdispProcedure)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
1382 1383 1384
    ITypeInfo *typeinfo;
    FUNCDESC *desc;
    HRESULT hr;
1385

1386
    TRACE("(%p)->(%s %p)\n", This, wine_dbgstr_variant(&index), ppdispProcedure);
1387

1388 1389 1390
    if (!ppdispProcedure) return E_POINTER;
    if (!This->module->host) return E_FAIL;

1391
    hr = start_script(This->module);
1392 1393 1394 1395 1396 1397 1398
    if (FAILED(hr)) return hr;

    hr = get_script_typeinfo(This->module, &typeinfo);
    if (FAILED(hr)) return hr;

    if (V_VT(&index) == VT_BSTR)
    {
1399 1400
        struct list *proc_list;
        ScriptProcedure *proc;
1401 1402 1403 1404
        ITypeComp *comp;
        BINDPTR bindptr;
        DESCKIND kind;
        ULONG hash;
1405
        UINT len;
1406

1407
        len = SysStringLen(V_BSTR(&index));
1408
        hash = LHashValOfNameSys(sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32, LOCALE_USER_DEFAULT, V_BSTR(&index));
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
        proc_list = &This->hash_table[hash % ARRAY_SIZE(This->hash_table)];

        /* Try to find it in the hash table */
        LIST_FOR_EACH_ENTRY(proc, proc_list, ScriptProcedure, entry)
        {
            if (proc->hash == hash && SysStringLen(proc->name) == len &&
                !memcmp(proc->name, V_BSTR(&index), len * sizeof(WCHAR)))
            {
                IScriptProcedure_AddRef(&proc->IScriptProcedure_iface);
                *ppdispProcedure = &proc->IScriptProcedure_iface;
                return S_OK;
            }
        }
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431

        hr = get_script_typecomp(This->module, typeinfo, &comp);
        if (FAILED(hr)) return hr;

        hr = ITypeComp_Bind(comp, V_BSTR(&index), hash, INVOKE_FUNC, &typeinfo, &kind, &bindptr);
        if (FAILED(hr)) return hr;

        switch (kind)
        {
        case DESCKIND_FUNCDESC:
1432
            hr = get_script_procedure(This, typeinfo, bindptr.lpfuncdesc, ppdispProcedure);
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
            ITypeInfo_Release(typeinfo);
            return hr;
        case DESCKIND_IMPLICITAPPOBJ:
        case DESCKIND_VARDESC:
            ITypeInfo_ReleaseVarDesc(typeinfo, bindptr.lpvardesc);
            ITypeInfo_Release(typeinfo);
            break;
        case DESCKIND_TYPECOMP:
            ITypeComp_Release(bindptr.lptcomp);
            break;
        default:
            break;
        }
        return CTL_E_ILLEGALFUNCTIONCALL;
    }

    hr = VariantChangeType(&index, &index, 0, VT_INT);
    if (FAILED(hr)) return hr;
    if (V_INT(&index) <= 0) return 0x800a0009;

    hr = ITypeInfo_GetFuncDesc(typeinfo, V_INT(&index) - 1, &desc);
    if (FAILED(hr)) return hr;

1456
    return get_script_procedure(This, typeinfo, desc, ppdispProcedure);
1457 1458 1459 1460 1461
}

static HRESULT WINAPI ScriptProcedureCollection_get_Count(IScriptProcedureCollection *iface, LONG *plCount)
{
    ScriptProcedureCollection *This = impl_from_IScriptProcedureCollection(iface);
1462 1463 1464
    TYPEATTR *attr;
    ITypeInfo *ti;
    HRESULT hr;
1465

1466
    TRACE("(%p)->(%p)\n", This, plCount);
1467

1468 1469 1470 1471 1472
    if (!plCount) return E_POINTER;
    if (!This->module->host) return E_FAIL;

    if (This->count == -1)
    {
1473 1474 1475
        hr = start_script(This->module);
        if (FAILED(hr)) return hr;

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
        hr = get_script_typeinfo(This->module, &ti);
        if (FAILED(hr)) return hr;

        hr = ITypeInfo_GetTypeAttr(ti, &attr);
        if (FAILED(hr)) return hr;

        This->count = attr->cFuncs;
        ITypeInfo_ReleaseTypeAttr(ti, attr);
    }

    *plCount = This->count;
    return S_OK;
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
}

static const IScriptProcedureCollectionVtbl ScriptProcedureCollectionVtbl = {
    ScriptProcedureCollection_QueryInterface,
    ScriptProcedureCollection_AddRef,
    ScriptProcedureCollection_Release,
    ScriptProcedureCollection_GetTypeInfoCount,
    ScriptProcedureCollection_GetTypeInfo,
    ScriptProcedureCollection_GetIDsOfNames,
    ScriptProcedureCollection_Invoke,
    ScriptProcedureCollection_get__NewEnum,
    ScriptProcedureCollection_get_Item,
    ScriptProcedureCollection_get_Count
};

1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
static void detach_script_host(ScriptHost *host)
{
    if (--host->module_count)
        return;

    if (host->script) {
        IActiveScript_Close(host->script);
        IActiveScript_Release(host->script);
    }

    if (host->parse)
        IActiveScriptParse_Release(host->parse);

1516 1517 1518
    if (host->error)
        IScriptError_Release(&host->error->IScriptError_iface);

1519
    host->parse = NULL;
1520
    host->error = NULL;
1521 1522 1523
    host->script = NULL;
}

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
static void detach_module(ScriptModule *module)
{
    ScriptHost *host = module->host;

    if (host) {
        module->host = NULL;
        detach_script_host(host);
        IActiveScriptSite_Release(&host->IActiveScriptSite_iface);
    }
}

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
static HRESULT WINAPI ScriptModule_QueryInterface(IScriptModule *iface, REFIID riid, void **ppv)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

    if (IsEqualGUID(&IID_IDispatch, riid) || IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IScriptModule, riid))
    {
        *ppv = &This->IScriptModule_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ScriptModule_AddRef(IScriptModule *iface)
{
    ScriptModule *This = impl_from_IScriptModule(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI ScriptModule_Release(IScriptModule *iface)
{
    ScriptModule *This = impl_from_IScriptModule(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if (!ref)
1573
    {
1574
        detach_module(This);
1575
        SysFreeString(This->name);
1576
        uncache_module_objects(This);
1577
        heap_free(This);
1578
    }
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 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 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

    return ref;
}

static HRESULT WINAPI ScriptModule_GetTypeInfoCount(IScriptModule *iface, UINT *pctinfo)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;
    return S_OK;
}

static HRESULT WINAPI ScriptModule_GetTypeInfo(IScriptModule *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    return get_typeinfo(IScriptModule_tid, ppTInfo);
}

static HRESULT WINAPI ScriptModule_GetIDsOfNames(IScriptModule *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ScriptModule *This = impl_from_IScriptModule(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);

    hr = get_typeinfo(IScriptModule_tid, &typeinfo);
    if (SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptModule_Invoke(IScriptModule *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    ScriptModule *This = impl_from_IScriptModule(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IScriptModule_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags,
                              pDispParams, pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptModule_get_Name(IScriptModule *iface, BSTR *pbstrName)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

1648
    TRACE("(%p)->(%p)\n", This, pbstrName);
1649

1650 1651 1652 1653 1654
    if (!pbstrName) return E_POINTER;
    if (!This->host) return E_FAIL;

    *pbstrName = SysAllocString(This->name ? This->name : L"Global");
    return *pbstrName ? S_OK : E_OUTOFMEMORY;
1655 1656 1657 1658 1659
}

static HRESULT WINAPI ScriptModule_get_CodeObject(IScriptModule *iface, IDispatch **ppdispObject)
{
    ScriptModule *This = impl_from_IScriptModule(iface);
1660
    HRESULT hr;
1661

1662
    TRACE("(%p)->(%p)\n", This, ppdispObject);
1663

1664 1665
    if (!This->host) return E_FAIL;

1666
    hr = start_script(This);
1667 1668 1669 1670 1671 1672 1673
    if (FAILED(hr)) return hr;

    hr = get_script_dispatch(This, ppdispObject);
    if (FAILED(hr)) return hr;

    IDispatch_AddRef(*ppdispObject);
    return hr;
1674 1675 1676 1677 1678 1679
}

static HRESULT WINAPI ScriptModule_get_Procedures(IScriptModule *iface, IScriptProcedureCollection **ppdispProcedures)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

1680
    TRACE("(%p)->(%p)\n", This, ppdispProcedures);
1681

1682 1683 1684 1685 1686 1687 1688 1689
    if (!This->host)
        return E_FAIL;

    if (This->procedures)
        IScriptProcedureCollection_AddRef(&This->procedures->IScriptProcedureCollection_iface);
    else
    {
        ScriptProcedureCollection *procs;
1690
        UINT i;
1691 1692 1693 1694 1695 1696

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

        procs->IScriptProcedureCollection_iface.lpVtbl = &ScriptProcedureCollectionVtbl;
        procs->ref = 1;
1697
        procs->count = -1;
1698
        procs->module = This;
1699 1700 1701
        for (i = 0; i < ARRAY_SIZE(procs->hash_table); i++)
            list_init(&procs->hash_table[i]);

1702 1703 1704 1705 1706 1707
        This->procedures = procs;
        IScriptModule_AddRef(&This->IScriptModule_iface);
    }

    *ppdispProcedures = &This->procedures->IScriptProcedureCollection_iface;
    return S_OK;
1708 1709 1710 1711 1712 1713
}

static HRESULT WINAPI ScriptModule_AddCode(IScriptModule *iface, BSTR code)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

1714
    TRACE("(%p)->(%s)\n", This, debugstr_w(code));
1715

1716 1717 1718 1719
    if (!This->host)
        return E_FAIL;

    return parse_script_text(This, code, SCRIPTTEXT_ISVISIBLE, NULL);
1720 1721 1722 1723 1724 1725
}

static HRESULT WINAPI ScriptModule_Eval(IScriptModule *iface, BSTR expression, VARIANT *res)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

1726
    TRACE("(%p)->(%s, %p)\n", This, debugstr_w(expression), res);
1727

1728 1729 1730 1731 1732 1733 1734
    if (!res)
        return E_POINTER;
    V_VT(res) = VT_EMPTY;
    if (!This->host)
        return E_FAIL;

    return parse_script_text(This, expression, SCRIPTTEXT_ISEXPRESSION, res);
1735 1736 1737 1738 1739 1740
}

static HRESULT WINAPI ScriptModule_ExecuteStatement(IScriptModule *iface, BSTR statement)
{
    ScriptModule *This = impl_from_IScriptModule(iface);

1741 1742 1743 1744
    TRACE("(%p)->(%s)\n", This, debugstr_w(statement));

    if (!This->host)
        return E_FAIL;
1745

1746
    return parse_script_text(This, statement, 0, NULL);
1747 1748 1749 1750 1751
}

static HRESULT WINAPI ScriptModule_Run(IScriptModule *iface, BSTR procedure_name, SAFEARRAY **parameters, VARIANT *res)
{
    ScriptModule *This = impl_from_IScriptModule(iface);
1752
    SAFEARRAY *sa;
1753

1754
    TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(procedure_name), parameters, res);
1755

1756 1757 1758 1759 1760 1761 1762 1763 1764
    if (!parameters || !res) return E_POINTER;
    if (!(sa = *parameters)) return E_POINTER;

    V_VT(res) = VT_EMPTY;
    if (sa->cDims == 0) return DISP_E_BADINDEX;
    if (!(sa->fFeatures & FADF_VARIANT)) return DISP_E_BADVARTYPE;
    if (!This->host) return E_FAIL;

    return run_procedure(This, procedure_name, sa, res);
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
}

static const IScriptModuleVtbl ScriptModuleVtbl = {
    ScriptModule_QueryInterface,
    ScriptModule_AddRef,
    ScriptModule_Release,
    ScriptModule_GetTypeInfoCount,
    ScriptModule_GetTypeInfo,
    ScriptModule_GetIDsOfNames,
    ScriptModule_Invoke,
    ScriptModule_get_Name,
    ScriptModule_get_CodeObject,
    ScriptModule_get_Procedures,
    ScriptModule_AddCode,
    ScriptModule_Eval,
    ScriptModule_ExecuteStatement,
    ScriptModule_Run
};

1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
static HRESULT WINAPI module_enum_QueryInterface(IEnumVARIANT *iface, REFIID riid, void **ppv)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);

    if (IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IEnumVARIANT, riid))
    {
        *ppv = &This->IEnumVARIANT_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI module_enum_AddRef(IEnumVARIANT *iface)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI module_enum_Release(IEnumVARIANT *iface)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if (!ref)
    {
        IActiveScriptSite_Release(&This->host->IActiveScriptSite_iface);
        IScriptControl_Release(&This->control->IScriptControl_iface);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI module_enum_Next(IEnumVARIANT *iface, ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);
    unsigned int i, num;

    TRACE("(%p)->(%u %p %p)\n", This, celt, rgVar, pCeltFetched);

    if (!rgVar) return E_POINTER;
    if (This->host != This->control->host) return E_FAIL;

    num = min(celt, This->host->module_count - This->pos);
    for (i = 0; i < num; i++)
    {
        V_VT(rgVar + i) = VT_DISPATCH;
        V_DISPATCH(rgVar + i) = (IDispatch*)(&This->control->modules[This->pos++]->IScriptModule_iface);
        IDispatch_AddRef(V_DISPATCH(rgVar + i));
    }

    if (pCeltFetched) *pCeltFetched = i;
    return i == celt ? S_OK : S_FALSE;
}

static HRESULT WINAPI module_enum_Skip(IEnumVARIANT *iface, ULONG celt)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);

    TRACE("(%p)->(%u)\n", This, celt);

    if (This->host != This->control->host) return E_FAIL;

    if (This->host->module_count - This->pos < celt)
    {
        This->pos = This->host->module_count;
        return S_FALSE;
    }
    This->pos += celt;
    return S_OK;
}

static HRESULT WINAPI module_enum_Reset(IEnumVARIANT *iface)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);

    TRACE("(%p)\n", This);

    if (This->host != This->control->host) return E_FAIL;

    This->pos = 0;
    return S_OK;
}

static HRESULT WINAPI module_enum_Clone(IEnumVARIANT *iface, IEnumVARIANT **ppEnum)
{
    struct module_enum *This = module_enum_from_IEnumVARIANT(iface);
    struct module_enum *clone;

    TRACE("(%p)->(%p)\n", This, ppEnum);

    if (!ppEnum) return E_POINTER;
    if (This->host != This->control->host) return E_FAIL;

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

    *clone = *This;
    clone->ref = 1;
    IActiveScriptSite_AddRef(&This->host->IActiveScriptSite_iface);
    IScriptControl_AddRef(&This->control->IScriptControl_iface);

    *ppEnum = &clone->IEnumVARIANT_iface;
    return S_OK;
}

static const IEnumVARIANTVtbl module_enum_vtbl = {
    module_enum_QueryInterface,
    module_enum_AddRef,
    module_enum_Release,
    module_enum_Next,
    module_enum_Skip,
    module_enum_Reset,
    module_enum_Clone
};

1913
static ScriptModule *create_module(ScriptHost *host, BSTR name)
1914 1915 1916
{
    ScriptModule *module;

1917
    if (!(module = heap_alloc_zero(sizeof(*module)))) return NULL;
1918 1919 1920

    module->IScriptModule_iface.lpVtbl = &ScriptModuleVtbl;
    module->ref = 1;
1921 1922 1923 1924 1925
    if (name && !(module->name = SysAllocString(name)))
    {
        heap_free(module);
        return NULL;
    }
1926 1927
    module->host = host;
    IActiveScriptSite_AddRef(&host->IActiveScriptSite_iface);
1928 1929 1930
    return module;
}

1931
static void release_modules(ScriptControl *control, BOOL force_detach)
1932
{
1933
    unsigned int i, module_count = control->host->module_count;
1934

1935 1936
    for (i = 0; i < module_count; i++) {
        if (force_detach) detach_module(control->modules[i]);
1937
        IScriptModule_Release(&control->modules[i]->IScriptModule_iface);
1938
    }
1939 1940 1941 1942

    heap_free(control->modules);
}

1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957
static ScriptModule *find_module(ScriptControl *control, BSTR name)
{
    unsigned int i;

    if (!wcsicmp(name, L"Global"))
        return control->modules[0];

    for (i = 1; i < control->host->module_count; i++)
    {
        if (!wcsicmp(name, control->modules[i]->name))
            return control->modules[i];
    }
    return NULL;
}

1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
static HRESULT WINAPI ScriptModuleCollection_QueryInterface(IScriptModuleCollection *iface, REFIID riid, void **ppv)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);

    if (IsEqualGUID(&IID_IDispatch, riid) || IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IScriptModuleCollection, riid))
    {
        *ppv = &This->IScriptModuleCollection_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ScriptModuleCollection_AddRef(IScriptModuleCollection *iface)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI ScriptModuleCollection_Release(IScriptModuleCollection *iface)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI ScriptModuleCollection_GetTypeInfoCount(IScriptModuleCollection *iface, UINT *pctinfo)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;
    return S_OK;
}

static HRESULT WINAPI ScriptModuleCollection_GetTypeInfo(IScriptModuleCollection *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    return get_typeinfo(IScriptModuleCollection_tid, ppTInfo);
}

static HRESULT WINAPI ScriptModuleCollection_GetIDsOfNames(IScriptModuleCollection *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);

    hr = get_typeinfo(IScriptModuleCollection_tid, &typeinfo);
    if (SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptModuleCollection_Invoke(IScriptModuleCollection *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IScriptModuleCollection_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags,
                              pDispParams, pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptModuleCollection_get__NewEnum(IScriptModuleCollection *iface, IUnknown **ppenumContexts)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
2054
    struct module_enum *module_enum;
2055

2056
    TRACE("(%p)->(%p)\n", This, ppenumContexts);
2057

2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
    if (!ppenumContexts) return E_POINTER;
    if (!This->host) return E_FAIL;

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

    module_enum->IEnumVARIANT_iface.lpVtbl = &module_enum_vtbl;
    module_enum->ref = 1;
    module_enum->pos = 0;
    module_enum->host = This->host;
    module_enum->control = This;
    IActiveScriptSite_AddRef(&This->host->IActiveScriptSite_iface);
    IScriptControl_AddRef(&This->IScriptControl_iface);

    *ppenumContexts = (IUnknown*)&module_enum->IEnumVARIANT_iface;
    return S_OK;
2074 2075 2076 2077 2078 2079
}

static HRESULT WINAPI ScriptModuleCollection_get_Item(IScriptModuleCollection *iface, VARIANT index,
        IScriptModule **ppmod)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
2080 2081 2082
    ScriptModule *module;
    unsigned int i;
    HRESULT hr;
2083

2084
    TRACE("(%p)->(%s %p)\n", This, wine_dbgstr_variant(&index), ppmod);
2085

2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
    if (!ppmod) return E_POINTER;
    if (!This->host) return E_FAIL;

    if (V_VT(&index) == VT_BSTR)
    {
        module = find_module(This, V_BSTR(&index));
        if (!module) return CTL_E_ILLEGALFUNCTIONCALL;
    }
    else
    {
        hr = VariantChangeType(&index, &index, 0, VT_INT);
        if (FAILED(hr)) return hr;

        i = V_INT(&index) - 1;
        if (i >= This->host->module_count) return 0x800a0009;

        module = This->modules[i];
    }

    *ppmod = &module->IScriptModule_iface;
    IScriptModule_AddRef(*ppmod);
    return S_OK;
2108 2109 2110 2111 2112 2113
}

static HRESULT WINAPI ScriptModuleCollection_get_Count(IScriptModuleCollection *iface, LONG *plCount)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);

2114
    TRACE("(%p)->(%p)\n", This, plCount);
2115

2116
    if (!plCount) return E_POINTER;
2117
    if (!This->host) return E_FAIL;
2118

2119
    *plCount = This->host->module_count;
2120
    return S_OK;
2121 2122 2123 2124 2125 2126
}

static HRESULT WINAPI ScriptModuleCollection_Add(IScriptModuleCollection *iface, BSTR name,
        VARIANT *object, IScriptModule **ppmod)
{
    ScriptControl *This = impl_from_IScriptModuleCollection(iface);
2127 2128 2129
    ScriptModule *module, **modules;
    ScriptHost *host = This->host;
    HRESULT hr;
2130

2131
    TRACE("(%p)->(%s %s %p)\n", This, wine_dbgstr_w(name), wine_dbgstr_variant(object), ppmod);
2132

2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
    if (!ppmod) return E_POINTER;
    if (!name || V_VT(object) != VT_DISPATCH) return E_INVALIDARG;
    if (!host) return E_FAIL;
    if (find_module(This, name)) return E_INVALIDARG;

    /* See if we need to grow the array */
    if (is_power_of_2(host->module_count))
    {
        modules = heap_realloc(This->modules, host->module_count * 2 * sizeof(*This->modules));
        if (!modules) return E_OUTOFMEMORY;
        This->modules = modules;
    }

    if (!(module = create_module(host, name)))
        return E_OUTOFMEMORY;

    /* If no object, Windows only calls AddNamedItem without adding a NULL object */
    if (V_DISPATCH(object))
        hr = add_script_object(host, name, V_DISPATCH(object), 0);
    else
        hr = IActiveScript_AddNamedItem(host->script, name, SCRIPTITEM_CODEONLY);

    if (FAILED(hr))
    {
        IScriptModule_Release(&module->IScriptModule_iface);
        return hr;
    }
    This->modules[host->module_count++] = module;

    *ppmod = &module->IScriptModule_iface;
    IScriptModule_AddRef(*ppmod);
    return S_OK;
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
}

static const IScriptModuleCollectionVtbl ScriptModuleCollectionVtbl = {
    ScriptModuleCollection_QueryInterface,
    ScriptModuleCollection_AddRef,
    ScriptModuleCollection_Release,
    ScriptModuleCollection_GetTypeInfoCount,
    ScriptModuleCollection_GetTypeInfo,
    ScriptModuleCollection_GetIDsOfNames,
    ScriptModuleCollection_Invoke,
    ScriptModuleCollection_get__NewEnum,
    ScriptModuleCollection_get_Item,
    ScriptModuleCollection_get_Count,
    ScriptModuleCollection_Add
};

2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
static void fill_error_info(ScriptError *error)
{
    EXCEPINFO info;

    if (error->info_filled) return;
    error->info_filled = TRUE;

    if (!error->object)
        return;
    if (FAILED(IActiveScriptError_GetExceptionInfo(error->object, &info)))
        return;
    if (info.pfnDeferredFillIn)
        info.pfnDeferredFillIn(&info);

    error->number = info.scode;
2196
    error->source = info.bstrSource;
2197
    error->desc = info.bstrDescription;
2198
    error->help_file = info.bstrHelpFile;
2199
    error->help_context = info.dwHelpContext;
2200 2201
}

2202 2203 2204 2205 2206 2207 2208 2209 2210
static void fill_error_text(ScriptError *error)
{
    if (error->text_filled) return;
    error->text_filled = TRUE;

    if (error->object)
        IActiveScriptError_GetSourceLineText(error->object, &error->text);
}

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
static void fill_error_pos(ScriptError *error)
{
    DWORD context;
    LONG column;
    ULONG line;

    if (error->pos_filled) return;
    error->pos_filled = TRUE;

    if (!error->object)
        return;
    if (FAILED(IActiveScriptError_GetSourcePosition(error->object, &context, &line, &column)))
        return;

    error->line = line;
2226
    error->column = column;
2227 2228
}

2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 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
static HRESULT WINAPI ScriptError_QueryInterface(IScriptError *iface, REFIID riid, void **ppv)
{
    ScriptError *This = impl_from_IScriptError(iface);

    if (IsEqualGUID(&IID_IDispatch, riid) || IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IScriptError, riid))
    {
        *ppv = &This->IScriptError_iface;
    }
    else
    {
        WARN("unsupported interface: (%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ScriptError_AddRef(IScriptError *iface)
{
    ScriptError *This = impl_from_IScriptError(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI ScriptError_Release(IScriptError *iface)
{
    ScriptError *This = impl_from_IScriptError(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

    if (!ref)
    {
2268
        IScriptError_Clear(&This->IScriptError_iface);
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI ScriptError_GetTypeInfoCount(IScriptError *iface, UINT *pctinfo)
{
    ScriptError *This = impl_from_IScriptError(iface);

    TRACE("(%p)->(%p)\n", This, pctinfo);

    *pctinfo = 1;
    return S_OK;
}

static HRESULT WINAPI ScriptError_GetTypeInfo(IScriptError *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    ScriptError *This = impl_from_IScriptError(iface);

    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);

    return get_typeinfo(IScriptError_tid, ppTInfo);
}

static HRESULT WINAPI ScriptError_GetIDsOfNames(IScriptError *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ScriptError *This = impl_from_IScriptError(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);

    hr = get_typeinfo(IScriptError_tid, &typeinfo);
    if (SUCCEEDED(hr))
    {
        hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptError_Invoke(IScriptError *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    ScriptError *This = impl_from_IScriptError(iface);
    ITypeInfo *typeinfo;
    HRESULT hr;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(IScriptError_tid, &typeinfo);
    if(SUCCEEDED(hr))
    {
        hr = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags,
                              pDispParams, pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hr;
}

static HRESULT WINAPI ScriptError_get_Number(IScriptError *iface, LONG *plNumber)
{
    ScriptError *This = impl_from_IScriptError(iface);

2340
    TRACE("(%p)->(%p)\n", This, plNumber);
2341

2342 2343 2344
    fill_error_info(This);
    *plNumber = This->number;
    return S_OK;
2345 2346 2347 2348 2349 2350
}

static HRESULT WINAPI ScriptError_get_Source(IScriptError *iface, BSTR *pbstrSource)
{
    ScriptError *This = impl_from_IScriptError(iface);

2351
    TRACE("(%p)->(%p)\n", This, pbstrSource);
2352

2353 2354 2355
    fill_error_info(This);
    *pbstrSource = SysAllocString(This->source);
    return S_OK;
2356 2357 2358 2359 2360 2361
}

static HRESULT WINAPI ScriptError_get_Description(IScriptError *iface, BSTR *pbstrDescription)
{
    ScriptError *This = impl_from_IScriptError(iface);

2362
    TRACE("(%p)->(%p)\n", This, pbstrDescription);
2363

2364 2365 2366
    fill_error_info(This);
    *pbstrDescription = SysAllocString(This->desc);
    return S_OK;
2367 2368 2369 2370 2371 2372
}

static HRESULT WINAPI ScriptError_get_HelpFile(IScriptError *iface, BSTR *pbstrHelpFile)
{
    ScriptError *This = impl_from_IScriptError(iface);

2373
    TRACE("(%p)->(%p)\n", This, pbstrHelpFile);
2374

2375 2376 2377
    fill_error_info(This);
    *pbstrHelpFile = SysAllocString(This->help_file);
    return S_OK;
2378 2379 2380 2381 2382 2383
}

static HRESULT WINAPI ScriptError_get_HelpContext(IScriptError *iface, LONG *plHelpContext)
{
    ScriptError *This = impl_from_IScriptError(iface);

2384
    TRACE("(%p)->(%p)\n", This, plHelpContext);
2385

2386 2387 2388
    fill_error_info(This);
    *plHelpContext = This->help_context;
    return S_OK;
2389 2390 2391 2392 2393 2394
}

static HRESULT WINAPI ScriptError_get_Text(IScriptError *iface, BSTR *pbstrText)
{
    ScriptError *This = impl_from_IScriptError(iface);

2395
    TRACE("(%p)->(%p)\n", This, pbstrText);
2396

2397 2398 2399
    fill_error_text(This);
    *pbstrText = SysAllocString(This->text);
    return S_OK;
2400 2401 2402 2403 2404 2405
}

static HRESULT WINAPI ScriptError_get_Line(IScriptError *iface, LONG *plLine)
{
    ScriptError *This = impl_from_IScriptError(iface);

2406
    TRACE("(%p)->(%p)\n", This, plLine);
2407

2408 2409 2410
    fill_error_pos(This);
    *plLine = This->line;
    return S_OK;
2411 2412 2413 2414 2415 2416
}

static HRESULT WINAPI ScriptError_get_Column(IScriptError *iface, LONG *plColumn)
{
    ScriptError *This = impl_from_IScriptError(iface);

2417
    TRACE("(%p)->(%p)\n", This, plColumn);
2418

2419 2420 2421
    fill_error_pos(This);
    *plColumn = This->column;
    return S_OK;
2422 2423 2424 2425 2426 2427
}

static HRESULT WINAPI ScriptError_Clear(IScriptError *iface)
{
    ScriptError *This = impl_from_IScriptError(iface);

2428
    TRACE("(%p)->()\n", This);
2429

2430 2431 2432 2433 2434
    if (This->object)
    {
        IActiveScriptError_Release(This->object);
        This->object = NULL;
    }
2435
    SysFreeString(This->text);
2436
    SysFreeString(This->source);
2437
    SysFreeString(This->desc);
2438
    SysFreeString(This->help_file);
2439

2440
    This->number = 0;
2441
    This->text = NULL;
2442
    This->source = NULL;
2443
    This->desc = NULL;
2444
    This->help_file = NULL;
2445
    This->help_context = 0;
2446
    This->line = 0;
2447
    This->column = 0;
2448 2449

    This->info_filled = FALSE;
2450
    This->text_filled = FALSE;
2451
    This->pos_filled = FALSE;
2452
    return S_OK;
2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473
}

static const IScriptErrorVtbl ScriptErrorVtbl = {
    ScriptError_QueryInterface,
    ScriptError_AddRef,
    ScriptError_Release,
    ScriptError_GetTypeInfoCount,
    ScriptError_GetTypeInfo,
    ScriptError_GetIDsOfNames,
    ScriptError_Invoke,
    ScriptError_get_Number,
    ScriptError_get_Source,
    ScriptError_get_Description,
    ScriptError_get_HelpFile,
    ScriptError_get_HelpContext,
    ScriptError_get_Text,
    ScriptError_get_Line,
    ScriptError_get_Column,
    ScriptError_Clear
};

2474
static HRESULT set_safety_opts(IActiveScript *script, VARIANT_BOOL use_safe_subset)
2475 2476
{
    IObjectSafety *objsafety;
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
    HRESULT hr;

    hr = IActiveScript_QueryInterface(script, &IID_IObjectSafety, (void**)&objsafety);
    if (FAILED(hr)) {
        FIXME("Could not get IObjectSafety, %#x\n", hr);
        return hr;
    }

    hr = IObjectSafety_SetInterfaceSafetyOptions(objsafety, &IID_IActiveScriptParse, INTERFACESAFE_FOR_UNTRUSTED_DATA,
                                                 use_safe_subset ? INTERFACESAFE_FOR_UNTRUSTED_DATA : 0);
    IObjectSafety_Release(objsafety);
    if (FAILED(hr)) {
        FIXME("SetInterfaceSafetyOptions failed, %#x\n", hr);
        return hr;
    }

    return hr;
}

static HRESULT init_script_host(ScriptControl *control, const CLSID *clsid, ScriptHost **ret)
{
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
    ScriptHost *host;
    HRESULT hr;

    *ret = NULL;

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

    host->IActiveScriptSite_iface.lpVtbl = &ActiveScriptSiteVtbl;
2508
    host->IActiveScriptSiteWindow_iface.lpVtbl = &ActiveScriptSiteWindowVtbl;
2509
    host->IServiceProvider_iface.lpVtbl = &ServiceProviderVtbl;
2510 2511 2512 2513
    host->ref = 1;
    host->script = NULL;
    host->parse = NULL;
    host->clsid = *clsid;
2514
    host->module_count = 1;
2515
    list_init(&host->named_items);
2516 2517 2518 2519 2520 2521 2522 2523

    hr = CoCreateInstance(&host->clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
            &IID_IActiveScript, (void**)&host->script);
    if (FAILED(hr)) {
        WARN("Failed to create an instance for %s, %#x\n", debugstr_guid(clsid), hr);
        goto failed;
    }

2524 2525
    hr = set_safety_opts(host->script, control->use_safe_subset);
    if (FAILED(hr)) goto failed;
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543

    hr = IActiveScript_SetScriptSite(host->script, &host->IActiveScriptSite_iface);
    if (FAILED(hr)) {
        WARN("SetScriptSite failed, %#x\n", hr);
        goto failed;
    }

    hr = IActiveScript_QueryInterface(host->script, &IID_IActiveScriptParse, (void**)&host->parse);
    if (FAILED(hr)) {
        WARN("Failed to get IActiveScriptParse, %#x\n", hr);
        goto failed;
    }

    hr = IActiveScriptParse_InitNew(host->parse);
    if (FAILED(hr)) {
        WARN("InitNew failed, %#x\n", hr);
        goto failed;
    }
2544
    host->script_state = SCRIPTSTATE_INITIALIZED;
2545
    host->site_hwnd = control->allow_ui ? control->site_hwnd : ((HWND)-1);
2546 2547
    host->error = control->error;
    IScriptError_AddRef(&host->error->IScriptError_iface);
2548 2549 2550 2551 2552

    *ret = host;
    return S_OK;

failed:
2553
    detach_script_host(host);
2554 2555 2556
    return hr;
}

2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
static HRESULT WINAPI ScriptControl_QueryInterface(IScriptControl *iface, REFIID riid, void **ppv)
{
    ScriptControl *This = impl_from_IScriptControl(iface);

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IScriptControl_iface;
    }else if(IsEqualGUID(&IID_IDispatch, riid)) {
        TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
        *ppv = &This->IScriptControl_iface;
    }else if(IsEqualGUID(&IID_IScriptControl, riid)) {
        TRACE("(%p)->(IID_IScriptControl %p)\n", This, ppv);
        *ppv = &This->IScriptControl_iface;
2570
    }else if(IsEqualGUID(&IID_IOleObject, riid)) {
2571
        TRACE("(%p)->(IID_IOleObject %p)\n", This, ppv);
2572
        *ppv = &This->IOleObject_iface;
2573
    }else if(IsEqualGUID(&IID_IPersistStreamInit, riid)) {
2574
        TRACE("(%p)->(IID_IPersistStreamInit %p)\n", This, ppv);
2575 2576
        *ppv = &This->IPersistStreamInit_iface;
    }else if(IsEqualGUID(&IID_IPersist, riid)) {
2577
        TRACE("(%p)->(IID_IPersist %p)\n", This, ppv);
2578
        *ppv = &This->IPersistStreamInit_iface;
2579 2580 2581
    }else if(IsEqualGUID(&IID_IOleControl, riid)) {
        TRACE("(%p)->(IID_IOleControl %p)\n", This, ppv);
        *ppv = &This->IOleControl_iface;
2582 2583 2584
    }else if(IsEqualGUID(&IID_IQuickActivate, riid)) {
        TRACE("(%p)->(IID_IQuickActivate %p)\n", This, ppv);
        *ppv = &This->IQuickActivate_iface;
2585 2586
    }else if(IsEqualGUID(&IID_IViewObject, riid)) {
        TRACE("(%p)->(IID_IViewObject %p)\n", This, ppv);
2587 2588 2589 2590 2591 2592 2593
        *ppv = &This->IViewObjectEx_iface;
    }else if(IsEqualGUID(&IID_IViewObject2, riid)) {
        TRACE("(%p)->(IID_IViewObject2 %p)\n", This, ppv);
        *ppv = &This->IViewObjectEx_iface;
    }else if(IsEqualGUID(&IID_IViewObjectEx, riid)) {
        TRACE("(%p)->(IID_IViewObjectEx %p)\n", This, ppv);
        *ppv = &This->IViewObjectEx_iface;
2594 2595 2596
    }else if(IsEqualGUID(&IID_IPointerInactive, riid)) {
        TRACE("(%p)->(IID_IPointerInactive %p)\n", This, ppv);
        *ppv = &This->IPointerInactive_iface;
2597 2598 2599
    }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
        TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
        *ppv = &This->IConnectionPointContainer_iface;
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
    }else {
        FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ScriptControl_AddRef(IScriptControl *iface)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
    LONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

static ULONG WINAPI ScriptControl_Release(IScriptControl *iface)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
    LONG ref = InterlockedDecrement(&This->ref);

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

2627 2628 2629
    if(!ref) {
        if (This->site)
            IOleClientSite_Release(This->site);
2630
        if (This->host)
2631
        {
2632
            release_modules(This, FALSE);
2633
            IActiveScriptSite_Release(&This->host->IActiveScriptSite_iface);
2634
        }
2635
        IScriptError_Release(&This->error->IScriptError_iface);
2636
        heap_free(This);
2637
    }
2638 2639 2640 2641 2642 2643 2644

    return ref;
}

static HRESULT WINAPI ScriptControl_GetTypeInfoCount(IScriptControl *iface, UINT *pctinfo)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2645 2646 2647
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
2648 2649 2650 2651 2652 2653
}

static HRESULT WINAPI ScriptControl_GetTypeInfo(IScriptControl *iface, UINT iTInfo,
        LCID lcid, ITypeInfo **ppTInfo)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2654 2655
    TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
    return get_typeinfo(IScriptControl_tid, ppTInfo);
2656 2657 2658 2659 2660 2661
}

static HRESULT WINAPI ScriptControl_GetIDsOfNames(IScriptControl *iface, REFIID riid,
        LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
    ITypeInfo *typeinfo;
    HRESULT hres;

    TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);

    hres = get_typeinfo(IScriptControl_tid, &typeinfo);
    if(SUCCEEDED(hres)) {
        hres = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
        ITypeInfo_Release(typeinfo);
    }

    return hres;
2674 2675 2676 2677 2678 2679 2680
}

static HRESULT WINAPI ScriptControl_Invoke(IScriptControl *iface, DISPID dispIdMember,
        REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
        EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
    ITypeInfo *typeinfo;
    HRESULT hres;

    TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hres = get_typeinfo(IScriptControl_tid, &typeinfo);
    if(SUCCEEDED(hres)) {
        hres = ITypeInfo_Invoke(typeinfo, iface, dispIdMember, wFlags,
                pDispParams, pVarResult, pExcepInfo, puArgErr);
        ITypeInfo_Release(typeinfo);
    }

    return hres;
2695 2696 2697 2698 2699
}

static HRESULT WINAPI ScriptControl_get_Language(IScriptControl *iface, BSTR *p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719
    LPOLESTR progidW;
    HRESULT hr;

    TRACE("(%p)->(%p)\n", This, p);

    if (!p)
        return E_POINTER;

    *p = NULL;

    if (!This->host)
        return S_OK;

    hr = ProgIDFromCLSID(&This->host->clsid, &progidW);
    if (FAILED(hr))
        return hr;

    *p = SysAllocString(progidW);
    CoTaskMemFree(progidW);
    return *p ? S_OK : E_OUTOFMEMORY;
2720 2721 2722 2723 2724
}

static HRESULT WINAPI ScriptControl_put_Language(IScriptControl *iface, BSTR language)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2725
    CLSID clsid;
2726
    HRESULT hres;
2727 2728 2729 2730 2731 2732 2733

    TRACE("(%p)->(%s)\n", This, debugstr_w(language));

    if (language && FAILED(CLSIDFromProgID(language, &clsid)))
        return CTL_E_INVALIDPROPERTYVALUE;

    if (This->host) {
2734
        release_modules(This, TRUE);
2735
        IActiveScriptSite_Release(&This->host->IActiveScriptSite_iface);
2736 2737 2738 2739 2740 2741
        This->host = NULL;
    }

    if (!language)
        return S_OK;

2742
    hres = init_script_host(This, &clsid, &This->host);
2743 2744 2745 2746 2747 2748
    if (FAILED(hres))
        return hres;

    /* Alloc global module */
    This->modules = heap_alloc_zero(sizeof(*This->modules));
    if (This->modules) {
2749
        This->modules[0] = create_module(This->host, NULL);
2750 2751 2752 2753 2754 2755
        if (!This->modules[0]) {
            heap_free(This->modules);
            This->modules = NULL;
            hres = E_OUTOFMEMORY;
        }
    }
2756 2757 2758
    else
        hres = E_OUTOFMEMORY;

2759 2760
    if (FAILED(hres)) {
        detach_script_host(This->host);
2761
        This->host = NULL;
2762 2763
    }
    return hres;
2764 2765 2766 2767 2768
}

static HRESULT WINAPI ScriptControl_get_State(IScriptControl *iface, ScriptControlStates *p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2769 2770 2771
    SCRIPTSTATE state;
    HRESULT hres;

2772 2773 2774 2775 2776 2777 2778 2779
    TRACE("(%p)->(%p)\n", This, p);

    if(!p)
        return E_POINTER;

    if(!This->host)
        return E_FAIL;

2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
    hres = IActiveScript_GetScriptState(This->host->script, &state);
    if (FAILED(hres)) return hres;

    switch (state)
    {
    case SCRIPTSTATE_INITIALIZED:
    case SCRIPTSTATE_STARTED:
        *p = Initialized;
        break;
    case SCRIPTSTATE_CONNECTED:
        *p = Connected;
        break;
    default:
        WARN("unexpected state %d\n", state);
        return E_FAIL;
    }
2796
    return S_OK;
2797 2798 2799 2800 2801
}

static HRESULT WINAPI ScriptControl_put_State(IScriptControl *iface, ScriptControlStates state)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2802 2803 2804 2805 2806 2807 2808 2809
    TRACE("(%p)->(%x)\n", This, state);

    if(!This->host)
        return E_FAIL;

    if(state != Initialized && state != Connected)
        return CTL_E_INVALIDPROPERTYVALUE;

2810
    return IActiveScript_SetScriptState(This->host->script, state == Connected ? SCRIPTSTATE_CONNECTED : SCRIPTSTATE_STARTED);
2811 2812 2813 2814 2815
}

static HRESULT WINAPI ScriptControl_put_SitehWnd(IScriptControl *iface, LONG hwnd)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2816

2817
    TRACE("(%p)->(%x)\n", This, hwnd);
2818

2819 2820 2821 2822
    if (hwnd && !IsWindow(LongToHandle(hwnd)))
        return CTL_E_INVALIDPROPERTYVALUE;

    This->site_hwnd = LongToHandle(hwnd);
2823 2824
    if (This->host)
        This->host->site_hwnd = This->allow_ui ? This->site_hwnd : ((HWND)-1);
2825
    return S_OK;
2826 2827 2828 2829 2830
}

static HRESULT WINAPI ScriptControl_get_SitehWnd(IScriptControl *iface, LONG *p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2831 2832 2833 2834 2835 2836 2837

    TRACE("(%p)->(%p)\n", This, p);

    if (!p) return E_POINTER;

    *p = HandleToLong(This->site_hwnd);
    return S_OK;
2838 2839 2840 2841 2842
}

static HRESULT WINAPI ScriptControl_get_Timeout(IScriptControl *iface, LONG *p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2843 2844 2845 2846 2847 2848 2849 2850

    TRACE("(%p)->(%p)\n", This, p);

    if (!p)
        return E_POINTER;

    *p = This->timeout;
    return S_OK;
2851 2852
}

2853
static HRESULT WINAPI ScriptControl_put_Timeout(IScriptControl *iface, LONG timeout)
2854
{
2855
    ScriptControl *This = impl_from_IScriptControl(iface);
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866

    TRACE("(%p)->(%d)\n", This, timeout);

    if (timeout < -1)
        return CTL_E_INVALIDPROPERTYVALUE;

    if (timeout != -1)
        FIXME("execution timeout ignored\n");

    This->timeout = timeout;
    return S_OK;
2867 2868 2869 2870 2871
}

static HRESULT WINAPI ScriptControl_get_AllowUI(IScriptControl *iface, VARIANT_BOOL *p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2872 2873 2874 2875 2876 2877 2878
    TRACE("(%p)->(%p)\n", This, p);

    if(!p)
        return E_POINTER;

    *p = This->allow_ui;
    return S_OK;
2879 2880 2881 2882 2883
}

static HRESULT WINAPI ScriptControl_put_AllowUI(IScriptControl *iface, VARIANT_BOOL allow_ui)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2884 2885 2886
    TRACE("(%p)->(%x)\n", This, allow_ui);

    This->allow_ui = allow_ui;
2887 2888
    if (This->host)
        This->host->site_hwnd = allow_ui ? This->site_hwnd : ((HWND)-1);
2889
    return S_OK;
2890 2891 2892 2893 2894
}

static HRESULT WINAPI ScriptControl_get_UseSafeSubset(IScriptControl *iface, VARIANT_BOOL *p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2895 2896 2897 2898 2899 2900 2901
    TRACE("(%p)->(%p)\n", This, p);

    if(!p)
        return E_POINTER;

    *p = This->use_safe_subset;
    return S_OK;
2902 2903
}

2904
static HRESULT WINAPI ScriptControl_put_UseSafeSubset(IScriptControl *iface, VARIANT_BOOL use_safe_subset)
2905 2906
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2907 2908
    TRACE("(%p)->(%x)\n", This, use_safe_subset);

2909 2910 2911
    if (This->host && This->use_safe_subset != use_safe_subset)
        set_safety_opts(This->host->script, use_safe_subset);

2912 2913
    This->use_safe_subset = use_safe_subset;
    return S_OK;
2914 2915 2916 2917 2918
}

static HRESULT WINAPI ScriptControl_get_Modules(IScriptControl *iface, IScriptModuleCollection **p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2919 2920 2921

    TRACE("(%p)->(%p)\n", This, p);

2922
    if (!This->host) return E_FAIL;
2923

2924 2925 2926
    *p = &This->IScriptModuleCollection_iface;
    IScriptControl_AddRef(iface);
    return S_OK;
2927 2928 2929 2930 2931
}

static HRESULT WINAPI ScriptControl_get_Error(IScriptControl *iface, IScriptError **p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2932 2933 2934 2935 2936 2937 2938 2939

    TRACE("(%p)->(%p)\n", This, p);

    if (!p) return E_POINTER;

    *p = &This->error->IScriptError_iface;
    IScriptError_AddRef(*p);
    return S_OK;
2940 2941 2942 2943 2944
}

static HRESULT WINAPI ScriptControl_get_CodeObject(IScriptControl *iface, IDispatch **p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2945 2946 2947 2948 2949 2950

    TRACE("(%p)->(%p)\n", This, p);

    if (!This->host) return E_FAIL;

    return IScriptModule_get_CodeObject(&This->modules[0]->IScriptModule_iface, p);
2951 2952 2953 2954 2955
}

static HRESULT WINAPI ScriptControl_get_Procedures(IScriptControl *iface, IScriptProcedureCollection **p)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2956 2957 2958 2959 2960 2961

    TRACE("(%p)->(%p)\n", This, p);

    if (!This->host) return E_FAIL;

    return IScriptModule_get_Procedures(&This->modules[0]->IScriptModule_iface, p);
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
}

static HRESULT WINAPI ScriptControl__AboutBox(IScriptControl *iface)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI ScriptControl_AddObject(IScriptControl *iface, BSTR name, IDispatch *object, VARIANT_BOOL add_members)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
    DWORD flags = SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE;

    TRACE("(%p)->(%s %p %x)\n", This, debugstr_w(name), object, add_members);

    if (!object)
        return E_INVALIDARG;

    if (!This->host)
        return E_FAIL;

    if (add_members)
        flags |= SCRIPTITEM_GLOBALMEMBERS;
2986
    return add_script_object(This->host, name, object, flags);
2987 2988 2989 2990 2991
}

static HRESULT WINAPI ScriptControl_Reset(IScriptControl *iface)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
2992 2993 2994 2995 2996 2997

    TRACE("(%p)\n", This);

    if (!This->host)
        return E_FAIL;

2998
    clear_named_items(This->host);
2999
    return set_script_state(This->host, SCRIPTSTATE_INITIALIZED);
3000 3001 3002 3003 3004
}

static HRESULT WINAPI ScriptControl_AddCode(IScriptControl *iface, BSTR code)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
3005 3006 3007

    TRACE("(%p)->(%s).\n", This, debugstr_w(code));

3008 3009 3010
    if (!This->host)
        return E_FAIL;

3011
    return parse_script_text(This->modules[0], code, SCRIPTTEXT_ISVISIBLE, NULL);
3012 3013 3014 3015 3016
}

static HRESULT WINAPI ScriptControl_Eval(IScriptControl *iface, BSTR expression, VARIANT *res)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
3017

3018
    TRACE("(%p)->(%s, %p).\n", This, debugstr_w(expression), res);
3019 3020 3021 3022

    if (!res)
        return E_POINTER;
    V_VT(res) = VT_EMPTY;
3023 3024
    if (!This->host)
        return E_FAIL;
3025

3026
    return parse_script_text(This->modules[0], expression, SCRIPTTEXT_ISEXPRESSION, res);
3027 3028 3029 3030 3031
}

static HRESULT WINAPI ScriptControl_ExecuteStatement(IScriptControl *iface, BSTR statement)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
3032 3033 3034

    TRACE("(%p)->(%s)\n", This, debugstr_w(statement));

3035 3036 3037
    if (!This->host)
        return E_FAIL;

3038
    return parse_script_text(This->modules[0], statement, 0, NULL);
3039 3040 3041 3042 3043
}

static HRESULT WINAPI ScriptControl_Run(IScriptControl *iface, BSTR procedure_name, SAFEARRAY **parameters, VARIANT *res)
{
    ScriptControl *This = impl_from_IScriptControl(iface);
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053
    SAFEARRAY *sa;

    TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(procedure_name), parameters, res);

    if (!parameters || !res) return E_POINTER;
    if (!(sa = *parameters)) return E_POINTER;

    V_VT(res) = VT_EMPTY;
    if (sa->cDims == 0) return DISP_E_BADINDEX;
    if (!(sa->fFeatures & FADF_VARIANT)) return DISP_E_BADVARTYPE;
3054
    if (!This->host) return E_FAIL;
3055

3056
    return run_procedure(This->modules[0], procedure_name, sa, res);
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
}

static const IScriptControlVtbl ScriptControlVtbl = {
    ScriptControl_QueryInterface,
    ScriptControl_AddRef,
    ScriptControl_Release,
    ScriptControl_GetTypeInfoCount,
    ScriptControl_GetTypeInfo,
    ScriptControl_GetIDsOfNames,
    ScriptControl_Invoke,
    ScriptControl_get_Language,
    ScriptControl_put_Language,
    ScriptControl_get_State,
    ScriptControl_put_State,
    ScriptControl_put_SitehWnd,
    ScriptControl_get_SitehWnd,
    ScriptControl_get_Timeout,
    ScriptControl_put_Timeout,
    ScriptControl_get_AllowUI,
    ScriptControl_put_AllowUI,
    ScriptControl_get_UseSafeSubset,
    ScriptControl_put_UseSafeSubset,
    ScriptControl_get_Modules,
    ScriptControl_get_Error,
    ScriptControl_get_CodeObject,
    ScriptControl_get_Procedures,
    ScriptControl__AboutBox,
    ScriptControl_AddObject,
    ScriptControl_Reset,
    ScriptControl_AddCode,
    ScriptControl_Eval,
    ScriptControl_ExecuteStatement,
    ScriptControl_Run
};

3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
static HRESULT WINAPI OleObject_QueryInterface(IOleObject *iface, REFIID riid, void **obj)
{
    ScriptControl *This = impl_from_IOleObject(iface);
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

static ULONG WINAPI OleObject_AddRef(IOleObject *iface)
{
    ScriptControl *This = impl_from_IOleObject(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI OleObject_Release(IOleObject *iface)
{
    ScriptControl *This = impl_from_IOleObject(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI OleObject_SetClientSite(IOleObject *iface, IOleClientSite *site)
{
    ScriptControl *This = impl_from_IOleObject(iface);

3114
    TRACE("(%p)->(%p)\n", This, site);
3115

3116 3117 3118 3119 3120 3121 3122
    if (This->site)
        IOleClientSite_Release(This->site);

    if ((This->site = site))
        IOleClientSite_AddRef(site);

    return S_OK;
3123 3124 3125 3126 3127 3128
}

static HRESULT WINAPI OleObject_GetClientSite(IOleObject *iface, IOleClientSite **site)
{
    ScriptControl *This = impl_from_IOleObject(iface);

3129
    TRACE("(%p)->(%p)\n", This, site);
3130

3131 3132 3133 3134 3135 3136 3137
    if (!site)
        return E_POINTER;

    if ((*site = This->site))
        IOleClientSite_AddRef(*site);

    return S_OK;
3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
}

static HRESULT WINAPI OleObject_SetHostNames(IOleObject *iface, LPCOLESTR containerapp, LPCOLESTR containerobj)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%s %s)\n", This, debugstr_w(containerapp), debugstr_w(containerobj));

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_Close(IOleObject *iface, DWORD save)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d)\n", This, save);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_SetMoniker(IOleObject *iface, DWORD which, IMoniker *moniker)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d %p)\n", This, which, moniker);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_GetMoniker(IOleObject *iface, DWORD assign, DWORD which, IMoniker **moniker)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d %d %p)\n", This, assign, which, moniker);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_InitFromData(IOleObject *iface, IDataObject *dataobj, BOOL creation,
    DWORD reserved)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%p %d %d)\n", This, dataobj, creation, reserved);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_GetClipboardData(IOleObject *iface, DWORD reserved, IDataObject **dataobj)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d %p)\n", This, reserved, dataobj);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_DoVerb(IOleObject *iface, LONG verb, LPMSG msg, IOleClientSite *active_site,
    LONG index, HWND hwndParent, LPCRECT rect)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d %p %p %d %p %p)\n", This, verb, msg, active_site, index, hwndParent, rect);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_EnumVerbs(IOleObject *iface, IEnumOLEVERB **enumoleverb)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%p)\n", This, enumoleverb);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_Update(IOleObject *iface)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)\n", This);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_IsUpToDate(IOleObject *iface)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)\n", This);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_GetUserClassID(IOleObject *iface, CLSID *clsid)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%p)\n", This, clsid);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_GetUserType(IOleObject *iface, DWORD form_of_type, LPOLESTR *usertype)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d %p)\n", This, form_of_type, usertype);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_SetExtent(IOleObject *iface, DWORD aspect, SIZEL *size)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d %p)\n", This, aspect, size);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_GetExtent(IOleObject *iface, DWORD aspect, SIZEL *size)
{
    ScriptControl *This = impl_from_IOleObject(iface);

3263
    TRACE("(%p)->(%d %p)\n", This, aspect, size);
3264

3265 3266 3267 3268 3269
    if (aspect != DVASPECT_CONTENT)
        return DV_E_DVASPECT;

    *size = This->extent;
    return S_OK;
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
}

static HRESULT WINAPI OleObject_Advise(IOleObject *iface, IAdviseSink *sink, DWORD *connection)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%p %p)\n", This, sink, connection);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_Unadvise(IOleObject *iface, DWORD connection)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%d)\n", This, connection);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_EnumAdvise(IOleObject *iface, IEnumSTATDATA **enumadvise)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%p)\n", This, enumadvise);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleObject_GetMiscStatus(IOleObject *iface, DWORD aspect, DWORD *status)
{
    ScriptControl *This = impl_from_IOleObject(iface);

3303
    TRACE("(%p)->(%d %p)\n", This, aspect, status);
3304

3305
    return OleRegGetMiscStatus(&CLSID_ScriptControl, aspect, status);
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343
}

static HRESULT WINAPI OleObject_SetColorScheme(IOleObject *iface, LOGPALETTE *logpal)
{
    ScriptControl *This = impl_from_IOleObject(iface);

    FIXME("(%p)->(%p)\n", This, logpal);

    return E_NOTIMPL;
}

static const IOleObjectVtbl OleObjectVtbl = {
    OleObject_QueryInterface,
    OleObject_AddRef,
    OleObject_Release,
    OleObject_SetClientSite,
    OleObject_GetClientSite,
    OleObject_SetHostNames,
    OleObject_Close,
    OleObject_SetMoniker,
    OleObject_GetMoniker,
    OleObject_InitFromData,
    OleObject_GetClipboardData,
    OleObject_DoVerb,
    OleObject_EnumVerbs,
    OleObject_Update,
    OleObject_IsUpToDate,
    OleObject_GetUserClassID,
    OleObject_GetUserType,
    OleObject_SetExtent,
    OleObject_GetExtent,
    OleObject_Advise,
    OleObject_Unadvise,
    OleObject_EnumAdvise,
    OleObject_GetMiscStatus,
    OleObject_SetColorScheme
};

3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface, REFIID riid, void **obj)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *clsid)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);

    FIXME("(%p)->(%p)\n", This, clsid);

    return E_NOTIMPL;
}

static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);

    FIXME("(%p)\n", This);

    return E_NOTIMPL;
}

static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, IStream *stream)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);

    FIXME("(%p)->(%p)\n", This, stream);

    return E_NOTIMPL;
}

static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, IStream *stream, BOOL clear_dirty)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);

    FIXME("(%p)->(%p %d)\n", This, stream, clear_dirty);

    return E_NOTIMPL;
}

static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface, ULARGE_INTEGER *size)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);

    FIXME("(%p)->(%p)\n", This, size);

    return E_NOTIMPL;
}

static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
{
    ScriptControl *This = impl_from_IPersistStreamInit(iface);

    FIXME("(%p)\n", This);

3413
    return S_OK;
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
}

static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
    PersistStreamInit_QueryInterface,
    PersistStreamInit_AddRef,
    PersistStreamInit_Release,
    PersistStreamInit_GetClassID,
    PersistStreamInit_IsDirty,
    PersistStreamInit_Load,
    PersistStreamInit_Save,
    PersistStreamInit_GetSizeMax,
    PersistStreamInit_InitNew
};

3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
static HRESULT WINAPI OleControl_QueryInterface(IOleControl *iface, REFIID riid, void **obj)
{
    ScriptControl *This = impl_from_IOleControl(iface);
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

static ULONG WINAPI OleControl_AddRef(IOleControl *iface)
{
    ScriptControl *This = impl_from_IOleControl(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI OleControl_Release(IOleControl *iface)
{
    ScriptControl *This = impl_from_IOleControl(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI OleControl_GetControlInfo(IOleControl *iface, CONTROLINFO *info)
{
    ScriptControl *This = impl_from_IOleControl(iface);

3450
    TRACE("(%p)->(%p)\n", This, info);
3451

3452 3453 3454 3455 3456 3457 3458
    if (!info)
        return E_POINTER;

    info->hAccel = NULL;
    info->cAccel = 0;

    return S_OK;
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
}

static HRESULT WINAPI OleControl_OnMnemonic(IOleControl *iface, MSG *msg)
{
    ScriptControl *This = impl_from_IOleControl(iface);

    FIXME("(%p)->(%p)\n", This, msg);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleControl_OnAmbientPropertyChange(IOleControl *iface, DISPID dispid)
{
    ScriptControl *This = impl_from_IOleControl(iface);

    FIXME("(%p)->(%#x)\n", This, dispid);

    return E_NOTIMPL;
}

static HRESULT WINAPI OleControl_FreezeEvents(IOleControl *iface, BOOL freeze)
{
    ScriptControl *This = impl_from_IOleControl(iface);

    FIXME("(%p)->(%d)\n", This, freeze);

    return E_NOTIMPL;
}

static const IOleControlVtbl OleControlVtbl = {
    OleControl_QueryInterface,
    OleControl_AddRef,
    OleControl_Release,
    OleControl_GetControlInfo,
    OleControl_OnMnemonic,
    OleControl_OnAmbientPropertyChange,
    OleControl_FreezeEvents
};

3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551
static HRESULT WINAPI QuickActivate_QueryInterface(IQuickActivate *iface, REFIID riid, void **obj)
{
    ScriptControl *This = impl_from_IQuickActivate(iface);
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

static ULONG WINAPI QuickActivate_AddRef(IQuickActivate *iface)
{
    ScriptControl *This = impl_from_IQuickActivate(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI QuickActivate_Release(IQuickActivate *iface)
{
    ScriptControl *This = impl_from_IQuickActivate(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI QuickActivate_QuickActivate(IQuickActivate *iface, QACONTAINER *container, QACONTROL *control)
{
    ScriptControl *This = impl_from_IQuickActivate(iface);

    FIXME("(%p)->(%p %p)\n", This, container, control);

    return E_NOTIMPL;
}

static HRESULT WINAPI QuickActivate_SetContentExtent(IQuickActivate *iface, SIZEL *size)
{
    ScriptControl *This = impl_from_IQuickActivate(iface);

    FIXME("(%p)->(%p)\n", This, size);

    return E_NOTIMPL;
}

static HRESULT WINAPI QuickActivate_GetContentExtent(IQuickActivate *iface, SIZEL *size)
{
    ScriptControl *This = impl_from_IQuickActivate(iface);

    FIXME("(%p)->(%p)\n", This, size);

    return E_NOTIMPL;
}

static const IQuickActivateVtbl QuickActivateVtbl = {
    QuickActivate_QueryInterface,
    QuickActivate_AddRef,
    QuickActivate_Release,
    QuickActivate_QuickActivate,
    QuickActivate_SetContentExtent,
    QuickActivate_GetContentExtent
};

3552
static HRESULT WINAPI ViewObject_QueryInterface(IViewObjectEx *iface, REFIID riid, void **obj)
3553
{
3554
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3555 3556 3557
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

3558
static ULONG WINAPI ViewObject_AddRef(IViewObjectEx *iface)
3559
{
3560
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3561 3562 3563
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

3564
static ULONG WINAPI ViewObject_Release(IViewObjectEx *iface)
3565
{
3566
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3567 3568 3569
    return IScriptControl_Release(&This->IScriptControl_iface);
}

3570
static HRESULT WINAPI ViewObject_Draw(IViewObjectEx *iface, DWORD drawaspect, LONG index, void *aspect,
3571 3572 3573
    DVTARGETDEVICE *device, HDC target_dev, HDC hdc_draw, const RECTL *bounds, const RECTL *win_bounds,
    BOOL (STDMETHODCALLTYPE *fn_continue)(ULONG_PTR cont), ULONG_PTR cont)
{
3574
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3575 3576 3577 3578 3579 3580 3581

    FIXME("(%p)->(%d %d %p %p %p %p %p %p %p %lu)\n", This, drawaspect, index, aspect, device, target_dev,
        hdc_draw, bounds, win_bounds, fn_continue, cont);

    return E_NOTIMPL;
}

3582
static HRESULT WINAPI ViewObject_GetColorSet(IViewObjectEx *iface, DWORD drawaspect, LONG index, void *aspect,
3583 3584
    DVTARGETDEVICE *device, HDC hic_target, LOGPALETTE **colorset)
{
3585
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3586 3587 3588 3589 3590 3591 3592

    FIXME("(%p)->(%d %d %p %p %p %p)\n", This, drawaspect, index, aspect, device, hic_target,
        colorset);

    return E_NOTIMPL;
}

3593
static HRESULT WINAPI ViewObject_Freeze(IViewObjectEx *iface, DWORD drawaspect, LONG index, void *aspect,
3594 3595
    DWORD *freeze)
{
3596
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3597 3598 3599 3600 3601 3602

    FIXME("(%p)->(%d %d %p %p)\n", This, drawaspect, index, aspect, freeze);

    return E_NOTIMPL;
}

3603
static HRESULT WINAPI ViewObject_Unfreeze(IViewObjectEx *iface, DWORD freeze)
3604
{
3605
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3606 3607 3608 3609 3610 3611

    FIXME("(%p)->(%d)\n", This, freeze);

    return E_NOTIMPL;
}

3612
static HRESULT WINAPI ViewObject_SetAdvise(IViewObjectEx *iface, DWORD aspects, DWORD flags, IAdviseSink *sink)
3613
{
3614
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3615

3616
    TRACE("(%p)->(%d %#x %p)\n", This, aspects, flags, sink);
3617

3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
    if (aspects != DVASPECT_CONTENT)
        return DV_E_DVASPECT;

    This->view_sink_flags = flags;
    if (This->view_sink)
        IAdviseSink_Release(This->view_sink);
    This->view_sink = sink;
    if (This->view_sink)
        IAdviseSink_AddRef(This->view_sink);

    return S_OK;
3629 3630
}

3631
static HRESULT WINAPI ViewObject_GetAdvise(IViewObjectEx *iface, DWORD *aspects, DWORD *flags, IAdviseSink **sink)
3632
{
3633
    ScriptControl *This = impl_from_IViewObjectEx(iface);
3634

3635
    TRACE("(%p)->(%p %p %p)\n", This, aspects, flags, sink);
3636

3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647
    if (aspects)
        *aspects = DVASPECT_CONTENT;
    if (flags)
        *flags = This->view_sink_flags;
    if (sink) {
        *sink = This->view_sink;
        if (*sink)
            IAdviseSink_AddRef(*sink);
    }

    return S_OK;
3648 3649
}

3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
static HRESULT WINAPI ViewObject_GetExtent(IViewObjectEx *iface, DWORD draw_aspect, LONG index,
    DVTARGETDEVICE *device, SIZEL *size)
{
    ScriptControl *This = impl_from_IViewObjectEx(iface);

    FIXME("(%p)->(%d %d %p %p)\n", This, draw_aspect, index, device, size);

    return E_NOTIMPL;
}

static HRESULT WINAPI ViewObject_GetRect(IViewObjectEx *iface, DWORD aspect, RECTL *rect)
{
    ScriptControl *This = impl_from_IViewObjectEx(iface);

    FIXME("(%p)->(%d %p)\n", This, aspect, rect);

    return E_NOTIMPL;
}

static HRESULT WINAPI ViewObject_GetViewStatus(IViewObjectEx *iface, DWORD *status)
{
    ScriptControl *This = impl_from_IViewObjectEx(iface);

3673
    TRACE("(%p)->(%p)\n", This, status);
3674

3675 3676
    *status = VIEWSTATUS_OPAQUE;
    return S_OK;
3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
}

static HRESULT WINAPI ViewObject_QueryHitPoint(IViewObjectEx *iface, DWORD aspect, const RECT *bounds,
    POINT pt, LONG close_hint, DWORD *hit_result)
{
    ScriptControl *This = impl_from_IViewObjectEx(iface);

    FIXME("(%p)->(%d %s %s %d %p)\n", This, aspect, wine_dbgstr_rect(bounds), wine_dbgstr_point(&pt), close_hint, hit_result);

    return E_NOTIMPL;
}

static HRESULT WINAPI ViewObject_QueryHitRect(IViewObjectEx *iface, DWORD aspect, const RECT *bounds,
    const RECT *loc, LONG close_hint, DWORD *hit_result)
{
    ScriptControl *This = impl_from_IViewObjectEx(iface);

    FIXME("(%p)->(%d %s %s %d %p)\n", This, aspect, wine_dbgstr_rect(bounds), wine_dbgstr_rect(loc), close_hint, hit_result);

    return E_NOTIMPL;
}

static HRESULT WINAPI ViewObject_GetNaturalExtent(IViewObjectEx *iface, DWORD aspect, LONG index,
    DVTARGETDEVICE *device, HDC target_hdc, DVEXTENTINFO *extent_info, SIZEL *size)
{
    ScriptControl *This = impl_from_IViewObjectEx(iface);

    FIXME("(%p)->(%d %d %p %p %p %p)\n", This, aspect, index, device, target_hdc, extent_info, size);

    return E_NOTIMPL;
}

static const IViewObjectExVtbl ViewObjectExVtbl = {
3710 3711 3712 3713 3714 3715 3716 3717
    ViewObject_QueryInterface,
    ViewObject_AddRef,
    ViewObject_Release,
    ViewObject_Draw,
    ViewObject_GetColorSet,
    ViewObject_Freeze,
    ViewObject_Unfreeze,
    ViewObject_SetAdvise,
3718 3719 3720 3721 3722 3723 3724
    ViewObject_GetAdvise,
    ViewObject_GetExtent,
    ViewObject_GetRect,
    ViewObject_GetViewStatus,
    ViewObject_QueryHitPoint,
    ViewObject_QueryHitRect,
    ViewObject_GetNaturalExtent
3725 3726
};

3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
static HRESULT WINAPI PointerInactive_QueryInterface(IPointerInactive *iface, REFIID riid, void **obj)
{
    ScriptControl *This = impl_from_IPointerInactive(iface);
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

static ULONG WINAPI PointerInactive_AddRef(IPointerInactive *iface)
{
    ScriptControl *This = impl_from_IPointerInactive(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI PointerInactive_Release(IPointerInactive *iface)
{
    ScriptControl *This = impl_from_IPointerInactive(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI PointerInactive_GetActivationPolicy(IPointerInactive *iface, DWORD *policy)
{
    ScriptControl *This = impl_from_IPointerInactive(iface);

3749
    TRACE("(%p)->(%p)\n", This, policy);
3750

3751 3752 3753 3754 3755
    if (!policy)
        return E_POINTER;

    *policy = 0;
    return S_OK;
3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786
}

static HRESULT WINAPI PointerInactive_OnInactiveMouseMove(IPointerInactive *iface, const RECT *bounds,
    LONG x, LONG y, DWORD key_state)
{
    ScriptControl *This = impl_from_IPointerInactive(iface);

    FIXME("(%p)->(%s %d %d %#x)\n", This, wine_dbgstr_rect(bounds), x, y, key_state);

    return E_NOTIMPL;
}

static HRESULT WINAPI PointerInactive_OnInactiveSetCursor(IPointerInactive *iface, const RECT *bounds,
    LONG x, LONG y, DWORD msg, BOOL set_always)
{
    ScriptControl *This = impl_from_IPointerInactive(iface);

    FIXME("(%p)->(%s %d %d %#x %d)\n", This, wine_dbgstr_rect(bounds), x, y, msg, set_always);

    return E_NOTIMPL;
}

static const IPointerInactiveVtbl PointerInactiveVtbl = {
    PointerInactive_QueryInterface,
    PointerInactive_AddRef,
    PointerInactive_Release,
    PointerInactive_GetActivationPolicy,
    PointerInactive_OnInactiveMouseMove,
    PointerInactive_OnInactiveSetCursor
};

3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface, REFIID riid, void **obj)
{
    ScriptControl *This = impl_from_IConnectionPointContainer(iface);
    return IScriptControl_QueryInterface(&This->IScriptControl_iface, riid, obj);
}

static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
{
    ScriptControl *This = impl_from_IConnectionPointContainer(iface);
    return IScriptControl_AddRef(&This->IScriptControl_iface);
}

static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
{
    ScriptControl *This = impl_from_IConnectionPointContainer(iface);
    return IScriptControl_Release(&This->IScriptControl_iface);
}

static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface, IEnumConnectionPoints **enum_points)
{
    ScriptControl *This = impl_from_IConnectionPointContainer(iface);

    FIXME("(%p)->(%p)\n", This, enum_points);

    return E_NOTIMPL;
}

static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface, REFIID riid, IConnectionPoint **cp)
{
    ScriptControl *This = impl_from_IConnectionPointContainer(iface);
3817
    ConnectionPoint *iter;
3818

3819
    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), cp);
3820

3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
    *cp = NULL;

    for (iter = This->cp_list; iter; iter = iter->next) {
        if (IsEqualIID(iter->riid, riid))
            *cp = &iter->IConnectionPoint_iface;
    }

    if (*cp) {
        IConnectionPoint_AddRef(*cp);
        return S_OK;
    }

    FIXME("unsupported connection point %s\n", debugstr_guid(riid));
    return CONNECT_E_NOCONNECTION;
3835 3836 3837 3838 3839 3840 3841 3842 3843 3844
}

static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl = {
    ConnectionPointContainer_QueryInterface,
    ConnectionPointContainer_AddRef,
    ConnectionPointContainer_Release,
    ConnectionPointContainer_EnumConnectionPoints,
    ConnectionPointContainer_FindConnectionPoint
};

3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface,
        REFIID riid, void **ppv)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IConnectionPoint_iface;
    }else if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
        TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
        *ppv = &This->IConnectionPoint_iface;
    }else {
        FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
        *ppv = NULL;
        return E_NOINTERFACE;
    }

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

static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
    return IConnectionPointContainer_AddRef(&This->control->IConnectionPointContainer_iface);
}

static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
    return IConnectionPointContainer_Release(&This->control->IConnectionPointContainer_iface);
}

static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *iid)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);

3882
    TRACE("(%p)->(%p)\n", This, iid);
3883

3884 3885
    *iid = *This->riid;
    return S_OK;
3886 3887 3888 3889 3890 3891 3892
}

static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
        IConnectionPointContainer **container)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);

3893
    TRACE("(%p)->(%p)\n", This, container);
3894

3895 3896 3897 3898 3899 3900 3901
    if (!container)
        return E_POINTER;

    *container = &This->control->IConnectionPointContainer_iface;
    IConnectionPointContainer_AddRef(*container);

    return S_OK;
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
}

static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *unk_sink,
        DWORD *cookie)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);

    FIXME("(%p)->(%p %p)\n", This, unk_sink, cookie);

    return E_NOTIMPL;
}

static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD cookie)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);

    FIXME("(%p)->(%d)\n", This, cookie);

    return E_NOTIMPL;
}

static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
        IEnumConnections **ppEnum)
{
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);

    FIXME("(%p)->(%p): stub\n", This, ppEnum);

    return E_NOTIMPL;
}

static const IConnectionPointVtbl ConnectionPointVtbl =
{
    ConnectionPoint_QueryInterface,
    ConnectionPoint_AddRef,
    ConnectionPoint_Release,
    ConnectionPoint_GetConnectionInterface,
    ConnectionPoint_GetConnectionPointContainer,
    ConnectionPoint_Advise,
    ConnectionPoint_Unadvise,
    ConnectionPoint_EnumConnections
};

static void ConnectionPoint_Init(ConnectionPoint *cp, ScriptControl *sc, REFIID riid)
{
    cp->IConnectionPoint_iface.lpVtbl = &ConnectionPointVtbl;
    cp->control = sc;
    cp->riid = riid;

    cp->next = sc->cp_list;
    sc->cp_list = cp;
}

3955 3956 3957
static HRESULT WINAPI ScriptControl_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
{
    ScriptControl *script_control;
3958
    DWORD dpi_x, dpi_y;
3959
    HRESULT hres;
3960
    HDC hdc;
3961 3962 3963

    TRACE("(%p %s %p)\n", outer, debugstr_guid(riid), ppv);

3964
    script_control = heap_alloc_zero(sizeof(*script_control));
3965 3966 3967
    if(!script_control)
        return E_OUTOFMEMORY;

3968 3969 3970 3971 3972 3973 3974
    script_control->error = heap_alloc_zero(sizeof(*script_control->error));
    if(!script_control->error)
    {
        heap_free(script_control);
        return E_OUTOFMEMORY;
    }

3975
    script_control->IScriptControl_iface.lpVtbl = &ScriptControlVtbl;
3976
    script_control->IPersistStreamInit_iface.lpVtbl = &PersistStreamInitVtbl;
3977
    script_control->IOleObject_iface.lpVtbl = &OleObjectVtbl;
3978
    script_control->IOleControl_iface.lpVtbl = &OleControlVtbl;
3979
    script_control->IQuickActivate_iface.lpVtbl = &QuickActivateVtbl;
3980
    script_control->IViewObjectEx_iface.lpVtbl = &ViewObjectExVtbl;
3981
    script_control->IPointerInactive_iface.lpVtbl = &PointerInactiveVtbl;
3982
    script_control->IConnectionPointContainer_iface.lpVtbl = &ConnectionPointContainerVtbl;
3983
    script_control->IScriptModuleCollection_iface.lpVtbl = &ScriptModuleCollectionVtbl;
3984
    script_control->ref = 1;
3985
    script_control->timeout = 10000;
3986
    script_control->allow_ui = VARIANT_TRUE;
3987
    script_control->use_safe_subset = VARIANT_FALSE;
3988

3989 3990 3991
    script_control->error->IScriptError_iface.lpVtbl = &ScriptErrorVtbl;
    script_control->error->ref = 1;

3992 3993
    ConnectionPoint_Init(&script_control->cp_scsource, script_control, &DIID_DScriptControlSource);
    ConnectionPoint_Init(&script_control->cp_propnotif, script_control, &IID_IPropertyNotifySink);
3994

3995 3996 3997 3998 3999 4000 4001 4002
    hdc = GetDC(0);
    dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
    dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
    ReleaseDC(0, hdc);

    script_control->extent.cx = MulDiv(38, 2540, dpi_x);
    script_control->extent.cy = MulDiv(38, 2540, dpi_y);

4003 4004 4005
    hres = IScriptControl_QueryInterface(&script_control->IScriptControl_iface, riid, ppv);
    IScriptControl_Release(&script_control->IScriptControl_iface);
    return hres;
4006 4007
}

4008 4009 4010
/******************************************************************
 *              DllMain (msscript.ocx.@)
 */
4011
BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)
4012
{
4013
    TRACE("(%p %d %p)\n", instance, reason, reserved);
4014 4015 4016 4017 4018

    switch(reason) {
    case DLL_WINE_PREATTACH:
        return FALSE;  /* prefer native version */
    case DLL_PROCESS_ATTACH:
4019
        msscript_instance = instance;
4020 4021
        DisableThreadLibraryCalls(instance);
        break;
4022 4023 4024 4025
    case DLL_PROCESS_DETACH:
        if(!reserved)
            release_typelib();
        break;
4026 4027 4028 4029 4030
    }

    return TRUE;
}

4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
{
    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
        *ppv = iface;
    }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
        TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
        *ppv = iface;
    }

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

    WARN("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
    return E_NOINTERFACE;
}

static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
{
    TRACE("(%p)\n", iface);
    return 2;
}

static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
{
    TRACE("(%p)\n", iface);
    return 1;
}

static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
{
    TRACE("(%p)->(%x)\n", iface, fLock);
    return S_OK;
}

static const IClassFactoryVtbl ScriptControlFactoryVtbl = {
    ClassFactory_QueryInterface,
    ClassFactory_AddRef,
    ClassFactory_Release,
    ScriptControl_CreateInstance,
    ClassFactory_LockServer
};

static IClassFactory ScriptControlFactory = { &ScriptControlFactoryVtbl };

4080 4081 4082 4083 4084
/***********************************************************************
 *		DllGetClassObject	(msscript.ocx.@)
 */
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
4085 4086 4087 4088 4089
    if(IsEqualGUID(&CLSID_ScriptControl, rclsid)) {
        TRACE("(CLSID_ScriptControl %s %p)\n", debugstr_guid(riid), ppv);
        return IClassFactory_QueryInterface(&ScriptControlFactory, riid, ppv);
    }

4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107
    FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
    return CLASS_E_CLASSNOTAVAILABLE;
}

/***********************************************************************
 *          DllCanUnloadNow (msscript.ocx.@)
 */
HRESULT WINAPI DllCanUnloadNow(void)
{
    TRACE("\n");
    return S_FALSE;
}

/***********************************************************************
 *          DllRegisterServer (msscript.ocx.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
4108 4109
    TRACE("()\n");
    return __wine_register_resources(msscript_instance);
4110 4111 4112 4113 4114 4115 4116 4117
}

/***********************************************************************
 *          DllUnregisterServer (msscript.ocx.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
    TRACE("()\n");
4118
    return __wine_unregister_resources(msscript_instance);
4119
}