1 /*
2  * Copyright (c) 2020 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 "presets/require_module.h"
16 #include "ace_mem_base.h"
17 #include "module_manager.h"
18 
19 namespace OHOS {
20 namespace ACELite {
Init()21 void RequireModule::Init()
22 {
23     const char * const funcNameRequire = "requireNative";
24     CreateNamedFunction(funcNameRequire, ImportModule);
25 }
26 
ImportModule(const jerry_value_t func,const jerry_value_t obj,const jerry_value_t * args,const jerry_length_t argsNum)27 jerry_value_t RequireModule::ImportModule(const jerry_value_t func,
28                                           const jerry_value_t obj,
29                                           const jerry_value_t* args,
30                                           const jerry_length_t argsNum)
31 {
32     const uint8_t requireArgc = 1;
33     if (argsNum != requireArgc) {
34         return UNDEFINED;
35     }
36     char* name = MallocStringOf(args[0]);
37     if (name == nullptr) {
38         return UNDEFINED;
39     }
40 
41     ModuleManager* moduleManager = ModuleManager::GetInstance();
42     jerry_value_t moduleObj = (jerry_value_t)(uintptr_t)(moduleManager->RequireModule(name));
43     ace_free(name);
44     name = nullptr;
45     return moduleObj;
46 }
47 } // namespace ACELite
48 } // namespace OHOS
49