Porting from FPC/Delphi to pas2js

From Lazarus wiki
Revision as of 14:50, 21 February 2019 by Mattias2 (talk | contribs) (Created page with "This page contains useful tips for Delphians and FPC users on how to port code to pas2js. =Numbers= * Internally all numbers are ''double'' * There is '''no''' ''Int64'' and...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This page contains useful tips for Delphians and FPC users on how to port code to pas2js.

Numbers

  • Internally all numbers are double
  • There is no Int64 and no QWord
  • All bitwise operators are limited to 32bit, including the mod operator, which is limited to signed 32bit.
  • 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.

Strings

  • String is UnicodeString and there are no other string types.
  • 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.

Asynchronous vs Waiting

JavaScript enforces an absolute async manner of programming:

  • Many calls are asynchronous and return immediately, like loading a resource.
  • 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.
  • There is no multithreading, no shared memory. Many browsers/JS engines allow to start workers, but that is more like processes than threads.