Lazarus

Programming => Networking and Web Programming => Pas2JS => Topic started by: Kryvich on January 22, 2019, 12:22:40 pm

Title: Pas2js, typed arrays and ANSI strings
Post by: Kryvich on January 22, 2019, 12:22:40 pm
Hi,
Can Pas2js transpile Delphi/FPC typed arrays to their counterparts in JavaScript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)? I mean
I see declarations of TJSInt32Array, TJSUint8Array, TJSUint16Array in js.pas, but when I try to transpile my test program I can't find these names in the result JS file.

If/when these arrays will be supported by the transpiler, we can move further and try to convert AnsiStrings as UInt8Array.
Title: Re: Pas2js, typed arrays and ANSI strings
Post by: Kryvich on January 22, 2019, 01:23:02 pm
OK I can use JavaScript typed arrays using bindings from js.pas!

Code: Pascal  [Select]
  1. program TestTypedArrays;
  2. {$IFDEF PAS2JS}
  3. uses
  4.   JS;
  5. {$ENDIF}
  6.  
  7. type
  8. {$IFDEF PAS2JS}
  9.   TByteArray = TJSUint8Array;
  10.   TWordArray = TJSUint16Array;
  11. {$ELSE}
  12.   TByteArray = array of Byte;
  13.   TWordArray = array of Word;
  14. {$ENDIF}
  15.  
  16. procedure Test;
  17. var
  18.   ba: TByteArray;
  19.   wa: TWordArray;
  20. begin
  21. {$IFDEF PAS2JS}
  22.   ba := TByteArray.new(10);
  23. {$ELSE}
  24.   SetLength(ba, 10);
  25. {$ENDIF}
  26.   ba[0] := $FF;
  27. {$IFDEF PAS2JS}
  28.   wa := TWordArray.new(20);
  29. {$ELSE}
  30.   SetLength(wa, 20);
  31. {$ENDIF}
  32.   wa[1] := $FFEE;
  33.   Writeln('ba[0] = ', ba[0], ', wa[1] = ', wa[1]);
  34. end;
  35.  
  36. begin
  37.   Test;
  38. end.

But there is too much IFDEFs. Would be nice to have the same syntax as in Delphi/FPC.
Title: Re: Pas2js, typed arrays and ANSI strings
Post by: mattias on February 18, 2019, 06:20:16 pm
At the moment all dynamic arrays are TJSArrays.
Using the typed arrays is an interesting proposal.