translation.html 78 KB

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