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

  1. program sample6;
  2.  
  3. uses
  4.   ifpscomp,
  5.   ifps3common,
  6.   ifps3,
  7.   ifps3utl,
  8.   ifps3lib_std,
  9.   ifps3lib_stdr,
  10.   ifpidelphi,
  11.   ifpidelphiruntime,
  12.  
  13.   Dialogs
  14.  
  15.   ;
  16.  
  17. procedure MyOwnFunction(const Data: string);
  18. begin
  19.   // Do something with Data
  20.   ShowMessage(Data);
  21. end;
  22.  
  23. function ScriptOnExportCheck(Sender: TIFPSPascalCompiler; Proc: PIFPSProcedure; const ProcDecl: string): Boolean;
  24. {
  25.   The OnExportCheck callback function is called for each function in the script
  26.   (Also for the main proc, with '!MAIN' as a Proc^.Name). ProcDecl contains the
  27.   result type and parameter types of a function using this format:
  28.   ProcDecl: ResultType + ' ' + Parameter1 + ' ' + Parameter2 + ' '+Parameter3 + .....
  29.   Parameter: ParameterType+TypeName
  30.   ParameterType is @ for a normal parameter and ! for a var parameter.
  31.   A result type of 0 means no result.
  32. }
  33. begin
  34.   if Proc^.Name = 'TEST' then // Check if the proc is the Test proc we want.
  35.   begin
  36.     if ProcDecl <> '0 @STRING' then // Check if the proc has the correct params.
  37.     begin
  38.       { Something is wrong, so cause an error at the declaration position of the proc. }
  39.       Sender.MakeError('', ecTypeMismatch, '')^.Position := Proc^.DeclarePosition;
  40.       Result := False;
  41.       Exit;
  42.     end;
  43.     Proc^.FExport := 1;
  44.     { Export the proc; This is needed because IFPS doesn't store the name of a
  45.       function by default }
  46.     Result := True;
  47.   end else Result := True;
  48. end;
  49.  
  50. function ScriptOnUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  51. { the OnUses callback function is called for each "uses" in the script.
  52.   It's always called with the parameter 'SYSTEM' at the top of the script. 
  53.   For example: uses ii1, ii2;   
  54.   This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
  55. }
  56. begin
  57.   if Name = 'SYSTEM' then
  58.   begin
  59.     RegisterStandardLibrary_C(Sender);
  60.     { Register the standard library. The standard library is in the 
  61.       ifps3lib_std.pas unit. This will register all standard functions like 
  62.       Copy, Pos, Length.
  63.     }
  64.  
  65.     RegisterDelphiFunctionC(Sender, 'procedure MyOwnFunction(Data: string)');
  66.     { This will register the function to the script engine. Now it can be used from within the script. When adding
  67.       functions always leave the const out, this is not yet supported but doesn't make a change. This function can be
  68.       found in the ifpidelphi.pas file. }
  69.  
  70.     Result := True;
  71.   end else
  72.     Result := False;
  73. end;
  74.  
  75. procedure ExecuteScript(const Script: string);
  76. var
  77.   Compiler: TIFPSPascalCompiler;
  78.   { TIFPSPascalCompiler is the compiler part of the scriptengine. This will 
  79.     translate a Pascal script into a compiled for the executer understands. } 
  80.   Exec: TIFPSExec;
  81.    { TIFPSExec is the executer part of the scriptengine. It uses the output of
  82.     the compiler to run a script. }
  83.   Data: string;
  84.  
  85.   N: PIfVariant;
  86.   { The variant in which we are going to store the parameter }
  87.   ParamList: TIfList;
  88.   { The parameter list}
  89. begin
  90.   Compiler := TIFPSPascalCompiler.Create; // create an instance of the compiler.
  91.   Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  92.  
  93.   Compiler.OnExportCheck := ScriptOnExportCheck; // Assign the onExportCheck event.
  94.  
  95.   if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  96.   begin
  97.     Compiler.Free;
  98.      // You could raise an exception here.
  99.     Exit;
  100.   end;
  101.  
  102.   Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  103.   Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
  104.  
  105.   Exec := TIFPSExec.Create;  // Create an instance of the executer.
  106.   RegisterStandardLibrary_R(Exec);
  107.   { The functions registered at compile time also need to be registered at runtime. These
  108.     functions can be found in the ifps3lib_stdr.pas unit. }
  109.  
  110.   RegisterDelphiFunctionR(Exec, @MyOwnFunction, 'MYOWNFUNCTION', cdRegister);
  111.   { This will register the function to the executer. The first parameter is the executer. The second parameter is a 
  112.     pointer to the function. The third parameter is the name of the function (in uppercase). And the last parameter is the
  113.     calling convention (usually Register). This function can be found in the ifpidelphiruntime.pas file. }
  114.  
  115.   if not Exec.LoadData(Data) then // Load the data from the Data string.
  116.   begin
  117.     { For some reason the script could not be loaded. This is usually the case when a 
  118.       library that has been used at compile time isn't registered at runtime. }
  119.     Exec.Free;
  120.      // You could raise an exception here.
  121.     Exit;
  122.   end;
  123.  
  124.   ParamList := TIfList.Create; // Create the parameter list
  125.  
  126.   N := CreateVariant(Exec.MemoryManager, Exec.FindType2(btString));
  127.   { Create a variant for the string parameter }
  128.   if n = nil then
  129.   begin
  130.     { Something is wrong. Exit here }
  131.     ParamList.Free;
  132.     Exec.Free;
  133.     Exit;
  134.   end;
  135.  
  136.   tbtstring(n^.tstring) := 'Test Parameter!';
  137.   // Put something in the string parameter.
  138.  
  139.   ParamList.Add(n); // Add it to the parameter list.
  140.  
  141.   Exec.RunProc(ParamList, Exec.GetProc('TEST'));
  142.   { This will call the test proc that was exported before }
  143.  
  144.   FreePIFVariantList(Exec.MemoryManager, ParamList); // Cleanup the parameters (This will also free N)
  145.  
  146.   Exec.Free; // Free the executer.
  147. end;
  148.  
  149.  
  150.  
  151. const
  152.   Script = 'procedure test(s: string); begin MyOwnFunction(''Test is called: ''+s);end; begin end.';
  153.  
  154. begin
  155.   ExecuteScript(Script);
  156. end.
  157.