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.//还有一些功能没有测试 如设置脚本变量的值 获取脚本变量的值等

测试RemObjects Pascal Script的更多相关文章

  1. 使用RemObjects Pascal Script (转)

    http://www.cnblogs.com/MaxWoods/p/3304954.html 摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演 ...

  2. 使用RemObjects Pascal Script

    摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演示如何创建一些简单的脚本. Pascal Script包括两个不同部分: 编译器 (uPSCo ...

  3. 在delphi中嵌入脚本语言--(译)RemObjects Pascal Script使用说明(1)(译)

    翻譯這篇文章源於我的一個通用工資計算平台的想法,在工資的計算中,不可避免的需要使用到自定義公式,然而對於自定義公式的實現,我自己想了一些,也在網上搜索了很多,解決辦法大致有以下幾種: 1. 自己寫代碼 ...

  4. Inno Setup Pascal Script to search for running process

    I am currently trying to do a validation at the uninstall moment. In a Pascal script function, in In ...

  5. Pascal Script

    MsgBox http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_msgbox ExpandConstant http://www.jrs ...

  6. delphi一些小技巧 从别处看到

    开发环境--------    Delphi 7是一个很经典的版本,在Win2000/XP下推荐安装Delphi 7来开发软件,在Vista下推荐使用Delphi 2007开发软件.安装好Delphi ...

  7. (转载)Delphi开发经验谈

    Delphi开发经验谈 开发环境-------- Delphi 7是一个很经典的版本,在Win2000/XP下推荐安装Delphi 7来开发软件,在Vista下推荐使用Delphi 2007开发软件. ...

  8. Pascal编译器大全(非常难得)

    http://www.pascaland.org/pascall.htm Some titles (french) : Compilateurs Pascal avec sources = compi ...

  9. 测试框架Mocha与断言expect

    测试框架Mocha与断言expect在浏览器和Node环境都可以使用除了Mocha以外,类似的测试框架还有Jasmine.Karma.Tape等,也很值得学习. 整个项目源代码: 为什么学习测试代码? ...

随机推荐

  1. C++ 螺旋矩阵算法

    清理磁盘空间的时候翻出了多年前写过的螺旋矩阵,代码效率和水平较低,纪念一下,保存到博客园! // ConsoleApplication3.cpp : 定义控制台应用程序的入口点. // #includ ...

  2. MySQL之EXPLAIN 执行计划详解

    explain 可以分析 select 语句的执行,即 MySQL 的“执行计划. 一.type 列 MySQL 在表里找到所需行的方式.包括(由左至右,由最差到最好): | All | index ...

  3. stdole.dll

    迁移至win1064位后,发布提示stdole.dll错误,查找半天,是因为引用了office组件问题,将其注释掉.解决.因为此块代码无用,但是对有用的代码如何解决发布问题,未找到合适解决方法.

  4. 设计模式及Python实现

    设计模式是什么? Christopher Alexander:“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心.这样你就能一次又一次地使用该方案而不必做重复劳动.” 设计 ...

  5. 关于sklearn,监督学习几种模型的对比

    # K近邻,适用于小型数据集,是很好的基准模型,容易解释 from sklearn.neighbors import KNeighborsClassifier # 线性模型,非常可靠的首选算法,适用于 ...

  6. python之Anaconda版本管理

    首先安装Anaconda,当其安装成功后,可以在cmd中测试是否安装成功,conda --version conda的环境管理 Conda的环境管理功能允许我们同时安装若干不同版本的Python,并能 ...

  7. mysql innodb 行级锁升级

    创建数据表test,表定义如下所示: CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NO ...

  8. 使用jstl方式替换服务器请求地址

    <c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>

  9. Vue 虚拟Dom 及 部分生命周期初探

    踏入前端,步入玄学 17年底至18年初附带做了vue的一些框架搭建,中途断断续续用了部分vue,时隔几个月后的工作又拾起vue,对于一些原理性的知识淡忘了,正值这段时间使用中遇到了一些坑,又拨了部分代 ...

  10. Sublime Text 中open in browser /view in browser 无反应

    问题 早上用Sublime Text写html的时候,发现右键的open in browser或view in browser命令都突然无法使用了,无法像以前一样在浏览器打开编写的页面了. 开始以为是 ...