2 * Copyright 2008 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
29 #include "jscript_classes.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
37 DEFINE_GUID(GUID_NULL
,0,0,0,0,0,0,0,0,0,0,0);
39 HINSTANCE jscript_hinstance
;
40 static DWORD jscript_tls
;
41 static ITypeInfo
*dispatch_typeinfo
;
43 static int weak_refs_compare(const void *key
, const struct rb_entry
*entry
)
45 const struct weak_refs_entry
*weak_refs_entry
= RB_ENTRY_VALUE(entry
, const struct weak_refs_entry
, entry
);
46 ULONG_PTR a
= (ULONG_PTR
)key
, b
= (ULONG_PTR
)LIST_ENTRY(weak_refs_entry
->list
.next
, struct weakmap_entry
, weak_refs_entry
)->key
;
47 return (a
> b
) - (a
< b
);
50 struct thread_data
*get_thread_data(void)
52 struct thread_data
*thread_data
= TlsGetValue(jscript_tls
);
55 thread_data
= calloc(1, sizeof(struct thread_data
));
58 thread_data
->thread_id
= GetCurrentThreadId();
59 list_init(&thread_data
->objects
);
60 rb_init(&thread_data
->weak_refs
, weak_refs_compare
);
61 TlsSetValue(jscript_tls
, thread_data
);
68 void release_thread_data(struct thread_data
*thread_data
)
70 if(--thread_data
->ref
)
74 TlsSetValue(jscript_tls
, NULL
);
77 HRESULT
get_dispatch_typeinfo(ITypeInfo
**out
)
83 if (!dispatch_typeinfo
)
85 hr
= LoadRegTypeLib(&IID_StdOle
, STDOLE_MAJORVERNUM
, STDOLE_MINORVERNUM
, STDOLE_LCID
, &typelib
);
86 if (FAILED(hr
)) return hr
;
88 hr
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IDispatch
, &typeinfo
);
89 ITypeLib_Release(typelib
);
90 if (FAILED(hr
)) return hr
;
92 if (InterlockedCompareExchangePointer((void**)&dispatch_typeinfo
, typeinfo
, NULL
))
93 ITypeInfo_Release(typeinfo
);
96 *out
= dispatch_typeinfo
;
100 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, void **ppv
)
104 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
105 TRACE("(%p)->(IID_IUnknown %p)\n", iface
, ppv
);
107 }else if(IsEqualGUID(&IID_IClassFactory
, riid
)) {
108 TRACE("(%p)->(IID_IClassFactory %p)\n", iface
, ppv
);
113 IUnknown_AddRef((IUnknown
*)*ppv
);
117 FIXME("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppv
);
118 return E_NOINTERFACE
;
121 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
123 TRACE("(%p)\n", iface
);
127 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
129 TRACE("(%p)\n", iface
);
133 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
135 TRACE("(%p)->(%x)\n", iface
, fLock
);
145 static HRESULT WINAPI
JScriptFactory_CreateInstance(IClassFactory
*iface
, IUnknown
*outer
,
146 REFIID riid
, void **ppv
)
148 TRACE("(%p %s %p)\n", outer
, debugstr_guid(riid
), ppv
);
152 return CLASS_E_NOAGGREGATION
;
155 return create_jscript_object(FALSE
, riid
, ppv
);
158 static const IClassFactoryVtbl JScriptFactoryVtbl
= {
159 ClassFactory_QueryInterface
,
161 ClassFactory_Release
,
162 JScriptFactory_CreateInstance
,
163 ClassFactory_LockServer
166 static IClassFactory JScriptFactory
= { &JScriptFactoryVtbl
};
168 static HRESULT WINAPI
JScriptEncodeFactory_CreateInstance(IClassFactory
*iface
, IUnknown
*outer
,
169 REFIID riid
, void **ppv
)
171 TRACE("(%p %s %p)\n", outer
, debugstr_guid(riid
), ppv
);
175 return CLASS_E_NOAGGREGATION
;
178 return create_jscript_object(TRUE
, riid
, ppv
);
181 static const IClassFactoryVtbl JScriptEncodeFactoryVtbl
= {
182 ClassFactory_QueryInterface
,
184 ClassFactory_Release
,
185 JScriptEncodeFactory_CreateInstance
,
186 ClassFactory_LockServer
189 static IClassFactory JScriptEncodeFactory
= { &JScriptEncodeFactoryVtbl
};
191 /******************************************************************
192 * DllMain (jscript.@)
194 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
196 TRACE("(%p %ld %p)\n", hInstDLL
, fdwReason
, lpv
);
199 case DLL_PROCESS_ATTACH
:
200 DisableThreadLibraryCalls(hInstDLL
);
201 jscript_hinstance
= hInstDLL
;
202 jscript_tls
= TlsAlloc();
203 if(jscript_tls
== TLS_OUT_OF_INDEXES
|| !init_strings())
206 case DLL_PROCESS_DETACH
:
208 if (dispatch_typeinfo
) ITypeInfo_Release(dispatch_typeinfo
);
209 if(jscript_tls
!= TLS_OUT_OF_INDEXES
) TlsFree(jscript_tls
);
217 /***********************************************************************
218 * DllGetClassObject (jscript.@)
220 HRESULT WINAPI
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
222 if(IsEqualGUID(&CLSID_JScript
, rclsid
)) {
223 TRACE("(CLSID_JScript %s %p)\n", debugstr_guid(riid
), ppv
);
224 return IClassFactory_QueryInterface(&JScriptFactory
, riid
, ppv
);
227 if(IsEqualGUID(&CLSID_JScriptEncode
, rclsid
)) {
228 TRACE("(CLSID_JScriptEncode %s %p)\n", debugstr_guid(riid
), ppv
);
229 return IClassFactory_QueryInterface(&JScriptEncodeFactory
, riid
, ppv
);
232 FIXME("%s %s %p\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
233 return CLASS_E_CLASSNOTAVAILABLE
;
236 /***********************************************************************
237 * DllCanUnloadNow (jscript.@)
239 HRESULT WINAPI
DllCanUnloadNow(void)
241 TRACE("() ref=%ld\n", module_ref
);
243 return module_ref
? S_FALSE
: S_OK
;