11cb0ef41Sopenharmony_ci#include <inttypes.h> 21cb0ef41Sopenharmony_ci#include <js_native_api.h> 31cb0ef41Sopenharmony_ci#include <limits.h> 41cb0ef41Sopenharmony_ci#include <stdio.h> 51cb0ef41Sopenharmony_ci#include "../common.h" 61cb0ef41Sopenharmony_ci#include "../entry_point.h" 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_cistatic napi_value IsLossless(napi_env env, napi_callback_info info) { 91cb0ef41Sopenharmony_ci size_t argc = 2; 101cb0ef41Sopenharmony_ci napi_value args[2]; 111cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci bool is_signed; 141cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bool(env, args[1], &is_signed)); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci bool lossless; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci if (is_signed) { 191cb0ef41Sopenharmony_ci int64_t input; 201cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless)); 211cb0ef41Sopenharmony_ci } else { 221cb0ef41Sopenharmony_ci uint64_t input; 231cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bigint_uint64(env, args[0], &input, &lossless)); 241cb0ef41Sopenharmony_ci } 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci napi_value output; 271cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_boolean(env, lossless, &output)); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci return output; 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_cistatic napi_value TestInt64(napi_env env, napi_callback_info info) { 331cb0ef41Sopenharmony_ci size_t argc = 1; 341cb0ef41Sopenharmony_ci napi_value args[1]; 351cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci napi_valuetype valuetype0; 401cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, valuetype0 == napi_bigint, 431cb0ef41Sopenharmony_ci "Wrong type of arguments. Expects a bigint as first argument."); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci int64_t input; 461cb0ef41Sopenharmony_ci bool lossless; 471cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bigint_int64(env, args[0], &input, &lossless)); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci napi_value output; 501cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_bigint_int64(env, input, &output)); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci return output; 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cistatic napi_value TestUint64(napi_env env, napi_callback_info info) { 561cb0ef41Sopenharmony_ci size_t argc = 1; 571cb0ef41Sopenharmony_ci napi_value args[1]; 581cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci napi_valuetype valuetype0; 631cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, valuetype0 == napi_bigint, 661cb0ef41Sopenharmony_ci "Wrong type of arguments. Expects a bigint as first argument."); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci uint64_t input; 691cb0ef41Sopenharmony_ci bool lossless; 701cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bigint_uint64( 711cb0ef41Sopenharmony_ci env, args[0], &input, &lossless)); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci napi_value output; 741cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_bigint_uint64(env, input, &output)); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci return output; 771cb0ef41Sopenharmony_ci} 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_cistatic napi_value TestWords(napi_env env, napi_callback_info info) { 801cb0ef41Sopenharmony_ci size_t argc = 1; 811cb0ef41Sopenharmony_ci napi_value args[1]; 821cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci napi_valuetype valuetype0; 871cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, valuetype0 == napi_bigint, 901cb0ef41Sopenharmony_ci "Wrong type of arguments. Expects a bigint as first argument."); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci size_t expected_word_count; 931cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bigint_words( 941cb0ef41Sopenharmony_ci env, args[0], NULL, &expected_word_count, NULL)); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci int sign_bit; 971cb0ef41Sopenharmony_ci size_t word_count = 10; 981cb0ef41Sopenharmony_ci uint64_t words[10]; 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bigint_words( 1011cb0ef41Sopenharmony_ci env, args[0], &sign_bit, &word_count, words)); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, word_count == expected_word_count, 1041cb0ef41Sopenharmony_ci "word counts do not match"); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci napi_value output; 1071cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_bigint_words( 1081cb0ef41Sopenharmony_ci env, sign_bit, word_count, words, &output)); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci return output; 1111cb0ef41Sopenharmony_ci} 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci// throws RangeError 1141cb0ef41Sopenharmony_cistatic napi_value CreateTooBigBigInt(napi_env env, napi_callback_info info) { 1151cb0ef41Sopenharmony_ci int sign_bit = 0; 1161cb0ef41Sopenharmony_ci size_t word_count = SIZE_MAX; 1171cb0ef41Sopenharmony_ci uint64_t words[10] = {0}; 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci napi_value output; 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_bigint_words( 1221cb0ef41Sopenharmony_ci env, sign_bit, word_count, words, &output)); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci return output; 1251cb0ef41Sopenharmony_ci} 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci// Test that we correctly forward exceptions from the engine. 1281cb0ef41Sopenharmony_cistatic napi_value MakeBigIntWordsThrow(napi_env env, napi_callback_info info) { 1291cb0ef41Sopenharmony_ci uint64_t words[10] = {0}; 1301cb0ef41Sopenharmony_ci napi_value output; 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci napi_status status = napi_create_bigint_words(env, 1331cb0ef41Sopenharmony_ci 0, 1341cb0ef41Sopenharmony_ci INT_MAX, 1351cb0ef41Sopenharmony_ci words, 1361cb0ef41Sopenharmony_ci &output); 1371cb0ef41Sopenharmony_ci if (status != napi_pending_exception) 1381cb0ef41Sopenharmony_ci napi_throw_error(env, NULL, "Expected status `napi_pending_exception`"); 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci return NULL; 1411cb0ef41Sopenharmony_ci} 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ciEXTERN_C_START 1441cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) { 1451cb0ef41Sopenharmony_ci napi_property_descriptor descriptors[] = { 1461cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("IsLossless", IsLossless), 1471cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("TestInt64", TestInt64), 1481cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("TestUint64", TestUint64), 1491cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("TestWords", TestWords), 1501cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("CreateTooBigBigInt", CreateTooBigBigInt), 1511cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("MakeBigIntWordsThrow", MakeBigIntWordsThrow), 1521cb0ef41Sopenharmony_ci }; 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_define_properties( 1551cb0ef41Sopenharmony_ci env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci return exports; 1581cb0ef41Sopenharmony_ci} 1591cb0ef41Sopenharmony_ciEXTERN_C_END 160