home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d456 / CAJSCRPT.ZIP / ifps3 / IFPS3CompExec.pas < prev    next >
Pascal/Delphi Source File  |  2002-07-21  |  12KB  |  448 lines

  1. unit IFPS3CompExec;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Classes, ifps3, ifps3debug, ifps3utl,
  7.   ifps3common, ifpscomp, ifpidelphi, ifpidelphiruntime, ifpidll2,
  8.   ifpidll2runtime, ifpiclass, ifpiclassruntime;
  9.  
  10.  
  11. type
  12.   TDelphiCallingConvention = ifpidelphiruntime.TDelphiCallingConvention;
  13.   TIFPSCompileTimeClassesImporter = ifpiclass.TIFPSCompileTimeClassesImporter;
  14.   TIFPSRuntimeClassImporter = ifpiclassruntime.TIFPSRuntimeClassImporter;
  15. const
  16.   cdRegister = ifpidelphiruntime.cdRegister;
  17.   cdPascal = ifpidelphiruntime.cdPascal;
  18.   CdCdecl = ifpidelphiruntime.CdCdecl;
  19.   CdStdCall = ifpidelphiruntime.CdStdCall;
  20. type
  21.   TIFPS3CompExec = class;
  22.   TIFPS3Plugin = class(TComponent)
  23.   private
  24.     FCompExec: TIFPS3CompExec;
  25.   protected
  26.     procedure SetCompiler(Value: TIFPS3CompExec); virtual;
  27.     procedure CompOnUses; virtual; abstract;
  28.     procedure ExecOnUses; virtual; abstract;
  29.   public
  30.     destructor Destroy; override;
  31.   published
  32.     property CompExec: TIFPS3CompExec read FCompExec write SetCompiler;
  33.   end;
  34.   TIFPS3DllPlugin = class(TIFPS3Plugin)
  35.   protected
  36.     procedure CompOnUses; override;
  37.     procedure ExecOnUses; override;
  38.   end;
  39.   TIFPS3ClOnCompImport = procedure (Sender: TObject; x: TIFPSCompileTimeClassesImporter) of object;
  40.   TIFPS3ClOnExecImport = procedure (Sender: TObject; x: TIFPSRuntimeClassImporter) of object;
  41.   TIFPS3ClassesPlugin = class(TIFPS3Plugin)
  42.   private
  43.     FOnCompImport: TIFPS3ClOnCompImport;
  44.     FOnExecImport: TIFPS3ClOnExecImport;
  45.     RI: TIFPSRuntimeClassImporter;
  46.   protected
  47.     procedure CompOnUses; override;
  48.     procedure ExecOnUses; override;
  49.   public
  50.     destructor Destroy; override;
  51.     function SetVarToInstance(const VarName: string; cl: TObject): Boolean;
  52.   published
  53.     property OnCompImport: TIFPS3ClOnCompImport read FOnCompImport write FOnCompImport;
  54.     property OnExecImport: TIFPS3ClOnExecImport read FOnExecImport write FOnExecImport;
  55.   end;
  56.  
  57.  
  58.   TIFPS3CompOptions = set of (icAllowNoBegin, icAllowUnit, icAllowNoEnd);
  59.   TIFPS3CompExecEvent = procedure (Sender: TIFPS3CompExec) of object;
  60.   TIFPS3CompExec = class(TComponent)
  61.   private
  62.     FCanAdd: Boolean;
  63.     FComp: TIFPSPascalCompiler;
  64.     FCompOptions: TIFPS3CompOptions;
  65.     FExec: TIFPSDebugExec;
  66.     FScript: TStrings;
  67.     FPlugins: TList;
  68.     FOnLine: TNotifyEvent;
  69.     FUseDebugInfo: Boolean;
  70.     FOnAfterExecute, FOnCompile, FOnExecute: TIFPS3CompExecEvent;
  71.     procedure SetScript(const Value: TStrings);
  72.     function GetCompMsg(i: Integer): PIFPSPascalCompilerMessage;
  73.     function GetCompMsgCount: Longint;
  74.     function GetAbout: string;
  75.     function ScriptUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  76.     procedure LoadExec;
  77.     function GetExecErrorByteCodePosition: Cardinal;
  78.     function GetExecErrorCode: TIFError;
  79.     function GetExecErrorParam: string;
  80.     function GetExecErrorProcNo: Cardinal;
  81.     function GetExecErrorString: string;
  82.     function GetExecErrorPosition: Cardinal;
  83.   public
  84.     constructor Create(AOwner: TComponent); override;
  85.     destructor Destroy; override;
  86.  
  87.     function Compile: Boolean;
  88.     function Execute: Boolean;
  89.  
  90.     procedure GetPosXY(const Position: Longint; var X, Y: Longint);
  91.  
  92.     property Comp: TIFPSPascalCompiler read FComp;
  93.     property Exec: TIFPSDebugExec read FExec;
  94.  
  95.     property CompilerMessageCount: Longint read GetCompMsgCount;
  96.     property CompilerMessages[i: Longint]: PIFPSPascalCompilerMessage read GetCompMsg;
  97.  
  98.     function CompilerErrorToStr(I: Longint): string;
  99.  
  100.     property ExecErrorProcNo: Cardinal read GetExecErrorProcNo;
  101.     property ExecErrorByteCodePosition: Cardinal read GetExecErrorByteCodePosition;
  102.     property ExecErrorPosition: Cardinal read GetExecErrorPosition;
  103.  
  104.     property ExecErrorCode: TIFError read GetExecErrorCode;
  105.     property ExecErrorParam: string read GetExecErrorParam;
  106.     property ExecErrorToString: string read GetExecErrorString;
  107.  
  108.     function AddFunctionEx(Ptr: Pointer; const Decl: string; CallingConv: TDelphiCallingConvention): Boolean;
  109.     function AddFunction(Ptr: Pointer; const Decl: string): Boolean;
  110.  
  111.     function AddRegisteredVariable(const VarName, VarType: string): Boolean;
  112.     function GetVariable(const Name: string): PIFVariant; 
  113.   published
  114.     property About: string read GetAbout;
  115.     property Script: TStrings read FScript write SetScript;
  116.     property CompilerOptions: TIFPS3CompOptions read FCompOptions write FCompOptions;
  117.     property OnLine: TNotifyEvent read FOnLine write FOnLine;
  118.     property OnCompile: TIFPS3CompExecEvent read FOnCompile write FOnCompile;
  119.     property OnExecute: TIFPS3CompExecEvent read FOnExecute write FOnExecute;
  120.     property OnAfterExecute: TIFPS3CompExecEvent read FOnAfterExecute write FOnAfterExecute;
  121.     property UseDebugInfo: Boolean read FUseDebugInfo write FUseDebugInfo default True;
  122.   end;
  123.  
  124. procedure Register;
  125.  
  126. implementation
  127. uses
  128.   ifps3lib_std, ifps3lib_stdr;
  129.  
  130. procedure Register;
  131. begin
  132.   RegisterComponents('Innerfuse', [TIFPS3CompExec, TIFPS3DllPlugin, TIFPS3ClassesPlugin]);
  133. end;
  134.  
  135. function CompScriptUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  136. begin
  137.   Result := TIFPS3CompExec(Sender.ID).ScriptUses(Sender, Name);
  138. end;
  139.  
  140. procedure ExecOnLine(Sender: TIFPSExec);
  141. begin
  142.   if assigned(TIFPS3CompExec(Sender.ID).FOnLine) then
  143.   begin
  144.     TIFPS3CompExec(Sender.ID).FOnLine(Sender.Id);
  145.   end;
  146. end;
  147. { TIFPS3CompilerPlugin }
  148.  
  149. destructor TIFPS3Plugin.Destroy;
  150. begin
  151.   SetCompiler(nil);
  152.   inherited Destroy;
  153. end;
  154.  
  155. procedure TIFPS3Plugin.SetCompiler(Value: TIFPS3CompExec);
  156. begin
  157.   if FCompExec <> Value then
  158.   begin
  159.     if FCompExec <> nil then
  160.     begin
  161.       FCompExec.FPlugins.Remove(Self);
  162.     end;
  163.     FCompExec := Value;
  164.     if FCompExec <> nil then
  165.     begin
  166.       FCompExec.FPlugins.Add(Self);
  167.     end;
  168.   end;
  169. end;
  170.  
  171. { TIFPS3CompExec }
  172.  
  173. function TIFPS3CompExec.AddFunction(Ptr: Pointer;
  174.   const Decl: string): Boolean;
  175. begin
  176.   Result := AddFunctionEx(Ptr, Decl, cdRegister);
  177. end;
  178.  
  179. function TIFPS3CompExec.AddFunctionEx(Ptr: Pointer; const Decl: string;
  180.   CallingConv: TDelphiCallingConvention): Boolean;
  181. var
  182.   P: PIFPSRegProc;
  183. begin
  184.   if not FCanAdd then begin Result := False; exit; end;
  185.   p := RegisterDelphiFunctionC2(Comp, Decl);
  186.   if p <> nil then
  187.   begin
  188.     RegisterDelphiFunctionR(Exec, Ptr, p^.Name, CallingConv);
  189.     Result := True;
  190.   end else Result := False;
  191. end;
  192.  
  193. function TIFPS3CompExec.AddRegisteredVariable(const VarName,
  194.   VarType: string): Boolean;
  195. var
  196.   FVar: PIFPSVar;
  197. begin
  198.   if not FCanAdd then begin Result := False; exit; end;
  199.   FVar := FComp.AddUsedVariableN(varname, vartype);
  200.   if fvar = nil then
  201.     result := False
  202.   else begin
  203.     fvar^.exportname := fvar^.Name;
  204.     Result := True;
  205.   end;
  206. end;
  207.  
  208. function TIFPS3CompExec.Compile: Boolean;
  209. var
  210.   i: Longint;
  211. begin
  212.   FExec.Clear;
  213.   FExec.ClearFunctionList;
  214.   RegisterStandardLibrary_R(Exec);
  215.   for i := 0 to FPlugins.Count -1 do
  216.   begin
  217.     TIFPS3Plugin(FPlugins[i]).ExecOnUses;
  218.   end;
  219.   FCanAdd := True;
  220.   FComp.AllowNoBegin := icAllowNoBegin in FCompOptions;
  221.   FComp.AllowUnit := icAllowUnit in FCompOptions;
  222.   FComp.AllowNoEnd := icAllowNoEnd in FCompOptions;
  223.   if FComp.Compile(FScript.Text) then
  224.   begin
  225.     FCanAdd := False;
  226.     LoadExec;
  227.     Result := True;
  228.   end else Result := False;
  229. end;
  230.  
  231. function TIFPS3CompExec.CompilerErrorToStr(I: Integer): string;
  232. begin
  233.   Result := IFPSMessageToString(CompilerMessages[i]); 
  234. end;
  235.  
  236. constructor TIFPS3CompExec.Create(AOwner: TComponent);
  237. begin
  238.   inherited Create(AOwner);
  239.   FComp := TIFPSPascalCompiler.Create;
  240.   FExec := TIFPSDebugExec.Create;
  241.   FScript := TStringList.Create;
  242.   FPlugins := TList.Create;
  243.  
  244.   FComp.ID := Self;
  245.   FComp.OnUses := CompScriptUses;
  246.   FExec.Id := Self;
  247.   FExec.OnRunLine:= ExecOnLine;
  248.   
  249.   FUseDebugInfo := True;
  250. end;
  251.  
  252. destructor TIFPS3CompExec.Destroy;
  253. var
  254.   i: Longint;
  255. begin
  256.   for i := FPlugins.count -1 downto 0 do
  257.   begin
  258.     TIFPS3Plugin(FPlugins[i]).SetCompiler(nil);
  259.   end;
  260.   FPlugins.Free;
  261.   FScript.Free;
  262.   FExec.Free;
  263.   FComp.Free;
  264.   inherited Destroy;
  265. end;
  266.  
  267. function TIFPS3CompExec.Execute: Boolean;
  268. begin
  269.   if @FOnExecute <> nil then
  270.     FOnExecute(Self);
  271.   Result := FExec.RunScript and (FExec.ExceptionCode = erNoError) ;
  272.   if @FOnAfterExecute <> nil then
  273.     FOnAfterExecute(Self);
  274. end;
  275.  
  276. function TIFPS3CompExec.GetAbout: string;
  277. begin
  278.   Result := TIFPSExec.About;
  279. end;
  280.  
  281. function TIFPS3CompExec.GetCompMsg(i: Integer): PIFPSPascalCompilerMessage;
  282. begin
  283.   Result := FComp.Msg[i];
  284. end;
  285.  
  286. function TIFPS3CompExec.GetCompMsgCount: Longint;
  287. begin
  288.   Result := FComp.MsgCount;
  289. end;
  290.  
  291. function TIFPS3CompExec.GetExecErrorByteCodePosition: Cardinal;
  292. begin
  293.   Result := Exec.ExceptionPos;
  294. end;
  295.  
  296. function TIFPS3CompExec.GetExecErrorCode: TIFError;
  297. begin
  298.   Result := Exec.ExceptionCode;
  299. end;
  300.  
  301. function TIFPS3CompExec.GetExecErrorParam: string;
  302. begin
  303.   Result := Exec.ExceptionString;
  304. end;
  305.  
  306. function TIFPS3CompExec.GetExecErrorPosition: Cardinal;
  307. begin
  308.   Result := FExec.TranslatePosition(Exec.ExceptionProcNo, Exec.ExceptionPos);
  309. end;
  310.  
  311. function TIFPS3CompExec.GetExecErrorProcNo: Cardinal;
  312. begin
  313.   Result := Exec.ExceptionProcNo;
  314. end;
  315.  
  316. function TIFPS3CompExec.GetExecErrorString: string;
  317. begin
  318.   Result := TIFErrorToString(Exec.ExceptionCode, Exec.ExceptionString);
  319. end;
  320.  
  321. procedure TIFPS3CompExec.GetPosXY(const Position: Integer; var X,
  322.   Y: Integer);
  323. var
  324.   s: string;
  325.   Pos, I: Longint;
  326. begin
  327.   s := FScript.Text;
  328.   if Position > Length(s) then Pos := Length(s) else Pos := Position;
  329.   x := 1;
  330.   y := 1;
  331.   i := 1;
  332.   while i <= Pos do
  333.   begin
  334.     if (s[i] = #13) or (s[i] = #10) then
  335.     begin
  336.       if ((s[i+1] = #13) or (s[i+1] = #10)) and (s[i] <> s[i+1]) then
  337.       inc(i);
  338.       x := 1;
  339.       inc(y);
  340.     end else inc(x);
  341.     inc(i);
  342.   end;
  343. end;
  344.  
  345. function TIFPS3CompExec.GetVariable(const Name: string): PIFVariant;
  346. begin
  347.   Result := FExec.GetVar2(name);
  348. end;
  349.  
  350. procedure TIFPS3CompExec.LoadExec;
  351. var
  352.   s: string;
  353. begin
  354.   FComp.GetOutput(s);
  355.   FExec.LoadData(s);
  356.   if FUseDebugInfo then
  357.   begin
  358.     FComp.GetDebugOutput(s);
  359.     FExec.LoadDebugData(s);
  360.   end;
  361. end;
  362.  
  363. function TIFPS3CompExec.ScriptUses(Sender: TIFPSPascalCompiler;
  364.   const Name: string): Boolean;
  365. var
  366.   i: Longint;
  367. begin
  368.   if Name = 'SYSTEM' then
  369.   begin
  370.     RegisterStandardLibrary_C(Comp);
  371.     for i := 0 to FPlugins.Count -1 do
  372.     begin
  373.       TIFPS3Plugin(FPlugins[i]).CompOnUses;
  374.     end;
  375.     if assigned(FOnCompile) then
  376.       FOnCompile(Self);
  377.     Result := True;
  378.   end else begin
  379.     Sender.MakeError('', ecUnknownIdentifier, Name);
  380.     Result := False;
  381.   end;
  382. end;
  383.  
  384. procedure TIFPS3CompExec.SetScript(const Value: TStrings);
  385. begin
  386.   FScript.Assign(Value);
  387. end;
  388.  
  389.  
  390. { TIFPS3DllPlugin }
  391.  
  392. procedure TIFPS3DllPlugin.CompOnUses;
  393. begin
  394.   CompExec.Comp.OnExternalProc := DllExternalProc;
  395. end;
  396.  
  397. procedure TIFPS3DllPlugin.ExecOnUses;
  398. begin
  399.   RegisterDLLRuntime(CompExec.Exec);
  400. end;
  401.  
  402. { TIFPS3ClassesPlugin }
  403.  
  404. procedure TIFPS3ClassesPlugin.CompOnUses;
  405. var
  406.   x: TIFPSCompileTimeClassesImporter;
  407. begin
  408.   x := TIFPSCompileTimeClassesImporter.Create(FCompExec.Comp, True);
  409.   if assigned(FOnCompImport) then
  410.     FOnCompImport(Self, x);
  411. end;
  412.  
  413. destructor TIFPS3ClassesPlugin.Destroy;
  414. begin
  415.   RI.Free;
  416.   inherited Destroy;
  417. end;
  418.  
  419. procedure TIFPS3ClassesPlugin.ExecOnUses;
  420. begin
  421.   if ri <> nil then
  422.   begin
  423.     RI.Free;
  424.     RI := nil;
  425.   end;
  426.   RI := TIFPSRuntimeClassImporter.Create;
  427.   if assigned(FOnExecImport) then
  428.     FOnExecImport(Self, RI);
  429.   RegisterClassLibraryRuntime(FCompExec.Exec, RI);
  430. end;
  431.  
  432. function TIFPS3ClassesPlugin.SetVarToInstance(const VarName: string; cl: TObject): Boolean;
  433. var
  434.   p: PIFVariant;
  435. begin
  436.   if FCompExec = nil then begin Result := False; exit; end;
  437.   p := FCompExec.GetVariable(VarName);
  438.   if p <> nil then
  439.   begin
  440.     SetVariantToClass(p, cl);
  441.     result := true;
  442.   end else result := false;
  443. end;
  444.  
  445.  
  446.  
  447. end.
  448.