home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d456 / CAJSCRPT.ZIP / ifps3 / help / sample2.dpr < prev    next >
Text File  |  2002-06-20  |  3KB  |  99 lines

  1. program sample2;
  2.  
  3. uses
  4.   ifpscomp,
  5.   ifps3,
  6.   ifps3lib_std,
  7.   ifps3lib_stdr,
  8.   ifpidelphi,
  9.   ifpidelphiruntime,
  10.  
  11.   Dialogs
  12.  
  13.   ;
  14.  
  15. procedure MyOwnFunction(const Data: string);
  16. begin
  17.   // Do something with Data
  18.   ShowMessage(Data);
  19. end;
  20.  
  21. function ScriptOnUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  22. { the OnUses callback function is called for each "uses" in the script.
  23.   It's always called with the parameter 'SYSTEM' at the top of the script. 
  24.   For example: uses ii1, ii2;   
  25.   This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
  26. }
  27. begin
  28.   if Name = 'SYSTEM' then
  29.   begin
  30.     RegisterStandardLibrary_C(Sender);
  31.     { Register the standard library. The standard library is in the 
  32.       ifps3lib_std.pas unit. This will register all standard functions like 
  33.       Copy, Pos, Length.
  34.     }
  35.  
  36.     RegisterDelphiFunctionC(Sender, 'procedure MyOwnFunction(Data: string)');
  37.     { This will register the function to the script engine. Now it can be used from within the script. When adding
  38.       functions always leave the const out, this is not yet supported but doesn't make a change. This function can be
  39.       found in the ifpidelphi.pas file. }
  40.  
  41.     Result := True;
  42.   end else
  43.     Result := False;
  44. end;
  45.  
  46. procedure ExecuteScript(const Script: string);
  47. var
  48.   Compiler: TIFPSPascalCompiler;
  49.   { TIFPSPascalCompiler is the compiler part of the scriptengine. This will 
  50.     translate a Pascal script into a compiled for the executer understands. } 
  51.   Exec: TIFPSExec;
  52.    { TIFPSExec is the executer part of the scriptengine. It uses the output of
  53.     the compiler to run a script. }
  54.   Data: string;
  55. begin
  56.   Compiler := TIFPSPascalCompiler.Create; // create an instance of the compiler.
  57.   Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  58.   if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  59.   begin
  60.     Compiler.Free;
  61.      // You could raise an exception here.
  62.     Exit;
  63.   end;
  64.  
  65.   Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  66.   Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
  67.  
  68.   Exec := TIFPSExec.Create;  // Create an instance of the executer.
  69.   RegisterStandardLibrary_R(Exec);
  70.   { The functions registered at compile time also need to be registered at runtime. These
  71.     functions can be found in the ifps3lib_stdr.pas unit. }
  72.  
  73.   RegisterDelphiFunctionR(Exec, @MyOwnFunction, 'MYOWNFUNCTION', cdRegister);
  74.   { This will register the function to the executer. The first parameter is the executer. The second parameter is a 
  75.     pointer to the function. The third parameter is the name of the function (in uppercase). And the last parameter is the
  76.     calling convention (usually Register). This function can be found in the ifpidelphiruntime.pas file. }  
  77.  
  78.   if not  Exec.LoadData(Data) then // Load the data from the Data string.
  79.   begin
  80.     { For some reason the script could not be loaded. This is usually the case when a 
  81.       library that has been used at compile time isn't registered at runtime. }
  82.     Exec.Free;
  83.      // You could raise an exception here.
  84.     Exit;
  85.   end;
  86.  
  87.   Exec.RunScript; // Run the script.
  88.   Exec.Free; // Free the executer.
  89. end;
  90.  
  91.  
  92.  
  93. const
  94.   Script = 'var s: string; begin s := ''Test''; S := s + ''ing;''; MyOwnFunction(s); end.';
  95.  
  96. begin
  97.   ExecuteScript(Script);
  98. end.
  99.