### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook (preview only)

### Author Topic: Pas2js, typed arrays and ANSI strings  (Read 1352 times)

#### Kryvich

• Newbie
• Posts: 4
##### Pas2js, typed arrays and ANSI strings
« on: January 22, 2019, 12:22:40 pm »
Hi,
Can Pas2js transpile Delphi/FPC typed arrays to their counterparts in JavaScript? I mean
• array of Integer -> Int32Array
• array of Byte -> UInt8Array
• array of Word -> UInt16Array
• etc.
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.

#### Kryvich

• Newbie
• Posts: 4
##### Re: Pas2js, typed arrays and ANSI strings
« Reply #1 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.