[PATCH v3 5/9] vbscript: Move the heap and global lists to the script dispatch object.

Gabriel Ivăncescu gabrielopcode at gmail.com
Fri Nov 1 10:05:06 CDT 2019


Signed-off-by: Gabriel Ivăncescu <gabrielopcode at gmail.com>
---

Note that we have to move the non-persistent vbscode from the context's list,
so it can be freed by the dispatch object only when it is released. Not doing
so will cause a crash even with Wine's own existing tests, since the script
dispatch will be held and released later when the code list no longer exists.

At the same time, the original approach wasn't quite correct either (using
the dispatch's code list when compiling the code), because of ordering issues.

 dlls/vbscript/compile.c  | 15 ++++++++-------
 dlls/vbscript/interp.c   | 20 ++++++++++++--------
 dlls/vbscript/vbdisp.c   | 39 ++++++++++++++++++++++++++++++---------
 dlls/vbscript/vbscript.c | 38 +++++++++++---------------------------
 dlls/vbscript/vbscript.h | 40 +++++++++++++++++++++-------------------
 5 files changed, 82 insertions(+), 70 deletions(-)

diff --git a/dlls/vbscript/compile.c b/dlls/vbscript/compile.c
index be4b37d..c57d569 100644
--- a/dlls/vbscript/compile.c
+++ b/dlls/vbscript/compile.c
@@ -1780,23 +1780,24 @@ static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
 
 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
 {
-    dynamic_var_t *var, **vars = script->global_vars;
-    function_t *func, **funcs = script->global_funcs;
+    ScriptDisp *obj = script->script_obj;
+    dynamic_var_t *var, **vars = obj->global_vars;
+    function_t *func, **funcs = obj->global_funcs;
     class_desc_t *class;
     vbscode_t *code;
     UINT i;
 
-    for(i = 0; i < script->global_vars_num; i++) {
+    for(i = 0; i < obj->global_vars_num; i++) {
         if(!wcsicmp(vars[i]->name, identifier))
             return TRUE;
     }
 
-    for(i = 0; i < script->global_funcs_num; i++) {
+    for(i = 0; i < obj->global_funcs_num; i++) {
         if(!wcsicmp(funcs[i]->name, identifier))
             return TRUE;
     }
 
-    for(class = script->classes; class; class = class->next) {
+    for(class = obj->classes; class; class = class->next) {
         if(!wcsicmp(class->name, identifier))
             return TRUE;
     }
@@ -2012,8 +2013,8 @@ HRESULT compile_procedure(script_ctx_t *script, const WCHAR *src, const WCHAR *d
     desc->func_cnt = 1;
     desc->funcs->entries[VBDISP_CALLGET] = &code->main_code;
 
-    desc->next = script->procs;
-    script->procs = desc;
+    desc->next = script->script_obj->procs;
+    script->script_obj->procs = desc;
 
     *ret = desc;
     return S_OK;
diff --git a/dlls/vbscript/interp.c b/dlls/vbscript/interp.c
index acd524f..77c4a0a 100644
--- a/dlls/vbscript/interp.c
+++ b/dlls/vbscript/interp.c
@@ -95,7 +95,7 @@ static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *re
     return FALSE;
 }
 
-static BOOL lookup_global_vars(script_ctx_t *script, const WCHAR *name, ref_t *ref)
+static BOOL lookup_global_vars(ScriptDisp *script, const WCHAR *name, ref_t *ref)
 {
     dynamic_var_t **vars = script->global_vars;
     UINT i, num = script->global_vars_num;
@@ -111,7 +111,7 @@ static BOOL lookup_global_vars(script_ctx_t *script, const WCHAR *name, ref_t *r
     return FALSE;
 }
 
-static BOOL lookup_global_funcs(script_ctx_t *script, const WCHAR *name, ref_t *ref)
+static BOOL lookup_global_funcs(ScriptDisp *script, const WCHAR *name, ref_t *ref)
 {
     function_t **funcs = script->global_funcs;
     UINT i, num = script->global_funcs_num;
@@ -129,6 +129,7 @@ static BOOL lookup_global_funcs(script_ctx_t *script, const WCHAR *name, ref_t *
 
 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
 {
+    ScriptDisp *script_obj = ctx->script->script_obj;
     named_item_t *item;
     IDispatch *disp;
     unsigned i;
@@ -160,7 +161,7 @@ static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_
     }
 
     if(ctx->func->type == FUNC_GLOBAL) {
-        if(lookup_global_vars(ctx->script, name, ref))
+        if(lookup_global_vars(script_obj, name, ref))
             return S_OK;
     }else {
         if(lookup_dynamic_vars(ctx->dynamic_vars, name, ref))
@@ -196,10 +197,10 @@ static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_
         }
     }
 
-    if(ctx->func->type != FUNC_GLOBAL && lookup_global_vars(ctx->script, name, ref))
+    if(ctx->func->type != FUNC_GLOBAL && lookup_global_vars(script_obj, name, ref))
         return S_OK;
 
-    if(lookup_global_funcs(ctx->script, name, ref))
+    if(lookup_global_funcs(script_obj, name, ref))
         return S_OK;
 
     hres = get_builtin_id(ctx->script->global_obj, name, &id);
@@ -236,12 +237,13 @@ static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_
 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
         BOOL is_const, VARIANT **out_var)
 {
+    ScriptDisp *script_obj = ctx->script->script_obj;
     dynamic_var_t *new_var;
     heap_pool_t *heap;
     WCHAR *str;
     unsigned size;
 
-    heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
+    heap = ctx->func->type == FUNC_GLOBAL ? &script_obj->heap : &ctx->heap;
 
     new_var = heap_pool_alloc(heap, sizeof(*new_var));
     if(!new_var)
@@ -257,7 +259,9 @@ static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
     V_VT(&new_var->v) = VT_EMPTY;
 
     if(ctx->func->type == FUNC_GLOBAL) {
-        add_global_var(ctx->script, new_var);
+        add_global_var(script_obj, new_var);
+        new_var->next = script_obj->dynamic_vars;
+        script_obj->dynamic_vars = new_var;
     }else {
         new_var->next = ctx->dynamic_vars;
         ctx->dynamic_vars = new_var;
@@ -1115,7 +1119,7 @@ static HRESULT interp_new(exec_ctx_t *ctx)
         return stack_push(ctx, &v);
     }
 
-    for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
+    for(class_desc = ctx->script->script_obj->classes; class_desc; class_desc = class_desc->next) {
         if(!wcsicmp(class_desc->name, arg))
             break;
     }
diff --git a/dlls/vbscript/vbdisp.c b/dlls/vbscript/vbdisp.c
index 7b5e28f..05426aa 100644
--- a/dlls/vbscript/vbdisp.c
+++ b/dlls/vbscript/vbdisp.c
@@ -569,11 +569,30 @@ static ULONG WINAPI ScriptDisp_Release(IDispatchEx *iface)
 {
     ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
     LONG ref = InterlockedDecrement(&This->ref);
+    vbscode_t *code, *code_next;
 
     TRACE("(%p) ref=%d\n", This, ref);
 
     if(!ref) {
         assert(!This->ctx);
+
+        while (This->procs)
+        {
+            class_desc_t *class_desc = This->procs;
+            This->procs = class_desc->next;
+            heap_free(class_desc);
+        }
+
+        LIST_FOR_EACH_ENTRY_SAFE(code, code_next, &This->code_list, vbscode_t, entry)
+        {
+            release_dynamic_vars(code->global_vars);
+            release_vbscode(code);
+        }
+        release_dynamic_vars(This->dynamic_vars);
+
+        heap_pool_free(&This->heap);
+        heap_free(This->global_vars);
+        heap_free(This->global_funcs);
         heap_free(This);
     }
 
@@ -641,8 +660,8 @@ static HRESULT WINAPI ScriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DW
     if(!This->ctx)
         return E_UNEXPECTED;
 
-    vars = This->ctx->global_vars;
-    num = This->ctx->global_vars_num;
+    vars = This->global_vars;
+    num = This->global_vars_num;
     for(i = 0; i < num; i++) {
         if(!wcsicmp(vars[i]->name, bstrName)) {
             *pid = i + 1;
@@ -650,8 +669,8 @@ static HRESULT WINAPI ScriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DW
         }
     }
 
-    funcs = This->ctx->global_funcs;
-    num = This->ctx->global_funcs_num;
+    funcs = This->global_funcs;
+    num = This->global_funcs_num;
     for(i = 0; i < num; i++) {
         if(!wcsicmp(funcs[i]->name, bstrName)) {
             *pid = i + 1 + DISPID_FUNCTION_MASK;
@@ -674,14 +693,14 @@ static HRESULT WINAPI ScriptDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lc
     if (id & DISPID_FUNCTION_MASK)
     {
         id &= ~DISPID_FUNCTION_MASK;
-        if (id > This->ctx->global_funcs_num)
+        if (id > This->global_funcs_num)
             return DISP_E_MEMBERNOTFOUND;
 
         switch (wFlags)
         {
         case DISPATCH_METHOD:
         case DISPATCH_METHOD | DISPATCH_PROPERTYGET:
-            hres = exec_script(This->ctx, TRUE, This->ctx->global_funcs[id - 1], NULL, pdp, pvarRes);
+            hres = exec_script(This->ctx, TRUE, This->global_funcs[id - 1], NULL, pdp, pvarRes);
             break;
         default:
             FIXME("Unsupported flags %x\n", wFlags);
@@ -691,16 +710,16 @@ static HRESULT WINAPI ScriptDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lc
         return hres;
     }
 
-    if (id > This->ctx->global_vars_num)
+    if (id > This->global_vars_num)
         return DISP_E_MEMBERNOTFOUND;
 
-    if (This->ctx->global_vars[id - 1]->is_const)
+    if (This->global_vars[id - 1]->is_const)
     {
         FIXME("const not supported\n");
         return E_NOTIMPL;
     }
 
-    return invoke_variant_prop(This->ctx, &This->ctx->global_vars[id - 1]->v, wFlags, pdp, pvarRes);
+    return invoke_variant_prop(This->ctx, &This->global_vars[id - 1]->v, wFlags, pdp, pvarRes);
 }
 
 static HRESULT WINAPI ScriptDisp_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
@@ -774,6 +793,8 @@ HRESULT create_script_disp(script_ctx_t *ctx, ScriptDisp **ret)
     script_disp->IDispatchEx_iface.lpVtbl = &ScriptDispVtbl;
     script_disp->ref = 1;
     script_disp->ctx = ctx;
+    heap_pool_init(&script_disp->heap);
+    list_init(&script_disp->code_list);
 
     *ret = script_disp;
     return S_OK;
diff --git a/dlls/vbscript/vbscript.c b/dlls/vbscript/vbscript.c
index 9165759..224e030 100644
--- a/dlls/vbscript/vbscript.c
+++ b/dlls/vbscript/vbscript.c
@@ -82,12 +82,14 @@ static inline BOOL is_started(VBScript *This)
 
 static HRESULT exec_global_code(script_ctx_t *ctx, vbscode_t *code, VARIANT *res)
 {
+    ScriptDisp *obj = ctx->script_obj;
+
     if (code->global_vars)
     {
         dynamic_var_t *var;
 
         for (var = code->global_vars; var; var = var->next)
-            if (!add_global_var(ctx, var))
+            if (!add_global_var(obj, var))
                 return E_OUTOFMEMORY;
     }
 
@@ -96,7 +98,7 @@ static HRESULT exec_global_code(script_ctx_t *ctx, vbscode_t *code, VARIANT *res
         function_t *func;
 
         for (func = code->funcs; func; func = func->next)
-            if (!add_global_func(ctx, func))
+            if (!add_global_func(obj, func))
                 return E_OUTOFMEMORY;
     }
 
@@ -112,8 +114,8 @@ static HRESULT exec_global_code(script_ctx_t *ctx, vbscode_t *code, VARIANT *res
             class = class->next;
         }
 
-        class->next = ctx->classes;
-        ctx->classes = code->classes;
+        class->next = obj->classes;
+        obj->classes = code->classes;
         code->last_class = class;
     }
 
@@ -166,31 +168,23 @@ IDispatch *lookup_named_item(script_ctx_t *ctx, const WCHAR *name, unsigned flag
 static void release_script(script_ctx_t *ctx)
 {
     vbscode_t *code, *code_next;
-    class_desc_t *class_desc;
-    UINT i;
 
     collect_objects(ctx);
     clear_ei(&ctx->ei);
 
-    for(i = 0; i < ctx->global_vars_num; i++)
-        VariantClear(&ctx->global_vars[i]->v);
-
-    heap_free(ctx->global_vars);
-    heap_free(ctx->global_funcs);
-    ctx->global_vars = NULL;
-    ctx->global_funcs = NULL;
-    ctx->global_vars_num = 0;
-    ctx->global_funcs_num = 0;
-
     LIST_FOR_EACH_ENTRY_SAFE(code, code_next, &ctx->code_list, vbscode_t, entry)
     {
         if(code->is_persistent)
         {
             code->pending_exec = TRUE;
             if(code->last_class) code->last_class->next = NULL;
+            release_dynamic_vars(code->global_vars);
         }
         else
-            release_vbscode(code);
+        {
+            list_remove(&code->entry);
+            list_add_tail(&ctx->script_obj->code_list, &code->entry);
+        }
     }
 
     while(!list_empty(&ctx->named_items)) {
@@ -203,13 +197,6 @@ static void release_script(script_ctx_t *ctx)
         heap_free(iter);
     }
 
-    while(ctx->procs) {
-        class_desc = ctx->procs;
-        ctx->procs = class_desc->next;
-
-        heap_free(class_desc);
-    }
-
     if(ctx->host_global) {
         IDispatch_Release(ctx->host_global);
         ctx->host_global = NULL;
@@ -234,8 +221,6 @@ static void release_script(script_ctx_t *ctx)
     }
 
     detach_global_objects(ctx);
-    heap_pool_free(&ctx->heap);
-    heap_pool_init(&ctx->heap);
 }
 
 static void destroy_script(script_ctx_t *ctx)
@@ -1013,7 +998,6 @@ HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory *iface, IUnknown *pU
     }
 
     ctx->safeopt = INTERFACE_USES_DISPEX;
-    heap_pool_init(&ctx->heap);
     list_init(&ctx->objects);
     list_init(&ctx->code_list);
     list_init(&ctx->named_items);
diff --git a/dlls/vbscript/vbscript.h b/dlls/vbscript/vbscript.h
index 24ff84d..fa7d4a1 100644
--- a/dlls/vbscript/vbscript.h
+++ b/dlls/vbscript/vbscript.h
@@ -125,11 +125,30 @@ struct _vbdisp_t {
     VARIANT props[1];
 };
 
+typedef struct _dynamic_var_t {
+    struct _dynamic_var_t *next;
+    VARIANT v;
+    const WCHAR *name;
+    BOOL is_const;
+} dynamic_var_t;
+
 typedef struct {
     IDispatchEx IDispatchEx_iface;
     LONG ref;
 
+    UINT global_vars_num;
+    UINT global_funcs_num;
+
+    dynamic_var_t *dynamic_vars;
+    dynamic_var_t **global_vars;
+    function_t **global_funcs;
+    class_desc_t *classes;
+    class_desc_t *procs;
+
     script_ctx_t *ctx;
+    heap_pool_t heap;
+
+    struct list code_list;
 } ScriptDisp;
 
 typedef struct _builtin_prop_t builtin_prop_t;
@@ -163,13 +182,6 @@ static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
     return dp->rgvarg + dp->cArgs-i-1;
 }
 
-typedef struct _dynamic_var_t {
-    struct _dynamic_var_t *next;
-    VARIANT v;
-    const WCHAR *name;
-    BOOL is_const;
-} dynamic_var_t;
-
 struct _script_ctx_t {
     IActiveScriptSite *site;
     LCID lcid;
@@ -186,16 +198,6 @@ struct _script_ctx_t {
 
     EXCEPINFO ei;
 
-    UINT global_vars_num;
-    UINT global_funcs_num;
-
-    dynamic_var_t **global_vars;
-    function_t **global_funcs;
-    class_desc_t *classes;
-    class_desc_t *procs;
-
-    heap_pool_t heap;
-
     struct list objects;
     struct list code_list;
     struct list named_items;
@@ -358,7 +360,7 @@ struct _vbscode_t {
     struct list entry;
 };
 
-static inline BOOL add_global_var(script_ctx_t *obj, dynamic_var_t *var)
+static inline BOOL add_global_var(ScriptDisp *obj, dynamic_var_t *var)
 {
     dynamic_var_t **vars;
 
@@ -375,7 +377,7 @@ static inline BOOL add_global_var(script_ctx_t *obj, dynamic_var_t *var)
     return TRUE;
 }
 
-static inline BOOL add_global_func(script_ctx_t *obj, function_t *func)
+static inline BOOL add_global_func(ScriptDisp *obj, function_t *func)
 {
     function_t **funcs;
 
-- 
2.21.0




More information about the wine-devel mailing list