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

  1. program sample4;
  2.  
  3. uses
  4.   ifpscomp,
  5.   ifps3,
  6.   ifps3lib_std,
  7.   ifps3lib_stdr,
  8.   ifpiclass,
  9.   ifpiclassruntime,
  10.   ifpii_std,
  11.   ifpii_controls,
  12.   ifpii_stdctrls,
  13.   ifpii_forms,
  14.   ifpiir_std,
  15.   ifpiir_controls,
  16.   ifpiir_stdctrls,
  17.   ifpiir_forms,
  18.   forms
  19.  
  20.   ;
  21.  
  22. function ScriptOnUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  23. { the OnUses callback function is called for each "uses" in the script. 
  24.   It's always called with the parameter 'SYSTEM' at the top of the script. 
  25.   For example: uses ii1, ii2;   
  26.   This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
  27. }
  28. var
  29.   CI: TIFPSCompileTimeClassesImporter; // can be found in ifpiclass.pas unit.
  30. begin
  31.   if Name = 'SYSTEM' then
  32.   begin
  33.     RegisterStandardLibrary_C(Sender);
  34.     { Register the standard library. The standard library is in the 
  35.       ifps3lib_std.pas unit. This will register all standard functions like 
  36.       Copy, Pos, Length.
  37.     }
  38.  
  39.     CI := TIFPSCompileTimeClassesImporter.Create(Sender, True);
  40.     { Create an instance of the class importer. The first parameter is the script engine. The second parameter is the 
  41.       auto-free parameter, since it's true, there is no need to free the library. }
  42.  
  43.     SIRegister_Std(CI);
  44.     { This will register the declarations of these classes:
  45.       TObject, TPersisent, TComponent. This can be found
  46.       in the ifpii_std.pas unit. }
  47.     SIRegister_Controls(CI);
  48.     { This will register the declarations of these classes:
  49.       TControl, TWinControl, TFont, TStrings, TStringList, TCanvas, TGraphicControl. This can be found
  50.       in the ifpii_controls.pas unit. }
  51.  
  52.     SIRegister_Forms(CI);
  53.     { This will register: TScrollingWinControl, TCustomForm, TForm and TApplication. ifpii_forms.pas unit. }
  54.   
  55.     SIRegister_stdctrls(CI);
  56.      { This will register: TButtonContol, TButton, TCustomCheckbox, TCheckBox, TCustomEdit, TEdit, TCustomMemo, TMemo,
  57.       TCustomLabel and TLabel. Can be found in the ifpii_stdctrls.pas unit. }
  58.  
  59.     Result := True;
  60.   end else
  61.     Result := False;
  62. end;
  63.  
  64. procedure ExecuteScript(const Script: string);
  65. var
  66.   Compiler: TIFPSPascalCompiler;
  67.   { TIFPSPascalCompiler is the compiler part of the scriptengine. This will 
  68.     translate a Pascal script into a compiled for the executer understands. } 
  69.   Exec: TIFPSExec;
  70.    { TIFPSExec is the executer part of the scriptengine. It uses the output of
  71.     the compiler to run a script. }
  72.   Data: string;
  73.   CI: TIFPSRuntimeClassImporter; // Can be found in the ifpiclassruntime.pas unit.
  74. begin
  75.   Compiler := TIFPSPascalCompiler.Create; // create an instance of the compiler.
  76.   Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  77.   if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  78.   begin
  79.     Compiler.Free;
  80.      // You could raise an exception here.
  81.     Exit;
  82.   end;
  83.  
  84.   Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  85.   Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
  86.  
  87.   CI := TIFPSRuntimeClassImporter.Create;
  88.   { Create an instance of the runtime class importer.}
  89.   
  90.   RIRegister_Std(CI);  // ifpiir_std.pas unit.
  91.   RIRegister_stdctrls(CI);  // ifpiir_stdctrls.pas unit.
  92.   RIRegister_Controls(CI); // ifpiir_controls.pas unit.
  93.   RIRegister_Forms(CI);  // ifpiir_forms.pas unit.
  94.  
  95.   Exec := TIFPSExec.Create;  // Create an instance of the executer.
  96.   RegisterStandardLibrary_R(Exec);
  97.  
  98.   RegisterClassLibraryRuntime(Exec, CI);
  99.   // Assign the runtime class importer to the executer.
  100.   
  101.   { The functions registered at compile time also need to be registered at runtime. These
  102.     functions can be found in the ifps3lib_stdr.pas unit. }
  103.   if not  Exec.LoadData(Data) then // Load the data from the Data string.
  104.   begin
  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. }
  107.     Exec.Free;
  108.      // You could raise an exception here.
  109.     Exit;
  110.   end;
  111.  
  112.   Exec.RunScript; // Run the script.
  113.   Exec.Free; // Free the executer.
  114.   CI.Free;  // Free the runtime class importer.
  115. end;
  116.  
  117.  
  118.  
  119. const
  120.   Script =
  121.     'var f: TForm; i: Longint; begin f := TForm.CreateNew(f,0); f.Show; for i := 0 to 1000000 do; f.Hide; f.free;  end.';
  122.  
  123. begin
  124.   ExecuteScript(Script);
  125. end.
  126.