Sign in Join
Wiki copytemporary

Porting from FPC/Delphi to pas2js: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Porting from FPC/Delphi to pas2js

OldNewLine
1+This page is for Delphians and FPC porting code to pas2js. It contains useful tips and common traps.
2+
3+# Numbers
4+
5+- Internally all numbers are *double*. Therefore integers range from -9,007,199,254,740,991 to 9,007,199,254,740,991, so about 1/1024th of a 64bit value.
6+- There is **no** *Int64* and no *QWord*.
7+- All **bitwise operators are limited to 32bit**, including the *mod* operator, which is limited to signed 32bit.
8+- **Integers overflows** at runtime differ from Delphi/FPC. For example adding *var i: byte = 200; ... i:=i+100;*' will result in *i=300* instead of *i=44* as in Delphi/FPC. When range checking *{\$R+}* is enabled *i:=300* will raise an *ERangeError*.
9+- **Division by zero** does not raise *EDivByZero*, instead it results in *NaN*.
10+- **Currency** has only 54 bits. Currency is internally a double, multiplied by 10000 and truncated. The below values are the safe limits, within every step exists. Since currency is a double it can take much larger values, but the result may differ from Delphi/FPC:
11+ - MaxCurrency = 900719925474.0991; // fpc: 922337203685477.5807;
12+ - MinCurrency = -900719925474.0991; // fpc: -922337203685477.5808;
13+
14+# Strings
15+
16+- String is UnicodeString and there are no other string types.
17+- Strings are immutable in JS. That means changing a single character creates a new string. That's why some fast Delphi/FPC string functions are much slower in pas2js.
18+
19+# Asynchronous vs Waiting
20+
21+JavaScript enforces an absolute async manner of programming:
22+
23+- Many calls are asynchronous and return immediately. For example loading a resource.
24+- There is **no** Application.ProcessMessages. You cannot wait till some event occurs. You must set an event. That's why anonymous functions are so frequently used in JS - they keep the local variables accessible.
25+- There is no multithreading, no shared memory. Many browsers/JS engines support webworkers, but that is more like processes than threads.
26+
27+# Details
28+
29+A more detailed list can be found in the [translation.html](https://gitlab.com/freepascal.org/fpc/source/-/tree/3.3.1/utils/pas2js/docs/translation.html).file in the sources.
30+
31+---
32+*From the Free Pascal wiki, page "Porting from FPC/Delphi to pas2js", as it stood on 2023-03-08 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
33+Category: Pas2js

Back to history