1 /*
2  * Copyright (c) 2023 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 "napi/native_api.h"
17 #include <cerrno>
18 #include <err.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #define ERRON_0 0
23 #define PARAM_0777 0777
24 
Warn(napi_env env, napi_callback_info info)25 static napi_value Warn(napi_env env, napi_callback_info info)
26 {
27     errno = ERRON_0;
28     int fd = open("/data/storage/el2/Fzl.txt", O_CREAT, PARAM_0777);
29     warn("error happen");
30     napi_value result = nullptr;
31     napi_create_int32(env, errno, &result);
32     close(fd);
33     return result;
34 }
35 
Warnx(napi_env env, napi_callback_info info)36 static napi_value Warnx(napi_env env, napi_callback_info info)
37 {
38     errno = ERRON_0;
39     int fd = open("/data/storage/el2/Fzl.txt", O_CREAT, PARAM_0777);
40     warnx("error happen");
41     napi_value result = nullptr;
42     napi_create_int32(env, errno, &result);
43     close(fd);
44     return result;
45 }
46 
47 EXTERN_C_START
Init(napi_env env, napi_value exports)48 static napi_value Init(napi_env env, napi_value exports)
49 {
50     napi_property_descriptor desc[] = {
51         {"warn", nullptr, Warn, nullptr, nullptr, nullptr, napi_default, nullptr},
52         {"warnx", nullptr, Warnx, nullptr, nullptr, nullptr, napi_default, nullptr},
53     };
54     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
55     return exports;
56 }
57 
58 EXTERN_C_END
59 
60 static napi_module demoModule = {
61     .nm_version = 1,
62     .nm_flags = 0,
63     .nm_filename = nullptr,
64     .nm_register_func = Init,
65     .nm_modname = "err",
66     .nm_priv = ((void *)0),
67     .reserved = {0},
68 };
69 
RegisterEntryModule(void)70 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }