Skip to content

Commit

Permalink
update naa to 6.0.0 and nodejs/node-addon-api#1283
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Feb 11, 2023
1 parent 75d937d commit 869adfe
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 78 deletions.
17 changes: 17 additions & 0 deletions packages/emnapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.13)
project(emnapi)

option(EMNAPI_INSTALL_SRC "EMNAPI_INSTALL_SRC" OFF)
option(EMNAPI_FIND_NODE_ADDON_API "EMNAPI_FIND_NODE_ADDON_API" OFF)

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(IS_EMSCRIPTEN ON)
Expand Down Expand Up @@ -41,6 +42,22 @@ set(EMNAPI_MT_TARGET_NAME "emnapi-mt")
set(DLMALLOC_TARGET_NAME "dlmalloc")
set(EMMALLOC_TARGET_NAME "emmalloc")

if(EMNAPI_FIND_NODE_ADDON_API)
execute_process(
COMMAND "node" "-p" "require('node-addon-api').include"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE OUTPUT_NAA_INCLUDE_DIR
ERROR_VARIABLE ERROR_NAA_INCLUDE_DIR
)
if(NOT (ERROR_NAA_INCLUDE_DIR STREQUAL ""))
message(WARNING "Cannot find module 'node-addon-api'")
else()
string(REGEX REPLACE "(\r?\n)|\"" "" OUTPUT_NAA_INCLUDE_DIR "${OUTPUT_NAA_INCLUDE_DIR}")
string(REPLACE "\\" "/" OUTPUT_NAA_INCLUDE_DIR "${OUTPUT_NAA_INCLUDE_DIR}")
list(PREPEND EMNAPI_INCLUDE "${OUTPUT_NAA_INCLUDE_DIR}")
endif()
endif()

if(IS_WASM32)
set(MALLOC_PUBLIC_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/malloc/sbrk.c"
Expand Down
186 changes: 186 additions & 0 deletions packages/emnapi/include/napi-inl.deprecated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#ifndef SRC_NAPI_INL_DEPRECATED_H_
#define SRC_NAPI_INL_DEPRECATED_H_

////////////////////////////////////////////////////////////////////////////////
// PropertyDescriptor class
////////////////////////////////////////////////////////////////////////////////

template <typename Getter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
const char* utf8name,
Getter getter,
napi_property_attributes attributes,
void* /*data*/) {
using CbData = details::CallbackData<Getter, Napi::Value>;
// TODO: Delete when the function is destroyed
auto callbackData = new CbData({getter, nullptr});

return PropertyDescriptor({utf8name,
nullptr,
nullptr,
CbData::Wrapper,
nullptr,
nullptr,
attributes,
callbackData});
}

template <typename Getter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
const std::string& utf8name,
Getter getter,
napi_property_attributes attributes,
void* data) {
return Accessor(utf8name.c_str(), getter, attributes, data);
}

template <typename Getter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
napi_value name,
Getter getter,
napi_property_attributes attributes,
void* /*data*/) {
using CbData = details::CallbackData<Getter, Napi::Value>;
// TODO: Delete when the function is destroyed
auto callbackData = new CbData({getter, nullptr});

return PropertyDescriptor({nullptr,
name,
nullptr,
CbData::Wrapper,
nullptr,
nullptr,
attributes,
callbackData});
}

template <typename Getter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
Name name, Getter getter, napi_property_attributes attributes, void* data) {
napi_value nameValue = name;
return PropertyDescriptor::Accessor(nameValue, getter, attributes, data);
}

template <typename Getter, typename Setter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
const char* utf8name,
Getter getter,
Setter setter,
napi_property_attributes attributes,
void* /*data*/) {
using CbData = details::AccessorCallbackData<Getter, Setter>;
// TODO: Delete when the function is destroyed
auto callbackData = new CbData({getter, setter, nullptr});

return PropertyDescriptor({utf8name,
nullptr,
nullptr,
CbData::GetterWrapper,
CbData::SetterWrapper,
nullptr,
attributes,
callbackData});
}

template <typename Getter, typename Setter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
const std::string& utf8name,
Getter getter,
Setter setter,
napi_property_attributes attributes,
void* data) {
return Accessor(utf8name.c_str(), getter, setter, attributes, data);
}

template <typename Getter, typename Setter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
napi_value name,
Getter getter,
Setter setter,
napi_property_attributes attributes,
void* /*data*/) {
using CbData = details::AccessorCallbackData<Getter, Setter>;
// TODO: Delete when the function is destroyed
auto callbackData = new CbData({getter, setter, nullptr});

return PropertyDescriptor({nullptr,
name,
nullptr,
CbData::GetterWrapper,
CbData::SetterWrapper,
nullptr,
attributes,
callbackData});
}

template <typename Getter, typename Setter>
inline PropertyDescriptor PropertyDescriptor::Accessor(
Name name,
Getter getter,
Setter setter,
napi_property_attributes attributes,
void* data) {
napi_value nameValue = name;
return PropertyDescriptor::Accessor(
nameValue, getter, setter, attributes, data);
}

template <typename Callable>
inline PropertyDescriptor PropertyDescriptor::Function(
const char* utf8name,
Callable cb,
napi_property_attributes attributes,
void* /*data*/) {
using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
using CbData = details::CallbackData<Callable, ReturnType>;
// TODO: Delete when the function is destroyed
auto callbackData = new CbData({cb, nullptr});

return PropertyDescriptor({utf8name,
nullptr,
CbData::Wrapper,
nullptr,
nullptr,
nullptr,
attributes,
callbackData});
}

template <typename Callable>
inline PropertyDescriptor PropertyDescriptor::Function(
const std::string& utf8name,
Callable cb,
napi_property_attributes attributes,
void* data) {
return Function(utf8name.c_str(), cb, attributes, data);
}

template <typename Callable>
inline PropertyDescriptor PropertyDescriptor::Function(
napi_value name,
Callable cb,
napi_property_attributes attributes,
void* /*data*/) {
using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
using CbData = details::CallbackData<Callable, ReturnType>;
// TODO: Delete when the function is destroyed
auto callbackData = new CbData({cb, nullptr});

return PropertyDescriptor({nullptr,
name,
CbData::Wrapper,
nullptr,
nullptr,
nullptr,
attributes,
callbackData});
}

template <typename Callable>
inline PropertyDescriptor PropertyDescriptor::Function(
Name name, Callable cb, napi_property_attributes attributes, void* data) {
napi_value nameValue = name;
return PropertyDescriptor::Function(nameValue, cb, attributes, data);
}

#endif // !SRC_NAPI_INL_DEPRECATED_H_
104 changes: 49 additions & 55 deletions packages/emnapi/include/napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <algorithm>
#include <cstring>
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
#if NAPI_HAS_THREADS
#include <mutex>
#endif
#include <type_traits>
Expand Down Expand Up @@ -203,7 +203,7 @@ struct FinalizeData {
Hint* hint;
};

#if (NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)))
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
template <typename ContextType = void,
typename Finalizer = std::function<void(Env, void*, ContextType*)>,
typename FinalizerDataType = void>
Expand Down Expand Up @@ -297,7 +297,7 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
return cb;
}
#endif // NAPI_VERSION > 4
#endif // NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT))
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS

template <typename Getter, typename Setter>
struct AccessorCallbackData {
Expand Down Expand Up @@ -331,9 +331,9 @@ struct AccessorCallbackData {

} // namespace details

// #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
// #include "napi-inl.deprecated.h"
// #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
#ifndef NODE_ADDON_API_DISABLE_DEPRECATED
#include "napi-inl.deprecated.h"
#endif // !NODE_ADDON_API_DISABLE_DEPRECATED

////////////////////////////////////////////////////////////////////////////////
// Module registration
Expand Down Expand Up @@ -1628,6 +1628,19 @@ inline MaybeOrValue<bool> Object::Seal() const {
napi_status status = napi_object_seal(_env, _value);
NAPI_RETURN_OR_THROW_IF_FAILED(_env, status, status == napi_ok, bool);
}

inline void Object::TypeTag(const napi_type_tag* type_tag) const {
napi_status status = napi_type_tag_object(_env, _value, type_tag);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}

inline bool Object::CheckTypeTag(const napi_type_tag* type_tag) const {
bool result;
napi_status status =
napi_check_object_type_tag(_env, _value, type_tag, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
#endif // NAPI_VERSION >= 8

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4638,35 +4651,35 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
return Value(_env, result);
}

// #if (NAPI_VERSION > 2)
// ////////////////////////////////////////////////////////////////////////////////
// // CallbackScope class
// ////////////////////////////////////////////////////////////////////////////////
#if (NAPI_VERSION > 2 && !defined(__wasm__))
////////////////////////////////////////////////////////////////////////////////
// CallbackScope class
////////////////////////////////////////////////////////////////////////////////

// inline CallbackScope::CallbackScope(napi_env env, napi_callback_scope scope)
// : _env(env), _scope(scope) {}
inline CallbackScope::CallbackScope(napi_env env, napi_callback_scope scope)
: _env(env), _scope(scope) {}

// inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
// : _env(env) {
// napi_status status =
// napi_open_callback_scope(_env, Object::New(env), context, &_scope);
// NAPI_THROW_IF_FAILED_VOID(_env, status);
// }
inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
: _env(env) {
napi_status status =
napi_open_callback_scope(_env, Object::New(env), context, &_scope);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}

// inline CallbackScope::~CallbackScope() {
// napi_status status = napi_close_callback_scope(_env, _scope);
// NAPI_FATAL_IF_FAILED(
// status, "CallbackScope::~CallbackScope", "napi_close_callback_scope");
// }
inline CallbackScope::~CallbackScope() {
napi_status status = napi_close_callback_scope(_env, _scope);
NAPI_FATAL_IF_FAILED(
status, "CallbackScope::~CallbackScope", "napi_close_callback_scope");
}

// inline CallbackScope::operator napi_callback_scope() const {
// return _scope;
// }
inline CallbackScope::operator napi_callback_scope() const {
return _scope;
}

// inline Napi::Env CallbackScope::Env() const {
// return Napi::Env(_env);
// }
// #endif
inline Napi::Env CallbackScope::Env() const {
return Napi::Env(_env);
}
#endif

////////////////////////////////////////////////////////////////////////////////
// AsyncContext class
Expand Down Expand Up @@ -4722,6 +4735,8 @@ inline Napi::Env AsyncContext::Env() const {
// AsyncWorker class
////////////////////////////////////////////////////////////////////////////////

#if NAPI_HAS_THREADS

inline AsyncWorker::AsyncWorker(const Function& callback)
: AsyncWorker(callback, "generic") {}

Expand Down Expand Up @@ -4803,29 +4818,6 @@ inline void AsyncWorker::Destroy() {
delete this;
}

inline AsyncWorker::AsyncWorker(AsyncWorker&& other) {
_env = other._env;
other._env = nullptr;
_work = other._work;
other._work = nullptr;
_receiver = std::move(other._receiver);
_callback = std::move(other._callback);
_error = std::move(other._error);
_suppress_destruct = other._suppress_destruct;
}

inline AsyncWorker& AsyncWorker::operator=(AsyncWorker&& other) {
_env = other._env;
other._env = nullptr;
_work = other._work;
other._work = nullptr;
_receiver = std::move(other._receiver);
_callback = std::move(other._callback);
_error = std::move(other._error);
_suppress_destruct = other._suppress_destruct;
return *this;
}

inline AsyncWorker::operator napi_async_work() const {
return _work;
}
Expand Down Expand Up @@ -4923,7 +4915,9 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
}
}

#if (NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)))
#endif // NAPI_HAS_THREADS

#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
////////////////////////////////////////////////////////////////////////////////
// TypedThreadSafeFunction<ContextType,DataType,CallJs> class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -6172,7 +6166,7 @@ inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(
const T* data, size_t count) const {
_worker->SendProgress_(data, count);
}
#endif // NAPI_VERSION > 3 && (defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT))
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS

////////////////////////////////////////////////////////////////////////////////
// Memory Management class
Expand Down
Loading

0 comments on commit 869adfe

Please sign in to comment.