Skip to content

Commit

Permalink
src: update to latest n-api
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node-addon-api#140
Reviewed-By: Jason Ginchereau <[email protected]>
  • Loading branch information
wroy7860 committed Sep 21, 2017
1 parent 55ccf71 commit 42e8373
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 98 deletions.
43 changes: 22 additions & 21 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,21 @@ struct AccessorCallbackData {
////////////////////////////////////////////////////////////////////////////////

#define NODE_API_MODULE(modname, regfunc) \
void __napi_ ## regfunc(napi_env env, \
napi_value exports, \
napi_value module, \
void* priv) { \
Napi::RegisterModule(env, exports, module, regfunc); \
napi_value __napi_ ## regfunc(napi_env env, \
napi_value exports) { \
return Napi::RegisterModule(env, exports, regfunc); \
} \
NAPI_MODULE(modname, __napi_ ## regfunc);

// Adapt the NAPI_MODULE registration function:
// - Wrap the arguments in NAPI wrappers.
// - Catch any NAPI errors and rethrow as JS exceptions.
inline void RegisterModule(napi_env env,
napi_value exports,
napi_value module,
ModuleRegisterCallback registerCallback) {
details::WrapCallback([&] {
registerCallback(Napi::Env(env),
Napi::Object(env, exports),
Napi::Object(env, module));
return nullptr;
inline napi_value RegisterModule(napi_env env,
napi_value exports,
ModuleRegisterCallback registerCallback) {
return details::WrapCallback([&] {
return napi_value(registerCallback(Napi::Env(env),
Napi::Object(env, exports)));
});
}

Expand Down Expand Up @@ -1209,7 +1204,7 @@ inline Function Function::New(napi_env env,

napi_value value;
napi_status status = napi_create_function(
env, utf8name, CbData::Wrapper, callbackData, &value);
env, utf8name, -1, CbData::Wrapper, callbackData, &value);
NAPI_THROW_IF_FAILED(env, status, Function());
return Function(env, value);
}
Expand Down Expand Up @@ -1274,7 +1269,7 @@ inline Value Function::MakeCallback(
napi_value recv, size_t argc, const napi_value* args) const {
napi_value result;
napi_status status = napi_make_callback(
_env, recv, _value, argc, args, &result);
_env, nullptr, recv, _value, argc, args, &result);
NAPI_THROW_IF_FAILED(_env, status, Value());
return Value(_env, result);
}
Expand Down Expand Up @@ -1481,7 +1476,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, message);
napi_fatal_error(location, -1, message, -1);
}

inline Error::Error() : ObjectReference(), _message(nullptr) {
Expand Down Expand Up @@ -2317,7 +2312,7 @@ inline Function ObjectWrap<T>::DefineClass(
void* data) {
napi_value value;
napi_status status = napi_define_class(
env, utf8name, T::ConstructorCallbackWrapper, data, properties.size(),
env, utf8name, -1, T::ConstructorCallbackWrapper, data, properties.size(),
reinterpret_cast<const napi_property_descriptor*>(properties.begin()), &value);
NAPI_THROW_IF_FAILED(env, status, Function());

Expand All @@ -2332,7 +2327,7 @@ inline Function ObjectWrap<T>::DefineClass(
void* data) {
napi_value value;
napi_status status = napi_define_class(
env, utf8name, T::ConstructorCallbackWrapper, data, properties.size(),
env, utf8name, -1, T::ConstructorCallbackWrapper, data, properties.size(),
reinterpret_cast<const napi_property_descriptor*>(properties.data()), &value);
NAPI_THROW_IF_FAILED(env, status, Function());

Expand Down Expand Up @@ -2685,8 +2680,14 @@ inline AsyncWorker::AsyncWorker(const Object& receiver, const Function& callback
: _env(callback.Env()),
_receiver(Napi::Persistent(receiver)),
_callback(Napi::Persistent(callback)) {
napi_status status = napi_create_async_work(
_env, OnExecute, OnWorkComplete, this, &_work);

napi_value resource_id;
napi_status status = napi_create_string_latin1(
_env, "generic", -1, &resource_id);
NAPI_THROW_IF_FAILED(_env, status);

status = napi_create_async_work(
_env, nullptr, resource_id, OnExecute, OnWorkComplete, this, &_work);
NAPI_THROW_IF_FAILED(_env, status);
}

Expand Down
2 changes: 1 addition & 1 deletion napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace Napi {
typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values

/// Defines the signature of a N-API C++ module's registration callback (init) function.
typedef void (*ModuleRegisterCallback)(Env env, Object exports, Object module);
typedef Object (*ModuleRegisterCallback)(Env env, Object exports);

/// Environment for N-API values and operations.
///
Expand Down
Loading

0 comments on commit 42e8373

Please sign in to comment.