Calling Delphi functions from JS issue

Hi,

I have a query regarding calling Delphi functions from JS. If you look at the code below:

type
  TForm1 = class(TWebForm)
    WebButton1: TWebButton;
    WebButton2: TWebButton;
    procedure WebButton1Click(Sender: TObject);
    procedure WebButton2Click(Sender: TObject);
    procedure Form1Create(Sender: TObject);
  private
    MyVar:String;
    procedure ShowMyVar;
  end;

implementation

{$R *.dfm}

procedure TForm1.Form1Create(Sender: TObject);
begin
  MyVar:='This is MyVar!';
end;

procedure TForm1.WebButton1Click(Sender: TObject);
begin
  asm let _MyVar=this.MyVar; end;
  asm alert(_MyVar) end;
end;

procedure TForm1.WebButton2Click(Sender: TObject);
begin
  asm let _ShowMyVar=this.ShowMyVar; end;
  asm _ShowMyVar() end;
end;

procedure TForm1.ShowMyVar;
begin
  ShowMessage(MyVar);
end;

end.

If we call WebButton1Click, the code works as I expect.
However, if we call WebButton2Click, the compiler says that MyVar is undefined.

If I use the debugger, I see that when we get inside the function call _ShowMyVar() , this suddenly becomes undefined and stops pointing to the class object.

Is this expected behaviour? And if so, could you direct me as to how to call Delphi functions using JS function pointers?

I have a workaround at the moment that involves creating a static object of the class object in the class itself and using that to call the functions, but this is very inelegant.

Thanks!

I retested this here with:

  TForm2 = class(TWebForm)
    WebButton1: TWebButton;
    procedure WebButton1Click(Sender: TObject);
  private
    { Private declarations }
    MyVar: string;
    procedure ShowMyVar;
  public
    { Public declarations }

  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.ShowMyVar;
begin
  ShowMessage(MyVar);
end;

procedure TForm2.WebButton1Click(Sender: TObject);
begin
  asm
    this.MyVar = 'Hello world';
    this.ShowMyVar();
  end;
  console.log(MyVar);
end;

and this works as expected here. Do you use the latest version of TMS WEB Core?

Hi Bruno,

Thanks for the reply.

Yes, the code you showed me worked.

However, in my previous example, I wasn't calling the function directly in an asm block; I was, in essence, creating a JS function pointer (i.e. asm let _ShowMyVar=this.ShowMyVar; end;) and then calling it with that. This is where my problem lies.

Thanks!

I'm not sure you can use a JS function pointer like that or treat this.ShowMyVar as function pointer

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.