• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include "runtime.h"
17 
18 #ifdef CJ_FRONTEND
19 #include "cj_runtime.h"
20 #endif
21 #include "js_runtime.h"
22 #include "ets_runtime.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 namespace {
27 std::unique_ptr<Runtime> g_preloadedInstance;
28 }
29 
Create(Runtime::Options & options)30 std::unique_ptr<Runtime> Runtime::Create(Runtime::Options &options)
31 {
32     switch (options.lang) {
33         case Runtime::Language::JS:
34             return JsRuntime::Create(options);
35 #ifdef CJ_FRONTEND
36         case Runtime::Language::CJ:
37             return CJRuntime::Create(options);
38 #endif
39         case Runtime::Language::ETS: {
40             std::unique_ptr<JsRuntime> jsRuntime;
41             if (options.lang == Runtime::Language::ETS) {
42                 options.lang = Runtime::Language::JS;
43                 jsRuntime = JsRuntime::Create(options);
44                 options.lang = Runtime::Language::ETS;
45             }
46             return ETSRuntime::Create(options, jsRuntime);
47         }
48         default:
49             return std::unique_ptr<Runtime>();
50     }
51 }
52 
SavePreloaded(std::unique_ptr<Runtime> && instance)53 void Runtime::SavePreloaded(std::unique_ptr<Runtime> &&instance)
54 {
55     if (instance) {
56         instance->FinishPreload();
57     }
58     g_preloadedInstance = std::move(instance);
59 }
60 
GetPreloaded(Language key)61 std::unique_ptr<Runtime> Runtime::GetPreloaded(Language key)
62 {
63     if (g_preloadedInstance &&
64         g_preloadedInstance->GetLanguage() == Language::ETS &&
65         key == Language::JS) {
66         return static_cast<ETSRuntime *>(g_preloadedInstance.get())->MoveJsRuntime();
67     }
68     return std::move(g_preloadedInstance);
69 }
70 }  // namespace AbilityRuntime
71 }  // namespace OHOS
72