translation.html 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>pas2js - Translation of Pascal (Delphi/FPC) programs to JavaScript</title>
  4. <meta name="description" content="Translation of Pascal (Delphi/FPC) programs to JavaScript">
  5. <meta name="keywords" content="translation,program,Delphi,Pascal,javascript,pas2js">
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <style type="text/css">
  8. body {
  9. padding: 20px;
  10. margin-left: 20px;
  11. }
  12. table.sample th {
  13. background-color: #cccccc;
  14. font: 120% gelvetica,arial,tahoma;
  15. }
  16. table.sample pre {
  17. color: blue;
  18. }
  19. table.sample td {
  20. padding-left: 20px;
  21. padding-right: 20px;
  22. }
  23. .section {
  24. padding-bottom: 1em;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="head">
  30. </div>
  31. <div class="section">
  32. <h2>Overview</h2>
  33. <a href="#about">About pas2js</a><br>
  34. <a href="#commandlineparameters">Command line parameters</a><br>
  35. <a href="#mode">Delphi and ObjFPC mode</a><br>
  36. <a href="#modules">Translating modules</a><br>
  37. <a href="#variables">Translating variables</a><br>
  38. <a href="#string">Translating string</a><br>
  39. <a href="#resourcestrings">Translating resourcestrings</a><br>
  40. <a href="#types">Translating types</a><br>
  41. <a href="#pointer">Translating pointer</a><br>
  42. <a href="#record">Translating record</a><br>
  43. <a href="#functions">Translating functions</a><br>
  44. <a href="#passbyref">Translating passing a parameter by reference</a><br>
  45. <a href="#nested functions">Translating nested functions</a><br>
  46. <a href="#forloop">Translating for-loop</a><br>
  47. <a href="#repeatuntil">Translating repeat..until</a><br>
  48. <a href="#whiledo">Translating while..do</a><br>
  49. <a href="#withdo">Translating with..do</a><br>
  50. <a href="#enums">Translating enums</a><br>
  51. <a href="#sets">Translating sets</a><br>
  52. <a href="#array">Translating array type</a><br>
  53. <a href="#class">Translating class type</a><br>
  54. <a href="#classof">Translating class-of type</a><br>
  55. <a href="#tobjectfree">Translating TObject.Free</a><br>
  56. <a href="#classinterfaces">Translating class interfaces</a><br>
  57. <a href="#attributes">Translating attributes</a><br>
  58. <a href="#tryfinally">Translating try..finally</a><br>
  59. <a href="#tryexcept">Translating try..except</a><br>
  60. <a href="#functiontype">Translating function types</a><br>
  61. <a href="#calljavascript">Calling JavaScript from Pascal</a><br>
  62. <a href="#asm">The asm block</a><br>
  63. <a href="#assembler">The procedure modifier assembler</a><br>
  64. <a href="#externalproc">The procedure modifier external</a><br>
  65. <a href="#varargs">The procedure modifier varargs</a><br>
  66. <a href="#externalvar">The var modifier external</a><br>
  67. <a href="#externalmembers">The external modifier of class members</a><br>
  68. <a href="#externalclass">External classes</a><br>
  69. <a href="#externalclassancestor">External class as ancestor</a><br>
  70. <a href="#jsvalue">The JSValue type</a><br>
  71. <a href="#bracketaccessor">Accessing JS object properties with the bracket accessor</a><br>
  72. <a href="#rtti">RTTI - Run Time Type Information</a><br>
  73. <a href="#compilerdirectives">Compiler directives</a><br>
  74. <a href="#othersupportedelements">Other supported Pascal elements</a><br>
  75. <a href="#notsupportedelements">Not supported elements</a><br>
  76. <a href="#sourcemaps">Creating source maps</a><br>
  77. </div>
  78. <div class="section">
  79. <h2 id="about">About pas2js</h2>
  80. pas2js is a compiler/transpiler to translate programs written in Pascal (subset of Delphi/ObjFPC syntax)
  81. to JavaScript.<br>
  82. The goal is to use strong typing, while still be able to use low level whenever you choose.<br>
  83. The compiled Pascal functions can be used in DOM events or called by JavaScript.<br>
  84. pas2js is written completely in FPC, runs on many platforms like Windows, Mac and Linux and more.
  85. It is built modular consisting of the following parts:
  86. <ul>
  87. <li>file cache - loading, caching files, converting to UTF-8</li>
  88. <li>file resolver - handling search paths, finding used units and include files</li>
  89. <li>scanner - reading tokens, handling compiler directives like $IfDef and $Include</li>
  90. <li>parser - reading the tokens, checking syntax, creating Pascal nodes</li>
  91. <li>resolver - resolving references, type checking and checking duplicate identifiers</li>
  92. <li>use analyzer - finding unused identifiers, emit hints and warning</li>
  93. <li>converter - translating Pascal nodes into JavaScript nodes</li>
  94. <li>compiler - handling config files, parameters, compiling recursively all used units, writes js</li>
  95. <li>command line interface - a small wrapper to embed the compiler into a console program</li>
  96. <li>library and interface - a small wrapper to embed the compiler into a library</li>
  97. </ul>
  98. Each part is tested separately and is used by other FPC tools as well. For example
  99. the scanner and parser are used by fpdoc too. Thus they are tested and extended by other
  100. programmers, reducing greatly the work for developing pas2js. Consistency is kept by
  101. several test suites, containing thousands of tests.
  102. </div>
  103. <div class="section">
  104. Note: The modular structure allows to compile any parts or the whole compiler into an IDE addon (not yet started).
  105. </div>
  106. <div class="section">
  107. <h2 id="commandlineparameters">Command line parameters</h2>
  108. Most parameters work the same as their FPC equivalent. pas2js has some options of its own (see -J options).
  109. <pre>
  110. Usage: pas2js &lt;your.pas&gt;
  111. Options:
  112. Put + after a boolean switch option to enable it, - to disable it
  113. @&lt;x&gt; : Read compiler options from file &lt;x&gt; in addition to the default pas2js.cfg
  114. -B : Rebuild all
  115. -d&lt;x&gt; : Defines the symbol &lt;x&gt;. Optional: -d&lt;x&gt;:=&lt;value&gt;
  116. -i&lt;x&gt; : Write information and halt. &lt;x&gt; is a combination of the following letters:
  117. V : Write short compiler version
  118. W : Write full compiler version
  119. -F... Set file names and paths:
  120. -Fe&lt;x&gt; : Redirect output to &lt;x&gt;
  121. -Fi&lt;x&gt; : Add &lt;x&gt; to include paths
  122. -Fu&lt;x&gt; : Add &lt;x&gt; to unit paths
  123. -FU&lt;x&gt; : Set unit output path to &lt;x&gt;
  124. -I&lt;x&gt; : Add &lt;x&gt; to include paths, same as -Fi
  125. -J... Extra options of pas2js
  126. -Jc : Write all JavaScript concatenated into the output file
  127. -Ji&lt;x&gt; : Insert JS file &lt;x&gt; into main JS file. E.g. -Jirtl.js. Can be given multiple times.
  128. -Jl : lower case identifiers
  129. -Jm : generate source maps
  130. -Jmsourceroot=&lt;x&gt; : use x as "sourceRoot", prefix URL for source file
  131. names.
  132. -Jmbasedir=&lt;x&gt; : write source file names relative to directory x.
  133. -Jminclude : include Pascal sources in source map.
  134. -Jm- : disable generating source maps
  135. -Ju&lt;x&gt; : Add &lt;x&gt; to foreign unit paths. Foreign units are not compiled.
  136. -Je&lt;x&gt; : Encode messages as &lt;x&gt;.
  137. -Jeconsole : Console codepage. Default.
  138. -Jesystem : System codepage. On non Windows console and system are the same.
  139. -Jeutf-8 : Unicode UTF-8. Default when using -Fe.
  140. -l : Write logo
  141. -MDelphi: Delphi 7 compatibility mode
  142. -MObjFPC: FPC's Object Pascal compatibility mode (default)
  143. -NS&lt;x&gt; : add &lt;x&gt; to namespaces. Namespaces with trailing - are removed.
  144. Delphi calls this flag "unit scope names".
  145. -n : Do not read the default config files
  146. -o&lt;x&gt; : Change main JavaScript file to &lt;x&gt;, "." means stdout
  147. -O&lt;x&gt; : Optimizations:
  148. -O- : Disable optimizations
  149. -O1 : Level 1 optimizations (quick and debugger friendly)
  150. -Oo&lt;x&gt; : Enable or disable optimization. The x is case insensitive:
  151. -OoEnumNumbers[-] : write enum values as number instead of name. Default in -O1.
  152. -OoRemoveNotUsedPrivates[-] : Default is enabled
  153. -OoRemoveNotUsedDeclarations[-] : Default enabled for programs with -Jc
  154. -OoRemoveNotUsedPublished[-] : Default is disabled
  155. -P&lt;x&gt; : Set target processor. Case insensitive:
  156. -Pecmascript5 : default
  157. -Pecmascript6
  158. -S&lt;x&gt; : Syntax options. &lt;x&gt; is a combination of the following letters:
  159. c : Support operators like C (*=,+=,/= and -=)
  160. d : Same as -Mdelphi
  161. 2 : Same as -Mobjfpc (default)
  162. -T&lt;x&gt; : Set target platform, case insensitive.
  163. -Tbrowser : default
  164. -Tnodejs : add pas.run(), includes -Jc
  165. -u&lt;x&gt; : Undefines the symbol &lt;x&gt;
  166. -v&lt;x&gt; : Be verbose. &lt;x&gt; is a combination of the following letters:
  167. e : show errors (default)
  168. w : show warnings
  169. n : show notes
  170. h : show hints
  171. i : show info
  172. l : show line numbers
  173. a : show everything
  174. 0 : show nothing (except errors)
  175. b : show file names with full path
  176. c : show conditionals
  177. t : show tried/used files
  178. d : show debug notes and info, enables -vni
  179. q : show message numbers
  180. x : show used tools
  181. -vm&lt;x&gt;,&lt;y&gt;: Do not show messages numbered &lt;x&gt; and &lt;y&gt;.
  182. -? : Show this help
  183. -h : Show this help
  184. </pre>
  185. </div>
  186. <div class="section">
  187. <h2 id="mode">Delphi and ObjFPC mode</h2>
  188. <h3>Delphi mode</h3>
  189. <ul>
  190. <li>Defines macro <i>DELPHI</i></li>
  191. <li>Assigning a function to a function type variable does not require the @ operator.
  192. For example, you can write either <i>OnGetThing:=GetValue;</i> or <i>OnGetThing:=@GetValue;</i>.</li>
  193. <li>A function type variable reference without brackets is treated as a call.
  194. For example: If <i>OnGetThing</i> is a variable of type <i>function: integer</i>
  195. you can write: <i>If OnGetThing=3 then ;</i>.</li>
  196. <li>You must use the @@ operator to get the procedure address (i.e. JS reference) of a procedure type variable.
  197. For example instead of <i>If OnClick=nil then ;</i> you must use <i>if @@OnClick=nil then ;</i>.</li>
  198. <li>Every procedure/method overload needs the 'overload' modifier.</li>
  199. </ul>
  200. <h3>ObjFPC mode</h3>
  201. This the default mode of pas2js and is generally more strict than the Delphi mode, and allows some more operations.
  202. <ul>
  203. <li>Defines macro <i>OBJFPC</i></li>
  204. <li>Assigning a function to a function type variable requires the @ operator.
  205. For example: <i>OnGetThing:=@GetValue;</i>.</li>
  206. <li>A function type variable always needs brackets to be called.
  207. For example: If <i>OnGetThing</i> is a variable of type <i>function: integer</i>
  208. then this is allowed: <i>If OnGetThing()=3 then ;</i>.
  209. While this gives an error: <i>If OnGetThing=3 then ;</i>.</li>
  210. <li>You can compare a procedure type with <i>nil</i>.
  211. For example <i>If OnClick=nil then ;</i>.</li>
  212. <li>You can compare a procedure type with a procedure address (i.e. JS reference).
  213. For example <i>If OnClick=@OnFormClick then ;</i>.</li>
  214. <li>The procedure modifier 'overload' can be omitted when all overloads are
  215. in one scope, e.g. a unit or a class. And if one procedure has such modifier
  216. all procedures with same name and in same scope are overloads as well.</li>
  217. </ul>
  218. </div>
  219. <div class="section">
  220. <h2 id="modules">Translating modules</h2>
  221. A Pascal Program is translated into the following JavaScript structure:
  222. <table class="sample">
  223. <tbody>
  224. <tr>
  225. <th>Pascal</th>
  226. <th>JavaScript Structure, not code!</th>
  227. </tr>
  228. <tr>
  229. <td>
  230. <pre>Program &lt;unitname&gt;;
  231. Implementation
  232. [implementation section]
  233. Begin
  234. [main code]
  235. End.
  236. </pre>
  237. </td>
  238. <td>
  239. <pre>pas.&lt;program&gt;={
  240. [implementation section],
  241. $main: function() {
  242. [main code]
  243. }
  244. };
  245. </pre>
  246. </td>
  247. </tr>
  248. </tbody>
  249. </table>
  250. </div>
  251. <div class="section">
  252. A Pascal Unit is translated into the following JavaScript structure:
  253. <table class="sample">
  254. <tbody>
  255. <tr>
  256. <th>Pascal</th>
  257. <th>JavaScript Structure, not code!</th>
  258. </tr>
  259. <tr>
  260. <td>
  261. <pre>Unit &lt;unitname&gt;;
  262. Interface
  263. [interface section]
  264. Implementation
  265. [implementation section]
  266. Initialization
  267. [initialization section]
  268. End.
  269. </pre>
  270. </td>
  271. <td>
  272. <pre>pas.&lt;unitname&gt;={
  273. [interface section],
  274. $impl: {
  275. [implementation section],
  276. },
  277. $init: function() {
  278. [initialization section]
  279. }
  280. };
  281. </pre>
  282. </td>
  283. </tr>
  284. </tbody>
  285. </table>
  286. Note: The <b>finalization</b> section is not supported by pas2js.<br>
  287. </div>
  288. <div class="section">
  289. To create and initialize the units in topological order the compiler translates
  290. an Unit to the following JavaScript code:
  291. <table class="sample">
  292. <tbody>
  293. <tr>
  294. <th>Pascal</th>
  295. <th>JavaScript</th>
  296. </tr>
  297. <tr>
  298. <td>
  299. <pre>Unit &lt;unitname&gt;;
  300. Interface
  301. [interface section]
  302. Implementation
  303. [implementation section]
  304. Initialization
  305. [initialization section]
  306. End.
  307. </pre>
  308. </td>
  309. <td>
  310. <pre>rtl.module('&lt;unitname&gt;',
  311. ['system',...other used units of the interface section...],
  312. function(){
  313. [interface section]
  314. this.$init=function(){
  315. [initialization section]
  316. };
  317. },
  318. [...used units of the implementation section],
  319. function(){
  320. [implementation section]
  321. }};
  322. </pre>
  323. </td>
  324. </tr>
  325. </tbody>
  326. </table>
  327. </div>
  328. <div class="section">
  329. Here is a more detailed example to make it more clear:
  330. <table class="sample">
  331. <tbody>
  332. <tr>
  333. <th>Pascal</th>
  334. <th>JavaScript</th>
  335. </tr>
  336. <tr>
  337. <td>
  338. <pre>Unit MyModule;
  339. Interface
  340. Uses Sysutils;
  341. var
  342. dIntf: double;
  343. sIntf: string = 'abc';
  344. procedure MyIntfProc;
  345. Implementation
  346. Uses Classes;
  347. Var dImpl:double;
  348. Procedure MyIntfProc;
  349. Begin
  350. dImpl:=dIntf;
  351. End;
  352. Procedure MyImplProc;
  353. Begin
  354. dImpl:=dIntf;
  355. End;
  356. Initialization
  357. End.
  358. </pre>
  359. </td>
  360. <td>
  361. <pre>rtl.module("MyModule",
  362. ["System","SysUtils"],
  363. function(){
  364. var $mod = this;
  365. var $impl = $mod.$impl;
  366. this.dIntf = 0.0;
  367. this.sIntf = "abc";
  368. this.MyIntfProc = function(){
  369. $impl.dImpl = $mod.dIntf;
  370. };
  371. this.$init = function() {
  372. };
  373. },
  374. ["Classes"],
  375. function(){
  376. var $mod = this;
  377. var $impl = $mod.$impl;
  378. $impl.dImpl = 0.0;
  379. $impl.MyImplProc = function() {
  380. $impl.dImpl = $mod.dIntf;
  381. };
  382. });
  383. </pre>
  384. </td>
  385. </tr>
  386. </tbody>
  387. </table>
  388. Notes:
  389. <ul>
  390. <li>Unit <i>System</i> is always loaded implicitely.</li>
  391. <li>References to other units are translated to full path. For example
  392. <i>TObject</i> is translated to <i>pas.system.TObject</i></li>
  393. <li>References to dotted unitnames, aka units with namespaces are translated
  394. to <i>pas["namespace.unitname"]</i>.</li>
  395. </ul>
  396. </div>
  397. <div class="section">
  398. <h2 id="variables">Translating variables</h2>
  399. Variables are converted without type, because JavaScript lacks a clear type.
  400. They are however always initialized, which helps JavaScript engines to optimize.
  401. <table class="sample">
  402. <tbody>
  403. <tr>
  404. <th>Pascal</th>
  405. <th>JavaScript</th>
  406. </tr>
  407. <tr>
  408. <td>
  409. <pre>Unit MyModule;
  410. Interface
  411. Uses Classes,Forms;
  412. const
  413. c1:integer=3;
  414. c2 = 'abc';
  415. c3 = 234;
  416. c4 = 12.45;
  417. c5 = nil;
  418. var
  419. v1:string;
  420. v2,v3:double;
  421. v4:byte=0;
  422. v5:TForm;
  423. v6:TIdentMapEntry;
  424. v7:string='abcäöü';
  425. v8:char='c';
  426. v9:array of byte;
  427. Implementation
  428. End.
  429. </pre>
  430. </td>
  431. <td>
  432. <pre>rtl.module("MyModule",
  433. ["System","Classes","Forms"],
  434. function(){
  435. this.c1 = 3;
  436. this.c2 = "abc";
  437. this.c3 = 234;
  438. this.c4 = 12.45;
  439. this.c5 = null;
  440. this.v1 = "";
  441. this.v2 = 0.0;
  442. this.v3 = 0.0;
  443. this.v4 = 0;
  444. this.v5 = null;
  445. this.v6 = new pas.Classes.TIdentMapEntry();
  446. this.v7 = "abcäöü";
  447. this.v8 = "c";
  448. this.v9 = [];
  449. },
  450. []);
  451. </pre>
  452. </td>
  453. </tr>
  454. </tbody>
  455. </table>
  456. Notes:
  457. <ul>
  458. <li>Type casting a <i>boolean</i> to <i>integer</i>, gives <i>0</i> for <i>false</i> and <i>1</i> for <i>true</i>.</li>
  459. <li>Type casting an <i>integer</i> to <i>boolean</i>, gives <i>false</i> for <i>0</i> and <i>true</i> otherwise.</li>
  460. <li>A <b>char</b> is translated to a JS string, because JS lacks a native char type.</li>
  461. <li>A <b>char</b> is a single JS char code. An UTF-16 codepoint can contain one or two <b>char</b>.</li>
  462. <li>Integers overflows at runtime differ from Delphi/FPC, due to the double format.
  463. For example adding <i>var i: byte = 200; ... i:=i+100;</i> will result in
  464. <i>i=300</i> instead of <i>i=44</i> as in Delphi/FPC.</li>
  465. </ul>
  466. </div>
  467. <div class="section">
  468. <h2 id="string">Translating string</h2>
  469. Strings are translated to JavaScript strings. They are initialized with ""
  470. and are never <b>null</b>.<br>
  471. There are no <i>ShortString, AnsiString or RawByteString</i>.
  472. <i>Unicodestring</i> and <i>Widestring</i> are alias of <i>String</i>.<br>
  473. JavaScript strings are immutable, which means
  474. that changing a single character in a string, creates a new string. So a <i>s[2]:='c';</i>
  475. is a slow operation in pas2js compared to Delphi/FPC.<br>
  476. Although pas2js creates .js files encoded as UTF-8 with BOM, JavaScript strings are
  477. UTF-16 at runtime. Keep in mind that one UTF-16 codepoint can need two <i>char</i>,
  478. and a visible glyph can need several codepoints. Same as in Delphi.
  479. </div>
  480. <div class="section">
  481. <h2 id="resourcestrings">Translating resourcestrings</h2>
  482. Resourcestrings are translated to JS objects with original (org) and current value.
  483. <table class="sample">
  484. <tbody>
  485. <tr>
  486. <th>Pascal</th>
  487. <th>JavaScript</th>
  488. </tr>
  489. <tr>
  490. <td>
  491. <pre>Unit MyModule;
  492. Interface
  493. resourcestring
  494. rsCompiler = 'pas2js';
  495. var
  496. s:string;
  497. Implementation
  498. initialization
  499. s:=rsCompiler;
  500. End.
  501. </pre>
  502. </td>
  503. <td>
  504. <pre>rtl.module("test1",["System"],function () {
  505. var $mod = this;
  506. this.s = "";
  507. $mod.$resourcestrings = {rsCompiler: {org: "pas2js"}};
  508. $mod.$init = function () {
  509. $mod.s = rtl.getResStr(pas.test1,"rsCompiler");
  510. };
  511. });
  512. </pre>
  513. </td>
  514. </tr>
  515. </tbody>
  516. </table>
  517. </div>
  518. <div class="section">
  519. <h2 id="types">Translating Types</h2>
  520. JavaScript type design has no declarative form, except for object types
  521. (so-called prototypes).
  522. That's why all the derivatives from simple Pascal types can not be translated.
  523. The compiler ensures type safety at compile time though, which is a big plus
  524. for using Pascal.<br>
  525. Complex Pascal types (classes, records, or arrays) are translated into
  526. JavaScript objects or arrays respectively.<br>
  527. </div>
  528. <div class="section">
  529. <h2 id="pointer">Translating pointer</h2>
  530. A <i>pointer</i> is translated to a reference. It can be assigned a class,
  531. a class reference, an array, a procedure var, a method var, a @proc address
  532. or a @method address. There is no pointer arithmetic and no typed
  533. pointers. You can find out its type using the functions <i>isArray</i>,
  534. <i>isClass</i>, <i>isClassRef</i>, <i>isCallback</i>, etc of unit <i>JS</i>.
  535. </div>
  536. <div class="section">
  537. <h2 id="record">Translating record type</h2>
  538. A record is translated to a JavaScript object.
  539. <table class="sample">
  540. <tbody>
  541. <tr>
  542. <th>Pascal</th>
  543. <th>JavaScript</th>
  544. </tr>
  545. <tr>
  546. <td>
  547. <pre>Unit MyModule;
  548. Interface
  549. Type
  550. TMyRecord = Record
  551. i: integer;
  552. s: string;
  553. d: TDateTime;
  554. End;
  555. Var
  556. r, s: TMyRecord;
  557. Implementation
  558. Initialization
  559. r.i := 123;
  560. r:=s;
  561. if r=s then ;
  562. End.
  563. </pre>
  564. </td>
  565. <td>
  566. <pre>rtl.module("MyModule",
  567. ["System"],
  568. function(){
  569. var $mod = this;
  570. this.TMyRecord = function(s) {
  571. if (s){
  572. this.i = s.i;
  573. this.s = s.s;
  574. this.d = s.d;
  575. } else {
  576. this.i = 0;
  577. this.s = "";
  578. this.d = 0.0;
  579. };
  580. this.$equal = function (b) {
  581. return (this.i == b.i) && (this.s == b.i) && (this.d == b.d);
  582. };
  583. };
  584. this.r = new this.TMyRecord();
  585. $mod.$init = function() {
  586. $mod.r.i=123;
  587. $mod.r = new $mod.TMyRecord($mod.s);
  588. if ($mod.r.$equal($mod.s)) ;
  589. },
  590. },
  591. []);
  592. </pre>
  593. </td>
  594. </tr>
  595. </tbody>
  596. </table>
  597. <ul>
  598. <li>The record variable creates a JavaScript object.</li>
  599. <li>Variant records are not supported.</li>
  600. <li>Supported: Assign, pass as argument, equal, not equal, array of record</li>
  601. <li>Not yet implemented: Constants, pointer of record, advanced records, operators.</li>
  602. <li>When assigning a record it is cloned. This is compatible with Delphi and FPC.</li>
  603. </ul>
  604. </div>
  605. <div class="section">
  606. <h2 id="functions">Translating functions</h2>
  607. <table class="sample">
  608. <tbody>
  609. <tr>
  610. <th>Pascal</th>
  611. <th>JavaScript</th>
  612. </tr>
  613. <tr>
  614. <td>
  615. <pre>Unit MyModule;
  616. Interface
  617. Function DoubleIt(n: integer): integer;
  618. Implementation
  619. Function DoubleIt(n: integer): integer;
  620. Begin
  621. Result:=2*n;
  622. End;
  623. End.
  624. </pre>
  625. </td>
  626. <td>
  627. <pre>rtl.module("MyModule",
  628. ["System"],
  629. function(){
  630. this.DoubleIt = function(n){
  631. Result = 0;
  632. Result = 2*n;
  633. return Result;
  634. };
  635. },
  636. []);
  637. </pre>
  638. </td>
  639. </tr>
  640. </tbody>
  641. </table>
  642. Notes:
  643. <ul>
  644. <li>Local variables become local JavaScript variables: <i>var l = 0;</i>.</li>
  645. <li>Local constants become JavaScript variables in the unit/program implementation section.</li>
  646. <li>Overloaded functions are given an unique name by appending $1, $2, ...<br>
  647. Overloading is always on. You don't need to add the <i>overload</i> modifier.</li>
  648. <li>Supported: default values, local types, FuncName:=</li>
  649. </ul>
  650. </div>
  651. <div class="section">
  652. <h2 id="passbyref">Translating passing a parameter by reference</h2>
  653. JavaScript lacks passing by reference. Instead a temporary object is created
  654. with a <i>get</i> and <i>set</i> function.
  655. That means changes within the procedure are immediately visible outside, compatible with Pascal.
  656. <table class="sample">
  657. <tbody>
  658. <tr>
  659. <th>Pascal</th>
  660. <th>JavaScript</th>
  661. </tr>
  662. <tr>
  663. <td>
  664. <pre>Program MyModule;
  665. Procedure DoubleIt(var n: integer);
  666. Begin
  667. n:=2*n;
  668. End;
  669. Function Doubling(n: integer): integer;
  670. Begin
  671. DoubleIt(n);
  672. Result:=n;
  673. End;
  674. Var
  675. i: integer = 7;
  676. Begin
  677. Doubling(i);
  678. End.
  679. </pre>
  680. </td>
  681. <td>
  682. <pre>rtl.module("program",
  683. ["System"],
  684. function(){
  685. var $mod = this;
  686. this.i = 7;
  687. this.DoubleIt = function(n){
  688. n.set(2*n.get());
  689. };
  690. this.Doubling = function(n){
  691. var Result = 0;
  692. DoubleIt({
  693. get:function(){
  694. return n
  695. },
  696. set:function(v){
  697. n=v;
  698. }
  699. });
  700. Result = n;
  701. return n;
  702. };
  703. $mod.$main = function(){
  704. Doubling($mod.i);
  705. }
  706. },
  707. []);
  708. </pre>
  709. </td>
  710. </tr>
  711. </tbody>
  712. </table>
  713. When the passed value is from another context, the context is passed too:
  714. <table class="sample">
  715. <tbody>
  716. <tr>
  717. <th>Pascal</th>
  718. <th>JavaScript</th>
  719. </tr>
  720. <tr>
  721. <td>
  722. <pre>Program MyModule;
  723. Procedure DoubleIt(var n: integer);
  724. Begin
  725. n:=2*n;
  726. End;
  727. Var
  728. i: integer = 7;
  729. Begin
  730. DoubleIt(i);
  731. End.
  732. </pre>
  733. </td>
  734. <td>
  735. <pre>rtl.module("program",
  736. ["System"],
  737. function(){
  738. var $mod = this;
  739. this.i = 7;
  740. this.DoubleIt = function(n){
  741. n.set(2*n.get());
  742. };
  743. $mod.$main = function(){
  744. DoubleIt({
  745. p:$mod,
  746. get:function(){
  747. return this.p.i
  748. },
  749. set:function(v){
  750. this.p.i=v;
  751. }
  752. });
  753. }
  754. },
  755. []);
  756. </pre>
  757. </td>
  758. </tr>
  759. </tbody>
  760. </table>
  761. Notes:
  762. <ul>
  763. <li>Contrary to Delphi/FPC it is allowed to pass a property to a </i>var/out</i> parameter.</li>
  764. </ul>
  765. </div>
  766. <div class="section">
  767. <h2 id="nested functions">Translating nested functions</h2>
  768. A nested function is translated to a local variable.
  769. <table class="sample">
  770. <tbody>
  771. <tr>
  772. <th>Pascal</th>
  773. <th>JavaScript</th>
  774. </tr>
  775. <tr>
  776. <td>
  777. <pre>Unit MyModule;
  778. Interface
  779. Function SumNNumbers(n, Adder: integer): integer;
  780. Implementation
  781. Function SumNNumbers(n, Adder: integer): integer;
  782. Function Add(k: integer): integer;
  783. Begin
  784. if k=1 then
  785. Result:=1
  786. else
  787. Result:=Add(k-1)+Adder;
  788. End;
  789. Begin
  790. Result:=Add(n);
  791. End;
  792. End.
  793. </pre>
  794. </td>
  795. <td>
  796. <pre>rtl.module("MyModule",
  797. ["System"],
  798. function(){
  799. this.DoubleIt = function(n,Adder){
  800. Result = 0;
  801. var Add = function(k) {
  802. Result = 0;
  803. if (k==1) {
  804. Result = 1;
  805. } else {
  806. Result = Add(k-1)+Adder;
  807. }
  808. return Result;
  809. };
  810. Result = Add(n);
  811. return Result;
  812. };
  813. },
  814. []);
  815. </pre>
  816. </td>
  817. </tr>
  818. </tbody>
  819. </table>
  820. Note: You can assign a nested procedure to a procedure variable. A nested
  821. procedure of a method can be assigned to a method variable.<br>
  822. JavaScript preserves the current local scope, including references to the
  823. local variables of parent functions. Local types and constants belong to the
  824. unit scope (singleton).<br>
  825. When a method has nested functions, the compiler adds a local var <i>Self</i>.
  826. </div>
  827. <div class="section">
  828. <h2 id="forloop">Translating for-loops</h2>
  829. The JavaScript for-loop executes the end expression every iteration, while
  830. Pascal only executes it once. Therefore a local variable is introduced.
  831. If the loop is not entered at all, the variable is not touched. If the loop
  832. was entered the variable contanis the last value.
  833. <table class="sample">
  834. <tbody>
  835. <tr>
  836. <th>Pascal</th>
  837. <th>JavaScript</th>
  838. </tr>
  839. <tr>
  840. <td>
  841. <pre>Unit MyModule;
  842. Interface
  843. Function SumNNumbers(n: integer): integer;
  844. Implementation
  845. Function SumNNumbers(n: integer): integer;
  846. Var
  847. i, j: integer;
  848. Begin
  849. j:=0;
  850. For i:=1 To n Do
  851. Begin
  852. j:=j+i;
  853. End;
  854. if i&lt;1 then j:=1;
  855. Result:=j;
  856. End;
  857. End.
  858. </pre>
  859. </td>
  860. <td>
  861. <pre>rtl.module("MyModule",
  862. ["System"],
  863. function(){
  864. this.SumNNumbers=function(n){
  865. Result = 0;
  866. j = 0;
  867. for (var $l1 = 1, $le2 = n; $l1 &lt;= $le2; $l1++) {
  868. i = $l1;
  869. j = j + i;
  870. };
  871. if (i&lt;1) j=1;
  872. Result = j;
  873. return Result;
  874. };
  875. },
  876. []);
  877. </pre>
  878. </td>
  879. </tr>
  880. </tbody>
  881. </table>
  882. Note: The after-loop decrement is only added if <i>i</i> is read after the loop.<br>
  883. </div>
  884. <div class="section">
  885. <h2 id="repeatuntil">Translating repeat..until</h2>
  886. The <i>repeat..until</i> is translated to a <i>do{}while()</i>.
  887. <table class="sample">
  888. <tbody>
  889. <tr>
  890. <th>Pascal</th>
  891. <th>JavaScript</th>
  892. </tr>
  893. <tr>
  894. <td>
  895. <pre>Unit MyModule;
  896. Interface
  897. Function SumNNumbers(n: integer): integer;
  898. Implementation
  899. Function SumNNumbers(n: integer): integer;
  900. Var
  901. i, j: integer;
  902. Begin
  903. j:=0;
  904. i:=0;
  905. Repeat
  906. i:=i+1;
  907. j:=j+i;
  908. Until i>=n;
  909. Result:=j;
  910. End;
  911. End.
  912. </pre>
  913. </td>
  914. <td>
  915. <pre>rtl.module("MyModule",
  916. ["System"],
  917. function(){
  918. this.SumNNumbers=function(n){
  919. Result = 0;
  920. j = 0;
  921. i = 0;
  922. do{
  923. i = (i + 1);
  924. j = (j + i);
  925. } while (!(i>=n));
  926. Result = j;
  927. return Result;
  928. };
  929. },
  930. []);
  931. </pre>
  932. </td>
  933. </tr>
  934. </tbody>
  935. </table>
  936. </div>
  937. <div class="section">
  938. <h2 id="whiledo">Translating while..do</h2>
  939. <table class="sample">
  940. <tbody>
  941. <tr>
  942. <th>Pascal</th>
  943. <th>JavaScript</th>
  944. </tr>
  945. <tr>
  946. <td>
  947. <pre>Unit MyModule;
  948. Interface
  949. Function SumNNumbers(n: integer): integer;
  950. Implementation
  951. Function SumNNumbers(n: integer): integer;
  952. Var
  953. i, j: integer;
  954. Begin
  955. j:=0;
  956. i:=0;
  957. While i&lt;n Do Begin
  958. i:=i+1;
  959. j:=j+i;
  960. End;
  961. Result:=j;
  962. End;
  963. End.
  964. </pre>
  965. </td>
  966. <td>
  967. <pre>rtl.module("MyModule",
  968. ["System"],
  969. function(){
  970. this.SumNNumbers=function(n){
  971. var Result = 0;
  972. var j = 0;
  973. var i = 0;
  974. while(i&lt;n){
  975. i = (i + 1);
  976. j = (j + i);
  977. };
  978. Result = j;
  979. return Result;
  980. };
  981. },
  982. []);
  983. </pre>
  984. </td>
  985. </tr>
  986. </tbody>
  987. </table>
  988. </div>
  989. <div class="section">
  990. <h2 id="casedo">Translating case..do</h2>
  991. Although JavaScript has something similar in form of the "switch" statement,
  992. it lacks ranges and is on current JS engines often slower than "if-else".
  993. Therefore a case..of is translated to if..else.
  994. <table class="sample">
  995. <tbody>
  996. <tr>
  997. <th>Pascal</th>
  998. <th>JavaScript</th>
  999. </tr>
  1000. <tr>
  1001. <td>
  1002. <pre>Program MyModule;
  1003. Var
  1004. i: integer;
  1005. Begin
  1006. case i of
  1007. 1: ;
  1008. 2: i:=3;
  1009. else
  1010. i:=4;
  1011. end;
  1012. End.
  1013. </pre>
  1014. </td>
  1015. <td>
  1016. <pre>rtl.module("program",
  1017. ["System"],
  1018. function(){
  1019. var $mod = this;
  1020. this.i = 0;
  1021. $mod.$main=function(n){
  1022. $tmp1 = $mod.i;
  1023. if ($tmp1 == 1){
  1024. } else if ($tmp1 == 2) {
  1025. i=3;
  1026. } else {
  1027. i=4;
  1028. }
  1029. };
  1030. },
  1031. []);
  1032. </pre>
  1033. </td>
  1034. </tr>
  1035. </tbody>
  1036. </table>
  1037. </div>
  1038. <div class="section">
  1039. <h2 id="withdo">Translating with..do</h2>
  1040. JavaScript has a <b>with</b>, but it is slow and deprecated.
  1041. Instead a temporary variable is used:
  1042. <table class="sample">
  1043. <tbody>
  1044. <tr>
  1045. <th>Pascal</th>
  1046. <th>JavaScript</th>
  1047. </tr>
  1048. <tr>
  1049. <td>
  1050. <pre>Unit MyModule;
  1051. Interface
  1052. type
  1053. TClassA = class
  1054. i: integer;
  1055. end;
  1056. procedure DoIt;
  1057. Implementation
  1058. procedure DoIt;
  1059. begin
  1060. with TClassA.Create do
  1061. i:=3;
  1062. end;
  1063. End.
  1064. </pre>
  1065. </td>
  1066. <td>
  1067. <pre>rtl.module("MyModule",
  1068. ["System"],
  1069. function(){
  1070. var $mod = this;
  1071. rtl.createClass($mod, "TClassA", pas.System.TObject, function () {
  1072. this.$init = function () {
  1073. this.i = 0;
  1074. };
  1075. });
  1076. this.DoIt = function(){
  1077. var $with1 = $mod.TClassA.$create("Create");
  1078. $with1.i = 3;
  1079. };
  1080. },
  1081. []);
  1082. </pre>
  1083. </td>
  1084. </tr>
  1085. </tbody>
  1086. </table>
  1087. Note: If the with-expression is already a local variable no new variable is
  1088. created. This is Delphi/FPC compatible.
  1089. </div>
  1090. <div class="section">
  1091. <h2 id="enums">Translating enums</h2>
  1092. Enum values are translated to numbers. The enum type is translated to an
  1093. object containing a mapping from name to number and number to name.
  1094. <table class="sample">
  1095. <tbody>
  1096. <tr>
  1097. <th>Pascal</th>
  1098. <th>JavaScript</th>
  1099. </tr>
  1100. <tr>
  1101. <td>
  1102. <pre>Unit MyModule;
  1103. Interface
  1104. type
  1105. TMyEnum = (
  1106. Red,
  1107. Green,
  1108. Blue);
  1109. var
  1110. e: TMyEnum = Blue;
  1111. procedure DoIt;
  1112. Implementation
  1113. procedure DoIt;
  1114. begin
  1115. e := Green;
  1116. end;
  1117. End.
  1118. </pre>
  1119. </td>
  1120. <td>
  1121. <pre>rtl.module("MyModule",
  1122. ["System"],
  1123. function(){
  1124. var $mod = this;
  1125. this.TMyEnum = {
  1126. "0":"Red",
  1127. Red:0,
  1128. "1":"Green",
  1129. Green:1,
  1130. "2":"Blue",
  1131. Blue:2
  1132. };
  1133. this.e = $mod.TMyEnum.Blue;
  1134. this.DoIt = function(){
  1135. $mod.e = $mod.TMyEnum.Green;
  1136. };
  1137. },
  1138. []);
  1139. </pre>
  1140. </td>
  1141. </tr>
  1142. </tbody>
  1143. </table>
  1144. <ul>
  1145. <li>Supported: ord(), low(), high(), pred(), succ(), type cast number to enum.</li>
  1146. <li>With optimization level -O1 the compiler uses numbers instead of names.</li>
  1147. <li>Not yet implemented: custom values for enum values.</li>
  1148. </ul>
  1149. </div>
  1150. <div class="section">
  1151. <h2 id="sets">Translating sets</h2>
  1152. A set s is translated to a JavaScript object, where for each included enum
  1153. holds <i>s.enumvalue==true</i>.
  1154. This allows arbitrary large sets and the <i>in</i> operator is fast.
  1155. <table class="sample">
  1156. <tbody>
  1157. <tr>
  1158. <th>Pascal</th>
  1159. <th>JavaScript</th>
  1160. </tr>
  1161. <tr>
  1162. <td>
  1163. <pre>Unit MyModule;
  1164. Interface
  1165. type
  1166. TColor = (Red, Green, Blue);
  1167. TColors = set of TColor;
  1168. procedure DoIt;
  1169. Implementation
  1170. procedure DoIt;
  1171. var
  1172. c: TColor;
  1173. S, T: TColors;
  1174. b: boolean;
  1175. begin
  1176. S:=T;
  1177. b:=Red in S;
  1178. Include(S,Blue);
  1179. Exclude(S,Blue);
  1180. S:=S+T;
  1181. S:=S-[Red,c];
  1182. b:=c in [Red..Blue];
  1183. end;
  1184. End.
  1185. </pre>
  1186. </td>
  1187. <td>
  1188. <pre>rtl.module("MyModule",
  1189. ["System"],
  1190. function(){
  1191. var $mod = this;
  1192. this.TColor = {
  1193. "0":"Red",
  1194. Red:0,
  1195. "1":"Green",
  1196. Green:1,
  1197. "2":"Blue",
  1198. Blue:2
  1199. };
  1200. $mod.DoIt = function(){
  1201. var c = 0;
  1202. var S = {};
  1203. var T = {};
  1204. var b = false;
  1205. S = rtl.refSet(T);
  1206. b = $mod.TColor.Red in S;
  1207. S = rtl.includeSet(S,$mod.TColor.Blue);
  1208. S = rtl.excludeSet(S,$mod.TColor.Blue);
  1209. S = rtl.unionSet(S,T);
  1210. S = rtl.diffSet(S,rtl.createSet($mod.TColor.Red,c));
  1211. b = c in rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);
  1212. };
  1213. },
  1214. []);
  1215. </pre>
  1216. </td>
  1217. </tr>
  1218. </tbody>
  1219. </table>
  1220. <ul>
  1221. <li>Supported:
  1222. <ul>
  1223. <li>Include</li>
  1224. <li>Exclude</li>
  1225. <li>literal</li>
  1226. <li>literal range, e.g. <i>[EnumA..EnumB], ['a'..'z']</i></li>
  1227. <li>union +</li>
  1228. <li>difference -</li>
  1229. <li>intersect *</li>
  1230. <li>symmetrical difference >&lt;</li>
  1231. <li>equal =</li>
  1232. <li>unequal &lt;></li>
  1233. <li>subset &lt;=</li>
  1234. <li>superset >=</li>
  1235. <li>set of anonymous enum type: <i>set of (enum1,enum2,...)</i></li>
  1236. </ul>
  1237. </li>
  1238. <li>Not supported: set of char, set of boolean</li>
  1239. <li>There is no optimization yet for small sets like in Delphi/FPC.</li>
  1240. <li>Assigning a set or passing the set as an argument only creates a
  1241. reference and marks the set as <i>shared</i>.
  1242. When a <i>shared</i> set is altered with Include/Exclude a new set is
  1243. created (copy on write).</li>
  1244. <li>Passing a set as an argument might clone the set.
  1245. Use the <i>const</i> modifier for parameters whenever possible.</li>
  1246. <li>Constant sets in expressions (e.g. <i>if c in ['a'..'z'] then</i>)
  1247. are not yet optimized and created every time. Create a <i>const</i> to avoid this.</li>
  1248. </ul>
  1249. </div>
  1250. <div class="section">
  1251. <h2 id="array">Translating array type</h2>
  1252. All arrays are translated into JavaScript arrays.<br>
  1253. Contrary to Delphi/FPC dynamic arrays are
  1254. not reference counted and do not copy on write. That means if you pass an
  1255. array to a procedure and change an element, the original array is changed.
  1256. <table class="sample">
  1257. <tbody>
  1258. <tr>
  1259. <th>Pascal</th>
  1260. <th>JavaScript</th>
  1261. </tr>
  1262. <tr>
  1263. <td>
  1264. <pre>Unit MyModule;
  1265. Interface
  1266. Type
  1267. TIntArr = Array of integer;
  1268. TObjArr = Array of TObject;
  1269. TRec = record c: char; end;
  1270. TRecArr = Array of TRec;
  1271. Procedure Test;
  1272. Implementation
  1273. Procedure Test;
  1274. Var
  1275. IntArr: TIntArr = (1,2,3);
  1276. ObjArr: TObjArr;
  1277. RecArr: TRecArr;
  1278. Begin
  1279. IntArr:=nil;
  1280. SetLength(IntArr,4);
  1281. IntArr[2]:=2;
  1282. IntArr[1]:=length(IntArr);
  1283. SetLength(ObjArr,5);
  1284. SetLength(RecArr,2,TRec);
  1285. End;
  1286. End.
  1287. </pre>
  1288. </td>
  1289. <td>
  1290. <pre>rtl.module("MyModule",
  1291. ["System"],
  1292. function(){
  1293. var $mod = this;
  1294. this.Test = function(){
  1295. this.TRec = function(s){
  1296. if (s){
  1297. this.c = s.c;
  1298. } else {
  1299. this.c = "";
  1300. };
  1301. this.$equal = function(b){
  1302. return (this.c == b.c);
  1303. };
  1304. };
  1305. this.IntArr = [1,2,3];
  1306. this.ObjArr = [];
  1307. this.RecArr = [];
  1308. this.Test = function(){
  1309. $mod.IntArr = [];
  1310. rtl.arraySetLength($mod.IntArr,4,0);
  1311. $mod.IntArr[2] = 2;
  1312. $mod.IntArr[1] = $mod.IntArr.length;
  1313. rtl.setArrayLength($mod.ObjArr,5,null);
  1314. rtl.setArrayLength($mod.RecArr,2,$mod.TRec);
  1315. }
  1316. };
  1317. },
  1318. []);
  1319. </pre>
  1320. </td>
  1321. </tr>
  1322. </tbody>
  1323. </table>
  1324. Notes:
  1325. <ul>
  1326. <li>Supported features of dynamic arrays: SetLength(), Length(), equal/notequal nil, low(), high(),
  1327. assigned(), concat(), copy(), insert(), delete(), multi dimensional, array of record</li>
  1328. <li>Dynamic array constants. E.g. const a: array of byte = (1,2)</li>
  1329. <li>Supported features of static arrays: length(), low(), high(), assigned(), concat(), copy(), const, except const records </li>
  1330. <li>Open arrays are implemented as dynamic arrays.</li>
  1331. <li>Calling <i>Concat()</i> with only one array simply returns the array
  1332. (no cloning). Calling it with multiple arrays creates a clone.
  1333. This is Delphi 10.1 compatible.</li>
  1334. <li>In Delphi/FPC an empty array is <i>nil</i>. In JS it can be <i>null</i> or <i>[]</i>.
  1335. For compatibility comparing an array with <i>nil</i> checks for <i>length(a)>0</i>.</li>
  1336. <li><i>function Assigned(array): boolean</i> results true iff <i>length(array)>0</i>.</li>
  1337. <li>Not yet implemented: array of const.</li>
  1338. <li><i>function copy(array,start=0,count=max): array</i></li>
  1339. <li><i>procedure insert(item,var array,const position)</i></li>
  1340. <li><i>procedure delete(var array,const start,count)</i></li>
  1341. </ul>
  1342. </div>
  1343. <div class="section">
  1344. <h2 id="class">Translating class type</h2>
  1345. Classes are implemented using <i>Object.create</i> and some rtl magic.
  1346. <table class="sample">
  1347. <tbody>
  1348. <tr>
  1349. <th>Pascal</th>
  1350. <th>JavaScript</th>
  1351. </tr>
  1352. <tr>
  1353. <td>
  1354. <pre>Unit MyModule;
  1355. Interface
  1356. Type
  1357. TClassA = Class(TObject)
  1358. Public
  1359. i: integer;
  1360. Procedure Add(a: integer);
  1361. End;
  1362. var
  1363. ObjA: TClassA;
  1364. Implementation
  1365. Procedure TClassA.Add(a: integer);
  1366. Begin
  1367. i:=i+a;
  1368. End;
  1369. Initialization
  1370. ObjA:=TClassA.Create;
  1371. End.
  1372. </pre>
  1373. </td>
  1374. <td>
  1375. <pre>rtl.module("MyModule",
  1376. ["System"],
  1377. function(){
  1378. var $mod = this;
  1379. rtl.createClass($mod,"TClassA",pas.System.TObject,function(){
  1380. this.$init = function () {
  1381. this.i = 0;
  1382. };
  1383. this.Add = function(a){
  1384. this.i = this.i + a;
  1385. };
  1386. });
  1387. this.ObjA = null;
  1388. $mod.$init = function(){
  1389. $mod.ObjA = $mod.TClassA.$create("Create");
  1390. };
  1391. },
  1392. []);
  1393. </pre>
  1394. </td>
  1395. </tr>
  1396. </tbody>
  1397. </table>
  1398. Notes:
  1399. <ul>
  1400. <li>Each class and each instance is an JS object.</li>
  1401. <li>Each class has a globally unique JS object, created by rtl.createClass.</li>
  1402. <li><i>Self</i> is never <i>nil</i>.</li>
  1403. <li>The method <i>TObject.Free</i> is using compiler magic. See <a href="#tobjectfree">Translating TObject.Free</a>.</li>
  1404. <li><i>Class.$class</i> is a reference to the class itself.</li>
  1405. <li><i>Class.$ancestor</i> is a reference to the ancestor class.</li>
  1406. <li>A class has <i>c.$ancestor == Object.getPrototypeOf(c)</i>.</li>
  1407. <li>A class instance has <i>o.$class == Object.getPrototypeOf(o)</i>.</li>
  1408. <li><i>Class.$classname</i> is the short name. E.g. <i>TClassA.$classname == 'TClassA'</i>.</li>
  1409. <li><i>Class.$name</i> is the long name. E.g. <i>TClassA.$name == 'MyModule.TClassA'</i>.</li>
  1410. <li><i>Class.$unitname</i> is the unit name. E.g. <i>TClassA.$unitname == 'MyModule'</i>.</li>
  1411. <li>The "<i>is</i>"-operator is implemented using "<i>isPrototypeOf</i>". Note that "<i>instanceof</i>" cannot be used, because classes are JS objects.</li>
  1412. <li>The "<i>as</i>" operator is implemented as <i>rtl.as(Object,Class)</i>.</li>
  1413. <li>Supported: constructor, destructor, private, protected, public,
  1414. strict private, strict protected, class vars, class methods, external methods,
  1415. virtual, override, abstract, call inherited, assigned(), type cast,
  1416. overloads, reintroduce, sealed class</li>
  1417. <li>Property:
  1418. <ul>
  1419. <li>References are replaced by getter/setter.</li>
  1420. <li>Supported: argument lists, default property, class property, stored modifier, index modifier.</li>
  1421. <li>Class property getter/setter are not static as in Delphi.</li>
  1422. <li>The <i>Index</i> modifier supports any constant, e.g. a string, while
  1423. Delphi only allows an ordinal (longint). -2147483648 is not a special
  1424. number in pas2js. Overriding a property with an index property is allowed
  1425. in Delphi and pas2js.</li>
  1426. </ul>
  1427. </li>
  1428. </ul>
  1429. </div>
  1430. <div class="section">
  1431. <h2 id="classof">Translating class-of type</h2>
  1432. A class-of is a reference to a class. See above about translating class.
  1433. <table class="sample">
  1434. <tbody>
  1435. <tr>
  1436. <th>Pascal</th>
  1437. <th>JavaScript</th>
  1438. </tr>
  1439. <tr>
  1440. <td>
  1441. <pre>Unit MyModule;
  1442. Interface
  1443. Type
  1444. TBird = Class(TObject)
  1445. Public
  1446. Class var Count: integer;
  1447. Class Procedure Add(a: integer); virtual;
  1448. End;
  1449. TBirds = class of TBird;
  1450. TPigeon = Class(TBird)
  1451. Public
  1452. Class Procedure Add(a: integer); override;
  1453. End;
  1454. var
  1455. BirdType: TBirds;
  1456. Implementation
  1457. Class Procedure TBird.Add(a: integer);
  1458. Begin
  1459. Count:=Count+a;
  1460. End;
  1461. Class Procedure TPigeon.Add(a: integer);
  1462. Begin
  1463. inherited Add(a+1);
  1464. End;
  1465. Initialization
  1466. BirdType:=TPigeon;
  1467. BirdType.Add(1);
  1468. End.
  1469. </pre>
  1470. </td>
  1471. <td>
  1472. <pre>rtl.module("MyModule",
  1473. ["System"],
  1474. function(){
  1475. var $mod = this;
  1476. rtl.createClass($mod,"TBird",pas.System.TObject,function () {
  1477. this.Count = 0;
  1478. this.Add = function (a) {
  1479. this.Count = this.Count + a;
  1480. };
  1481. });
  1482. rtl.createClass($mod,"TPigeon",$mod.TBird,function () {
  1483. this.Add = function (a) {
  1484. $mod.TBird.Add.call(this,a + 1);
  1485. };
  1486. });
  1487. $mod.$init = function(){
  1488. $mod.BirdType = $mod.TPigeon;
  1489. $mod.BirdType.Add(1);
  1490. };
  1491. },
  1492. []);
  1493. </pre>
  1494. </td>
  1495. </tr>
  1496. </tbody>
  1497. </table>
  1498. Note that <i>this</i> in a class method is the class itself.<br>
  1499. <br>
  1500. Notes:<br>
  1501. <ul>
  1502. <li>Contrary to Delphi/FPC the "is" operator works with class-of.</li>
  1503. </ul>
  1504. </div>
  1505. <div class="section">
  1506. <h2 id="tobjectfree">Translating TObject.Free</h2>
  1507. In Delphi/FPC AnObject.Free checks if Self is nil, then calls the destructor
  1508. and frees the memory, without changing the reference.
  1509. In JavaScript however calling a method with AnObject=nil causes a crash.
  1510. And memory cannot be freed explicitely. Memory is only
  1511. freed if all references are gone (e.g. set to <i>null</i>).<br>
  1512. Therefore pas2js adds code to call the destructor and sets the variable to <i>nil</i>:<br>
  1513. <ul>
  1514. <li><i>Obj.Free</i> on a local variable or argument is translated to
  1515. <i>Obj = rtl.freeLoc(Obj);</i>.</li>
  1516. <li><i>Obj.Free</i> on a non local variable is translated to
  1517. <i>rtl.free(this,"Obj");</i>.</li>
  1518. <li>Not supported: Freeing a property or function result.<br>
  1519. For example <i>List[i].Free</i> gives a compiler error. The property
  1520. setter might create side effects, which would be incompatible to Delphi/FPC.
  1521. </li>
  1522. </ul>
  1523. Notes:
  1524. <ul>
  1525. <li>If the destructor raises an exception, the variable is not set to <i>nil</i>.
  1526. This is compatible to Delphi/FPC, where the memory is not freed in this case.</li>
  1527. <li>Alternatively you can use <i>FreeAndNil</i>, which first changes
  1528. the variable to <i>nil</i> and then calls the destructor.</li>
  1529. </ul>
  1530. </div>
  1531. <div class="section">
  1532. <h2 id="classinterfaces">Translating class interfaces</h2>
  1533. Class interfaces are not yet implemented. To make porting code easier there
  1534. is a <i>{$modeswitch ignoreinterfaces}</i>, that ignores interface declarations
  1535. and the class interface list.
  1536. </div>
  1537. <div class="section">
  1538. <h2 id="attributes">Translating attributes</h2>
  1539. Attributes are not yet implemented. To make porting code easier there
  1540. is a <i>{$modeswitch ignoreattributes}</i>, that ignores attributes.
  1541. </div>
  1542. <div class="section">
  1543. <h2 id="tryfinally">Translating try..finally</h2>
  1544. JavaScript has the same, so it translates straight forward.
  1545. </div>
  1546. <div class="section">
  1547. <h2 id="tryexcept">Translating try..except</h2>
  1548. <table class="sample">
  1549. <tbody>
  1550. <tr>
  1551. <th>Pascal</th>
  1552. <th>JavaScript</th>
  1553. </tr>
  1554. <tr>
  1555. <td>
  1556. <pre>Unit MyModule;
  1557. Interface
  1558. Uses SysUtils, Math, JS;
  1559. Function DoIt(n: integer): double;
  1560. Implementation
  1561. Function DoIt(n: integer): double;
  1562. var E: Exception;
  1563. Begin
  1564. try
  1565. Result:=double(7.0)/n;
  1566. if not IsFinite(Result) then
  1567. if n=0 then
  1568. raise EZeroDivide.Create
  1569. else
  1570. raise EOverflow.Create;
  1571. except
  1572. on EZeroDivide do Result:=0.0;
  1573. on E2: EOverflow do Result:=0.0;
  1574. else
  1575. raise EAbort.Create('Something other: '+String(JS.JSExceptObject));
  1576. end;
  1577. End;
  1578. End.
  1579. </pre>
  1580. </td>
  1581. <td>
  1582. <pre>rtl.module("MyModule",
  1583. ["System","SysUtils"],
  1584. function(){
  1585. this.DoIt=function(n){
  1586. Result = 0;
  1587. var E = null;
  1588. try{
  1589. Result = 7.0 / n;
  1590. if (!IsFinite(Result)){
  1591. if (n==0){
  1592. throw pas.SysUtils.EZeroDivide.$create("Create");
  1593. } else {
  1594. throw pas.SysUtils.EOverflow.$create("Create");
  1595. };
  1596. };
  1597. }catch($e){
  1598. if (pas.SysUtils.EZeroDivide.isPrototypeOf($e)){
  1599. Result = 0.0;
  1600. } else if (pas.SysUtils.EOverflow.isPrototypeOf($e)){
  1601. var E2 = $e;
  1602. Result = 0.0;
  1603. } else {
  1604. throw pas.SysUtils.EAbort.$create("Create",["Something other: "+(""+$e)]);
  1605. }
  1606. }
  1607. return Result;
  1608. };
  1609. },
  1610. []);
  1611. </pre>
  1612. </td>
  1613. </tr>
  1614. </tbody>
  1615. </table>
  1616. Notes:
  1617. <ul>
  1618. <li>Division by zero does not raise an exception in JavaScript. Instead it results in Infinity, except for 0/0 which results in NaN.</li>
  1619. <li>There is no ExceptObject in SysUtils.</li>
  1620. <li>When calling external functions keep in mind that JS allows to
  1621. throw (raise) any value, often a string.<br>
  1622. You can access the current except value via JSExceptValue in unit JS.<br>
  1623. Note that this is only valid inside the catch-block. The compiler will not warn,
  1624. if you use it outside.</li>
  1625. </div>
  1626. <div class="section">
  1627. <h2 id="functiontype">Translating function type</h2>
  1628. JavaScript functions work like Delphi's "reference to function", which
  1629. means like closures. Normal functions and nested functions can simply be
  1630. assigned to variables.
  1631. A Pascal method needs <b>this</b> to be the class or class instance.<br>
  1632. Note that <i>bind</i> cannot be used, because it does not support the <i>equal</i> operator.
  1633. Instead a wrapper is created:
  1634. <table class="sample">
  1635. <tbody>
  1636. <tr>
  1637. <th>Pascal</th>
  1638. <th>JavaScript</th>
  1639. </tr>
  1640. <tr>
  1641. <td>
  1642. <pre>Unit UnitA;
  1643. Program MyModule;
  1644. type
  1645. TMyMethod = procedure(n: integer) of object;
  1646. TBird = class
  1647. procedure DoIt(n: integer); virtual; abstract;
  1648. end;
  1649. TMyProc = procedure(n: integer);
  1650. procedure DoSome(n: integer);
  1651. begin
  1652. end;
  1653. var
  1654. m: TMyMethod;
  1655. Bird: TBird;
  1656. p: TMyProc;
  1657. Begin
  1658. m:[email protected];
  1659. m(3);
  1660. p:=@DoSome;
  1661. p(4);
  1662. End.
  1663. </pre>
  1664. </td>
  1665. <td>
  1666. <pre>rtl.module("program",
  1667. ["System","UnitA"],
  1668. function(){
  1669. var $mod = this;
  1670. rtl.createClass($mod,"TBird",pas.System.TObject,function(){
  1671. this.DoIt = function (n) {
  1672. };
  1673. });
  1674. this.DoSome = function (n) {
  1675. };
  1676. this.m = null;
  1677. this.Bird = null;
  1678. this.p = null;
  1679. $mod.$main = function() {
  1680. $mod.m = rtl.createCallback($mod.Bird,"DoIt");
  1681. $mod.m(3);
  1682. $mod.p = $mod.DoSome;
  1683. $mod.p(4);
  1684. };
  1685. },
  1686. []);
  1687. rtl = {
  1688. ...
  1689. createCallback: function(scope, fn){
  1690. var cb = function(){
  1691. return scope[fn].apply(scope,arguments);
  1692. };
  1693. cb.scope = scope;
  1694. cb.fn = fn;
  1695. return cb;
  1696. },
  1697. ...
  1698. </pre>
  1699. </td>
  1700. </tr>
  1701. </tbody>
  1702. </table>
  1703. Notes:
  1704. <ul>
  1705. <li>You can assign a nested procedure to procedure variable.
  1706. You don't need and you must not add the FPC "<i>is nested</i>" modifier.</li>
  1707. </ul>
  1708. </div>
  1709. <div class="section">
  1710. <h2 id="calljavascript">Calling JavaScript from Pascal</h2>
  1711. Pas2js allows to write low level functions and/or access a JavaScript library
  1712. with the following possibilities:
  1713. </div>
  1714. <div class="section">
  1715. <h2 id="asm">The asm block</h2>
  1716. The asm block is pure JavaScript, that is copied directly into the generated .js file.
  1717. <table class="sample">
  1718. <tbody>
  1719. <tr>
  1720. <th>Pascal</th>
  1721. <th>JavaScript</th>
  1722. </tr>
  1723. <tr>
  1724. <td>
  1725. <pre>Program MyModule;
  1726. var
  1727. s: string;
  1728. Begin
  1729. s = 'Hello World!';
  1730. Asm
  1731. console.log(s);
  1732. End;
  1733. End.
  1734. </pre>
  1735. </td>
  1736. <td>
  1737. <pre>rtl.module("program",
  1738. ["System"],
  1739. function(){
  1740. var $mod = this;
  1741. this.s = '';
  1742. $mod.$main = function(){
  1743. $mod.s = "Hello World!";
  1744. console.log(s);
  1745. };
  1746. },
  1747. []);
  1748. </pre>
  1749. </td>
  1750. </tr>
  1751. </tbody>
  1752. </table>
  1753. Notes:
  1754. <ul>
  1755. <li>The block is indented to produce more readable JS code.
  1756. All lines are indented or unindented the same amount, i.e. sub indentation is kept.</li>
  1757. <li>The compiler does neither parse, nor check the syntax of the JS.</li>
  1758. <li>The compiler does not know what Pascal identifiers are used by the
  1759. asm-block and might remove them, if no Pascal code is using them.
  1760. To make sure that an identifier is kept, add some dummy code like
  1761. <i>if MyVar=0 then;</i></li>
  1762. <li>Accessing an interface, program or library identifier:<br>
  1763. <ul>
  1764. <li>From inside the module you can use <i>$mod.Identifier</i>.</li>
  1765. <li>Otherwise use the fully qualified path <i>pas.Unitname.Identifier</i>.</li>
  1766. </ul>
  1767. </li>
  1768. <li>Accessing an implementation identifier:<br>
  1769. <ul>
  1770. <li>From inside the unit you can use <i>$impl.Identifier</i>.</li>
  1771. <li>Otherwise use the path <i>pas.Unitname.$impl.Identifier</i>.</li>
  1772. </ul>
  1773. </li>
  1774. <li>Accessing a class instance member (field, procedure, function,
  1775. constructor, destructor) from a method of the class: use <i>this.Identifier</i>.
  1776. Inside a nested function of a method you use the <i>Self.Identifier</i>.
  1777. </li>
  1778. <li>Accessing a class member (class var, class procedure, class function)
  1779. from a method of the class: for writing use <i>this.$class.Identifier</i>,
  1780. for reading you can omit the <i>$class</i>.</li>
  1781. <li>Accessing a class member (class var, class procedure, class function)
  1782. from a class method of the class: use <i>this.Identifier</i>.</li>
  1783. <li>Access to Properties must use the getter/setter.</li>
  1784. <li>When calling a Pascal method, make sure the <b>this</b> is correct:
  1785. <ul>
  1786. <li>A class method (e.g. <i>class function</i>, <i>class procedure</i>)
  1787. needs the class as <i>this</i>.<br>
  1788. <b>Wrong</b>: <i>aCar.DoIt(params,...)</i><br>
  1789. <b>Correct</b>: <i>aCar.$class.DoIt(params,...)</i><br>
  1790. </li>
  1791. </ul>
  1792. </li>
  1793. <li>Calling a Pascal function from a HTML/DOM-element:
  1794. For example to call a function when user clicks a DOM element you can
  1795. assign a function to the <i>onclick</i> property. This will call
  1796. the function with <i>this</i> set to the DOM element.<br>
  1797. Pascal methods needs a wrapper to set <i>this</i> to the
  1798. instance. Examples:
  1799. <ul>
  1800. <li>An unit function: <i>DOMElement.onclick = $mod.DoIt;</i></li>
  1801. <li>An implementation function: <i>DOMElement.onclick = $impl.DoIt;</i>.</li>
  1802. <li>A method: <i>DOMElement.onclick = this.DoIt.bind(this);</i></li>
  1803. <li>A class function/procedure: <i>DOMElement.onclick = this.DoIt.bind(this.$class);</i></li>
  1804. <li>A nested function: <i>DOMElement.onclick = DoIt;</i>.</li>
  1805. </ul>
  1806. </li>
  1807. </li>
  1808. </ul>
  1809. </div>
  1810. <div class="section">
  1811. <h2 id="assembler">The procedure modifier assembler</h2>
  1812. You can write pure JavaScript functions like this:
  1813. <table class="sample">
  1814. <tbody>
  1815. <tr>
  1816. <th>Pascal</th>
  1817. <th>JavaScript</th>
  1818. </tr>
  1819. <tr>
  1820. <td>
  1821. <pre>Program MyModule;
  1822. Procedure Log(const s: string); assembler;
  1823. Asm
  1824. console.log(s);
  1825. end;
  1826. Begin
  1827. Log('Hello World!');
  1828. End.
  1829. </pre>
  1830. </td>
  1831. <td>
  1832. <pre>rtl.module("program",
  1833. ["System"],
  1834. function(){
  1835. var $mod = this;
  1836. this.Log = function(s){
  1837. console.log(s);
  1838. };
  1839. $mod.$main = function(){
  1840. $mod.Log("Hello World!");
  1841. };
  1842. },
  1843. []);
  1844. </pre>
  1845. </td>
  1846. </tr>
  1847. </tbody>
  1848. </table>
  1849. See also <a href="#asm">asm</a>.
  1850. </div>
  1851. <div class="section">
  1852. <h2 id="externalproc">The procedure modifier external</h2>
  1853. The procedure modifier <i>external</i> requires a string constant and tells the
  1854. compiler to replace a reference with this string value. The value is not
  1855. checked for JS syntax.
  1856. <table class="sample">
  1857. <tbody>
  1858. <tr>
  1859. <th>Pascal</th>
  1860. <th>JavaScript</th>
  1861. </tr>
  1862. <tr>
  1863. <td>
  1864. <pre>Program MyModule;
  1865. Procedure ConsoleLog(const s: string); external name 'console.log';
  1866. // Note: an external procedure has no begin..end block
  1867. Begin
  1868. ConsoleLog('Hello World!');
  1869. End.
  1870. </pre>
  1871. </td>
  1872. <td>
  1873. <pre>rtl.module("program",
  1874. ["System"],
  1875. function(){
  1876. var $mod = this;
  1877. $mod.$main = function(){
  1878. console.log("Hello World!");
  1879. };
  1880. },
  1881. []);
  1882. </pre>
  1883. </td>
  1884. </tr>
  1885. </tbody>
  1886. </table>
  1887. </div>
  1888. <div class="section">
  1889. <h2 id="varargs">The procedure modifier varargs</h2>
  1890. Appending the <b>varargs</b> modifier to a procedure allows to pass arbitrary
  1891. more parameters to a function. To access these non typed arguments use
  1892. either <i>JSArguments</i> from unit JS or an <i>asm..end</i> block.
  1893. <table class="sample">
  1894. <tbody>
  1895. <tr>
  1896. <th>Pascal</th>
  1897. <th>JavaScript</th>
  1898. </tr>
  1899. <tr>
  1900. <td>
  1901. <pre>Program MyModule;
  1902. uses JS;
  1903. function Sum(b: boolean): longint; varargs;
  1904. var i: longint;
  1905. begin
  1906. if b then
  1907. asm
  1908. for (var i=0; i&lt;arguments.length; i++) Result+=arguments[i];
  1909. end
  1910. else
  1911. for i:=0 to JSArguments.length-1 do
  1912. Result:=Result+longint(JSArguments[i]);
  1913. end;
  1914. var
  1915. i: integer;
  1916. Begin
  1917. i:=Sum(true,2,4,6); // i=12
  1918. i:=Sum(false,2,4,6); // i=12
  1919. End.
  1920. </pre>
  1921. </td>
  1922. <td>
  1923. <pre>rtl.module("program",
  1924. ["System","JS"],
  1925. function(){
  1926. var $mod = this;
  1927. this.Sum = function(b){
  1928. var Result = 0;
  1929. var i = 0;
  1930. if (b){
  1931. for (var i=0; i&lt;arguments.length; i++) Result+=arguments[i];
  1932. } else {
  1933. for (var $l1 = 1, $le2 = argumens.length; $l1 &lt;= $le2; $l1++){
  1934. $i = $l1;
  1935. Result = Result + arguments[i];
  1936. }
  1937. }
  1938. return Result;
  1939. };
  1940. this.i = 0;
  1941. $mod.$main = function(){
  1942. $mod.i = $mod.Sum(true,2,4,6);
  1943. $mod.i = $mod.Sum(false,2,4,6);
  1944. };
  1945. },
  1946. []);
  1947. </pre>
  1948. </td>
  1949. </tr>
  1950. </tbody>
  1951. </table>
  1952. The above example defines a function <i>Sum</i>, that requires the first parameter to
  1953. be a boolean and then an arbitrary number of parameters. The compiler does not
  1954. type check the other parameters, so you can pass anything readable.
  1955. </div>
  1956. <div class="section">
  1957. <h2 id="externalvar">The var modifier external</h2>
  1958. The var modifier <i>external</i> allows to use a JavaScript variable or constant.
  1959. <table class="sample">
  1960. <tbody>
  1961. <tr>
  1962. <th>Pascal</th>
  1963. <th>JavaScript</th>
  1964. </tr>
  1965. <tr>
  1966. <td>
  1967. <pre>Program MyModule;
  1968. var
  1969. EulersNumber: Double; external name 'Math.E';
  1970. d: double;
  1971. Begin
  1972. d:=EulersNumber;
  1973. End.
  1974. </pre>
  1975. </td>
  1976. <td>
  1977. <pre>rtl.module("program",
  1978. ["System"],
  1979. function(){
  1980. var $mod = this;
  1981. this.d = 0.0;
  1982. $mod.$main = function(){
  1983. $mod.d = Math.E;
  1984. };
  1985. },
  1986. []);
  1987. </pre>
  1988. </td>
  1989. </tr>
  1990. </tbody>
  1991. </table>
  1992. </div>
  1993. <div class="section">
  1994. <h2 id="externalmembers">The external modifier of class members</h2>
  1995. The method modifier <i>external</i> works as the procedure modifier, except
  1996. it uses the scope of the class or instance.<br>
  1997. The field modifier <i>external</i> works as the var modifier, except
  1998. it uses the scope of the class or instance.<br>
  1999. Requires the modeswitch <b>externalclass</b>.
  2000. <table class="sample">
  2001. <tbody>
  2002. <tr>
  2003. <th>Pascal</th>
  2004. <th>JavaScript</th>
  2005. </tr>
  2006. <tr>
  2007. <td>
  2008. <pre>Program MyModule;
  2009. {$modeswitch externalclass}
  2010. type
  2011. TWrapper = class
  2012. private
  2013. // let's assume this object has a $Handle and an $id
  2014. public
  2015. Id: NativeInt; external name '$Id';
  2016. function GetState(typ: longint): NativeInt; external name '$Handle.GetState';
  2017. procedure DoIt;
  2018. end;
  2019. procedure TWrapper.DoIt;
  2020. begin
  2021. Id := GetState(4);
  2022. end;
  2023. var
  2024. W: TWrapper;
  2025. Begin
  2026. W.Id := 2;
  2027. W.GetState(3);
  2028. End.
  2029. </pre>
  2030. </td>
  2031. <td>
  2032. <pre>rtl.module("program",
  2033. ["System"],
  2034. function(){
  2035. var $mod = this;
  2036. rtl.createClass($mod, "TWrapper", pas.System.TObject, function () {
  2037. this.DoIt = function(){
  2038. this.$Id = this.$Handle.GetState(4);
  2039. };
  2040. });
  2041. this.W = null;
  2042. $mod.$main = function(){
  2043. $mod.W.$Id = 2;
  2044. $mod.W.$Handle.GetState(3);
  2045. };
  2046. },
  2047. []);
  2048. </pre>
  2049. </td>
  2050. </tr>
  2051. </tbody>
  2052. </table>
  2053. </div>
  2054. <div class="section">
  2055. <h2 id="externalclass">External classes</h2>
  2056. pas2js introduces a new class modifier "<i>external name</i>", which makes
  2057. the whole class external.
  2058. External classes allow to easily declare Pascal wrappers for JavaScript
  2059. objects and function objects.<br>
  2060. They need the modeswitch <b>externalclass</b> in front of the class.<br>
  2061. An external class is not a TObject and has none of its methods.<br>
  2062. All members are external. If you omit the <i>external</i> modifier the
  2063. external name is the member name. Keep in mind that JS is case sensitive.<br>
  2064. Destructors are not allowed.<br>
  2065. Constructors are only allowed with the name <i>New</i> and a call
  2066. translates to <i>new ExtClass(params)</i>.
  2067. Properties work the same as with Pascal classes, i.e. are replaced by Getter/Setter.<br>
  2068. <table class="sample">
  2069. <tbody>
  2070. <tr>
  2071. <th>Pascal</th>
  2072. <th>JavaScript</th>
  2073. </tr>
  2074. <tr>
  2075. <td>
  2076. <pre>Program MyModule;
  2077. {$modeswitch externalclass}
  2078. type
  2079. TJSDate = class external name 'Date'
  2080. private
  2081. function getYear: NativeInt;
  2082. procedure setYear(const AValue: NativeInt);
  2083. public
  2084. constructor New;
  2085. constructor New(const MilliSecsSince1970: NativeInt);
  2086. class function now: NativeInt;
  2087. property Year: NativeInt read getYear write setYear;
  2088. end;
  2089. var
  2090. d: TJSDate;
  2091. Begin
  2092. d:=TJSDate.New;
  2093. d.Year:=d.Year+1;
  2094. End.
  2095. </pre>
  2096. </td>
  2097. <td>
  2098. <pre>rtl.module("program",["System"],function () {
  2099. var $mod = this;
  2100. this.d = null;
  2101. $mod.$main = function () {
  2102. $mod.d = new Date();
  2103. $mod.d.setYear($mod.d.getYear() + 1);
  2104. };
  2105. });
  2106. </pre>
  2107. </td>
  2108. </tr>
  2109. </tbody>
  2110. </table>
  2111. Notes:
  2112. <ul>
  2113. <li>Any class instance can be type casted to any root class.</li>
  2114. <li>A Pascal class can descend from an external class.</li>
  2115. <li>You can define a class-of external class and the <b>is</b> and <b>as</b>
  2116. operators work similar.</li>
  2117. <li>Class variables work as in JavaScript. That means, each descendant and each
  2118. instance can have its own value. For example <i>TExtA.Value</i> might be
  2119. different from <i>InstanceExtA.Value</i>. Setting <i>InstanceExtA.Value</i>
  2120. does not change <i>TExtA.Value</i>.</li>
  2121. <li>Class functions and class procedures are allowed, but can only be called via the class, not via an instance.<br>
  2122. For example you can call the class function <i>TJSString.fromCharCode()</i>, but you cannot
  2123. call <i>aJSString.fromCharCode()</i>.</li>
  2124. <li>An external class can descend from another external class.</li>
  2125. </ul>
  2126. </div>
  2127. <div class="section">
  2128. <h2 id="externalclassancestor">External class as ancestor</h2>
  2129. A Pascal class can descend from an external class.<br>
  2130. The methods <i>AfterConstruction</i> and <i>BeforeDestruction</i>
  2131. are called if they exist.<br>
  2132. New instances are created by default with <i>Object.create(ancestorclass)</i>.<br>
  2133. You can override this, by providing a<br>
  2134. <b>class function NewInstance(fnname: string; const paramsarray): TPasClass; virtual;</b>.
  2135. This method is called to create a new instance and before calling the constructor.
  2136. The name is arbitrary, but the function must be the first non private,
  2137. non external, virtual class function with the class as result type.<br>
  2138. <table class="sample">
  2139. <tbody>
  2140. <tr>
  2141. <th>Pascal</th>
  2142. <th>JavaScript</th>
  2143. </tr>
  2144. <tr>
  2145. <td>
  2146. <pre>Program MyModule;
  2147. {$modeswitch externalclass}
  2148. type
  2149. TExtA = class external name 'ExtA'
  2150. end;
  2151. TMyB = class(TExtA)
  2152. protected
  2153. class function NewInstance(fnname: string; const paramarray): TMyB; virtual;
  2154. end;
  2155. class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;
  2156. Begin
  2157. asm
  2158. Result = Object.create(ExtA);
  2159. end;
  2160. End;
  2161. Begin
  2162. End.
  2163. </pre>
  2164. </td>
  2165. <td>
  2166. <pre>rtl.module("program",["System"],function () {
  2167. var $mod = this;
  2168. rtl.createClassExt($mod, "TMyB", ExtA, "NewInstance", function () {
  2169. this.$init = function () {
  2170. };
  2171. this.$final = function () {
  2172. };
  2173. this.NewInstance = function (fnname, paramarray) {
  2174. var Result = null;
  2175. Result = Object.create(ExtA);
  2176. return Result;
  2177. };
  2178. });
  2179. $mod.$main = function () {
  2180. };
  2181. });
  2182. </pre>
  2183. </td>
  2184. </tr>
  2185. </tbody>
  2186. </table>
  2187. </div>
  2188. <div class="section">
  2189. <h2 id="jsvalue">The JSValue type</h2>
  2190. Pas2js introduces a new type <b>JSValue</b>, which works similar to a JS variable.
  2191. You can assign almost any value to it and it can be type casted to many types.
  2192. JSValue is useful for JS wrappers, when a variable can have multiple types.
  2193. And it can be used for containers storing arbitrary data, e.g. a list of JSValue.<br>
  2194. Key features:<br>
  2195. <ul>
  2196. <li>A JSValue variable initial value is undefined.</li>
  2197. <li>Operators: =, &lt;&gt;</li>
  2198. <li>type casting a <i>JSValue</i> to ...
  2199. <ul>
  2200. <li><i>Integer: Math.floor(aJSValue)</i> Note: may return <i>NaN</i></li>
  2201. <li><i>Boolean: !(aJSValue == false)</i> Note: works for numbers too, <i>0==false</i></li>
  2202. <li><i>Double: rtl.getNumber(aJSValue)</i> Note: <i>typeof(n)=="number"?n:NaN;</i></li>
  2203. <li><i>String: ""+aJSValue</i></li>
  2204. <li><i>Char: rtl.getChar(aJSValue)</i> Note: <i>((typeof(c)!="string") && (c.length==1)) ? c : ""</i></li>
  2205. <li>class instance or class-of: <i>rtl.getObject()</i> Note: checks for type <i>"object"</i></li>
  2206. <li>enum type</li>
  2207. <li>pointer</li>
  2208. </ul>
  2209. </li>
  2210. <li>A JSValue in a conditional expressions <i>If aJSValue then, while aJSValue do,
  2211. repeat until aJSValue</i> has the same meaning as in JS: the condition is
  2212. true, if the value is not <i>undefined, false, null, NaN, 0, ''</i>.
  2213. Note that <i>new Boolean(false)</i> is not <i>null</i> and the condition is true.
  2214. </li>
  2215. <li><i>function Assigned(V: jsvalue): boolean</i> returns true if<br>
  2216. <i>(V!=undefined) && (V!=null) && (!rtl.isArray(V) || (V.length > 0))</i></li>
  2217. <li><i>function StrictEqual(const A: jsvalue; const B): boolean</i></li>
  2218. <li><i>function StrictInequal(const A: jsvalue; const B): boolean</i></li>
  2219. <li>Any array can be assigned to an <i>array of jsvalue</i>.</li>
  2220. <li>The unit JS provides many utility functions for JSValue, like <i>hasString,
  2221. hasValue, isBoolean, isNumber, isInteger, isObject, isClass, isClassInstance, etc..</i></li>
  2222. </ul>
  2223. </div>
  2224. <div class="section">
  2225. <h2 id="bracketaccessor">Accessing JS object properties with the bracket accessor</h2>
  2226. Pas2js allows to define index properties that map directly to the JS object properties.
  2227. For example the default property of TJSObject allows to get and set the
  2228. properties of an object. For example <i>TJSObject(AnObject)['Name']:=Value;</i><br>
  2229. Another example is the default property of TJSArray, that allows access via integers
  2230. <i>aTJSArray[3]:=Value;</i><br>
  2231. To define your own bracket accessor define a normal index property and define
  2232. the getter/setter as <i>external name '[]'</i>.<br>
  2233. Here is an example for a read only accessor:
  2234. <table class="sample">
  2235. <tbody>
  2236. <tr>
  2237. <th>Pascal</th>
  2238. <th>JavaScript</th>
  2239. </tr>
  2240. <tr>
  2241. <td>
  2242. <pre>Program MyModule;
  2243. {$modeswitch externalclass}
  2244. type
  2245. TExtA = class external name 'ExtA'
  2246. private
  2247. function GetItems(Index: integer): String; external name '[]';
  2248. public
  2249. property Items[Index: integer]: String read GetItems; default;
  2250. end;
  2251. var
  2252. Obj: TExtA;
  2253. s: String;
  2254. Begin
  2255. ... get Obj from somewhere ...
  2256. s:=Obj[2];
  2257. End.
  2258. </pre>
  2259. </td>
  2260. <td>
  2261. <pre>rtl.module("program",["System"],function () {
  2262. var $mod = this;
  2263. this.Obj = undefined;
  2264. this.s = "";
  2265. $mod.$main = function () {
  2266. $mod.s = Obj[2];
  2267. };
  2268. });
  2269. </pre>
  2270. </td>
  2271. </tr>
  2272. </tbody>
  2273. </table>
  2274. Notes:
  2275. <ul>
  2276. <li>A property can have a mix of normal accessor and bracket accessor.
  2277. For example a bracket accessor as getter and a normal function as setter.</li>
  2278. </ul>
  2279. </div>
  2280. <div class="section">
  2281. <h2 id="rtti">RTTI - Run Time Type Information</h2>
  2282. The RTTI provides access to the type data of all published properties,
  2283. fields and methods. The type data provides similar information as Delphi/FPC,
  2284. but the internals are very different. Delphi/FPC uses pointers,
  2285. variant records and fake static arrays, which have no equivalent in JS.
  2286. Instead pas2js uses external classes. For example:
  2287. <pre>
  2288. TTypeInfo = class external name 'rtl.tTypeInfo'
  2289. public
  2290. Name: String external name 'name';
  2291. Kind: TTypeKind external name 'kind';
  2292. end;
  2293. TTypeInfoClass = class of TTypeInfo;
  2294. TTypeInfoInteger = class external name 'rtl.tTypeInfoInteger'(TTypeInfo)
  2295. public
  2296. MinValue: NativeInt external name 'minvalue';
  2297. MaxValue: NativeInt external name 'maxvalue';
  2298. OrdType : TOrdType external name 'ordtype';
  2299. end;
  2300. </pre>
  2301. The <b>typeinfo</b> function works on type, var, const and property identifiers.
  2302. By default it returns a <i>pointer</i>. If the typinfo unit is used it returns the
  2303. appropiate <i>TTypeInfo</i>. For instance <i>typeinfo(integer)</i> returns
  2304. a <i>TTypeInfoInteger</i>.<br>
  2305. <i>Typeinfo</i> of a <i>var</i> or <i>const</i> returns the typeinfo of its
  2306. type, not of its current runtime value. The exception is a class and class-of instance
  2307. variable (e.g. <i>var o: TObject; ... typeinfo(o)</i>), which returns the
  2308. typeinfo of the current runtime value.
  2309. If <i>o</i> is <i>null</i> it will give a JS error.<br>
  2310. Local types (i.e. inside a procedure) do not have typeinfo.<br>
  2311. Open array parameters are not yet supported.<br>
  2312. </div>
  2313. <div class="section">
  2314. <h2 id="compilerdirectives">Compiler directives</h2>
  2315. In config files:
  2316. <ul>
  2317. <li>#IFDEF macroname</li>
  2318. <li>#IFNDEF macroname</li>
  2319. <li>#IF expression - same as $if, except only defines</li>
  2320. <li>#ELSEIF</li>
  2321. <li>#ELSE</li>
  2322. <li>#ENDIF</li>
  2323. <li>#ERROR text</li>
  2324. </ul>
  2325. In source files:
  2326. <ul>
  2327. <li>{$Define <i>MacroName</i>}: defines macro <i>MacroName</i> with value '1'.</li>
  2328. <li>{$Define <i>MacroName:=value</i>}: defines macro <i>MacroName</i> with custom value.</li>
  2329. <li>{$Undef <i>MacroName</i>}: undefines macro <i>MacroName</i>.</li>
  2330. <li>{$IfDef <i>MacroName</i>}: if <i>MacroName</i> is not defined, skip to next $Else or $EndIf. Can be nested.</li>
  2331. <li>{$IfNDef <i>MacroName</i>}: as $IfDef, except negated.</li>
  2332. <li>{$If <i>boolean expression</i>}: if <i>expression</i> evaluates to true
  2333. (not '0'), skip to next $Else or $EndIf. Can be nested.<br>
  2334. Supported functions and operators:<br>
  2335. <ul>
  2336. <li>macro - replaced by its value, a simple define has value '1'</li>
  2337. <li>defined(macro) - '1' if defined, '0' otherwise</li>
  2338. <li>undefined(macro) - as <i>not defined(macro)</i></li>
  2339. <li>option(letter) - same as <i>{$IFOpt letter+}</i></li>
  2340. <li>not - first level of precedence</li>
  2341. <li>*, /, div, mod, and, shl, shr - second level of precedence</li>
  2342. <li>+, -, or, xor - third level of precedence</li>
  2343. <li>=, &lt;&gt;, &lt;, &gt;, &lt;=, &gt;= - fourth level of precedence</li>
  2344. <li>If the operands can be converted to numbers they are combined as numbers, otherwise as strings.</li>
  2345. </ul>
  2346. Not supported functions and operators:<br>
  2347. <ul>
  2348. <li>defined(Pascal identifier), undefined(Pascal identifier)</li>
  2349. <li>declared(Pascal identifier)</li>
  2350. <li>in operator</li>
  2351. </ul>
  2352. </li>
  2353. <li>{$IfOpt <i>Letter+,-</i>}: if <i>expression</i> evaluates to true (not '0'), skip to next $Else or $EndIf. Can be nested.</li>
  2354. <li>{$Else}: If previous $IfDef was skipped, execute next block, otherwise skip.</li>
  2355. <li>{$ElseIf <i>boolean expression</i>}: As $Else, except with an extra expression like $if to test. There can be multiple $elseif.</li>
  2356. <li>{$EndIf}: ends an $IfDef block</li>
  2357. <li>{$mode delphi} or {$mode objfpc}: Same as -Mdelphi or -Mobjfpc, but only for this unit. You can use units of both modes in a program. If present must be at the top of the unit, or after the module name.</li>
  2358. <li>{$modeswitch externalclass}: allow declaring external classes</li>
  2359. <li>{$macro on|off} enables macro replacements. Only macros with a custom value are replaced. Macros are never replaced inside directives.</li>
  2360. <li>{$I filename} or {$include filename} - insert include file</li>
  2361. <li>{$ERROR text}</li>
  2362. <li>{$WARNING text}</li>
  2363. <li>{$NOTE text}</li>
  2364. <li>{$HINT text}</li>
  2365. <li>{$M+}, {$TypeInfo on}: switches default visibility for class members from public to published</li>
  2366. </ul>
  2367. Defines:
  2368. <ul>
  2369. <li>PASJS</li>
  2370. <li>PAS2JS_FULLVERSION - major*1000+minor*100+release, e.g. 1.2.3 = 10203</li>
  2371. <li>Target platform: BROWSER, NODEJS</li>
  2372. <li>Target processor: ECMAScript5, ECMAScript6, ECMAScript=5</li>
  2373. <li>Mode: DELPHI, OBJFPC</li>
  2374. </ul>
  2375. </div>
  2376. <div class="section">
  2377. <h2 id="numbers">Numbers</h2>
  2378. JavaScript only supports double. All Pascal number types and enum values
  2379. are mapped to this. A double supports integers from<br>
  2380. MinInteger = -$10000000000000;<br>
  2381. MaxInteger = $fffffffffffff;<br>
  2382. MinDouble = 5.0e-324;<br>
  2383. MaxDouble = 1.7e+308;<br>
  2384. <br>
  2385. Intrinsic integer types:
  2386. <ul>
  2387. <li>Byte - unsigned 8-bit</li>
  2388. <li>ShortInt - signed 8-bit</li>
  2389. <li>Word - unsigned 16-bit</li>
  2390. <li>SmallInt - signed 16-bit</li>
  2391. <li>LongWord - unsigned 32-bit</li>
  2392. <li>LongInt - signed 32-bit</li>
  2393. <li>NativeUInt - unsigned 52-bit</li>
  2394. <li>NativeInt - signed 53-bit</li>
  2395. </ul>
  2396. Notes:
  2397. <ul>
  2398. <li>Division by zero does not raise an exception. 0/0 results in NaN, positive/0 is Infinity, negative/0 is -Infinity.</li>
  2399. <li>NaN&lt;&gt;NaN</li>
  2400. <li>Overflows work differently. For example in Delphi adding 100 to a byte of 200 gives <i>300 and $ff = 44</i>, while in pas2js it gives 300, which is not a byte anymore.</li>
  2401. <li>Math.isNan(double) tests for NaN. Otherwise false. isNan(Infinity)=false.</li>
  2402. <li>Math.isFinite(double) tests if not NaN, positive or negative infinity.</li>
  2403. <li>Math.isInfinite(double) tests if positive or negative infinity.</li>
  2404. <li>For more functions see unit Math.</li>
  2405. <li>To make porting easier Single is defined in the system unit as alias of
  2406. double, but gives a warning. Since using higher precision might give
  2407. unexpected results you should check every place.</li>
  2408. </ul>
  2409. </div>
  2410. <div class="section">
  2411. <h2 id="othersupportedelements">Other supported Pascal elements</h2>
  2412. <ul>
  2413. <li><b>break</b>, <b>continue</b>, <b>exit</b>, <b>exit()</b></li>
  2414. <li><b>chr</b>, <b>ord</b></li>
  2415. <li>alias type, but not type alias type</li>
  2416. <li>inc()/dec() to += -=</li>
  2417. <li>Converts "a div b" to "Math.floor(a / b)"</li>
  2418. <li>and, or, xor, not: logical and bitwise</li>
  2419. <li>Name conflicts with JS identifiers are automatically fixed by changing case.
  2420. For example a Pascal function "<i>apply"</i> is renamed to "<i>Apply</i>".</li>
  2421. <li>The built-in procedure <b>str</b> works with boolean, integer, float and enumvalue.<br>
  2422. Additionally there is <b>str</b> function, that takes an arbitrary number of
  2423. arguments and returns a concatenated string. It supports string as parameter too.
  2424. For example s:=str(i,' ',d:1:5).<br>
  2425. Width and precision is supported. str(i:10) will add spaces to the left to fill up to 10 characters.</b>
  2426. str(aDouble:1:5) returns a string in decimal format with 5 digits for the fraction.</li>
  2427. </ul>
  2428. </div>
  2429. <div class="section">
  2430. <h2 id="notsupportedelements">Not supported elements</h2>
  2431. <ul>
  2432. <li>Advanced records</li>
  2433. <li>Anonymous functions</li>
  2434. <li>Array of const</li>
  2435. <li>Attributes</li>
  2436. <li>Currency</li>
  2437. <li>Enumeration for..in..do</li>
  2438. <li>Enums with custom values</li>
  2439. <li>Generics</li>
  2440. <li>Global properties</li>
  2441. <li>Futures</li>
  2442. <li>Helpers for types, classes, records</li>
  2443. <li>Inline</li>
  2444. <li>Interfaces</li>
  2445. <li>Library</li>
  2446. <li>Nested types in class</li>
  2447. <li>Objects</li>
  2448. <li>Operator overloading</li>
  2449. <li>Pointer of record</li>
  2450. <li>Pointer arithmetic</li>
  2451. <li>Resources</li>
  2452. <li>RTTI extended, $RTTI</li>
  2453. <li>Runtime checks: Overflow -Co, $Q</li>
  2454. <li>Runtime checks: Range -Cr, $R</li>
  2455. <li>Runtime checks: Typecast -CR</li>
  2456. <li>Scoped enums</li>
  2457. <li>Set of char, boolean, custom range</li>
  2458. <li>Type alias, e.g. type TTranslateString = type string;</li>
  2459. <li>Var Absolute modifier</li>
  2460. <li>Variant records</li>
  2461. <li>Variants</li>
  2462. </ul>
  2463. </div>
  2464. <div class="section">
  2465. <h2 id="targetprocessor">JavaScript Version</h2>
  2466. Code generation depending on -P option:
  2467. <ul>
  2468. <li>ECMAScript5</li>
  2469. <li>ECMAScript6: using 0b for binary literals, and 0o for octal literals</li>
  2470. </ul>
  2471. </div>
  2472. <div class="section">
  2473. <h2 id="sourcemaps">Creating source maps</h2>
  2474. Source maps are files telling the browser what JavaScript comes from which
  2475. original source (e.g. Pascal file), similar to debug information in FPC/Delphi.<br>
  2476. In 2017 FireFox and Chrome supports source maps.<br>
  2477. You can enable generating source map files by using the <i>-Jm</i> option.<br>
  2478. The compiler generates one module.js.map file for every generated module.js file.
  2479. The last line of the .js file contains the line<br>
  2480. <i>//# sourceMappingURL=module.js.map</i><br>
  2481. telling the browser where to find the source map.<br>
  2482. The source map contains references to the Pascal files and included .js
  2483. files (e.g. -Jirtl.js) relative to the location of the source map.
  2484. Note that if the Pascal file lies in a parent directory, the relativ path
  2485. contains '../'. You can change the base directory of the relative paths by using
  2486. the option <i>-Jmbasedir=&lt;x&gt;</i>. For example <i>-JmC:\www\pas</i>
  2487. creates paths relative to C:\www\pas.<br>
  2488. You can set the base URL, where the browser finds the Pascal sources, by passing
  2489. the <i>-Jmsourceroot=&lt;x&gt;</i> option. For example
  2490. <i>-Jmsourceroot=http://www.yoursite.com/pas/</i>. The browser prepends this
  2491. to the source map filenames when downloading the original source files
  2492. (e.g. the .pas files).<br>
  2493. You can include the whole Pascal sources in the source map using the option
  2494. <i>-Jminclude</i>.<br>
  2495. <br>
  2496. To show the generated mapping for each line you can use the tool fpc/packages/fcl-js/examples/srcmapdump.<br>
  2497. </div>
  2498. <div id="footer">
  2499. </div>
  2500. </body>
  2501. </html>