2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
25 #include "vbscript_classes.h"
26 #include "vbsglobal.h"
27 #include "vbsregexp55.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 WINE_DECLARE_DEBUG_CHANNEL(heap);
34 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
36 static HINSTANCE vbscript_hinstance;
38 static ITypeLib *typelib;
39 static ITypeInfo *typeinfos[LAST_tid];
41 static REFIID tid_ids[] = {
42 #define XDIID(iface) &DIID_ ## iface,
47 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
54 static const WCHAR vbscript_dll1W[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','1',0};
56 hres = LoadTypeLib(vbscript_dll1W, &tl);
58 ERR("LoadRegTypeLib failed: %08x\n", hres);
62 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
69 hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
71 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
75 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
76 ITypeInfo_Release(ti);
79 *typeinfo = typeinfos[tid];
83 static void release_typelib(void)
90 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++) {
92 ITypeInfo_Release(typeinfos[i]);
95 ITypeLib_Release(typelib);
98 const char *debugstr_variant(const VARIANT *v)
104 return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v)));
112 return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
114 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
116 return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
118 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
120 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
122 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
124 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
126 return wine_dbg_sprintf("{vt %d}", V_VT(v));
130 #define MIN_BLOCK_SIZE 128
131 #define ARENA_FREE_FILLER 0xaa
133 static inline DWORD block_size(DWORD block)
135 return MIN_BLOCK_SIZE << block;
138 void heap_pool_init(heap_pool_t *heap)
140 memset(heap, 0, sizeof(*heap));
141 list_init(&heap->custom_blocks);
144 void *heap_pool_alloc(heap_pool_t *heap, size_t size)
151 if(!heap->block_cnt) {
153 heap->blocks = heap_alloc(sizeof(void*));
158 tmp = heap_alloc(block_size(0));
162 heap->blocks[0] = tmp;
166 if(heap->offset + size <= block_size(heap->last_block)) {
167 tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
168 heap->offset += size;
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*));
179 heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
180 if(!heap->blocks[heap->block_cnt])
188 return heap->blocks[heap->last_block];
191 list = heap_alloc(size + sizeof(struct list));
195 list_add_head(&heap->custom_blocks, list);
199 void *heap_pool_grow(heap_pool_t *heap, void *mem, DWORD size, DWORD inc)
203 if(mem == (BYTE*)heap->blocks[heap->last_block] + heap->offset-size
204 && heap->offset+inc < block_size(heap->last_block)) {
209 ret = heap_pool_alloc(heap, size+inc);
210 if(ret) /* FIXME: avoid copying for custom blocks */
211 memcpy(ret, mem, size);
215 void heap_pool_clear(heap_pool_t *heap)
222 while((tmp = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
230 for(i=0; i < heap->block_cnt; i++)
231 memset(heap->blocks[i], ARENA_FREE_FILLER, block_size(i));
234 heap->last_block = heap->offset = 0;
238 void heap_pool_free(heap_pool_t *heap)
242 heap_pool_clear(heap);
244 for(i=0; i < heap->block_cnt; i++)
245 heap_free(heap->blocks[i]);
246 heap_free(heap->blocks);
248 heap_pool_init(heap);
251 heap_pool_t *heap_pool_mark(heap_pool_t *heap)
260 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
264 if(IsEqualGUID(&IID_IUnknown, riid)) {
265 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
267 }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
268 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
273 IUnknown_AddRef((IUnknown*)*ppv);
277 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
278 return E_NOINTERFACE;
281 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
283 TRACE("(%p)\n", iface);
287 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
289 TRACE("(%p)\n", iface);
293 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
295 TRACE("(%p)->(%x)\n", iface, fLock);
299 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
300 ClassFactory_QueryInterface,
302 ClassFactory_Release,
303 VBScriptFactory_CreateInstance,
304 ClassFactory_LockServer
307 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
309 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl = {
310 ClassFactory_QueryInterface,
312 ClassFactory_Release,
313 VBScriptRegExpFactory_CreateInstance,
314 ClassFactory_LockServer
317 static IClassFactory VBScriptRegExpFactory = { &VBScriptRegExpFactoryVtbl };
319 /******************************************************************
320 * DllMain (vbscript.@)
322 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
324 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
328 case DLL_WINE_PREATTACH:
329 return FALSE; /* prefer native version */
330 case DLL_PROCESS_ATTACH:
331 DisableThreadLibraryCalls(hInstDLL);
332 vbscript_hinstance = hInstDLL;
334 case DLL_PROCESS_DETACH:
336 release_regexp_typelib();
342 /***********************************************************************
343 * DllGetClassObject (vbscript.@)
345 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
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);
355 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
356 return CLASS_E_CLASSNOTAVAILABLE;
359 /***********************************************************************
360 * DllCanUnloadNow (vbscript.@)
362 HRESULT WINAPI DllCanUnloadNow(void)
367 /***********************************************************************
368 * DllRegisterServer (vbscript.@)
370 HRESULT WINAPI DllRegisterServer(void)
373 return __wine_register_resources(vbscript_hinstance);
376 /***********************************************************************
377 * DllUnregisterServer (vbscript.@)
379 HRESULT WINAPI DllUnregisterServer(void)
382 return __wine_unregister_resources(vbscript_hinstance);