Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
napi: use new.target jsrt api (#451)
Browse files Browse the repository at this point in the history
Now that Jsrt has added a way to fetch the new.target
argument from a function callback invocation, change
the N-API layer to fetch this value.
  • Loading branch information
boingoing authored Jan 17, 2018
1 parent d4795cd commit c6b7fca
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions src/node_api_jsrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ class ExternalCallback {
: _env(env), _cb(cb), _data(data) {
}

// JsNativeFunction
// JsEnhancedNativeFunction
static JsValueRef CALLBACK Callback(JsValueRef callee,
bool isConstructCall,
JsValueRef *arguments,
uint16_t argumentCount,
JsNativeFunctionInfo *info,
void *callbackState) {
jsrtimpl::ExternalCallback* externalCallback =
reinterpret_cast<jsrtimpl::ExternalCallback*>(callbackState);
Expand All @@ -238,15 +238,8 @@ class ExternalCallback {
napi_clear_last_error();

CallbackInfo cbInfo;
cbInfo.thisArg = reinterpret_cast<napi_value>(arguments[0]);

// TODO(digitalinfinity): This is incorrect as per spec, but
// implementing this behavior for now to at least handle the case
// where folks check newTarget != null to determine they're in a
// constructor.
// JSRT will need to implement an API to get new.target for this
// to work correctly
cbInfo.newTarget = (isConstructCall ? cbInfo.thisArg : nullptr);
cbInfo.thisArg = reinterpret_cast<napi_value>(info->thisArg);
cbInfo.newTarget = reinterpret_cast<napi_value>(info->newTargetArg);
cbInfo.argc = argumentCount - 1;
cbInfo.argv = reinterpret_cast<napi_value*>(arguments + 1);
cbInfo.data = externalCallback->_data;
Expand Down Expand Up @@ -688,24 +681,20 @@ napi_status napi_create_function(napi_env env,
}

JsValueRef function;
JsValueRef name = JS_INVALID_REFERENCE;
if (utf8name != nullptr) {
JsValueRef name;
CHECK_JSRT(JsCreateString(
utf8name,
length == -1 ? strlen(utf8name) : length,
&name));
CHECK_JSRT(JsCreateNamedFunction(
name,
jsrtimpl::ExternalCallback::Callback,
externalCallback,
&function));
} else {
CHECK_JSRT(JsCreateFunction(
jsrtimpl::ExternalCallback::Callback,
externalCallback,
&function));
}

CHECK_JSRT(JsCreateEnhancedFunction(
jsrtimpl::ExternalCallback::Callback,
name,
externalCallback,
&function));

CHECK_JSRT(JsSetObjectBeforeCollectCallback(
function, externalCallback, jsrtimpl::ExternalCallback::Finalize));

Expand All @@ -730,19 +719,17 @@ static napi_status napi_create_property_function(napi_env env,
CHECK_NAPI(napi_typeof(env, property_name, &nameType));

JsValueRef function;
JsValueRef name = JS_INVALID_REFERENCE;
if (nameType == napi_string) {
CHECK_JSRT(JsCreateNamedFunction(
property_name,
jsrtimpl::ExternalCallback::Callback,
externalCallback,
&function));
} else {
CHECK_JSRT(JsCreateFunction(
jsrtimpl::ExternalCallback::Callback,
externalCallback,
&function));
name = property_name;
}

CHECK_JSRT(JsCreateEnhancedFunction(
jsrtimpl::ExternalCallback::Callback,
name,
externalCallback,
&function));

CHECK_JSRT(JsSetObjectBeforeCollectCallback(
function, externalCallback, jsrtimpl::ExternalCallback::Finalize));

Expand Down Expand Up @@ -770,10 +757,10 @@ napi_status napi_define_class(napi_env env,
}

JsValueRef constructor;
CHECK_JSRT(JsCreateNamedFunction(namestring,
jsrtimpl::ExternalCallback::Callback,
externalCallback,
&constructor));
CHECK_JSRT(JsCreateEnhancedFunction(jsrtimpl::ExternalCallback::Callback,
namestring,
externalCallback,
&constructor));

CHECK_JSRT(JsSetObjectBeforeCollectCallback(
constructor, externalCallback, jsrtimpl::ExternalCallback::Finalize));
Expand Down

0 comments on commit c6b7fca

Please sign in to comment.