Componentes.Terceros.RemObj.../internal/5.0.23.613/1/Pascal Script for Delphi/Samples/Console/sample2.dpr
david 2824855ea7 - Modificación del paquete RemObjects_Core_D10 para que sea un paquete de runtime/designtime (antes era designtime sólo)
- Recompilación en Delphi10 de todos los paquetes de RO para generar las DCU's en Lib\D10
- Recompilación en Delphi10 de todos los paquetes de DA para generar las DCU's en Lib\D10

git-svn-id: https://192.168.0.254/svn/Componentes.Terceros.RemObjects@9 b6239004-a887-0f4b-9937-50029ccdca16
2007-09-10 14:06:19 +00:00

81 lines
2.7 KiB
ObjectPascal

program sample2;
uses
uPSCompiler,
uPSRuntime,
Dialogs
;
procedure MyOwnFunction(const Data: string);
begin
// Do something with Data
ShowMessage(Data);
end;
function ScriptOnUses(Sender: TPSPascalCompiler; const Name: string): Boolean;
{ the OnUses callback function is called for each "uses" in the script.
It's always called with the parameter 'SYSTEM' at the top of the script.
For example: uses ii1, ii2;
This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
}
begin
if Name = 'SYSTEM' then begin
Sender.AddDelphiFunction('procedure MyOwnFunction(Data: string)');
{ This will register the function to the script engine. Now it can be used from
within the script.}
Result := True;
end else
Result := False;
end;
procedure ExecuteScript(const Script: string);
var
Compiler: TPSPascalCompiler;
{ TPSPascalCompiler is the compiler part of the script engine. This will
translate a Pascal script into compiled data for the executer. }
Exec: TPSExec;
{ TPSExec is the executer part of the script engine. It uses the output of
the compiler to run a script. }
Data: string;
begin
Compiler := TPSPascalCompiler.Create; // create an instance of the compiler.
Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
if not Compiler.Compile(Script) then begin // Compile the Pascal script into bytecode.
Compiler.Free;
// You could raise an exception here.
Exit;
end;
Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
Compiler.Free; // After compiling the script, there is no further need for the compiler.
Exec := TPSExec.Create; // Create an instance of the executer.
Exec.RegisterDelphiFunction(@MyOwnFunction, 'MYOWNFUNCTION', cdRegister);
{ This will register the function to the executer. The first parameter is the executer. The second parameter is a
pointer to the function. The third parameter is the name of the function (in uppercase). And the last parameter is the
calling convention (usually Register). }
if not Exec.LoadData(Data) then begin // Load the data from the Data string.
{ For some reason the script could not be loaded. This is usually the case when a
library that has been used at compile time isn't registered at runtime. }
Exec.Free;
// You could raise an exception here.
Exit;
end;
Exec.RunScript; // Run the script.
Exec.Free; // Free the executer.
end;
const
Script = 'var s: string; begin s := ''Test''; S := s + ''ing;''; MyOwnFunction(s); end.';
begin
ExecuteScript(Script);
end.