Skip to content

Commit

Permalink
n-api: add fast paths for integer getters
Browse files Browse the repository at this point in the history
Ref: nodejs#14379
PR-URL: nodejs#14393
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
  • Loading branch information
addaleax authored and Gabriel Schulhof committed Apr 10, 2018
1 parent e885661 commit 55e3ca8
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,12 @@ napi_status napi_get_value_int32(napi_env env,
CHECK_ARG(env, result);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);

if (val->IsInt32()) {
*result = val.As<v8::Int32>()->Value();
return napi_clear_last_error(env);
}

RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected);

v8::Isolate* isolate = env->isolate;
Expand All @@ -1931,6 +1937,12 @@ napi_status napi_get_value_uint32(napi_env env,
CHECK_ARG(env, result);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);

if (val->IsUint32()) {
*result = val.As<v8::Uint32>()->Value();
return napi_clear_last_error(env);
}

RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected);

v8::Isolate* isolate = env->isolate;
Expand All @@ -1950,6 +1962,13 @@ napi_status napi_get_value_int64(napi_env env,
CHECK_ARG(env, result);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);

// This is still a fast path very likely to be taken.
if (val->IsInt32()) {
*result = val.As<v8::Int32>()->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
Expand Down

0 comments on commit 55e3ca8

Please sign in to comment.