Browse Source

pas2js: added OnReadFile hook

git-svn-id: trunk@37792 -
Mattias Gaertner 7 years ago
parent
commit
656ddcccf7

+ 3 - 1
utils/pas2js/pas2jscompiler.pp

@@ -1878,6 +1878,7 @@ var
   aFile: TPas2jsFileLineReader;
   aFile: TPas2jsFileLineReader;
   IfLvl, SkipLvl, OldCfgLineNumber: Integer;
   IfLvl, SkipLvl, OldCfgLineNumber: Integer;
   Skip: TSkip;
   Skip: TSkip;
+  CacheFile: TPas2jsCachedFile;
 begin
 begin
   if ShowTriedUsedFiles then
   if ShowTriedUsedFiles then
     Log.LogMsgIgnoreFilter(nReadingOptionsFromFile,[CfgFilename]);
     Log.LogMsgIgnoreFilter(nReadingOptionsFromFile,[CfgFilename]);
@@ -1889,7 +1890,8 @@ begin
     OldCfgFilename:=FCurrentCfgFilename;
     OldCfgFilename:=FCurrentCfgFilename;
     FCurrentCfgFilename:=CfgFilename;
     FCurrentCfgFilename:=CfgFilename;
     OldCfgLineNumber:=FCurrentCfgLineNumber;
     OldCfgLineNumber:=FCurrentCfgLineNumber;
-    aFile:=TPas2jsFileLineReader.Create(CfgFilename);
+    CacheFile:=FileCache.LoadTextFile(CfgFilename);
+    aFile:=CacheFile.CreateLineReader(true);
     while not aFile.IsEOF do begin
     while not aFile.IsEOF do begin
       Line:=aFile.ReadLine;
       Line:=aFile.ReadLine;
       FCurrentCfgLineNumber:=aFile.LineNumber;
       FCurrentCfgLineNumber:=aFile.LineNumber;

+ 36 - 44
utils/pas2js/pas2jsfilecache.pp

@@ -110,6 +110,8 @@ type
     property LoadedFileAge: longint read FLoadedFileAge;// only valid if Loaded=true
     property LoadedFileAge: longint read FLoadedFileAge;// only valid if Loaded=true
   end;
   end;
 
 
+  TPas2jsReadFileEvent = function(aFilename: string; var aSource: string): boolean of object;
+
   TPas2jsCachedFilesState = (
   TPas2jsCachedFilesState = (
     cfsMainJSFileResolved
     cfsMainJSFileResolved
     );
     );
@@ -137,6 +139,7 @@ type
     FMainSrcFile: string;
     FMainSrcFile: string;
     FNamespaces: TStringList;
     FNamespaces: TStringList;
     FNamespacesFromCmdLine: integer;
     FNamespacesFromCmdLine: integer;
+    FOnReadFile: TPas2jsReadFileEvent;
     FOptions: TP2jsFileCacheOptions;
     FOptions: TP2jsFileCacheOptions;
     FReadLineCounter: SizeInt;
     FReadLineCounter: SizeInt;
     FResetStamp: TChangeStamp;
     FResetStamp: TChangeStamp;
@@ -160,6 +163,8 @@ type
     procedure SetSrcMapBaseDir(const AValue: string);
     procedure SetSrcMapBaseDir(const AValue: string);
     procedure SetUnitOutputPath(AValue: string);
     procedure SetUnitOutputPath(AValue: string);
     procedure SetOption(Flag: TP2jsFileCacheOption; Enable: boolean);
     procedure SetOption(Flag: TP2jsFileCacheOption; Enable: boolean);
+  protected
+    function ReadFile(Filename: string; var Source: string): boolean; virtual;
   public
   public
     constructor Create(aLog: TPas2jsLogger);
     constructor Create(aLog: TPas2jsLogger);
     destructor Destroy; override;
     destructor Destroy; override;
@@ -199,6 +204,7 @@ type
     property UnitOutputPath: string read FUnitOutputPath write SetUnitOutputPath; // includes trailing pathdelim
     property UnitOutputPath: string read FUnitOutputPath write SetUnitOutputPath; // includes trailing pathdelim
     property UnitPaths: TStringList read FUnitPaths;
     property UnitPaths: TStringList read FUnitPaths;
     property UnitPathsFromCmdLine: integer read FUnitPathsFromCmdLine;
     property UnitPathsFromCmdLine: integer read FUnitPathsFromCmdLine;
+    property OnReadFile: TPas2jsReadFileEvent read FOnReadFile write FOnReadFile;
   end;
   end;
 
 
 function CompareFilenameWithCachedFile(Filename, CachedFile: Pointer): integer;
 function CompareFilenameWithCachedFile(Filename, CachedFile: Pointer): integer;
@@ -300,33 +306,8 @@ end;
 { TPas2jsFileLineReader }
 { TPas2jsFileLineReader }
 
 
 constructor TPas2jsFileLineReader.Create(const AFilename: string);
 constructor TPas2jsFileLineReader.Create(const AFilename: string);
-var
-  ms: TMemoryStream;
-  NewSource, FileEncoding: string;
 begin
 begin
-  //writeln('TPas2jsFileLineReader.Create ',AFilename);
-  inherited Create(AFilename);
-  ms:=TMemoryStream.Create;
-  try
-    try
-      ms.LoadFromFile(Filename);
-      SetLength(NewSource,ms.Size);
-      ms.Position:=0;
-      if NewSource<>'' then
-        ms.Read(NewSource[1],length(NewSource));
-    except
-      on E: Exception do begin
-        EPas2jsFileCache.Create('Error reading file "'+Filename+'": '+E.Message);
-        exit;
-      end;
-    end;
-  finally
-    ms.Free;
-  end;
-  FileEncoding:='';
-  FSource:=ConvertTextToUTF8(NewSource,FileEncoding);
-  FSrcPos:=PChar(FSource);
-  FIsEOF:=FSource='';
+  raise Exception.Create('TPas2jsFileLineReader.Create no cache "'+AFilename+'"');
 end;
 end;
 
 
 constructor TPas2jsFileLineReader.Create(aFile: TPas2jsCachedFile);
 constructor TPas2jsFileLineReader.Create(aFile: TPas2jsCachedFile);
@@ -413,7 +394,6 @@ function TPas2jsCachedFile.Load(RaiseOnError: boolean): boolean;
   end;
   end;
 
 
 var
 var
-  ms: TMemoryStream;
   NewSource: string;
   NewSource: string;
 begin
 begin
   {$IFDEF VerboseFileCache}
   {$IFDEF VerboseFileCache}
@@ -447,23 +427,8 @@ begin
     Err('File is a directory "'+Filename+'"');
     Err('File is a directory "'+Filename+'"');
     exit;
     exit;
   end;
   end;
-  ms:=TMemoryStream.Create;
-  try
-    try
-      ms.LoadFromFile(Filename);
-      SetLength(NewSource,ms.Size);
-      ms.Position:=0;
-      if NewSource<>'' then
-        ms.Read(NewSource[1],length(NewSource));
-    except
-      on E: Exception do begin
-        Err('Error reading file "'+Filename+'": '+E.Message);
-        exit;
-      end;
-    end;
-  finally
-    ms.Free;
-  end;
+  NewSource:='';
+  if not Cache.ReadFile(Filename,NewSource) then exit;
   {$IFDEF VerboseFileCache}
   {$IFDEF VerboseFileCache}
   writeln('TPas2jsCachedFile.Load ENCODE ',Filename,' FFileEncoding=',FFileEncoding);
   writeln('TPas2jsCachedFile.Load ENCODE ',Filename,' FFileEncoding=',FFileEncoding);
   {$ENDIF}
   {$ENDIF}
@@ -895,6 +860,33 @@ begin
     Exclude(FStates,cfsMainJSFileResolved);
     Exclude(FStates,cfsMainJSFileResolved);
 end;
 end;
 
 
+function TPas2jsFilesCache.ReadFile(Filename: string; var Source: string
+  ): boolean;
+var
+  ms: TMemoryStream;
+begin
+  Result:=false;
+  try
+    if Assigned(OnReadFile) then
+      if OnReadFile(Filename,Source) then exit;
+    ms:=TMemoryStream.Create;
+    try
+      ms.LoadFromFile(Filename);
+      SetLength(Source,ms.Size);
+      ms.Position:=0;
+      if Source<>'' then
+        ms.Read(Source[1],length(Source));
+      Result:=true;
+    finally
+      ms.Free;
+    end;
+  except
+    on E: Exception do begin
+      EPas2jsFileCache.Create('Error reading file "'+Filename+'": '+E.Message);
+    end;
+  end;
+end;
+
 constructor TPas2jsFilesCache.Create(aLog: TPas2jsLogger);
 constructor TPas2jsFilesCache.Create(aLog: TPas2jsLogger);
 begin
 begin
   inherited Create;
   inherited Create;

+ 36 - 6
utils/pas2js/pas2jslib.lpi

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?xml version="1.0" encoding="UTF-8"?>
 <CONFIG>
 <CONFIG>
   <ProjectOptions>
   <ProjectOptions>
-    <Version Value="10"/>
+    <Version Value="11"/>
     <General>
     <General>
       <Flags>
       <Flags>
         <MainUnitHasCreateFormStatements Value="False"/>
         <MainUnitHasCreateFormStatements Value="False"/>
@@ -20,20 +20,50 @@
       <Version Value="2"/>
       <Version Value="2"/>
     </PublishOptions>
     </PublishOptions>
     <RunParams>
     <RunParams>
-      <local>
-        <FormatVersion Value="1"/>
-      </local>
+      <FormatVersion Value="2"/>
+      <Modes Count="1">
+        <Mode0 Name="default"/>
+      </Modes>
     </RunParams>
     </RunParams>
-    <Units Count="2">
+    <Units Count="9">
       <Unit0>
       <Unit0>
         <Filename Value="pas2jslib.pp"/>
         <Filename Value="pas2jslib.pp"/>
         <IsPartOfProject Value="True"/>
         <IsPartOfProject Value="True"/>
       </Unit0>
       </Unit0>
       <Unit1>
       <Unit1>
-        <Filename Value="pas2jscompiler.pas"/>
+        <Filename Value="pas2jscompiler.pp"/>
         <IsPartOfProject Value="True"/>
         <IsPartOfProject Value="True"/>
         <UnitName Value="Pas2jsCompiler"/>
         <UnitName Value="Pas2jsCompiler"/>
       </Unit1>
       </Unit1>
+      <Unit2>
+        <Filename Value="pas2jsfilecache.pp"/>
+        <IsPartOfProject Value="True"/>
+        <UnitName Value="Pas2jsFileCache"/>
+      </Unit2>
+      <Unit3>
+        <Filename Value="pas2js_defines.inc"/>
+        <IsPartOfProject Value="True"/>
+      </Unit3>
+      <Unit4>
+        <Filename Value="pas2jsfileutils.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit4>
+      <Unit5>
+        <Filename Value="pas2jsfileutilsunix.inc"/>
+        <IsPartOfProject Value="True"/>
+      </Unit5>
+      <Unit6>
+        <Filename Value="pas2jsfileutilswin.inc"/>
+        <IsPartOfProject Value="True"/>
+      </Unit6>
+      <Unit7>
+        <Filename Value="pas2jslogger.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit7>
+      <Unit8>
+        <Filename Value="pas2jspparser.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit8>
     </Units>
     </Units>
   </ProjectOptions>
   </ProjectOptions>
   <CompilerOptions>
   <CompilerOptions>

+ 12 - 3
utils/pas2js/pas2jslib.pp

@@ -11,7 +11,7 @@ uses
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 Type
 Type
-  TLibLogCallBack = Procedure (Data : Pointer; Msg : PansiChar; MsgLen : Integer); stdcall;
+  TLibLogCallBack = Procedure (Data : Pointer; Msg : PAnsiChar; MsgLen : Integer); stdcall;
   TWriteJSCallBack = Procedure (Data : Pointer;
   TWriteJSCallBack = Procedure (Data : Pointer;
     AFileName: PAnsiChar; AFileNameLen : Integer;
     AFileName: PAnsiChar; AFileNameLen : Integer;
     AFileData : PAnsiChar; AFileDataLen: Int32); stdcall;
     AFileData : PAnsiChar; AFileDataLen: Int32); stdcall;
@@ -30,6 +30,7 @@ Type
     Function DoWriteJSFile(const DestFilename: String; aWriter: TPas2JSMapper): Boolean; override;
     Function DoWriteJSFile(const DestFilename: String; aWriter: TPas2JSMapper): Boolean; override;
     Procedure GetLastError(AError : PAnsiChar; Var AErrorLength : Longint;
     Procedure GetLastError(AError : PAnsiChar; Var AErrorLength : Longint;
       AErrorClass : PAnsiChar; Var AErrorClassLength : Longint);
       AErrorClass : PAnsiChar; Var AErrorClassLength : Longint);
+    Function ReadFile(aFilename: string; var aSource: string): boolean; virtual;
   Public
   Public
     Constructor Create; override;
     Constructor Create; override;
     Procedure DoLibraryLog(Sender : TObject; Const Msg : String);
     Procedure DoLibraryLog(Sender : TObject; Const Msg : String);
@@ -60,8 +61,9 @@ begin
     end;
     end;
 end;
 end;
 
 
-procedure TLibraryPas2JSCompiler.GetLastError(AError: PAnsiChar; var AErrorLength : Longint;
-  AErrorClass: PAnsiChar; var AErrorClassLength : Longint);
+procedure TLibraryPas2JSCompiler.GetLastError(AError: PAnsiChar;
+  Var AErrorLength: Longint; AErrorClass: PAnsiChar;
+  Var AErrorClassLength: Longint);
 
 
 Var
 Var
   L : Integer;
   L : Integer;
@@ -79,10 +81,17 @@ begin
     Move(FLastErrorClass[1],AErrorClass^,L);
     Move(FLastErrorClass[1],AErrorClass^,L);
 end;
 end;
 
 
+function TLibraryPas2JSCompiler.ReadFile(aFilename: string; var aSource: string
+  ): boolean;
+begin
+  Result:=false; // use default reader
+end;
+
 constructor TLibraryPas2JSCompiler.Create;
 constructor TLibraryPas2JSCompiler.Create;
 begin
 begin
   inherited Create;
   inherited Create;
   Log.OnLog:=@DoLibraryLog;
   Log.OnLog:=@DoLibraryLog;
+  FileCache.OnReadFile:=@ReadFile;
 end;
 end;
 
 
 procedure TLibraryPas2JSCompiler.DoLibraryLog(Sender: TObject; const Msg: String);
 procedure TLibraryPas2JSCompiler.DoLibraryLog(Sender: TObject; const Msg: String);