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