translation.html 92 KB

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