13af6ab5fSopenharmony_ci 23af6ab5fSopenharmony_ci# The cheat sheet for ArkTS frontend 33af6ab5fSopenharmony_ci 43af6ab5fSopenharmony_ci### How to compile a module 53af6ab5fSopenharmony_ci 63af6ab5fSopenharmony_ciAt the beginning, need to define what we want to compile. 73af6ab5fSopenharmony_ciThere are three kinds of compilation units: 83af6ab5fSopenharmony_ci 93af6ab5fSopenharmony_ci- *Separate modules* 103af6ab5fSopenharmony_ci- *Declaration modules* 113af6ab5fSopenharmony_ci- *Packages* 123af6ab5fSopenharmony_ci 133af6ab5fSopenharmony_ci(For more information, see ArkTS spec 13 chapter) 143af6ab5fSopenharmony_ciIn any case, regardless of whether we have an entry point or not, the compilation 153af6ab5fSopenharmony_ciand execution commands will be the same and look like this: 163af6ab5fSopenharmony_ci 173af6ab5fSopenharmony_ci```bash 183af6ab5fSopenharmony_ci# compilation of separate module and library 193af6ab5fSopenharmony_ci./bin/es2panda --extension=sts --output=out.abc --opt-level=2 x.sts 203af6ab5fSopenharmony_ci./bin/es2panda --extension=sts --output=etsstdlib.abc --opt-level=2 --gen-stdlib=true 213af6ab5fSopenharmony_ci# execution command 223af6ab5fSopenharmony_ci./bin/ark --boot-panda-files=etsstdlib.abc --load-runtimes=ets out.abc ETSGLOBAL::main 233af6ab5fSopenharmony_ci``` 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ciMore details about separate modules, conceptually: 263af6ab5fSopenharmony_ci- A separate module can have an entry point (be a program) or be a module with no entry point 273af6ab5fSopenharmony_ciwhich can be imported and used as a kind of library 283af6ab5fSopenharmony_ci- If a module has an entry point then 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ci``` 313af6ab5fSopenharmony_ciEntry_point := ArkTS_main | ArkTS_top-level-statements | ArkTS_top-level-statements ArkTS_main - 3 options 323af6ab5fSopenharmony_ciThus, Runtime_main () { 333af6ab5fSopenharmony_ci if (ArkTS_top_level_statements_in_place) call ArkTS_top-level-statements // 1st step 343af6ab5fSopenharmony_ci if (ArkTS_main_in_place) call ArkTS_main // 2nd step 353af6ab5fSopenharmony_ci} // Case, when both are missed, should never happen - guaranteed by front-end and code generation 363af6ab5fSopenharmony_ci``` 373af6ab5fSopenharmony_ci 383af6ab5fSopenharmony_ciIn practice: 393af6ab5fSopenharmony_ciFor the 1st step all top-level statements (see spec 13.8 point) will be placed inside 403af6ab5fSopenharmony_ci*ETSGLOBAL._$init$_()* function which is called inside *ETSGLOBAL._cctor_()*. 413af6ab5fSopenharmony_ciFor the 2nd step we just always generate *ETSGLOBAL.main()* function and call after 423af6ab5fSopenharmony_ciconstructor for ETSGLOBAL. But also *ETSGLOBAL._$init$_()* can be called as a consequence of `main` call. 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_ci*Note1:* `main` entrypoint method is possibly synthetic and always exists actually. 453af6ab5fSopenharmony_ci*Note2:* Package modules are not fully supported, see #16267 internal issue. 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_ci### How to execute a module 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ciThe process of program execution on our VM is the same in all cases. 503af6ab5fSopenharmony_ciTo execute compiled module *out.abc* we have several options: 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_ci```bash 533af6ab5fSopenharmony_ci# Interpreter 543af6ab5fSopenharmony_ci./bin/ark --compiler-enable-jit=false --boot-panda-files=etsstdlib.abc \ 553af6ab5fSopenharmony_ci--load-runtimes=ets out.abc ETSGLOBAL::main 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ci# JIT 583af6ab5fSopenharmony_ci./bin/ark --compiler-enable-jit=true --boot-panda-files=etsstdlib.abc \ 593af6ab5fSopenharmony_ci --load-runtimes=ets out.abc ETSGLOBAL::main 603af6ab5fSopenharmony_ci 613af6ab5fSopenharmony_ci# AOT - 2 steps 623af6ab5fSopenharmony_ci./bin/ark_aot --boot-panda-files=etsstdlib.abc --load-runtimes=ets \ 633af6ab5fSopenharmony_ci--paoc-panda-files out.abc --paoc-output out.aot 643af6ab5fSopenharmony_ci./bin/ark --boot-panda-files=etsstdlib.abc --load-runtimes=ets \ 653af6ab5fSopenharmony_ci--aot-files=out.aot out.abc ETSGLOBAL::main 663af6ab5fSopenharmony_ci``` 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ciSome additional options that can be helpful: 693af6ab5fSopenharmony_ci- *--no-async-jit* 703af6ab5fSopenharmony_ci- *--compiler-hotness-threshold=0* (to initiate jit compilation on the first method call) 713af6ab5fSopenharmony_ciFor more details see *./bin/ark --help*. 723af6ab5fSopenharmony_ci 733af6ab5fSopenharmony_ci### How to compile a library module 743af6ab5fSopenharmony_ci 753af6ab5fSopenharmony_ciThe library module is compiled the same way as a regular module: 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_ci```bash 783af6ab5fSopenharmony_ci./bin/es2panda --extension=sts --output=out.abc --opt-level=2 --gen-stdlib=false /path/to/module/folder 793af6ab5fSopenharmony_ci``` 803af6ab5fSopenharmony_ci 813af6ab5fSopenharmony_ci*Notes:* 823af6ab5fSopenharmony_ci- Standard library is an implicit import as defined in spec 13.5 point. 833af6ab5fSopenharmony_ciAs for now it's imported via *arktsconfig.json* file. This file is generated by cmake in the 843af6ab5fSopenharmony_ci*path/to/build/tools/es2panda/generated/* folder. It has 2 default paths related to stdlib - 853af6ab5fSopenharmony_ci*std* and *compat*. They're imported in each module that we compile. 863af6ab5fSopenharmony_ci- Package modules are not fully supported, see #16267 internal issue. 873af6ab5fSopenharmony_ci 883af6ab5fSopenharmony_ci### How to use .abc and .d.sts without samples 893af6ab5fSopenharmony_ci 903af6ab5fSopenharmony_citodo 913af6ab5fSopenharmony_ci 923af6ab5fSopenharmony_ci### How to find out what is in .abc 933af6ab5fSopenharmony_ci 943af6ab5fSopenharmony_ciTo see what entities were generated by es2panda, we have *ark_disasm* tool. 953af6ab5fSopenharmony_ciIts input is .abc file, its output is .pa (panda assembler) file. 963af6ab5fSopenharmony_ci 973af6ab5fSopenharmony_ci```bash 983af6ab5fSopenharmony_ci./bin/ark_disasm out.abc out.pa 993af6ab5fSopenharmony_ci``` 1003af6ab5fSopenharmony_ci 1013af6ab5fSopenharmony_ciIt shows all functions, instructions, records, also their flags, access modifiers 1023af6ab5fSopenharmony_ciand external/internal marks. 1033af6ab5fSopenharmony_ci 1043af6ab5fSopenharmony_ci### How package names are involved in the program 1053af6ab5fSopenharmony_ci 1063af6ab5fSopenharmony_ciPackage names become a prefix of all entities declared in a package. For example, 1073af6ab5fSopenharmony_ciif we have such code: 1083af6ab5fSopenharmony_ci 1093af6ab5fSopenharmony_ci```typescript 1103af6ab5fSopenharmony_cipackage P1 1113af6ab5fSopenharmony_cifunction foo() {} 1123af6ab5fSopenharmony_ci``` 1133af6ab5fSopenharmony_ci 1143af6ab5fSopenharmony_ciThe foo name in the bytecode will be *P1.ETSGLOBAL.foo*. 1153af6ab5fSopenharmony_ci*Note:* For now we have some problems with generating names for packages 1163af6ab5fSopenharmony_ci 1173af6ab5fSopenharmony_ci### About —global-module-prefix option 1183af6ab5fSopenharmony_ci 1193af6ab5fSopenharmony_citodo 1203af6ab5fSopenharmony_ci 1213af6ab5fSopenharmony_ci### How does global scope affect .abc 1223af6ab5fSopenharmony_ci 1233af6ab5fSopenharmony_citodo 1243af6ab5fSopenharmony_ci 1253af6ab5fSopenharmony_ci### What is available without importing 1263af6ab5fSopenharmony_ci 1273af6ab5fSopenharmony_ciImplicit import is defined in spec 13.5 point. In details, we have *arktsconfig.json* 1283af6ab5fSopenharmony_cifile. This file is generated by cmake in the *path/to/build/tools/es2panda/generated/* 1293af6ab5fSopenharmony_cifolder. It has 2 default paths related to stdlib - *std* and *compat*. They're imported 1303af6ab5fSopenharmony_ciin each module that we compile. The default config file looks like this: 1313af6ab5fSopenharmony_ci 1323af6ab5fSopenharmony_ci```json 1333af6ab5fSopenharmony_ci{ 1343af6ab5fSopenharmony_ci "compilerOptions": { 1353af6ab5fSopenharmony_ci "baseUrl": "/path/to/static_core", 1363af6ab5fSopenharmony_ci "paths": { 1373af6ab5fSopenharmony_ci "std": ["/path/to/static_core/plugins/ets/stdlib/std"], 1383af6ab5fSopenharmony_ci "escompat": ["/path/to/static_core/plugins/ets/stdlib/escompat"], 1393af6ab5fSopenharmony_ci "import_tests": ["/path/to/static_core/tools/es2panda/test/parser/ets/import_tests"], 1403af6ab5fSopenharmony_ci "dynamic_import_tests": ["/path/to/static_core/tools/es2panda/test/parser/ets/dynamic_import_tests"] 1413af6ab5fSopenharmony_ci }, 1423af6ab5fSopenharmony_ci "dynamicPaths": { 1433af6ab5fSopenharmony_ci "dynamic_js_import_tests": {"language": "js", "hasDecl": false}, 1443af6ab5fSopenharmony_ci "/path/to/static_core/tools/es2panda/test/parser/ets/dynamic_import_tests": {"language": "js", "hasDecl": true} 1453af6ab5fSopenharmony_ci } 1463af6ab5fSopenharmony_ci } 1473af6ab5fSopenharmony_ci} 1483af6ab5fSopenharmony_ci``` 1493af6ab5fSopenharmony_ci 1503af6ab5fSopenharmony_ciIn additional, implicitly imported paths can be added. After that, your custom config file 1513af6ab5fSopenharmony_cishould be passed to the appropriate option: 1523af6ab5fSopenharmony_ci 1533af6ab5fSopenharmony_ci```bash 1543af6ab5fSopenharmony_ci./bin/es2panda --extension=sts --output=out.abc --opt-level=2 --gen-stdlib=false \ 1553af6ab5fSopenharmony_ci--arktsconfig=/path/to/arktsconfig.json x.sts 1563af6ab5fSopenharmony_ci``` 1573af6ab5fSopenharmony_ci 1583af6ab5fSopenharmony_ci### How to make one .abc file from two 1593af6ab5fSopenharmony_ci 1603af6ab5fSopenharmony_ciTool *ark_link* can do it. Need to pass an arbitrary number of files as arguments to 1613af6ab5fSopenharmony_cithis application and it will output a combined .abc file. 1623af6ab5fSopenharmony_ci 1633af6ab5fSopenharmony_ci```bash 1643af6ab5fSopenharmony_ci./bin/ark_link --output out.abc -- a.abc b.abc 1653af6ab5fSopenharmony_ci``` 1663af6ab5fSopenharmony_ci 1673af6ab5fSopenharmony_ci*Note:* Be aware of redefinition. 1683af6ab5fSopenharmony_ci 1693af6ab5fSopenharmony_ci### How to connect the native .so 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ciHave 2 options to form a correct .so for further work with it: 1723af6ab5fSopenharmony_ci- Need to register all the functions that will be called from ts on the native side. 1733af6ab5fSopenharmony_ciHow to do this - see the file peer_lib/cpp/arkts/convertors-ark.cc/convertors-ark.cc 1743af6ab5fSopenharmony_cifrom the https://gitee.com/nikolay-igotti/idlize/ repo 1753af6ab5fSopenharmony_ci- Register through the construction: 1763af6ab5fSopenharmony_ci"ETS_EXPORT ets_"return type" ETS_CALL ETS_classname_methodname(EtsEnv *, args) {..}", 1773af6ab5fSopenharmony_cie.g.: 1783af6ab5fSopenharmony_ci 1793af6ab5fSopenharmony_ci```cpp 1803af6ab5fSopenharmony_ciextern "C" ETS_EXPORT ets_int ETS_CALL EtsNapiOnLoad(EtsEnv *env) { 1813af6ab5fSopenharmony_ci if (!registerNatives(env, env->FindClass("NativeModule.NativeModule"))) return -1; 1823af6ab5fSopenharmony_ci return ETS_NAPI_VERSION_1_0; 1833af6ab5fSopenharmony_ci} 1843af6ab5fSopenharmony_ci``` 1853af6ab5fSopenharmony_ci 1863af6ab5fSopenharmony_ci### How to execute program with native .so 1873af6ab5fSopenharmony_ci 1883af6ab5fSopenharmony_ciNeed to write *loadLibrary("name of the .so")* in the right place on the ts side. 1893af6ab5fSopenharmony_ciAn example can be found in *NativeModule.sts* file which can be generated by: 1903af6ab5fSopenharmony_ci 1913af6ab5fSopenharmony_ci```bash 1923af6ab5fSopenharmony_cinpm panda:sdk:install 1933af6ab5fSopenharmony_ciarkts:make 1943af6ab5fSopenharmony_ci``` 1953af6ab5fSopenharmony_ci 1963af6ab5fSopenharmony_ciFinally, need to execute with LD_LIBRARY_PATH=/path/to/directory/with/so passed to ark, 1973af6ab5fSopenharmony_ciso that the code that loads the lib will see it. 1983af6ab5fSopenharmony_ci 1993af6ab5fSopenharmony_ci### How to connect interop with dynamic js vm 2003af6ab5fSopenharmony_ci 2013af6ab5fSopenharmony_citodo 2023af6ab5fSopenharmony_ci 2033af6ab5fSopenharmony_ci### What are the limits of the internal keyword? One .abc? One module? 2043af6ab5fSopenharmony_ci 2053af6ab5fSopenharmony_citodo 206