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 #define MIN_BLOCK_SIZE 128
99 #define ARENA_FREE_FILLER 0xaa
101 static inline DWORD
block_size(DWORD block
)
103 return MIN_BLOCK_SIZE
<< block
;
106 void heap_pool_init(heap_pool_t
*heap
)
108 memset(heap
, 0, sizeof(*heap
));
109 list_init(&heap
->custom_blocks
);
112 void *heap_pool_alloc(heap_pool_t
*heap
, size_t size
)
119 if(!heap
->block_cnt
) {
121 heap
->blocks
= heap_alloc(sizeof(void*));
126 tmp
= heap_alloc(block_size(0));
130 heap
->blocks
[0] = tmp
;
134 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
135 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
136 heap
->offset
+= size
;
140 if(size
<= block_size(heap
->last_block
+1)) {
141 if(heap
->last_block
+1 == heap
->block_cnt
) {
142 tmp
= heap_realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
147 heap
->blocks
[heap
->block_cnt
] = heap_alloc(block_size(heap
->block_cnt
));
148 if(!heap
->blocks
[heap
->block_cnt
])
156 return heap
->blocks
[heap
->last_block
];
159 list
= heap_alloc(size
+ sizeof(struct list
));
163 list_add_head(&heap
->custom_blocks
, list
);
167 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
171 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
172 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
177 ret
= heap_pool_alloc(heap
, size
+inc
);
178 if(ret
) /* FIXME: avoid copying for custom blocks */
179 memcpy(ret
, mem
, size
);
183 void heap_pool_clear(heap_pool_t
*heap
)
190 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
198 for(i
=0; i
< heap
->block_cnt
; i
++)
199 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
202 heap
->last_block
= heap
->offset
= 0;
206 void heap_pool_free(heap_pool_t
*heap
)
210 heap_pool_clear(heap
);
212 for(i
=0; i
< heap
->block_cnt
; i
++)
213 heap_free(heap
->blocks
[i
]);
214 heap_free(heap
->blocks
);
216 heap_pool_init(heap
);
219 heap_pool_t
*heap_pool_mark(heap_pool_t
*heap
)
228 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppv
)
232 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
233 TRACE("(%p)->(IID_IUnknown %p)\n", iface
, ppv
);
235 }else if(IsEqualGUID(&IID_IClassFactory
, riid
)) {
236 TRACE("(%p)->(IID_IClassFactory %p)\n", iface
, ppv
);
241 IUnknown_AddRef((IUnknown
*)*ppv
);
245 FIXME("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
246 return E_NOINTERFACE
;
249 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
251 TRACE("(%p)\n", iface
);
255 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
257 TRACE("(%p)\n", iface
);
261 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
263 TRACE("(%p)->(%x)\n", iface
, fLock
);
267 static const IClassFactoryVtbl VBScriptFactoryVtbl
= {
268 ClassFactory_QueryInterface
,
270 ClassFactory_Release
,
271 VBScriptFactory_CreateInstance
,
272 ClassFactory_LockServer
275 static IClassFactory VBScriptFactory
= { &VBScriptFactoryVtbl
};
277 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl
= {
278 ClassFactory_QueryInterface
,
280 ClassFactory_Release
,
281 VBScriptRegExpFactory_CreateInstance
,
282 ClassFactory_LockServer
285 static IClassFactory VBScriptRegExpFactory
= { &VBScriptRegExpFactoryVtbl
};
287 /******************************************************************
288 * DllMain (vbscript.@)
290 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
292 TRACE("(%p %d %p)\n", hInstDLL
, fdwReason
, lpv
);
296 case DLL_WINE_PREATTACH
:
297 return FALSE
; /* prefer native version */
298 case DLL_PROCESS_ATTACH
:
299 DisableThreadLibraryCalls(hInstDLL
);
300 vbscript_hinstance
= hInstDLL
;
302 case DLL_PROCESS_DETACH
:
305 release_regexp_typelib();
311 /***********************************************************************
312 * DllGetClassObject (vbscript.@)
314 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
316 if(IsEqualGUID(&CLSID_VBScript
, rclsid
)) {
317 TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid
), ppv
);
318 return IClassFactory_QueryInterface(&VBScriptFactory
, riid
, ppv
);
319 }else if(IsEqualGUID(&CLSID_VBScriptRegExp
, rclsid
)) {
320 TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid
), ppv
);
321 return IClassFactory_QueryInterface(&VBScriptRegExpFactory
, riid
, ppv
);
324 FIXME("%s %s %p\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
325 return CLASS_E_CLASSNOTAVAILABLE
;
328 /***********************************************************************
329 * DllCanUnloadNow (vbscript.@)
331 HRESULT WINAPI
DllCanUnloadNow(void)
336 /***********************************************************************
337 * DllRegisterServer (vbscript.@)
339 HRESULT WINAPI
DllRegisterServer(void)
342 return __wine_register_resources(vbscript_hinstance
);
345 /***********************************************************************
346 * DllUnregisterServer (vbscript.@)
348 HRESULT WINAPI
DllUnregisterServer(void)
351 return __wine_unregister_resources(vbscript_hinstance
);