msvcr90: Check more fields in _getptd tests.
[wine] / dlls / vbscript / vbscript_main.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "initguid.h"
20
21 #include "vbscript.h"
22 #include "objsafe.h"
23 #include "mshtmhst.h"
24 #include "rpcproxy.h"
25 #include "vbscript_classes.h"
26 #include "vbsglobal.h"
27 #include "vbsregexp55.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 WINE_DECLARE_DEBUG_CHANNEL(heap);
33
34 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
35
36 static HINSTANCE vbscript_hinstance;
37
38 static ITypeLib *typelib;
39 static ITypeInfo *typeinfos[LAST_tid];
40
41 static REFIID tid_ids[] = {
42 #define XDIID(iface) &DIID_ ## iface,
43 TID_LIST
44 #undef XDIID
45 };
46
47 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
48 {
49     HRESULT hres;
50
51     if (!typelib) {
52         ITypeLib *tl;
53
54         static const WCHAR vbscript_dll1W[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','1',0};
55
56         hres = LoadTypeLib(vbscript_dll1W, &tl);
57         if(FAILED(hres)) {
58             ERR("LoadRegTypeLib failed: %08x\n", hres);
59             return hres;
60         }
61
62         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
63             ITypeLib_Release(tl);
64     }
65
66     if(!typeinfos[tid]) {
67         ITypeInfo *ti;
68
69         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
70         if(FAILED(hres)) {
71             ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
72             return hres;
73         }
74
75         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
76             ITypeInfo_Release(ti);
77     }
78
79     *typeinfo = typeinfos[tid];
80     return S_OK;
81 }
82
83 static void release_typelib(void)
84 {
85     unsigned i;
86
87     if(!typelib)
88         return;
89
90     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++) {
91         if(typeinfos[i])
92             ITypeInfo_Release(typeinfos[i]);
93     }
94
95     ITypeLib_Release(typelib);
96 }
97
98 const char *debugstr_variant(const VARIANT *v)
99 {
100     if(!v)
101         return "(null)";
102
103     if(V_ISBYREF(v))
104         return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v)));
105
106     switch(V_VT(v)) {
107     case VT_EMPTY:
108         return "{VT_EMPTY}";
109     case VT_NULL:
110         return "{VT_NULL}";
111     case VT_I2:
112         return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
113     case VT_I4:
114         return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
115     case VT_UI4:
116         return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
117     case VT_R8:
118         return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
119     case VT_BSTR:
120         return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
121     case VT_DISPATCH:
122         return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
123     case VT_BOOL:
124         return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
125     default:
126         return wine_dbg_sprintf("{vt %d}", V_VT(v));
127     }
128 }
129
130 #define MIN_BLOCK_SIZE  128
131 #define ARENA_FREE_FILLER  0xaa
132
133 static inline DWORD block_size(DWORD block)
134 {
135     return MIN_BLOCK_SIZE << block;
136 }
137
138 void heap_pool_init(heap_pool_t *heap)
139 {
140     memset(heap, 0, sizeof(*heap));
141     list_init(&heap->custom_blocks);
142 }
143
144 void *heap_pool_alloc(heap_pool_t *heap, size_t size)
145 {
146     struct list *list;
147     void *tmp;
148
149     size = (size+3)&~3;
150
151     if(!heap->block_cnt) {
152         if(!heap->blocks) {
153             heap->blocks = heap_alloc(sizeof(void*));
154             if(!heap->blocks)
155                 return NULL;
156         }
157
158         tmp = heap_alloc(block_size(0));
159         if(!tmp)
160             return NULL;
161
162         heap->blocks[0] = tmp;
163         heap->block_cnt = 1;
164     }
165
166     if(heap->offset + size <= block_size(heap->last_block)) {
167         tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
168         heap->offset += size;
169         return tmp;
170     }
171
172     if(size <= block_size(heap->last_block+1)) {
173         if(heap->last_block+1 == heap->block_cnt) {
174             tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
175             if(!tmp)
176                 return NULL;
177
178             heap->blocks = tmp;
179             heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
180             if(!heap->blocks[heap->block_cnt])
181                 return NULL;
182
183             heap->block_cnt++;
184         }
185
186         heap->last_block++;
187         heap->offset = size;
188         return heap->blocks[heap->last_block];
189     }
190
191     list = heap_alloc(size + sizeof(struct list));
192     if(!list)
193         return NULL;
194
195     list_add_head(&heap->custom_blocks, list);
196     return list+1;
197 }
198
199 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
200 {
201     void *ret;
202
203     if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
204             && heap->offset+inc < block_size(heap->last_block)) {
205         heap->offset += inc;
206         return mem;
207     }
208
209     ret = heap_pool_alloc(heap, size+inc);
210     if(ret) /* FIXME: avoid copying for custom blocks */
211         memcpy(ret, mem, size);
212     return ret;
213 }
214
215 void heap_pool_clear(heap_pool_t *heap)
216 {
217     struct list *tmp;
218
219     if(!heap)
220         return;
221
222     while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
223         list_remove(tmp);
224         heap_free(tmp);
225     }
226
227     if(WARN_ON(heap)) {
228         DWORD i;
229
230         for(i=0; i < heap->block_cnt; i++)
231             memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
232     }
233
234     heap->last_block = heap->offset = 0;
235     heap->mark = FALSE;
236 }
237
238 void heap_pool_free(heap_pool_t *heap)
239 {
240     DWORD i;
241
242     heap_pool_clear(heap);
243
244     for(i=0; i < heap->block_cnt; i++)
245         heap_free(heap->blocks[i]);
246     heap_free(heap->blocks);
247
248     heap_pool_init(heap);
249 }
250
251 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
252 {
253     if(heap->mark)
254         return NULL;
255
256     heap->mark = TRUE;
257     return heap;
258 }
259
260 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
261 {
262     *ppv = NULL;
263
264     if(IsEqualGUID(&IID_IUnknown, riid)) {
265         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
266         *ppv = iface;
267     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
268         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
269         *ppv = iface;
270     }
271
272     if(*ppv) {
273         IUnknown_AddRef((IUnknown*)*ppv);
274         return S_OK;
275     }
276
277     FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
278     return E_NOINTERFACE;
279 }
280
281 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
282 {
283     TRACE("(%p)\n", iface);
284     return 2;
285 }
286
287 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
288 {
289     TRACE("(%p)\n", iface);
290     return 1;
291 }
292
293 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
294 {
295     TRACE("(%p)->(%x)\n", iface, fLock);
296     return S_OK;
297 }
298
299 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
300     ClassFactory_QueryInterface,
301     ClassFactory_AddRef,
302     ClassFactory_Release,
303     VBScriptFactory_CreateInstance,
304     ClassFactory_LockServer
305 };
306
307 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
308
309 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl = {
310     ClassFactory_QueryInterface,
311     ClassFactory_AddRef,
312     ClassFactory_Release,
313     VBScriptRegExpFactory_CreateInstance,
314     ClassFactory_LockServer
315 };
316
317 static IClassFactory VBScriptRegExpFactory = { &VBScriptRegExpFactoryVtbl };
318
319 /******************************************************************
320  *              DllMain (vbscript.@)
321  */
322 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
323 {
324     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
325
326     switch(fdwReason)
327     {
328     case DLL_WINE_PREATTACH:
329         return FALSE;  /* prefer native version */
330     case DLL_PROCESS_ATTACH:
331         DisableThreadLibraryCalls(hInstDLL);
332         vbscript_hinstance = hInstDLL;
333         break;
334     case DLL_PROCESS_DETACH:
335         release_typelib();
336         release_regexp_typelib();
337     }
338
339     return TRUE;
340 }
341
342 /***********************************************************************
343  *              DllGetClassObject       (vbscript.@)
344  */
345 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
346 {
347     if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
348         TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
349         return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
350     }else if(IsEqualGUID(&CLSID_VBScriptRegExp, rclsid)) {
351         TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid), ppv);
352         return IClassFactory_QueryInterface(&VBScriptRegExpFactory, riid, ppv);
353     }
354
355     FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
356     return CLASS_E_CLASSNOTAVAILABLE;
357 }
358
359 /***********************************************************************
360  *          DllCanUnloadNow (vbscript.@)
361  */
362 HRESULT WINAPI DllCanUnloadNow(void)
363 {
364     return S_FALSE;
365 }
366
367 /***********************************************************************
368  *          DllRegisterServer (vbscript.@)
369  */
370 HRESULT WINAPI DllRegisterServer(void)
371 {
372     TRACE("()\n");
373     return __wine_register_resources(vbscript_hinstance);
374 }
375
376 /***********************************************************************
377  *          DllUnregisterServer (vbscript.@)
378  */
379 HRESULT WINAPI DllUnregisterServer(void)
380 {
381     TRACE("()\n");
382     return __wine_unregister_resources(vbscript_hinstance);
383 }