Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

View File

@ -0,0 +1,11 @@
{
"targets": [
{
"target_name": "test_number",
"sources": [
"test_number.c",
"test_null.c",
]
}
]
}

View File

@ -0,0 +1,134 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const test_number = require(`./build/${common.buildType}/test_number`);
// Testing api calls for number
function testNumber(num) {
assert.strictEqual(num, test_number.Test(num));
}
testNumber(0);
testNumber(-0);
testNumber(1);
testNumber(-1);
testNumber(100);
testNumber(2121);
testNumber(-1233);
testNumber(986583);
testNumber(-976675);
/* eslint-disable no-loss-of-precision */
testNumber(
98765432213456789876546896323445679887645323232436587988766545658);
testNumber(
-4350987086545760976737453646576078997096876957864353245245769809);
/* eslint-enable no-loss-of-precision */
testNumber(Number.MIN_SAFE_INTEGER);
testNumber(Number.MAX_SAFE_INTEGER);
testNumber(Number.MAX_SAFE_INTEGER + 10);
testNumber(Number.MIN_VALUE);
testNumber(Number.MAX_VALUE);
testNumber(Number.MAX_VALUE + 10);
testNumber(Number.POSITIVE_INFINITY);
testNumber(Number.NEGATIVE_INFINITY);
testNumber(Number.NaN);
function testUint32(input, expected = input) {
assert.strictEqual(expected, test_number.TestUint32Truncation(input));
}
// Test zero
testUint32(0.0, 0);
testUint32(-0.0, 0);
// Test overflow scenarios
testUint32(4294967295);
testUint32(4294967296, 0);
testUint32(4294967297, 1);
testUint32(17 * 4294967296 + 1, 1);
testUint32(-1, 0xffffffff);
// Validate documented behavior when value is retrieved as 32-bit integer with
// `napi_get_value_int32`
function testInt32(input, expected = input) {
assert.strictEqual(expected, test_number.TestInt32Truncation(input));
}
// Test zero
testInt32(0.0, 0);
testInt32(-0.0, 0);
// Test min/max int32 range
testInt32(-Math.pow(2, 31));
testInt32(Math.pow(2, 31) - 1);
// Test overflow scenarios
testInt32(4294967297, 1);
testInt32(4294967296, 0);
testInt32(4294967295, -1);
testInt32(4294967296 * 5 + 3, 3);
// Test min/max safe integer range
testInt32(Number.MIN_SAFE_INTEGER, 1);
testInt32(Number.MAX_SAFE_INTEGER, -1);
// Test within int64_t range (with precision loss)
testInt32(-Math.pow(2, 63) + (Math.pow(2, 9) + 1), 1024);
testInt32(Math.pow(2, 63) - (Math.pow(2, 9) + 1), -1024);
// Test min/max double value
testInt32(-Number.MIN_VALUE, 0);
testInt32(Number.MIN_VALUE, 0);
testInt32(-Number.MAX_VALUE, 0);
testInt32(Number.MAX_VALUE, 0);
// Test outside int64_t range
testInt32(-Math.pow(2, 63) + (Math.pow(2, 9)), 0);
testInt32(Math.pow(2, 63) - (Math.pow(2, 9)), 0);
// Test non-finite numbers
testInt32(Number.POSITIVE_INFINITY, 0);
testInt32(Number.NEGATIVE_INFINITY, 0);
testInt32(Number.NaN, 0);
// Validate documented behavior when value is retrieved as 64-bit integer with
// `napi_get_value_int64`
function testInt64(input, expected = input) {
assert.strictEqual(expected, test_number.TestInt64Truncation(input));
}
// Both V8 and ChakraCore return a sentinel value of `0x8000000000000000` when
// the conversion goes out of range, but V8 treats it as unsigned in some cases.
const RANGEERROR_POSITIVE = Math.pow(2, 63);
const RANGEERROR_NEGATIVE = -Math.pow(2, 63);
// Test zero
testInt64(0.0, 0);
testInt64(-0.0, 0);
// Test min/max safe integer range
testInt64(Number.MIN_SAFE_INTEGER);
testInt64(Number.MAX_SAFE_INTEGER);
// Test within int64_t range (with precision loss)
testInt64(-Math.pow(2, 63) + (Math.pow(2, 9) + 1));
testInt64(Math.pow(2, 63) - (Math.pow(2, 9) + 1));
// Test min/max double value
testInt64(-Number.MIN_VALUE, 0);
testInt64(Number.MIN_VALUE, 0);
testInt64(-Number.MAX_VALUE, RANGEERROR_NEGATIVE);
testInt64(Number.MAX_VALUE, RANGEERROR_POSITIVE);
// Test outside int64_t range
testInt64(-Math.pow(2, 63) + (Math.pow(2, 9)), RANGEERROR_NEGATIVE);
testInt64(Math.pow(2, 63) - (Math.pow(2, 9)), RANGEERROR_POSITIVE);
// Test non-finite numbers
testInt64(Number.POSITIVE_INFINITY, 0);
testInt64(Number.NEGATIVE_INFINITY, 0);
testInt64(Number.NaN, 0);

View File

@ -0,0 +1,77 @@
#include <js_native_api.h>
#include "../common.h"
// Unifies the way the macros declare values.
typedef double double_t;
#define BINDING_FOR_CREATE(initial_capital, lowercase) \
static napi_value Create##initial_capital(napi_env env, \
napi_callback_info info) { \
napi_value return_value, call_result; \
lowercase##_t value = 42; \
NODE_API_CALL(env, napi_create_object(env, &return_value)); \
add_returned_status(env, \
"envIsNull", \
return_value, \
"Invalid argument", \
napi_invalid_arg, \
napi_create_##lowercase(NULL, value, &call_result)); \
napi_create_##lowercase(env, value, NULL); \
add_last_status(env, "resultIsNull", return_value); \
return return_value; \
}
#define BINDING_FOR_GET_VALUE(initial_capital, lowercase) \
static napi_value GetValue##initial_capital(napi_env env, \
napi_callback_info info) { \
napi_value return_value, call_result; \
lowercase##_t value = 42; \
NODE_API_CALL(env, napi_create_object(env, &return_value)); \
NODE_API_CALL(env, napi_create_##lowercase(env, value, &call_result)); \
add_returned_status( \
env, \
"envIsNull", \
return_value, \
"Invalid argument", \
napi_invalid_arg, \
napi_get_value_##lowercase(NULL, call_result, &value)); \
napi_get_value_##lowercase(env, NULL, &value); \
add_last_status(env, "valueIsNull", return_value); \
napi_get_value_##lowercase(env, call_result, NULL); \
add_last_status(env, "resultIsNull", return_value); \
return return_value; \
}
BINDING_FOR_CREATE(Double, double)
BINDING_FOR_CREATE(Int32, int32)
BINDING_FOR_CREATE(Uint32, uint32)
BINDING_FOR_CREATE(Int64, int64)
BINDING_FOR_GET_VALUE(Double, double)
BINDING_FOR_GET_VALUE(Int32, int32)
BINDING_FOR_GET_VALUE(Uint32, uint32)
BINDING_FOR_GET_VALUE(Int64, int64)
void init_test_null(napi_env env, napi_value exports) {
const napi_property_descriptor test_null_props[] = {
DECLARE_NODE_API_PROPERTY("createDouble", CreateDouble),
DECLARE_NODE_API_PROPERTY("createInt32", CreateInt32),
DECLARE_NODE_API_PROPERTY("createUint32", CreateUint32),
DECLARE_NODE_API_PROPERTY("createInt64", CreateInt64),
DECLARE_NODE_API_PROPERTY("getValueDouble", GetValueDouble),
DECLARE_NODE_API_PROPERTY("getValueInt32", GetValueInt32),
DECLARE_NODE_API_PROPERTY("getValueUint32", GetValueUint32),
DECLARE_NODE_API_PROPERTY("getValueInt64", GetValueInt64),
};
napi_value test_null;
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
NODE_API_CALL_RETURN_VOID(
env,
napi_define_properties(env,
test_null,
sizeof(test_null_props) / sizeof(*test_null_props),
test_null_props));
NODE_API_CALL_RETURN_VOID(
env, napi_set_named_property(env, exports, "testNull", test_null));
}

View File

@ -0,0 +1,8 @@
#ifndef TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_
#define TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_
#include <js_native_api.h>
void init_test_null(napi_env env, napi_value exports);
#endif // TEST_JS_NATIVE_API_TEST_NUMBER_TEST_NULL_H_

View File

@ -0,0 +1,18 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const { testNull } = require(`./build/${common.buildType}/test_number`);
const expectedCreateResult = {
envIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument',
};
const expectedGetValueResult = {
envIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument',
valueIsNull: 'Invalid argument',
};
[ 'Double', 'Int32', 'Uint32', 'Int64' ].forEach((typeName) => {
assert.deepStrictEqual(testNull['create' + typeName](), expectedCreateResult);
assert.deepStrictEqual(testNull['getValue' + typeName](), expectedGetValueResult);
});

View File

@ -0,0 +1,110 @@
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "test_null.h"
static napi_value Test(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
napi_valuetype valuetype0;
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects a number as first argument.");
double input;
NODE_API_CALL(env, napi_get_value_double(env, args[0], &input));
napi_value output;
NODE_API_CALL(env, napi_create_double(env, input, &output));
return output;
}
static napi_value TestUint32Truncation(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
napi_valuetype valuetype0;
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects a number as first argument.");
uint32_t input;
NODE_API_CALL(env, napi_get_value_uint32(env, args[0], &input));
napi_value output;
NODE_API_CALL(env, napi_create_uint32(env, input, &output));
return output;
}
static napi_value TestInt32Truncation(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
napi_valuetype valuetype0;
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects a number as first argument.");
int32_t input;
NODE_API_CALL(env, napi_get_value_int32(env, args[0], &input));
napi_value output;
NODE_API_CALL(env, napi_create_int32(env, input, &output));
return output;
}
static napi_value TestInt64Truncation(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments");
napi_valuetype valuetype0;
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
NODE_API_ASSERT(env, valuetype0 == napi_number,
"Wrong type of arguments. Expects a number as first argument.");
int64_t input;
NODE_API_CALL(env, napi_get_value_int64(env, args[0], &input));
napi_value output;
NODE_API_CALL(env, napi_create_int64(env, input, &output));
return output;
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("Test", Test),
DECLARE_NODE_API_PROPERTY("TestInt32Truncation", TestInt32Truncation),
DECLARE_NODE_API_PROPERTY("TestUint32Truncation", TestUint32Truncation),
DECLARE_NODE_API_PROPERTY("TestInt64Truncation", TestInt64Truncation),
};
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
init_test_null(env, exports);
return exports;
}
EXTERN_C_END