Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGus Caplan <me@gus.host>2018-06-09 06:41:20 +0300
committerMichaƫl Zasso <targos@protonmail.com>2018-07-14 13:03:01 +0300
commit3096ee5a4b6e1ee56d1b4c1776975f26381e8a38 (patch)
treea5e5e5fb2e63ae04886710e99cdb1ef6c3de96eb /test/addons-napi/test_bigint
parent27d17d460013e256b6993707bbb6764391cba93b (diff)
napi: add bigint support
PR-URL: https://github.com/nodejs/node/pull/21226 Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
Diffstat (limited to 'test/addons-napi/test_bigint')
-rw-r--r--test/addons-napi/test_bigint/binding.gyp8
-rw-r--r--test/addons-napi/test_bigint/test.js45
-rw-r--r--test/addons-napi/test_bigint/test_bigint.c142
3 files changed, 195 insertions, 0 deletions
diff --git a/test/addons-napi/test_bigint/binding.gyp b/test/addons-napi/test_bigint/binding.gyp
new file mode 100644
index 00000000000..1b9f75bab4f
--- /dev/null
+++ b/test/addons-napi/test_bigint/binding.gyp
@@ -0,0 +1,8 @@
+{
+ "targets": [
+ {
+ "target_name": "test_bigint",
+ "sources": [ "test_bigint.c" ]
+ }
+ ]
+}
diff --git a/test/addons-napi/test_bigint/test.js b/test/addons-napi/test_bigint/test.js
new file mode 100644
index 00000000000..b92c8104593
--- /dev/null
+++ b/test/addons-napi/test_bigint/test.js
@@ -0,0 +1,45 @@
+'use strict';
+const common = require('../../common');
+const assert = require('assert');
+const {
+ IsLossless,
+ TestInt64,
+ TestUint64,
+ TestWords,
+ CreateTooBigBigInt,
+} = require(`./build/${common.buildType}/test_bigint`);
+
+[
+ 0n,
+ -0n,
+ 1n,
+ -1n,
+ 100n,
+ 2121n,
+ -1233n,
+ 986583n,
+ -976675n,
+ 98765432213456789876546896323445679887645323232436587988766545658n,
+ -4350987086545760976737453646576078997096876957864353245245769809n,
+].forEach((num) => {
+ if (num > -(2n ** 63n) && num < 2n ** 63n) {
+ assert.strictEqual(TestInt64(num), num);
+ assert.strictEqual(IsLossless(num, true), true);
+ } else {
+ assert.strictEqual(IsLossless(num, true), false);
+ }
+
+ if (num >= 0 && num < 2n ** 64n) {
+ assert.strictEqual(TestUint64(num), num);
+ assert.strictEqual(IsLossless(num, false), true);
+ } else {
+ assert.strictEqual(IsLossless(num, false), false);
+ }
+
+ assert.strictEqual(num, TestWords(num));
+});
+
+assert.throws(CreateTooBigBigInt, {
+ name: 'RangeError',
+ message: 'Maximum BigInt size exceeded',
+});
diff --git a/test/addons-napi/test_bigint/test_bigint.c b/test/addons-napi/test_bigint/test_bigint.c
new file mode 100644
index 00000000000..e3516628e88
--- /dev/null
+++ b/test/addons-napi/test_bigint/test_bigint.c
@@ -0,0 +1,142 @@
+#define NAPI_EXPERIMENTAL
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <node_api.h>
+#include "../common.h"
+
+static napi_value IsLossless(napi_env env, napi_callback_info info) {
+ size_t argc = 2;
+ napi_value args[2];
+ NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, NULL, NULL));
+
+ bool is_signed;
+ NAPI_CALL(env, napi_get_value_bool(env, args[1], &is_signed));
+
+ bool lossless;
+
+ if (is_signed) {
+ int64_t input;
+ NAPI_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless));
+ } else {
+ uint64_t input;
+ NAPI_CALL(env, napi_get_value_bigint_uint64(env, args[0], &input, &lossless));
+ }
+
+ napi_value output;
+ NAPI_CALL(env, napi_get_boolean(env, lossless, &output));
+
+ return output;
+}
+
+static napi_value TestInt64(napi_env env, napi_callback_info info) {
+ size_t argc = 1;
+ napi_value args[1];
+ NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
+
+ NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
+
+ napi_valuetype valuetype0;
+ NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
+
+ NAPI_ASSERT(env, valuetype0 == napi_bigint,
+ "Wrong type of arguments. Expects a bigint as first argument.");
+
+ int64_t input;
+ bool lossless;
+ NAPI_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless));
+
+ napi_value output;
+ NAPI_CALL(env, napi_create_bigint_int64(env, input, &output));
+
+ return output;
+}
+
+static napi_value TestUint64(napi_env env, napi_callback_info info) {
+ size_t argc = 1;
+ napi_value args[1];
+ NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
+
+ NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
+
+ napi_valuetype valuetype0;
+ NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
+
+ NAPI_ASSERT(env, valuetype0 == napi_bigint,
+ "Wrong type of arguments. Expects a bigint as first argument.");
+
+ uint64_t input;
+ bool lossless;
+ NAPI_CALL(env, napi_get_value_bigint_uint64(
+ env, args[0], &input, &lossless));
+
+ napi_value output;
+ NAPI_CALL(env, napi_create_bigint_uint64(env, input, &output));
+
+ return output;
+}
+
+static napi_value TestWords(napi_env env, napi_callback_info info) {
+ size_t argc = 1;
+ napi_value args[1];
+ NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
+
+ NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
+
+ napi_valuetype valuetype0;
+ NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
+
+ NAPI_ASSERT(env, valuetype0 == napi_bigint,
+ "Wrong type of arguments. Expects a bigint as first argument.");
+
+ size_t expected_word_count;
+ NAPI_CALL(env, napi_get_value_bigint_words(
+ env, args[0], NULL, &expected_word_count, NULL));
+
+ int sign_bit;
+ size_t word_count = 10;
+ uint64_t words[10];
+
+ NAPI_CALL(env, napi_get_value_bigint_words(
+ env, args[0], &sign_bit, &word_count, &words));
+
+ NAPI_ASSERT(env, word_count == expected_word_count,
+ "word counts do not match");
+
+ napi_value output;
+ NAPI_CALL(env, napi_create_bigint_words(
+ env, sign_bit, word_count, words, &output));
+
+ return output;
+}
+
+// throws RangeError
+static napi_value CreateTooBigBigInt(napi_env env, napi_callback_info info) {
+ int sign_bit = 0;
+ size_t word_count = SIZE_MAX;
+ uint64_t words[10];
+
+ napi_value output;
+
+ NAPI_CALL(env, napi_create_bigint_words(
+ env, sign_bit, word_count, words, &output));
+
+ return output;
+}
+
+static napi_value Init(napi_env env, napi_value exports) {
+ napi_property_descriptor descriptors[] = {
+ DECLARE_NAPI_PROPERTY("IsLossless", IsLossless),
+ DECLARE_NAPI_PROPERTY("TestInt64", TestInt64),
+ DECLARE_NAPI_PROPERTY("TestUint64", TestUint64),
+ DECLARE_NAPI_PROPERTY("TestWords", TestWords),
+ DECLARE_NAPI_PROPERTY("CreateTooBigBigInt", CreateTooBigBigInt),
+ };
+
+ NAPI_CALL(env, napi_define_properties(
+ env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
+
+ return exports;
+}
+
+NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)