vbscript: Added function invocation supprot to do_icall.
[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
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
29
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 static HINSTANCE vbscript_hinstance;
33
34 const char *debugstr_variant(const VARIANT *v)
35 {
36     if(!v)
37         return "(null)";
38
39     if(V_ISBYREF(v))
40         return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v)));
41
42     switch(V_VT(v)) {
43     case VT_EMPTY:
44         return "{VT_EMPTY}";
45     case VT_NULL:
46         return "{VT_NULL}";
47     case VT_I2:
48         return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
49     case VT_I4:
50         return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
51     case VT_UI4:
52         return wine_dbg_sprintf("{VT_UI4: %u}", V_UI4(v));
53     case VT_R8:
54         return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
55     case VT_BSTR:
56         return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
57     case VT_DISPATCH:
58         return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
59     case VT_BOOL:
60         return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
61     default:
62         return wine_dbg_sprintf("{vt %d}", V_VT(v));
63     }
64 }
65
66 #define MIN_BLOCK_SIZE  128
67
68 static inline DWORD block_size(DWORD block)
69 {
70     return MIN_BLOCK_SIZE << block;
71 }
72
73 void vbsheap_init(vbsheap_t *heap)
74 {
75     memset(heap, 0, sizeof(*heap));
76     list_init(&heap->custom_blocks);
77 }
78
79 void *vbsheap_alloc(vbsheap_t *heap, size_t size)
80 {
81     struct list *list;
82     void *tmp;
83
84     size = (size+3)&~3;
85
86     if(!heap->block_cnt) {
87         if(!heap->blocks) {
88             heap->blocks = heap_alloc(sizeof(void*));
89             if(!heap->blocks)
90                 return NULL;
91         }
92
93         tmp = heap_alloc(block_size(0));
94         if(!tmp)
95             return NULL;
96
97         heap->blocks[0] = tmp;
98         heap->block_cnt = 1;
99     }
100
101     if(heap->offset + size <= block_size(heap->last_block)) {
102         tmp = ((BYTE*)heap->blocks[heap->last_block])+heap->offset;
103         heap->offset += size;
104         return tmp;
105     }
106
107     if(size <= block_size(heap->last_block+1)) {
108         if(heap->last_block+1 == heap->block_cnt) {
109             tmp = heap_realloc(heap->blocks, (heap->block_cnt+1)*sizeof(void*));
110             if(!tmp)
111                 return NULL;
112
113             heap->blocks = tmp;
114             heap->blocks[heap->block_cnt] = heap_alloc(block_size(heap->block_cnt));
115             if(!heap->blocks[heap->block_cnt])
116                 return NULL;
117
118             heap->block_cnt++;
119         }
120
121         heap->last_block++;
122         heap->offset = size;
123         return heap->blocks[heap->last_block];
124     }
125
126     list = heap_alloc(size + sizeof(struct list));
127     if(!list)
128         return NULL;
129
130     list_add_head(&heap->custom_blocks, list);
131     return list+1;
132 }
133
134 void vbsheap_free(vbsheap_t *heap)
135 {
136     struct list *iter;
137     DWORD i;
138
139     while((iter = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
140         list_remove(iter);
141         heap_free(iter);
142     }
143
144     for(i=0; i < heap->block_cnt; i++)
145         heap_free(heap->blocks[i]);
146     heap_free(heap->blocks);
147 }
148
149 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
150 {
151     *ppv = NULL;
152
153     if(IsEqualGUID(&IID_IUnknown, riid)) {
154         TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
155         *ppv = iface;
156     }else if(IsEqualGUID(&IID_IClassFactory, riid)) {
157         TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
158         *ppv = iface;
159     }
160
161     if(*ppv) {
162         IUnknown_AddRef((IUnknown*)*ppv);
163         return S_OK;
164     }
165
166     FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
167     return E_NOINTERFACE;
168 }
169
170 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
171 {
172     TRACE("(%p)\n", iface);
173     return 2;
174 }
175
176 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
177 {
178     TRACE("(%p)\n", iface);
179     return 1;
180 }
181
182 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
183 {
184     TRACE("(%p)->(%x)\n", iface, fLock);
185     return S_OK;
186 }
187
188 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
189     ClassFactory_QueryInterface,
190     ClassFactory_AddRef,
191     ClassFactory_Release,
192     VBScriptFactory_CreateInstance,
193     ClassFactory_LockServer
194 };
195
196 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
197
198 /******************************************************************
199  *              DllMain (vbscript.@)
200  */
201 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
202 {
203     TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
204
205     switch(fdwReason)
206     {
207     case DLL_WINE_PREATTACH:
208         return FALSE;  /* prefer native version */
209     case DLL_PROCESS_ATTACH:
210         DisableThreadLibraryCalls(hInstDLL);
211         vbscript_hinstance = hInstDLL;
212         break;
213     }
214
215     return TRUE;
216 }
217
218 /***********************************************************************
219  *              DllGetClassObject       (vbscript.@)
220  */
221 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
222 {
223     if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
224         TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
225         return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
226     }
227
228     FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
229     return CLASS_E_CLASSNOTAVAILABLE;
230 }
231
232 /***********************************************************************
233  *          DllCanUnloadNow (vbscript.@)
234  */
235 HRESULT WINAPI DllCanUnloadNow(void)
236 {
237     return S_FALSE;
238 }
239
240 /***********************************************************************
241  *          DllRegisterServer (vbscript.@)
242  */
243 HRESULT WINAPI DllRegisterServer(void)
244 {
245     TRACE("()\n");
246     return __wine_register_resources(vbscript_hinstance);
247 }
248
249 /***********************************************************************
250  *          DllUnregisterServer (vbscript.@)
251  */
252 HRESULT WINAPI DllUnregisterServer(void)
253 {
254     TRACE("()\n");
255     return __wine_unregister_resources(vbscript_hinstance);
256 }