1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "js_app_environment.h"
16 #include "ace_event_error_code.h"
17 #include "ace_log.h"
18 #include "async_task_manager.h"
19 #include "handler.h"
20 #include "js_app_context.h"
21 #include "js_framework_raw.h"
22 #include "js_fwk_common.h"
23 #include "js_profiler.h"
24 #include "module_manager.h"
25 #include "platform_adapter.h"
26 #include "presets/app_data_module.h"
27 #include "presets/console_module.h"
28 #include "presets/feature_ability_module.h"
29 #include "presets/intl_module.h"
30 #include "presets/jstest_module.h"
31 #include "presets/localization_module.h"
32 #include "presets/profiler_module.h"
33 #include "presets/render_module.h"
34 #include "presets/require_module.h"
35 #if (FEATURE_SYSCAP_MODULE == 1)
36 #include "presets/syscap_module.h"
37 #endif
38 #include "presets/timer_module.h"
39 #include "presets/version_module.h"
40 #include "product_adapter.h"
41 #include "system_info.h"
42 #if (JS_ENGINE_STATIC_MULTI_CONTEXTS_ENABLED == 1)
43 #include "generate-bytecode.h"
44 #endif
45 
46 namespace OHOS {
47 namespace ACELite {
JsAppEnvironment()48 JsAppEnvironment::JsAppEnvironment()
49 {
50     SetEngineSnapshotMode(snapshotMode_);
51 }
52 
LoadAceBuiltInModules() const53 void JsAppEnvironment::LoadAceBuiltInModules() const
54 {
55     ConsoleModule::Load();
56     RenderModule::Load();
57     RequireModule::Load();
58     FeaAbilityModule::Load();
59     JsTestModule::Load();
60     TimersModule::Load();
61 #if (FEATURE_SYSCAP_MODULE == 1)
62     SyscapsModule::Load();
63 #endif
64     PerformaceProfilerModule::Load();
65     AceVersionModule::Load();
66     IntlControlModule::Load();
67     AppDataModule::Load();
68 }
69 
InitJsFramework() const70 void JsAppEnvironment::InitJsFramework() const
71 {
72     START_TRACING(ENGINE_INIT);
73 #if (JS_ENGINE_STATIC_MULTI_CONTEXTS_ENABLED == 1)
74     js_task_context_init();
75 #endif
76 #if (JERRY_PORTING_DEPENDENCY == 0)
77     Srand((unsigned)jerry_port_get_current_time());
78 #endif
79     Debugger::GetInstance().SetupJSContext();
80     jerry_init(JERRY_INIT_EMPTY);
81     STOP_TRACING();
82     START_TRACING(FWK_INIT);
83 #if (JSFWK_TEST == 1)
84     jerry_value_t globalThis = jerry_get_global_object();
85     jerry_release_value(jerryx_set_property_str(globalThis, "globalThis", globalThis));
86     jerry_release_value(globalThis);
87 #endif // JSFWK_TEST
88     AsyncTaskManager::GetInstance().Init();
89     LoadAceBuiltInModules();
90     ProductAdapter::LoadExtraPresetModules();
91     LoadFramework();
92     LocalModule::Load();
93     SystemInfo::GetInstance().Initialize();
94     STOP_TRACING();
95 }
96 
LoadFramework() const97 void JsAppEnvironment::LoadFramework() const
98 {
99     size_t len = 0;
100     // load framework js/snapshot file to buffer
101     const char * const jsFrameworkScript = GetFrameworkRawBuffer(snapshotMode_, len);
102     const jerry_char_t *jScript = reinterpret_cast<const jerry_char_t *>(jsFrameworkScript);
103     // eval framework to expose
104     START_TRACING(FWK_CODE_EVAL);
105 
106     jerry_value_t retValue = UNDEFINED;
107     if (snapshotMode_) {
108         retValue = jerry_exec_snapshot(reinterpret_cast<const uint32_t *>(jScript), len, 0, 1);
109     } else {
110         retValue = jerry_eval(jScript, len, JERRY_PARSE_NO_OPTS);
111     }
112     STOP_TRACING();
113     bool hasError = jerry_value_is_error(retValue);
114     if (hasError) {
115         HILOG_ERROR(HILOG_MODULE_ACE, "Failed to load JavaScript framework.");
116         ACE_ERROR_CODE_PRINT(EXCE_ACE_FWK_LAUNCH_FAILED, EXCE_ACE_INIT_FWK_FAILED);
117         PrintErrorMessage(retValue);
118     } else {
119         HILOG_INFO(HILOG_MODULE_ACE, "Success to load JavaScript framework.");
120     }
121     jerry_release_value(retValue);
122     Debugger::GetInstance().StartDebugger();
123 }
124 
Cleanup()125 void JsAppEnvironment::Cleanup()
126 {
127     Debugger debugger = Debugger::GetInstance();
128     debugger.TearDownDebugger();
129     FeaAbilityModule::Release();
130     ProductAdapter::UnloadExtraPresetModules();
131 
132     // clean up engine, NOTE: all JS value must be released properly befor cleanup
133     jerry_cleanup();
134     // free the external JS context, only can be called after clean up engine
135     debugger.ReleaseJSContext();
136 #if (JS_ENGINE_STATIC_MULTI_CONTEXTS_ENABLED == 1)
137     jerry_port_default_remove_current_context_record();
138 #endif
139 }
140 
141 /**
142  * Decide the runtime mode of jerry
143  */
InitRuntimeMode()144 void JsAppEnvironment::InitRuntimeMode()
145 {
146     // if debugger is disabled, give a chance to use JS mode manually on device
147     SetEngineSnapshotModeManually(snapshotMode_);
148     if (snapshotMode_) {
149         HILOG_DEBUG(HILOG_MODULE_ACE, "ACELite is running in snapshot mode");
150     } else {
151         HILOG_DEBUG(HILOG_MODULE_ACE, "ACELite is running in JS parser mode");
152     }
153 }
154 } // namespace ACELite
155 } // namespace OHOS
156