From 55e3ca88bc1c40d582bf76eaba7059ba01386d41 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 20 Jul 2017 17:03:11 +0200 Subject: [PATCH] n-api: add fast paths for integer getters Ref: https://github.com/nodejs/node/issues/14379 PR-URL: https://github.com/nodejs/node/pull/14393 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Jason Ginchereau --- src/node_api.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/node_api.cc b/src/node_api.cc index 58aeaf08979501..986a9facccac9d 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1912,6 +1912,12 @@ napi_status napi_get_value_int32(napi_env env, CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + if (val->IsInt32()) { + *result = val.As()->Value(); + return napi_clear_last_error(env); + } + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); v8::Isolate* isolate = env->isolate; @@ -1931,6 +1937,12 @@ napi_status napi_get_value_uint32(napi_env env, CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + if (val->IsUint32()) { + *result = val.As()->Value(); + return napi_clear_last_error(env); + } + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); v8::Isolate* isolate = env->isolate; @@ -1950,6 +1962,13 @@ napi_status napi_get_value_int64(napi_env env, CHECK_ARG(env, result); v8::Local val = v8impl::V8LocalValueFromJsValue(value); + + // This is still a fast path very likely to be taken. + if (val->IsInt32()) { + *result = val.As()->Value(); + return napi_clear_last_error(env); + } + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); // v8::Value::IntegerValue() converts NaN to INT64_MIN, inconsistent with