3

How can I call a procedure from a class that is created in main Form. Can it be done like this pseudo code shows?

type
  TDemo = class
    procedure test;
    constructor Create;
    destructor Destroy; override;
  end;

var
  Form28: TForm28;
  Demo:TDemo;

implementation

{$R *.dfm}

procedure TForm28.Button1Click(Sender: TObject);
var
   prog : IdwsProgram;
   exec : IdwsProgramExecution;
begin
   Demo := TDemo.Create;
   prog := DelphiWebScript1.Compile('Demo.Test;');
   exec := prog.Execute;
end;
MartynA
  • 30,454
  • 4
  • 32
  • 73

2 Answers2

2

There is a limited RTTI exposer and RTTI connector, which allow to access Delphi classes through RTTI.

These RTTI tools haven't been explored much however, as most of the Delphi classes are not "safe" to use for scripting. By that I mean that it's easy to crash the host or leak memory, and so "raw" Delphi classes are typically unsuitable for end-user scripting (ie. end users won't have a right to error, you won't be able to offer stable debugging, etc.).

An alternative to manual exposure and strengthening of the exposed classes in event handlers of a TdwsUnit is to expose your classes as OLE Automation objects, and then you can use the DWScript COM Connector to access them. Then benefits is that to expose the automation objects, you'll usually have had to do at least minimal strengthening vs memory leaks and dangling pointers, and your automation classes will be accessible from other COM-capable environments.

As an example of RTTI going wrong, consider a fully automatically-managed VCL classes like TComponent or TCollection, if you only have raw RTTI exposure, than a script doing something like:

item := myCollection.Add;
myCollection.Clear;
item.Caption := 'hello bug';

will result in a random memory overwrite in the host application, without any safe ways to notify the script user about the potential error.

The upcoming Delphi ARC compilers may offer a way to mitigate the memory overwrites for some classes (though not all, due to the way ARC is currently implemented/circumvented for TComponent and others). Also Delphi ARC compilers are currently unsupported (for a variety of reasons, the most prominent one being I currently don't have access to them).

Eric Grange
  • 5,931
  • 1
  • 39
  • 61
  • AFAIK paxcompiler can do this. I wonder why DWScript can't do it. –  Oct 11 '13 at 14:52
  • Pax Compiler doesn't provide sandboxing nor encapsulation, bugs in Pax scripts can crash the host, and you can't offer stable debugging capabilities either. DWScript was built around being able to sandbox script execution, abort any script at any time without leaks, and generally provide survive buggy scripts (so they can be debugged from the host application). – Eric Grange Oct 14 '13 at 02:38
1

To do this, you first have to expose your native class to the script engine. Have a look at the TdwsUnit component. It declares a script unit that interfaces with external code. You'd drop one on your form, define the class, define its methods, and hook up event handlers on the OnEval events that call the external routines.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 1
    Yep but what if i have 200 methods? Do i have to add all those manually? I mean there has to be an easy way to talk to the "outside" world. Can't this be done via RTTI? –  Oct 04 '13 at 17:22
  • At the moment, if you want the script engine to be able to interface with the class, yes. It's one of DWS's rougher edges; it was designed as more of a self-contained scripting system (for building webpages, hence the name) than as an automation layer atop a strong standalone codebase the way some scripting systems are. I've been working with Eric on some additional functionality to make this sort of thing easier, but it's not ready yet. I know that some RTTI-based interface exists, but I'm not familiar with how it works, and I do know that if you use it, you lose static type checking. – Mason Wheeler Oct 04 '13 at 17:28
  • Well that sucks.. big big time. And unfortunately is a show stopper. –  Oct 04 '13 at 17:30
  • @EricSantos: I know. That's why I'm working on it. :) – Mason Wheeler Oct 04 '13 at 17:38
  • Is there any script engine that can do this? –  Oct 04 '13 at 18:49