1 /*
2  * Copyright (c) 2021 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 <uv.h>
16 #include "js_napi_common.h"
17 #include "napi/native_api.h"
18 #include "napi/native_common.h"
19 #include "napi/native_node_api.h"
20 namespace ACE {
21 namespace NAPI {
22 namespace SYSTEM_TEST_NAPI {
ScopeRunInCallback(napi_env env,napi_callback_info info)23 napi_value ScopeRunInCallback(napi_env env, napi_callback_info info)
24 {
25     static constexpr int SCOPE_ARGC_TWO = 2;
26     static constexpr int SCOPE_ARGC_THREE = 3;
27     HILOG_INFO("%{public}s,called", __func__);
28     size_t argc = SCOPE_ARGC_THREE;
29     napi_value args[SCOPE_ARGC_THREE] = { nullptr };
30 
31     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr));
32     NAPI_ASSERT(env, argc == SCOPE_ARGC_THREE, "Wrong number of arguments");
33 
34     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
35 
36     napi_valuetype valuetype;
37     NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
38     NAPI_ASSERT(env, valuetype == napi_object, "Wrong type of arguments. Expects an object as first argument.");
39 
40     NAPI_CALL(env, napi_typeof(env, args[1], &valuetype));
41     NAPI_ASSERT(env, valuetype == napi_string, "Wrong type of arguments. Expects a string as second argument.");
42 
43     NAPI_CALL(env, napi_typeof(env, args[SCOPE_ARGC_TWO], &valuetype));
44     NAPI_ASSERT(env, valuetype == napi_function, "Wrong type of arguments. Expects a function as third argument.");
45 
46     napi_async_context context;
47     NAPI_CALL(env, napi_async_init(env, args[0], args[1], &context));
48 
49     napi_callback_scope scope = nullptr;
50     NAPI_CALL(env, napi_open_callback_scope(env, args[0], context, &scope));
51 
52     napi_value result = nullptr;
53     NAPI_CALL(env, napi_call_function(env, args[0], args[SCOPE_ARGC_TWO], 0, nullptr, &result));
54 
55     NAPI_CALL(env, napi_close_callback_scope(env, scope));
56     NAPI_CALL(env, napi_async_destroy(env, context));
57 
58     return result;
59 }
60 
CallBackScopeInit(napi_env env,napi_value exports)61 napi_value CallBackScopeInit(napi_env env, napi_value exports)
62 {
63     HILOG_INFO("%{public}s,called", __func__);
64     napi_property_descriptor descriptors[] = {
65         DECLARE_NAPI_FUNCTION("testScopeRunInCallback", ScopeRunInCallback),
66     };
67 
68     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
69 
70     return exports;
71 }
72 } // namespace SYSTEM_TEST_NAPI
73 } // namespace NAPI
74 } // namespace ACE
75