Using RemObjects Pascal Script

Source: Internet
Author: User

Http://www.cnblogs.com/MaxWoods/p/3304954.html

Excerpt from RemObjects Wiki

This article provides a general overview of RemObjects Pascal script and shows how to create some simple scripts.

The Pascal script consists of two different parts:

    • Compiler (UPSCOMPILER.PAS)
    • Run-time (Upsruntime.pas)

The two parts are independent of each other. They can be used separately or through the Tpsscript control, which is defined in the Upscomponent.pas unit and is easily encapsulated in these two parts.

To use the control version of Pascal Script, first place the control on the form or data module, and set the Script property to invoke the compile and execute methods. Compile errors, warnings, The hint can be obtained in the Compilermessages array property, and the same run-time error is stored in the Execerrortostring property.

The following example compiles and executes an empty script ("Begin End."):

Var

messages:string;

Compiled:boolean;

Begin

Ce. Script.text: = ' begin end. ';

Compiled: = Ce.compile;

For I: = 0 to Ce.compilermessagecount-1 do

Messages: = Messages +

Ce.compilermessages[i]. Messagetostring +

#13 # #;

If Compiled Then

Messages: = Messages + ' succesfully compiled ' #13 # #;

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

If Compiled then BEGIN

If Ce.execute Then

ShowMessage (' succesfully Executed ')

Else

ShowMessage (' Error while executing script: ' +

ce.execerrortostring);

End

End

By default, the control adds only a few standard functions to the scripting engine (specific functions can be obtained from the top of the Upscomponents.pas cell).

In addition to the standard functions, Pascal script contains several library functions:

Tpsdllplugin

Allows the script to use the exported function in the DLL, syntax:
function FindWindow (C1, C2:pchar): Longint; external' [email protected] stdcall ';

Tpsimport_classes

Import TObject and classes units.

Tpsimport_dateutils

Import Date/time related functions.

Tpsimport_comobj

COM objects can be used in scripts.

tpsimport_db

Import Db.pas.

Tpsimport_forms

Import forms and menus units.

Tpsimport_controls

Import Controls.pas and Graphics.pas units.

Tpsimport_stdctrls

Import Extctrls and buttons.

To use these libraries, add the appropriate controls to the form or data module, and select the Tpscompiler control after clicking the Plugins property [...] button to add a new item and set its Plugin property to a specific plug-in control. In addition to these standard library functions, you can easily add new functions to the scripting engine. For this purpose, first create the functions to be exported to the scripting engine, for example:

Procedure Tform1.shownewmessage (const message:string);

Begin

ShowMessage (' shownewmessage invoked: ' #13 #10+message);

End

Then, implement the Oncompile event for the Tpscompile control, and use the Addmethod method to register the actual method:

Procedure Tform1.cecompile (Sender:tpsscript);

Begin

Sender.addmethod (self, @TForm1. Shownewmessage,

' Procedure Shownewmessage

(const message:string);

End

How to invoke in a script:

Begin

Shownewmessage (' Show this! ');

End.

Advanced Features

The Pascal script supports precompilation, you can use {$IFDEF}, {$ELSE}, {$ENDIF} directives, and you can use the {$I filename.inc} directive to introduce additional file content into the script. In order to use this feature, The Usepreprocessor property must be set to true, and the Mainfilename property must match the script name in the Scripts property. The Defines property specifies predefined directives that are processed in the Onneedfile event to introduce additional files.

function Tform1.ceneedfile (sender:tobject;

Const orginfilename:string;

var FileName, output:string): Boolean;

Var

path:string;

F:tfilestream;

Begin

Path: = Extractfilepath (paramstr (0)) + FileName;

Try

F: = Tfilestream.create (Path, fmopenread or fmsharedenywrite);

Except

Result: = false;

Exit

End

Try

SetLength (Output, f.size);

F.read (Output[1], Length (Output));

Finally

F.free;

End

Result: = True;

End

When these properties are set, the Compilermessages array property outputs the name of the containing file.

In addition, you can invoke functions in the script in Delphi. The following code is defined in the script:

function TestFunction (param1:double; data:string): Longint;

Begin

Shownewmessage (' Param1: ' +floattostring (param1)

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

Result: = 1234567;

End

Begin

End.

Before you can use a function in a script, you must check the function parameters and return value types, which are available in the Onverifyproc event.

Procedure Tform1.ceverifyproc (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

The Exportcheck function checks whether the parameters match. In this example, Btu8 is a Boolean (return value type), Btdouble is the first argument, and Btstring is the second argument. [PmIn, PmIn] indicates that two parameters are in parameters. To invoke this script function you also need to create an event declaration for this function.

Type

Ttestfunction = function (param1:double;

data:string): Longint of object;

//...

Var

Meth:ttestfunction;

Meth: = Ttestfunction (CE. Getprocmethod (' testfunction '));

If @Meth = Nil Then

Raise Exception.create (' Unable to call testfunction ');

ShowMessage (' Result: ' +inttostr (Meth (pi, Datetimetostr (now)));

You can also add variables to the scripting engine so that they can be used in scripts. The Addregisteredvariable function implementation can be called in the OnExecute event:

Procedure Tform1.ceexecute (Sender:tpsscript);

Begin

Ce. Setvartoinstance ("Self", self);

^^ ^ for class variables

Vsetint (CE. GetVariable (' MYVAR '), 1234567);

End

After the script has finished executing, read the new value of the variable, which can be called in the Onafterexecute event: Vgetint (CE. GetVariable (' MYVAR ')).

To register an external variable with the scripting engine, there are two steps to add a variable declaration to the script first in the Oncompile event, using the Addregisteredptrvariable function.

Procedure Tmyform.psscriptcompile (Sender:tpsscript);

Begin

Sender.addregisteredptrvariable (' MyClass ', ' TButton ');

Sender.addregisteredptrvariable (' MyVar ', ' longint ');

End

This imports the external variables MyClass and MyVar. Second, associate a variable with a specific pointer in the OnExecute event:

Procedure Tmyform.psscriptexecute (Sender:tpsscript);

Begin

Psscript.setpointertodata (' MyVar ', @MyVar, Psscript.findbasetype (BTS32));

Psscript.setpointertodata (' Memo1 ', @Memo1, Psscript.findnamedtype (' TMemo '));

End

There are two types of variables in the script, the underlying type (the simple type of the table below), and the class type. The underlying type is defined in the Upsutils.pas unit, You can use the Findbasetype function to get the. Class type is obtained by name using Findnamedtype. Modifying a variable in a script directly affects the associated variable.

Base type:

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

A control-based Pascal script can also execute script functions. You need to use the ExecuteFunction method.

ShowMessage (Compexec.executefunction ([1234.5678, 4321,

' Test '],

' TestFunction '));

This will perform functions called ' TestFunction ', with three parameters, a float type, an integer type, and a string type. The return value is passed directly to ShowMessage.

Attention:

    • To use some functions and constants, it is necessary to introduce Upscompiler.pas, Upsruntime.pas, and Upsutils.pas into uses.
    • The scripting engine does not actively invoke application.processmessages, which causes the application to hang when the script runs. To avoid this problem, You can call application.processmessages in the Tpsscript.online event.
    • If you want to import a custom class to the scripting engine, you can use the tools in the/unit-importing/directory to generate the imported class library.
    • If you are importing a custom class to the script script engine, you can use the tools under the Bin directory to generate the Import class library.
    • If you are using compiler and runtime separately, see the import and Kylix examples.
    • The debug paradigm requires a control synedit http://synedit.sourceforge.net.

Retrieved from "Http://wiki.remobjects.com/wiki/Using_RemObjects_Pascal_Script"

Use remobjects Pascal Script (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.