Skip to content

Commit 869adfe

Browse files
committed
update naa to 6.0.0 and nodejs/node-addon-api#1283
1 parent 75d937d commit 869adfe

File tree

4 files changed

+285
-78
lines changed

4 files changed

+285
-78
lines changed

packages/emnapi/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.13)
33
project(emnapi)
44

55
option(EMNAPI_INSTALL_SRC "EMNAPI_INSTALL_SRC" OFF)
6+
option(EMNAPI_FIND_NODE_ADDON_API "EMNAPI_FIND_NODE_ADDON_API" OFF)
67

78
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
89
set(IS_EMSCRIPTEN ON)
@@ -41,6 +42,22 @@ set(EMNAPI_MT_TARGET_NAME "emnapi-mt")
4142
set(DLMALLOC_TARGET_NAME "dlmalloc")
4243
set(EMMALLOC_TARGET_NAME "emmalloc")
4344

45+
if(EMNAPI_FIND_NODE_ADDON_API)
46+
execute_process(
47+
COMMAND "node" "-p" "require('node-addon-api').include"
48+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
49+
OUTPUT_VARIABLE OUTPUT_NAA_INCLUDE_DIR
50+
ERROR_VARIABLE ERROR_NAA_INCLUDE_DIR
51+
)
52+
if(NOT (ERROR_NAA_INCLUDE_DIR STREQUAL ""))
53+
message(WARNING "Cannot find module 'node-addon-api'")
54+
else()
55+
string(REGEX REPLACE "(\r?\n)|\"" "" OUTPUT_NAA_INCLUDE_DIR "${OUTPUT_NAA_INCLUDE_DIR}")
56+
string(REPLACE "\\" "/" OUTPUT_NAA_INCLUDE_DIR "${OUTPUT_NAA_INCLUDE_DIR}")
57+
list(PREPEND EMNAPI_INCLUDE "${OUTPUT_NAA_INCLUDE_DIR}")
58+
endif()
59+
endif()
60+
4461
if(IS_WASM32)
4562
set(MALLOC_PUBLIC_SOURCES
4663
"${CMAKE_CURRENT_SOURCE_DIR}/src/malloc/sbrk.c"
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#ifndef SRC_NAPI_INL_DEPRECATED_H_
2+
#define SRC_NAPI_INL_DEPRECATED_H_
3+
4+
////////////////////////////////////////////////////////////////////////////////
5+
// PropertyDescriptor class
6+
////////////////////////////////////////////////////////////////////////////////
7+
8+
template <typename Getter>
9+
inline PropertyDescriptor PropertyDescriptor::Accessor(
10+
const char* utf8name,
11+
Getter getter,
12+
napi_property_attributes attributes,
13+
void* /*data*/) {
14+
using CbData = details::CallbackData<Getter, Napi::Value>;
15+
// TODO: Delete when the function is destroyed
16+
auto callbackData = new CbData({getter, nullptr});
17+
18+
return PropertyDescriptor({utf8name,
19+
nullptr,
20+
nullptr,
21+
CbData::Wrapper,
22+
nullptr,
23+
nullptr,
24+
attributes,
25+
callbackData});
26+
}
27+
28+
template <typename Getter>
29+
inline PropertyDescriptor PropertyDescriptor::Accessor(
30+
const std::string& utf8name,
31+
Getter getter,
32+
napi_property_attributes attributes,
33+
void* data) {
34+
return Accessor(utf8name.c_str(), getter, attributes, data);
35+
}
36+
37+
template <typename Getter>
38+
inline PropertyDescriptor PropertyDescriptor::Accessor(
39+
napi_value name,
40+
Getter getter,
41+
napi_property_attributes attributes,
42+
void* /*data*/) {
43+
using CbData = details::CallbackData<Getter, Napi::Value>;
44+
// TODO: Delete when the function is destroyed
45+
auto callbackData = new CbData({getter, nullptr});
46+
47+
return PropertyDescriptor({nullptr,
48+
name,
49+
nullptr,
50+
CbData::Wrapper,
51+
nullptr,
52+
nullptr,
53+
attributes,
54+
callbackData});
55+
}
56+
57+
template <typename Getter>
58+
inline PropertyDescriptor PropertyDescriptor::Accessor(
59+
Name name, Getter getter, napi_property_attributes attributes, void* data) {
60+
napi_value nameValue = name;
61+
return PropertyDescriptor::Accessor(nameValue, getter, attributes, data);
62+
}
63+
64+
template <typename Getter, typename Setter>
65+
inline PropertyDescriptor PropertyDescriptor::Accessor(
66+
const char* utf8name,
67+
Getter getter,
68+
Setter setter,
69+
napi_property_attributes attributes,
70+
void* /*data*/) {
71+
using CbData = details::AccessorCallbackData<Getter, Setter>;
72+
// TODO: Delete when the function is destroyed
73+
auto callbackData = new CbData({getter, setter, nullptr});
74+
75+
return PropertyDescriptor({utf8name,
76+
nullptr,
77+
nullptr,
78+
CbData::GetterWrapper,
79+
CbData::SetterWrapper,
80+
nullptr,
81+
attributes,
82+
callbackData});
83+
}
84+
85+
template <typename Getter, typename Setter>
86+
inline PropertyDescriptor PropertyDescriptor::Accessor(
87+
const std::string& utf8name,
88+
Getter getter,
89+
Setter setter,
90+
napi_property_attributes attributes,
91+
void* data) {
92+
return Accessor(utf8name.c_str(), getter, setter, attributes, data);
93+
}
94+
95+
template <typename Getter, typename Setter>
96+
inline PropertyDescriptor PropertyDescriptor::Accessor(
97+
napi_value name,
98+
Getter getter,
99+
Setter setter,
100+
napi_property_attributes attributes,
101+
void* /*data*/) {
102+
using CbData = details::AccessorCallbackData<Getter, Setter>;
103+
// TODO: Delete when the function is destroyed
104+
auto callbackData = new CbData({getter, setter, nullptr});
105+
106+
return PropertyDescriptor({nullptr,
107+
name,
108+
nullptr,
109+
CbData::GetterWrapper,
110+
CbData::SetterWrapper,
111+
nullptr,
112+
attributes,
113+
callbackData});
114+
}
115+
116+
template <typename Getter, typename Setter>
117+
inline PropertyDescriptor PropertyDescriptor::Accessor(
118+
Name name,
119+
Getter getter,
120+
Setter setter,
121+
napi_property_attributes attributes,
122+
void* data) {
123+
napi_value nameValue = name;
124+
return PropertyDescriptor::Accessor(
125+
nameValue, getter, setter, attributes, data);
126+
}
127+
128+
template <typename Callable>
129+
inline PropertyDescriptor PropertyDescriptor::Function(
130+
const char* utf8name,
131+
Callable cb,
132+
napi_property_attributes attributes,
133+
void* /*data*/) {
134+
using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
135+
using CbData = details::CallbackData<Callable, ReturnType>;
136+
// TODO: Delete when the function is destroyed
137+
auto callbackData = new CbData({cb, nullptr});
138+
139+
return PropertyDescriptor({utf8name,
140+
nullptr,
141+
CbData::Wrapper,
142+
nullptr,
143+
nullptr,
144+
nullptr,
145+
attributes,
146+
callbackData});
147+
}
148+
149+
template <typename Callable>
150+
inline PropertyDescriptor PropertyDescriptor::Function(
151+
const std::string& utf8name,
152+
Callable cb,
153+
napi_property_attributes attributes,
154+
void* data) {
155+
return Function(utf8name.c_str(), cb, attributes, data);
156+
}
157+
158+
template <typename Callable>
159+
inline PropertyDescriptor PropertyDescriptor::Function(
160+
napi_value name,
161+
Callable cb,
162+
napi_property_attributes attributes,
163+
void* /*data*/) {
164+
using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
165+
using CbData = details::CallbackData<Callable, ReturnType>;
166+
// TODO: Delete when the function is destroyed
167+
auto callbackData = new CbData({cb, nullptr});
168+
169+
return PropertyDescriptor({nullptr,
170+
name,
171+
CbData::Wrapper,
172+
nullptr,
173+
nullptr,
174+
nullptr,
175+
attributes,
176+
callbackData});
177+
}
178+
179+
template <typename Callable>
180+
inline PropertyDescriptor PropertyDescriptor::Function(
181+
Name name, Callable cb, napi_property_attributes attributes, void* data) {
182+
napi_value nameValue = name;
183+
return PropertyDescriptor::Function(nameValue, cb, attributes, data);
184+
}
185+
186+
#endif // !SRC_NAPI_INL_DEPRECATED_H_

packages/emnapi/include/napi-inl.h

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include <algorithm>
1414
#include <cstring>
15-
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
15+
#if NAPI_HAS_THREADS
1616
#include <mutex>
1717
#endif
1818
#include <type_traits>
@@ -203,7 +203,7 @@ struct FinalizeData {
203203
Hint* hint;
204204
};
205205

206-
#if (NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)))
206+
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
207207
template <typename ContextType = void,
208208
typename Finalizer = std::function<void(Env, void*, ContextType*)>,
209209
typename FinalizerDataType = void>
@@ -297,7 +297,7 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
297297
return cb;
298298
}
299299
#endif // NAPI_VERSION > 4
300-
#endif // NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT))
300+
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
301301

302302
template <typename Getter, typename Setter>
303303
struct AccessorCallbackData {
@@ -331,9 +331,9 @@ struct AccessorCallbackData {
331331

332332
} // namespace details
333333

334-
// #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
335-
// #include "napi-inl.deprecated.h"
336-
// #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
334+
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
335+
#include "napi-inl.deprecated.h"
336+
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED
337337

338338
////////////////////////////////////////////////////////////////////////////////
339339
// Module registration
@@ -1628,6 +1628,19 @@ inline MaybeOrValue<bool> Object::Seal() const {
16281628
napi_status status = napi_object_seal(_env, _value);
16291629
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
16301630
}
1631+
1632+
inline void Object::TypeTag(const napi_type_tag* type_tag) const {
1633+
napi_status status = napi_type_tag_object(_env, _value, type_tag);
1634+
NAPI_THROW_IF_FAILED_VOID(_env, status);
1635+
}
1636+
1637+
inline bool Object::CheckTypeTag(const napi_type_tag* type_tag) const {
1638+
bool result;
1639+
napi_status status =
1640+
napi_check_object_type_tag(_env, _value, type_tag, &result);
1641+
NAPI_THROW_IF_FAILED(_env, status, false);
1642+
return result;
1643+
}
16311644
#endif // NAPI_VERSION >= 8
16321645

16331646
////////////////////////////////////////////////////////////////////////////////
@@ -4638,35 +4651,35 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
46384651
return Value(_env, result);
46394652
}
46404653

4641-
// #if (NAPI_VERSION > 2)
4642-
// ////////////////////////////////////////////////////////////////////////////////
4643-
// // CallbackScope class
4644-
// ////////////////////////////////////////////////////////////////////////////////
4654+
#if (NAPI_VERSION > 2 && !defined(__wasm__))
4655+
////////////////////////////////////////////////////////////////////////////////
4656+
// CallbackScope class
4657+
////////////////////////////////////////////////////////////////////////////////
46454658

4646-
// inline CallbackScope::CallbackScope(napi_env env, napi_callback_scope scope)
4647-
// : _env(env), _scope(scope) {}
4659+
inline CallbackScope::CallbackScope(napi_env env, napi_callback_scope scope)
4660+
: _env(env), _scope(scope) {}
46484661

4649-
// inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
4650-
// : _env(env) {
4651-
// napi_status status =
4652-
// napi_open_callback_scope(_env, Object::New(env), context, &_scope);
4653-
// NAPI_THROW_IF_FAILED_VOID(_env, status);
4654-
// }
4662+
inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
4663+
: _env(env) {
4664+
napi_status status =
4665+
napi_open_callback_scope(_env, Object::New(env), context, &_scope);
4666+
NAPI_THROW_IF_FAILED_VOID(_env, status);
4667+
}
46554668

4656-
// inline CallbackScope::~CallbackScope() {
4657-
// napi_status status = napi_close_callback_scope(_env, _scope);
4658-
// NAPI_FATAL_IF_FAILED(
4659-
// status, "CallbackScope::~CallbackScope", "napi_close_callback_scope");
4660-
// }
4669+
inline CallbackScope::~CallbackScope() {
4670+
napi_status status = napi_close_callback_scope(_env, _scope);
4671+
NAPI_FATAL_IF_FAILED(
4672+
status, "CallbackScope::~CallbackScope", "napi_close_callback_scope");
4673+
}
46614674

4662-
// inline CallbackScope::operator napi_callback_scope() const {
4663-
// return _scope;
4664-
// }
4675+
inline CallbackScope::operator napi_callback_scope() const {
4676+
return _scope;
4677+
}
46654678

4666-
// inline Napi::Env CallbackScope::Env() const {
4667-
// return Napi::Env(_env);
4668-
// }
4669-
// #endif
4679+
inline Napi::Env CallbackScope::Env() const {
4680+
return Napi::Env(_env);
4681+
}
4682+
#endif
46704683

46714684
////////////////////////////////////////////////////////////////////////////////
46724685
// AsyncContext class
@@ -4722,6 +4735,8 @@ inline Napi::Env AsyncContext::Env() const {
47224735
// AsyncWorker class
47234736
////////////////////////////////////////////////////////////////////////////////
47244737

4738+
#if NAPI_HAS_THREADS
4739+
47254740
inline AsyncWorker::AsyncWorker(const Function& callback)
47264741
: AsyncWorker(callback, "generic") {}
47274742

@@ -4803,29 +4818,6 @@ inline void AsyncWorker::Destroy() {
48034818
delete this;
48044819
}
48054820

4806-
inline AsyncWorker::AsyncWorker(AsyncWorker&& other) {
4807-
_env = other._env;
4808-
other._env = nullptr;
4809-
_work = other._work;
4810-
other._work = nullptr;
4811-
_receiver = std::move(other._receiver);
4812-
_callback = std::move(other._callback);
4813-
_error = std::move(other._error);
4814-
_suppress_destruct = other._suppress_destruct;
4815-
}
4816-
4817-
inline AsyncWorker& AsyncWorker::operator=(AsyncWorker&& other) {
4818-
_env = other._env;
4819-
other._env = nullptr;
4820-
_work = other._work;
4821-
other._work = nullptr;
4822-
_receiver = std::move(other._receiver);
4823-
_callback = std::move(other._callback);
4824-
_error = std::move(other._error);
4825-
_suppress_destruct = other._suppress_destruct;
4826-
return *this;
4827-
}
4828-
48294821
inline AsyncWorker::operator napi_async_work() const {
48304822
return _work;
48314823
}
@@ -4923,7 +4915,9 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
49234915
}
49244916
}
49254917

4926-
#if (NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)))
4918+
#endif // NAPI_HAS_THREADS
4919+
4920+
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
49274921
////////////////////////////////////////////////////////////////////////////////
49284922
// TypedThreadSafeFunction<ContextType,DataType,CallJs> class
49294923
////////////////////////////////////////////////////////////////////////////////
@@ -6172,7 +6166,7 @@ inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(
61726166
const T* data, size_t count) const {
61736167
_worker->SendProgress_(data, count);
61746168
}
6175-
#endif // NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT))
6169+
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
61766170

61776171
////////////////////////////////////////////////////////////////////////////////
61786172
// Memory Management class

0 commit comments

Comments
 (0)