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
23 #include <vbscript_classes.h>
24 #include <vbsglobal.h>
26 WINE_DECLARE_DEBUG_CHANNEL(heap
);
28 static HINSTANCE vbscript_hinstance
;
30 static ITypeLib
*typelib
;
31 static ITypeInfo
*typeinfos
[LAST_tid
];
33 static REFIID tid_ids
[] = {
34 #define XDIID(iface) &DIID_ ## iface,
39 HRESULT
get_typeinfo(tid_t tid
, ITypeInfo
**typeinfo
)
46 static const WCHAR vbscript_dll1W
[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','1',0};
48 hres
= LoadTypeLib(vbscript_dll1W
, &tl
);
50 ERR("LoadRegTypeLib failed: %08x\n", hres
);
54 if(InterlockedCompareExchangePointer((void**)&typelib
, tl
, NULL
))
61 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, tid_ids
[tid
], &ti
);
63 ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids
[tid
]), hres
);
67 if(InterlockedCompareExchangePointer((void**)(typeinfos
+tid
), ti
, NULL
))
68 ITypeInfo_Release(ti
);
71 *typeinfo
= typeinfos
[tid
];
75 static void release_typelib(void)
82 for(i
=0; i
< sizeof(typeinfos
)/sizeof(*typeinfos
); i
++) {
84 ITypeInfo_Release(typeinfos
[i
]);
87 ITypeLib_Release(typelib
);
90 const char *debugstr_variant(const VARIANT
*v
)
96 return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v
)));
104 return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v
));
106 return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v
));
108 return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v
));
110 return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v
));
112 return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v
)));
114 return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v
));
116 return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v
));
118 return wine_dbg_sprintf("{vt %d}", V_VT(v
));
122 #define MIN_BLOCK_SIZE 128
123 #define ARENA_FREE_FILLER 0xaa
125 static inline DWORD
block_size(DWORD block
)
127 return MIN_BLOCK_SIZE
<< block
;
130 void heap_pool_init(heap_pool_t
*heap
)
132 memset(heap
, 0, sizeof(*heap
));
133 list_init(&heap
->custom_blocks
);
136 void *heap_pool_alloc(heap_pool_t
*heap
, size_t size
)
143 if(!heap
->block_cnt
) {
145 heap
->blocks
= heap_alloc(sizeof(void*));
150 tmp
= heap_alloc(block_size(0));
154 heap
->blocks
[0] = tmp
;
158 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
159 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
160 heap
->offset
+= size
;
164 if(size
<= block_size(heap
->last_block
+1)) {
165 if(heap
->last_block
+1 == heap
->block_cnt
) {
166 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
171 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
172 if(!heap
->blocks
[heap
->block_cnt
])
180 return heap
->blocks
[heap
->last_block
];
183 list
= heap_alloc(size
+ sizeof(struct list
));
187 list_add_head(&heap
->custom_blocks
, list
);
191 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
195 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
196 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
201 ret
= heap_pool_alloc(heap
, size
+inc
);
202 if(ret
) /* FIXME: avoid copying for custom blocks */
203 memcpy(ret
, mem
, size
);
207 void heap_pool_clear(heap_pool_t
*heap
)
214 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
222 for(i
=0; i
< heap
->block_cnt
; i
++)
223 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
226 heap
->last_block
= heap
->offset
= 0;
230 void heap_pool_free(heap_pool_t
*heap
)
234 heap_pool_clear(heap
);
236 for(i
=0; i
< heap
->block_cnt
; i
++)
237 heap_free(heap
->blocks
[i
]);
238 heap_free(heap
->blocks
);
240 heap_pool_init(heap
);
243 heap_pool_t
*heap_pool_mark(heap_pool_t
*heap
)
252 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppv
)
256 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
257 TRACE("(%p)->(IID_IUnknown %p)\n", iface
, ppv
);
259 }else if(IsEqualGUID(&IID_IClassFactory
, riid
)) {
260 TRACE("(%p)->(IID_IClassFactory %p)\n", iface
, ppv
);
265 IUnknown_AddRef((IUnknown
*)*ppv
);
269 FIXME("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
270 return E_NOINTERFACE
;
273 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
275 TRACE("(%p)\n", iface
);
279 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
281 TRACE("(%p)\n", iface
);
285 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
287 TRACE("(%p)->(%x)\n", iface
, fLock
);
291 static const IClassFactoryVtbl VBScriptFactoryVtbl
= {
292 ClassFactory_QueryInterface
,
294 ClassFactory_Release
,
295 VBScriptFactory_CreateInstance
,
296 ClassFactory_LockServer
299 static IClassFactory VBScriptFactory
= { &VBScriptFactoryVtbl
};
301 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl
= {
302 ClassFactory_QueryInterface
,
304 ClassFactory_Release
,
305 VBScriptRegExpFactory_CreateInstance
,
306 ClassFactory_LockServer
309 static IClassFactory VBScriptRegExpFactory
= { &VBScriptRegExpFactoryVtbl
};
311 /******************************************************************
312 * DllMain (vbscript.@)
314 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
316 TRACE("(%p %d %p)\n", hInstDLL
, fdwReason
, lpv
);
320 case DLL_WINE_PREATTACH
:
321 return FALSE
; /* prefer native version */
322 case DLL_PROCESS_ATTACH
:
323 DisableThreadLibraryCalls(hInstDLL
);
324 vbscript_hinstance
= hInstDLL
;
326 case DLL_PROCESS_DETACH
:
329 release_regexp_typelib();
335 /***********************************************************************
336 * DllGetClassObject (vbscript.@)
338 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
340 if(IsEqualGUID(&CLSID_VBScript
, rclsid
)) {
341 TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid
), ppv
);
342 return IClassFactory_QueryInterface(&VBScriptFactory
, riid
, ppv
);
343 }else if(IsEqualGUID(&CLSID_VBScriptRegExp
, rclsid
)) {
344 TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid
), ppv
);
345 return IClassFactory_QueryInterface(&VBScriptRegExpFactory
, riid
, ppv
);
348 FIXME("%s %s %p\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
349 return CLASS_E_CLASSNOTAVAILABLE
;
352 /***********************************************************************
353 * DllCanUnloadNow (vbscript.@)
355 HRESULT WINAPI
DllCanUnloadNow(void)
360 /***********************************************************************
361 * DllRegisterServer (vbscript.@)
363 HRESULT WINAPI
DllRegisterServer(void)
366 return __wine_register_resources(vbscript_hinstance
);
369 /***********************************************************************
370 * DllUnregisterServer (vbscript.@)
372 HRESULT WINAPI
DllUnregisterServer(void)
375 return __wine_unregister_resources(vbscript_hinstance
);