From 78374f72d29d7fec28d6b869f8ba0502317db517 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 25 Sep 2018 18:02:59 +0200 Subject: [PATCH] doc: number documentation PR-URL: https://github.com/nodejs/node-addon-api/pull/356 Reviewed-By: Michael Dawson --- doc/number.md | 147 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 139 insertions(+), 8 deletions(-) diff --git a/doc/number.md b/doc/number.md index d1bee4810..8226909a8 100644 --- a/doc/number.md +++ b/doc/number.md @@ -1,29 +1,160 @@ # Number -A Javascript number value. +`Napi::Number` class is a representation of the JavaScript `Number` object. The +`Napi::Number` class inherits its behavior from `Napi::Value` class +(for more info see [`Napi::Value`](value.md)) + ## Methods ### Constructor +Creates a new _empty_ instance of a `Napi::Number` object. + +```cpp +Napi::Number(); +``` + +Returns a new _empty_ `Napi::Number` object. + +### Contructor + +Creates a new instance of a `Napi::Number` object. + +```cpp +Napi::Number(napi_env env, napi_value value); +``` + + - `[in] env`: The `napi_env` environment in which to construct the `Napi::Nuber` object. + - `[in] value`: The `napi_value` which is a handle for a JavaScript `Number`. + + Returns a non-empty `Napi::Number` object. + + ### New + + Creates a new instance of a `Napi::Number` object. + +```cpp +Napi::Number Napi::Number::New(napi_env env, double value); +``` + - `[in] env`: The `napi_env` environment in which to construct the `Napi::Nuber` object. + - `[in] value`: The `napi_value` which is a handle for a JavaScript `Number`. + +Creates a new instance of a `Napi::Number` object. + +### Int32Value + +Converts a `Napi::Number` value to a `uint32_t` primitive type. + +```cpp +Napi::Number::Int32Value() const; +``` + +Returns the `int32_t` primitive type of the corresponding `Napi::Number` object. + +### Uint32Value + +Converts a `Napi::Number` value to a `uint32_t` primitive type. + +```cpp +Napi::Number::Uint32Value() const; +``` + +Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object. + +### Int64Value + +Converts a `Napi::Number` value to a `int64_t` primitive type. + +```cpp +Napi::Number::Int64Value() const; +``` + +Returns the `int64_t` primitive type of the corresponding `Napi::Number` object. + +### FloatValue + +Converts a `Napi::Number` value to a `float` primitive type. + ```cpp -Napi::Number::New(Napi::Env env, double value); +Napi::Number::FloatValue() const; ``` - - `[in] env`: The `napi_env` Environment - - `[in] value`: The value the Javascript Number will contain + +Returns the `float` primitive type of the corresponding `Napi::Number` object. + +### DoubleValue + +Converts a `Napi::Number` value to a `double` primitive type. ```cpp -Napi::Number::Number(); +Napi::Number::DoubleValue() const; ``` -returns a new empty Javascript Number -You can easily cast a Javascript number to one of: +Returns the `double` primitive type of the corresponding `Napi::Number` object. + +## Operators + +The `Napi::Number` class contains a set of operators to easily cast JavaScript +`Number` object to one of the following primitive types: + - `int32_t` - `uint32_t` - `int64_t` - `float` - `double` -The following shows an example of casting a number to an uint32_t value. +### operator int32_t + +Converts a `Napi::Number` value to a `int32_t` primitive. + +```cpp +Napi::Number::operator int32_t() const; +``` + +Returns the `int32_t` primitive type of the corresponding `Napi::Number` object. + +### operator uint32_t + +Converts a `Napi::Number` value to a `uint32_t` primitive type. + +```cpp +Napi::Number::operator uint32_t() const; +``` + +Returns the `uint32_t` primitive type of the corresponding `Napi::Number` object. + +### operator int64_t + +Converts a `Napi::Number` value to a `int64_t` primitive type. + +```cpp +Napi::Number::operator int64_t() const; +``` + +Returns the `int64_t` primitive type of the corresponding `Napi::Number` object. + +### operator float + +Converts a `Napi::Number` value to a `float` primitive type. + +```cpp +Napi::Number::operator float() const; +``` + +Returns the `float` primitive type of the corresponding `Napi::Number` object. + +### operator double + +Converts a `Napi::Number` value to a `double` primitive type. + +```cpp +Napi::Number::operator double() const; +``` + +Returns the `double` primitive type of the corresponding `Napi::Number` object. + +### Example + +The following shows an example of casting a number to an `uint32_t` value. ```cpp uint32_t operatorVal = Napi::Number::New(Env(), 10.0); // Number to unsigned 32 bit integer