translation.html 94 KB

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