usp10: Some unicode code points will force us into bidi mode.
[wine.git] / dlls / vbscript / vbscript_main.c
blobc47227f80cbdd8d2f5efafb870ec5b241dc85fc6
1 /*
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
19 #include "initguid.h"
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"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
34 static HINSTANCE vbscript_hinstance;
36 static ITypeLib *typelib;
37 static ITypeInfo *typeinfos[LAST_tid];
39 static REFIID tid_ids[] = {
40 #define XDIID(iface) &DIID_ ## iface,
41 TID_LIST
42 #undef XDIID
45 HRESULT get_typeinfo(tid_t tid, ITypeInfo **typeinfo)
47 HRESULT hres;
49 if (!typelib) {
50 ITypeLib *tl;
52 static const WCHAR vbscript_dll1W[] = {'v','b','s','c','r','i','p','t','.','d','l','l','\\','1',0};
54 hres = LoadTypeLib(vbscript_dll1W, &tl);
55 if(FAILED(hres)) {
56 ERR("LoadRegTypeLib failed: %08x\n", hres);
57 return hres;
60 if(InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
61 ITypeLib_Release(tl);
64 if(!typeinfos[tid]) {
65 ITypeInfo *ti;
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;
73 if(InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
74 ITypeInfo_Release(ti);
77 *typeinfo = typeinfos[tid];
78 return S_OK;
81 static void release_typelib(void)
83 unsigned i;
85 if(!typelib)
86 return;
88 for(i=0; i < sizeof(typeinfos)/sizeof(*typeinfos); i++) {
89 if(typeinfos[i])
90 ITypeInfo_Release(typeinfos[i]);
93 ITypeLib_Release(typelib);
96 const char *debugstr_variant(const VARIANT *v)
98 if(!v)
99 return "(null)";
101 if(V_ISBYREF(v))
102 return wine_dbg_sprintf("{V_BYREF -> %s}", debugstr_variant(V_BYREF(v)));
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));
128 #define MIN_BLOCK_SIZE 128
130 static inline DWORD block_size(DWORD block)
132 return MIN_BLOCK_SIZE << block;
135 void vbsheap_init(vbsheap_t *heap)
137 memset(heap, 0, sizeof(*heap));
138 list_init(&heap->custom_blocks);
141 void *vbsheap_alloc(vbsheap_t *heap, size_t size)
143 struct list *list;
144 void *tmp;
146 size = (size+3)&~3;
148 if(!heap->block_cnt) {
149 if(!heap->blocks) {
150 heap->blocks = heap_alloc(sizeof(void*));
151 if(!heap->blocks)
152 return NULL;
155 tmp = heap_alloc(block_size(0));
156 if(!tmp)
157 return NULL;
159 heap->blocks[0] = tmp;
160 heap->block_cnt = 1;
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;
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;
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;
180 heap->block_cnt++;
183 heap->last_block++;
184 heap->offset = size;
185 return heap->blocks[heap->last_block];
188 list = heap_alloc(size + sizeof(struct list));
189 if(!list)
190 return NULL;
192 list_add_head(&heap->custom_blocks, list);
193 return list+1;
196 void vbsheap_free(vbsheap_t *heap)
198 struct list *iter;
199 DWORD i;
201 while((iter = list_next(&heap->custom_blocks, &heap->custom_blocks))) {
202 list_remove(iter);
203 heap_free(iter);
206 for(i=0; i < heap->block_cnt; i++)
207 heap_free(heap->blocks[i]);
208 heap_free(heap->blocks);
211 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
213 *ppv = NULL;
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;
223 if(*ppv) {
224 IUnknown_AddRef((IUnknown*)*ppv);
225 return S_OK;
228 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
229 return E_NOINTERFACE;
232 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
234 TRACE("(%p)\n", iface);
235 return 2;
238 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
240 TRACE("(%p)\n", iface);
241 return 1;
244 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
246 TRACE("(%p)->(%x)\n", iface, fLock);
247 return S_OK;
250 static const IClassFactoryVtbl VBScriptFactoryVtbl = {
251 ClassFactory_QueryInterface,
252 ClassFactory_AddRef,
253 ClassFactory_Release,
254 VBScriptFactory_CreateInstance,
255 ClassFactory_LockServer
258 static IClassFactory VBScriptFactory = { &VBScriptFactoryVtbl };
260 /******************************************************************
261 * DllMain (vbscript.@)
263 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
265 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpv);
267 switch(fdwReason)
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();
279 return TRUE;
282 /***********************************************************************
283 * DllGetClassObject (vbscript.@)
285 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
287 if(IsEqualGUID(&CLSID_VBScript, rclsid)) {
288 TRACE("(CLSID_VBScript %s %p)\n", debugstr_guid(riid), ppv);
289 return IClassFactory_QueryInterface(&VBScriptFactory, riid, ppv);
292 FIXME("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
293 return CLASS_E_CLASSNOTAVAILABLE;
296 /***********************************************************************
297 * DllCanUnloadNow (vbscript.@)
299 HRESULT WINAPI DllCanUnloadNow(void)
301 return S_FALSE;
304 /***********************************************************************
305 * DllRegisterServer (vbscript.@)
307 HRESULT WINAPI DllRegisterServer(void)
309 TRACE("()\n");
310 return __wine_register_resources(vbscript_hinstance);
313 /***********************************************************************
314 * DllUnregisterServer (vbscript.@)
316 HRESULT WINAPI DllUnregisterServer(void)
318 TRACE("()\n");
319 return __wine_unregister_resources(vbscript_hinstance);