File: bigint.cc

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (91 lines) | stat: -rw-r--r-- 2,072 bytes parent folder | download
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
#if (NAPI_VERSION > 5)

#include "napi.h"

#include "test_helper.h"

using namespace Napi;

namespace {

Value IsLossless(const CallbackInfo& info) {
  Env env = info.Env();

  BigInt big = info[0].As<BigInt>();
  bool is_signed = MaybeUnwrap(info[1].ToBoolean()).Value();

  bool lossless;
  if (is_signed) {
    big.Int64Value(&lossless);
  } else {
    big.Uint64Value(&lossless);
  }

  return Boolean::New(env, lossless);
}

Value IsBigInt(const CallbackInfo& info) {
  Env env = info.Env();

  BigInt big = info[0].As<BigInt>();

  return Boolean::New(env, big.IsBigInt());
}

Value TestInt64(const CallbackInfo& info) {
  bool lossless;
  int64_t input = info[0].As<BigInt>().Int64Value(&lossless);

  return BigInt::New(info.Env(), input);
}

Value TestUint64(const CallbackInfo& info) {
  bool lossless;
  uint64_t input = info[0].As<BigInt>().Uint64Value(&lossless);

  return BigInt::New(info.Env(), input);
}

Value TestWords(const CallbackInfo& info) {
  BigInt big = info[0].As<BigInt>();

  size_t expected_word_count = big.WordCount();

  int sign_bit;
  size_t word_count = 10;
  uint64_t words[10] = {0};

  big.ToWords(&sign_bit, &word_count, words);

  if (word_count != expected_word_count) {
    Error::New(info.Env(), "word count did not match")
        .ThrowAsJavaScriptException();
    return BigInt();
  }

  return BigInt::New(info.Env(), sign_bit, word_count, words);
}

Value TestTooBigBigInt(const CallbackInfo& info) {
  int sign_bit = 0;
  size_t word_count = SIZE_MAX;
  uint64_t words[10] = {0};

  return BigInt::New(info.Env(), sign_bit, word_count, words);
}

}  // anonymous namespace

Object InitBigInt(Env env) {
  Object exports = Object::New(env);
  exports["IsLossless"] = Function::New(env, IsLossless);
  exports["IsBigInt"] = Function::New(env, IsBigInt);
  exports["TestInt64"] = Function::New(env, TestInt64);
  exports["TestUint64"] = Function::New(env, TestUint64);
  exports["TestWords"] = Function::New(env, TestWords);
  exports["TestTooBigBigInt"] = Function::New(env, TestTooBigBigInt);

  return exports;
}

#endif