• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <string>
17 #include "napi/native_api.h"
18 // [Start napi_load_module_with_info_entry_har_name]
loadModule(napi_env env,napi_callback_info info)19 static napi_value loadModule(napi_env env, napi_callback_info info)
20 {
21     napi_value result;
22     // 1. 使用napi_load_module_with_info加载library
23     napi_status status = napi_load_module_with_info(env, "library", "com.example.application/entry", &result);
24 
25     napi_value testFn;
26     // 2. 使用napi_get_named_property获取test函数
27     napi_get_named_property(env, result, "test", &testFn);
28     // 3. 使用napi_call_function调用函数test
29     napi_call_function(env, result, testFn, 0, nullptr, nullptr);
30 
31     napi_value value;
32     napi_value key;
33     std::string keyStr = "value";
34     napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
35     // 4. 使用napi_get_property获取变量value
36     napi_get_property(env, result, key, &value);
37     return result;
38 }
39 // [End napi_load_module_with_info_entry_har_name]
40 EXTERN_C_START
Init(napi_env env,napi_value exports)41 static napi_value Init(napi_env env, napi_value exports)
42 {
43     napi_property_descriptor desc[] = {
44         {"loadModule", nullptr, loadModule, nullptr, nullptr, nullptr, napi_default, nullptr}};
45     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
46     return exports;
47 }
48 EXTERN_C_END
49 
50 static napi_module demoModule = {
51     .nm_version = 1,
52     .nm_flags = 0,
53     .nm_filename = nullptr,
54     .nm_register_func = Init,
55     .nm_modname = "entryone",
56     .nm_priv = ((void *)0),
57     .reserved = {0},
58 };
59 
RegisterEntryModule(void)60 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
61