12
procedure MyOwnFunction(const Data: string);
14
// Do something with Data
18
function ScriptOnExportCheck(Sender: TPSPascalCompiler; Proc: TPSInternalProcedure; const ProcDecl: AnsiString): Boolean;
20
function ScriptOnExportCheck(Sender: TPSPascalCompiler; Proc: TPSInternalProcedure; const ProcDecl: string): Boolean;
23
The OnExportCheck callback function is called for each function in the script
24
(Also for the main proc, with '!MAIN' as a Proc^.Name). ProcDecl contains the
25
result type and parameter types of a function using this format:
26
ProcDecl: ResultType + ' ' + Parameter1 + ' ' + Parameter2 + ' '+Parameter3 + .....
27
Parameter: ParameterType+TypeName
28
ParameterType is @ for a normal parameter and ! for a var parameter.
29
A result type of 0 means no result.
32
if Proc.Name = 'TEST' then // Check if the proc is the Test proc we want.
34
if not ExportCheck(Sender, Proc, [0, {$IFDEF UNICODE}btUnicodeString{$ELSE}btString{$ENDIF}], [pmIn]) then // Check if the proc has the correct params.
36
{ Something is wrong, so cause an error at the declaration position of the proc. }
37
Sender.MakeError('', ecTypeMismatch, '');
42
end else Result := True;
46
function ScriptOnUses(Sender: TPSPascalCompiler; const Name: AnsiString): Boolean;
48
function ScriptOnUses(Sender: TPSPascalCompiler; const Name: string): Boolean;
50
{ the OnUses callback function is called for each "uses" in the script.
51
It's always called with the parameter 'SYSTEM' at the top of the script.
52
For example: uses ii1, ii2;
53
This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
56
if Name = 'SYSTEM' then
58
Sender.AddDelphiFunction('procedure MyOwnFunction(Data: string)');
59
{ This will register the function to the script engine. Now it can be used from within the script. }
66
procedure ExecuteScript(const Script: string);
68
Compiler: TPSPascalCompiler;
69
{ TPSPascalCompiler is the compiler part of the scriptengine. This will
70
translate a Pascal script into a compiled form the executer understands. }
72
{ TPSExec is the executer part of the scriptengine. It uses the output of
73
the compiler to run a script. }
74
{$IFDEF UNICODE}Data: AnsiString;{$ELSE}Data: string;{$ENDIF}
77
{ The variant in which we are going to store the parameter }
81
Compiler := TPSPascalCompiler.Create; // create an instance of the compiler.
82
Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
84
Compiler.OnExportCheck := ScriptOnExportCheck; // Assign the onExportCheck event.
86
if not Compiler.Compile(Script) then // Compile the Pascal script into bytecode.
89
// You could raise an exception here.
93
Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
94
Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
96
Exec := TPSExec.Create; // Create an instance of the executer.
98
Exec.RegisterDelphiFunction(@MyOwnFunction, 'MYOWNFUNCTION', cdRegister);
99
{ This will register the function to the executer. The first parameter is the executer. The second parameter is a
100
pointer to the function. The third parameter is the name of the function (in uppercase). And the last parameter is the
101
calling convention (usually Register). }
103
if not Exec.LoadData(Data) then // Load the data from the Data string.
105
{ For some reason the script could not be loaded. This is usually the case when a
106
library that has been used at compile time isn't registered at runtime. }
108
// You could raise an exception here.
112
ParamList := TIfList.Create; // Create the parameter list
114
N := CreateHeapVariant(Exec.FindType2(btString));
115
{ Create a variant for the string parameter }
118
{ Something is wrong. Exit here }
123
VSetString(n, 'Test Parameter!');
124
// Put something in the string parameter.
126
ParamList.Add(n); // Add it to the parameter list.
128
Exec.RunProc(ParamList, Exec.GetProc('TEST'));
129
{ This will call the test proc that was exported before }
131
FreePIFVariantList(ParamList); // Cleanup the parameters (This will also free N)
133
Exec.Free; // Free the executer.
139
Script = 'procedure test(s: string); begin MyOwnFunction(''Test is called: ''+s);end; begin end.';
142
ExecuteScript(Script);