From: Gabriel Ivăncescu gabrielopcode@gmail.com
Signed-off-by: Gabriel Ivăncescu gabrielopcode@gmail.com --- dlls/jscript/jscript.c | 32 +++++++++++++++++++++----------- dlls/jscript/jscript.h | 8 ++++++++ dlls/jscript/jscript_main.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 12 deletions(-)
diff --git a/dlls/jscript/jscript.c b/dlls/jscript/jscript.c index b1940b770d3..191747c7173 100644 --- a/dlls/jscript/jscript.c +++ b/dlls/jscript/jscript.c @@ -51,8 +51,8 @@ typedef struct { LONG ref;
DWORD safeopt; + struct thread_data *thread_data; script_ctx_t *ctx; - LONG thread_id; LCID lcid; DWORD version; BOOL html_mode; @@ -524,8 +524,10 @@ static void decrease_state(JScript *This, SCRIPTSTATE state) FIXME("NULL ctx\n"); }
- if(state == SCRIPTSTATE_UNINITIALIZED || state == SCRIPTSTATE_CLOSED) - This->thread_id = 0; + if((state == SCRIPTSTATE_UNINITIALIZED || state == SCRIPTSTATE_CLOSED) && This->thread_data) { + release_thread_data(This->thread_data); + This->thread_data = NULL; + }
if(This->site) { IActiveScriptSite_Release(This->site); @@ -708,6 +710,8 @@ static ULONG WINAPI JScript_Release(IActiveScript *iface) This->ctx->active_script = NULL; script_release(This->ctx); } + if(This->thread_data) + release_thread_data(This->thread_data); free(This); unlock_module(); } @@ -726,6 +730,7 @@ static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface, IActiveScriptSite *pass) { JScript *This = impl_from_IActiveScript(iface); + struct thread_data *thread_data; named_item_t *item; LCID lcid; HRESULT hres; @@ -738,8 +743,13 @@ static HRESULT WINAPI JScript_SetScriptSite(IActiveScript *iface, if(This->site) return E_UNEXPECTED;
- if(InterlockedCompareExchange(&This->thread_id, GetCurrentThreadId(), 0)) + if(!(thread_data = get_thread_data())) + return E_OUTOFMEMORY; + + if(InterlockedCompareExchangePointer((void**)&This->thread_data, thread_data, NULL)) { + release_thread_data(thread_data); return E_UNEXPECTED; + }
if(!This->ctx) { script_ctx_t *ctx = calloc(1, sizeof(script_ctx_t)); @@ -821,7 +831,7 @@ static HRESULT WINAPI JScript_SetScriptState(IActiveScript *iface, SCRIPTSTATE s
TRACE("(%p)->(%d)\n", This, ss);
- if(This->thread_id && GetCurrentThreadId() != This->thread_id) + if(This->thread_data && This->thread_data->thread_id != GetCurrentThreadId()) return E_UNEXPECTED;
if(ss == SCRIPTSTATE_UNINITIALIZED) { @@ -865,7 +875,7 @@ static HRESULT WINAPI JScript_GetScriptState(IActiveScript *iface, SCRIPTSTATE * if(!pssState) return E_POINTER;
- if(This->thread_id && This->thread_id != GetCurrentThreadId()) + if(This->thread_data && This->thread_data->thread_id != GetCurrentThreadId()) return E_UNEXPECTED;
*pssState = This->ctx ? This->ctx->state : SCRIPTSTATE_UNINITIALIZED; @@ -878,7 +888,7 @@ static HRESULT WINAPI JScript_Close(IActiveScript *iface)
TRACE("(%p)->()\n", This);
- if(This->thread_id && This->thread_id != GetCurrentThreadId()) + if(This->thread_data && This->thread_data->thread_id != GetCurrentThreadId()) return E_UNEXPECTED;
decrease_state(This, SCRIPTSTATE_CLOSED); @@ -897,7 +907,7 @@ static HRESULT WINAPI JScript_AddNamedItem(IActiveScript *iface,
TRACE("(%p)->(%s %lx)\n", This, debugstr_w(pstrName), dwFlags);
- if(This->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED) + if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || !This->ctx || This->ctx->state == SCRIPTSTATE_CLOSED) return E_UNEXPECTED;
if(dwFlags & SCRIPTITEM_GLOBALMEMBERS) { @@ -959,7 +969,7 @@ static HRESULT WINAPI JScript_GetScriptDispatch(IActiveScript *iface, LPCOLESTR if(!ppdisp) return E_POINTER;
- if(This->thread_id != GetCurrentThreadId() || !This->ctx->global) { + if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || !This->ctx->global) { *ppdisp = NULL; return E_UNEXPECTED; } @@ -1101,7 +1111,7 @@ static HRESULT WINAPI JScriptParse_ParseScriptText(IActiveScriptParse *iface, debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLine, dwFlags, pvarResult, pexcepinfo);
- if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) + if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) return E_UNEXPECTED;
if(pstrItemName) { @@ -1204,7 +1214,7 @@ static HRESULT WINAPI JScriptParseProcedure_ParseProcedureText(IActiveScriptPars debugstr_w(pstrProcedureName), debugstr_w(pstrItemName), punkContext, debugstr_w(pstrDelimiter), wine_dbgstr_longlong(dwSourceContextCookie), ulStartingLineNumber, dwFlags, ppdisp);
- if(This->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) + if(!This->thread_data || This->thread_data->thread_id != GetCurrentThreadId() || This->ctx->state == SCRIPTSTATE_CLOSED) return E_UNEXPECTED;
if(pstrItemName) { diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h index 650c9278793..0e4f97b085d 100644 --- a/dlls/jscript/jscript.h +++ b/dlls/jscript/jscript.h @@ -129,6 +129,14 @@ typedef HRESULT (*builtin_setter_t)(script_ctx_t*,jsdisp_t*,jsval_t);
HRESULT builtin_set_const(script_ctx_t*,jsdisp_t*,jsval_t);
+struct thread_data { + LONG ref; + LONG thread_id; +}; + +struct thread_data *get_thread_data(void); +void release_thread_data(struct thread_data*); + typedef struct named_item_t { jsdisp_t *script_obj; IDispatch *disp; diff --git a/dlls/jscript/jscript_main.c b/dlls/jscript/jscript_main.c index 882c419ff83..2afe6fb2ff7 100644 --- a/dlls/jscript/jscript_main.c +++ b/dlls/jscript/jscript_main.c @@ -37,8 +37,34 @@ LONG module_ref = 0; DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
HINSTANCE jscript_hinstance; +static DWORD jscript_tls; static ITypeInfo *dispatch_typeinfo;
+struct thread_data *get_thread_data(void) +{ + struct thread_data *thread_data = TlsGetValue(jscript_tls); + + if(!thread_data) { + thread_data = calloc(1, sizeof(struct thread_data)); + if(!thread_data) + return NULL; + thread_data->thread_id = GetCurrentThreadId(); + TlsSetValue(jscript_tls, thread_data); + } + + thread_data->ref++; + return thread_data; +} + +void release_thread_data(struct thread_data *thread_data) +{ + if(--thread_data->ref) + return; + + free(thread_data); + TlsSetValue(jscript_tls, NULL); +} + HRESULT get_dispatch_typeinfo(ITypeInfo **out) { ITypeInfo *typeinfo; @@ -164,13 +190,16 @@ BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv) case DLL_PROCESS_ATTACH: DisableThreadLibraryCalls(hInstDLL); jscript_hinstance = hInstDLL; - if(!init_strings()) + jscript_tls = TlsAlloc(); + if(jscript_tls == TLS_OUT_OF_INDEXES || !init_strings()) return FALSE; break; case DLL_PROCESS_DETACH: if (lpv) break; if (dispatch_typeinfo) ITypeInfo_Release(dispatch_typeinfo); + if(jscript_tls != TLS_OUT_OF_INDEXES) TlsFree(jscript_tls); free_strings(); + break; }
return TRUE;