unit Pas2jsCompilerProxy; {$IFDEF FPC} {$mode objfpc}{$H+} {$ENDIF} {$IFDEF darwin} {$DEFINE UseCDecl} {$ENDIF} interface uses Classes, SysUtils, LibPas2jsIntf; Type { TPas2JSCompilerProxy } TPas2JSCompilerProxy = class(TObject) Private FCompiler : PPas2JSCompiler; FPasFile: TFileStream; Protected Procedure WriteLog(Const S : AnsiString); Procedure WriteJS(Const AFileName,AFileData : AnsiString); Procedure StartReadPasFile(Const AFileName : AnsiString); Procedure ReadChunk(ABuffer : PAnsiChar; Var AChunkSize : Cardinal); Procedure DoneReadPasFile; Public Constructor Create; virtual; Destructor Destroy; override; Procedure Run(ACompilerExe, AWorkingDir : String; CommandLine : TStringList; DoReset : Boolean); Procedure Execute; Property PasFile : TFileStream Read FPasFile; end; implementation {$ifndef fpc} const AllFilesMask = '*.*'; type TUnicodeSearchRec = TSearchRec; {$endif} Procedure DoLog(Data : Pointer; Msg : PansiChar; MsgLen : Integer); {$IFDEF UseCDecl}cdecl{$ELSE}stdcall{$ENDIF}; Var S : AnsiString; begin SetLength(S{%H-},MsgLen); if MsgLen>0 then Move(Msg^,S[1],MsgLen); TPas2JSCompilerProxy(Data).WriteLog(S); end; Procedure DoWriteJS(Data : Pointer; AFileName: PAnsiChar; AFileNameLen : Integer; AFileData : PAnsiChar; AFileDataLen: Int32); {$IFDEF UseCDecl}cdecl{$ELSE}stdcall{$ENDIF}; Var Src,DestFileName : AnsiString; begin SetLength(DestFileName{%H-},AFileNameLen); if AFileNameLen>0 then Move(AFileName^,DestFileName[1],AFileNameLen); SetLength(Src{%H-},AFileDataLen); if AFileDataLen>0 then Move(AFileData^,Src[1],AFileDataLen); TPas2JSCompilerProxy(Data).WriteJS(DestFileName,Src); end; procedure DoReadPasJS(Data: Pointer; AFileName: PAnsiChar; AFileNameLen: Integer; AFileData: PAnsiChar; Var AFileDataLen: Cardinal); {$IFDEF UseCDecl}cdecl{$ELSE}stdcall{$ENDIF}; Var DestFileName : AnsiString; BytesToRead : Cardinal; begin SetLength(DestFileName{%H-},AFileNameLen); if AFileNameLen>0 then Move(AFileName^,DestFileName[1],AFileNameLen); TPas2JSCompilerProxy(Data).StartReadPasFile(AFileName); BytesToRead:=AFileDatalen; TPas2JSCompilerProxy(Data).ReadChunk(AFileData,AFileDataLen); if AFileDatalen0; end; FindClose(Info); Result:=true; end; Function DoUnitAlias(Data: Pointer; AUnitName: PAnsiChar; AUnitNameMaxLen: Integer): boolean; {$IFDEF UseCDecl}cdecl{$ELSE}stdcall{$ENDIF}; var Old, New: AnsiString; begin Result:=false; Old:=AUnitName; if Data=nil then ; New:=''; if SameText(Old,'Test.Foo.Alias1') then New:='Bar' else if SameText(Old,'Test.Foo.Alias2') then New:='Test.Foo.SomeLongUnitName'; if New<>'' then begin writeln('Info: DoUnitAlias Old="',Old,'" New="',New,'"'); if AUnitNameMaxLen make sure it is valid during this proc CmdLn[i]:=PAnsiChar(SCmdLn[i]); end; CmdLn[CommandLine.Count]:=Nil; if not RunPas2JSCompiler(FCompiler,PAnsiChar(ACompilerExe),PAnsiChar(AWorkingDir),PPAnsiChar(Cmdln),DoReset) then begin ErrorLength:=1024; ErrorClassLength:=1024; SetLength(Err{%H-},ErrorLength); SetLength(ErrClassname{%H-},ErrorClassLength); GetPas2JSCompilerLastError(FCompiler,@Err[1],ErrorLength,@ErrClassname[1],ErrorClassLength); SetLength(Err,ErrorLength); SetLength(ErrClassname,ErrorClassLength); writeln(Format('Error of class "%s" raised when compiling : %s',[ErrClassname,Err])); ExitCode:=1; end; end; procedure TPas2JSCompilerProxy.Execute; Var Cmd : TStringList; I : integer; begin Cmd:=TStringList.Create; try for I:=1 to ParamCount do Cmd.Add(Paramstr(i)); Run(ParamStr(0),GetCurrentDir,Cmd,False); finally Cmd.Free; end; end; end.