Browse Source

[PATCH 146/188] update writing globals section

From cabd10e07f9794a473b12411a1c527f423c6c5b6 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Wed, 25 Mar 2020 14:45:43 -0400

git-svn-id: branches/wasm@46142 -
nickysn 5 years ago
parent
commit
b828875a43
1 changed files with 27 additions and 0 deletions
  1. 27 0
      utils/wasmbin/wasmbinwriter.pas

+ 27 - 0
utils/wasmbin/wasmbinwriter.pas

@@ -53,6 +53,7 @@ type
     procedure WriteFuncTypeSect;
     procedure WriteFuncTypeSect;
     procedure WriteTableSect;
     procedure WriteTableSect;
     procedure WriteMemorySect;
     procedure WriteMemorySect;
+    procedure WriteGlobalSect;
     procedure WriteFuncSect;
     procedure WriteFuncSect;
     procedure WriteExportSect;
     procedure WriteExportSect;
     procedure WriteCodeSect;
     procedure WriteCodeSect;
@@ -261,6 +262,12 @@ begin
     inc(writeSec);
     inc(writeSec);
   end;
   end;
 
 
+  // 06 globals section
+  if m.GlobalCount>0 then begin
+    WriteGlobalSect;
+    inc(writeSec);
+  end;
+
   // 07 export section
   // 07 export section
   if m.ExportCount>0 then begin
   if m.ExportCount>0 then begin
     WriteExportSect;
     WriteExportSect;
@@ -368,6 +375,26 @@ begin
   SectionEnd(sc);
   SectionEnd(sc);
 end;
 end;
 
 
+procedure TBinWriter.WriteGlobalSect;
+var
+  sc : TSectionRec;
+  i  : integer;
+  g  : TWasmGlobal;
+begin
+  SectionBegin(SECT_GLOBAL, sc);
+
+  WriteU32(dst, module.GlobalCount);
+  for i:=0 to module.GlobalCount-1 do begin
+    g := module.GetGlobal(i);
+    dst.WriteByte(g.tp);
+    if g.isMutable then dst.WriteByte(1)
+    else dst.WriteByte(0);
+    WriteInstList(g.StartValue, sc.datapos);
+  end;
+
+  SectionEnd(sc);
+end;
+
 procedure TBinWriter.WriteFuncSect;
 procedure TBinWriter.WriteFuncSect;
 var
 var
   sc : TSectionRec;
   sc : TSectionRec;