summaryrefslogtreecommitdiff
path: root/test/js-native-api/test_bigint/test_bigint.c
blob: 4befc171baa5dab028b260cdec2bc50e8d4c20ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#define NAPI_EXPERIMENTAL

#include <inttypes.h>
#include <stdio.h>
#include <js_native_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;
}

EXTERN_C_START
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;
}
EXTERN_C_END