translation.html 94 KB

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