From af70c3b4781d8df928d183580b8075cd566ce0b5 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Tue, 18 Jul 2017 20:22:35 -0700 Subject: [PATCH] n-api: optimize number API performance - Add separate APIs for creating different kinds of numbers, because creating a V8 number value from an integer is faster than creating one from a double. - When getting number values, avoid getting the current context because the context will not actually be used and is expensive to obtain. - When creating values, don't use v8::TryCatch (NAPI_PREAMBLE), because these functions have no possibility of executing JS code. Refs: https://github.com/nodejs/node/issues/14379 PR-URL: https://github.com/nodejs/node/pull/14573 Reviewed-By: Timothy Gu Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig --- doc/api/n-api.md | 85 ++++++++++-- src/node_api.cc | 131 +++++++++++------- src/node_api.h | 11 +- .../2_function_arguments/binding.c | 2 +- test/addons-napi/6_object_wrap/myobject.cc | 6 +- test/addons-napi/7_factory_wrap/myobject.cc | 4 +- test/addons-napi/7_factory_wrap/myobject.h | 2 +- test/addons-napi/8_passing_wrapped/binding.cc | 2 +- test/addons-napi/test_async/test_async.cc | 2 +- test/addons-napi/test_buffer/test_buffer.c | 2 +- .../test_constructor/test_constructor.c | 6 +- .../test_conversions/test_conversions.c | 8 +- test/addons-napi/test_general/test_general.c | 2 +- test/addons-napi/test_number/test_number.c | 4 +- test/addons-napi/test_object/test_object.c | 4 +- .../test_properties/test_properties.c | 4 +- .../test_reference/test_reference.c | 6 +- test/addons-napi/test_string/test_string.c | 4 +- 18 files changed, 197 insertions(+), 88 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index b9c45088ba415e..e8508e341f4761 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1372,12 +1372,81 @@ JavaScript DataView Objects are described in [Section 24.3][] of the ECMAScript Language Specification. ### Functions to convert from C types to N-API -#### *napi_create_number* +#### *napi_create_int32* +```C +napi_status napi_create_int32(napi_env env, int32_t value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Integer value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C `int32_t` type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. + +#### *napi_create_uint32* + +```C +napi_status napi_create_uint32(napi_env env, uint32_t value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Unsigned integer value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C `uint32_t` type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. + +#### *napi_create_int64* + +```C +napi_status napi_create_int64(napi_env env, int64_t value, napi_value* result) +``` + +- `[in] env`: The environment that the API is invoked under. +- `[in] value`: Integer value to be represented in JavaScript. +- `[out] result`: A `napi_value` representing a JavaScript Number. + +Returns `napi_ok` if the API succeeded. + +This API is used to convert from the C `int64_t` type to the JavaScript +Number type. + +The JavaScript Number type is described in +[Section 6.1.6](https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type) +of the ECMAScript Language Specification. Note the complete range of `int64_t` +cannot be represented with full precision in JavaScript. Integer values +outside the range of +[`Number.MIN_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.min_safe_integer) +-(2^53 - 1) - +[`Number.MAX_SAFE_INTEGER`](https://tc39.github.io/ecma262/#sec-number.max_safe_integer) +(2^53 - 1) will lose precision. + +#### *napi_create_double* + ```C -napi_status napi_create_number(napi_env env, double value, napi_value* result) +napi_status napi_create_double(napi_env env, double value, napi_value* result) ``` - `[in] env`: The environment that the API is invoked under. @@ -1386,7 +1455,7 @@ napi_status napi_create_number(napi_env env, double value, napi_value* result) Returns `napi_ok` if the API succeeded. -This API is used to convert from the C double type to the JavaScript +This API is used to convert from the C `double` type to the JavaScript Number type. The JavaScript Number type is described in @@ -2170,7 +2239,7 @@ status = napi_create_object(env, &obj); if (status != napi_ok) return status; // Create a napi_value for 123 -status = napi_create_number(env, 123, &value); +status = napi_create_int32(env, 123, &value); if (status != napi_ok) return status; // obj.myProp = 123 @@ -2244,9 +2313,9 @@ if (status != napi_ok) return status; // Create napi_values for 123 and 456 napi_value fooValue, barValue; -status = napi_create_number(env, 123, &fooValue); +status = napi_create_int32(env, 123, &fooValue); if (status != napi_ok) return status; -status = napi_create_number(env, 456, &barValue); +status = napi_create_int32(env, 456, &barValue); if (status != napi_ok) return status; // Set the properties @@ -2707,7 +2776,7 @@ status = napi_get_named_property(env, global, "AddTwo", &add_two); if (status != napi_ok) return; // const arg = 1337 -status = napi_create_number(env, 1337, &arg); +status = napi_create_int32(env, 1337, &arg); if (status != napi_ok) return; napi_value* argv = &arg; diff --git a/src/node_api.cc b/src/node_api.cc index 6e9f9bbc07e176..32acced231d68c 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -818,9 +818,7 @@ const char* error_messages[] = {nullptr, "The async work item was cancelled", "napi_escape_handle already called on scope"}; -static napi_status napi_clear_last_error(napi_env env) { - CHECK_ENV(env); - +static inline napi_status napi_clear_last_error(napi_env env) { env->last_error.error_code = napi_ok; // TODO(boingoing): Should this be a callback? @@ -829,13 +827,13 @@ static napi_status napi_clear_last_error(napi_env env) { return napi_ok; } -static napi_status napi_set_last_error(napi_env env, napi_status error_code, - uint32_t engine_error_code, - void* engine_reserved) { +static inline +napi_status napi_set_last_error(napi_env env, napi_status error_code, + uint32_t engine_error_code, + void* engine_reserved) { env->last_error.error_code = error_code; env->last_error.engine_error_code = engine_error_code; env->last_error.engine_reserved = engine_reserved; - return error_code; } @@ -1436,42 +1434,42 @@ napi_status napi_get_prototype(napi_env env, } napi_status napi_create_object(napi_env env, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Object::New(env->isolate)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_array(napi_env env, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Array::New(env->isolate)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_array_with_length(napi_env env, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Array::New(env->isolate, length)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_string_latin1(napi_env env, const char* str, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); auto isolate = env->isolate; @@ -1483,28 +1481,28 @@ napi_status napi_create_string_latin1(napi_env env, CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure); *result = v8impl::JsValueFromV8LocalValue(str_maybe.ToLocalChecked()); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_string_utf8(napi_env env, const char* str, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); v8::Local s; CHECK_NEW_FROM_UTF8_LEN(env, s, str, length); *result = v8impl::JsValueFromV8LocalValue(s); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_string_utf16(napi_env env, const char16_t* str, size_t length, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); auto isolate = env->isolate; @@ -1516,19 +1514,55 @@ napi_status napi_create_string_utf16(napi_env env, CHECK_MAYBE_EMPTY(env, str_maybe, napi_generic_failure); *result = v8impl::JsValueFromV8LocalValue(str_maybe.ToLocalChecked()); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } -napi_status napi_create_number(napi_env env, +napi_status napi_create_double(napi_env env, double value, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); *result = v8impl::JsValueFromV8LocalValue( v8::Number::New(env->isolate, value)); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); +} + +napi_status napi_create_int32(napi_env env, + int32_t value, + napi_value* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + + *result = v8impl::JsValueFromV8LocalValue( + v8::Integer::New(env->isolate, value)); + + return napi_clear_last_error(env); +} + +napi_status napi_create_uint32(napi_env env, + uint32_t value, + napi_value* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + + *result = v8impl::JsValueFromV8LocalValue( + v8::Integer::NewFromUnsigned(env->isolate, value)); + + return napi_clear_last_error(env); +} + +napi_status napi_create_int64(napi_env env, + int64_t value, + napi_value* result) { + CHECK_ENV(env); + CHECK_ARG(env, result); + + *result = v8impl::JsValueFromV8LocalValue( + v8::Number::New(env->isolate, static_cast(value))); + + return napi_clear_last_error(env); } napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) { @@ -1549,7 +1583,7 @@ napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) { napi_status napi_create_symbol(napi_env env, napi_value description, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, result); v8::Isolate* isolate = env->isolate; @@ -1564,7 +1598,7 @@ napi_status napi_create_symbol(napi_env env, v8::Symbol::New(isolate, desc.As())); } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } static napi_status set_error_code(napi_env env, @@ -1624,7 +1658,7 @@ napi_status napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1638,14 +1672,14 @@ napi_status napi_create_error(napi_env env, *result = v8impl::JsValueFromV8LocalValue(error_obj); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_type_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1659,14 +1693,14 @@ napi_status napi_create_type_error(napi_env env, *result = v8impl::JsValueFromV8LocalValue(error_obj); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_create_range_error(napi_env env, napi_value code, napi_value msg, napi_value* result) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, msg); CHECK_ARG(env, result); @@ -1680,7 +1714,7 @@ napi_status napi_create_range_error(napi_env env, *result = v8impl::JsValueFromV8LocalValue(error_obj); - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_typeof(napi_env env, @@ -1951,14 +1985,13 @@ napi_status napi_get_value_int32(napi_env env, if (val->IsInt32()) { *result = val.As()->Value(); - return napi_clear_last_error(env); - } - - RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); + } else { + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); - v8::Isolate* isolate = env->isolate; - v8::Local context = isolate->GetCurrentContext(); - *result = val->Int32Value(context).FromJust(); + // Empty context: https://github.com/nodejs/node/issues/14379 + v8::Local context; + *result = val->Int32Value(context).FromJust(); + } return napi_clear_last_error(env); } @@ -1976,14 +2009,13 @@ napi_status napi_get_value_uint32(napi_env env, if (val->IsUint32()) { *result = val.As()->Value(); - return napi_clear_last_error(env); - } - - RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); + } else { + RETURN_STATUS_IF_FALSE(env, val->IsNumber(), napi_number_expected); - v8::Isolate* isolate = env->isolate; - v8::Local context = isolate->GetCurrentContext(); - *result = val->Uint32Value(context).FromJust(); + // Empty context: https://github.com/nodejs/node/issues/14379 + v8::Local context; + *result = val->Uint32Value(context).FromJust(); + } return napi_clear_last_error(env); } @@ -2013,8 +2045,8 @@ napi_status napi_get_value_int64(napi_env env, if (std::isnan(doubleValue)) { *result = 0; } else { - v8::Isolate* isolate = env->isolate; - v8::Local context = isolate->GetCurrentContext(); + // Empty context: https://github.com/nodejs/node/issues/14379 + v8::Local context; *result = val->IntegerValue(context).FromJust(); } @@ -2793,11 +2825,10 @@ napi_status napi_get_buffer_info(napi_env env, napi_value value, void** data, size_t* length) { - NAPI_PREAMBLE(env); + CHECK_ENV(env); CHECK_ARG(env, value); - v8::Local buffer = - v8impl::V8LocalValueFromJsValue(value).As(); + v8::Local buffer = v8impl::V8LocalValueFromJsValue(value); if (data != nullptr) { *data = node::Buffer::Data(buffer); @@ -2806,7 +2837,7 @@ napi_status napi_get_buffer_info(napi_env env, *length = node::Buffer::Length(buffer); } - return GET_RETURN_STATUS(env); + return napi_clear_last_error(env); } napi_status napi_is_arraybuffer(napi_env env, napi_value value, bool* result) { diff --git a/src/node_api.h b/src/node_api.h index 5a2ba946ec8114..cf4128be210fe7 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -127,9 +127,18 @@ NAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value* result); NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env, size_t length, napi_value* result); -NAPI_EXTERN napi_status napi_create_number(napi_env env, +NAPI_EXTERN napi_status napi_create_double(napi_env env, double value, napi_value* result); +NAPI_EXTERN napi_status napi_create_int32(napi_env env, + int32_t value, + napi_value* result); +NAPI_EXTERN napi_status napi_create_uint32(napi_env env, + uint32_t value, + napi_value* result); +NAPI_EXTERN napi_status napi_create_int64(napi_env env, + int64_t value, + napi_value* result); NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, const char* str, size_t length, diff --git a/test/addons-napi/2_function_arguments/binding.c b/test/addons-napi/2_function_arguments/binding.c index a5ccac7b61c458..92f89fd2ffa199 100644 --- a/test/addons-napi/2_function_arguments/binding.c +++ b/test/addons-napi/2_function_arguments/binding.c @@ -24,7 +24,7 @@ napi_value Add(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_double(env, args[1], &value1)); napi_value sum; - NAPI_CALL(env, napi_create_number(env, value0 + value1, &sum)); + NAPI_CALL(env, napi_create_double(env, value0 + value1, &sum)); return sum; } diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index c2557facc06382..56b00ddae49a32 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -86,7 +86,7 @@ napi_value MyObject::GetValue(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_unwrap(env, _this, reinterpret_cast(&obj))); napi_value num; - NAPI_CALL(env, napi_create_number(env, obj->value_, &num)); + NAPI_CALL(env, napi_create_double(env, obj->value_, &num)); return num; } @@ -116,7 +116,7 @@ napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) { obj->value_ += 1; napi_value num; - NAPI_CALL(env, napi_create_number(env, obj->value_, &num)); + NAPI_CALL(env, napi_create_double(env, obj->value_, &num)); return num; } @@ -140,7 +140,7 @@ napi_value MyObject::Multiply(napi_env env, napi_callback_info info) { const int kArgCount = 1; napi_value argv[kArgCount]; - NAPI_CALL(env, napi_create_number(env, obj->value_ * multiple, argv)); + NAPI_CALL(env, napi_create_double(env, obj->value_ * multiple, argv)); napi_value instance; NAPI_CALL(env, napi_new_instance(env, cons, kArgCount, argv, &instance)); diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index d6b374d7bb7ff8..4a2b284439ddb9 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -45,7 +45,7 @@ napi_value MyObject::New(napi_env env, napi_callback_info info) { if (valuetype == napi_undefined) { obj->counter_ = 0; } else { - NAPI_CALL(env, napi_get_value_double(env, args[0], &obj->counter_)); + NAPI_CALL(env, napi_get_value_uint32(env, args[0], &obj->counter_)); } obj->env_ = env; @@ -88,7 +88,7 @@ napi_value MyObject::PlusOne(napi_env env, napi_callback_info info) { obj->counter_ += 1; napi_value num; - NAPI_CALL(env, napi_create_number(env, obj->counter_, &num)); + NAPI_CALL(env, napi_create_uint32(env, obj->counter_, &num)); return num; } diff --git a/test/addons-napi/7_factory_wrap/myobject.h b/test/addons-napi/7_factory_wrap/myobject.h index c0b8522c609c4c..28ca94d16e30dd 100644 --- a/test/addons-napi/7_factory_wrap/myobject.h +++ b/test/addons-napi/7_factory_wrap/myobject.h @@ -18,7 +18,7 @@ class MyObject { static napi_ref constructor; static napi_value New(napi_env env, napi_callback_info info); static napi_value PlusOne(napi_env env, napi_callback_info info); - double counter_; + uint32_t counter_; napi_env env_; napi_ref wrapper_; }; diff --git a/test/addons-napi/8_passing_wrapped/binding.cc b/test/addons-napi/8_passing_wrapped/binding.cc index 581638bdfa2b07..c22ac6442f6c4e 100644 --- a/test/addons-napi/8_passing_wrapped/binding.cc +++ b/test/addons-napi/8_passing_wrapped/binding.cc @@ -24,7 +24,7 @@ napi_value Add(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_unwrap(env, args[1], reinterpret_cast(&obj2))); napi_value sum; - NAPI_CALL(env, napi_create_number(env, obj1->Val() + obj2->Val(), &sum)); + NAPI_CALL(env, napi_create_double(env, obj1->Val() + obj2->Val(), &sum)); return sum; } diff --git a/test/addons-napi/test_async/test_async.cc b/test/addons-napi/test_async/test_async.cc index f257b268b93159..ca76fa2d33b132 100644 --- a/test/addons-napi/test_async/test_async.cc +++ b/test/addons-napi/test_async/test_async.cc @@ -52,7 +52,7 @@ void Complete(napi_env env, napi_status status, void* data) { napi_value argv[2]; NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &argv[0])); - NAPI_CALL_RETURN_VOID(env, napi_create_number(env, c->_output, &argv[1])); + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, c->_output, &argv[1])); napi_value callback; NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, c->_callback, &callback)); diff --git a/test/addons-napi/test_buffer/test_buffer.c b/test/addons-napi/test_buffer/test_buffer.c index 880149e1ff8561..0e12bedfb5602f 100644 --- a/test/addons-napi/test_buffer/test_buffer.c +++ b/test/addons-napi/test_buffer/test_buffer.c @@ -55,7 +55,7 @@ napi_value newExternalBuffer(napi_env env, napi_callback_info info) { napi_value getDeleterCallCount(napi_env env, napi_callback_info info) { napi_value callCount; - NAPI_CALL(env, napi_create_number(env, deleterCallCount, &callCount)); + NAPI_CALL(env, napi_create_int32(env, deleterCallCount, &callCount)); return callCount; } diff --git a/test/addons-napi/test_constructor/test_constructor.c b/test/addons-napi/test_constructor/test_constructor.c index 220d564753ca10..a991dab8533bf1 100644 --- a/test/addons-napi/test_constructor/test_constructor.c +++ b/test/addons-napi/test_constructor/test_constructor.c @@ -12,7 +12,7 @@ napi_value GetValue(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); napi_value number; - NAPI_CALL(env, napi_create_number(env, value_, &number)); + NAPI_CALL(env, napi_create_double(env, value_, &number)); return number; } @@ -53,7 +53,7 @@ napi_value GetStaticValue(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); napi_value number; - NAPI_CALL(env, napi_create_number(env, static_value_, &number)); + NAPI_CALL(env, napi_create_double(env, static_value_, &number)); return number; } @@ -61,7 +61,7 @@ napi_value GetStaticValue(napi_env env, napi_callback_info info) { void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; - NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); + NAPI_CALL_RETURN_VOID(env, napi_create_double(env, value_, &number)); napi_property_descriptor properties[] = { { "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 }, diff --git a/test/addons-napi/test_conversions/test_conversions.c b/test/addons-napi/test_conversions/test_conversions.c index 637cff43b82c4d..a8d526c763c9a2 100644 --- a/test/addons-napi/test_conversions/test_conversions.c +++ b/test/addons-napi/test_conversions/test_conversions.c @@ -24,7 +24,7 @@ napi_value AsInt32(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_int32(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, value, &output)); + NAPI_CALL(env, napi_create_int32(env, value, &output)); return output; } @@ -38,7 +38,7 @@ napi_value AsUInt32(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_uint32(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, value, &output)); + NAPI_CALL(env, napi_create_uint32(env, value, &output)); return output; } @@ -52,7 +52,7 @@ napi_value AsInt64(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_int64(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, (double)value, &output)); + NAPI_CALL(env, napi_create_int64(env, (double)value, &output)); return output; } @@ -66,7 +66,7 @@ napi_value AsDouble(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_double(env, args[0], &value)); napi_value output; - NAPI_CALL(env, napi_create_number(env, value, &output)); + NAPI_CALL(env, napi_create_double(env, value, &output)); return output; } diff --git a/test/addons-napi/test_general/test_general.c b/test/addons-napi/test_general/test_general.c index 2d2d13a3624e6e..858611939474b2 100644 --- a/test/addons-napi/test_general/test_general.c +++ b/test/addons-napi/test_general/test_general.c @@ -29,7 +29,7 @@ napi_value testGetVersion(napi_env env, napi_callback_info info) { uint32_t version; napi_value result; NAPI_CALL(env, napi_get_version(env, &version)); - NAPI_CALL(env, napi_create_number(env, version, &result)); + NAPI_CALL(env, napi_create_uint32(env, version, &result)); return result; } diff --git a/test/addons-napi/test_number/test_number.c b/test/addons-napi/test_number/test_number.c index 1054741d2de369..6b28afe18ff9e8 100644 --- a/test/addons-napi/test_number/test_number.c +++ b/test/addons-napi/test_number/test_number.c @@ -18,7 +18,7 @@ napi_value Test(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_double(env, args[0], &input)); napi_value output; - NAPI_CALL(env, napi_create_number(env, input, &output)); + NAPI_CALL(env, napi_create_double(env, input, &output)); return output; } @@ -40,7 +40,7 @@ napi_value TestInt32Truncation(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_int32(env, args[0], &input)); napi_value output; - NAPI_CALL(env, napi_create_number(env, input, &output)); + NAPI_CALL(env, napi_create_int32(env, input, &output)); return output; } diff --git a/test/addons-napi/test_object/test_object.c b/test/addons-napi/test_object/test_object.c index 663e561a35b197..88ac79c170f5ea 100644 --- a/test/addons-napi/test_object/test_object.c +++ b/test/addons-napi/test_object/test_object.c @@ -144,7 +144,7 @@ napi_value New(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_create_object(env, &ret)); napi_value num; - NAPI_CALL(env, napi_create_number(env, 987654321, &num)); + NAPI_CALL(env, napi_create_int32(env, 987654321, &num)); NAPI_CALL(env, napi_set_named_property(env, ret, "test_number", num)); @@ -187,7 +187,7 @@ napi_value Inflate(napi_env env, napi_callback_info info) { double double_val; NAPI_CALL(env, napi_get_value_double(env, value, &double_val)); - NAPI_CALL(env, napi_create_number(env, double_val + 1, &value)); + NAPI_CALL(env, napi_create_double(env, double_val + 1, &value)); NAPI_CALL(env, napi_set_property(env, obj, property_str, value)); } diff --git a/test/addons-napi/test_properties/test_properties.c b/test/addons-napi/test_properties/test_properties.c index 3053fda4864e8f..3f4f0a6bcbba96 100644 --- a/test/addons-napi/test_properties/test_properties.c +++ b/test/addons-napi/test_properties/test_properties.c @@ -10,7 +10,7 @@ napi_value GetValue(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, argc == 0, "Wrong number of arguments"); napi_value number; - NAPI_CALL(env, napi_create_number(env, value_, &number)); + NAPI_CALL(env, napi_create_double(env, value_, &number)); return number; } @@ -61,7 +61,7 @@ napi_value HasNamedProperty(napi_env env, napi_callback_info info) { void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_value number; - NAPI_CALL_RETURN_VOID(env, napi_create_number(env, value_, &number)); + NAPI_CALL_RETURN_VOID(env, napi_create_double(env, value_, &number)); napi_value name_value; NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, diff --git a/test/addons-napi/test_reference/test_reference.c b/test/addons-napi/test_reference/test_reference.c index 0d3925ee5d04bc..b16e10d70361b5 100644 --- a/test/addons-napi/test_reference/test_reference.c +++ b/test/addons-napi/test_reference/test_reference.c @@ -8,7 +8,7 @@ static napi_ref test_reference = NULL; napi_value GetFinalizeCount(napi_env env, napi_callback_info info) { napi_value result; - NAPI_CALL(env, napi_create_number(env, finalize_count, &result)); + NAPI_CALL(env, napi_create_int32(env, finalize_count, &result)); return result; } @@ -107,7 +107,7 @@ napi_value IncrementRefcount(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_reference_ref(env, test_reference, &refcount)); napi_value result; - NAPI_CALL(env, napi_create_number(env, refcount, &result)); + NAPI_CALL(env, napi_create_uint32(env, refcount, &result)); return result; } @@ -119,7 +119,7 @@ napi_value DecrementRefcount(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_reference_unref(env, test_reference, &refcount)); napi_value result; - NAPI_CALL(env, napi_create_number(env, refcount, &result)); + NAPI_CALL(env, napi_create_uint32(env, refcount, &result)); return result; } diff --git a/test/addons-napi/test_string/test_string.c b/test/addons-napi/test_string/test_string.c index 5cd6d413a679fb..ec80e2c7b57fa8 100644 --- a/test/addons-napi/test_string/test_string.c +++ b/test/addons-napi/test_string/test_string.c @@ -174,7 +174,7 @@ napi_value Utf16Length(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_string_utf16(env, args[0], NULL, 0, &length)); napi_value output; - NAPI_CALL(env, napi_create_number(env, (double)length, &output)); + NAPI_CALL(env, napi_create_uint32(env, (uint32_t)length, &output)); return output; } @@ -196,7 +196,7 @@ napi_value Utf8Length(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], NULL, 0, &length)); napi_value output; - NAPI_CALL(env, napi_create_number(env, (double)length, &output)); + NAPI_CALL(env, napi_create_uint32(env, (uint32_t)length, &output)); return output; }