Sign in Join
Wiki copytemporary

pas2js: what changed

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

OldNewLine
11## Pas2js : What is it ?
22
33### Compiler
44
5-Pas2js is an open source [[Pascal]] to [[JavaScript]] transpiler. It parses Object Pascal and emits JavaScript. The JavaScript is currently of level ECMAScript 5 and should run in any browser or in Node.js (target "nodejs"). It is available in 5 forms:
5+Pas2js is an open source [[Pascal]] to [[JavaScript]] transpiler. It parses Object Pascal and emits JavaScript. The JavaScript is currently of level ECMAScript 5 and should run in any browser or in Node.js (target "nodejs"). It is available in 5 forms:
66
7-- as a library
8-- as a command-line program
9-- as a webserver
10-- as a node.js program
11-- as a program running in the browser.
7+ * as a library
8+ * as a command-line program
9+ * as a webserver
10+ * as a node.js program
11+ * as a program running in the browser.
1212
13-It transpiles from actual Pascal source, it has no intermediate .ppu files. That means all sources must always be available.
13+It transpiles from actual Pascal source, it has no intermediate .ppu files. That means all sources must always be available.
1414
15-Through external class definitions, the compiler can use JavaScript classes:
15+Through external class definitions, the compiler can use JavaScript classes:
1616
17-- All classes available in the JavaScript runtime, and in the browser are available\
18- through import units (comparable to the windows or unix units for the native compiler).
19-- For Node.js, basic support for the nodejs runtime environment is available.
20-- An import unit for jQuery is available (libjquery)
17+ * All classes available in the JavaScript runtime, and in the browser are available
18+through import units (comparable to the windows or unix units for the native compiler).
19+ * For Node.js, basic support for the nodejs runtime environment is available.
20+ * An import unit for jQuery is available (libjquery)
2121
22-As a non commercial open source project we are always searching for helping hands. If you want to contribute see [[Pas2js How to contribute|here]].
22+As a non commercial open source project we are always searching for helping hands. If you want to contribute see [[Pas2js How to contribute|here]].
2323
24-This project is **not** related to a similar named project on github.
24+This project is **not** related to a similar named project on github.
2525
2626### RTL
2727
28-For the generated code to work, a small JavaScript file is needed: rtl.js. It defines an object rtl. This object will start the Object Pascal code if you include a call to rtl.run() in the [[HTML]] page.
29-
30-```html4strict
28+For the generated code to work, a small JavaScript file is needed: rtl.js. It defines an object rtl. This object will start the Object Pascal code if you include a call to rtl.run() in the [[HTML]] page.
29+```
3130<script type="application/javascript">
32- rtl.run()
33-</script>
31+ rtl.run()
32+ </script>
3433```
3534
36-pas2js can automatically include this file in the generated output, like this:
37-
38- pas2js -Jc -Jirtl.js -Tbrowser hello.pas
35+pas2js can automatically include this file in the generated output, like this:
36+```
37+pas2js -Jc -Jirtl.js -Tbrowser hello.pas
38+```
3939
40-For nodejs, the compiler will insert the call to rtl.run() automatically at the end of the generated Javascript file.
40+For nodejs, the compiler will insert the call to rtl.run() automatically at the end of the generated Javascript file.
4141
42-There is a basic Object Pascal RTL, several units from the FPC Packages are also available
42+There is a basic Object Pascal RTL, several units from the FPC Packages are also available
4343
44-- system
45-- sysutils
46-- Math
47-- strutils
48-- rtlconst
49-- classes
50-- contnrs
51-- DB (yes, TDataset)
52-- fpcunit testsuite
53-- custapp
54-- restconnection
55-- js (javascript system objects)
56-- web (browser provided objects)
57-- libjquery (jquery is available too)
58-- nodejs (basic node runtime environment)
59-- typinfo
60-- objpas
61-- browserconsole (support writeln)
62-- dateutils
63-- browserapp
64-- nodejsapp
44+ * system
45+ * sysutils
46+ * Math
47+ * strutils
48+ * rtlconst
49+ * classes
50+ * contnrs
51+ * DB (yes, TDataset)
52+ * fpcunit testsuite
53+ * custapp
54+ * restconnection
55+ * js (javascript system objects)
56+ * web (browser provided objects)
57+ * libjquery (jquery is available too)
58+ * nodejs (basic node runtime environment)
59+ * typinfo
60+ * objpas
61+ * browserconsole (support writeln)
62+ * dateutils
63+ * browserapp
64+ * nodejsapp
6565
6666## Where to get it
6767
68-The pas2js compiler and RTL are - naturally - open source and can be downloaded and used freely.
68+The pas2js compiler and RTL are - naturally - open source and can be downloaded and used freely.
6969
7070### Releases
7171
72-The releases contain binaries for **Windows (32 and 64bit), Linux (64 bit) and macOS**.
72+The releases contain binaries for **Windows (32 and 64bit), Linux (64 bit) and macOS**.
7373
74-Installation procedure:
74+Installation procedure:
7575
76-1\. Download pas2js from:
76+1\. Download pas2js from:
7777
78-- <https://getpas2js.freepascal.org/>
78+ * <https://getpas2js.freepascal.org/>
7979
80-
8180Every version has a directory with the version number.
8281
8382A list of changes can be found on the changelog page [[Pas2JS Version Changes]]
8483
85-2\. Unpack it in folder of your choice. The example below uses **C:\lazarus\pas2js\**. The release contains three folders:
84+2\. Unpack it in folder of your choice. The example below uses **C:\lazarus\pas2js\**. The release contains three folders:
8685
87-- bin - contains the compiler as executable (pas2js or pas2js.exe), a pas2js.cfg, a library and some utilities.
88-- demo - lots of examples
89-- packages - the Pascal units of the RTL and other packages.
90-- tools - html2form - HTML to pascal code converter program
91-- utils - A script to create a pas2js.cfg: createconfig.pp
86+ * bin - contains the compiler as executable (pas2js or pas2js.exe), a pas2js.cfg, a library and some utilities.
87+ * demo - lots of examples
88+ * packages - the Pascal units of the RTL and other packages.
89+ * tools - html2form - HTML to pascal code converter program
90+ * utils - A script to create a pas2js.cfg: createconfig.pp
9291
9392
94-![](/wiki-images/f/fa/Pas2js-folder.png)
9593
94+[![Pas2js Folder](https://upload.wikimedia.org/wikipedia/commons/f/fa/Pas2js-folder.png)](</File:Pas2js-folder.png> "Pas2js Folder")
95+
96963\. To use pas2js into Lazarus IDE see: <https://wiki.freepascal.org/lazarus_pas2js_integration>
9797
9898### GIT
9999
100-The sources are available on gitlab, and a read-only mirror exists on github.
101-
102-The makefile expects the sources of FPC to be present under the compiler directory. You can copy/clone the FPC sources there or set enviroment variable FPCDIR. Note that a symlink to the FPC sources will not work.
100+The sources are available on gitlab, and a read-only mirror exists on github.
103101
104-Cloning pas2js and fpc git repos:
102+The makefile expects the sources of FPC to be present under the compiler directory. You can copy/clone the FPC sources there or set enviroment variable FPCDIR. Note that a symlink to the FPC sources will not work.
105103
106-``` bash
104+Cloning pas2js and fpc git repos:
105+```
107106git clone https://gitlab.com/freepascal.org/fpc/pas2js.git pas2js
108-cd pas2js
109-git config --local pull.rebase true
110-git clone https://gitlab.com/freepascal.org/fpc/source.git compiler
111-cd compiler
112-git config --local pull.rebase true
107+ cd pas2js
108+ git config --local pull.rebase true
109+ git clone https://gitlab.com/freepascal.org/fpc/source.git compiler
110+ cd compiler
111+ git config --local pull.rebase true
113112```
114113
115-Updating local git repos:
116-
117-``` bash
114+Updating local git repos:
115+```
118116cd pas2js
119-git pull
120-cd compiler
121-git pull
117+ git pull
118+ cd compiler
119+ git pull
122120```
123121
124-Switching to the fixes branch:
125-
126-``` bash
122+Switching to the fixes branch:
123+```
127124cd pas2js
128-git checkout fixes_3_0
129-cd compiler
130-git checkout pas2js/fixes_3_0
125+ git checkout fixes_3_0
126+ cd compiler
127+ git checkout pas2js/fixes_3_0
131128```
132129
133130### Building on Linux/macOS
134131
135-Change to the directory and build it with:
136-
137-``` bash
132+Change to the directory and build it with:
133+```
138134make clean all
139135```
140136
141-This creates *bin/\$(TargetCPU)-\$(TargetOS)/pas2js* (Windows: pas2js.exe). For example on Linux 64bit it creates *bin/x86_64-linux/pas2js*, while under Windows 64bit it creates *bin\x86_64-win\pas2js.exe*. And a basic config file *bin/x86_64-linux/pas2js.cfg*.
137+This creates _bin/$(TargetCPU)-$(TargetOS)/pas2js_ (Windows: pas2js.exe). For example on Linux 64bit it creates _bin/x86_64-linux/pas2js_ , while under Windows 64bit it creates _bin\x86_64-win\pas2js.exe_. And a basic config file _bin/x86_64-linux/pas2js.cfg_.
142138
143139### Building on Windows
144140
145-Make sure that you use the *make.exe* from fpc, not the one from Delphi by setting the **PATH**: For example if you installed the 32-bit version of fpc in *C:\YourPathOfFPC\3.2.2*:
146-
147-``` bash
141+Make sure that you use the _make.exe_ from fpc, not the one from Delphi by setting the **PATH** : For example if you installed the 32-bit version of fpc in _C:\YourPathOfFPC\3.2.2_ :
142+```
148143set PATH=C:\YourPathOfFPC\3.2.2\bin\i386-win32;%PATH%
149144```
150145
151-If you installed the 64-bit version of fpc in C:\YourPathOfFPC\3.2.2 use
152-
153-``` bash
146+If you installed the 64-bit version of fpc in C:\YourPathOfFPC\3.2.2 use
147+```
154148set PATH=C:\YourPathOfFPC\3.2.2\bin\x86-64-win64;%PATH%
155149```
156150
157-Then build with
158-
159-``` bash
151+Then build with
152+```
160153make clean all
161154```
162155
163-If you see "Error makefile ... Command syntax error" your "set PATH" was not correct. When make all has succesfully finished it created with a 32-bit fpc the executable *bin/i386-win32/pas2js.exe* and a basic config file *bin/i386-win32/pas2js.cfg*.
156+If you see "Error makefile ... Command syntax error" your "set PATH" was not correct. When make all has succesfully finished it created with a 32-bit fpc the executable _bin/i386-win32/pas2js.exe_ and a basic config file _bin/i386-win32/pas2js.cfg_.
164157
165158### pas2js.cfg
166159
167-When you built via **make**, it creates a **pas2js.cfg** in *bin/\$(TargetCPU)-\$(TargetOS)/*.
160+When you built via **make** , it creates a **pas2js.cfg** in _bin/$(TargetCPU)-$(TargetOS)/_.
168161
169-You can create a basic pas2js.cfg yourself by either using *tools/createconfig/createconfig* or manually:
162+You can create a basic pas2js.cfg yourself by either using _tools/createconfig/createconfig_ or manually:
170163
171164For more about pas2js.cfg see here: [[pas2js.cfg]]
172165
173166## How to use pas2js
174167
175-The command-line arguments are kept mostly the same as the FPC command-line arguments. Error messages are also in the same format.
168+The command-line arguments are kept mostly the same as the FPC command-line arguments. Error messages are also in the same format.
176169
177-The compiler needs access to all sources, and so you need to specify the path to the sources of all used units.
170+The compiler needs access to all sources, and so you need to specify the path to the sources of all used units.
178171
179-As for the FPC compiler, a configuration file is supported, which has the same syntax as the FPC config file. Note that the snapshots and svn version already contains a default pas2js.cfg with unit search paths (-Fu) for the rtl and fcl. See here how for details about the [[pas2js.cfg]].
172+As for the FPC compiler, a configuration file is supported, which has the same syntax as the FPC config file. Note that the snapshots and svn version already contains a default pas2js.cfg with unit search paths (-Fu) for the rtl and fcl. See here how for details about the [[pas2js.cfg]].
180173
181-Basically, the command is the same as any FPC command line. The only thing that is different is the target: *-Tbrowser* or *-Tnodeejs*
174+Basically, the command is the same as any FPC command line. The only thing that is different is the target: _-Tbrowser_ or _-Tnodeejs_
182175
183-Here is the complete list of [[pas2js command line arguments|command line arguments]].
176+Here is the complete list of [[pas2js command line arguments|command line arguments]].
184177
185178### for the browser
186179
187-Consider the classical:
188-
189-``` pascal
180+Consider the classical:
181+```
190182program hello;
191-
192-begin
193- Writeln('Hello, world!');
194-end.
183+
184+ begin
185+ Writeln('Hello, world!');
186+ end.
195187```
196188
197-Yes, writeln is supported. Here is how to compile it:
198-
199- pas2js -Jc -Jirtl.js -Tbrowser hello.pas
200-
201-When compiled succesfully, the code can be run in the browser by opening a html file in the browser with the following content:
189+Yes, writeln is supported. Here is how to compile it:
190+```
191+pas2js -Jc -Jirtl.js -Tbrowser hello.pas
192+```
202193
203-```html4strict
194+When compiled succesfully, the code can be run in the browser by opening a html file in the browser with the following content:
195+```
204196<html>
205- <head>
206- <meta charset="utf-8"/>
207- <script type="application/javascript" src="hello.js"></script>
208- </head>
209- <body>
210- <script type="application/javascript">
211- rtl.run();
212- </script>
213- </body>
214-</html>
197+ <head>
198+ <meta charset="utf-8"/>
199+ <script type="application/javascript" src="hello.js"></script>
200+ </head>
201+ <body>
202+ <script type="application/javascript">
203+ rtl.run();
204+ </script>
205+ </body>
206+ </html>
215207```
216208
217-The files that are needed are:
218-
219-- hello.html
220-- hello.js
209+The files that are needed are:
221210
222-Whether hello.html is opened by double-clicking it in the explorer or put on a server and opened with an URL, is not relevant for the functioning.
211+ * hello.html
212+ * hello.js
223213
224-The output is visible in the browser's web developer console. By including the browserconsole unit, it will be visible in the browser page:
214+Whether hello.html is opened by double-clicking it in the explorer or put on a server and opened with an URL, is not relevant for the functioning.
225215
226-``` pascal
216+The output is visible in the browser's web developer console. By including the browserconsole unit, it will be visible in the browser page:
217+```
227218program hello;
228-
229-uses browserconsole;
230-
231-begin
232- Writeln('Hello, world!');
233-end.
219+
220+ uses browserconsole;
221+
222+ begin
223+ Writeln('Hello, world!');
224+ end.
234225```
235226
236227### for NodeJS
237-
238- pas2js -Tnodejs hello.pas
239-
240-When compiled succesfully, the code can be run in node using the following command.
228+```
229+pas2js -Tnodejs hello.pas
230+```
241231
242- nodejs hello.js
232+When compiled succesfully, the code can be run in node using the following command.
233+```
234+nodejs hello.js
235+```
243236
244237### for modules
245238
246-Pas2js supports compiling modules:
247-
248- pas2js -Tmodule mymodule.pas
249-
250-When compiled succesfully, the code can be used in a web page using the following tag:
239+Pas2js supports compiling modules:
240+```
241+pas2js -Tmodule mymodule.pas
242+```
251243
252- <script type="module" src="mymodule.js"><script>
244+When compiled succesfully, the code can be used in a web page using the following tag:
245+```
246+<script type="module" src="mymodule.js"><script>
247+```
253248
254249More info can be found on the [[pas2js modules|Modules page]]
255250
256251## Supported syntax elements
257252
258-A significant amount of Object Pascal syntax is supported, including RTTI. A more detailed list can be found in the [[Pas2js Transpiler]] file in the sources.
253+A significant amount of Object Pascal syntax is supported, including RTTI. A more detailed list can be found in the [[Pas2js Transpiler]] file in the sources.
259254
260-- [[Mode Delphi|Delphi]] and [[Mode ObjFPC|ObjFPC]] mode
261-- [[Program]], [[Unit|Units]], namespaces
262-- [[pas2js modules|Library]] since 2.1.0
263-- unit initialization, but not finalization
264-- [[Var]], [[Const]], [[Type]]
265-- string (unicodestring), char (widechar), [[Boolean]], [[Double]], [[Byte]], [[Shortint]], [[Word]], [[Smallint]], longword, [[Longint]], nativeint(int53), nativeuint(int52), currency
266-- resourcestrings
267-- [[Pointer]] (as a reference to a class, array, record, pointer of record, interface, but no pointer arithmetic)
268-- [[Record]] (but no variant records), advanced records (since 1.3)
269-- [[Function|Functions]], [[Procedure|Procedures]], nested, anonymous functions (since 1.1)
270-- function types, of object, reference to (closures)
271-- function arguments: default, const, var, out
272-- [[If]]-then-else
273-- [[For]]-do
274-- [[Repeat]]-until
275-- [[While]]-do
276-- [[With]]-do
277-- try-finally
278-- try-except
279-- enums
280-- sets
281-- arrays static, dynamic, open, multi dimensionals, array of const
282-- String like array operations: *a:=\[1,2,3\]+\[1,1\];*
283-- class type, visibility, virtual, override, abstract, overload, properties, class properties, class var, class const, constructor, destructor, class constructor (since 1.5)
284-- class-of
285-- nested classes
286-- interfaces: CORBA, COM, delegations, method resolution, reference counting, TVirtualInterface
287-- external classes, vars, const
288-- class helpers, record helpers, type helpers (since 1.3)
289-- Enumeration for..in..do
290-- Type alias, e.g. type TTranslateString = type string;
291-- RTTI
292-- asm block for embedding JavaScript directly
293-- compiler directives (e.g. \$ifdef, \$if, \$define, \$modeswitch, \$R+)
294-- compile time and run time range checking. Overflow checking since 1.5.
295-- attributes (since 1.5)
296-- [[pas2js modules|$linklib]] directive to link [[pas2js modules|modules]].
297-- [[pas2js Generics|generics]] (since 1.5)
298-- [[pas2js resources|Resources]] (since 1.5)
255+ * [[Mode Delphi|Delphi]] and [[Mode ObjFPC|ObjFPC]] mode
256+ * [[Program]], [[Unit|Units]], namespaces
257+ * [[pas2js modules|Library]] since 2.1.0
258+ * unit initialization, but not finalization
259+ * [[Var]], [[Const]], [[Type]]
260+ * string (unicodestring), char (widechar), [[Boolean]], [[Double]], [[Byte]], [[Shortint]], [[Word]], [[Smallint]], longword, [[Longint]], nativeint(int53), nativeuint(int52), currency
261+ * resourcestrings
262+ * [[Pointer]] (as a reference to a class, array, record, pointer of record, interface, but no pointer arithmetic)
263+ * [[Record]] (but no variant records), advanced records (since 1.3)
264+ * [[Function|Functions]], [[Procedure|Procedures]], nested, anonymous functions (since 1.1)
265+ * function types, of object, reference to (closures)
266+ * function arguments: default, const, var, out
267+ * [[If]]-then-else
268+ * [[For]]-do
269+ * [[Repeat]]-until
270+ * [[While]]-do
271+ * [[With]]-do
272+ * try-finally
273+ * try-except
274+ * enums
275+ * sets
276+ * arrays static, dynamic, open, multi dimensionals, array of const
277+ * String like array operations: _a:=[1,2,3]+[1,1];_
278+ * class type, visibility, virtual, override, abstract, overload, properties, class properties, class var, class const, constructor, destructor, class constructor (since 1.5)
279+ * class-of
280+ * nested classes
281+ * interfaces: CORBA, COM, delegations, method resolution, reference counting, TVirtualInterface
282+ * external classes, vars, const
283+ * class helpers, record helpers, type helpers (since 1.3)
284+ * Enumeration for..in..do
285+ * Type alias, e.g. type TTranslateString = type string;
286+ * RTTI
287+ * extended RTTI (since 3.1)
288+ * asm block for embedding JavaScript directly
289+ * compiler directives (e.g. $ifdef, $if, $define, $modeswitch, $R+)
290+ * compile time and run time range checking. Overflow checking since 1.5.
291+ * attributes (since 1.5)
292+ * [[pas2js modules|$linklib]] directive to link [[pas2js modules|modules]].
293+ * [[pas2js Generics|generics]] (since 1.5)
294+ * [[pas2js resources|Resources]] (since 1.5)
299295
300-There are some constructs that are naturally not supported and will never be supported:
296+There are some constructs that are naturally not supported and will never be supported:
301297
302-- Anything involving memory pointers and pointer arithmetic.
303-- Variant records
298+ * Anything involving memory pointers and pointer arithmetic.
299+ * Variant records
304300
305-And there are some extra constructs unique to pas2js:
301+And there are some extra constructs unique to pas2js:
306302
307-- **JSValue** - a base type representing a JavaScript value.
308-- function Str(X\[,Y,Z...\]): string - same as procedure Str, except as a function
309-- [[pas2js AsyncAWait|async/await]]
303+ * **JSValue** \- a base type representing a JavaScript value.
304+ * function Str(X[,Y,Z...]): string - same as procedure Str, except as a function
305+ * [[pas2js AsyncAWait|async/await]]
310306
311307## Planned language features
312308
313-Basically, the idea is to get the pas2js transpiler up to the same level as FPC or Delphi. That means the following needs to be added:
309+Basically, the idea is to get the pas2js transpiler up to the same level as FPC or Delphi. That means the following needs to be added:
314310
315-- Extended RTTI
316-- Operator Overloading
317-- anonymous records, e.g. *var a: array of record w: word; end;*
318-- [[Pas2js Package|Packages]]
311+ * Operator Overloading
312+ * Anonymous records, e.g. _var a: array of record w: word; end;_
313+ * [[Pas2js Package|Packages]]
314+ * RTTI for local types
315+ * Array of class-interfaces
316+ * Generics distinguished by number of templates, e.g. TProc<T>, TProc<S,T>
319317
320-Needless to say, anything requiring direct memory access is not going to be supported.
318+Needless to say, anything requiring direct memory access is not going to be supported.
321319
322320## Other unimplemented features
323321
324-- Array of interface
325-- Enums with custom values
326-- Finalization sections, class destructors
327-- Futures
328-- Global properties
329-- Inline
330-- Library
331-- Objects
332-- Pointer arithmetic
333-- Record field interface
334-- Variants
322+ * Array of interface
323+ * Enums with custom values
324+ * Finalization sections, class destructors
325+ * Futures
326+ * Global properties
327+ * Inline
328+ * Library
329+ * Objects
330+ * Pointer arithmetic
331+ * Record field interface
332+ * Variants
335333
336334## Lazarus integration of pas2js
337335
338-Lazarus understands the concept of external classes as used by pas2js, so code completion will work.
336+Lazarus understands the concept of external classes as used by pas2js, so code completion will work.
339337
340-Since Lazarus 1.9 the IDE can use pas2js.exe as a normal compiler.
338+Since Lazarus 1.9 the IDE can use pas2js.exe as a normal compiler.
341339
342-The integration is described here: [[lazarus pas2js integration]]. It is still under construction, but deep integration with lazarus is planned.
340+The integration is described here: [[lazarus pas2js integration]]. It is still under construction, but deep integration with lazarus is planned.
343341
344342## Importing Javascript classes
345343
346-To import a javascript class, one writes a normal class definition that mimics the Javascript class. It is possible to use properties. Many examples can be found in the JS, web, nodejs and libjquery units.
347-
348-Here is a simple example:
344+To import a javascript class, one writes a normal class definition that mimics the Javascript class. It is possible to use properties. Many examples can be found in the JS, web, nodejs and libjquery units.
349345
350-``` pascal
351- TJSFunction = class external name 'Function'(TJSObject)
352- private
353- Flength: NativeInt external name 'length';
354- Fprototyp: TJSFunction external name 'prototyp';
355- public
356- name: String;
357- property prototyp: TJSFunction read Fprototyp;
358- property length: NativeInt read Flength;
359- function apply(thisArg: TJSObject; const ArgArray: TJSValueDynArray): JSValue; varargs;
360- function bind(thisArg: TJSObject): JSValue; varargs;
361- function call(thisArg: TJSObject): JSValue; varargs;
362- end;
346+Here is a simple example:
363347```
348+TJSFunction = class external name 'Function'(TJSObject)
349+ private
350+ Flength: NativeInt external name 'length';
351+ Fprototyp: TJSFunction external name 'prototyp';
352+ public
353+ name: String;
354+ property prototyp: TJSFunction read Fprototyp;
355+ property length: NativeInt read Flength;
356+ function apply(thisArg: TJSObject; const ArgArray: TJSValueDynArray): JSValue; varargs;
357+ function bind(thisArg: TJSObject): JSValue; varargs;
358+ function call(thisArg: TJSObject): JSValue; varargs;
359+ end;
360+```
364361
365-This declares the `TJSFunction` object : in Javascript, functions are objects.
362+This declares the `TJSFunction` object : in Javascript, functions are objects.
366363
367-- The "`external name 'Function'`" means that you declare a Javascript class where the Javascript name of the class is 'Function'.
368-- The `(TJSObject)` means it descends from `TJSObject` also an external class. There does not need to be an ancestor type.
369-- Fields are declared just as in Pascal.
370-- To declare read-only fields, a trick can be used: declare the field using an external name "thename" modifier, and declare a read-only property with the same name.\
371- (see the length declaration)
372-- `Varargs` can be used to indicate that a function accepts any number of arguments.
373-- `JSValue` can be used to indicate an unknown type.\
374- It is more or less equivalent to a Variant.
364+ * The "`external name 'Function'`" means that you declare a Javascript class where the Javascript name of the class is 'Function'.
365+ * The `(TJSObject)` means it descends from `TJSObject` also an external class. There does not need to be an ancestor type.
366+ * Fields are declared just as in Pascal.
367+ * To declare read-only fields, a trick can be used: declare the field using an external name "thename" modifier, and declare a read-only property with the same name.
368+(see the length declaration)
369+ * `Varargs` can be used to indicate that a function accepts any number of arguments.
370+ * `JSValue` can be used to indicate an unknown type.
371+It is more or less equivalent to a Variant.
375372
376373## Create simple JS objects with the new function
377374
378-Some JS-framework functions expect an JS object as parameter. Here is how to do that in Pascal using the *new* function from unit *JS*:
379-
380-``` javascript
375+Some JS-framework functions expect an JS object as parameter. Here is how to do that in Pascal using the _new_ function from unit _JS_ :
376+```
381377// JavaScript:
382-DoIt({name:"Fred", id:3, size:4.3});
378+ DoIt({name:"Fred", id:3, size:4.3});
383379```
384-
385-``` pascal
380+```
386381// Pascal;
387-DoIt(new(['name','Fred', 'id',3, 'size',4.3]));
382+ DoIt(new(['name','Fred', 'id',3, 'size',4.3]));
388383```
389384
390-You can nest it to create sub objects:
391-
392-``` javascript
385+You can nest it to create sub objects:
386+```
393387// JavaScript:
394-DoIt({name:"Fred", size:{width:3,height:2}});
388+ DoIt({name:"Fred", size:{width:3,height:2}});
395389```
396-
397-``` pascal
390+```
398391// Pascal;
399-DoIt(new(['name','Fred', 'size',new(['width',3, 'height',2])]));
392+ DoIt(new(['name','Fred', 'size',new(['width',3, 'height',2])]));
400393```
401394
402-You can use TJSArray._of to create JS arrays on the fly:
403-
404-``` javascript
395+You can use TJSArray._of to create JS arrays on the fly:
396+```
405397// JavaScript:
406-DoIt({numbers:[1,2,3]});
398+ DoIt({numbers:[1,2,3]});
407399```
408-
409-``` pascal
400+```
410401// Pascal;
411-DoIt(new(['numbers',TJSArray._of(1,2,3)]));
402+ DoIt(new(['numbers',TJSArray._of(1,2,3)]));
412403```
413404
414405## Resource strings
415406
416-The pas2js transpiler can generate a JSON file (extension *.jrs*) with all the resource strings in your program.
417-
418-This is a quite simple file. A JSON object exists for every unit, with each json property a resource string.
407+The pas2js transpiler can generate a JSON file (extension _.jrs_) with all the resource strings in your program.
419408
420- {
421- "trs2" : {
422- "ResUsed" : "This resourcestring is used",
423- "ResUnUsed" : "This resourcestring is not used",
424- "ImplResUsed" : "This implementation resourcestring is used"
409+This is a quite simple file. A JSON object exists for every unit, with each json property a resource string.
410+```
411+{
412+ "trs2" : {
413+ "ResUsed" : "This resourcestring is used",
414+ "ResUnUsed" : "This resourcestring is not used",
415+ "ImplResUsed" : "This implementation resourcestring is used"
425416 },
426- "trs1" : {
427- "MyString" : "The very nice string we will need to translate"
417+ "trs1" : {
418+ "MyString" : "The very nice string we will need to translate"
428419 }
429420 }
430-
431-This file can be translated, and the translation file can be loaded using the **rstranslate** unit, part of the rtl. There are demo programs which demonstrate the use of this feature.
421+```
432422
433-The generating of this file is controlled by the **-Jr** option. It can take 3 possible arguments:
423+This file can be translated, and the translation file can be loaded using the **rstranslate** unit, part of the rtl. There are demo programs which demonstrate the use of this feature.
434424
435-1. *none* This is the default, no file is generated.
436-2. *unit* one file per compiled unit will be generated. This file will contain all resource strings of the unit.
437-3. *program* one file is generated for the main file. This fill will contain all used resource strings for the main file and all the units it uses.
425+The generating of this file is controlled by the **-Jr** option. It can take 3 possible arguments:
438426
439-If you compile a program, then the *program* option will generate a file with all the used resource strings in your program.
427+ 1. _none_ This is the default, no file is generated.
428+ 2. _unit_ one file per compiled unit will be generated. This file will contain all resource strings of the unit.
429+ 3. _program_ one file is generated for the main file. This fill will contain all used resource strings for the main file and all the units it uses.
440430
441-The above example was generated using the command:
431+If you compile a program, then the _program_ option will generate a file with all the used resource strings in your program.
442432
443- pas2js -Jrprogram trs1.pp -B
433+The above example was generated using the command:
434+```
435+pas2js -Jrprogram trs1.pp -B
436+```
444437
445-Note that the format is different from the format used by FPC:
438+Note that the format is different from the format used by FPC:
446439
447-- Identifiers in the file are case sensitive: the names must be typed as they appear in the source file.
448-- The strings are grouped per unit, this allows to load them fasters
449-- The hash and bytes parts are missing, they make little sense in a Javascript context.
440+ * Identifiers in the file are case sensitive: the names must be typed as they appear in the source file.
441+ * The strings are grouped per unit, this allows to load them fasters
442+ * The hash and bytes parts are missing, they make little sense in a Javascript context.
450443
451444## Exceptions
452445
453-Exceptions are translated to actual Javascript exceptions. The rtl.js has several mechanisms to deal with uncaught exceptions. The basic mechanism is setting the *showUncaughtExceptions* to true before calling rtl.run() in your html file:
454-
455- <script type="application/javascript">
446+Exceptions are translated to actual Javascript exceptions. The rtl.js has several mechanisms to deal with uncaught exceptions. The basic mechanism is setting the _showUncaughtExceptions_ to true before calling rtl.run() in your html file:
447+```
448+<script type="application/javascript">
456449 rtl.showUncaughtExceptions=true;
457450 rtl.run();
458451 </script>
452+```
459453
460-the browser will then use a *window.alert()* to show uncaught exceptions.
454+the browser will then use a _window.alert()_ to show uncaught exceptions.
461455
462456More explanations can be found in [[pas2js exceptions]]
463457
464458## Debugging
465459
466-The generated Javascript source code is of course visible and debuggable in the browser.
467-
468-Moreover, the transpiler can generate a source map, which means that you will be able to see and debug the Pascal code in the browser. (not everything will work, but many things do. This depends on the browser too.)
469-
470-A source map can be generated using the command-line parameter:
471-
472- -Jm
473-
474-The easiest is to include the Pascal sources in the source map:
460+The generated Javascript source code is of course visible and debuggable in the browser.
475461
476- -Jminclude
462+Moreover, the transpiler can generate a source map, which means that you will be able to see and debug the Pascal code in the browser. (not everything will work, but many things do. This depends on the browser too.)
477463
478-By default all source filenames are relative to .js.map. You can tell the compiler to store all file names relative to a specific local base directory:
464+A source map can be generated using the command-line parameter:
465+```
466+-Jm
467+```
479468
480- -Jmbasedir=DirName
469+The easiest is to include the Pascal sources in the source map:
470+```
471+-Jminclude
472+```
481473
482-And you can store an URL in the map, so the browser will use URL/above-relative-file-name to get the source:
474+By default all source filenames are relative to .js.map. You can tell the compiler to store all file names relative to a specific local base directory:
475+```
476+-Jmbasedir=DirName
477+```
483478
484- -Jmsourceroot=URL
479+And you can store an URL in the map, so the browser will use URL/above-relative-file-name to get the source:
480+```
481+-Jmsourceroot=URL
482+```
485483
486484## Porting from FPC/Delphi
487485
488-See [[Porting from FPC/Delphi to pas2js|here]] for tips and traps porting code from FPC and Delphi.
486+See [[Porting from FPC/Delphi to pas2js|here]] for tips and traps porting code from FPC and Delphi.
489487
490-Delphi cannot parse some of the constructs that exist in *pas2js* (namely: external classes). You can create stub declarations suitable for the Delphi parser with the [[pas2js makestub|stub creator]].
488+Delphi cannot parse some of the constructs that exist in _pas2js_ (namely: external classes). You can create stub declarations suitable for the Delphi parser with the [[pas2js makestub|stub creator]].
491489
492490## Bugs
493491
494-Please report bugs in the [FPC Bug Tracker](https://bugs.freepascal.org) with category *pas2js*.
492+Please report bugs in the [FPC Bug Tracker](<https://bugs.freepascal.org>) with category _pas2js_.
495493
496494## Examples
497495
498-- Time Tracking Application: <https://www.devstructor.com/demos/pas2js-time/source.zip> (sources: <https://www.devstructor.com/demos/pas2js-time/source.zip>)
499-- Drawing and animation on canvas: <http://ragnemalm.se/images/santa/santa.html> (sources: <http://ragnemalm.se/images/santa/>)
500-- WebGL: <https://github.com/genericptr/Pas2JS-WebGL#pas2js-webgl>
501-- Allegro Web Game: <https://lainz.github.io/AllegroPas2JS-Demo-Game/index.html> (sources: <https://github.com/lainz/AllegroPas2JS-Demo-Game>)
502-- Pas2JS (freepascal) Compiler Tutorials on RetroNick's Programming Channel: <https://youtube.com/playlist?list=PLdLUbKp49uouo9UMrBDvXIoJx6j084_ef>
496+ * Time Tracking Application: <https://www.devstructor.com/demos/pas2js-time/source.zip> (sources: <https://www.devstructor.com/demos/pas2js-time/source.zip>)
497+ * Drawing and animation on canvas: <http://ragnemalm.se/images/santa/santa.html> (sources: <http://ragnemalm.se/images/santa/>)
498+ * WebGL: <https://github.com/genericptr/Pas2JS-WebGL#pas2js-webgl>
499+ * Allegro Web Game: <https://lainz.github.io/AllegroPas2JS-Demo-Game/index.html> (sources: <https://github.com/lainz/AllegroPas2JS-Demo-Game>)
500+ * Pas2JS (freepascal) Compiler Tutorials on RetroNick's Programming Channel: <https://youtube.com/playlist?list=PLdLUbKp49uouo9UMrBDvXIoJx6j084_ef>
503501
504502## Lazarus Widgetset
505503
506504The ultimate goal is of course to have the LCL running in the web. Discussions on this topic are delegated to a separate page. [[pas2js widgetsets]]
507505
508506## FAQ
509507
510508### Why is a simple hello world program so big?
511509
512-This is mainly due to the used rtl.js. The rtl.js contains code for Pascal modules, classes, RTTI, sets, range checks, etc and is written with big WebApps in mind, not for scripts with a few lines of code.
510+This is mainly due to the used rtl.js. The rtl.js contains code for Pascal modules, classes, RTTI, sets, range checks, etc and is written with big WebApps in mind, not for scripts with a few lines of code.
513511
514-1. You can use a Javascript [[pas2js minifier|minifier]] to reduce the created Javascript
515-2. You can create your own minified rtl.js by removing all functions you don't need. Eventually this will be done automatically by pas2js.
512+ 1. You can use a Javascript [[pas2js minifier|minifier]] to reduce the created Javascript
513+ 2. You can create your own minified rtl.js by removing all functions you don't need. Eventually this will be done automatically by pas2js.
516514
517515### Why are asm blocks bad?
518516
519-Asm blocks are useful for things you cannot do with pas2js. But there are some downsides: pas2js does not parse the JS. Neither does it check the syntax, nor does it know what Pascal identifiers the code is referencing. That means any identifier only accessed by the asm block will be removed by the pas2js' optimizer.
517+Asm blocks are useful for things you cannot do with pas2js. But there are some downsides: pas2js does not parse the JS. Neither does it check the syntax, nor does it know what Pascal identifiers the code is referencing. That means any identifier only accessed by the asm block will be removed by the pas2js' optimizer.
520518
521-Therefore always try to do it in Pascal. Remember you can typecast values to JSValue, objects to TJSObject, arrays to TJSArray, strings to TJSString, etc to use almost all JS features.
519+Therefore always try to do it in Pascal. Remember you can typecast values to JSValue, objects to TJSObject, arrays to TJSArray, strings to TJSString, etc to use almost all JS features.
522520
523521### Why not parse asm blocks?
524522
525-Any compiletime JS parser can only do a syntax check and parse only simple JS. But since simple JS can be better written in Pascal, it is somewhat pointless and has therefore low priority.
523+Any compiletime JS parser can only do a syntax check and parse only simple JS. But since simple JS can be better written in Pascal, it is somewhat pointless and has therefore low priority.
526524
527525### What about optimization X?
528526
529527See here for [[Pas2js optimizations]]
530528
531529## See also
532530
533-- [[pas2js html2form]] - a small tool that converts an HTML file to a Pascal class.
531+ * [[pas2js html2form]] \- a small tool that converts an HTML file to a Pascal class.
532+ * [[pas2js designtime]]
533+ * [[lazarus pas2js integration]]
534+ * [[Pas2JS Version Changes|Pas2JS version changes]]
534535
535536---
536-*From the Free Pascal wiki, page "pas2js", as it stood on 2024-02-21 after a change by Michael, 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).*
537+*From the Free Pascal wiki, page "pas2js", as the Internet Archive kept it on 2026-01-04, last changed on 2025-07-22. 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).*

Back to history