Skip to content

Instantly share code, notes, and snippets.

@devsnek
Created April 17, 2018 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devsnek/34c3243a650ef5f4419a16961bca5cd8 to your computer and use it in GitHub Desktop.
Save devsnek/34c3243a650ef5f4419a16961bca5cd8 to your computer and use it in GitHub Desktop.
diff --git a/napi-inl.h b/napi-inl.h
index 884f8cb..920f055 100644
--- a/napi-inl.h
+++ b/napi-inl.h
@@ -506,6 +506,34 @@ inline double Number::DoubleValue() const {
}
////////////////////////////////////////////////////////////////////////////////
+// BigInt class
+////////////////////////////////////////////////////////////////////////////////
+
+inline BigInt BigInt::New(napi_env env, int64_t val) {
+ napi_value value;
+ napi_status status = napi_create_bigint(env, val, &value);
+ NAPI_THROW_IF_FAILED(env, status, BigInt());
+ return BigInt(env, value);
+}
+
+inline BigInt::BigInt() : Value() {
+}
+
+inline BigInt::BigInt(napi_env env, napi_value value) : Value(env, value) {
+}
+
+inline BigInt::operator int64_t() const {
+ return Int64Value();
+}
+
+inline int64_t BigInt::Int64Value() const {
+ int64_t result;
+ napi_status status = napi_get_value_int64(_env, _value, &result);
+ NAPI_THROW_IF_FAILED(_env, status, 0);
+ return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
// Name class
////////////////////////////////////////////////////////////////////////////////
diff --git a/napi.h b/napi.h
index a2d1f20..9c9d46f 100644
--- a/napi.h
+++ b/napi.h
@@ -52,6 +52,7 @@ namespace Napi {
class Value;
class Boolean;
class Number;
+ class BigInt;
class String;
class Object;
class Array;
@@ -64,14 +65,16 @@ namespace Napi {
class TypedArray;
template <typename T> class TypedArrayOf;
- typedef TypedArrayOf<int8_t> Int8Array; ///< Typed-array of signed 8-bit integers
- typedef TypedArrayOf<uint8_t> Uint8Array; ///< Typed-array of unsigned 8-bit integers
- typedef TypedArrayOf<int16_t> Int16Array; ///< Typed-array of signed 16-bit integers
- typedef TypedArrayOf<uint16_t> Uint16Array; ///< Typed-array of unsigned 16-bit integers
- typedef TypedArrayOf<int32_t> Int32Array; ///< Typed-array of signed 32-bit integers
- typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
- typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
- typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
+ typedef TypedArrayOf<int8_t> Int8Array; ///< Typed-array of signed 8-bit integers
+ typedef TypedArrayOf<uint8_t> Uint8Array; ///< Typed-array of unsigned 8-bit integers
+ typedef TypedArrayOf<int16_t> Int16Array; ///< Typed-array of signed 16-bit integers
+ typedef TypedArrayOf<uint16_t> Uint16Array; ///< Typed-array of unsigned 16-bit integers
+ typedef TypedArrayOf<int32_t> Int32Array; ///< Typed-array of signed 32-bit integers
+ typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
+ typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
+ typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
+ typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed-array of 64-bit signed integers.
+ typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed-array of 64-bit unsigned integers.
/// Defines the signature of a N-API C++ module's registration callback (init) function.
typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
@@ -169,6 +172,7 @@ namespace Napi {
bool IsNull() const; ///< Tests if a value is a null JavaScript value.
bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
bool IsNumber() const; ///< Tests if a value is a JavaScript number.
+ bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
bool IsString() const; ///< Tests if a value is a JavaScript string.
bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
bool IsArray() const; ///< Tests if a value is a JavaScript array.
@@ -240,6 +244,21 @@ namespace Napi {
double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
};
+ class BigInt : public Value {
+ public:
+ static BigInt New(
+ napi_env env, ///< N-API environment
+ int64_t value ///< Number value
+ );
+
+ BigInt(); ///< Creats a new _empty_ BigInt instance.
+ BigInt(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
+
+ operator int64_t() const; ///< Converts a BigInt value to a 64-bit signed integer value.
+
+ int64_t Int64Value() const; ///< Converts a BigInt value to a 64-bit signed integer value.
+ };
+
/// A JavaScript string or symbol value (that can be used as a property name).
class Name : public Value {
public:
diff --git a/src/node_api.cc b/src/node_api.cc
index 20fada9..6cfdd52 100644
--- a/src/node_api.cc
+++ b/src/node_api.cc
@@ -1697,6 +1697,18 @@ napi_status napi_create_int64(napi_env env,
return napi_clear_last_error(env);
}
+napi_status napi_create_bigint(napi_env env,
+ int64_t value,
+ napi_value* result) {
+ CHECK_ENV(env);
+ CHECK_ARG(env, result);
+
+ *result = v8impl::JsValueFromV8LocalValue(
+ v8::BigInt::New(env->isolate, value));
+
+ return napi_clear_last_error(env);
+}
+
napi_status napi_get_boolean(napi_env env, bool value, napi_value* result) {
CHECK_ENV(env);
CHECK_ARG(env, result);
@@ -1862,6 +1874,8 @@ napi_status napi_typeof(napi_env env,
if (v->IsNumber()) {
*result = napi_number;
+ else if (v->IsBigInt()) {
+ *result = napi_bigint;
} else if (v->IsString()) {
*result = napi_string;
} else if (v->IsFunction()) {
@@ -2162,6 +2176,13 @@ napi_status napi_get_value_int64(napi_env env,
v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);
+ /*
+ if (val->IsBigInt()) {
+ *result = val->Magic();
+ return napi_clear_last_error(env);
+ }
+ */
+
// This is still a fast path very likely to be taken.
if (val->IsInt32()) {
*result = val.As<v8::Int32>()->Value();
@@ -3209,6 +3230,14 @@ napi_status napi_create_typedarray(napi_env env,
CREATE_TYPED_ARRAY(
env, Float64Array, 8, buffer, byte_offset, length, typedArray);
break;
+ case napi_bigint64_array:
+ CREATE_TYPED_ARRAY(
+ env, BigInt64Array, 8, buffer, byte_offset, length, typedArray);
+ break;
+ case napi_biguint64_array:
+ CREATE_TYPED_ARRAY(
+ env, BigUint64Array, 8, buffer, byte_offset, length, typedArray);
+ break;
default:
return napi_set_last_error(env, napi_invalid_arg);
}
@@ -3251,6 +3280,10 @@ napi_status napi_get_typedarray_info(napi_env env,
*type = napi_float32_array;
} else if (value->IsFloat64Array()) {
*type = napi_float64_array;
+ } else if (value->IsBigInt64Array()) {
+ *type = napi_bigint64_array;
+ } else if (alue->IsBigUint64Array()) {
+ *type = napi_biguint64_array;
}
}
diff --git a/src/node_api.h b/src/node_api.h
index a3a07a6..9b68eb3 100644
--- a/src/node_api.h
+++ b/src/node_api.h
@@ -141,6 +141,9 @@ NAPI_EXTERN napi_status napi_create_uint32(napi_env env,
NAPI_EXTERN napi_status napi_create_int64(napi_env env,
int64_t value,
napi_value* result);
+NAPI_EXTERN napi_status napi_create_bigint(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/src/node_api_types.h b/src/node_api_types.h
index 230c1f4..f4be6db 100644
--- a/src/node_api_types.h
+++ b/src/node_api_types.h
@@ -37,6 +37,7 @@ typedef enum {
napi_null,
napi_boolean,
napi_number,
+ napi_bigint,
napi_string,
napi_symbol,
napi_object,
@@ -54,6 +55,8 @@ typedef enum {
napi_uint32_array,
napi_float32_array,
napi_float64_array,
+ napi_bigint64_array,
+ napi_biguint64_array,
} napi_typedarray_type;
typedef enum {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment