From e82c98bfce885e79fd7f9f10cd8d6c060bb1f90d Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Sat, 1 Apr 2023 03:20:53 +0300 Subject: [PATCH] * wasm32: generate module info by traversing through current_module and its used_units list recursively. This should fix #40229 --- compiler/wasm32/nwasmutil.pas | 59 ++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/compiler/wasm32/nwasmutil.pas b/compiler/wasm32/nwasmutil.pas index 37d470d61b..f7eb228f48 100644 --- a/compiler/wasm32/nwasmutil.pas +++ b/compiler/wasm32/nwasmutil.pas @@ -69,6 +69,26 @@ implementation class procedure twasmnodeutils.InsertObjectInfo; + var + modules: array of tmodule; + + function ModuleExists(m: tmodule): Boolean; + var + q: tmodule; + begin + result:=true; + for q in modules do + if q=m then + exit; + result:=false; + end; + + procedure AddModule(m: tmodule); + begin + SetLength(modules,Length(modules)+1); + modules[High(modules)]:=m; + end; + procedure WriteImportDll(list: TAsmList; proc: tprocdef); begin thlcgwasm(hlcg).g_procdef(list,proc); @@ -76,22 +96,34 @@ implementation list.Concat(tai_import_name.create(proc.mangledname,proc.import_name^)); end; - procedure InsertUnitInfo(list : TAsmList;cur_unit: tused_unit); + procedure InsertModuleInfo(list : TAsmList;u: tmodule); var i: Integer; def : tdef; proc : tprocdef; + cur_unit : tused_unit; begin - if (cur_unit.u.moduleflags * [mf_init,mf_finalize])<>[] then + if ModuleExists(u) then + exit; + AddModule(u); + + cur_unit:=tused_unit(u.used_units.First); + while assigned(cur_unit) do begin - if mf_init in cur_unit.u.moduleflags then - list.Concat(tai_functype.create(make_mangledname('INIT$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[]))); - if mf_finalize in cur_unit.u.moduleflags then - list.Concat(tai_functype.create(make_mangledname('FINALIZE$',cur_unit.u.globalsymtable,''),TWasmFuncType.Create([],[]))); + InsertModuleInfo(list,cur_unit.u); + cur_unit:=tused_unit(cur_unit.Next); end; - for i:=0 to cur_unit.u.deflist.Count-1 do + + if ((u.moduleflags * [mf_init,mf_finalize])<>[]) and assigned(u.globalsymtable) then begin - def:=tdef(cur_unit.u.deflist[i]); + if mf_init in u.moduleflags then + list.Concat(tai_functype.create(make_mangledname('INIT$',u.globalsymtable,''),TWasmFuncType.Create([],[]))); + if mf_finalize in u.moduleflags then + list.Concat(tai_functype.create(make_mangledname('FINALIZE$',u.globalsymtable,''),TWasmFuncType.Create([],[]))); + end; + for i:=0 to u.deflist.Count-1 do + begin + def:=tdef(u.deflist[i]); if assigned(def) and (tdef(def).typ = procdef) then begin proc := tprocdef(def); @@ -112,6 +144,8 @@ implementation begin inherited; + FillChar(modules,sizeof(modules),0); + list:=current_asmdata.asmlists[al_start]; list.Concat(tai_globaltype.create(STACK_POINTER_SYM,wbt_i32,false)); @@ -143,16 +177,11 @@ implementation thlcgwasm(hlcg).g_procdef(list,proc); end; end; + InsertModuleInfo(list,current_module); cur_unit:=tused_unit(usedunits.First); while assigned(cur_unit) do begin - InsertUnitInfo(list,cur_unit); - cur_unit:=tused_unit(cur_unit.Next); - end; - cur_unit:=tused_unit(current_module.used_units.First); - while assigned(cur_unit) do - begin - InsertUnitInfo(list,cur_unit); + InsertModuleInfo(list,cur_unit.u); cur_unit:=tused_unit(cur_unit.Next); end; end;