Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-api: add fast paths for integer getters #14393

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,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 @@ -1928,6 +1934,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 @@ -1947,6 +1959,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