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
;
37 static ITypeInfo
*dispatch_typeinfo
;
39 BSTR
get_vbscript_string(int id
)
42 if(!LoadStringW(vbscript_hinstance
, id
, buf
, ARRAY_SIZE(buf
))) return NULL
;
43 return SysAllocString(buf
);
46 #define MIN_BLOCK_SIZE 128
47 #define ARENA_FREE_FILLER 0xaa
49 static inline DWORD
block_size(DWORD block
)
51 return MIN_BLOCK_SIZE
<< block
;
54 void heap_pool_init(heap_pool_t
*heap
)
56 memset(heap
, 0, sizeof(*heap
));
57 list_init(&heap
->custom_blocks
);
60 void *heap_pool_alloc(heap_pool_t
*heap
, size_t size
)
67 if(!heap
->block_cnt
) {
69 heap
->blocks
= malloc(sizeof(void*));
74 tmp
= malloc(block_size(0));
78 heap
->blocks
[0] = tmp
;
82 if(heap
->offset
+ size
<= block_size(heap
->last_block
)) {
83 tmp
= ((BYTE
*)heap
->blocks
[heap
->last_block
])+heap
->offset
;
88 if(size
<= block_size(heap
->last_block
+1)) {
89 if(heap
->last_block
+1 == heap
->block_cnt
) {
90 tmp
= realloc(heap
->blocks
, (heap
->block_cnt
+1)*sizeof(void*));
95 heap
->blocks
[heap
->block_cnt
] = malloc(block_size(heap
->block_cnt
));
96 if(!heap
->blocks
[heap
->block_cnt
])
104 return heap
->blocks
[heap
->last_block
];
107 list
= malloc(size
+ sizeof(struct list
));
111 list_add_head(&heap
->custom_blocks
, list
);
115 void *heap_pool_grow(heap_pool_t
*heap
, void *mem
, DWORD size
, DWORD inc
)
119 if(mem
== (BYTE
*)heap
->blocks
[heap
->last_block
] + heap
->offset
-size
120 && heap
->offset
+inc
< block_size(heap
->last_block
)) {
125 ret
= heap_pool_alloc(heap
, size
+inc
);
126 if(ret
) /* FIXME: avoid copying for custom blocks */
127 memcpy(ret
, mem
, size
);
131 void heap_pool_clear(heap_pool_t
*heap
)
138 while((tmp
= list_next(&heap
->custom_blocks
, &heap
->custom_blocks
))) {
146 for(i
=0; i
< heap
->block_cnt
; i
++)
147 memset(heap
->blocks
[i
], ARENA_FREE_FILLER
, block_size(i
));
150 heap
->last_block
= heap
->offset
= 0;
154 void heap_pool_free(heap_pool_t
*heap
)
158 heap_pool_clear(heap
);
160 for(i
=0; i
< heap
->block_cnt
; i
++)
161 free(heap
->blocks
[i
]);
164 heap_pool_init(heap
);
167 heap_pool_t
*heap_pool_mark(heap_pool_t
*heap
)
176 HRESULT
get_dispatch_typeinfo(ITypeInfo
**out
)
182 if (!dispatch_typeinfo
)
184 hr
= LoadRegTypeLib(&IID_StdOle
, STDOLE_MAJORVERNUM
, STDOLE_MINORVERNUM
, STDOLE_LCID
, &typelib
);
185 if (FAILED(hr
)) return hr
;
187 hr
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IDispatch
, &typeinfo
);
188 ITypeLib_Release(typelib
);
189 if (FAILED(hr
)) return hr
;
191 if (InterlockedCompareExchangePointer((void**)&dispatch_typeinfo
, typeinfo
, NULL
))
192 ITypeInfo_Release(typeinfo
);
195 *out
= dispatch_typeinfo
;
199 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppv
)
203 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
204 TRACE("(%p)->(IID_IUnknown %p)\n", iface
, ppv
);
206 }else if(IsEqualGUID(&IID_IClassFactory
, riid
)) {
207 TRACE("(%p)->(IID_IClassFactory %p)\n", iface
, ppv
);
212 IUnknown_AddRef((IUnknown
*)*ppv
);
216 FIXME("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
217 return E_NOINTERFACE
;
220 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
222 TRACE("(%p)\n", iface
);
226 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
228 TRACE("(%p)\n", iface
);
232 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
234 TRACE("(%p)->(%x)\n", iface
, fLock
);
238 static const IClassFactoryVtbl VBScriptFactoryVtbl
= {
239 ClassFactory_QueryInterface
,
241 ClassFactory_Release
,
242 VBScriptFactory_CreateInstance
,
243 ClassFactory_LockServer
246 static IClassFactory VBScriptFactory
= { &VBScriptFactoryVtbl
};
248 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl
= {
249 ClassFactory_QueryInterface
,
251 ClassFactory_Release
,
252 VBScriptRegExpFactory_CreateInstance
,
253 ClassFactory_LockServer
256 static IClassFactory VBScriptRegExpFactory
= { &VBScriptRegExpFactoryVtbl
};
258 /******************************************************************
259 * DllMain (vbscript.@)
261 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
263 TRACE("(%p %ld %p)\n", hInstDLL
, fdwReason
, lpv
);
267 case DLL_PROCESS_ATTACH
:
268 DisableThreadLibraryCalls(hInstDLL
);
269 vbscript_hinstance
= hInstDLL
;
271 case DLL_PROCESS_DETACH
:
273 if (dispatch_typeinfo
) ITypeInfo_Release(dispatch_typeinfo
);
274 release_regexp_typelib();
280 /***********************************************************************
281 * DllGetClassObject (vbscript.@)
283 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
285 if(IsEqualGUID(&CLSID_VBScript
, rclsid
)) {
286 TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid
), ppv
);
287 return IClassFactory_QueryInterface(&VBScriptFactory
, riid
, ppv
);
288 }else if(IsEqualGUID(&CLSID_VBScriptRegExp
, rclsid
)) {
289 TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid
), ppv
);
290 return IClassFactory_QueryInterface(&VBScriptRegExpFactory
, riid
, ppv
);
293 FIXME("%s %s %p\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
294 return CLASS_E_CLASSNOTAVAILABLE
;