From c6b7fcab1b176c76fe80a433ad6489a011641c4f Mon Sep 17 00:00:00 2001 From: Taylor Woll Date: Tue, 16 Jan 2018 19:47:14 -0800 Subject: [PATCH] napi: use new.target jsrt api (#451) 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. --- src/node_api_jsrt.cc | 59 +++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/node_api_jsrt.cc b/src/node_api_jsrt.cc index f61a3fed5e3..99f500b54f3 100644 --- a/src/node_api_jsrt.cc +++ b/src/node_api_jsrt.cc @@ -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(callbackState); @@ -238,15 +238,8 @@ class ExternalCallback { napi_clear_last_error(); CallbackInfo cbInfo; - cbInfo.thisArg = reinterpret_cast(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(info->thisArg); + cbInfo.newTarget = reinterpret_cast(info->newTargetArg); cbInfo.argc = argumentCount - 1; cbInfo.argv = reinterpret_cast(arguments + 1); cbInfo.data = externalCallback->_data; @@ -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)); @@ -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)); @@ -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));