13af6ab5fSopenharmony_ci# es2panda_lib generation
23af6ab5fSopenharmony_ci
33af6ab5fSopenharmony_ci### How to run:
43af6ab5fSopenharmony_ci```bash
53af6ab5fSopenharmony_cininja gen_api   # only generates es2panda_lib.cpp and es2panda_lib.h
63af6ab5fSopenharmony_ci
73af6ab5fSopenharmony_cininja es2panda-public   # compiles es2panda_lib
83af6ab5fSopenharmony_ci```
93af6ab5fSopenharmony_ci
103af6ab5fSopenharmony_ci### Tests:
113af6ab5fSopenharmony_ciTests are located in `ets_frontend/ets2panda/test/unit/public`, their names start with "e2p_test_plugin".
123af6ab5fSopenharmony_ci
133af6ab5fSopenharmony_ciRun tests:
143af6ab5fSopenharmony_ci```bash
153af6ab5fSopenharmony_cininja es2panda-plugin-test
163af6ab5fSopenharmony_ci```
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci## Notes, how to work with cppToCTypes.yaml
203af6ab5fSopenharmony_ciIn the c++ code of es2panda, there are types that are not presents in c. es2panda_lib must have a c interface. cppToCTypes.yaml describes the translation of complex types from c++ to c.
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_ciWhy do we describe working with arguments and not with types? Because 1 argument from c++ can expand into several c arguments. For example, `std::vector<int> A` in c will be represented as `int* A, int ALen`.
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ciLet's look at the example of `std::pair<type_1, type_2> some_name` how to use this yaml:
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_ci### es2panda_arg
293af6ab5fSopenharmony_ciThis is the key field that is used for pattern matching! Patterns in the form of `|some_identifier|`.
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_ci```
323af6ab5fSopenharmony_ci- es2panda_arg:
333af6ab5fSopenharmony_ci    name: '|arg_name|'
343af6ab5fSopenharmony_ci    type:
353af6ab5fSopenharmony_ci        name: std::pair
363af6ab5fSopenharmony_ci        template_args:
373af6ab5fSopenharmony_ci        - type: '|first_type|'
383af6ab5fSopenharmony_ci        - type: '|seond_type|'
393af6ab5fSopenharmony_ci```
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_ciLet's assume that in .rb, the search for arguments suitable for this template takes place using validation:
423af6ab5fSopenharmony_ci`es2panda_arg["type"]["name"] == candidate_arg["type"]["name"]`.
433af6ab5fSopenharmony_ci
443af6ab5fSopenharmony_ciSuppose our candidate is `std::pair<int*, int> A`.
453af6ab5fSopenharmony_ciParsed to yaml is:
463af6ab5fSopenharmony_ci```
473af6ab5fSopenharmony_ci- name: A
483af6ab5fSopenharmony_ci  type:
493af6ab5fSopenharmony_ci    name: std::pair
503af6ab5fSopenharmony_ci    template_args:
513af6ab5fSopenharmony_ci    - type:
523af6ab5fSopenharmony_ci        name: int
533af6ab5fSopenharmony_ci        ptr_depth: 1
543af6ab5fSopenharmony_ci    - type:
553af6ab5fSopenharmony_ci        name: int
563af6ab5fSopenharmony_ci```
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_cies2panda_arg ONLY serves to match templates for the desired type from c++.
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ciIn our example we'll have:
613af6ab5fSopenharmony_ci```
623af6ab5fSopenharmony_ci|arg_name| = A
633af6ab5fSopenharmony_ci|first_type| = {"name": int, "ptr_depth": 1}
643af6ab5fSopenharmony_ci|second_type| = {"name": int}
653af6ab5fSopenharmony_ci```
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_ciIn any subsequent fields in cppToCTypes.yaml we can use these templates. They will be replaced with the desired values.
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ciIMPORTANT! The raw argument from es2panda will be saved intact and will not be changed. Therefore, it is not necessary to describe all possible fields for an argument in es2panda_arg, only those that are used to match templates of the form `|some_field|` and filter the argument that fits this template (usually `es2panda["type"]["name"]`) are needed.
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_ci### Features
723af6ab5fSopenharmony_ciNot in es2panda args you can use `|some_code|` pattern like .rb code snippet. 
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ciExample:
753af6ab5fSopenharmony_ci```rb
763af6ab5fSopenharmony_ci|es2panda_arg.type.ptr_depth|
773af6ab5fSopenharmony_ci# or
783af6ab5fSopenharmony_ci|new_args.0.type.ptr_depth|
793af6ab5fSopenharmony_ci```
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_ciNote: Recall that es2panda_arg is used only for pattern matching, and after matching, the raw es2panda argument is copied in its entirety. Therefore, even if you did not describe the ptr_depth field in es2panda_arg, but it is in the raw argument itself, you will be able to get this value.
82