translation.html 75 KB

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