delphidwscript

May DWS calling functions from sample unit?


May DWS calling functions from sample unit? For example:

FExecution.Info.Func['Test.ClickProc'].Call(AParams); (This is not working)

FExecution - IdwsProgramExecution;

Function declared in Script code:

unit Test;
Uses UTestUnit;
procedure TestFunc(LParam: string);
begin
  ShowMessage(LParam);
end;
procedure ClickProc(Sender: TObject);
begin
  ShowMessage('DWS');
end;
var  S: TStringList;
var  btn: TButton;
begin
  btn := TButton.Create(MainForm);
  btn.OnClick := ClickProc;
  btn.Parent(MainForm);
end.

ClickProc in this example.


Solution

  • I don't think it can be done.

    The symbol lookup in the call to Info.Func[] doesn't resolve the name into a unit scope and an identifier but instead does a simple lookup for a symbol named "Test.ClickProc". However since the ClickProc procedure symbol is named "ClickProc" the lookup doesn't find it.

    I thought that maybe it would be possible to find the procedure symbol manually via the symbol tables...

    var
      FExecution: IdwsProgramExecution;
      UnitSymbol: TUnitMainSymbol;
      FuncSymbol: TFuncSymbol;
      Info: IInfo;
    begin
      ...
      UnitSymbol := FExecution.Prog.UnitMains.Find('Test');
      FuncSymbol := UnitSymbol.Table.FindSymbol('ClickProc', cvPublic) as TFuncSymbol; // Returns nil :-(
      Info := FExecution.Info.FuncBySym[FuncSymbol];
      Info.Call;
      ...
    

    ... but after consulting the "documentation" (read: studying the source, stepping through with a debugger and much trial and error) I've come to the conclusion that it cannot be done reliably at this time. It is possible to find the "ClickProc" symbol in one of the many symbol tables but I've not been able to find a reliable way to qualify the symbol with a unit name/symbol.