Skip to content

Commit

Permalink
n-api: DRY napi_coerce_to_x() API methods
Browse files Browse the repository at this point in the history
PR-URL: #27796
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
bnoordhuis authored and targos committed May 28, 2019
1 parent 530e63a commit 2b7ad12
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2139,21 +2139,6 @@ napi_status napi_get_value_string_utf16(napi_env env,
return napi_clear_last_error(env);
}

napi_status napi_coerce_to_object(napi_env env,
napi_value value,
napi_value* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, value);
CHECK_ARG(env, result);

v8::Local<v8::Context> context = env->context();
v8::Local<v8::Object> obj;
CHECK_TO_OBJECT(env, context, obj, value);

*result = v8impl::JsValueFromV8LocalValue(obj);
return GET_RETURN_STATUS(env);
}

napi_status napi_coerce_to_bool(napi_env env,
napi_value value,
napi_value* result) {
Expand All @@ -2168,37 +2153,28 @@ napi_status napi_coerce_to_bool(napi_env env,
return GET_RETURN_STATUS(env);
}

napi_status napi_coerce_to_number(napi_env env,
napi_value value,
napi_value* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, value);
CHECK_ARG(env, result);

v8::Local<v8::Context> context = env->context();
v8::Local<v8::Number> num;

CHECK_TO_NUMBER(env, context, num, value);

*result = v8impl::JsValueFromV8LocalValue(num);
return GET_RETURN_STATUS(env);
}

napi_status napi_coerce_to_string(napi_env env,
napi_value value,
napi_value* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, value);
CHECK_ARG(env, result);

v8::Local<v8::Context> context = env->context();
v8::Local<v8::String> str;

CHECK_TO_STRING(env, context, str, value);

*result = v8impl::JsValueFromV8LocalValue(str);
return GET_RETURN_STATUS(env);
}
#define GEN_COERCE_FUNCTION(UpperCaseName, MixedCaseName, LowerCaseName) \
napi_status napi_coerce_to_##LowerCaseName(napi_env env, \
napi_value value, \
napi_value* result) { \
NAPI_PREAMBLE(env); \
CHECK_ARG(env, value); \
CHECK_ARG(env, result); \
\
v8::Local<v8::Context> context = env->context(); \
v8::Local<v8::MixedCaseName> str; \
\
CHECK_TO_##UpperCaseName(env, context, str, value); \
\
*result = v8impl::JsValueFromV8LocalValue(str); \
return GET_RETURN_STATUS(env); \
}

GEN_COERCE_FUNCTION(NUMBER, Number, number)
GEN_COERCE_FUNCTION(OBJECT, Object, object)
GEN_COERCE_FUNCTION(STRING, String, string)

#undef GEN_COERCE_FUNCTION

napi_status napi_wrap(napi_env env,
napi_value js_object,
Expand Down

0 comments on commit 2b7ad12

Please sign in to comment.