translation.html 109 KB

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