测试RemObjects Pascal Script

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, uPSComponent, StdCtrls, uPSCompiler, uPSUtils, uPSRuntime;

type

TTestFunction = function (Param1: Double; Data: string): LongInt of object;

TForm1 = class(TForm)
    st: TPSScript;
    Button1: TButton;
    Button2: TButton;
    procedure stCompile(Sender: TPSScript);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure stVerifyProc(Sender: TPSScript; Proc: TPSInternalProcedure;
      const Decl: string; var Error: Boolean);
private
    procedure ShowNewMessage(const AMessage: string);

end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
with st.Script do
begin
    Add('Program test;');
    Add('function TestFunction(Param1: Double; Data: String): Longint;');
    Add('begin');
    Add(' ShowNewMessage(''Param1:''+ FloatToStr(Param1)+ #13#10+ ''Data''+ Data);');
    Add(' Result := 1234567;');
    Add('end;');
    Add('var MyVar: Integer;');
    Add('begin');
    Add(' MyVar := 1000;');
    Add(' ShowNewMessage(''全局变量MyVar=''+IntToStr(MyVar)+'' '');//调用了Delphi中的方法');
    Add('end.');
end;
if not st.Compile then
    raise Exception.Create('编译的时候发生错误!');
st.Execute;
end;

//Delphi调用脚本中的方法
procedure TForm1.Button2Click(Sender: TObject);
var
meth: TTestFunction;
begin
meth := TTestFunction(st.GetProcMethod('TESTFUNCTION'));
if @meth= nil then
    raise Exception.Create('Unable call TestFunction');
ShowMessage('Result:'+ IntToStr(meth(Pi, DateTimeToStr(Now))));
end;

//Delphi声明的方法 可以在脚本中调用
procedure TForm1.ShowNewMessage(const AMessage: string);
begin
ShowMessage('ShowNewMessage invoked:'+ #13#10+ AMessage);
end;

//在TPSScript控件的Compile事件中导出方法 这样就可以在脚本中调用了
procedure TForm1.stCompile(Sender: TPSScript);
begin
Sender.AddMethod(Self, @TForm1.ShowNewMessage,
                   'procedure ShowNewMessage (const AMessage: string);');
end;

//在调用脚本中的方法前触发TPSScript控件的VerifyProc事件验证函数类型[返回值,参数等信息]
procedure TForm1.stVerifyProc(Sender: TPSScript; Proc: TPSInternalProcedure;
const Decl: string; var Error: Boolean);
begin
if Proc.Name = 'TESTFUNCTION' then
begin
    if not ExportCheck(Sender.Comp, Proc, [btS32, btDouble, btString], [pmIn, pmIn]) then
    begin
      Sender.Comp.MakeError('',ecCustomError, 'Function header for TestFunction does not match.');
      Error := True;
    end
    else
    begin
      Error := False;
    end;

end
else
    Error := False;
end;

end.//还有一些功能没有测试 如设置脚本变量的值 获取脚本变量的值等

超强大、好用的Pascal语言解释器:RemObjects Pascal Script,支持以下特性: Pascal Script is a widely-used set of components for Delphi that makes it easy to add Pascal-based scripting support to your applications, so that they can extend or control your application with custom scripts. Get Pascal Script Free Now Start using Pascal Script now and download your free copy, including full source code. Pascal Script for Delphi Pascal Script is a free scripting engine that allows you to use most of the Object Pascal language within your Delphi or Free Pascal projects at runtime. Written completely in Delphi, it is composed of a set of units that can be compiled into your executable, eliminating the need to distribute any external files. Pascal Script started out as a need for a good working script, when there were none available at the time. Why use a scripting engine? A scripting engine allows an end user to customize an application to his or her needs without having to recompile it. In addition, you can update your applications by just sending a new script file that could even be compiled to byte code, which cannot easily be transformed back to source code. Pascal Script includes the following features: Variables, Constants Standard language constructs: Begin/End If/Then/Else For/To/Downto/Do Case x Of Repeat/Until While Uses Exit Continue Break Functions inside the script Calling any external DLL function (no special function headers required) Calling registered external methods All common types like Byte, Shortint, Char, Word, SmallInt, Cardinal, Longint, Integer, String, Real, Double, Single, Extended, Boolean, Array, Record, Enumerations, Variants Allows the importing and use of classes, with events, properties, methods and constructors Allows the importing and use of interface and their members Allows IDispatch dynamic method invocation through Variant Assignment of script functions to Delphi events Uses byte code as an intermediate format and allows storing and reloading compiled scripts Easy to use component version Support for include files Support for compiler defines Capability to call RemObjects SDK Services from within scripts Pascal Script includes a tool to create headers for importing classes and interfaces
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值