2023年6月20日发(作者:)

在delphi中嵌⼊脚本语⾔--(译)RemObjectsPascalScript使⽤说明。。。在delphi中嵌⼊脚本语⾔--(译)RemObjects Pascal Script使⽤说明(1)(译)

翻译这篇⽂章源于我的⼀个通⽤⼯资计算平台的想法,在⼯资的计算中,不可避免的需要使⽤到⾃定义公式,然⽽对于⾃定义公式的实现,我⾃⼰想了⼀些,也在⽹上搜索了很多,解决办法⼤致有以下⼏种:

1. ⾃⼰写代码去解析公式。这种⽅法的缺点是,解析的代码很难实现,如果公式的功能⽐较完整,如增加条件判断或⾃定义函数。不亚于实现了⼀个简单的语⾔编译嚣或解释嚣。所以,只能实现⼀些诸如加减乘除之类的简单公式。

2. 打包成SQL传给数据库去执⾏。这显然不是⼀种好办法。⽽且需要与特定的数据库和表结构进⾏适应。

3. 我想到在foxpro中有宏替换功能&,那不如就借⽤它的这个功能,即利⽤foxpro写⼀个dll,在这个dll中实现了将字符串转换成指令执⾏的功能,然后在delphi中加载这个dll,将公式传⼊dll中的函数执⾏。这应该是⼀个办法,但我还没有去实现它。

4. 内嵌脚本语⾔。

也只有第四种办法⽐较理想的,于是我就找到了RemObjects Pascal Script,安装,并翻译了这篇使⽤说明。

再把应⽤范围扩⼤⼀点,其实在编译型程序中嵌⼊脚本语⾔可以解决很多应⽤程序⾃动化的问题,在了解并实际写了⼏个RemObjectsPascal Script的实从程序后。内⼼还是蛮兴奋的。

PS01 - Using the RemObjects Pascal Script

使⽤RemObjects Pascal Script

This article provides an overview of the new RemObjects Pascal Script and explains how to create some simple scripts.

这篇⽂章提供了RemObjects Pascal Script的⼀个概览,以及说明了如何去创建⼀些简单的脚本。

Pascal Script comprises two different parts:

Compiler ()

Runtime ()

Pascal Script由两个部分组成:

编译嚣()

运⾏时()

The two parts have no interdependencies on each other. You can use them directly, or you can use them in the TPSScriptcomponent, which can be found in the unit, and wraps them both in one easy to use class.

这两部分之间是没有相互依赖的。你可以直接使⽤她们,或才你可以透过TPSSCript组件来使⽤她们,TPSSCript组件在单元中,她对上述两个部分进⾏⼀些包装以便我们可以很容易的使⽤。

To use the component version of Pascal Script, you must first place it on your form or data module, set or assign the scriptproperty, call the Compile method, and call the Execute method. Compile errors, warnings or hints can be found in theCompilerMessages array property, while runtime errors can be found by reading the ExecErrorToString property.

要使⽤Pascal Script组件,你⾸先要将它从组件⾯板中拖置窗体或module中,然后设置它的script属性,然后调⽤它的Compile⽅法进⾏编译,再然后调⽤它的Execute⽅法来执⾏脚本。编译的errors,hints,warnings可以通过其属性CompilerMessages取得,这个属性是⼀个数组。如果是运⾏时的错误,则可以通过属性ExecErrorToString取得。

The following example will compile and execute an empty script ("begin end."):

下⾯的例⼦将编译并执⾏⼀个空脚本("begin end."):

var

Messages: string;

compiled: boolean;

begin

:= 'begin end.';

Compiled := e;

for i := 0 to erMessageCount -1 do

Messages := Messages +

erMessages[i].MessageToString + #13#10;

if Compiled then

Messages := Messages + 'Succesfully compiled'#13#10;

ShowMessage('Compiled Script: '#13#10+Messages);

if Compiled then begin

if e then

ShowMessage('Succesfully Executed')

else

ShowMessage('Error while executing script: '+

rorToString);

end;

end;

By default, the component only adds a few standard functions to the scripting engine (the exact list can be found at the topof ).

缺省情况下,组件只加⼊⼀少部分标淮的functions到脚本引擎中(具体可以在单元头中找到)

Besides the standard functions, there are a few libraries included with Pascal Script:

TPSDllPlugin Allow scripts to use dll functions, the syntax is like:

function FindWindow(C1, C2: PChar): Longint; external 'FindWindowA@ stdcall';

TPSImport_Classes Import library for TObject and the Classes unit.

TPSImport_DateUtils Import library for date/time related functions.

TPSImport_ComObj Access COM Objects from your scripts.

TPSImport_DB Import library for .

TPSImport_Forms Import library for the Forms & Menus units.

TPSImport_Controls Import library to and .

TPSImport_StdCtrls Import library for ExtCtrls and Buttons.

除了这些标淮的functions之外,Pascal Script还包含了⼀少部分程式库:

TPSDllPlugin 允许脚本可以使⽤外部DLL函数,其调⽤语法类似下例:

function FindWindow(C1, C2: PChar): Longint; external 'FindWindowA@ stdcall';

TPSImport_Classes 导⼊对应于TObject和Classes单元的libraries;

TPSImport_DateUtils 导⼊⽇期时间相关的libraries;

TPSImport_ComObj 在脚本中访问COM对象;

TPSImport_DB 导⼊对应于单元的libraries;

TPSImport_Forms 导⼊对应于Forms和Menus单元的libraries;

TPSImport_Controls 导⼊对应于和单元的libraries;

TPSImport_StdCtrls 导⼊对应于ExtCtrls和Buttons的libraries.

To use these libraries, add them to your form or data module, select the [...] button next to the plugins property of theTPSCompiler component, add a new item and set the Plugin property to the plugin component.

要使⽤这些libraries,将它们从组件⾯板中拖⾄窗体数据data module中,然后设置TPSCompiler的plugins属性,在其中增加条⽬,并将条⽬指向这些plugin组件。

Besides the standard libraries, you can easily add new functions to the scripting engine. In order to do that, create a newmethod you would like to expose to the scripting engine, for example:

除了这些标淮的libraries之外,你还可以很⽅便地向脚本引擎中添加新的函数。要做到这⼀点,创建⼀个你要加⼊到脚本中的method,如下例:

procedure wMessage(const Message: string);

begin

ShowMessage('ShowNewMessage invoked:'#13#10+Message);

end;

Then, assign an event handler to the OnCompile event and use the AddMethod method of TPSCompiler to add the actualmethod:

然后,在TPSCompiler 的OnCompile事件中将该⽅法添加⼊脚本中:

procedure ile(Sender: TPSScript);

begin

hod(Self, @wMessage,

'procedure ShowNewMessage

(const Message: string);');

end;

A sample script that uses this function could look like this:

这样, 你就可以在脚本中使⽤这个函数,如下:

begin

ShowNewMessage('Show This !');

end.

Advanced Features

⾼级功能

Pascal Script includes a preprocessor that allows you to use defines ({$IFDEF}, {$ELSE}, {$ENDIF}) and include other files inyour script ({$I }). To enable these features, you must set the UsePreprocessor property to true and theMainFileName property to match the name of the script in the Script property. The Defines property specifies which definesare set by default, and the OnNeedFile event is called when an include file is needed.

Pascal Script包含了⼀个预处理程序,以便你可以在脚本中使⽤编译预定义(defines)({$IFDEF}, {$ELSE}, {$ENDIF}) 以及在脚本中包含其它脚本 ({$I })。要达到这个功能,你需要设置UsePreprocessor属性为true,and the MainFileName property tomatch the name of the script in the Script property. 。Defines属性指定要缺省时定义哪些defines;OnNeedFile事件代码在需要包含的⽂件时被执⾏。

function File(Sender: TObject;

const OrginFileName: String;

var FileName, Output: String): Boolean;

var

path: string;

f: TFileStream;

begin

Path := ExtractFilePath(ParamStr(0)) + FileName;

try

F := (Path, fmOpenRead or fmShareDenyWrite);

except

Result := false;

exit;

end;

try

SetLength(Output, );

(Output[1], Length(Output));

finally

;

end;

Result := True;

end;

When these properties are set, the CompilerMessages array property will include the file name these messages occur in.

当这些属性被设置以后,CompilerMessages属性可就将可能包含这些⽂件名。

Additionally, you can call scripted functions from Delphi. The following sample will be used as a script:

另外,你可以在Delphi中调⽤脚本裡的函数。如下函数被定义在脚本中,后⾯将会在delphi中被调⽤:

function TestFunction(Param1: Double; Data: String): Longint; begin

ShowNewMessage('Param1: '+FloatToString(param1)

+#13#10+'Data: '+Data);

Result := 1234567;

end;

begin

end.

Before this scripted function can be used, it has to be checked to match its parameter and result types, which can be donein the OnVerifyProc event.

在使⽤调⽤这个函数之前,必须对其进⾏⼀个校验,校验其参数和返回值类型,在OnVerifyProc执⾏这个校验。

procedure fyProc(Sender: TPSScript;

Proc: TPSInternalProcedure;

const Decl: String;

var Error: Boolean);

begin

if = 'TESTFUNCTION' then begin

if not ExportCheck(, Proc,

[btS32, btDouble, btString], [pmIn, pmIn]) then begin

ror('', ecCustomError, 'Function header for

TestFunction does not match.');

Error := True;

end

else begin

Error := False;

end;

end

else

Error := False;

end;

The ExportCheck function checks if the parameters match. In this case, btu8 is a boolean (the result type), btdouble is thefirst parameter, and btString the second parameter. [pmIn, pmIn] specifies that both parameters are IN parameters. To callthis scripted function, you have to create an event declaration for this function and call that.

ExportCheck函数检查参数的匹配情况。在这个例⼦中,btu8是⼀个布尔型(返回值类型),btdouble是第⼀个参数,btString是第⼆个参数。[pmIn, pmIn]表⽰两个参数都是输⼊参数。要调⽤这个脚本函数,你需要为它创建⼀个函数类型声明。

type

TTestFunction = function (Param1: Double;

Data: String): Longint of object;

//...

var

Meth: TTestFunction;

Meth := TTestFunction(cMethod('TESTFUNCTION'));

if @Meth = nil then

raise ('Unable to call TestFunction');

ShowMessage('Result: '+IntToStr(Meth(pi, DateTimeToStr(Now))));

It's also possible to add variables to the script engine, which can be used from within the script. To do this, you have to usethe AddRegisteredVariable function. You can set this in the OnExecute event :

还可以向脚本引擎中添加变量,然后就可以在脚本中使⽤这些变量 。要做到这⼀点,你需要使⽤AddRegisteredVariable函数。可以在OnExecute设置它:

procedure ute(Sender: TPSScript);

begin ToInstance('SELF', Self);

// ^^^ For class variables

VSetInt(iable('MYVAR'), 1234567);

end;

To read this variable back, after the script has finished executing, you can use the OnAfterExecute event:

若要再去读取这个变量的值,在脚本执⾏完成后,在OnAfterExecute事件中访问:

VGetInt(iable('MYVAR')).

Registering external variables to the script engine is also possible. It's a two step process, first, in the OnCompile event, addthe types to the script using the AddRegisteredPTRVariable function.

注册⼀个外部变量到脚本引擎中也是可以的。这需要两个步骤,⾸先在OnCompile事件中使⽤AddRegisteredPTRVariable函数将变量类型添加到脚本中。

procedure ptCompile(Sender: TPSScript);

begin

isteredPTRVariable('MyClass', 'TButton');

isteredPTRVariable('MyVar', 'Longint');

end;

This will register the external MyClass and MyVar variables. Second, attach a pointer to these variables in the OnExecuteevent:

这样就注册了MyClass 和 MyVar这两个变量。第⼆步,在OnExecute中通过将变量值的地址指针传给变量来实现给变量赋值:

procedure ptExecute(Sender: TPSScript);

begin

nterToData('MyVar', @MyVar, seType(bts32));

nterToData('Memo1', @Memo1, medType('TMemo'));

end;

There are two types of variables in Pascal Script, base types, which are simple types (see the table below), and class types are registered in the unit and can be located using the FindBaseType function. Class types have tobe located by name, using the FindNamedType. Changes to these variables have a direct effect on the actual variable.

在Pascal Script中有两种类型的变量,⼀种是基本类型,包含⼀些简单的类型,下⾯会列出;另⼀种是类类型。基本类型是在被注册进去的,可以通过FindBaseType函数找到。类类型需要使⽤FindNamedType函数通过名称找到。改变这些变量将直接地影响到实际的变量。

Base types:

btU8 Byte

btS8 Shortint

btU16 Word

btS16 Smallint

btU32 Longword

btS32 Longint

btS64 Int64

btSingle Single

btDouble Double

btExtended Extended

btVariant Variant

btString String

btWideString WideString

btChar Char

btWideChar WideChar

The component version of Pascal Script also supports execution of scripted functions. This works by using theExecuteFunction method. Pascal Script组件同样也⽀持脚本函数。这通过ExecuteFunction来调⽤。

ShowMessage(eFunction([1234.5678, 4321,

'test'],

'TestFunction'));

This will execute the function called 'TestFunction' with 3 parameters, a float, an integer and a string. The result will bepassed back to ShowMessage.

这个例⼦将执⾏⼀个名为TestFunction的函数,这个函数包含3个参数,⼀个float,⼀个integer和⼀个string。函数的返回值传回给ShowMessage。

Notes:

For some functions and constants, it might be necessary to add: , and/or toyour uses list.

The script engine never calls sMessages by itself, so your application might hang, while the script isrunning. To avoid this, you can add sMessages to the event.

It's possible to import your own classes in the script engine. Pascal Script includes a tool to create import libraries in the/Unit-Importing/ directory.

It's possible to import your own classes in the script engine. Pascal Script includes a tool to create import libraries in the Bindirectory.

For examples on how to use the compiler and runtime separately, see the Import and Kylix samples.

The Debug requires SynEdit /.

注意:

⼀些必要的函数和常量应该被加⼊到uses 列表中:, , ;

脚本引擎不会⾃⾏调⽤sMessages,因此在脚本执⾏时你的应⽤程序可能会终⽌。要避免这⼀点,你可以将sMessages加⼊到TPSScript的OnLine事件中;

可能需要在脚本中引⼊你⾃⼰的类,Pascal Script包含⼀个⼯具以便创建引⼊的库,这个⼯具在Unit-Importing⽬录中;

可能需要在脚本中引⼊你⾃⼰的类,Pascal Script包含⼀个⼯具以便创建引⼊的库,这个⼯具在Bin⽬录中;

安装⽬录中可以找到单独使⽤Comiler和Runtim的实例;

脚本调试需要SynEdit /.

--------------------

2023年6月20日发(作者:)

在delphi中嵌⼊脚本语⾔--(译)RemObjectsPascalScript使⽤说明。。。在delphi中嵌⼊脚本语⾔--(译)RemObjects Pascal Script使⽤说明(1)(译)

翻译这篇⽂章源于我的⼀个通⽤⼯资计算平台的想法,在⼯资的计算中,不可避免的需要使⽤到⾃定义公式,然⽽对于⾃定义公式的实现,我⾃⼰想了⼀些,也在⽹上搜索了很多,解决办法⼤致有以下⼏种:

1. ⾃⼰写代码去解析公式。这种⽅法的缺点是,解析的代码很难实现,如果公式的功能⽐较完整,如增加条件判断或⾃定义函数。不亚于实现了⼀个简单的语⾔编译嚣或解释嚣。所以,只能实现⼀些诸如加减乘除之类的简单公式。

2. 打包成SQL传给数据库去执⾏。这显然不是⼀种好办法。⽽且需要与特定的数据库和表结构进⾏适应。

3. 我想到在foxpro中有宏替换功能&,那不如就借⽤它的这个功能,即利⽤foxpro写⼀个dll,在这个dll中实现了将字符串转换成指令执⾏的功能,然后在delphi中加载这个dll,将公式传⼊dll中的函数执⾏。这应该是⼀个办法,但我还没有去实现它。

4. 内嵌脚本语⾔。

也只有第四种办法⽐较理想的,于是我就找到了RemObjects Pascal Script,安装,并翻译了这篇使⽤说明。

再把应⽤范围扩⼤⼀点,其实在编译型程序中嵌⼊脚本语⾔可以解决很多应⽤程序⾃动化的问题,在了解并实际写了⼏个RemObjectsPascal Script的实从程序后。内⼼还是蛮兴奋的。

PS01 - Using the RemObjects Pascal Script

使⽤RemObjects Pascal Script

This article provides an overview of the new RemObjects Pascal Script and explains how to create some simple scripts.

这篇⽂章提供了RemObjects Pascal Script的⼀个概览,以及说明了如何去创建⼀些简单的脚本。

Pascal Script comprises two different parts:

Compiler ()

Runtime ()

Pascal Script由两个部分组成:

编译嚣()

运⾏时()

The two parts have no interdependencies on each other. You can use them directly, or you can use them in the TPSScriptcomponent, which can be found in the unit, and wraps them both in one easy to use class.

这两部分之间是没有相互依赖的。你可以直接使⽤她们,或才你可以透过TPSSCript组件来使⽤她们,TPSSCript组件在单元中,她对上述两个部分进⾏⼀些包装以便我们可以很容易的使⽤。

To use the component version of Pascal Script, you must first place it on your form or data module, set or assign the scriptproperty, call the Compile method, and call the Execute method. Compile errors, warnings or hints can be found in theCompilerMessages array property, while runtime errors can be found by reading the ExecErrorToString property.

要使⽤Pascal Script组件,你⾸先要将它从组件⾯板中拖置窗体或module中,然后设置它的script属性,然后调⽤它的Compile⽅法进⾏编译,再然后调⽤它的Execute⽅法来执⾏脚本。编译的errors,hints,warnings可以通过其属性CompilerMessages取得,这个属性是⼀个数组。如果是运⾏时的错误,则可以通过属性ExecErrorToString取得。

The following example will compile and execute an empty script ("begin end."):

下⾯的例⼦将编译并执⾏⼀个空脚本("begin end."):

var

Messages: string;

compiled: boolean;

begin

:= 'begin end.';

Compiled := e;

for i := 0 to erMessageCount -1 do

Messages := Messages +

erMessages[i].MessageToString + #13#10;

if Compiled then

Messages := Messages + 'Succesfully compiled'#13#10;

ShowMessage('Compiled Script: '#13#10+Messages);

if Compiled then begin

if e then

ShowMessage('Succesfully Executed')

else

ShowMessage('Error while executing script: '+

rorToString);

end;

end;

By default, the component only adds a few standard functions to the scripting engine (the exact list can be found at the topof ).

缺省情况下,组件只加⼊⼀少部分标淮的functions到脚本引擎中(具体可以在单元头中找到)

Besides the standard functions, there are a few libraries included with Pascal Script:

TPSDllPlugin Allow scripts to use dll functions, the syntax is like:

function FindWindow(C1, C2: PChar): Longint; external 'FindWindowA@ stdcall';

TPSImport_Classes Import library for TObject and the Classes unit.

TPSImport_DateUtils Import library for date/time related functions.

TPSImport_ComObj Access COM Objects from your scripts.

TPSImport_DB Import library for .

TPSImport_Forms Import library for the Forms & Menus units.

TPSImport_Controls Import library to and .

TPSImport_StdCtrls Import library for ExtCtrls and Buttons.

除了这些标淮的functions之外,Pascal Script还包含了⼀少部分程式库:

TPSDllPlugin 允许脚本可以使⽤外部DLL函数,其调⽤语法类似下例:

function FindWindow(C1, C2: PChar): Longint; external 'FindWindowA@ stdcall';

TPSImport_Classes 导⼊对应于TObject和Classes单元的libraries;

TPSImport_DateUtils 导⼊⽇期时间相关的libraries;

TPSImport_ComObj 在脚本中访问COM对象;

TPSImport_DB 导⼊对应于单元的libraries;

TPSImport_Forms 导⼊对应于Forms和Menus单元的libraries;

TPSImport_Controls 导⼊对应于和单元的libraries;

TPSImport_StdCtrls 导⼊对应于ExtCtrls和Buttons的libraries.

To use these libraries, add them to your form or data module, select the [...] button next to the plugins property of theTPSCompiler component, add a new item and set the Plugin property to the plugin component.

要使⽤这些libraries,将它们从组件⾯板中拖⾄窗体数据data module中,然后设置TPSCompiler的plugins属性,在其中增加条⽬,并将条⽬指向这些plugin组件。

Besides the standard libraries, you can easily add new functions to the scripting engine. In order to do that, create a newmethod you would like to expose to the scripting engine, for example:

除了这些标淮的libraries之外,你还可以很⽅便地向脚本引擎中添加新的函数。要做到这⼀点,创建⼀个你要加⼊到脚本中的method,如下例:

procedure wMessage(const Message: string);

begin

ShowMessage('ShowNewMessage invoked:'#13#10+Message);

end;

Then, assign an event handler to the OnCompile event and use the AddMethod method of TPSCompiler to add the actualmethod:

然后,在TPSCompiler 的OnCompile事件中将该⽅法添加⼊脚本中:

procedure ile(Sender: TPSScript);

begin

hod(Self, @wMessage,

'procedure ShowNewMessage

(const Message: string);');

end;

A sample script that uses this function could look like this:

这样, 你就可以在脚本中使⽤这个函数,如下:

begin

ShowNewMessage('Show This !');

end.

Advanced Features

⾼级功能

Pascal Script includes a preprocessor that allows you to use defines ({$IFDEF}, {$ELSE}, {$ENDIF}) and include other files inyour script ({$I }). To enable these features, you must set the UsePreprocessor property to true and theMainFileName property to match the name of the script in the Script property. The Defines property specifies which definesare set by default, and the OnNeedFile event is called when an include file is needed.

Pascal Script包含了⼀个预处理程序,以便你可以在脚本中使⽤编译预定义(defines)({$IFDEF}, {$ELSE}, {$ENDIF}) 以及在脚本中包含其它脚本 ({$I })。要达到这个功能,你需要设置UsePreprocessor属性为true,and the MainFileName property tomatch the name of the script in the Script property. 。Defines属性指定要缺省时定义哪些defines;OnNeedFile事件代码在需要包含的⽂件时被执⾏。

function File(Sender: TObject;

const OrginFileName: String;

var FileName, Output: String): Boolean;

var

path: string;

f: TFileStream;

begin

Path := ExtractFilePath(ParamStr(0)) + FileName;

try

F := (Path, fmOpenRead or fmShareDenyWrite);

except

Result := false;

exit;

end;

try

SetLength(Output, );

(Output[1], Length(Output));

finally

;

end;

Result := True;

end;

When these properties are set, the CompilerMessages array property will include the file name these messages occur in.

当这些属性被设置以后,CompilerMessages属性可就将可能包含这些⽂件名。

Additionally, you can call scripted functions from Delphi. The following sample will be used as a script:

另外,你可以在Delphi中调⽤脚本裡的函数。如下函数被定义在脚本中,后⾯将会在delphi中被调⽤:

function TestFunction(Param1: Double; Data: String): Longint; begin

ShowNewMessage('Param1: '+FloatToString(param1)

+#13#10+'Data: '+Data);

Result := 1234567;

end;

begin

end.

Before this scripted function can be used, it has to be checked to match its parameter and result types, which can be donein the OnVerifyProc event.

在使⽤调⽤这个函数之前,必须对其进⾏⼀个校验,校验其参数和返回值类型,在OnVerifyProc执⾏这个校验。

procedure fyProc(Sender: TPSScript;

Proc: TPSInternalProcedure;

const Decl: String;

var Error: Boolean);

begin

if = 'TESTFUNCTION' then begin

if not ExportCheck(, Proc,

[btS32, btDouble, btString], [pmIn, pmIn]) then begin

ror('', ecCustomError, 'Function header for

TestFunction does not match.');

Error := True;

end

else begin

Error := False;

end;

end

else

Error := False;

end;

The ExportCheck function checks if the parameters match. In this case, btu8 is a boolean (the result type), btdouble is thefirst parameter, and btString the second parameter. [pmIn, pmIn] specifies that both parameters are IN parameters. To callthis scripted function, you have to create an event declaration for this function and call that.

ExportCheck函数检查参数的匹配情况。在这个例⼦中,btu8是⼀个布尔型(返回值类型),btdouble是第⼀个参数,btString是第⼆个参数。[pmIn, pmIn]表⽰两个参数都是输⼊参数。要调⽤这个脚本函数,你需要为它创建⼀个函数类型声明。

type

TTestFunction = function (Param1: Double;

Data: String): Longint of object;

//...

var

Meth: TTestFunction;

Meth := TTestFunction(cMethod('TESTFUNCTION'));

if @Meth = nil then

raise ('Unable to call TestFunction');

ShowMessage('Result: '+IntToStr(Meth(pi, DateTimeToStr(Now))));

It's also possible to add variables to the script engine, which can be used from within the script. To do this, you have to usethe AddRegisteredVariable function. You can set this in the OnExecute event :

还可以向脚本引擎中添加变量,然后就可以在脚本中使⽤这些变量 。要做到这⼀点,你需要使⽤AddRegisteredVariable函数。可以在OnExecute设置它:

procedure ute(Sender: TPSScript);

begin ToInstance('SELF', Self);

// ^^^ For class variables

VSetInt(iable('MYVAR'), 1234567);

end;

To read this variable back, after the script has finished executing, you can use the OnAfterExecute event:

若要再去读取这个变量的值,在脚本执⾏完成后,在OnAfterExecute事件中访问:

VGetInt(iable('MYVAR')).

Registering external variables to the script engine is also possible. It's a two step process, first, in the OnCompile event, addthe types to the script using the AddRegisteredPTRVariable function.

注册⼀个外部变量到脚本引擎中也是可以的。这需要两个步骤,⾸先在OnCompile事件中使⽤AddRegisteredPTRVariable函数将变量类型添加到脚本中。

procedure ptCompile(Sender: TPSScript);

begin

isteredPTRVariable('MyClass', 'TButton');

isteredPTRVariable('MyVar', 'Longint');

end;

This will register the external MyClass and MyVar variables. Second, attach a pointer to these variables in the OnExecuteevent:

这样就注册了MyClass 和 MyVar这两个变量。第⼆步,在OnExecute中通过将变量值的地址指针传给变量来实现给变量赋值:

procedure ptExecute(Sender: TPSScript);

begin

nterToData('MyVar', @MyVar, seType(bts32));

nterToData('Memo1', @Memo1, medType('TMemo'));

end;

There are two types of variables in Pascal Script, base types, which are simple types (see the table below), and class types are registered in the unit and can be located using the FindBaseType function. Class types have tobe located by name, using the FindNamedType. Changes to these variables have a direct effect on the actual variable.

在Pascal Script中有两种类型的变量,⼀种是基本类型,包含⼀些简单的类型,下⾯会列出;另⼀种是类类型。基本类型是在被注册进去的,可以通过FindBaseType函数找到。类类型需要使⽤FindNamedType函数通过名称找到。改变这些变量将直接地影响到实际的变量。

Base types:

btU8 Byte

btS8 Shortint

btU16 Word

btS16 Smallint

btU32 Longword

btS32 Longint

btS64 Int64

btSingle Single

btDouble Double

btExtended Extended

btVariant Variant

btString String

btWideString WideString

btChar Char

btWideChar WideChar

The component version of Pascal Script also supports execution of scripted functions. This works by using theExecuteFunction method. Pascal Script组件同样也⽀持脚本函数。这通过ExecuteFunction来调⽤。

ShowMessage(eFunction([1234.5678, 4321,

'test'],

'TestFunction'));

This will execute the function called 'TestFunction' with 3 parameters, a float, an integer and a string. The result will bepassed back to ShowMessage.

这个例⼦将执⾏⼀个名为TestFunction的函数,这个函数包含3个参数,⼀个float,⼀个integer和⼀个string。函数的返回值传回给ShowMessage。

Notes:

For some functions and constants, it might be necessary to add: , and/or toyour uses list.

The script engine never calls sMessages by itself, so your application might hang, while the script isrunning. To avoid this, you can add sMessages to the event.

It's possible to import your own classes in the script engine. Pascal Script includes a tool to create import libraries in the/Unit-Importing/ directory.

It's possible to import your own classes in the script engine. Pascal Script includes a tool to create import libraries in the Bindirectory.

For examples on how to use the compiler and runtime separately, see the Import and Kylix samples.

The Debug requires SynEdit /.

注意:

⼀些必要的函数和常量应该被加⼊到uses 列表中:, , ;

脚本引擎不会⾃⾏调⽤sMessages,因此在脚本执⾏时你的应⽤程序可能会终⽌。要避免这⼀点,你可以将sMessages加⼊到TPSScript的OnLine事件中;

可能需要在脚本中引⼊你⾃⼰的类,Pascal Script包含⼀个⼯具以便创建引⼊的库,这个⼯具在Unit-Importing⽬录中;

可能需要在脚本中引⼊你⾃⼰的类,Pascal Script包含⼀个⼯具以便创建引⼊的库,这个⼯具在Bin⽬录中;

安装⽬录中可以找到单独使⽤Comiler和Runtim的实例;

脚本调试需要SynEdit /.

--------------------