1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include "native_parameters_js.h"
16d9f0492fSopenharmony_ci#include "securec.h"
17d9f0492fSopenharmony_ci
18d9f0492fSopenharmony_cistatic constexpr int MAX_LENGTH = 128;
19d9f0492fSopenharmony_cistatic constexpr int ARGC_NUMBER = 2;
20d9f0492fSopenharmony_cistatic constexpr int ARGC_THREE_NUMBER = 3;
21d9f0492fSopenharmony_cistatic constexpr int BUF_LENGTH = 256;
22d9f0492fSopenharmony_ci
23d9f0492fSopenharmony_ciusing StorageAsyncContext = struct StorageAsyncContext {
24d9f0492fSopenharmony_ci    napi_env env = nullptr;
25d9f0492fSopenharmony_ci    napi_async_work work = nullptr;
26d9f0492fSopenharmony_ci
27d9f0492fSopenharmony_ci    char key[BUF_LENGTH] = { 0 };
28d9f0492fSopenharmony_ci    size_t keyLen = 0;
29d9f0492fSopenharmony_ci    char value[BUF_LENGTH] = { 0 };
30d9f0492fSopenharmony_ci    size_t valueLen = 0;
31d9f0492fSopenharmony_ci    napi_deferred deferred = nullptr;
32d9f0492fSopenharmony_ci    napi_ref callbackRef = nullptr;
33d9f0492fSopenharmony_ci
34d9f0492fSopenharmony_ci    int status = -1;
35d9f0492fSopenharmony_ci    std::string getValue;
36d9f0492fSopenharmony_ci};
37d9f0492fSopenharmony_ci
38d9f0492fSopenharmony_ciusing StorageAsyncContextPtr = StorageAsyncContext *;
39d9f0492fSopenharmony_ci
40d9f0492fSopenharmony_cistatic void SetCallbackWork(napi_env env, StorageAsyncContextPtr asyncContext)
41d9f0492fSopenharmony_ci{
42d9f0492fSopenharmony_ci    napi_value resource = nullptr;
43d9f0492fSopenharmony_ci    napi_create_string_utf8(env, "JSStartupSet", NAPI_AUTO_LENGTH, &resource);
44d9f0492fSopenharmony_ci    napi_create_async_work(
45d9f0492fSopenharmony_ci        env, nullptr, resource,
46d9f0492fSopenharmony_ci        [](napi_env env, void *data) {
47d9f0492fSopenharmony_ci            StorageAsyncContext *asyncContext = reinterpret_cast<StorageAsyncContext *>(data);
48d9f0492fSopenharmony_ci            asyncContext->status = SetParameter(asyncContext->key, asyncContext->value);
49d9f0492fSopenharmony_ci            PARAM_JS_LOGV("JSApp set::asyncContext-> status = %d, asyncContext->key = %s, asyncContext->value = %s.",
50d9f0492fSopenharmony_ci                asyncContext->status, asyncContext->key, asyncContext->value);
51d9f0492fSopenharmony_ci        },
52d9f0492fSopenharmony_ci        [](napi_env env, napi_status status, void *data) {
53d9f0492fSopenharmony_ci            StorageAsyncContext *asyncContext = reinterpret_cast<StorageAsyncContext *>(data);
54d9f0492fSopenharmony_ci            napi_value result[ARGC_NUMBER] = { 0 };
55d9f0492fSopenharmony_ci            if (asyncContext->status == 0) {
56d9f0492fSopenharmony_ci                napi_get_undefined(env, &result[0]);
57d9f0492fSopenharmony_ci                napi_get_undefined(env, &result[1]);
58d9f0492fSopenharmony_ci            } else {
59d9f0492fSopenharmony_ci                napi_value value = nullptr;
60d9f0492fSopenharmony_ci                napi_create_object(env, &result[0]);
61d9f0492fSopenharmony_ci                napi_create_int32(env, asyncContext->status, &value);
62d9f0492fSopenharmony_ci                napi_set_named_property(env, result[0], "code", value);
63d9f0492fSopenharmony_ci                napi_get_undefined(env, &result[1]);
64d9f0492fSopenharmony_ci            }
65d9f0492fSopenharmony_ci
66d9f0492fSopenharmony_ci            if (asyncContext->deferred) {
67d9f0492fSopenharmony_ci                if (asyncContext->status == 0) {
68d9f0492fSopenharmony_ci                    napi_resolve_deferred(env, asyncContext->deferred, result[1]);
69d9f0492fSopenharmony_ci                } else {
70d9f0492fSopenharmony_ci                    napi_reject_deferred(env, asyncContext->deferred, result[0]);
71d9f0492fSopenharmony_ci                }
72d9f0492fSopenharmony_ci            } else {
73d9f0492fSopenharmony_ci                napi_value callback = nullptr;
74d9f0492fSopenharmony_ci                napi_value callResult = nullptr;
75d9f0492fSopenharmony_ci                napi_get_reference_value(env, asyncContext->callbackRef, &callback);
76d9f0492fSopenharmony_ci                napi_call_function(env, nullptr, callback, ARGC_NUMBER, result, &callResult);
77d9f0492fSopenharmony_ci                napi_delete_reference(env, asyncContext->callbackRef);
78d9f0492fSopenharmony_ci            }
79d9f0492fSopenharmony_ci            napi_delete_async_work(env, asyncContext->work);
80d9f0492fSopenharmony_ci            delete asyncContext;
81d9f0492fSopenharmony_ci        },
82d9f0492fSopenharmony_ci        reinterpret_cast<void *>(asyncContext), &asyncContext->work);
83d9f0492fSopenharmony_ci    napi_queue_async_work(env, asyncContext->work);
84d9f0492fSopenharmony_ci}
85d9f0492fSopenharmony_ci
86d9f0492fSopenharmony_cistatic napi_value Set(napi_env env, napi_callback_info info)
87d9f0492fSopenharmony_ci{
88d9f0492fSopenharmony_ci    size_t argc = ARGC_THREE_NUMBER;
89d9f0492fSopenharmony_ci    napi_value argv[ARGC_THREE_NUMBER] = { nullptr };
90d9f0492fSopenharmony_ci    napi_value thisVar = nullptr;
91d9f0492fSopenharmony_ci    void *data = nullptr;
92d9f0492fSopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
93d9f0492fSopenharmony_ci    NAPI_ASSERT(env, argc >= ARGC_NUMBER, "requires 2 parameter");
94d9f0492fSopenharmony_ci    StorageAsyncContextPtr asyncContext = new StorageAsyncContext();
95d9f0492fSopenharmony_ci    asyncContext->env = env;
96d9f0492fSopenharmony_ci    for (size_t i = 0; i < argc; i++) {
97d9f0492fSopenharmony_ci        napi_valuetype valueType = napi_null;
98d9f0492fSopenharmony_ci        napi_typeof(env, argv[i], &valueType);
99d9f0492fSopenharmony_ci
100d9f0492fSopenharmony_ci        if (i == 0 && valueType == napi_string) {
101d9f0492fSopenharmony_ci            napi_get_value_string_utf8(env, argv[i], asyncContext->key,
102d9f0492fSopenharmony_ci                BUF_LENGTH - 1, &asyncContext->keyLen);
103d9f0492fSopenharmony_ci        } else if (i == 1 && valueType == napi_string) {
104d9f0492fSopenharmony_ci            napi_get_value_string_utf8(env, argv[i], asyncContext->value,
105d9f0492fSopenharmony_ci                BUF_LENGTH - 1, &asyncContext->valueLen);
106d9f0492fSopenharmony_ci        } else if (i == ARGC_NUMBER && valueType == napi_function) {
107d9f0492fSopenharmony_ci            napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef);
108d9f0492fSopenharmony_ci        } else {
109d9f0492fSopenharmony_ci            delete asyncContext;
110d9f0492fSopenharmony_ci            NAPI_ASSERT(env, false, "type mismatch");
111d9f0492fSopenharmony_ci        }
112d9f0492fSopenharmony_ci    }
113d9f0492fSopenharmony_ci
114d9f0492fSopenharmony_ci    napi_value result = nullptr;
115d9f0492fSopenharmony_ci    if (asyncContext->callbackRef == nullptr) {
116d9f0492fSopenharmony_ci        napi_create_promise(env, &asyncContext->deferred, &result);
117d9f0492fSopenharmony_ci    } else {
118d9f0492fSopenharmony_ci        napi_get_undefined(env, &result);
119d9f0492fSopenharmony_ci    }
120d9f0492fSopenharmony_ci
121d9f0492fSopenharmony_ci    SetCallbackWork(env, asyncContext);
122d9f0492fSopenharmony_ci    return result;
123d9f0492fSopenharmony_ci}
124d9f0492fSopenharmony_ci
125d9f0492fSopenharmony_cistatic napi_value SetSync(napi_env env, napi_callback_info info)
126d9f0492fSopenharmony_ci{
127d9f0492fSopenharmony_ci    size_t argc = ARGC_NUMBER;
128d9f0492fSopenharmony_ci    napi_value args[ARGC_NUMBER] = { nullptr };
129d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
130d9f0492fSopenharmony_ci    NAPI_ASSERT(env, argc == ARGC_NUMBER, "Wrong number of arguments");
131d9f0492fSopenharmony_ci    napi_valuetype valuetype0 = napi_null;
132d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
133d9f0492fSopenharmony_ci    napi_valuetype valuetype1 = napi_null;
134d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
135d9f0492fSopenharmony_ci    NAPI_ASSERT(env, valuetype0 == napi_string && valuetype1 == napi_string, "Wrong argument type. string expected.");
136d9f0492fSopenharmony_ci
137d9f0492fSopenharmony_ci    char keyBuf[BUF_LENGTH] = { 0 };
138d9f0492fSopenharmony_ci    size_t keySize = 0;
139d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], keyBuf, BUF_LENGTH - 1, &keySize));
140d9f0492fSopenharmony_ci    if (keySize >= MAX_LENGTH) {
141d9f0492fSopenharmony_ci        return nullptr;
142d9f0492fSopenharmony_ci    }
143d9f0492fSopenharmony_ci
144d9f0492fSopenharmony_ci    char valueBuf[BUF_LENGTH] = { 0 };
145d9f0492fSopenharmony_ci    size_t valueSize = 0;
146d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_get_value_string_utf8(env, args[1], valueBuf, BUF_LENGTH - 1, &valueSize));
147d9f0492fSopenharmony_ci    if (valueSize >= MAX_LENGTH) {
148d9f0492fSopenharmony_ci        return nullptr;
149d9f0492fSopenharmony_ci    }
150d9f0492fSopenharmony_ci
151d9f0492fSopenharmony_ci    std::string keyStr = keyBuf;
152d9f0492fSopenharmony_ci    std::string valueStr = valueBuf;
153d9f0492fSopenharmony_ci    int setResult = SetParameter(keyStr.c_str(), valueStr.c_str());
154d9f0492fSopenharmony_ci    PARAM_JS_LOGV("JSApp SetSync::setResult = %d, input keyBuf = %s.", setResult, keyBuf);
155d9f0492fSopenharmony_ci
156d9f0492fSopenharmony_ci    napi_value napiValue = nullptr;
157d9f0492fSopenharmony_ci    if (setResult != 0) { // set failed
158d9f0492fSopenharmony_ci        std::stringstream ss;
159d9f0492fSopenharmony_ci        ss << "set: " << keyStr << " failed, error code: " << setResult;
160d9f0492fSopenharmony_ci        napi_throw_error((env), nullptr, ss.str().c_str());
161d9f0492fSopenharmony_ci    } else {
162d9f0492fSopenharmony_ci        napi_get_undefined(env, &napiValue);
163d9f0492fSopenharmony_ci    }
164d9f0492fSopenharmony_ci    return napiValue;
165d9f0492fSopenharmony_ci}
166d9f0492fSopenharmony_ci
167d9f0492fSopenharmony_cistatic napi_value GetSync(napi_env env, napi_callback_info info)
168d9f0492fSopenharmony_ci{
169d9f0492fSopenharmony_ci    size_t argc = ARGC_NUMBER;
170d9f0492fSopenharmony_ci    napi_value args[ARGC_NUMBER] = { nullptr };
171d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
172d9f0492fSopenharmony_ci    NAPI_ASSERT(env, argc == 1 || argc == ARGC_NUMBER, "Wrong number of arguments");
173d9f0492fSopenharmony_ci    napi_valuetype valuetype0 = napi_null;
174d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
175d9f0492fSopenharmony_ci    NAPI_ASSERT(env, valuetype0 == napi_string, "Wrong argument type. Numbers expected.");
176d9f0492fSopenharmony_ci
177d9f0492fSopenharmony_ci    napi_valuetype valuetype1 = napi_null;
178d9f0492fSopenharmony_ci    if (argc == ARGC_NUMBER) {
179d9f0492fSopenharmony_ci        NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
180d9f0492fSopenharmony_ci        NAPI_ASSERT(env, (valuetype1 == napi_string) || (valuetype1 == napi_undefined),
181d9f0492fSopenharmony_ci            "Wrong argument type. string expected.");
182d9f0492fSopenharmony_ci    }
183d9f0492fSopenharmony_ci
184d9f0492fSopenharmony_ci    char keyBuf[BUF_LENGTH] = { 0 };
185d9f0492fSopenharmony_ci    size_t keySize = 0;
186d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], keyBuf, BUF_LENGTH - 1, &keySize));
187d9f0492fSopenharmony_ci    if (keySize >= MAX_LENGTH) {
188d9f0492fSopenharmony_ci        return nullptr;
189d9f0492fSopenharmony_ci    }
190d9f0492fSopenharmony_ci
191d9f0492fSopenharmony_ci    std::string keyStr = keyBuf;
192d9f0492fSopenharmony_ci    std::string valueStr = "";
193d9f0492fSopenharmony_ci    std::string getValue = "";
194d9f0492fSopenharmony_ci    if (argc == ARGC_NUMBER) {
195d9f0492fSopenharmony_ci        char valueBuf[BUF_LENGTH] = { 0 };
196d9f0492fSopenharmony_ci        size_t valueSize = 0;
197d9f0492fSopenharmony_ci        if (valuetype1 == napi_undefined) {
198d9f0492fSopenharmony_ci            valueStr = "";
199d9f0492fSopenharmony_ci        } else {
200d9f0492fSopenharmony_ci            NAPI_CALL(env, napi_get_value_string_utf8(env, args[1], valueBuf, BUF_LENGTH - 1, &valueSize));
201d9f0492fSopenharmony_ci            if (valueSize >= MAX_LENGTH) {
202d9f0492fSopenharmony_ci                return nullptr;
203d9f0492fSopenharmony_ci            }
204d9f0492fSopenharmony_ci            valueStr = valueBuf;
205d9f0492fSopenharmony_ci        }
206d9f0492fSopenharmony_ci    }
207d9f0492fSopenharmony_ci    int ret = OHOS::system::GetStringParameter(keyStr, getValue, valueStr);
208d9f0492fSopenharmony_ci    PARAM_JS_LOGV("JSApp GetSync::getValue = %s, input keyStr = %s.", getValue.c_str(), keyBuf);
209d9f0492fSopenharmony_ci
210d9f0492fSopenharmony_ci    napi_value napiValue = nullptr;
211d9f0492fSopenharmony_ci    if (ret == 0) {
212d9f0492fSopenharmony_ci        NAPI_CALL(env, napi_create_string_utf8(env, getValue.c_str(), strlen(getValue.c_str()), &napiValue));
213d9f0492fSopenharmony_ci    }
214d9f0492fSopenharmony_ci    return napiValue;
215d9f0492fSopenharmony_ci}
216d9f0492fSopenharmony_ci
217d9f0492fSopenharmony_cistatic void GetCallbackWork(napi_env env, StorageAsyncContextPtr asyncContext)
218d9f0492fSopenharmony_ci{
219d9f0492fSopenharmony_ci    napi_value resource = nullptr;
220d9f0492fSopenharmony_ci    napi_create_string_utf8(env, "JSStartupGet", NAPI_AUTO_LENGTH, &resource);
221d9f0492fSopenharmony_ci    napi_create_async_work(
222d9f0492fSopenharmony_ci        env, nullptr, resource,
223d9f0492fSopenharmony_ci        [](napi_env env, void *data) {
224d9f0492fSopenharmony_ci            StorageAsyncContext *asyncContext = reinterpret_cast<StorageAsyncContext *>(data);
225d9f0492fSopenharmony_ci            asyncContext->status =
226d9f0492fSopenharmony_ci                OHOS::system::GetStringParameter(asyncContext->key, asyncContext->getValue, asyncContext->value);
227d9f0492fSopenharmony_ci            PARAM_JS_LOGV("JSApp get status = %d, asyncContext->getValue = %s, asyncContext->key = %s, value = %s.",
228d9f0492fSopenharmony_ci                asyncContext->status, asyncContext->getValue.c_str(), asyncContext->key, asyncContext->value);
229d9f0492fSopenharmony_ci        },
230d9f0492fSopenharmony_ci        [](napi_env env, napi_status status, void *data) {
231d9f0492fSopenharmony_ci            StorageAsyncContext *asyncContext = reinterpret_cast<StorageAsyncContext *>(data);
232d9f0492fSopenharmony_ci            napi_value result[ARGC_NUMBER] = { 0 };
233d9f0492fSopenharmony_ci            if (asyncContext->status == 0) {
234d9f0492fSopenharmony_ci                napi_get_undefined(env, &result[0]);
235d9f0492fSopenharmony_ci                napi_create_string_utf8(env, asyncContext->getValue.c_str(), strlen(asyncContext->getValue.c_str()),
236d9f0492fSopenharmony_ci                    &result[1]);
237d9f0492fSopenharmony_ci            } else {
238d9f0492fSopenharmony_ci                napi_value message = nullptr;
239d9f0492fSopenharmony_ci                napi_create_object(env, &result[0]);
240d9f0492fSopenharmony_ci                napi_create_int32(env, asyncContext->status, &message);
241d9f0492fSopenharmony_ci                napi_set_named_property(env, result[0], "code", message);
242d9f0492fSopenharmony_ci                napi_get_undefined(env, &result[1]);
243d9f0492fSopenharmony_ci            }
244d9f0492fSopenharmony_ci
245d9f0492fSopenharmony_ci            if (asyncContext->deferred) {
246d9f0492fSopenharmony_ci                if (asyncContext->status == 0) {
247d9f0492fSopenharmony_ci                    napi_resolve_deferred(env, asyncContext->deferred, result[1]);
248d9f0492fSopenharmony_ci                } else {
249d9f0492fSopenharmony_ci                    napi_reject_deferred(env, asyncContext->deferred, result[0]);
250d9f0492fSopenharmony_ci                }
251d9f0492fSopenharmony_ci            } else {
252d9f0492fSopenharmony_ci                napi_value callback = nullptr;
253d9f0492fSopenharmony_ci                napi_value callResult = nullptr;
254d9f0492fSopenharmony_ci                napi_get_reference_value(env, asyncContext->callbackRef, &callback);
255d9f0492fSopenharmony_ci                napi_call_function(env, nullptr, callback, ARGC_NUMBER, result, &callResult);
256d9f0492fSopenharmony_ci                napi_delete_reference(env, asyncContext->callbackRef);
257d9f0492fSopenharmony_ci            }
258d9f0492fSopenharmony_ci            napi_delete_async_work(env, asyncContext->work);
259d9f0492fSopenharmony_ci            delete asyncContext;
260d9f0492fSopenharmony_ci        },
261d9f0492fSopenharmony_ci        reinterpret_cast<void *>(asyncContext), &asyncContext->work);
262d9f0492fSopenharmony_ci    napi_queue_async_work(env, asyncContext->work);
263d9f0492fSopenharmony_ci}
264d9f0492fSopenharmony_ci
265d9f0492fSopenharmony_cistatic napi_value Get(napi_env env, napi_callback_info info)
266d9f0492fSopenharmony_ci{
267d9f0492fSopenharmony_ci    size_t argc = ARGC_THREE_NUMBER;
268d9f0492fSopenharmony_ci    napi_value argv[ARGC_THREE_NUMBER] = { nullptr };
269d9f0492fSopenharmony_ci    napi_value thisVar = nullptr;
270d9f0492fSopenharmony_ci    void *data = nullptr;
271d9f0492fSopenharmony_ci    napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
272d9f0492fSopenharmony_ci    NAPI_ASSERT(env, argc >= 1, "requires 1 parameter");
273d9f0492fSopenharmony_ci    StorageAsyncContextPtr asyncContext = new StorageAsyncContext();
274d9f0492fSopenharmony_ci    asyncContext->env = env;
275d9f0492fSopenharmony_ci    for (size_t i = 0; i < argc; i++) {
276d9f0492fSopenharmony_ci        napi_valuetype valueType = napi_null;
277d9f0492fSopenharmony_ci        napi_typeof(env, argv[i], &valueType);
278d9f0492fSopenharmony_ci
279d9f0492fSopenharmony_ci        if (i == 0 && valueType == napi_string) {
280d9f0492fSopenharmony_ci            napi_get_value_string_utf8(env, argv[i], asyncContext->key,
281d9f0492fSopenharmony_ci                BUF_LENGTH - 1, &asyncContext->keyLen);
282d9f0492fSopenharmony_ci        } else if (i == 1 && valueType == napi_string) {
283d9f0492fSopenharmony_ci            napi_get_value_string_utf8(env, argv[i], asyncContext->value,
284d9f0492fSopenharmony_ci                BUF_LENGTH - 1, &asyncContext->valueLen);
285d9f0492fSopenharmony_ci        } else if (i == 1 && valueType == napi_undefined) {
286d9f0492fSopenharmony_ci            strcpy_s(asyncContext->value, BUF_LENGTH, "\0");
287d9f0492fSopenharmony_ci        } else if (i == 1 && valueType == napi_function) {
288d9f0492fSopenharmony_ci            napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef);
289d9f0492fSopenharmony_ci            break;
290d9f0492fSopenharmony_ci        } else if (i == ARGC_NUMBER && valueType == napi_function) {
291d9f0492fSopenharmony_ci            napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef);
292d9f0492fSopenharmony_ci        } else {
293d9f0492fSopenharmony_ci            delete asyncContext;
294d9f0492fSopenharmony_ci            NAPI_ASSERT(env, false, "type mismatch");
295d9f0492fSopenharmony_ci        }
296d9f0492fSopenharmony_ci    }
297d9f0492fSopenharmony_ci
298d9f0492fSopenharmony_ci    napi_value result = nullptr;
299d9f0492fSopenharmony_ci    if (asyncContext->callbackRef == nullptr) {
300d9f0492fSopenharmony_ci        napi_create_promise(env, &asyncContext->deferred, &result);
301d9f0492fSopenharmony_ci    } else {
302d9f0492fSopenharmony_ci        napi_get_undefined(env, &result);
303d9f0492fSopenharmony_ci    }
304d9f0492fSopenharmony_ci
305d9f0492fSopenharmony_ci    GetCallbackWork(env, asyncContext);
306d9f0492fSopenharmony_ci    return result;
307d9f0492fSopenharmony_ci}
308d9f0492fSopenharmony_ci
309d9f0492fSopenharmony_ciEXTERN_C_START
310d9f0492fSopenharmony_ci/*
311d9f0492fSopenharmony_ci * Module init
312d9f0492fSopenharmony_ci */
313d9f0492fSopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
314d9f0492fSopenharmony_ci{
315d9f0492fSopenharmony_ci    /*
316d9f0492fSopenharmony_ci     * Attribute definition
317d9f0492fSopenharmony_ci     */
318d9f0492fSopenharmony_ci    napi_property_descriptor desc[] = {
319d9f0492fSopenharmony_ci        DECLARE_NAPI_FUNCTION("set", Set),
320d9f0492fSopenharmony_ci        DECLARE_NAPI_FUNCTION("setSync", SetSync),
321d9f0492fSopenharmony_ci        DECLARE_NAPI_FUNCTION("get", Get),
322d9f0492fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getSync", GetSync),
323d9f0492fSopenharmony_ci        DECLARE_NAPI_FUNCTION("wait", ParamWait),
324d9f0492fSopenharmony_ci        DECLARE_NAPI_FUNCTION("getWatcher", GetWatcher)
325d9f0492fSopenharmony_ci    };
326d9f0492fSopenharmony_ci    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
327d9f0492fSopenharmony_ci    return RegisterWatcher(env, exports);
328d9f0492fSopenharmony_ci}
329d9f0492fSopenharmony_ciEXTERN_C_END
330d9f0492fSopenharmony_ci
331d9f0492fSopenharmony_ci/*
332d9f0492fSopenharmony_ci * Module definition
333d9f0492fSopenharmony_ci */
334d9f0492fSopenharmony_cistatic napi_module _module = {
335d9f0492fSopenharmony_ci    .nm_version = 1,
336d9f0492fSopenharmony_ci    .nm_flags = 0,
337d9f0492fSopenharmony_ci    .nm_filename = NULL,
338d9f0492fSopenharmony_ci    .nm_register_func = Init,
339d9f0492fSopenharmony_ci    .nm_modname = "systemparameter",
340d9f0492fSopenharmony_ci    .nm_priv = ((void *)0),
341d9f0492fSopenharmony_ci    .reserved = { 0 }
342d9f0492fSopenharmony_ci};
343d9f0492fSopenharmony_ci
344d9f0492fSopenharmony_ci/*
345d9f0492fSopenharmony_ci * Module registration function
346d9f0492fSopenharmony_ci */
347d9f0492fSopenharmony_ciextern "C" __attribute__((constructor)) void RegisterModule(void)
348d9f0492fSopenharmony_ci{
349d9f0492fSopenharmony_ci    napi_module_register(&_module);
350d9f0492fSopenharmony_ci}
351