Переглянути джерело

+ avoid duplicate functypes in the types section of the wasm module we produce

Nikolay Nikolov 3 роки тому
батько
коміт
a58368d20d
2 змінених файлів з 27 додано та 1 видалено
  1. 5 1
      compiler/ogwasm.pas
  2. 22 0
      compiler/wasm32/cpubase.pas

+ 5 - 1
compiler/ogwasm.pas

@@ -292,8 +292,12 @@ implementation
       end;
 
     procedure TWasmObjData.DeclareFuncType(ft: tai_functype);
+      var
+        i: Integer;
       begin
-        { todo: check and avoid adding duplicates }
+        for i:=low(FFuncTypes) to high(FFuncTypes) do
+          if ft.functype.Equals(FFuncTypes[i]) then
+            exit;
 
         SetLength(FFuncTypes,Length(FFuncTypes)+1);
         FFuncTypes[High(FFuncTypes)]:=TWasmFuncType.Create(ft.functype);

+ 22 - 0
compiler/wasm32/cpubase.pas

@@ -100,6 +100,7 @@ uses
         constructor Create(afunctype: TWasmFuncType);
         procedure add_param(param: TWasmBasicType);
         procedure add_result(res: TWasmBasicType);
+        function Equals(Obj: TObject): boolean; override;
       end;
 
       {# This should define the array of instructions as string }
@@ -424,4 +425,25 @@ uses
         results[High(results)]:=res;
       end;
 
+    function TWasmFuncType.Equals(Obj: TObject): boolean;
+      var
+        O: TWasmFuncType;
+      begin
+        if Obj=Self then
+          exit(true)
+        else if (Obj<>nil) and (Obj is TWasmFuncType) then
+          begin
+            O:=TWasmFuncType(Obj);
+            if (Length(params)<>Length(O.params)) or (Length(results)<>Length(O.results)) then
+              exit(false);
+            if (Length(params)>0) and (CompareByte(params[0],O.params[0],Length(params)*SizeOf(params[0]))<>0) then
+              exit(false);
+            if (Length(results)>0) and (CompareByte(results[0],O.results[0],Length(results)*SizeOf(results[0]))<>0) then
+              exit(false);
+            Result:=true;
+          end
+        else
+          Result:=inherited Equals(Obj);
+      end;
+
 end.