Browse Source

* Added library version of pas2js

git-svn-id: trunk@37752 -
michael 7 years ago
parent
commit
3496256179
4 changed files with 274 additions and 0 deletions
  1. 2 0
      .gitattributes
  2. 6 0
      utils/pas2js/fpmake.pp
  3. 70 0
      utils/pas2js/pas2jslib.lpi
  4. 196 0
      utils/pas2js/pas2jslib.pp

+ 2 - 0
.gitattributes

@@ -16948,6 +16948,8 @@ utils/pas2js/pas2jsfilecache.pp svneol=native#text/plain
 utils/pas2js/pas2jsfileutils.pp svneol=native#text/plain
 utils/pas2js/pas2jsfileutils.pp svneol=native#text/plain
 utils/pas2js/pas2jsfileutilsunix.inc svneol=native#text/plain
 utils/pas2js/pas2jsfileutilsunix.inc svneol=native#text/plain
 utils/pas2js/pas2jsfileutilswin.inc svneol=native#text/plain
 utils/pas2js/pas2jsfileutilswin.inc svneol=native#text/plain
+utils/pas2js/pas2jslib.lpi svneol=native#text/plain
+utils/pas2js/pas2jslib.pp svneol=native#text/plain
 utils/pas2js/pas2jslogger.pp svneol=native#text/plain
 utils/pas2js/pas2jslogger.pp svneol=native#text/plain
 utils/pas2js/pas2jspparser.pp svneol=native#text/plain
 utils/pas2js/pas2jspparser.pp svneol=native#text/plain
 utils/pas2js/samples/arraydemo.pp svneol=native#text/plain
 utils/pas2js/samples/arraydemo.pp svneol=native#text/plain

+ 6 - 0
utils/pas2js/fpmake.pp

@@ -42,6 +42,12 @@ begin
     PT.Dependencies.AddUnit('pas2jsfilecache');
     PT.Dependencies.AddUnit('pas2jsfilecache');
     PT.Dependencies.AddUnit('pas2jslogger');
     PT.Dependencies.AddUnit('pas2jslogger');
     PT.Dependencies.AddUnit('pas2jspparser');
     PT.Dependencies.AddUnit('pas2jspparser');
+    PT:=P.Targets.AddLibrary('pas2jslib.pp');
+    PT.Dependencies.AddUnit('pas2jscompiler');
+    PT.Dependencies.AddUnit('pas2jsfileutils');
+    PT.Dependencies.AddUnit('pas2jsfilecache');
+    PT.Dependencies.AddUnit('pas2jslogger');
+    PT.Dependencies.AddUnit('pas2jspparser');
     end;
     end;
 end;
 end;
 
 

+ 70 - 0
utils/pas2js/pas2jslib.lpi

@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="10"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <MainUnit Value="0"/>
+      <Title Value="Pas2JS Shared Library version"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <BuildModes Count="1">
+      <Item1 Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+    </PublishOptions>
+    <RunParams>
+      <local>
+        <FormatVersion Value="1"/>
+      </local>
+    </RunParams>
+    <Units Count="2">
+      <Unit0>
+        <Filename Value="pas2jslib.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit0>
+      <Unit1>
+        <Filename Value="pas2jscompiler.pas"/>
+        <IsPartOfProject Value="True"/>
+        <UnitName Value="Pas2jsCompiler"/>
+      </Unit1>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <Target>
+      <Filename Value="pas2jslib"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
+    </SearchPaths>
+    <CodeGeneration>
+      <RelocatableUnit Value="True"/>
+    </CodeGeneration>
+    <Linking>
+      <Options>
+        <ExecutableType Value="Library"/>
+      </Options>
+    </Linking>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions Count="3">
+      <Item1>
+        <Name Value="EAbort"/>
+      </Item1>
+      <Item2>
+        <Name Value="ECodetoolError"/>
+      </Item2>
+      <Item3>
+        <Name Value="EFOpenError"/>
+      </Item3>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 196 - 0
utils/pas2js/pas2jslib.pp

@@ -0,0 +1,196 @@
+library pas2jslib;
+
+{$mode objfpc}
+{$H+}
+
+uses
+  SysUtils, Classes, FPPJsSrcMap, Pas2jsFileCache, Pas2jsCompiler;
+
+{ ---------------------------------------------------------------------
+  Compiler descendant, usable in library
+  ---------------------------------------------------------------------}
+
+Type
+  TLibLogCallBack = Procedure (Data : Pointer; Msg : PansiChar; MsgLen : Integer); stdcall;
+  TWriteJSCallBack = Procedure (Data : Pointer;
+    AFileName: PAnsiChar; AFileNameLen : Integer;
+    AFileData : PAnsiChar; AFileDataLen: Int32); stdcall;
+
+  { TLibraryPas2JSCompiler }
+
+  TLibraryPas2JSCompiler = Class(TPas2JSCompiler)
+  private
+    FLastError: String;
+    FLastErrorClass: String;
+    FOnLibLogCallBack: TLibLogCallBack;
+    FOnLibLogData: Pointer;
+    FOnWriteJSCallBack: TWriteJSCallBack;
+    FOnWriteJSData: Pointer;
+  Protected
+    Function DoWriteJSFile(const DestFilename: String; aWriter: TPas2JSMapper): Boolean; override;
+    Procedure GetLastError(AError : PAnsiChar; Var AErrorLength : Longint;
+      AErrorClass : PAnsiChar; Var AErrorClassLength : Longint);
+  Public
+    Constructor Create; override;
+    Procedure DoLibraryLog(Sender : TObject; Const Msg : String);
+    Function LibraryRun(ACompilerExe, AWorkingDir : PAnsiChar; CommandLine : PPAnsiChar; DoReset : Boolean) :Boolean; stdcall;
+    Property LastError : String Read FLastError Write FLastError;
+    Property LastErrorClass : String Read FLastErrorClass Write FLastErrorClass;
+    Property OnLibLogCallBack : TLibLogCallBack Read FOnLibLogCallBack Write FOnLibLogCallBack;
+    Property OnLibLogData : Pointer Read FOnLibLogData Write FOnLibLogData;
+    Property OnWriteJSCallBack : TWriteJSCallBack Read FOnWriteJSCallBack Write FOnWriteJSCallBack;
+    Property OnWriteJSData : Pointer Read FOnWriteJSData Write FOnWriteJSData;
+  end;
+
+{ TLibraryPas2JSCompiler }
+
+function TLibraryPas2JSCompiler.DoWriteJSFile(const DestFilename: String; aWriter: TPas2JSMapper): Boolean;
+
+Var
+  Src : string;
+
+begin
+  Result:=Assigned(OnWriteJSCallBack);
+  if Result then
+    try
+      Src:=aWriter.AsAnsistring;
+      OnWriteJSCallBack(OnWriteJSData,PAnsiChar(DestFileName),Length(DestFileName),PAnsiChar(Src),Length(Src));
+    except
+      Result:=False;
+    end;
+end;
+
+procedure TLibraryPas2JSCompiler.GetLastError(AError: PAnsiChar; var AErrorLength : Longint;
+  AErrorClass: PAnsiChar; var AErrorClassLength : Longint);
+
+Var
+  L : Integer;
+
+begin
+  L:=Length(LastError);
+  if (L>AErrorLength) then
+    L:=AErrorLength;
+  if (L>0) then
+    Move(FLastError[1],AError^,L);
+  L:=Length(LastErrorClass);
+  if L>AErrorClassLength then
+    L:=AErrorClassLength;
+  if (L>0) then
+    Move(FLastErrorClass[1],AErrorClass^,L);
+end;
+
+constructor TLibraryPas2JSCompiler.Create;
+begin
+  inherited Create;
+  Log.OnLog:=@DoLibraryLog;
+end;
+
+procedure TLibraryPas2JSCompiler.DoLibraryLog(Sender: TObject; const Msg: String);
+begin
+  if Assigned(FOnLibLogCallBack) then
+    FOnLibLogCallBack(FOnLibLogData,PAnsiChar(Msg),Length(Msg))
+  else if isConsole then
+    Writeln(Msg);
+end;
+
+function TLibraryPas2JSCompiler.LibraryRun(ACompilerExe, AWorkingDir: PAnsiChar;
+  CommandLine: PPAnsiChar; DoReset: Boolean): Boolean; stdcall;
+
+Var
+  C,W : AnsiString;
+  CmdLine : TStrings;
+  PP : PPAnsiChar;
+
+begin
+  Result:=False;
+  C:=ACompilerExe;
+  W:=AWorkingDir;
+  CmdLine:=TStringList.Create;
+  try
+    PP:=CommandLine;
+    While (PP^<>Nil) do
+      begin
+      CmdLine.Add(pp^);
+      Inc(PP);
+      end;
+    try
+      Run(C,W,CmdLine,DoReset);
+      Result:=(ExitCode=0);
+      if Not Result then
+        begin
+        LastError:=Format('Compiler exited with exit code %d',[ExitCode]);
+        LastErrorClass:=ECompilerTerminate.ClassName;
+        end;
+    except
+      On E : Exception do
+        begin
+        LastError:=E.Message;
+        LastErrorClass:=E.ClassName;
+        end;
+    end;
+  finally
+    CmdLine.free;
+  end;
+end;
+
+{ ---------------------------------------------------------------------
+  Flat interface
+  ---------------------------------------------------------------------}
+
+Type
+  PPas2JSCompiler = Pointer;
+  PStubCreator = Pointer;
+
+
+Procedure SetPas2JSWriteJSCallBack(P : PPas2JSCompiler; ACallBack : TWriteJSCallBack;
+  CallBackData : Pointer); stdcall;
+
+begin
+  TLibraryPas2JSCompiler(P).OnWriteJSCallBack:=ACallBack;
+  TLibraryPas2JSCompiler(P).OnWriteJSData:=CallBackData;
+end;
+
+Procedure SetPas2JSCompilerLogCallBack(P : PPas2JSCompiler; ACallBack : TLibLogCallBack;
+  CallBackData : Pointer); stdcall;
+
+begin
+  TLibraryPas2JSCompiler(P).OnLibLogCallBack:=ACallBack;
+  TLibraryPas2JSCompiler(P).OnLibLogData:=CallBackData;
+end;
+
+Function RunPas2JSCompiler(P : PPas2JSCompiler; ACompilerExe, AWorkingDir : PAnsiChar;
+  CommandLine : PPAnsiChar; DoReset : Boolean) : Boolean; stdcall;
+
+begin
+  Result:=TLibraryPas2JSCompiler(P).LibraryRun(ACompilerExe,AWorkingDir,CommandLine,DoReset)
+end;
+
+Procedure FreePas2JSCompiler(P : PPas2JSCompiler); stdcall;
+
+begin
+  TLibraryPas2JSCompiler(P).Free;
+end;
+
+Function GetPas2JSCompiler : PPas2JSCompiler; stdcall;
+
+begin
+  Result:=TLibraryPas2JSCompiler.Create;
+end;
+
+Procedure GetPas2JSCompilerLastError(P : PPas2JSCompiler; AError : PAnsiChar;
+  Var AErrorLength : Longint; AErrorClass : PAnsiChar; Var AErrorClassLength : Longint); stdcall;
+
+begin
+  TLibraryPas2JSCompiler(P).GetLastError(AError,AErrorLength,AErrorClass,AErrorClassLength);
+end;
+
+exports
+  GetPas2JSCompiler,
+  FreePas2JSCompiler,
+  RunPas2JSCompiler,
+  SetPas2JSWriteJSCallBack,
+  SetPas2JSCompilerLogCallBack,
+  GetPas2JSCompilerLastError;
+
+end.
+