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

  1. program sample5;
  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. 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, 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.     AddImportedClassVariable(Sender, 'Application', 'TApplication');
  60.     // Registers the application variable to the script engine.
  61.  
  62.     Result := True;
  63.   end else
  64.     Result := False;
  65. end;
  66.  
  67. procedure ExecuteScript(const Script: string);
  68. var
  69.   Compiler: TIFPSPascalCompiler;
  70.   { TIFPSPascalCompiler is the compiler part of the scriptengine. This will 
  71.     translate a Pascal script into a compiled for the executer understands. } 
  72.   Exec: TIFPSExec;
  73.    { TIFPSExec is the executer part of the scriptengine. It uses the output of
  74.     the compiler to run a script. }
  75.   Data: string;
  76.   CI: TIFPSRuntimeClassImporter; // Can be found in the ifpiclassruntime.pas unit.
  77. begin
  78.   Compiler := TIFPSPascalCompiler.Create; // create an instance of the compiler.
  79.   Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  80.   if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  81.   begin
  82.     Compiler.Free;
  83.      // You could raise an exception here.
  84.     Exit;
  85.   end;
  86.  
  87.   Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  88.   Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
  89.  
  90.   CI := TIFPSRuntimeClassImporter.Create;
  91.   { Create an instance of the runtime class importer.}
  92.   
  93.   RIRegister_Std(CI);  // ifpiir_std.pas unit.
  94.   RIRegister_Controls(CI); // ifpiir_controls.pas unti.
  95.   RIRegister_stdctrls(CI);  // ifpiir_stdctrls.pas unit.
  96.   RIRegister_Forms(CI);  // ifpiir_forms.pas unit.
  97.  
  98.   Exec := TIFPSExec.Create;  // Create an instance of the executer.
  99.   RegisterStandardLibrary_R(Exec);
  100.  
  101.   RegisterClassLibraryRuntime(Exec, CI);
  102.   // Assign the runtime class importer to the executer.
  103.   
  104.   { The functions registered at compile time also need to be registered at runtime. These
  105.     functions can be found in the ifps3lib_stdr.pas unit. }
  106.   if not  Exec.LoadData(Data) then // Load the data from the Data string.
  107.   begin
  108.     { For some reason the script could not be loaded. This is usually the case when a 
  109.       library that has been used at compile time isn't registered at runtime. }
  110.     Exec.Free;
  111.      // You could raise an exception here.
  112.     Exit;
  113.   end;
  114.  
  115.   SetVariantToClass(Exec.GetVarNo(Exec.GetVar('APPLICATION')), Application);
  116.    // This will set the script's Application variable to the real Application variable.
  117.  
  118.   Exec.RunScript; // Run the script.
  119.   Exec.Free; // Free the executer.
  120.   CI.Free;  // Free the runtime class importer. 
  121. end;
  122.  
  123.  
  124.  
  125.  
  126. const
  127.   Script =
  128.     'var f: TForm; i: Longint; begin f := TForm.CreateNew(f, 0); f.Show; while f.Visible do Application.ProcessMessages; F.free;  end.';
  129.  
  130. begin
  131.   ExecuteScript(Script);
  132. end.
  133.