Procedure as function argument

I have a delphi function that I would like to run in the script:

type
  ...
  TInputProc = reference to procedure(AForm: TInputForm);
  ...
function Accept(var AResult: Variant; AInitProc: TInputProc): Boolean;
  …


Function is registered as:

type
  TToolsLibrary = class(TatScripterLibrary)
    ...
    procedure _Accept(M: TatVirtualMachine);
    ...
  end;
  ...
procedure TToolsLibrary._Accept(M: TatVirtualMachine);
var
  V: Variant;
  R: Boolean;
begin
  V := M.GetInputArgAsVariant(0);
  R := Accept(V, TInputProc(M.GetInputArgAsInteger(1)));
  M.SetInputArg(1, V);
  M.ReturnOutputArg(R);
end;

procedure TToolsLibrary.Init;
begin
  ...
  Scripter.DefineClassByRTTI(TInputForm);
  Scripter.DefineMethod('Accept', 2, TatTypeKind.tkInteger, nil, _Accept, False, 0, 'AResult:Variant;InitProc:TInputProc').SetVarArgs([0]);
  ...
end;


How can I call function Accept in script?

procedure InitInput(AForm: TInputForm);
begin
  ..
end;

var
  V: Variant;
begin
  ...
  Accept(V, InitInput); // Compiler error: Not enough actual parameters for subroutine 'InitInput'. Expected 1 parameters.
  ...
end


TIA and best regards
Branko

Scripter doesn't accept function pointers/references, unfortunately you can't pass, declare or receive values as TInputProc type.

But you can do workarounds, for example from script you could pass the name of the routine:


Then when implementing _Accept, you do in a way where you create an intermediate InputProc function that in turn calls the routine in the script by the name, calling


 AMachine.ExecuteSubroutine(PassedFunctionName, ObjectToVar(TheInputForm));


I hope it's clear.

Thank you! It works!

Have a nice day

BTW- When posting code with [code] ... [/ code], each line is followed by an empty line. What am I doing wrong?

I'm not sure, I don't see it happening here for me?


FirstLine;
SecondLine;

Wagner R. Landgraf2019-05-27 13:05:28