PaxCompiler脚本编译的一些要注意的问题

使用PaxCompiler做脚本的时候,有的时候我们需要在脚本中引用其他的脚本文件,如果脚本文件放入程序的根目录引用是没有任何问题的,但是我们做脚本的时候不可能脚本都放到程序的根目录,这样引用的会找不到要引用的脚本文件pas,但是PaxCompiler提供了一个事件,可以在脚本编译的时候手工加载引用的pas具体如下:


PaxCompiler1.OnUsedUnit := DoUsedUnit;

function TForm1.DoUsedUnit(Sender: TPaxCompiler; const UnitName: string;
  var SourceCode: string): Boolean;
var
 List:TStrings ;
begin
 if CompareText(UnitName,'PXL') = 0 then
 begin
  List:=TStringList.Create ;
  try
  List.LoadFromFile(GetMediaPath+UnitName+'.pas');
  SourceCode :=List.Text;
  Result:=True ;
  Exit ;
  finally
  List.Free ;
  end;
 end;
 Result:=False ;
end;


还有需要注意的是,如果是预编译脚本成二进制文件,尽量不要将引用文件生成PCU文件,默认PCU是放入程序根目录的,如果脚本不放入根目录会导致异常,其实也可以使用Delphi的标准加载单元的方式 in “.\***.pas”,但是考虑跨平台的需求,就尽量不能使用了,编译脚本的时候设置不生成引用文件的PCU文件就可以了,否则在移动平台会发生找不到目录的异常:


var
  PaxCompiler1: TPaxCompiler;
  PaxInterpreter1: TPaxInterpreter;
  PaxPascalLanguage1: TPaxPascalLanguage;
begin
  PaxCompiler1 := TPaxCompiler.Create(nil);
  PaxInterpreter1 := TPaxInterpreter.Create(nil);
  PaxPascalLanguage1 := TPaxPascalLanguage.Create(nil);
  try
  PaxCompiler1.Reset ;
  //PaxCompiler1.OnSavePCU:=DoSavePCU ;
  //PaxCompiler1.OnSavePCUFinished:=DoSavePCUFinished;
//  PaxCompiler1.AddModule('1', 'Pascal');
//  PaxCompiler1.AddCodeFromFile('1',GetMediaPath+'PXL.pas') ;
//  if PaxCompiler1.Compile(PaxInterpreter1, true, false) then
//  begin
//    PaxInterpreter1.SaveToFile(GetMediaPath+'PXL.PCU');
//  end;
//



  PaxCompiler1.OnUsedUnit := DoUsedUnit;
//  PaxCompiler1.OnSavePCU:=DoSavePCU ;
//  PaxCompiler1.OnSavePCUFinished:=DoSavePCUFinished;
//  PaxCompiler1.OnLoadPCU:=DoLoadPCU;
//  PaxCompiler1.OnLoadPCUFinished:=DoLoadPCUFinished;

  PaxCompiler1.RegisterLanguage(PaxPascalLanguage1);
  PaxCompiler1.AddModule('1', 'Pascal');
  PaxCompiler1.AddCodeFromFile('1', GetMediaPath+'script.pas');

  {参数二 是否保存引用文件的PCU,参数三 是否将PCU文件作为运行时包}
  if PaxCompiler1.Compile(PaxInterpreter1, False, false) then
  begin
    //PaxInterpreter1.Run ;
    PaxInterpreter1.SaveToFile(GetMediaPath+'script.bin');
  end
  else
  begin
   ShowMessage(PaxCompiler1.ErrorMessage[0]) ;
  end;

  finally
  FreeAndNil(PaxCompiler1);
  FreeAndNil(PaxInterpreter1);
  FreeAndNil(PaxPascalLanguage1);
  end;
end;

为了保证运行的效率,在程序开始的时候或者直接发行编译成二进制的脚本文件会更有效率,否则的话在手机平台下,就编译一个空的脚本文件也是需要很长时间的,程序会发生明显的卡顿,更不要说每次执行脚本都先编译再执行,那如果再手机平台下,程序估计会卡的死死的,通过上面的编译就可以生成一个二进制的包,每次执行脚本我们都执行那个二进制的脚本文件就可以了,省略了每次编译的过程,程序速度是非常快的,省略编译的步奏,脚本的执行速度和delphi自己的执行速度是差不多的, PaxCompiler自带的例子里面有测试,具体执行二进制脚本代码如下:



var
 PaxInterpreter1: TPaxInterpreter;
begin
 PaxInterpreter1 := TPaxInterpreter.Create(nil);
 try
  PaxInterpreter1.LoadFromFile(GetMediaPath+'script.bin');
  PaxInterpreter1.MapGlobal;
  PaxInterpreter1.Run;
 finally
 FreeAndNil(PaxInterpreter1);
 end;
end;



一般只要编译成功,执行二进制脚本不会发生错误的,需要注意的是保证注册到脚本的类函数等正确.




paxCompiler is an embeddable compiler of the Object Pascal, Basic and JavaScript programming languages. The key features of the paxCompiler are: The compiler generates machine code for Intel compatible processors (IA-32 architecture). It is possible to use the compiler as a scripting engine. You can embed the compiler into host application, register host-defined types, routines, variables and constants for the engine, read/write script-defined variables, call script-defined functions etc. You can save/load compiled scripts to/from a stream. The compiler was initially written in Borland Delphi as a set of Delphi components. TPaxCompiler, TPaxProgram and TPaxPascalLanguage are Delphi components that allows you to embed the paxCompiler into Delphi, Kylix or Borland C++ Builder application so you can customize and extend the application without having to recompile it. Source code of paxCompiler is compatible with Free Pascal 2.0. paxCompiler, implemented as paxcompilerlib.dll, can be used for scripting Microsoft Visual C++ applications. The compiler supports Object Pascal language based on the Delphi 7 standard. Syntax of Basic language is similar to VB.NET. Script-defined functions support register (Borland fastcall), cdecl, stdcall, safecall, pascal and msfastcall (Microsoft fastcall) calling conventions. Cross-language programming support. You can use Pascal units in Basic and JavaScript programs and vice versa. COM support. TPaxCompilerDebugger and TPaxCompilerExplorer components implement debugger (breakpoints, call stack, watches, step over, trace into, run to cursor, pause) and code explorer capabilities. Script-defined types support run-time information (RTTI). You can bind instances of script-defined classes with dfm files. You can import host-defined types with paxCompiler importer. You can create stand alone executable files. Support of compiled units and run-time packages. paxCompiler engine is thread safe. The roadmap of the compiler includes: Java compiler, C++ compiler, built-in assembler.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值