Browse Source

webidltopas: started outputformat

mattias 3 years ago
parent
commit
ad9383f2c0
3 changed files with 271 additions and 105 deletions
  1. 0 1
      packages/webidl/src/webidlparser.pp
  2. 156 75
      packages/webidl/src/webidltopas.pp
  3. 115 29
      utils/pas2js/webidl2pas.pp

+ 0 - 1
packages/webidl/src/webidlparser.pp

@@ -1608,7 +1608,6 @@ begin
   Result:=Add(FDefinitions,aClass,AName);
   Result:=Add(FDefinitions,aClass,AName);
 end;
 end;
 
 
-
 function TWebIDLContext.Add(aParent: TIDLBaseObject; aClass: TIDLDefinitionClass; const AName: UTF8String): TIDLDefinition;
 function TWebIDLContext.Add(aParent: TIDLBaseObject; aClass: TIDLDefinitionClass; const AName: UTF8String): TIDLDefinition;
 begin
 begin
   if Assigned(aParent) then
   if Assigned(aParent) then

+ 156 - 75
packages/webidl/src/webidltopas.pp

@@ -23,7 +23,7 @@ uses
 
 
 Type
 Type
 
 
-  { TWebIDLToPas }
+  { TBaseWebIDLToPas }
 
 
   { TPasData }
   { TPasData }
 
 
@@ -35,10 +35,16 @@ Type
     Property PasName : String read FPasName;
     Property PasName : String read FPasName;
   end;
   end;
 
 
-  TConversionOption = (coDictionaryAsClass,coUseNativeTypeAliases,coExternalConst,coExpandUnionTypeArgs,coaddOptionsToheader);
+  TConversionOption = (
+    coDictionaryAsClass,
+    coUseNativeTypeAliases,
+    coExternalConst,
+    coExpandUnionTypeArgs,
+    coAddOptionsToHeader
+    );
   TConversionOptions = Set of TConversionOption;
   TConversionOptions = Set of TConversionOption;
 
 
-  TWebIDLToPas = Class(TPascalCodeGenerator)
+  TBaseWebIDLToPas = Class(TPascalCodeGenerator)
   private
   private
     FClassPrefix: String;
     FClassPrefix: String;
     FClassSuffix: String;
     FClassSuffix: String;
@@ -127,10 +133,11 @@ Type
     procedure WriteIncludeInterfaceCode; virtual;
     procedure WriteIncludeInterfaceCode; virtual;
     Property Context : TWebIDLContext Read FContext;
     Property Context : TWebIDLContext Read FContext;
   Public
   Public
-    Constructor Create(Aowner : TComponent); override;
-    Destructor Destroy; override;
-    Procedure Execute;
-  Published
+    constructor Create(Aowner : TComponent); override;
+    destructor Destroy; override;
+    procedure Execute; virtual;
+    procedure WriteOptions; virtual;
+  Public
     Property InputFileName : String Read FInputFileName Write FInputFileName;
     Property InputFileName : String Read FInputFileName Write FInputFileName;
     Property OutputFileName : String Read FOutputFileName Write FOutputFileName;
     Property OutputFileName : String Read FOutputFileName Write FOutputFileName;
     Property Verbose : Boolean Read FVerbose Write FVerbose;
     Property Verbose : Boolean Read FVerbose Write FVerbose;
@@ -145,6 +152,42 @@ Type
     Property DictionaryClassParent : String Read FDictionaryClassParent Write FDictionaryClassParent;
     Property DictionaryClassParent : String Read FDictionaryClassParent Write FDictionaryClassParent;
   end;
   end;
 
 
+  { TWebIDLToPas2js }
+
+  TWebIDLToPas2js = class(TBaseWebIDLToPas)
+  Published
+    Property InputFileName;
+    Property OutputFileName;
+    Property Verbose;
+    Property FieldPrefix;
+    Property ClassPrefix;
+    Property ClassSuffix;
+    Property Options;
+    Property WebIDLVersion;
+    Property TypeAliases;
+    Property IncludeInterfaceCode;
+    Property IncludeImplementationCode;
+    Property DictionaryClassParent;
+  end;
+
+  { TWebIDLToPasWasmJob }
+
+  TWebIDLToPasWasmJob = class(TBaseWebIDLToPas)
+  Published
+    Property InputFileName;
+    Property OutputFileName;
+    Property Verbose;
+    Property FieldPrefix;
+    Property ClassPrefix;
+    Property ClassSuffix;
+    Property Options;
+    Property WebIDLVersion;
+    Property TypeAliases;
+    Property IncludeInterfaceCode;
+    Property IncludeImplementationCode;
+    Property DictionaryClassParent;
+  end;
+
 implementation
 implementation
 
 
 uses typinfo;
 uses typinfo;
@@ -156,27 +199,27 @@ begin
   FPasName:=APasName;
   FPasName:=APasName;
 end;
 end;
 
 
-{ TWebIDLToPas }
+{ TBaseWebIDLToPas }
 
 
-function TWebIDLToPas.CreateContext: TWebIDLContext;
+function TBaseWebIDLToPas.CreateContext: TWebIDLContext;
 begin
 begin
   Result:=TWebIDLContext.Create(True);
   Result:=TWebIDLContext.Create(True);
 end;
 end;
 
 
-function TWebIDLToPas.CreateScanner(S : TStream) :  TWebIDLScanner;
+function TBaseWebIDLToPas.CreateScanner(S : TStream) :  TWebIDLScanner;
 
 
 begin
 begin
   Result:=TWebIDLScanner.Create(S);
   Result:=TWebIDLScanner.Create(S);
 end;
 end;
 
 
-function TWebIDLToPas.CreateParser(aContext : TWebIDLContext;S : TWebIDLScanner) :  TWebIDLParser;
+function TBaseWebIDLToPas.CreateParser(aContext : TWebIDLContext;S : TWebIDLScanner) :  TWebIDLParser;
 
 
 begin
 begin
   Result:=TWebIDLParser.Create(aContext,S);
   Result:=TWebIDLParser.Create(aContext,S);
   Result.Version:=FWebIDLVersion;
   Result.Version:=FWebIDLVersion;
 end;
 end;
 
 
-procedure TWebIDLToPas.Parse;
+procedure TBaseWebIDLToPas.Parse;
 
 
 Var
 Var
   F : TFileStream;
   F : TFileStream;
@@ -197,7 +240,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TWebIDLToPas.GetName(ADef: TIDLDefinition): String;
+function TBaseWebIDLToPas.GetName(ADef: TIDLDefinition): String;
 
 
 begin
 begin
   If Assigned(ADef) and (TObject(ADef.Data) is TPasData) then
   If Assigned(ADef) and (TObject(ADef.Data) is TPasData) then
@@ -206,7 +249,7 @@ begin
     Result:=ADef.Name;
     Result:=ADef.Name;
 end;
 end;
 
 
-function TWebIDLToPas.HaveConsts(aList: TIDLDefinitionList): Boolean;
+function TBaseWebIDLToPas.HaveConsts(aList: TIDLDefinitionList): Boolean;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -218,7 +261,7 @@ begin
       Exit(True);
       Exit(True);
 end;
 end;
 
 
-function TWebIDLToPas.WritePrivateReadOnlyFields(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WritePrivateReadOnlyFields(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -233,7 +276,7 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.WriteProperties(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteProperties(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -248,7 +291,7 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.WriteConst(aConst: TIDLConstDefinition): Boolean;
+function TBaseWebIDLToPas.WriteConst(aConst: TIDLConstDefinition): Boolean;
 
 
 Const
 Const
   ConstTypes : Array[TConstType] of String =
   ConstTypes : Array[TConstType] of String =
@@ -273,7 +316,7 @@ begin
     end;
     end;
 end;
 end;
 
 
-function TWebIDLToPas.WriteConsts(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteConsts(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -289,7 +332,7 @@ begin
   Undent;
   Undent;
 end;
 end;
 
 
-function TWebIDLToPas.WritePlainFields(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WritePlainFields(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -307,7 +350,7 @@ begin
   Undent;
   Undent;
 end;
 end;
 
 
-function TWebIDLToPas.WriteDictionaryField(
+function TBaseWebIDLToPas.WriteDictionaryField(
   aField: TIDLDictionaryMemberDefinition): Boolean;
   aField: TIDLDictionaryMemberDefinition): Boolean;
 
 
 Var
 Var
@@ -327,7 +370,7 @@ begin
   AddLn(Def);
   AddLn(Def);
 end;
 end;
 
 
-function TWebIDLToPas.WriteDictionaryFields(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteDictionaryFields(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -343,7 +386,7 @@ begin
   Undent;
   Undent;
 end;
 end;
 
 
-function TWebIDLToPas.WriteMethodDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteMethodDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -358,7 +401,7 @@ begin
            Inc(Result);
            Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.AddSequenceDef(ST: TIDLSequenceTypeDefDefinition
+function TBaseWebIDLToPas.AddSequenceDef(ST: TIDLSequenceTypeDefDefinition
   ): Boolean;
   ): Boolean;
 
 
 var
 var
@@ -375,7 +418,7 @@ begin
     end;
     end;
 end;
 end;
 
 
-function TWebIDLToPas.WriteFunctionImplicitTypes(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteFunctionImplicitTypes(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D,D2,D3 : TIDLDefinition;
   D,D2,D3 : TIDLDefinition;
@@ -412,7 +455,7 @@ begin
     AddLn('');
     AddLn('');
 end;
 end;
 
 
-function TWebIDLToPas.WriteAttributeImplicitTypes(aList: TIDLDefinitionList
+function TBaseWebIDLToPas.WriteAttributeImplicitTypes(aList: TIDLDefinitionList
   ): Integer;
   ): Integer;
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -427,7 +470,7 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.WriteDictionaryMemberImplicitTypes(
+function TBaseWebIDLToPas.WriteDictionaryMemberImplicitTypes(
   aList: TIDLDefinitionList): Integer;
   aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
@@ -443,7 +486,7 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-procedure TWebIDLToPas.EnsureUniqueNames(ML : TIDLDefinitionList);
+procedure TBaseWebIDLToPas.EnsureUniqueNames(ML : TIDLDefinitionList);
 
 
 Var
 Var
   L : TFPObjectHashTable;
   L : TFPObjectHashTable;
@@ -506,7 +549,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TWebIDLToPas.WriteInterfaceDef(Intf: TIDLInterfaceDefinition): Boolean;
+function TBaseWebIDLToPas.WriteInterfaceDef(Intf: TIDLInterfaceDefinition): Boolean;
 
 
 Var
 Var
   CN,PN : String;
   CN,PN : String;
@@ -556,7 +599,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TWebIDLToPas.WriteDictionaryDef(aDict: TIDLDictionaryDefinition
+function TBaseWebIDLToPas.WriteDictionaryDef(aDict: TIDLDictionaryDefinition
   ): Boolean;
   ): Boolean;
 
 
 Var
 Var
@@ -591,7 +634,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-constructor TWebIDLToPas.Create(Aowner: TComponent);
+constructor TBaseWebIDLToPas.Create(Aowner: TComponent);
 begin
 begin
   inherited Create(Aowner);
   inherited Create(Aowner);
   WebIDLVersion:=v2;
   WebIDLVersion:=v2;
@@ -607,7 +650,7 @@ begin
 end;
 end;
 
 
 
 
-destructor TWebIDLToPas.Destroy;
+destructor TBaseWebIDLToPas.Destroy;
 begin
 begin
   FreeAndNil(FIncludeInterfaceCode);
   FreeAndNil(FIncludeInterfaceCode);
   FreeAndNil(FIncludeImplementationCode);
   FreeAndNil(FIncludeImplementationCode);
@@ -617,7 +660,7 @@ begin
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
-procedure TWebIDLToPas.WriteImplementation;
+procedure TBaseWebIDLToPas.WriteImplementation;
 
 
 Var
 Var
   S : String;
   S : String;
@@ -629,7 +672,7 @@ begin
   Addln('');
   Addln('');
 end;
 end;
 
 
-function TWebIDLToPas.GetTypeName(aTypeDef : TIDLTypeDefDefinition; ForTypeDef : Boolean = False): String;
+function TBaseWebIDLToPas.GetTypeName(aTypeDef : TIDLTypeDefDefinition; ForTypeDef : Boolean = False): String;
 
 
 begin
 begin
   if ATypeDef is TIDLSequenceTypeDefDefinition then
   if ATypeDef is TIDLSequenceTypeDefDefinition then
@@ -646,7 +689,7 @@ begin
     Result:=GetTypeName(aTypeDef.TypeName,ForTypeDef);
     Result:=GetTypeName(aTypeDef.TypeName,ForTypeDef);
 end;
 end;
 
 
-function TWebIDLToPas.GetTypeName(const aTypeName: String; ForTypeDef: Boolean
+function TBaseWebIDLToPas.GetTypeName(const aTypeName: String; ForTypeDef: Boolean
   ): String;
   ): String;
 
 
 
 
@@ -710,7 +753,7 @@ begin
   Result:=TN;
   Result:=TN;
 end;
 end;
 
 
-function TWebIDLToPas.WritePrivateReadOnlyField(aAttr: TIDLAttributeDefinition
+function TBaseWebIDLToPas.WritePrivateReadOnlyField(aAttr: TIDLAttributeDefinition
   ): Boolean;
   ): Boolean;
 
 
 begin
 begin
@@ -718,7 +761,7 @@ begin
   Result:=true;
   Result:=true;
 end;
 end;
 
 
-function TWebIDLToPas.WriteField(aAttr: TIDLAttributeDefinition): Boolean;
+function TBaseWebIDLToPas.WriteField(aAttr: TIDLAttributeDefinition): Boolean;
 
 
 Var
 Var
   Def,TN,N : String;
   Def,TN,N : String;
@@ -742,7 +785,7 @@ begin
   AddLn(Def);
   AddLn(Def);
 end;
 end;
 
 
-function TWebIDLToPas.WriteReadonlyProperty(aAttr: TIDLAttributeDefinition
+function TBaseWebIDLToPas.WriteReadonlyProperty(aAttr: TIDLAttributeDefinition
   ): Boolean;
   ): Boolean;
 
 
 Var
 Var
@@ -759,7 +802,7 @@ begin
 end;
 end;
 
 
 
 
-function TWebIDLToPas.WriteForwardClassDef(D: TIDLStructuredDefinition): Boolean;
+function TBaseWebIDLToPas.WriteForwardClassDef(D: TIDLStructuredDefinition): Boolean;
 
 
 begin
 begin
   Result:=not D.IsPartial;
   Result:=not D.IsPartial;
@@ -767,7 +810,7 @@ begin
     AddLn('%s = Class;',[GetName(D)]);
     AddLn('%s = Class;',[GetName(D)]);
 end;
 end;
 
 
-function TWebIDLToPas.WriteForwardClassDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteForwardClassDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -786,14 +829,14 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-procedure TWebIDLToPas.WriteSequenceDef(aDef : TIDLSequenceTypeDefDefinition);
+procedure TBaseWebIDLToPas.WriteSequenceDef(aDef : TIDLSequenceTypeDefDefinition);
 
 
 begin
 begin
   Addln('%s = array of %s;',[GetName(aDef),GetTypeName(aDef.ElementType)])
   Addln('%s = array of %s;',[GetName(aDef),GetTypeName(aDef.ElementType)])
 end;
 end;
 
 
 
 
-procedure TWebIDLToPas.WriteUnionDef(aDef : TIDLUnionTypeDefDefinition);
+procedure TBaseWebIDLToPas.WriteUnionDef(aDef : TIDLUnionTypeDefDefinition);
 
 
 Var
 Var
   S : UTF8String;
   S : UTF8String;
@@ -811,13 +854,13 @@ begin
 end;
 end;
 
 
 
 
-procedure TWebIDLToPas.WritePromiseDef(aDef : TIDLPromiseTypeDefDefinition);
+procedure TBaseWebIDLToPas.WritePromiseDef(aDef : TIDLPromiseTypeDefDefinition);
 
 
 begin
 begin
   AddLn('%s = TJSPromise;',[GetName(aDef)]);
   AddLn('%s = TJSPromise;',[GetName(aDef)]);
 end;
 end;
 
 
-procedure TWebIDLToPas.WriteAliasTypeDef(aDef : TIDLTypeDefDefinition);
+procedure TBaseWebIDLToPas.WriteAliasTypeDef(aDef : TIDLTypeDefDefinition);
 
 
 Var
 Var
   TN : String;
   TN : String;
@@ -827,7 +870,7 @@ begin
   AddLn('%s = %s;',[GetName(aDef),TN]);
   AddLn('%s = %s;',[GetName(aDef),TN]);
 end;
 end;
 
 
-function TWebIDLToPas.WriteTypeDef(aDef: TIDLTypeDefDefinition): Boolean;
+function TBaseWebIDLToPas.WriteTypeDef(aDef: TIDLTypeDefDefinition): Boolean;
 
 
 begin
 begin
   Result:=True;
   Result:=True;
@@ -843,7 +886,7 @@ begin
     WriteAliasTypeDef(aDef);
     WriteAliasTypeDef(aDef);
 end;
 end;
 
 
-function TWebIDLToPas.WriteRecordDef(aDef: TIDLRecordDefinition): Boolean;
+function TBaseWebIDLToPas.WriteRecordDef(aDef: TIDLRecordDefinition): Boolean;
 
 
 Var
 Var
   KT,VT : String;
   KT,VT : String;
@@ -865,7 +908,7 @@ begin
   AddLn('end;');
   AddLn('end;');
 end;
 end;
 
 
-function TWebIDLToPas.WriteTypeDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteTypeDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -880,14 +923,14 @@ begin
         Inc(Result);
         Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.WriteEnumDef(aDef: TIDLEnumDefinition): Boolean;
+function TBaseWebIDLToPas.WriteEnumDef(aDef: TIDLEnumDefinition): Boolean;
 
 
 begin
 begin
   Result:=True;
   Result:=True;
   AddLn('%s = String;',[GetName(aDef)]);
   AddLn('%s = String;',[GetName(aDef)]);
 end;
 end;
 
 
-function TWebIDLToPas.WriteEnumDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteEnumDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -902,7 +945,7 @@ begin
         Inc(Result);
         Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.GetArguments(aList: TIDLDefinitionList;
+function TBaseWebIDLToPas.GetArguments(aList: TIDLDefinitionList;
   ForceBrackets: Boolean): String;
   ForceBrackets: Boolean): String;
 
 
 Var
 Var
@@ -929,7 +972,7 @@ Type
   // Additional arguments can never be added to a partial list...
   // Additional arguments can never be added to a partial list...
   TIDLPartialDefinitionList = Class(TIDLDefinitionList);
   TIDLPartialDefinitionList = Class(TIDLDefinitionList);
 
 
-function TWebIDLToPas.CloneNonPartialArgumentList(aList: TFPObjectlist;
+function TBaseWebIDLToPas.CloneNonPartialArgumentList(aList: TFPObjectlist;
   ADest: TFPObjectlist; AsPartial: Boolean): integer;
   ADest: TFPObjectlist; AsPartial: Boolean): integer;
 
 
 Var
 Var
@@ -964,7 +1007,7 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TWebIDLToPas.AddArgumentToOverloads(aList: TFPObjectlist; AName,ATypeName : String);
+procedure TBaseWebIDLToPas.AddArgumentToOverloads(aList: TFPObjectlist; AName,ATypeName : String);
 
 
 Var
 Var
   I : Integer;
   I : Integer;
@@ -986,7 +1029,7 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TWebIDLToPas.AddArgumentToOverloads(aList: TFPObjectlist; adef: TIDLArgumentDefinition);
+procedure TBaseWebIDLToPas.AddArgumentToOverloads(aList: TFPObjectlist; adef: TIDLArgumentDefinition);
 
 
 Var
 Var
   I : Integer;
   I : Integer;
@@ -1009,7 +1052,7 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TWebIDLToPas.AddUnionOverloads(aList: TFPObjectlist; AName : String; UT : TIDLUnionTypeDefDefinition);
+procedure TBaseWebIDLToPas.AddUnionOverloads(aList: TFPObjectlist; AName : String; UT : TIDLUnionTypeDefDefinition);
 
 
 Var
 Var
   L,L2 : TFPObjectList;
   L,L2 : TFPObjectList;
@@ -1056,7 +1099,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TWebIDLToPas.CheckUnionTypeDefinition(D: TIDLDefinition
+function TBaseWebIDLToPas.CheckUnionTypeDefinition(D: TIDLDefinition
   ): TIDLUnionTypeDefDefinition;
   ): TIDLUnionTypeDefDefinition;
 
 
 begin
 begin
@@ -1071,7 +1114,7 @@ begin
     end
     end
 end;
 end;
 
 
-procedure TWebIDLToPas.AddOverloads(aList: TFPObjectlist;
+procedure TBaseWebIDLToPas.AddOverloads(aList: TFPObjectlist;
   adef: TIDLFunctionDefinition; aIdx: Integer);
   adef: TIDLFunctionDefinition; aIdx: Integer);
 
 
 Var
 Var
@@ -1097,7 +1140,7 @@ begin
   AddOverloads(aList,aDef,aIdx+1);
   AddOverloads(aList,aDef,aIdx+1);
 end;
 end;
 
 
-function TWebIDLToPas.GetOverloads(aDef: TIDLFunctionDefinition): TFPObjectlist;
+function TBaseWebIDLToPas.GetOverloads(aDef: TIDLFunctionDefinition): TFPObjectlist;
 
 
 begin
 begin
   Result:=TFPObjectList.Create;
   Result:=TFPObjectList.Create;
@@ -1110,7 +1153,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TWebIDLToPas.WriteFunctionTypeDefinition(aDef: TIDLFunctionDefinition): Boolean;
+function TBaseWebIDLToPas.WriteFunctionTypeDefinition(aDef: TIDLFunctionDefinition): Boolean;
 
 
 Var
 Var
   FN,RT,Args : String;
   FN,RT,Args : String;
@@ -1128,7 +1171,7 @@ begin
     AddLn('%s = function %s: %s;',[FN,Args,RT])
     AddLn('%s = function %s: %s;',[FN,Args,RT])
 end;
 end;
 
 
-function TWebIDLToPas.WriteFunctionDefinition(aDef: TIDLFunctionDefinition): Boolean;
+function TBaseWebIDLToPas.WriteFunctionDefinition(aDef: TIDLFunctionDefinition): Boolean;
 
 
 Var
 Var
   FN,RT,Suff,Args : String;
   FN,RT,Suff,Args : String;
@@ -1173,7 +1216,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TWebIDLToPas.WriteCallBackDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteCallBackDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -1189,7 +1232,7 @@ begin
            Inc(Result);
            Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.WriteDictionaryDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteDictionaryDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -1205,7 +1248,7 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-function TWebIDLToPas.WriteInterfaceDefs(aList: TIDLDefinitionList): Integer;
+function TBaseWebIDLToPas.WriteInterfaceDefs(aList: TIDLDefinitionList): Integer;
 
 
 Var
 Var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -1221,7 +1264,7 @@ begin
           Inc(Result);
           Inc(Result);
 end;
 end;
 
 
-procedure TWebIDLToPas.Getoptions(L : TStrings);
+procedure TBaseWebIDLToPas.Getoptions(L : TStrings);
 
 
 Var
 Var
   S : String;
   S : String;
@@ -1252,7 +1295,7 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TWebIDLToPas.AddOptionsToHeader;
+procedure TBaseWebIDLToPas.AddOptionsToHeader;
 
 
 Var
 Var
   L : TStrings;
   L : TStrings;
@@ -1266,7 +1309,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-procedure TWebIDLToPas.WriteIncludeInterfaceCode;
+procedure TBaseWebIDLToPas.WriteIncludeInterfaceCode;
 
 
 Var
 Var
   S : String;
   S : String;
@@ -1276,12 +1319,12 @@ begin
     Addln(S);
     Addln(S);
 end;
 end;
 
 
-procedure TWebIDLToPas.WritePascal;
+procedure TBaseWebIDLToPas.WritePascal;
 
 
 begin
 begin
   CreateUnitClause;
   CreateUnitClause;
   CreateHeader;
   CreateHeader;
-  if coaddOptionsToheader in Options then
+  if coAddOptionsToHeader in Options then
     AddOptionsToHeader;
     AddOptionsToHeader;
   EnsureSection(csType);
   EnsureSection(csType);
   Indent;
   Indent;
@@ -1300,20 +1343,20 @@ begin
   Source.SaveToFile(OutputFileName);
   Source.SaveToFile(OutputFileName);
 end;
 end;
 
 
-function TWebIDLToPas.BaseUnits: String;
+function TBaseWebIDLToPas.BaseUnits: String;
 
 
 begin
 begin
   Result:='SysUtils, JS'
   Result:='SysUtils, JS'
 end;
 end;
 
 
-function TWebIDLToPas.CreatePasName(aName: String): TPasData;
+function TBaseWebIDLToPas.CreatePasName(aName: String): TPasData;
 
 
 begin
 begin
   Result:=TPasData.Create(EscapeKeyWord(aName));
   Result:=TPasData.Create(EscapeKeyWord(aName));
   FPasNameList.Add(Result);
   FPasNameList.Add(Result);
 end;
 end;
 
 
-function TWebIDLToPas.AllocatePasName(D: TIDLDefinition; ParentName: String): TPasData;
+function TBaseWebIDLToPas.AllocatePasName(D: TIDLDefinition; ParentName: String): TPasData;
 
 
 Var
 Var
   CN : String;
   CN : String;
@@ -1350,25 +1393,25 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TWebIDLToPas.SetTypeAliases(AValue: TStrings);
+procedure TBaseWebIDLToPas.SetTypeAliases(AValue: TStrings);
 begin
 begin
   if FTypeAliases=AValue then Exit;
   if FTypeAliases=AValue then Exit;
   FTypeAliases.Assign(AValue);
   FTypeAliases.Assign(AValue);
 end;
 end;
 
 
-procedure TWebIDLToPas.SetIncludeInterfaceCode(AValue: TStrings);
+procedure TBaseWebIDLToPas.SetIncludeInterfaceCode(AValue: TStrings);
 begin
 begin
   if FIncludeInterfaceCode=AValue then Exit;
   if FIncludeInterfaceCode=AValue then Exit;
   FIncludeInterfaceCode.Assign(AValue);
   FIncludeInterfaceCode.Assign(AValue);
 end;
 end;
 
 
-procedure TWebIDLToPas.SetIncludeImplementationCode(AValue: TStrings);
+procedure TBaseWebIDLToPas.SetIncludeImplementationCode(AValue: TStrings);
 begin
 begin
   if FIncludeImplementationCode=AValue then Exit;
   if FIncludeImplementationCode=AValue then Exit;
   FIncludeImplementationCode.Assign(AValue);
   FIncludeImplementationCode.Assign(AValue);
 end;
 end;
 
 
-procedure TWebIDLToPas.AllocatePasNames(aList : TIDLDefinitionList; ParentName: String = '');
+procedure TBaseWebIDLToPas.AllocatePasNames(aList : TIDLDefinitionList; ParentName: String = '');
 
 
 var
 var
   D : TIDLDefinition;
   D : TIDLDefinition;
@@ -1379,7 +1422,7 @@ begin
 end;
 end;
 
 
 
 
-procedure TWebIDLToPas.ProcessDefinitions;
+procedure TBaseWebIDLToPas.ProcessDefinitions;
 
 
 begin
 begin
   FContext.AppendPartials;
   FContext.AppendPartials;
@@ -1387,9 +1430,15 @@ begin
   AllocatePasNames(FContext.Definitions);
   AllocatePasNames(FContext.Definitions);
 end;
 end;
 
 
-procedure TWebIDLToPas.Execute;
+procedure TBaseWebIDLToPas.Execute;
 
 
 begin
 begin
+  if Verbose then
+    begin
+    WriteOptions;
+    DoLog('');
+    end;
+
   FContext:=CreateContext;
   FContext:=CreateContext;
   try
   try
     FContext.Aliases:=Self.TypeAliases;
     FContext.Aliases:=Self.TypeAliases;
@@ -1403,5 +1452,37 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure TBaseWebIDLToPas.WriteOptions;
+
+  function CodeInfo(Src: TStrings): string;
+  begin
+    Result:='';
+    if Src.Count=0 then
+      exit;
+    Result:=Result+IntToStr(Src.Count)+' lines';
+  end;
+
+var
+  i: Integer;
+begin
+  DoLog('Options of '+ClassName+':');
+  DoLog('Verbose='+BoolToStr(Verbose,true));
+  DoLog('InputFileName='+InputFileName);
+  DoLog('OutputFileName='+OutputFileName);
+  DoLog('WebIDLVersion='+GetEnumName(TypeInfo(TWebIDLVersion),ord(WebIDLVersion)));
+  DoLog('FieldPrefix='+FieldPrefix);
+  DoLog('ClassPrefix='+ClassPrefix);
+  DoLog('ClassSuffix='+ClassSuffix);
+  DoLog('DictionaryClassParent='+DictionaryClassParent);
+  if TypeAliases.Count>0 then
+    for i:=0 to TypeAliases.Count-1 do
+      DoLog('TypeAliases['+IntToStr(i)+']='+TypeAliases[i])
+  else
+    DoLog('TypeAliases=');
+  DoLog('IncludeInterfaceCode='+CodeInfo(IncludeInterfaceCode));
+  DoLog('IncludeImplementationCode='+CodeInfo(IncludeImplementationCode));
+  //Property Options : TConversionOptions Read FOptions Write FOptions;
+end;
+
 end.
 end.
 
 

+ 115 - 29
utils/pas2js/webidl2pas.pp

@@ -19,14 +19,26 @@ program webidl2pas;
 uses
 uses
   Classes, SysUtils, CustApp, webidlscanner, webidltopas, pascodegen, typinfo;
   Classes, SysUtils, CustApp, webidlscanner, webidltopas, pascodegen, typinfo;
 
 
+type
+  TWebIDLToPasFormat = (
+    wifPas2js,
+    wifWasmJob
+    );
+const
+  WebIDLToPasFormatNames: array[TWebIDLToPasFormat] of string = (
+    'pas2js',
+    'wasmjob'
+    );
+
 type
 type
 
 
   { TWebIDLToPasApplication }
   { TWebIDLToPasApplication }
 
 
   TWebIDLToPasApplication = class(TCustomApplication)
   TWebIDLToPasApplication = class(TCustomApplication)
   private
   private
-    FWebIDLToPas: TWebIDLToPas;
-    function Checkoption(Var O: TCOnversionOPtions; C: TCOnversionOPtion;
+    FOutputFormat: TWebIDLToPasFormat;
+    FWebIDLToPas: TBaseWebIDLToPas;
+    function Checkoption(Var O: TConversionOptions; C: TCOnversionOPtion;
       const AShort: Char; const aLong: String): Boolean;
       const AShort: Char; const aLong: String): Boolean;
     procedure DoConvertLog(Sender: TObject; {%H-}LogType: TCodegenLogType; const Msg: String);
     procedure DoConvertLog(Sender: TObject; {%H-}LogType: TCodegenLogType; const Msg: String);
     function GetInputFileName: String;
     function GetInputFileName: String;
@@ -37,8 +49,9 @@ type
     procedure SetunitName(AValue: String);
     procedure SetunitName(AValue: String);
   protected
   protected
     procedure DoRun; override;
     procedure DoRun; override;
+    procedure InitWebIDLToPas; virtual;
   Protected
   Protected
-    Property WebIDLToPas : TWebIDLToPas Read FWebIDLToPas;
+    Property WebIDLToPas : TBaseWebIDLToPas Read FWebIDLToPas;
   public
   public
     constructor Create(TheOwner: TComponent); override;
     constructor Create(TheOwner: TComponent); override;
     destructor Destroy; override;
     destructor Destroy; override;
@@ -46,6 +59,7 @@ type
     Property UnitName : String Read GetUnitName Write SetunitName;
     Property UnitName : String Read GetUnitName Write SetunitName;
     property InputFileName : String Read GetInputFileName Write SetinputFileName;
     property InputFileName : String Read GetInputFileName Write SetinputFileName;
     property OutputFileName : String Read GetOutputFileName Write SetOutputFileName;
     property OutputFileName : String Read GetOutputFileName Write SetOutputFileName;
+    property OutputFormat: TWebIDLToPasFormat read FOutputFormat write FOutputFormat;
   end;
   end;
 
 
 { TWebIDLToPasApplication }
 { TWebIDLToPasApplication }
@@ -88,7 +102,8 @@ begin
   FWebIDLToPas.OutputUnitName:=aValue;
   FWebIDLToPas.OutputUnitName:=aValue;
 end;
 end;
 
 
-Function TWebIDLToPasApplication.Checkoption(Var O : TCOnversionOPtions;C : TCOnversionOPtion; Const AShort : Char; Const aLong : String) : Boolean;
+function TWebIDLToPasApplication.Checkoption(var O: TConversionOptions;
+  C: TCOnversionOPtion; const AShort: Char; const aLong: String): Boolean;
 
 
 begin
 begin
   Result:=HasOption(aShort,ALong);
   Result:=HasOption(aShort,ALong);
@@ -98,45 +113,91 @@ end;
 
 
 procedure TWebIDLToPasApplication.DoRun;
 procedure TWebIDLToPasApplication.DoRun;
 
 
+  procedure E(const Msg: string);
+  begin
+    writeln('Error: ',Msg);
+    Halt(1);
+  end;
+
 var
 var
   A,ErrorMsg: String;
   A,ErrorMsg: String;
   O : TConversionOptions;
   O : TConversionOptions;
   I : Integer;
   I : Integer;
+  ok: Boolean;
+  f: TWebIDLToPasFormat;
 
 
 begin
 begin
   Terminate;
   Terminate;
   // quick check parameters
   // quick check parameters
-  ErrorMsg:=CheckOptions('hi:o:u:m:n:vx:t:ced::pw:', ['help','input:','output:','unitname:','include:','implementation:','verbose','extra:','typealiases:','constexternal','expandunionargs','dicttoclass::','optionsinheader','webidlversion:']);
+  ErrorMsg:=CheckOptions('ced::hi:m:n:o:pt:u:vw:x:', [
+    'help',
+    'constexternal',
+    'dicttoclass::',
+    'expandunionargs',
+    'outputformat:',
+    'input:',
+    'implementation:',
+    'include:',
+    'output:',
+    'optionsinheader',
+    'typealiases:',
+    'unitname:',
+    'verbose',
+    'webidlversion:',
+    'extra:'
+    ]);
   if (ErrorMsg<>'') or HasOption('h','help') then
   if (ErrorMsg<>'') or HasOption('h','help') then
     begin
     begin
     WriteHelp(ErrorMsg);
     WriteHelp(ErrorMsg);
-    Exit;
+    if ErrorMsg<>'' then
+      Halt(1)
+    else
+      Exit;
     end;
     end;
+
+  // first read outputformat and create FWebIDLToPas
+  if HasOption('f','outputformat') then
+    begin
+    A:=GetOptionValue('f','outputformat');
+    ok:=false;
+    for f in TWebIDLToPasFormat do
+      begin
+      if SameText(A,WebIDLToPasFormatNames[f]) then
+        begin
+        OutputFormat:=f;
+        ok:=true;
+        end;
+      end;
+    if not ok then
+      E('unknown outputformat "'+A+'"');
+    end;
+  InitWebIDLToPas;
+
+  // then set verbosity
+  FWebIDLToPas.Verbose:=HasOption('v','verbose');
+
+  // read other options
   O:=[];
   O:=[];
   Checkoption(O,coExternalConst,'c','constexternal');
   Checkoption(O,coExternalConst,'c','constexternal');
-  Checkoption(O,coExpandUnionTypeArgs,'e','expandunionargs');
-  CheckOption(O,coaddOptionsToheader,'p','optionsinheader');
+
   if Checkoption(O,coDictionaryAsClass,'d','dicttoclass') then
   if Checkoption(O,coDictionaryAsClass,'d','dicttoclass') then
     FWebIDLToPas.DictionaryClassParent:=GetOptionValue('d','dicttoclass');
     FWebIDLToPas.DictionaryClassParent:=GetOptionValue('d','dicttoclass');
-  FWebIDLToPas.Options:=O;
+
+  Checkoption(O,coExpandUnionTypeArgs,'e','expandunionargs');
+
   InputFileName:=GetOptionValue('i','input');
   InputFileName:=GetOptionValue('i','input');
-  OutputFileName:=GetOptionValue('o','output');
-  UnitName:=GetOptionValue('u','unitname');
-  FWebIDLToPas.Verbose:=HasOption('v','verbose');
-  if HasOption('w','webidlversion') then
-    begin
-    A:=GetOptionValue('w','webidlversion');
-    I:=GetEnumValue(TypeInfo(TWebIDLVersion),A);
-    if (I<>-1) then
-      FWebIDLToPas.WebIDLVersion:=TWebIDLVersion(I)
-    else
-      Raise EConvertError.CreateFmt('Invalid webidl version: %s',[A]);
-    end;
-  if HasOption('n','include') then
-    FWebIDLToPas.IncludeInterfaceCode.LoadFromFile(GetOptionValue('n','include'));
+
   if HasOption('m','implementation') then
   if HasOption('m','implementation') then
     FWebIDLToPas.IncludeImplementationCode.LoadFromFile(GetOptionValue('m','implementation'));
     FWebIDLToPas.IncludeImplementationCode.LoadFromFile(GetOptionValue('m','implementation'));
-  FWebIDLToPas.ExtraUnits:=GetOPtionValue('x','extra');
+
+  if HasOption('n','include') then
+    FWebIDLToPas.IncludeInterfaceCode.LoadFromFile(GetOptionValue('n','include'));
+
+  OutputFileName:=GetOptionValue('o','output');
+
+  CheckOption(O,coAddOptionsToHeader,'p','optionsinheader');
+
+  FWebIDLToPas.Options:=O;
   A:=GetOptionValue('t','typealiases');
   A:=GetOptionValue('t','typealiases');
   if (Copy(A,1,1)='@') then
   if (Copy(A,1,1)='@') then
     begin
     begin
@@ -145,6 +206,8 @@ begin
     end
     end
   else
   else
     FWebIDLToPas.TypeAliases.CommaText:=A;
     FWebIDLToPas.TypeAliases.CommaText:=A;
+
+  UnitName:=GetOptionValue('u','unitname');
   if UnitName='' then
   if UnitName='' then
     UnitName:=ChangeFileExt(ExtractFileName(InputFileName),'');
     UnitName:=ChangeFileExt(ExtractFileName(InputFileName),'');
   if OutputFileName='' then
   if OutputFileName='' then
@@ -152,16 +215,32 @@ begin
     if (UnitName<>'') then
     if (UnitName<>'') then
       OutputFileName:=ExtractFilePath(InputFileName)+UnitName+'.pas';
       OutputFileName:=ExtractFilePath(InputFileName)+UnitName+'.pas';
     end;
     end;
+
+  if HasOption('w','webidlversion') then
+    begin
+    A:=GetOptionValue('w','webidlversion');
+    I:=GetEnumValue(TypeInfo(TWebIDLVersion),A);
+    if (I<>-1) then
+      FWebIDLToPas.WebIDLVersion:=TWebIDLVersion(I)
+    else
+      E('Invalid webidl version: "'+A+'"');
+    end;
+
+  FWebIDLToPas.ExtraUnits:=GetOptionValue('x','extra');
+
   FWebIDLToPas.Execute;
   FWebIDLToPas.Execute;
   // stop program loop
   // stop program loop
   Terminate;
   Terminate;
 end;
 end;
 
 
-constructor TWebIDLToPasApplication.Create(TheOwner: TComponent);
+procedure TWebIDLToPasApplication.InitWebIDLToPas;
 begin
 begin
-  inherited Create(TheOwner);
-  StopOnException:=True;
-  FWebIDLToPas:=TWebIDLToPas.Create(Self);
+  case OutputFormat of
+  wifWasmJob:
+    FWebIDLToPas:=TWebIDLToPasWasmJob.Create(Self);
+  else
+    FWebIDLToPas:=TWebIDLToPas2js.Create(Self);
+  end;
   FWebIDLToPas.OnLog:=@DoConvertLog;
   FWebIDLToPas.OnLog:=@DoConvertLog;
   FWebIDLToPas.ClassPrefix:='TJS';
   FWebIDLToPas.ClassPrefix:='TJS';
   FWebIDLToPas.ClassSuffix:='';
   FWebIDLToPas.ClassSuffix:='';
@@ -169,6 +248,12 @@ begin
   FWebIDLToPas.KeywordPrefix:='';
   FWebIDLToPas.KeywordPrefix:='';
 end;
 end;
 
 
+constructor TWebIDLToPasApplication.Create(TheOwner: TComponent);
+begin
+  inherited Create(TheOwner);
+  StopOnException:=True;
+end;
+
 destructor TWebIDLToPasApplication.Destroy;
 destructor TWebIDLToPasApplication.Destroy;
 begin
 begin
   FreeAndNil(FWebIDLToPas);
   FreeAndNil(FWebIDLToPas);
@@ -184,8 +269,9 @@ begin
   Writeln(StdErr,'Where option is one or more of');
   Writeln(StdErr,'Where option is one or more of');
   Writeln(StdErr,'-h  --help                 this help text');
   Writeln(StdErr,'-h  --help                 this help text');
   Writeln(StdErr,'-c  --constexternal        Write consts as external const (no value)');
   Writeln(StdErr,'-c  --constexternal        Write consts as external const (no value)');
-  Writeln(StdErr,'-e  --expandunionargs      Add overloads for all Union typed function arguments');
   Writeln(StdErr,'-d  --dicttoclass[=Parent] Write dictionaries as classes');
   Writeln(StdErr,'-d  --dicttoclass[=Parent] Write dictionaries as classes');
+  Writeln(StdErr,'-e  --expandunionargs      Add overloads for all Union typed function arguments');
+  Writeln(StdErr,'-f  --outputformat=[pas2js|wasmjob] Output format, default ',WebIDLToPasFormatNames[OutputFormat]);
   Writeln(StdErr,'-i  --input=FileName       input webidl file');
   Writeln(StdErr,'-i  --input=FileName       input webidl file');
   Writeln(StdErr,'-m  --implementation=Filename include file as implementation');
   Writeln(StdErr,'-m  --implementation=Filename include file as implementation');
   Writeln(StdErr,'-n  --include=Filename     include file at end of interface');
   Writeln(StdErr,'-n  --include=Filename     include file at end of interface');