Recent

Author Topic: Javascript to pas  (Read 2116 times)

ramersonw

  • Newbie
  • Posts: 2
Javascript to pas
« on: March 07, 2022, 06:54:43 pm »
Hi,

Hi, I'm trying to use a lib in my pas2js project, but I'm not able to make it work.

I'm triyng to use the snippet below:
Code: Javascript  [Select][+][-]
  1. const myPromisse = MyPromisseInit();
  2.  
  3. myPromisse.then((data) => {
  4.   const temp = new data.SomeConstructor();
  5. })
  6.  

My pascal code is as follows:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}
  4. {$modeswitch externalclass}
  5.  
  6. uses
  7.   JS;
  8.  
  9. type
  10.   TData = class external name 'Object' (TJSObject)
  11.   public
  12.     constructor SomeConstructor();
  13.   end;
  14.  
  15.   function MyPromisseInit(): TJSPromise external name 'MyPromisseInit';
  16.  
  17.  
  18. var
  19.   test: TJSPromise;
  20. begin
  21.   test := MyPromisseInit();
  22.   test._then(function (data: JSValue): JSValue
  23.   var
  24.     _data: TData absolute data;
  25.     res: JSValue;
  26.   begin
  27.     res := _data.SomeConstructor();
  28.   end);
  29. end.
  30.  

But it did not work.
Compile says project1.lpr(27,18) Error: External class instance cannot access static constructor SomeConstructor


What I have to change to make it work?

Edited the pascal snippet with complete code.
« Last Edit: March 07, 2022, 07:37:39 pm by ramersonw »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Javascript to pas
« Reply #1 on: March 07, 2022, 07:19:45 pm »
What I have to change to make it work?

Everything.

You can't nest blocks like that, and in particular you have to declare variables (var declaration) outside the begin/end block that will reference them.

Don't trust the compiler error messages until you've got the layout right.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Javascript to pas
« Reply #2 on: March 07, 2022, 10:10:18 pm »
Actually that is Delphi anonymous methods syntax.

ramersonw

  • Newbie
  • Posts: 2
Re: Javascript to pas
« Reply #3 on: March 08, 2022, 12:14:42 am »
Yes. It's an anonymous function and It Works with pas2js transpiler.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Javascript to pas
« Reply #4 on: March 08, 2022, 09:11:11 am »
In that case I'd like to apologise for being misleading.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018