Browse Source

[PATCH 100/188] writing proper tables binary information

From 2824a87b3f4fc9948124b48e50414f30a9b0d24f Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Tue, 17 Mar 2020 09:41:49 -0400

git-svn-id: branches/wasm@46096 -
nickysn 5 years ago
parent
commit
5997e5bb56
3 changed files with 43 additions and 1 deletions
  1. 38 0
      utils/wasmbin/wasmbinwriter.pas
  2. 4 1
      utils/wasmbin/wasmmodule.pas
  3. 1 0
      utils/wasmbin/watparser.pas

+ 38 - 0
utils/wasmbin/wasmbinwriter.pas

@@ -51,6 +51,7 @@ type
 
     procedure WriteImportSect;
     procedure WriteFuncTypeSect;
+    procedure WriteTableSect;
     procedure WriteFuncSect;
     procedure WriteExportSect;
     procedure WriteCodeSect;
@@ -84,8 +85,23 @@ type
 // returns the list of local arrays
 procedure GetLocalInfo(func: TWasmFunc; out loc: TLocalInfoArray);
 
+procedure WriteLimit(dst: TStream; amin, amax: LongWord);
+
 implementation
 
+procedure WriteLimit(dst: TStream; amin, amax: LongWord);
+begin
+  if not Assigned(dst) then Exit;
+  if amax>0 then begin
+    dst.WriteByte(1);
+    WriteU32(dst, amin);
+    WriteU32(dst, amax);
+  end else begin
+    dst.WriteByte(0);
+    WriteU32(dst, amin);
+  end;
+end;
+
 procedure GetLocalInfo(func: TWasmFunc; out loc: TLocalInfoArray);
 var
   i   : integer;
@@ -231,6 +247,12 @@ begin
     inc(writeSec);
   end;
 
+  // 04 tables section
+  if m.TableCount>0 then begin
+    WriteTableSect;
+    inc(writeSec);
+  end;
+
   // 07 export section
   if m.ExportCount>0 then begin
     WriteExportSect;
@@ -299,6 +321,22 @@ begin
   SectionEnd(sc);
 end;
 
+procedure TBinWriter.WriteTableSect;
+var
+  sc  : TSectionRec;
+  i   : integer;
+  t   : TWasmTable;
+begin
+  SectionBegin(SECT_TABLE, sc);
+  WriteU32(dst, module.TableCount);
+  for i:=0 to module.TableCount-1 do begin
+    t:=module.GetTable(i);
+    dst.WriteByte(t.elemsType);
+    WriteLimit(dst, t.min, t.max);
+  end;
+  SectionEnd(sc);
+end;
+
 procedure TBinWriter.WriteFuncSect;
 var
   sc : TSectionRec;

+ 4 - 1
utils/wasmbin/wasmmodule.pas

@@ -140,7 +140,10 @@ type
   { TWasmTable }
 
   TWasmTable = class(TObject)
-    id    : TWasmId;
+    id        : TWasmId;
+    elemsType : Byte; // type of elements
+    min       : LongWord;
+    max       : LongWord;
   end;
 
   { TWasmModule }

+ 1 - 0
utils/wasmbin/watparser.pas

@@ -381,6 +381,7 @@ begin
   sc.Next;
   ParseId(sc, dst.id);
   ConsumeToken(sc, weFuncRef);
+  dst.elemsType := elem_type;
   ConsumeToken(sc, weCloseBrace);
 end;