remobjects / pascalscript

pascalscript
Other
447 stars 178 forks source link

How to run Event in PascalScript? #230

Closed luizluvizutto closed 4 years ago

luizluvizutto commented 4 years ago

Hello friends! I'm having difficulty running an event in PascalScript. In this example, I have a "type" and a "class".

TEvento = procedure( txt: String );

TClassEvento = class( TComponent )
private
   FEvento: TEvento;
public
   property Evento: TEvento read FEvento write FEvento;
   procedure Executar;
end;

procedure ABC( msg: String );

implementation

procedure ABC( msg: String );
begin
   ShowMessage( msg ); 
end;

{ TClassEvento }

procedure TClassEvento.Executar;
begin
   if Assigned(FEvento) then begin
      FEvento( 'Olá Mundo' );
   end;
end;

procedure test;
var O: TClassEvento; 
begin
   O := TClassEvento.Create(NIL);
   try
      O.Evento := ABC;
      O.Executar;
   finally
      O.Free;
   end;
end;

With TEvento and TClassEvento registered in Pascal Script, as shown below:

procedure ABC( txt: String );
begin
   ShowMessage('Estou no Script: ' + txt);
end;

var O: TClassEvento; 
begin
   O := TClassEvento.Create(NIL);
   try
      O.Evento := @ABC;
      O.Executar;
   finally
      O.Free;
   end;
end.

Comments: I saw in some grammars that you need to add the "@" O.Evento: = @ABC; when using PascalScript and does not work. When I try to run without the "@" I get the return: [Error] (10:22): Invalid number of parameters

The link contains the example I set up.

Sorry for my English, I'm from Brazil and my sincere thanks.

pult commented 4 years ago

? of object ? TEvento = procedure( txt: String ) of object;

...
procedure SIRegister_TClassEvento(CL: TPSPascalCompiler);
begin
  CL.AddTypeS('TEvento', 'procedure( txt: String )');
  with CL.AddClassN(CL.FindClass('TComponent'),'TClassEvento') do
  begin
    RegisterProperty('Evento', 'TEvento', iptrw);
...
luizluvizutto commented 4 years ago

I made the change and the same problem.

pult commented 4 years ago

I have no problems. Not enough information to analyze. Need a complete example, IDE version...

luizluvizutto commented 4 years ago

Hello, I'm using Delphi 10.3 Community Edition Follow the link of the example I set up: https://github.com/luizluvizutto/TstPascalScript

pult commented 4 years ago

Working fine when correct your code:

Un_Evento.pas TEvento = procedure( txt: String ) of object; // changed declaration to method

Un_Principal.pas

TFrmPrincipal = class(TForm)
    ...
    procedure ProcedureDoEvento( txt: String ); // move to method
  end;
.,.
procedure TFrmPrincipal.ProcedureDoEvento( txt: String ); // implement as method
..
luizluvizutto commented 4 years ago

My problem is in the Script.

Perhaps I am not able to explain my problem correctly, because my English is weak.

When I run "Run script" nothing happens.

In this link I uploaded an exe of this example. If you click on "Run Script" the ABC method is not triggered. https://www.dropbox.com/sh/840au9t57xinvgs/AADxE1yfrlrx8aULBl_RVBtSa?dl=0

pult commented 4 years ago

Detailed: https://github.com/pult/TstPascalScript/commit/54838fd0c00896018f054d02e5e48e57aa74f649

luizluvizutto commented 4 years ago

@pult thanks for the concern brother.

I cloned your sources and tested it. Did not work. Clicking the "Executar Script" button nothing happens.

Send me your compiled executable for testing.

pult commented 4 years ago

binary (xe3, dx10.3, dx10.4 x86, x64 ) and full sources with thirdparty... TstPascalScript-pult-full_v2020.0605.0217.7z

luizluvizutto commented 4 years ago

Hello friend, it looks like I'm getting close to the solution!

Your executables worked!

I replaced the "rps" fonts in my source "... \ pascalscript \ Source" and recompiled my system and it worked!

Now my question was, what is different about your sources that you don't have in the main repository?

Or rather, what are the sources with the thirdparty? I think my problem is there.

And again, thanks for the help!

pult commented 4 years ago

https://github.com/pult/pascalscript

luizluvizutto commented 4 years ago

Hello Friend! Well, first I want to congratulate you on the fork of remobjects and, above all, to make something that I think is very important for Script work. Only I had some doubts. I even apologize for meddling. Wouldn't these adjustments make it legal for RemObjects to implement their sources?

pult commented 4 years ago

No. The standard(lelag) version RemObj should also work fine. Problems are possible in the latest versions of Delphi and the incomplete implementation of "InvokeCall.inc". I'm just disabling its use.

relativ commented 3 years ago

hi,

how you solve the problem ? i had same problem , can describe it ? i don't understand from the reading.

thanks