translation.html 77 KB

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