mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-10-16 08:46:07 +02:00
* Move compiler files to package.
* Separate lib compiler from library file. git-svn-id: trunk@37835 -
This commit is contained in:
parent
5bedaf11f4
commit
4a566ceec1
17
.gitattributes
vendored
17
.gitattributes
vendored
@ -6869,6 +6869,15 @@ packages/pastojs/Makefile.fpc svneol=native#text/plain
|
||||
packages/pastojs/fpmake.pp svneol=native#text/plain
|
||||
packages/pastojs/src/fppas2js.pp svneol=native#text/plain
|
||||
packages/pastojs/src/fppjssrcmap.pp svneol=native#text/plain
|
||||
packages/pastojs/src/pas2js_defines.inc svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jscompiler.pp svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jsfilecache.pp svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jsfileutils.pp svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jsfileutilsunix.inc svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jsfileutilswin.inc svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jslibcompiler.pp svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jslogger.pp svneol=native#text/plain
|
||||
packages/pastojs/src/pas2jspparser.pp svneol=native#text/plain
|
||||
packages/pastojs/tests/tcconverter.pp svneol=native#text/plain
|
||||
packages/pastojs/tests/tcmodules.pas svneol=native#text/plain
|
||||
packages/pastojs/tests/tcoptimizations.pas svneol=native#text/plain
|
||||
@ -16945,16 +16954,8 @@ utils/pas2js/fpmake.pp svneol=native#text/plain
|
||||
utils/pas2js/pas2js.cfg svneol=native#text/plain
|
||||
utils/pas2js/pas2js.lpi svneol=native#text/plain
|
||||
utils/pas2js/pas2js.pp svneol=native#text/plain
|
||||
utils/pas2js/pas2js_defines.inc svneol=native#text/plain
|
||||
utils/pas2js/pas2jscompiler.pp svneol=native#text/plain
|
||||
utils/pas2js/pas2jsfilecache.pp svneol=native#text/plain
|
||||
utils/pas2js/pas2jsfileutils.pp svneol=native#text/plain
|
||||
utils/pas2js/pas2jsfileutilsunix.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/pas2jspparser.pp svneol=native#text/plain
|
||||
utils/pas2js/samples/arraydemo.pp svneol=native#text/plain
|
||||
utils/pas2js/samples/fordemo.pp svneol=native#text/plain
|
||||
utils/pas2js/samples/fordowndemo.pp svneol=native#text/plain
|
||||
|
@ -21,19 +21,30 @@ begin
|
||||
P.Version:='3.1.1';
|
||||
P.OSes := AllOses-[embedded,msdos,win16,macos,palmos];
|
||||
P.Dependencies.Add('fcl-js');
|
||||
P.Dependencies.Add('fcl-json');
|
||||
P.Dependencies.Add('fcl-passrc');
|
||||
Defaults.Options.Add('-Sc');
|
||||
|
||||
P.Author := 'Free Pascal development team';
|
||||
P.License := 'LGPL with modification, ';
|
||||
P.HomepageURL := 'www.freepascal.org';
|
||||
|
||||
P.SourcePath.Add('src');
|
||||
P.IncludePath.Add('src');
|
||||
P.SupportBuildModes := [bmOneByOne];
|
||||
|
||||
P.Options.Add('-S2h');
|
||||
|
||||
T:=P.Targets.AddUnit('fppas2js.pp');
|
||||
T:=P.Targets.AddUnit('fppjssrcmap.pp');
|
||||
T:=P.Targets.AddUnit('pas2jsfilecache.pp');
|
||||
T:=P.Targets.AddUnit('pas2jsfileutils.pp');
|
||||
T.Dependencies.AddInclude('pas2jsfileutilsunix.inc',AllUnixOSes);
|
||||
T.Dependencies.AddInclude('pas2jsfileutilswin.inc',AllWindowsOSes);
|
||||
T:=P.Targets.AddUnit('pas2jslogger.pp');
|
||||
T:=P.Targets.AddUnit('pas2jspparser.pp');
|
||||
T:=P.Targets.AddUnit('pas2jscompiler.pp');
|
||||
T:=P.Targets.AddUnit('pas2jslibcompiler.pp');
|
||||
{$ifndef ALLPACKAGES}
|
||||
Run;
|
||||
end;
|
||||
|
258
packages/pastojs/src/pas2jslibcompiler.pp
Normal file
258
packages/pastojs/src/pas2jslibcompiler.pp
Normal file
@ -0,0 +1,258 @@
|
||||
unit pas2jslibcompiler;
|
||||
|
||||
{$mode objfpc}
|
||||
{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, Classes, FPPJsSrcMap, Pas2jsFileCache, Pas2jsCompiler;
|
||||
|
||||
{ ---------------------------------------------------------------------
|
||||
Compiler descendant, usable in library
|
||||
---------------------------------------------------------------------}
|
||||
Const
|
||||
DefaultReadBufferSize = 32*1024; // 32kb buffer
|
||||
|
||||
Type
|
||||
TLibLogCallBack = Procedure (Data : Pointer; Msg : PAnsiChar; MsgLen : Integer); stdcall;
|
||||
TWriteJSCallBack = Procedure (Data : Pointer;
|
||||
AFileName: PAnsiChar; AFileNameLen : Integer;
|
||||
AFileData : PAnsiChar; AFileDataLen: Int32); stdcall;
|
||||
TReadPasCallBack = Procedure (Data : Pointer;
|
||||
AFileName: PAnsiChar; AFileNameLen : Integer;
|
||||
AFileData : PAnsiChar; Var AFileDataLen: Int32); stdcall;
|
||||
|
||||
{ TLibraryPas2JSCompiler }
|
||||
|
||||
TLibraryPas2JSCompiler = Class(TPas2JSCompiler)
|
||||
private
|
||||
FLastError: String;
|
||||
FLastErrorClass: String;
|
||||
FOnLibLogCallBack: TLibLogCallBack;
|
||||
FOnLibLogData: Pointer;
|
||||
FOnReadPasData: Pointer;
|
||||
FOnReadPasFile: TReadPasCallBack;
|
||||
FOnWriteJSCallBack: TWriteJSCallBack;
|
||||
FOnWriteJSData: Pointer;
|
||||
FReadBufferLen: Cardinal;
|
||||
Protected
|
||||
Function DoWriteJSFile(const DestFilename: String; aWriter: TPas2JSMapper): Boolean; override;
|
||||
Procedure GetLastError(AError : PAnsiChar; Var AErrorLength : Longint;
|
||||
AErrorClass : PAnsiChar; Var AErrorClassLength : Longint);
|
||||
Function ReadFile(aFilename: string; var aSource: string): boolean; virtual;
|
||||
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;
|
||||
Property OnReadPasFile : TReadPasCallBack Read FOnReadPasFile Write FOnReadPasFile;
|
||||
Property OnReadPasData : Pointer Read FOnReadPasData Write FOnReadPasData;
|
||||
Property ReadBufferLen : Cardinal Read FReadBufferLen Write FReadBufferLen;
|
||||
end;
|
||||
|
||||
Type
|
||||
PPas2JSCompiler = Pointer;
|
||||
PStubCreator = Pointer;
|
||||
|
||||
|
||||
Procedure SetPas2JSWriteJSCallBack(P : PPas2JSCompiler; ACallBack : TWriteJSCallBack; CallBackData : Pointer); stdcall;
|
||||
Procedure SetPas2JSCompilerLogCallBack(P : PPas2JSCompiler; ACallBack : TLibLogCallBack; CallBackData : Pointer); stdcall;
|
||||
Procedure SetPas2JSReadPasCallBack(P : PPas2JSCompiler; ACallBack : TReadPasCallBack; CallBackData : Pointer; ABufferSize : Cardinal); stdcall;
|
||||
Function RunPas2JSCompiler(P : PPas2JSCompiler; ACompilerExe, AWorkingDir : PAnsiChar; CommandLine : PPAnsiChar; DoReset : Boolean) : Boolean; stdcall;
|
||||
Procedure FreePas2JSCompiler(P : PPas2JSCompiler); stdcall;
|
||||
Function GetPas2JSCompiler : PPas2JSCompiler; stdcall;
|
||||
Procedure GetPas2JSCompilerLastError(P : PPas2JSCompiler; AError : PAnsiChar; Var AErrorLength : Longint; AErrorClass : PAnsiChar; Var AErrorClassLength : Longint); stdcall;
|
||||
|
||||
implementation
|
||||
|
||||
{ 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);
|
||||
AErrorLength:=L;
|
||||
L:=Length(LastErrorClass);
|
||||
if L>AErrorClassLength then
|
||||
L:=AErrorClassLength;
|
||||
if (L>0) then
|
||||
Move(FLastErrorClass[1],AErrorClass^,L);
|
||||
AErrorClassLength:=L;
|
||||
end;
|
||||
|
||||
function TLibraryPas2JSCompiler.ReadFile(aFilename: string; var aSource: string): boolean;
|
||||
|
||||
Var
|
||||
Buf : Array of AnsiChar;
|
||||
S : TStringStream;
|
||||
BytesRead : Cardinal;
|
||||
|
||||
begin
|
||||
if Not Assigned(OnReadPasFile) then
|
||||
Exit(False);
|
||||
S:=nil;
|
||||
try
|
||||
if ReadBufferLen=0 then
|
||||
ReadBufferLen:=DefaultReadBufferSize;
|
||||
SetLength(Buf,ReadBufferLen);
|
||||
S:=TStringStream.Create(''{$IF FPC_FULLVERSION>=30101},CP_ACP{$ENDIF});
|
||||
Repeat
|
||||
BytesRead:=ReadBufferLen;
|
||||
FOnReadPasFile(OnReadPasData,PAnsiChar(aFileName),Length(aFileName),@Buf[0],BytesRead);
|
||||
If BytesRead>0 then
|
||||
S.Write(Buf[0],BytesRead);
|
||||
Until (BytesRead<ReadBufferLen);
|
||||
Result:=S.Size<>0;
|
||||
if Result then
|
||||
aSource:=S.DataString;
|
||||
finally
|
||||
SetLength(Buf,0);
|
||||
S.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TLibraryPas2JSCompiler.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
Log.OnLog:=@DoLibraryLog;
|
||||
FileCache.OnReadFile:=@ReadFile;
|
||||
FReadBufferLen:=DefaultReadBufferSize;
|
||||
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
|
||||
---------------------------------------------------------------------}
|
||||
|
||||
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;
|
||||
|
||||
Procedure SetPas2JSReadPasCallBack(P : PPas2JSCompiler; ACallBack : TReadPasCallBack; CallBackData : Pointer; ABufferSize : Cardinal); stdcall;
|
||||
|
||||
begin
|
||||
TLibraryPas2JSCompiler(P).OnReadPasData:=CallBackData;
|
||||
TLibraryPas2JSCompiler(P).OnReadPasFile:=ACallback;
|
||||
if (ABufferSize=0) then
|
||||
ABufferSize:=DefaultReadBufferSize;
|
||||
TLibraryPas2JSCompiler(P).ReadBufferLen:=ABufferSize;
|
||||
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;
|
||||
|
||||
end.
|
||||
|
@ -21,33 +21,16 @@ begin
|
||||
P.HomepageURL := 'www.freepascal.org';
|
||||
P.Description := 'Convert pascal sources to javascript.';
|
||||
P.Email := 'michael@freepascal.org';
|
||||
Defaults.Options.Add('-Sc');
|
||||
P.NeedLibC:= false;
|
||||
|
||||
P.Directory:=ADirectory;
|
||||
P.Version:='3.1.1';
|
||||
P.Dependencies.Add('fcl-json');
|
||||
P.Dependencies.Add('fcl-js');
|
||||
P.Dependencies.Add('fcl-passrc');
|
||||
P.Dependencies.Add('pastojs');
|
||||
T:=P.Targets.AddUnit('pas2jscompiler.pp');
|
||||
T:=P.Targets.AddUnit('pas2jsfilecache.pp');
|
||||
T:=P.Targets.AddUnit('pas2jsfileutils.pp');
|
||||
T.Dependencies.AddInclude('pas2jsfileutilsunix.inc',AllUnixOSes);
|
||||
T.Dependencies.AddInclude('pas2jsfileutilswin.inc',AllWindowsOSes);
|
||||
T:=P.Targets.AddUnit('pas2jslogger.pp');
|
||||
T:=P.Targets.AddUnit('pas2jspparser.pp');
|
||||
PT:=P.Targets.AddProgram('pas2js.pp');
|
||||
PT.Dependencies.AddUnit('pas2jscompiler');
|
||||
PT.Dependencies.AddUnit('pas2jsfileutils');
|
||||
PT.Dependencies.AddUnit('pas2jsfilecache');
|
||||
PT.Dependencies.AddUnit('pas2jslogger');
|
||||
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;
|
||||
|
||||
|
@ -21,9 +21,16 @@
|
||||
<Version Value="2"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<CommandLineParams Value="-Tbrowser -Jirtl.js -Jc /home/michael/projects/pas2js/demo/rtl/democollection.pas"/>
|
||||
</local>
|
||||
<FormatVersion Value="2"/>
|
||||
<Modes Count="1">
|
||||
<Mode0 Name="default"/>
|
||||
<Mode0 Name="default">
|
||||
<local>
|
||||
<CommandLineParams Value="-Tbrowser -Jirtl.js -Jc /home/michael/projects/pas2js/demo/rtl/democollection.pas"/>
|
||||
</local>
|
||||
</Mode0>
|
||||
</Modes>
|
||||
</RunParams>
|
||||
<Units Count="9">
|
||||
|
@ -4,6 +4,7 @@
|
||||
<Version Value="11"/>
|
||||
<General>
|
||||
<Flags>
|
||||
<SaveOnlyProjectUnits Value="True"/>
|
||||
<MainUnitHasCreateFormStatements Value="False"/>
|
||||
<MainUnitHasTitleStatement Value="False"/>
|
||||
</Flags>
|
||||
@ -25,45 +26,15 @@
|
||||
<Mode0 Name="default"/>
|
||||
</Modes>
|
||||
</RunParams>
|
||||
<Units Count="9">
|
||||
<Units Count="2">
|
||||
<Unit0>
|
||||
<Filename Value="pas2jslib.pp"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit0>
|
||||
<Unit1>
|
||||
<Filename Value="pas2jscompiler.pp"/>
|
||||
<Filename Value="../../packages/pastojs/src/pas2jslibcompiler.pp"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="Pas2jsCompiler"/>
|
||||
</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>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
|
@ -4,241 +4,8 @@ library pas2jslib;
|
||||
{$H+}
|
||||
|
||||
uses
|
||||
SysUtils, Classes, FPPJsSrcMap, Pas2jsFileCache, Pas2jsCompiler;
|
||||
SysUtils, Classes, FPPJsSrcMap, Pas2jsFileCache, Pas2jsCompiler, Pas2jsLibCompiler;
|
||||
|
||||
{ ---------------------------------------------------------------------
|
||||
Compiler descendant, usable in library
|
||||
---------------------------------------------------------------------}
|
||||
Const
|
||||
DefaultReadBufferSize = 32*1024; // 32kb buffer
|
||||
|
||||
Type
|
||||
TLibLogCallBack = Procedure (Data : Pointer; Msg : PAnsiChar; MsgLen : Integer); stdcall;
|
||||
TWriteJSCallBack = Procedure (Data : Pointer;
|
||||
AFileName: PAnsiChar; AFileNameLen : Integer;
|
||||
AFileData : PAnsiChar; AFileDataLen: Int32); stdcall;
|
||||
TReadPasCallBack = Procedure (Data : Pointer;
|
||||
AFileName: PAnsiChar; AFileNameLen : Integer;
|
||||
AFileData : PAnsiChar; Var AFileDataLen: Int32); stdcall;
|
||||
|
||||
{ TLibraryPas2JSCompiler }
|
||||
|
||||
TLibraryPas2JSCompiler = Class(TPas2JSCompiler)
|
||||
private
|
||||
FLastError: String;
|
||||
FLastErrorClass: String;
|
||||
FOnLibLogCallBack: TLibLogCallBack;
|
||||
FOnLibLogData: Pointer;
|
||||
FOnReadPasData: Pointer;
|
||||
FOnReadPasFile: TReadPasCallBack;
|
||||
FOnWriteJSCallBack: TWriteJSCallBack;
|
||||
FOnWriteJSData: Pointer;
|
||||
FReadBufferLen: Cardinal;
|
||||
Protected
|
||||
Function DoWriteJSFile(const DestFilename: String; aWriter: TPas2JSMapper): Boolean; override;
|
||||
Procedure GetLastError(AError : PAnsiChar; Var AErrorLength : Longint;
|
||||
AErrorClass : PAnsiChar; Var AErrorClassLength : Longint);
|
||||
Function ReadFile(aFilename: string; var aSource: string): boolean; virtual;
|
||||
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;
|
||||
Property OnReadPasFile : TReadPasCallBack Read FOnReadPasFile Write FOnReadPasFile;
|
||||
Property OnReadPasData : Pointer Read FOnReadPasData Write FOnReadPasData;
|
||||
Property ReadBufferLen : Cardinal Read FReadBufferLen Write FReadBufferLen;
|
||||
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;
|
||||
|
||||
function TLibraryPas2JSCompiler.ReadFile(aFilename: string; var aSource: string): boolean;
|
||||
|
||||
Var
|
||||
Buf : Array of AnsiChar;
|
||||
S : TStringStream;
|
||||
BytesRead : Cardinal;
|
||||
|
||||
begin
|
||||
if Not Assigned(OnReadPasFile) then
|
||||
Exit(False);
|
||||
S:=nil;
|
||||
try
|
||||
if ReadBufferLen=0 then
|
||||
ReadBufferLen:=DefaultReadBufferSize;
|
||||
SetLength(Buf,ReadBufferLen);
|
||||
S:=TStringStream.Create(''{$IF FPC_FULLVERSION>=30101},CP_ACP{$ENDIF});
|
||||
Repeat
|
||||
BytesRead:=ReadBufferLen;
|
||||
FOnReadPasFile(OnReadPasData,PAnsiChar(aFileName),Length(aFileName),@Buf[0],BytesRead);
|
||||
If BytesRead>0 then
|
||||
S.Write(Buf[0],BytesRead);
|
||||
Until (BytesRead<ReadBufferLen);
|
||||
Result:=S.Size<>0;
|
||||
if Result then
|
||||
aSource:=S.DataString;
|
||||
finally
|
||||
SetLength(Buf,0);
|
||||
S.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TLibraryPas2JSCompiler.Create;
|
||||
begin
|
||||
inherited Create;
|
||||
Log.OnLog:=@DoLibraryLog;
|
||||
FileCache.OnReadFile:=@ReadFile;
|
||||
FReadBufferLen:=DefaultReadBufferSize;
|
||||
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;
|
||||
|
||||
Procedure SetPas2JSReadPasCallBack(P : PPas2JSCompiler; ACallBack : TReadPasCallBack; CallBackData : Pointer; ABufferSize : Cardinal); stdcall;
|
||||
|
||||
begin
|
||||
TLibraryPas2JSCompiler(P).OnReadPasData:=CallBackData;
|
||||
TLibraryPas2JSCompiler(P).OnReadPasFile:=ACallback;
|
||||
if (ABufferSize=0) then
|
||||
ABufferSize:=DefaultReadBufferSize;
|
||||
TLibraryPas2JSCompiler(P).ReadBufferLen:=ABufferSize;
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user