Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary refactoring from Babylon Native #569

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ inline bool Value::IsDataView() const {
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
inline bool Value::IsBuffer() const {
if (IsEmpty()) {
return false;
Expand All @@ -473,6 +473,7 @@ inline bool Value::IsBuffer() const {
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

inline bool Value::IsExternal() const {
return Type() == napi_external;
Expand Down Expand Up @@ -1354,12 +1355,12 @@ inline ArrayBuffer::ArrayBuffer(napi_env env, napi_value value, void* data, size
: Object(env, value), _data(data), _length(length) {
}

inline void* ArrayBuffer::Data() {
inline void* ArrayBuffer::Data() const {
EnsureInfo();
return _data;
}

inline size_t ArrayBuffer::ByteLength() {
inline size_t ArrayBuffer::ByteLength() const {
EnsureInfo();
return _length;
}
Expand Down Expand Up @@ -1787,6 +1788,7 @@ inline Value Function::Call(napi_value recv, size_t argc, const napi_value* args
return Value(_env, result);
}

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
inline Value Function::MakeCallback(
napi_value recv,
const std::initializer_list<napi_value>& args,
Expand All @@ -1812,6 +1814,7 @@ inline Value Function::MakeCallback(
NAPI_THROW_IF_FAILED(_env, status, Value());
return Value(_env, result);
}
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

inline Object Function::New(const std::initializer_list<napi_value>& args) const {
return New(args.size(), args.begin());
Expand Down Expand Up @@ -1863,6 +1866,7 @@ inline void Promise::Deferred::Reject(napi_value value) const {
inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {
}

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
////////////////////////////////////////////////////////////////////////////////
// Buffer<T> class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1981,6 +1985,7 @@ inline void Buffer<T>::EnsureInfo() const {
_data = static_cast<T*>(voidData);
}
}
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

////////////////////////////////////////////////////////////////////////////////
// Error class
Expand Down Expand Up @@ -2045,7 +2050,7 @@ inline Error Error::New(napi_env env, const std::string& message) {
}

inline NAPI_NO_RETURN void Error::Fatal(const char* location, const char* message) {
napi_fatal_error(location, NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
napi_fatal_error(location, NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated

}

inline Error::Error() : ObjectReference() {
Expand Down Expand Up @@ -2567,6 +2572,7 @@ inline Napi::Value FunctionReference::Call(
return scope.Escape(result);
}

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
inline Napi::Value FunctionReference::MakeCallback(
napi_value recv,
const std::initializer_list<napi_value>& args,
Expand Down Expand Up @@ -2603,6 +2609,7 @@ inline Napi::Value FunctionReference::MakeCallback(
}
return scope.Escape(result);
}
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

inline Object FunctionReference::New(const std::initializer_list<napi_value>& args) const {
EscapableHandleScope scope(_env);
Expand Down Expand Up @@ -3511,7 +3518,7 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
return Value(_env, result);
}


#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
#if (NAPI_VERSION > 2)
////////////////////////////////////////////////////////////////////////////////
// CallbackScope class
Expand Down Expand Up @@ -4138,6 +4145,7 @@ inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) {
NAPI_THROW_IF_FAILED(env, status, 0);
return result;
}
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

} // namespace Napi

Expand Down
23 changes: 21 additions & 2 deletions napi.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#ifndef SRC_NAPI_H_
#define SRC_NAPI_H_

#ifdef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
#define NAPI_NO_RETURN
#endif

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
#include <node_api.h>
#else
#include "js_native_api.h"
#endif

#include <functional>
#include <initializer_list>
#include <memory>
Expand Down Expand Up @@ -787,8 +796,8 @@ namespace Napi {
ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
ArrayBuffer(napi_env env, napi_value value); ///< Wraps a N-API value primitive.

void* Data(); ///< Gets a pointer to the data buffer.
size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
void* Data() const; ///< Gets a pointer to the data buffer.
size_t ByteLength() const; ///< Gets the length of the array buffer in bytes.
Comment on lines -790 to +800
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are unrelated, though relevant. Can you please submit them in a separate PR?


private:
mutable void* _data;
Expand Down Expand Up @@ -1006,6 +1015,7 @@ namespace Napi {
Value Call(napi_value recv, const std::vector<napi_value>& args) const;
Value Call(napi_value recv, size_t argc, const napi_value* args) const;

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
Value MakeCallback(napi_value recv,
const std::initializer_list<napi_value>& args,
napi_async_context context = nullptr) const;
Expand All @@ -1016,6 +1026,7 @@ namespace Napi {
size_t argc,
const napi_value* args,
napi_async_context context = nullptr) const;
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

Object New(const std::initializer_list<napi_value>& args) const;
Object New(const std::vector<napi_value>& args) const;
Expand Down Expand Up @@ -1044,6 +1055,7 @@ namespace Napi {
Promise(napi_env env, napi_value value);
};

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
template <typename T>
class Buffer : public Uint8Array {
public:
Expand Down Expand Up @@ -1076,6 +1088,7 @@ namespace Napi {
Buffer(napi_env env, napi_value value, size_t length, T* data);
void EnsureInfo() const;
};
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

/// Holds a counted reference to a value; initially a weak reference unless otherwise specified,
/// may be changed to/from a strong reference by adjusting the refcount.
Expand Down Expand Up @@ -1187,6 +1200,7 @@ namespace Napi {
Napi::Value Call(napi_value recv, const std::vector<napi_value>& args) const;
Napi::Value Call(napi_value recv, size_t argc, const napi_value* args) const;

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
Napi::Value MakeCallback(napi_value recv,
const std::initializer_list<napi_value>& args,
napi_async_context context = nullptr) const;
Expand All @@ -1197,6 +1211,7 @@ namespace Napi {
size_t argc,
const napi_value* args,
napi_async_context context = nullptr) const;
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

Object New(const std::initializer_list<napi_value>& args) const;
Object New(const std::vector<napi_value>& args) const;
Expand Down Expand Up @@ -1754,6 +1769,7 @@ namespace Napi {
napi_escapable_handle_scope _scope;
};

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
#if (NAPI_VERSION > 2)
class CallbackScope {
public:
Expand Down Expand Up @@ -1855,6 +1871,7 @@ namespace Napi {
std::string _error;
bool _suppress_destruct;
};
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

#if (NAPI_VERSION > 3)
class ThreadSafeFunction {
Expand Down Expand Up @@ -2063,6 +2080,7 @@ namespace Napi {
};
#endif

#ifndef NODE_ADDON_API_DISABLE_NODE_SPECIFIC
// Memory management.
class MemoryManagement {
public:
Expand All @@ -2075,6 +2093,7 @@ namespace Napi {
static uint32_t GetNapiVersion(Env env);
static const napi_node_version* GetNodeVersion(Env env);
};
#endif // NODE_ADDON_API_DISABLE_NODE_SPECIFIC

} // namespace Napi

Expand Down