[PATCH 3/7] vbscript: Move the heap and global lists to the script dispatch object.

Gabriel Ivăncescu gabrielopcode at gmail.com
Tue Oct 29 10:19:36 CDT 2019


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

Their lifespan must be limited to the script dispatch, and contained within
the script dispatch's context (the pstrItemName argument when it will be
implemented). When the script is stopped, they must be released since the
persistent code will be re-add anyway to rebuild them.

 dlls/vbscript/compile.c  | 31 ++++++++++++++++---------------
 dlls/vbscript/interp.c   | 16 +++++++++-------
 dlls/vbscript/vbdisp.c   | 13 +++++++++++--
 dlls/vbscript/vbscript.c | 15 ---------------
 dlls/vbscript/vbscript.h | 28 ++++++++++++++--------------
 5 files changed, 50 insertions(+), 53 deletions(-)

diff --git a/dlls/vbscript/compile.c b/dlls/vbscript/compile.c
index 8852cd2..6ab8352 100644
--- a/dlls/vbscript/compile.c
+++ b/dlls/vbscript/compile.c
@@ -1737,23 +1737,23 @@ static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
     return S_OK;
 }
 
-static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
+static BOOL lookup_script_identifier(ScriptDisp *obj, const WCHAR *identifier)
 {
     class_desc_t *class;
     dynamic_var_t *var;
     function_t *func;
 
-    for(var = script->global_vars; var; var = var->next) {
+    for(var = obj->global_vars; var; var = var->next) {
         if(!wcsicmp(var->name, identifier))
             return TRUE;
     }
 
-    for(func = script->global_funcs; func; func = func->next) {
+    for(func = obj->global_funcs; func; func = func->next) {
         if(!wcsicmp(func->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;
     }
@@ -1763,26 +1763,27 @@ static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifi
 
 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
 {
+    ScriptDisp *obj = script->script_obj;
     class_desc_t *class;
     dynamic_var_t *var;
     function_t *func;
 
     for(var = ctx->global_vars; var; var = var->next) {
-        if(lookup_script_identifier(script, var->name)) {
+        if(lookup_script_identifier(obj, var->name)) {
             FIXME("%s: redefined\n", debugstr_w(var->name));
             return E_FAIL;
         }
     }
 
     for(func = ctx->funcs; func; func = func->next) {
-        if(lookup_script_identifier(script, func->name)) {
+        if(lookup_script_identifier(obj, func->name)) {
             FIXME("%s: redefined\n", debugstr_w(func->name));
             return E_FAIL;
         }
     }
 
     for(class = ctx->classes; class; class = class->next) {
-        if(lookup_script_identifier(script, class->name)) {
+        if(lookup_script_identifier(obj, class->name)) {
             FIXME("%s: redefined\n", debugstr_w(class->name));
             return E_FAIL;
         }
@@ -1917,15 +1918,15 @@ HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *deli
 
         for(var = ctx.global_vars; var->next; var = var->next);
 
-        var->next = script->global_vars;
-        script->global_vars = ctx.global_vars;
+        var->next = script->script_obj->global_vars;
+        script->script_obj->global_vars = ctx.global_vars;
     }
 
     if(ctx.funcs) {
         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
 
-        new_func->next = script->global_funcs;
-        script->global_funcs = ctx.funcs;
+        new_func->next = script->script_obj->global_funcs;
+        script->script_obj->global_funcs = ctx.funcs;
     }
 
     if(ctx.classes) {
@@ -1938,8 +1939,8 @@ HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *deli
             class = class->next;
         }
 
-        class->next = script->classes;
-        script->classes = ctx.classes;
+        class->next = script->script_obj->classes;
+        script->script_obj->classes = ctx.classes;
     }
 
     if(TRACE_ON(vbscript_disas))
@@ -1971,8 +1972,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;
 
     list_add_tail(&script->script_obj->noexec_code_list, &code->entry);
 
diff --git a/dlls/vbscript/interp.c b/dlls/vbscript/interp.c
index 60c4340..3c9595f 100644
--- a/dlls/vbscript/interp.c
+++ b/dlls/vbscript/interp.c
@@ -97,6 +97,7 @@ static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *re
 
 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;
     function_t *func;
     IDispatch *disp;
@@ -128,7 +129,7 @@ static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_
         }
     }
 
-    if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
+    if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? script_obj->global_vars : ctx->dynamic_vars, name, ref))
         return S_OK;
 
     if(ctx->func->type != FUNC_GLOBAL) {
@@ -162,10 +163,10 @@ static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_
         }
     }
 
-    if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
+    if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(script_obj->global_vars, name, ref))
         return S_OK;
 
-    for(func = ctx->script->global_funcs; func; func = func->next) {
+    for(func = script_obj->global_funcs; func; func = func->next) {
         if(!wcsicmp(func->name, name)) {
             ref->type = REF_FUNC;
             ref->u.f = func;
@@ -207,12 +208,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)
@@ -228,8 +230,8 @@ 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) {
-        new_var->next = ctx->script->global_vars;
-        ctx->script->global_vars = new_var;
+        new_var->next = script_obj->global_vars;
+        script_obj->global_vars = new_var;
     }else {
         new_var->next = ctx->dynamic_vars;
         ctx->dynamic_vars = new_var;
@@ -1066,7 +1068,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 e84f31b..3ea3feb 100644
--- a/dlls/vbscript/vbdisp.c
+++ b/dlls/vbscript/vbdisp.c
@@ -617,11 +617,19 @@ static ULONG WINAPI ScriptDisp_Release(IDispatchEx *iface)
     if(!ref) {
         assert(!This->ctx);
 
+        release_dynamic_vars(This->global_vars);
+        while (This->procs)
+        {
+            class_desc_t *class_desc = This->procs;
+            This->procs = class_desc->next;
+            heap_free(class_desc);
+        }
         while (!list_empty(&This->code_list))
             release_vbscode(LIST_ENTRY(list_head(&This->code_list), vbscode_t, entry));
         while (!list_empty(&This->noexec_code_list))
             release_vbscode(LIST_ENTRY(list_head(&This->noexec_code_list), vbscode_t, entry));
 
+        heap_pool_free(&This->heap);
         heap_free(This->ident_map);
         heap_free(This);
     }
@@ -697,7 +705,7 @@ static HRESULT WINAPI ScriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DW
         }
     }
 
-    for(var = This->ctx->global_vars; var; var = var->next) {
+    for(var = This->global_vars; var; var = var->next) {
         if(!wcsicmp(var->name, bstrName)) {
             ident = add_ident(This, var->name);
             if(!ident)
@@ -710,7 +718,7 @@ static HRESULT WINAPI ScriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DW
         }
     }
 
-    for(func = This->ctx->global_funcs; func; func = func->next) {
+    for(func = This->global_funcs; func; func = func->next) {
         if(!wcsicmp(func->name, bstrName)) {
             ident = add_ident(This, func->name);
             if(!ident)
@@ -835,6 +843,7 @@ HRESULT create_script_disp(script_ctx_t *ctx, ScriptDisp **ret)
     script_disp->ctx = ctx;
     list_init(&script_disp->code_list);
     list_init(&script_disp->noexec_code_list);
+    heap_pool_init(&script_disp->heap);
 
     *ret = script_disp;
     return S_OK;
diff --git a/dlls/vbscript/vbscript.c b/dlls/vbscript/vbscript.c
index 5e39f76..f4ac249 100644
--- a/dlls/vbscript/vbscript.c
+++ b/dlls/vbscript/vbscript.c
@@ -130,14 +130,9 @@ IDispatch *lookup_named_item(script_ctx_t *ctx, const WCHAR *name, unsigned flag
 
 static void release_script(script_ctx_t *ctx)
 {
-    class_desc_t *class_desc;
-
     collect_objects(ctx);
     clear_ei(&ctx->ei);
 
-    release_dynamic_vars(ctx->global_vars);
-    ctx->global_vars = NULL;
-
     while(!list_empty(&ctx->named_items)) {
         named_item_t *iter = LIST_ENTRY(list_head(&ctx->named_items), named_item_t, entry);
 
@@ -148,13 +143,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;
@@ -179,8 +167,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)
@@ -963,7 +949,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 cada4e2..9ce309e 100644
--- a/dlls/vbscript/vbscript.h
+++ b/dlls/vbscript/vbscript.h
@@ -120,6 +120,13 @@ 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 _ident_map_t ident_map_t;
 
 typedef struct {
@@ -133,6 +140,13 @@ typedef struct {
     script_ctx_t *ctx;
     struct list code_list;
     struct list noexec_code_list;
+
+    dynamic_var_t *global_vars;
+    function_t *global_funcs;
+    class_desc_t *classes;
+    class_desc_t *procs;
+
+    heap_pool_t heap;
 } ScriptDisp;
 
 typedef struct _builtin_prop_t builtin_prop_t;
@@ -166,13 +180,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;
@@ -189,13 +196,6 @@ struct _script_ctx_t {
 
     EXCEPINFO ei;
 
-    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;
-- 
2.21.0




More information about the wine-devel mailing list