js-application-framework.md 10.0 KB
Newer Older
L
liudongmiao 已提交
1 2 3 4 5 6 7 8
# JS Application Framework<a name="EN-US_TOPIC_0000001078402300"></a>

-   [Introduction](#section11660541593)
-   [Directory Structure](#section1464106163817)
-   [Constraints](#section1718733212019)
-   [Using targets](#section1460013282612)
-   [Using Runtime-core](#section1460223932718)
-   [Repositories Involved](#section11703194974217)
W
wenjun 已提交
9 10 11

## Introduction<a name="section11660541593"></a>

12
The JS application framework allows you to develop web-like applications across platforms. The framework uses Toolkit to pack your  **.hml**,  **.css**, and  **.js**  files to a JavaScript bundle, parses the bundle, generates the native UI view component tree, and then renders it for display. You can use the declarative APIs to develop applications. This allows data to drive view changes and avoids a large number of view operations, greatly simplifying application development.
W
wenjun 已提交
13 14 15

The following figure shows the framework modules.

L
liudongmiao 已提交
16 17
**Figure  1**  Framework architecture<a name="fig1771713591545"></a>  
![](figures/framework-architecture.png "framework-architecture")
W
wenjun 已提交
18 19 20

## Directory Structure<a name="section1464106163817"></a>

L
liudongmiao 已提交
21
The source code of the framework is stored in  **/foundation/ace/ace\_engine\_lite**. The directory structure is as follows.
W
wenjun 已提交
22 23

```
L
liudongmiao 已提交
24 25 26 27 28 29 30 31 32 33
/foundation/ace/ace_engine_lite
├── frameworks       # Framework code
│   ├── examples    # Sample code
│   ├── include     # Header files
│   ├── packages    # JavaScript implementation
│   ├── src        # Source code
│   ├── targets     # Configuration files of target devices
│   └── tools       # Tool code
├── interfaces       # APIs exposed externally
│   └── innerkits   # Header files for internal subsystems
34
│       └── builtin # JavaScript third-party module APIs exposed by the JS application framework
L
liudongmiao 已提交
35
└── test             # Test cases
W
wenjun 已提交
36 37 38 39
```

## Constraints<a name="section1718733212019"></a>

40
-   Language versions:
W
wenjun 已提交
41
    -   C++11 or later
42
    -   JavaScript ES5.1
W
wenjun 已提交
43 44 45


-   Framework runtime memory consists of:
46 47
    -   Runtime memory for the JavaScript engine: The memory size is adjustable and depends on the complexity of the device application. Generally, 64 KB to 512 KB is recommended.
    -   Native memory for the framework itself: For devices whose memory capacity exceeds 100 KB, it is recommended that a pre-allocated memory pool be used for native memory management. The memory pool is shared with the native UI framework.
W
wenjun 已提交
48

49
-   The framework provides different specifications for various chip platforms and underlying OS capabilities:
W
wenjun 已提交
50 51 52 53 54
    -   Cortex-M RAM and ROM
        -   JavaScript engine memory pool: greater than 48 KB \(recommended\)
        -   RAM: memory pool shared with the native UI \(recommended\). The size must be greater than 80 KB.
        -   ROM: greater than 300 KB \(for the JS application framework and related subsystems, such as native UI and JavaScript engine\)

55
    -   Cortex-A RAM and ROM
W
wenjun 已提交
56 57 58 59 60 61 62 63
        -   JavaScript engine memory pool: greater than 128 KB \(recommended\)
        -   RAM: greater than 512 KB \(recommended\)
        -   ROM: greater than 2 MB \(for the JS application framework and related subsystems, such as native UI and JavaScript engine\)



## Using targets<a name="section1460013282612"></a>

64 65 66 67
The implementation of the JS application framework consists of the following two parts:

-   Native part: The native part is developed in C++ and is the main body of the framework.
-   JavaScript part: The JavaScript part supports the runtime environment of JavaScript files, and supports the interaction between the JavaScript runtime and native framework through some global functions or objects exposed to the JavaScript engine.
W
wenjun 已提交
68

L
liudongmiao 已提交
69
The framework uses feature macros to customize function code to be compiled on different platforms. The feature macros are stored in header files in  **foundation/ace/ace\_engine\_lite/frameworks/targets**. The directory structure is as follows:
W
wenjun 已提交
70 71

```
L
liudongmiao 已提交
72
/foundation/ace/ace_engine_lite/frameworks/targets
W
wenjun 已提交
73 74
├── default/
│   └── acelite_config.h
L
liudongmiao 已提交
75
├── linux/                # Linux environment configuration files
W
wenjun 已提交
76
│   └── acelite_config.h
L
liudongmiao 已提交
77
 ├── liteos_a/            # Environment configuration files for LiteOS Cortex-A
W
wenjun 已提交
78
│   └── acelite_config.h
L
liudongmiao 已提交
79
├── liteos_m/             # Environment configuration files for LiteOS Cortex-M
W
wenjun 已提交
80 81 82
│   └── acelite_config.h
├── platform_adapter.cpp
├── platform_adapter.h
L
liudongmiao 已提交
83 84 85
└── simulator/            # Simulator environment configuration files
    ├── acelite_config.h
    └── BUILD.gn
W
wenjun 已提交
86 87
```

L
liudongmiao 已提交
88
Note: Currently only the target compilation for LiteOS Cortex-A is open-source, which is built using Ninja \(BUILD.gn\). Other targets such as simulator \(CMake+MingW\) and LiteOS Cortex-M \(IAR\) are not completely open and will be gradually released after the adaptation is complete. The following examples describe the role of the  **targets**  directory in building different targets.
89

L
liudongmiao 已提交
90
When compiling for different platform targets, use the  **acelite\_config.h**  file in the corresponding platform directory. You can configure the header file searching path for compilation to locate the file to use. The following takes Ninja and CMake build tools as examples:
W
wenjun 已提交
91

L
liudongmiao 已提交
92
-   Ninja
W
wenjun 已提交
93

94 95
    ```
      if (ohos_kernel_type == "liteos_a" || ohos_kernel_type== "liteos_m" ||
L
liudongmiao 已提交
96 97
          ohos_kernel_type == "liteos_riscv") { # Select different header file searching paths based on the target kernel platform.
        include_dirs += [ "targets/liteos_a" ]
98 99 100 101
      } else if (ohos_kernel_type == "linux") {
        include_dirs += [ "targets/linux" ]
      }
    ```
W
wenjun 已提交
102 103


L
liudongmiao 已提交
104
-   CMake
105 106

    ```
W
wenjun 已提交
107
    ......
L
liudongmiao 已提交
108 109
    set(ACE_LITE_CONFIG_PATH "${CMAKE_CURRENT_SOURCE_DIR}/targets/simulator") # Set the simulator search path to targets/simulator.
    set(ACE_LITE_INNERKITS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../interfaces/innerkits/builtin")
110
    set(JSFWK_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/include")
L
liudongmiao 已提交
111
    set(JSFWK_INNERKITS_BUILTIN_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../foundation/ace/ace_engine_lite/interfaces/innerkits/builtin")
112
    set(JSFWK_SOURCE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/src/core")
L
liudongmiao 已提交
113 114 115
    set(UIKIT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../foundation/graphic/lite")
    set(THIRTY_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../third_party")
    set(JSFWK_SIMULATOR_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../tools/developer_tools_lite/graphic_tool/simulator")
116
    set(AAFWK_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../aafwk")
L
liudongmiao 已提交
117
    set(UTILS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../utils/native/lite")
118 119 120 121 122 123 124 125 126 127 128 129
    
    # header files
    include_directories(
        ${ACE_LITE_CONFIG_PATH}
        ${JSFWK_INCLUDE_PATH}/async
        ${JSFWK_INCLUDE_PATH}/base
        ${JSFWK_INCLUDE_PATH}/context
        ${JSFWK_INCLUDE_PATH}/jsi
        ${JSFWK_SOURCE_PATH}
        ......
    ```

W
wenjun 已提交
130 131 132

The  **acelite\_config.h**  file is used to enable or disable the feature macros of different platforms. It can also be used to define constants for shielding platform differences. For example, platform file systems are different, and the names of some fixed directories might be different. These constants can be defined as follows:

L
liudongmiao 已提交
133
-   liteos\_a/acelite\_config.h
W
wenjun 已提交
134

135 136 137
    ```
    #define JS_FRAMEWORK_PATH "//system/ace/bin/"
    ```
W
wenjun 已提交
138 139


L
liudongmiao 已提交
140
-   simulator/acelite\_config.h
141 142 143 144 145

    ```
    #define JS_FRAMEWORK_PATH "..\\..\\..\\jsfwk\\packages\\runtime-core\\build"
    ```

W
wenjun 已提交
146 147 148 149 150 151

## Using Runtime-core<a name="section1460223932718"></a>

Runtime-core is a JavaScript-based simple data hijacking framework provided by the JS application framework to implement unidirectional data binding. The directory structure is as follows:

```
L
liudongmiao 已提交
152
/foundation/ace/ace_engine_lite/frameworks/packages
W
wenjun 已提交
153
└── runtime-core
L
liudongmiao 已提交
154 155 156 157 158
    ├── .babelrc          # Babel configuration file
    ├── contribution.md
    ├── .editorconfig     # IDE configuration file
    ├── .eslintignore     # Configuration file of the ESLint tool. You can set a directory or files that will not be scanned by the ESLint tool.
    ├── .eslintrc.js      # ESLint configuration file for scanning rules
W
wenjun 已提交
159
    ├── .gitignore
L
liudongmiao 已提交
160
    ├── package.json      # NPM file
161
    ├── package-lock.json # NPM dependency lock file
L
liudongmiao 已提交
162 163 164 165
    ├── .prettierrc       # Configuration file for code formatting rules
     ├── scripts          # Directory for compilation scripts
    │   ├── build.js     # Compilation script
    │   └── configs.js   # Rollup configuration file
W
wenjun 已提交
166
    ├── .size-snapshot.json
L
liudongmiao 已提交
167 168
    └── src               # Source code
        ├── core          # ViewModel core implementation code
W
wenjun 已提交
169 170
        │   └── index.js
        ├── index.js
L
liudongmiao 已提交
171
        ├── observer      # Data hijacking implementation code
W
wenjun 已提交
172 173 174 175
        │   ├── index.js
        │   ├── observer.js
        │   ├── subject.js
        │   └── utils.js
L
liudongmiao 已提交
176
        ├── profiler      # profiler directory
W
wenjun 已提交
177
        │   └── index.js
L
liudongmiao 已提交
178
        └── __test__      # Test cases
W
wenjun 已提交
179 180 181 182 183 184 185
            └── index.test.js
```

The following NPM commands are supported:

-   **npm run build**

186
    The JavaScript engine integrated in the JS application framework supports ES5.1 syntax only. However, the runtime-core is implemented using JavaScript ES6. Therefore, you should use Babel for syntax degradation and use Rollup to package the code. Run the  **npm run build**  command, and the packaged files are output to the  **build**  directory. 
W
wenjun 已提交
187 188 189

    ```
    build/
L
liudongmiao 已提交
190
    ├── framework-dev.js     // Framework code used in the development environment (uncompressed and obfuscated)
W
wenjun 已提交
191
    ├── framework-dev.min.js // Framework code used in the development environment (compressed and obfuscated)
L
liudongmiao 已提交
192 193
    ├── framework.js         // Framework code used in the production environment (uncompressed and obfuscated)
    └── framework.min.js     // Framework code used in the production environment (compressed and obfuscated)
W
wenjun 已提交
194 195 196 197 198 199 200 201 202
    ```

-   **npm run test**

    Runtime-core uses Jest for unit testing. Run the  **npm run test**  command to start the unit test.


## Repositories Involved<a name="section11703194974217"></a>

L
liudongmiao 已提交
203
ace\_engine\_lite
W
wenjun 已提交
204