Browse Source

[PATCH 094/188] added parsing of table (without elements)

From aeb69f66c429c5ac82d725301e2ea0fad453cac7 Mon Sep 17 00:00:00 2001
From: Dmitry Boyarintsev <[email protected]>
Date: Mon, 16 Mar 2020 15:38:25 -0400

git-svn-id: branches/wasm@46090 -
nickysn 5 years ago
parent
commit
1b5d7087ea
2 changed files with 66 additions and 0 deletions
  1. 40 0
      utils/wasmbin/wasmmodule.pas
  2. 26 0
      utils/wasmbin/watparser.pas

+ 40 - 0
utils/wasmbin/wasmmodule.pas

@@ -130,6 +130,19 @@ type
     function AddFunc: TWasmFunc;
     function AddFunc: TWasmFunc;
   end;
   end;
 
 
+  { TWasmId }
+
+  TWasmId = record
+    idNum : integer;
+    id    : string;
+  end;
+
+  { TWasmTable }
+
+  TWasmTable = class(TObject)
+    id    : TWasmId;
+  end;
+
   { TWasmModule }
   { TWasmModule }
 
 
   TWasmModule = class(TObject)
   TWasmModule = class(TObject)
@@ -138,10 +151,15 @@ type
     types   : TList;
     types   : TList;
     funcs   : TList;
     funcs   : TList;
     exp     : TList;
     exp     : TList;
+    tables  : TList;
   public
   public
     constructor Create;
     constructor Create;
     destructor Destroy; override;
     destructor Destroy; override;
 
 
+    function AddTable: TWasmTable;
+    function GetTable(i: integer): TWasmTable;
+    function TableCount: Integer;
+
     function AddImport: TWasmImport;
     function AddImport: TWasmImport;
     function GetImport(i: integer): TWasmImport;
     function GetImport(i: integer): TWasmImport;
     function ImportCount: Integer;
     function ImportCount: Integer;
@@ -401,10 +419,13 @@ begin
   funcs := TList.Create;
   funcs := TList.Create;
   exp := TList.Create;
   exp := TList.Create;
   imports := TList.Create;
   imports := TList.Create;
+  tables := TList.Create;
 end;
 end;
 
 
 destructor TWasmModule.Destroy;
 destructor TWasmModule.Destroy;
 begin
 begin
+  ClearList(tables);
+  tables.Free;
   ClearList(imports);
   ClearList(imports);
   imports.Free;
   imports.Free;
   ClearList(exp);
   ClearList(exp);
@@ -416,6 +437,25 @@ begin
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
+function TWasmModule.AddTable: TWasmTable;
+begin
+  Result:=TWasmTable.Create;
+  tables.Add(Result);
+end;
+
+function TWasmModule.GetTable(i: integer): TWasmTable;
+begin
+  if (i>=0) and (i<tables.Count) then
+    Result:=TWasmTable(tables[i])
+  else
+    Result:=nil;
+end;
+
+function TWasmModule.TableCount: Integer;
+begin
+  Result:=tables.Count;
+end;
+
 function TWasmModule.AddImport: TWasmImport;
 function TWasmModule.AddImport: TWasmImport;
 begin
 begin
   Result:=TWasmImport.Create;
   Result:=TWasmImport.Create;

+ 26 - 0
utils/wasmbin/watparser.pas

@@ -348,6 +348,29 @@ begin
   sc.Next;
   sc.Next;
 end;
 end;
 
 
+function ParseId(sc: TWatScanner; var id: TWasmId): boolean;
+begin
+  if sc.token = weNumber then begin
+    id.idNum := sc.resInt32;
+    id.id := '';
+    Result := true;
+  end else if sc.token = weIdent then begin
+    id.id := sc.resText;
+    id.idNum := -1;
+    Result := true;
+  end else
+    Result := false;
+  if Result then sc.Next;
+end;
+
+procedure ParseTable(sc: TWatScanner; dst: TWasmTable);
+begin
+  sc.Next;
+  ParseId(sc, dst.id);
+  ConsumeToken(sc, weFuncRef);
+  ConsumeToken(sc, weCloseBrace);
+end;
+
 procedure ParseModuleInt(sc: TWatScanner; dst: TWasmModule);
 procedure ParseModuleInt(sc: TWatScanner; dst: TWasmModule);
 var
 var
   tk      : TWatToken;
   tk      : TWatToken;
@@ -372,6 +395,9 @@ begin
           ParseImport(sc, imp);
           ParseImport(sc, imp);
           symlist.Clear;
           symlist.Clear;
         end;
         end;
+        weTable: begin
+          ParseTable(sc, dst.AddTable)
+        end;
         weFunc: begin
         weFunc: begin
           f:=dst.AddFunc;
           f:=dst.AddFunc;
           symlist.ToLinkInfo(f.LinkInfo);
           symlist.ToLinkInfo(f.LinkInfo);