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