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 <csetjmp>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <js_native_api.h>
21 #include <node_api.h>
22 
23 #define PARAM_0 0
24 #define PARAM_1 1
25 #define PARAM_2 2
26 #define PARAM_UNNORMAL (-1)
27 #define ERRON_0 0
28 #define ONEVAL 1
29 #define MINUSONE (-1)
30 
31 static int count = PARAM_0;
Longjmp(napi_env env, napi_callback_info info)32 static napi_value Longjmp(napi_env env, napi_callback_info info)
33 {
34     jmp_buf jmp;
35     count = PARAM_0;
36     int ret = setjmp(jmp);
37     if (count == PARAM_0) {
38         count = PARAM_1;
39         longjmp(jmp, PARAM_1);
40     }
41     count = PARAM_0;
42     napi_value result = nullptr;
43     if (ret == PARAM_0) {
44         napi_create_int32(env, PARAM_UNNORMAL, &result);
45     } else {
46         napi_create_int32(env, PARAM_0, &result);
47     }
48     return result;
49 }
50 
_Longjmp(napi_env env, napi_callback_info info)51 static napi_value _Longjmp(napi_env env, napi_callback_info info)
52 {
53     jmp_buf jmp;
54     count = PARAM_0;
55     int ret = _setjmp(jmp);
56     if (count == PARAM_0) {
57         count = PARAM_1;
58         _longjmp(jmp, PARAM_1);
59     }
60     count = PARAM_0;
61     napi_value result = nullptr;
62     if (ret == PARAM_0) {
63         napi_create_int32(env, PARAM_UNNORMAL, &result);
64     } else {
65         napi_create_int32(env, PARAM_0, &result);
66     }
67     return result;
68 }
69 
_Setjmp(napi_env env, napi_callback_info info)70 static napi_value _Setjmp(napi_env env, napi_callback_info info)
71 {
72     napi_value result = nullptr;
73     jmp_buf jmp;
74     count = PARAM_0;
75     int ret = _setjmp(jmp);
76     if (count == PARAM_0 && ret != PARAM_0) {
77         count = PARAM_0;
78         napi_create_int32(env, PARAM_UNNORMAL, &result);
79         return result;
80     }
81     if (count == PARAM_1 && ret == PARAM_0) {
82         count = PARAM_0;
83         napi_create_int32(env, PARAM_UNNORMAL, &result);
84         return result;
85     }
86     if (count == PARAM_0) {
87         count = PARAM_1;
88         _longjmp(jmp, PARAM_1);
89     }
90     count = PARAM_0;
91     if (ret == PARAM_0) {
92         napi_create_int32(env, PARAM_UNNORMAL, &result);
93     } else {
94         napi_create_int32(env, PARAM_0, &result);
95     }
96     return result;
97 }
98 
99 EXTERN_C_START
Init(napi_env env, napi_value exports)100 static napi_value Init(napi_env env, napi_value exports)
101 {
102     napi_property_descriptor desc[] = {
103         {"longjmp", nullptr, Longjmp, nullptr, nullptr, nullptr, napi_default, nullptr},
104         {"_longjmp", nullptr, _Longjmp, nullptr, nullptr, nullptr, napi_default, nullptr},
105         {"_setjmp", nullptr, _Setjmp, nullptr, nullptr, nullptr, napi_default, nullptr},
106     };
107     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
108     return exports;
109 }
110 EXTERN_C_END
111 
112 static napi_module demoModule = {
113     .nm_version = 1,
114     .nm_flags = 0,
115     .nm_filename = "setjmpndk1",
116     .nm_register_func = Init,
117     .nm_modname = "setjmpndk1",
118     .nm_priv = ((void *)0),
119     .reserved = {0},
120 };
121 
RegisterEntryModule(void)122 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
123