EMBEDDEDSCRIPTENGINE.PAS

unit EmbeddedScriptEngine;

{Implementation of REMObjects Pascal Script Engine www.remobjects.com}


interface

uses
  uPSCompiler,
  uPSRuntime,
  Dialogs;

var
App : variant;   //global TurboCAD application - this is a bit dodgy
//I want to register the TC application object rather than wrapper functions
//Just don't know how at the moement

procedure ExecuteScript(const Script: string);
function  TC_Wrapp_line(x0,y0,z0,x1,y1,z1:double): integer;
function  TC_Wrapp_Text(text:string;x,y,z,height:double): integer;


implementation


function TC_Wrapp_line(x0,y0,z0,x1,y1,z1:double): integer;
begin
 App.Graphics.addlinesingle(x0,y0,z0,x1,y1,z1);
 result :=  1;
end;

function TC_Wrapp_Text(text:string;x,y,z,height:double): integer;
begin
 App.Graphics.AddText(text, x, y, z, height);
 result :=  2;
end;

procedure MyOwnFunction(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
 // showmessage('Onuses called');

  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.}
    Sender.AddDelphiFunction('function TC_Wrapp_line(x0,y0,z0,x1,y1,z1:double): integer');
    Sender.AddDelphiFunction('function TC_Wrapp_Text(text:string;x,y,z,height:double): integer');

    Result := True;
  end else
    Result := False;



end;

procedure ExecuteScript(const Script: string);
var
  Compiler: TPSPascalCompiler;
  { TPSPascalCompiler is the compiler part of the scriptengine. This will 
    translate a Pascal script into a compiled for the executer understands. }
  Exec: TPSExec;
   { TPSExec is the executer part of the scriptengine. It uses the output of
    the compiler to run a script. }
  Data: string;

begin

  // showmessage(Script);
  Compiler := TPSPascalCompiler.Create; // create an instance of the compiler.
  Compiler.OnUses := ScriptOnUses; // assign the OnUses event.

  if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  begin
    Compiler.Free;
     // You could raise an exception here.
     showmessage('error');
    Exit;
  end;

  Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  Compiler.Free; // After compiling the script, there is no need for the compiler anymore.

  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). }

  Exec.RegisterDelphiFunction(@TC_Wrapp_line, 'TC_WRAPP_LINE', cdRegister);
  Exec.RegisterDelphiFunction(@TC_Wrapp_Text, 'TC_WRAPP_TEXT', cdRegister);


  if not  Exec.LoadData(Data) then // Load the data from the Data string.
  begin
    { 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;


end.

Generated by PasToWeb, a tool by Marco Cantù.