remobjects / pascalscript

pascalscript
Other
447 stars 178 forks source link

Call PascalScript function from Lazarus #256

Closed Dingo64 closed 2 years ago

Dingo64 commented 2 years ago

Code in Lazarus:

  TCallback = procedure;

  FFForm = class(TObject)
    procedure OnCreate(Callback: TCallback);
  end;   

procedure TForm1.PSScript1Compile(Sender: TPSScript);
var CustomClass: TPSCompileTimeClass;
begin    
  Sender.AddFunction(@MWrites, 'procedure Writes(const s: string)'); 
[...]

procedure MWrites(const s: string);
begin
  showmessage(s);
end;    

procedure FFForm.OnCreate(Callback: TCallback);
begin
  ShowMessage('1');
  Callback;
end;   

My code in PascalScript:

procedure FormCreate; 
begin
  Writes('2');
end;

var Form: FForm;
begin
Form := FForm.Create;
Form.OnCreate(@FormCreate);
Form.Free;
end.

My function FFForm.OnCreate is executed just fine and I can see the showmessage('1') showing a dialogbox saying "1" but the Callback is not executed and the code "Writes('2')" is not executing.

How I can I execute this callback from Lazarus?

Dingo64 commented 2 years ago

Seems Callback must be "procedure of object" and then it works!