1#include <inttypes.h> 2#include <js_native_api.h> 3#include <limits.h> 4#include <stdio.h> 5#include "../common.h" 6#include "../entry_point.h" 7 8static napi_value IsLossless(napi_env env, napi_callback_info info) { 9 size_t argc = 2; 10 napi_value args[2]; 11 NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 12 13 bool is_signed; 14 NODE_API_CALL(env, napi_get_value_bool(env, args[1], &is_signed)); 15 16 bool lossless; 17 18 if (is_signed) { 19 int64_t input; 20 NODE_API_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless)); 21 } else { 22 uint64_t input; 23 NODE_API_CALL(env, napi_get_value_bigint_uint64(env, args[0], &input, &lossless)); 24 } 25 26 napi_value output; 27 NODE_API_CALL(env, napi_get_boolean(env, lossless, &output)); 28 29 return output; 30} 31 32static napi_value TestInt64(napi_env env, napi_callback_info info) { 33 size_t argc = 1; 34 napi_value args[1]; 35 NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 36 37 NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); 38 39 napi_valuetype valuetype0; 40 NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); 41 42 NODE_API_ASSERT(env, valuetype0 == napi_bigint, 43 "Wrong type of arguments. Expects a bigint as first argument."); 44 45 int64_t input; 46 bool lossless; 47 NODE_API_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless)); 48 49 napi_value output; 50 NODE_API_CALL(env, napi_create_bigint_int64(env, input, &output)); 51 52 return output; 53} 54 55static napi_value TestUint64(napi_env env, napi_callback_info info) { 56 size_t argc = 1; 57 napi_value args[1]; 58 NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 59 60 NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); 61 62 napi_valuetype valuetype0; 63 NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); 64 65 NODE_API_ASSERT(env, valuetype0 == napi_bigint, 66 "Wrong type of arguments. Expects a bigint as first argument."); 67 68 uint64_t input; 69 bool lossless; 70 NODE_API_CALL(env, napi_get_value_bigint_uint64( 71 env, args[0], &input, &lossless)); 72 73 napi_value output; 74 NODE_API_CALL(env, napi_create_bigint_uint64(env, input, &output)); 75 76 return output; 77} 78 79static napi_value TestWords(napi_env env, napi_callback_info info) { 80 size_t argc = 1; 81 napi_value args[1]; 82 NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 83 84 NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); 85 86 napi_valuetype valuetype0; 87 NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); 88 89 NODE_API_ASSERT(env, valuetype0 == napi_bigint, 90 "Wrong type of arguments. Expects a bigint as first argument."); 91 92 size_t expected_word_count; 93 NODE_API_CALL(env, napi_get_value_bigint_words( 94 env, args[0], NULL, &expected_word_count, NULL)); 95 96 int sign_bit; 97 size_t word_count = 10; 98 uint64_t words[10]; 99 100 NODE_API_CALL(env, napi_get_value_bigint_words( 101 env, args[0], &sign_bit, &word_count, words)); 102 103 NODE_API_ASSERT(env, word_count == expected_word_count, 104 "word counts do not match"); 105 106 napi_value output; 107 NODE_API_CALL(env, napi_create_bigint_words( 108 env, sign_bit, word_count, words, &output)); 109 110 return output; 111} 112 113// throws RangeError 114static napi_value CreateTooBigBigInt(napi_env env, napi_callback_info info) { 115 int sign_bit = 0; 116 size_t word_count = SIZE_MAX; 117 uint64_t words[10] = {0}; 118 119 napi_value output; 120 121 NODE_API_CALL(env, napi_create_bigint_words( 122 env, sign_bit, word_count, words, &output)); 123 124 return output; 125} 126 127// Test that we correctly forward exceptions from the engine. 128static napi_value MakeBigIntWordsThrow(napi_env env, napi_callback_info info) { 129 uint64_t words[10] = {0}; 130 napi_value output; 131 132 napi_status status = napi_create_bigint_words(env, 133 0, 134 INT_MAX, 135 words, 136 &output); 137 if (status != napi_pending_exception) 138 napi_throw_error(env, NULL, "Expected status `napi_pending_exception`"); 139 140 return NULL; 141} 142 143EXTERN_C_START 144napi_value Init(napi_env env, napi_value exports) { 145 napi_property_descriptor descriptors[] = { 146 DECLARE_NODE_API_PROPERTY("IsLossless", IsLossless), 147 DECLARE_NODE_API_PROPERTY("TestInt64", TestInt64), 148 DECLARE_NODE_API_PROPERTY("TestUint64", TestUint64), 149 DECLARE_NODE_API_PROPERTY("TestWords", TestWords), 150 DECLARE_NODE_API_PROPERTY("CreateTooBigBigInt", CreateTooBigBigInt), 151 DECLARE_NODE_API_PROPERTY("MakeBigIntWordsThrow", MakeBigIntWordsThrow), 152 }; 153 154 NODE_API_CALL(env, napi_define_properties( 155 env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); 156 157 return exports; 158} 159EXTERN_C_END 160