1 /*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "common.h"
17 #include "javascriptapi.h"
18
19 static const char *TAG = "[javascriptapi_object_wrap]";
20
21 class testNapiWrap {
22 public:
23 static napi_value Init(napi_env env, napi_value exports);
24 static void Destructor(napi_env env, void *nativeObject, void *finalizeHint);
25
26 private:
27 explicit testNapiWrap(napi_value value_ = 0);
28 ~testNapiWrap();
29
30 static napi_value New(napi_env env, napi_callback_info info);
31 static napi_value Tyof(napi_env env, napi_callback_info info);
32
33 napi_value value_;
34 napi_env env_;
35 napi_ref wrapper_;
36 };
37
38 static thread_local napi_ref g_ref = nullptr;
39
testNapiWrap(napi_value value)40 testNapiWrap::testNapiWrap(napi_value value) : value_(value), env_(nullptr), wrapper_(nullptr) {}
41
~testNapiWrap()42 testNapiWrap::~testNapiWrap() { napi_delete_reference(env_, wrapper_); }
43
Destructor(napi_env env,void * nativeObject,void * finalizeHint)44 void testNapiWrap::Destructor(napi_env env, void *nativeObject, [[maybe_unused]] void *finalizeHint)
45 {
46 reinterpret_cast<testNapiWrap *>(nativeObject)->~testNapiWrap();
47 }
48
Tyof(napi_env env,napi_callback_info info)49 napi_value testNapiWrap::Tyof(napi_env env, napi_callback_info info)
50 {
51 napi_value jsThis;
52 napi_valuetype result;
53 napi_value resultStr;
54 napi_status status;
55 size_t argc = PARAM1;
56 napi_value argv[PARAM1];
57 const napi_extended_error_info *extended_error_info;
58 status = napi_get_cb_info(env, info, &argc, argv, &jsThis, nullptr);
59 if (status != napi_ok) {
60 getErrMsg(status, env, extended_error_info, "get cb info", TAG);
61 return NULL;
62 }
63 testNapiWrap *obj;
64 status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&obj));
65 if (status != napi_ok || obj == nullptr) {
66 getErrMsg(status, env, extended_error_info, "call napi_typeof()", TAG);
67 return NULL;
68 }
69 status = napi_typeof(env, argv[0], &result);
70 if (status != napi_ok) {
71 getErrMsg(status, env, extended_error_info, "call napi_typeof()", TAG);
72 return NULL;
73 }
74 status = napiValueType2Str(env, result, &resultStr);
75 if (status != napi_ok) {
76 std::string errMsg = "Failed to convert napi_valuetype " + std::to_string(status) + " to string";
77 napi_throw_error(env, NULL, errMsg.c_str());
78 return NULL;
79 }
80 return resultStr;
81 }
82
New(napi_env env,napi_callback_info info)83 napi_value testNapiWrap::New(napi_env env, napi_callback_info info)
84 {
85 napi_value newTarget;
86 napi_get_new_target(env, info, &newTarget);
87 if (newTarget != nullptr) {
88 size_t argc = PARAM1;
89 napi_value args[PARAM1];
90 napi_value jsThis;
91 napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
92 napi_value value;
93 testNapiWrap *obj = new testNapiWrap(value);
94 obj->env_ = env;
95 napi_wrap(env, jsThis, reinterpret_cast<void *>(obj), testNapiWrap::Destructor,
96 nullptr, // finalize_hint
97 &obj->wrapper_);
98 return jsThis;
99 } else {
100 size_t argc = PARAM1;
101 napi_value args[PARAM1];
102 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
103 napi_value cons;
104 napi_get_reference_value(env, g_ref, &cons);
105 napi_value instance;
106 napi_new_instance(env, cons, argc, args, &instance);
107 return instance;
108 }
109 }
110
Init(napi_env env,napi_value exports)111 napi_value testNapiWrap::Init(napi_env env, napi_value exports)
112 {
113 napi_property_descriptor properties[] = {{"Tyof", nullptr, Tyof, nullptr, nullptr, nullptr, napi_default, nullptr}};
114 napi_value cons;
115 napi_define_class(env, "testNapiWrap", NAPI_AUTO_LENGTH, New, nullptr, 1, properties, &cons);
116 napi_create_reference(env, cons, 1, &g_ref);
117 napi_set_named_property(env, exports, "testNapiWrap", cons);
118 return exports;
119 }
120
121
WrapInit(napi_env env,napi_value exports)122 napi_value WrapInit(napi_env env, napi_value exports)
123 {
124 testNapiWrap::Init(env, exports);
125 return exports;
126 }
127