Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi FastScript - dem Script ein Interface hinzufügen (https://www.delphipraxis.net/145324-fastscript-dem-script-ein-interface-hinzufuegen.html)

geskill 29. Dez 2009 22:19


FastScript - dem Script ein Interface hinzufügen
 
Hallo,
kurz vorweg, ich bin gerade am Testen dieser Komponente, eigentlich gefällt sie mir ganz gut, da der Hersteller auch ein Rabatt für seine Komponenten übers neue Jahr gibt, wäre es ein guter Kaufgrund.

Leider, aber auch verständlich da gerade Feiertage, sind ist in dem Support Forum nicht viel los.

Mein Problem besteht darin, dass ich im Programm einige Interfaces habe, die ich auch direkt dem User des Scripts zur Verfügung stellen möchte.

Aus der Dokumentation konnte ich hier entnehmen:
Zitat:

Adding an object to the script
To add an object to a script, call the TfsScript.AddObject method. The first parameter
is the name of the object, the second one is the object itself.
Delphi-Quellcode:
fsScript1.AddObject('Button1', Button1);
If object has an unregistered type, you have to register it before calling AddObject:
Delphi-Quellcode:
fsScript1.AddClass(TForm1, 'TForm');
fsScript1.AddObject('Form1', Form1);
[...]

Adding a class to the script
To add a class to a script, call the TfsScript.AddClass method. The first parameter is
the class type, the second one is the name of the parent class.
Delphi-Quellcode:
type
TMyClass = class(TObject)
...
end;

fsScript1.AddClass(TMyClass, 'TObject');
This will make all the published properies of this class available. If you want to make
this class available for all the scripts, it is recommended to add this class to the
fsGlobalUnit which is a global ancestor of all the scripts.
Vielleicht hat ja jemand schon derartiges Vorgehabt und hätte eine Idee für mich. Alternativen wüsste ich auch nicht, würde auch gerne bei dieser bleiben; benutze den JScript Dialekt.

Grüße

RWarnecke 29. Dez 2009 22:39

Re: FastScript - dem Script ein Interface hinzufügen
 
So erstelle ich doch ein Interface :
Delphi-Quellcode:
type
  TApplicationImpl = class(TInterfacedObject, IApplication)
  private
  {...}
  protected
  {...}
  public
  {...}
  end;
Wenn Du das ganze jetzt so versucht wie es für die Klasse beschrieben ist, sollte man über das Interface auf den Scriptbereich von FastReport zugreifen können. So würde ich es probieren.

geskill 29. Dez 2009 22:58

Re: FastScript - dem Script ein Interface hinzufügen
 
Hallo,
genau, dass mit dem AddClass kann man ja so machen, jedoch wenn man dann die Instanz vom laufenden Programm dem Script übergeben will weiß ich nicht weiter:

IProgrammVariable ist jetzt in dem Beispiel vom Typ IApplication.
Delphi-Quellcode:
AddObject('ScriptVariable',IProgrammVariable);
Geht nicht, da etwas von "TObject" erwartet wird :(

RWarnecke 29. Dez 2009 23:07

Re: FastScript - dem Script ein Interface hinzufügen
 
Der Teil aus Beitrag #2 steht ja in Deinem Programm. Derjenige der über das Interface auf die Anwendung zugreift, greift ja über diesen Teil auf die Funktionen Deines Programm zu :
Delphi-Quellcode:
type
  IApplication = interface(IInterface)['{E770EE0C-05F3-4CC5-AF02-497F968287EA}']
    function xy:integer;
  end;
Die Funktione XY wird dann in dem Object TApplicationImpl deklariert und aufgerufen. Dieses Object solltest Du dann an FastScript übergeben können.

geskill 29. Dez 2009 23:24

Re: FastScript - dem Script ein Interface hinzufügen
 
Dieses Objekt von dem du sprichst verwende ich im ganzen Programm nicht. (Hoffe ich weiß was du meinst)

Ums ein bisschen zu verdeutlichen:
Also die Instanz von TApplicationImpl merke ich mir nur als Inteface.
Delphi-Quellcode:
var
  IProgrammVariable: IApplication;

procedure Erstellen;
begin
  IProgrammVariable := TApplicationImpl.Create();

  IProgrammVariable.xy;
end;
Später will ich das dann dem Script hinzufügen:
Delphi-Quellcode:
procedure Hinzufügen;
begin
  with FfsScript do
  begin
    with AddClass(TApplicationImpl, 'TApplicationImpl') do
    begin
      AddMethod('procedure xy', CallMethod);
    end;
    AddObject('ScriptVariable', IProgrammVariable);
  end;
end;

RWarnecke 30. Dez 2009 02:12

Re: FastScript - dem Script ein Interface hinzufügen
 
Ja und nein. Ich schreibe es nochmal etwas ausführlicher. Um die Basis eines Interface für ein Programm zur Verfügung zu stellen, brauchst Du zum Beispiel diese Klasse :
Delphi-Quellcode:
  TApplicationImpl = class(TInterfacedObject, IApplication)
  private
    FAppIntf: TApplicationImpl;
  public
    constructor Create(aAppIntf: TApplicationImpl);
    // overrides
    function Test_1: WideString;
    function Test_2: Integer;
  end;
Diese Klasse führt Operationen im Programm aus, wie zum Beispiel Daten aus einer Datenbank auslesen. Diese Daten werden nun dem Interface zur Verfügung gestellt und können abgerufen werden. Es geht natürlich auch der Weg rückwärts, also das Schreiben. In dem Sourcecode vom PlugIn hast Du dann folgendes deklariert um auf die Funktionen der Klasse zuzugreifen :
Delphi-Quellcode:
  IApplication = interface(IInterface)['{1C851830-2F05-434F-A4DC-A46482B8332F}']  // <-- Mit STRG+SHIFT+G erstellen
    function Test_1: WideString;
    function Test_2: Integer;
  end;
Über das Interface gereifst Du nun auf die Klasse zu und liest oder schreibst Werte. Zusätzlich sollte es doch möglich sein, dass das Object TApplicationImpl an FastScript übergeben wird mit :
Delphi-Quellcode:
faScript1.AddClass(TApplicationImpl, 'TInterfacedObject');
Jetzt könnte zum Beispiel die Funktion Test_1 irgendeinen Wert von FastScript bekommen und an das Interface weitergeben.

geskill 30. Dez 2009 12:07

Re: FastScript - dem Script ein Interface hinzufügen
 
Liste der Anhänge anzeigen (Anzahl: 1)
Das ist soweit ja möglich. Aber wenn man jetzt im Script ist und der Anwender schreibt so was:

Code:
ScriptVariable.Test_1;
Dann dafür müsste man ja FastScript diese Befehle übergeben:

Delphi-Quellcode:
#
  with AddClass(TApplicationImpl, 'TApplicationImpl') do
  begin
    AddMethod('procedure Test_1', CallMethod);
  end;
   
  AddObject('ScriptVariable', IProgrammVariable);
================================================== ==========
Wäre da nur diese eine Prozedur könnte ich gleich die ganze Klasse weglassen und nur die Prozedur hinzufügen. Habe hier mal das aktuelle Interface.

http://img5.imagebanana.com/img/4oqi.../Interface.jpg

So soll der User z.b. diesen Script Code benutzen können:
Code:
for(var i = 0; i < ScriptVariable.MirrorCount; i++)
  for(var j = 0; j < ScriptVariable.Mirror[I].DirectlinksMirrorCount; j++)
    ShowMessage(ScriptVariable.Mirror[I].DirectlinksMirror[J]);

geskill 30. Dez 2009 17:36

Re: FastScript - dem Script ein Interface hinzufügen
 
Aber egal wie ich es drehe:
Programm kommt nicht mal in GetProp :(

Delphi-Quellcode:
function TTemplateParser.GetProp(Instance: TObject; ClassType: TClass; const PropName: String): Variant;
begin
  Result := 0;
  if PropName = 'MirrorCount' then
    Result := IMirrorController(Instance as TMirrorController).MirrorCount;
end;

function TTemplateParser.Exec(s: string): string;
begin
  Result := '';

  with FfsScript do
  begin
    with AddClass(TMirrorController, 'TMirrorController') do
    // with AddClass(TMirrorController, 'TInterfacedObject') do // geht genauso wenig
    begin
      AddProperty('MirrorCount', 'Integer', GetProp, nil);
    end;

    AddObject('IMirrorController', FITabSheetController.MirrorController as TMirrorController);
ScriptCode z.b.:
Code:
ShowMessage(IntToStr(IMirrorController.MirrorCount));

geskill 31. Dez 2009 14:58

Re: FastScript - dem Script ein Interface hinzufügen
 
ups, da hatte ich mir wohl ein Eigentor geschossen, ein paar Zeilen später im Code hatte ich ein clear Befehl, der alle hinzugefügten Objekte wieder gelöscht hat.
Somit geht es soweit. Habe mir die Komponenten jetzt gekauft.


Falls irgendwann jemand einen Lösungsansatz braucht:
Delphi-Quellcode:
function TTemplateParser.GetProp(Instance: TObject; ClassType: TClass; const PropName: String): Variant;
begin
  Result := 0;
  if PropName = 'MIRRORCOUNT' then
    Result := IMirrorController(Instance as TMirrorController).MirrorCount
  else if PropName = 'DIRECTLINKSMIRRORCOUNT' then
    Result := IMirrorControl(Instance as TMirrorControl).DirectlinksMirrorCount
  else if PropName = 'CRYPTERCOUNT' then
    Result := IMirrorControl(Instance as TMirrorControl).CrypterCount
  else if PropName = 'NAME' then
    Result := ICrypterPanel(Instance as TCrypterPanel).Name
  else if PropName = 'LINK' then
    Result := ICrypterPanel(Instance as TCrypterPanel).Link;
end;

function TTemplateParser.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String;
  var Params: Variant): Variant;
begin
  Result := 0;
  if MethodName = 'MIRROR.GET' then
    Result := Integer(IMirrorController(Instance as TMirrorController).Mirror[Params[0]] as TMirrorControl)
  else if MethodName = 'DIRECTLINKSMIRROR.GET' then
    Result := IMirrorControl(Instance as TMirrorControl).DirectlinksMirror[Params[0]]
  else if MethodName = 'CRYPTER.GET' then
    Result := Integer(IMirrorControl(Instance as TMirrorControl).Crypter[Params[0]] as TCrypterPanel)
end;

function TTemplateParser.Exec(s: string): string;
var
  I: Integer;
begin
  Result := '';

  with FfsScript do
  begin
    Clear;
    Lines.Text := s;
    Parent := fsGlobalUnit;

    with AddClass(TCrypterPanel, 'TCrypterPanel') do
    begin
      AddProperty('Name', 'string', GetProp, nil);
      AddProperty('Link', 'string', GetProp, nil);
    end;

    with AddClass(TMirrorControl, 'TMirrorControl') do
    begin
      AddIndexProperty('DirectlinksMirror', 'Integer', 'string', CallMethod);
      AddProperty('DirectlinksMirrorCount', 'Integer', GetProp, nil);

      AddIndexProperty('Crypter', 'Integer', 'TCrypterPanel', CallMethod);
      AddProperty('CrypterCount', 'Integer', GetProp, nil);
    end;

    with AddClass(TMirrorController, 'TMirrorController') do
    begin
      AddIndexProperty('Mirror', 'Integer', 'TMirrorControl', CallMethod);
      AddProperty('MirrorCount', 'Integer', GetProp, nil);
    end;
    AddObject('IMirrorController', FITabSheetController.MirrorController as TMirrorController);

    if Compile then
      Execute;
    else
      Error(ErrorMsg);

  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:50 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz