vbscript: Added IRegExp2 stub.
[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
33 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
34
35 static HINSTANCE vbscript_hinstance;
36
37 static ITypeLib *typelib;
38 static ITypeInfo *typeinfos[LAST_tid];
39
40 static REFIID tid_ids[] = {
41 #define XDIID(iface) &DIID_ ## iface,
42 TID_LIST
43 #undef XDIID
44 };
45
46 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
47 {
48     HRESULT hres;
49
50     if (!typelib) {
51         ITypeLib *tl;
52
53         static const WCHAR vbscript_dll1W[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','1',0};
54
55         hres = LoadTypeLib(vbscript_dll1W, &tl);
56         if(FAILED(hres)) {
57             ERR("LoadRegTypeLib failed: %08x\n", hres);
58             return hres;
59         }
60
61         if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
62             ITypeLib_Release(tl);
63     }
64
65     if(!typeinfos[tid]) {
66         ITypeInfo *ti;
67
68         hres = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
69         if(FAILED(hres)) {
70             ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hres);
71             return hres;
72         }
73
74         if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
75             ITypeInfo_Release(ti);
76     }
77
78     *typeinfo = typeinfos[tid];
79     return S_OK;
80 }
81
82 static void release_typelib(void)
83 {
84     unsigned i;
85
86     if(!typelib)
87         return;
88
89     for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++) {
90         if(typeinfos[i])
91             ITypeInfo_Release(typeinfos[i]);
92     }
93
94     ITypeLib_Release(typelib);
95 }
96
97 const char *debugstr_variant(const VARIANT *v)
98 {
99     if(!v)
100         return "(null)";
101
102     if(V_ISBYREF(v))
103         return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v)));
104
105     switch(V_VT(v)) {
106     case VT_EMPTY:
107         return "{VT_EMPTY}";
108     case VT_NULL:
109         return "{VT_NULL}";
110     case VT_I2:
111         return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
112     case VT_I4:
113         return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
114     case VT_UI4:
115         return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
116     case VT_R8:
117         return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
118     case VT_BSTR:
119         return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
120     case VT_DISPATCH:
121         return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
122     case VT_BOOL:
123         return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
124     default:
125         return wine_dbg_sprintf("{vt %d}", V_VT(v));
126     }
127 }
128
129 #define MIN_BLOCK_SIZE  128
130
131 static inline DWORD block_size(DWORD block)
132 {
133     return MIN_BLOCK_SIZE << block;
134 }
135
136 void vbsheap_init(vbsheap_t *heap)
137 {
138     memset(heap, 0, sizeof(*heap));
139     list_init(&heap->custom_blocks);
140 }
141
142 void *vbsheap_alloc(vbsheap_t *heap, size_t size)
143 {
144     struct list *list;
145     void *tmp;
146
147     size = (size+3)&~3;
148
149     if(!heap->block_cnt) {
150         if(!heap->blocks) {
151             heap->blocks = heap_alloc(sizeof(void*));
152             if(!heap->blocks)
153                 return NULL;
154         }
155
156         tmp = heap_alloc(block_size(0));
157         if(!tmp)
158             return NULL;
159
160         heap->blocks[0] = tmp;
161         heap->block_cnt = 1;
162     }
163
164     if(heap->offset + size <= block_size(heap->last_block)) {
165         tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
166         heap->offset += size;
167         return tmp;
168     }
169
170     if(size <= block_size(heap->last_block+1)) {
171         if(heap->last_block+1 == heap->block_cnt) {
172             tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
173             if(!tmp)
174                 return NULL;
175
176             heap->blocks = tmp;
177             heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
178             if(!heap->blocks[heap->block_cnt])
179                 return NULL;
180
181             heap->block_cnt++;
182         }
183
184         heap->last_block++;
185         heap->offset = size;
186         return heap->blocks[heap->last_block];
187     }
188
189     list = heap_alloc(size + sizeof(struct list));
190     if(!list)
191         return NULL;
192
193     list_add_head(&heap->custom_blocks, list);
194     return list+1;
195 }
196
197 void vbsheap_free(vbsheap_t *heap)
198 {
199     struct list *iter;
200     DWORD i;
201
202     while((iter = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
203         list_remove(iter);
204         heap_free(iter);
205     }
206
207     for(i=0; i < heap->block_cnt; i++)
208         heap_free(heap->blocks[i]);
209     heap_free(heap->blocks);
210 }
211
212 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
213 {
214     *ppv = NULL;
215
216     if(IsEqualGUID(&IID_IUnknown, riid)) {
217         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
218         *ppv = iface;
219     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
220         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
221         *ppv = iface;
222     }
223
224     if(*ppv) {
225         IUnknown_AddRef((IUnknown*)*ppv);
226         return S_OK;
227     }
228
229     FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
230     return E_NOINTERFACE;
231 }
232
233 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
234 {
235     TRACE("(%p)\n", iface);
236     return 2;
237 }
238
239 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
240 {
241     TRACE("(%p)\n", iface);
242     return 1;
243 }
244
245 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
246 {
247     TRACE("(%p)->(%x)\n", iface, fLock);
248     return S_OK;
249 }
250
251 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
252     ClassFactory_QueryInterface,
253     ClassFactory_AddRef,
254     ClassFactory_Release,
255     VBScriptFactory_CreateInstance,
256     ClassFactory_LockServer
257 };
258
259 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
260
261 static const IClassFactoryVtbl VBScriptRegExpFactoryVtbl = {
262     ClassFactory_QueryInterface,
263     ClassFactory_AddRef,
264     ClassFactory_Release,
265     VBScriptRegExpFactory_CreateInstance,
266     ClassFactory_LockServer
267 };
268
269 static IClassFactory VBScriptRegExpFactory = { &VBScriptRegExpFactoryVtbl };
270
271 /******************************************************************
272  *              DllMain (vbscript.@)
273  */
274 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
275 {
276     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
277
278     switch(fdwReason)
279     {
280     case DLL_WINE_PREATTACH:
281         return FALSE;  /* prefer native version */
282     case DLL_PROCESS_ATTACH:
283         DisableThreadLibraryCalls(hInstDLL);
284         vbscript_hinstance = hInstDLL;
285         break;
286     case DLL_PROCESS_DETACH:
287         release_typelib();
288     }
289
290     return TRUE;
291 }
292
293 /***********************************************************************
294  *              DllGetClassObject       (vbscript.@)
295  */
296 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
297 {
298     if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
299         TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
300         return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
301     }else if(IsEqualGUID(&CLSID_VBScriptRegExp, rclsid)) {
302         TRACE("(CLSID_VBScriptRegExp %s %p)\n", debugstr_guid(riid), ppv);
303         return IClassFactory_QueryInterface(&VBScriptRegExpFactory, riid, ppv);
304     }
305
306     FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
307     return CLASS_E_CLASSNOTAVAILABLE;
308 }
309
310 /***********************************************************************
311  *          DllCanUnloadNow (vbscript.@)
312  */
313 HRESULT WINAPI DllCanUnloadNow(void)
314 {
315     return S_FALSE;
316 }
317
318 /***********************************************************************
319  *          DllRegisterServer (vbscript.@)
320  */
321 HRESULT WINAPI DllRegisterServer(void)
322 {
323     TRACE("()\n");
324     return __wine_register_resources(vbscript_hinstance);
325 }
326
327 /***********************************************************************
328  *          DllUnregisterServer (vbscript.@)
329  */
330 HRESULT WINAPI DllUnregisterServer(void)
331 {
332     TRACE("()\n");
333     return __wine_unregister_resources(vbscript_hinstance);
334 }