1 /*
2 * Copyright (c) 2020-2021 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/console_module.h"
27 #include "presets/feature_ability_module.h"
28 #include "presets/intl_module.h"
29 #include "presets/jstest_module.h"
30 #include "presets/localization_module.h"
31 #include "presets/profiler_module.h"
32 #include "presets/render_module.h"
33 #include "presets/require_module.h"
34 #include "presets/timer_module.h"
35 #include "presets/version_module.h"
36 #include "product_adapter.h"
37 #include "system_info.h"
38 #if (JS_ENGINE_STATIC_MULTI_CONTEXTS_ENABLED == 1)
39
40 extern "C" {
41 #include "generate-bytecode.h"
42 }
43 #endif
44
45 namespace OHOS {
46 namespace ACELite {
JsAppEnvironment()47 JsAppEnvironment::JsAppEnvironment()
48 {
49 SetEngineSnapshotMode(snapshotMode_);
50 }
51
LoadAceBuiltInModules() const52 void JsAppEnvironment::LoadAceBuiltInModules() const
53 {
54 ConsoleModule::Load();
55 RenderModule::Load();
56 RequireModule::Load();
57 FeaAbilityModule::Load();
58 JsTestModule::Load();
59 TimersModule::Load();
60 PerformaceProfilerModule::Load();
61 AceVersionModule::Load();
62 IntlControlModule::Load();
63 }
64
InitJsFramework() const65 void JsAppEnvironment::InitJsFramework() const
66 {
67 START_TRACING(ENGINE_INIT);
68 #if (JS_ENGINE_STATIC_MULTI_CONTEXTS_ENABLED == 1)
69 js_task_context_init();
70 #endif
71 #if (JERRY_PORTING_DEPENDENCY == 0)
72 Srand((unsigned)jerry_port_get_current_time());
73 #endif
74 Debugger::GetInstance().SetupJSContext();
75 jerry_init(JERRY_INIT_EMPTY);
76 STOP_TRACING();
77 START_TRACING(FWK_INIT);
78 #if (JSFWK_TEST == 1)
79 jerry_value_t globalThis = jerry_get_global_object();
80 jerry_release_value(jerryx_set_property_str(globalThis, "globalThis", globalThis));
81 jerry_release_value(globalThis);
82 #endif // JSFWK_TEST
83 AsyncTaskManager::GetInstance().Init();
84 LoadAceBuiltInModules();
85 ProductAdapter::LoadExtraPresetModules();
86 LoadFramework();
87 LocalModule::Load();
88 SystemInfo::GetInstance().Initialize();
89 STOP_TRACING();
90 }
91
LoadFramework() const92 void JsAppEnvironment::LoadFramework() const
93 {
94 size_t len = 0;
95 // load framework js/snapshot file to buffer
96 const char * const jsFrameworkScript = GetFrameworkRawBuffer(snapshotMode_, len);
97 const jerry_char_t *jScript = reinterpret_cast<const jerry_char_t *>(jsFrameworkScript);
98 // eval framework to expose
99 START_TRACING(FWK_CODE_EVAL);
100
101 jerry_value_t retValue = UNDEFINED;
102 if (snapshotMode_) {
103 retValue = jerry_exec_snapshot(reinterpret_cast<const uint32_t *>(jScript), len, 0, 1);
104 } else {
105 retValue = jerry_eval(jScript, len, JERRY_PARSE_NO_OPTS);
106 }
107 STOP_TRACING();
108 bool hasError = jerry_value_is_error(retValue);
109 if (hasError) {
110 HILOG_ERROR(HILOG_MODULE_ACE, "Failed to load JavaScript framework.");
111 ACE_ERROR_CODE_PRINT(EXCE_ACE_FWK_LAUNCH_FAILED, EXCE_ACE_INIT_FWK_FAILED);
112 PrintErrorMessage(retValue);
113 } else {
114 HILOG_INFO(HILOG_MODULE_ACE, "Success to load JavaScript framework.");
115 }
116 jerry_release_value(retValue);
117 Debugger::GetInstance().StartDebugger();
118 }
119
Cleanup()120 void JsAppEnvironment::Cleanup()
121 {
122 Debugger::GetInstance().TearDownDebugger();
123 FeaAbilityModule::Release();
124 ProductAdapter::UnloadExtraPresetModules();
125
126 // clean up engine, NOTE: all JS value must be released properly befor cleanup
127 jerry_cleanup();
128 // free the external JS context, only can be called after clean up engine
129 Debugger::GetInstance().ReleaseJSContext();
130 #if (JS_ENGINE_STATIC_MULTI_CONTEXTS_ENABLED == 1)
131 jerry_port_default_remove_current_context_record();
132 #endif
133 }
134
135 /**
136 * Decide the runtime mode of jerry
137 */
InitRuntimeMode()138 void JsAppEnvironment::InitRuntimeMode()
139 {
140 // if debugger is disabled, give a chance to use JS mode manually on device
141 SetEngineSnapshotModeManually(snapshotMode_);
142 if (snapshotMode_) {
143 HILOG_DEBUG(HILOG_MODULE_ACE, "ACELite is running in snapshot mode");
144 } else {
145 HILOG_DEBUG(HILOG_MODULE_ACE, "ACELite is running in JS parser mode");
146 }
147 }
148 } // namespace ACELite
149 } // namespace OHOS
150