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,10 @@
{
"targets": [
{
"target_name": "test_general",
"sources": [
"test_general.c"
]
}
]
}

View File

@ -0,0 +1,97 @@
'use strict';
// Flags: --expose-gc
const common = require('../../common');
const test_general = require(`./build/${common.buildType}/test_general`);
const assert = require('assert');
const { gcUntil } = require('../../common/gc');
const val1 = '1';
const val2 = 1;
const val3 = 1;
class BaseClass {
}
class ExtendedClass extends BaseClass {
}
const baseObject = new BaseClass();
const extendedObject = new ExtendedClass();
// Test napi_strict_equals
assert.ok(test_general.testStrictEquals(val1, val1));
assert.strictEqual(test_general.testStrictEquals(val1, val2), false);
assert.ok(test_general.testStrictEquals(val2, val3));
// Test napi_get_prototype
assert.strictEqual(test_general.testGetPrototype(baseObject),
Object.getPrototypeOf(baseObject));
assert.strictEqual(test_general.testGetPrototype(extendedObject),
Object.getPrototypeOf(extendedObject));
// Prototypes for base and extended should be different.
assert.notStrictEqual(test_general.testGetPrototype(baseObject),
test_general.testGetPrototype(extendedObject));
// Test version management functions
assert.strictEqual(test_general.testGetVersion(), 10);
[
123,
'test string',
function() {},
new Object(),
true,
undefined,
Symbol(),
].forEach((val) => {
assert.strictEqual(test_general.testNapiTypeof(val), typeof val);
});
// Since typeof in js return object need to validate specific case
// for null
assert.strictEqual(test_general.testNapiTypeof(null), 'null');
// Assert that wrapping twice fails.
const x = {};
test_general.wrap(x);
assert.throws(() => test_general.wrap(x),
{ name: 'Error', message: 'Invalid argument' });
// Clean up here, otherwise derefItemWasCalled() will be polluted.
test_general.removeWrap(x);
// Ensure that wrapping, removing the wrap, and then wrapping again works.
const y = {};
test_general.wrap(y);
test_general.removeWrap(y);
// Wrapping twice succeeds if a remove_wrap() separates the instances
test_general.wrap(y);
// Clean up here, otherwise derefItemWasCalled() will be polluted.
test_general.removeWrap(y);
// Test napi_adjust_external_memory
const adjustedValue = test_general.testAdjustExternalMemory();
assert.strictEqual(typeof adjustedValue, 'number');
assert(adjustedValue > 0);
async function runGCTests() {
// Ensure that garbage collecting an object with a wrapped native item results
// in the finalize callback being called.
assert.strictEqual(test_general.derefItemWasCalled(), false);
(() => test_general.wrap({}))();
await gcUntil('deref_item() was called upon garbage collecting a ' +
'wrapped object.',
() => test_general.derefItemWasCalled());
// Ensure that removing a wrap and garbage collecting does not fire the
// finalize callback.
let z = {};
test_general.testFinalizeWrap(z);
test_general.removeWrap(z);
z = null;
await gcUntil(
'finalize callback was not called upon garbage collection.',
() => (!test_general.finalizeWasCalled()));
}
runGCTests();

View File

@ -0,0 +1,57 @@
'use strict';
if (process.argv[2] === 'child') {
const common = require('../../common');
const test_general = require(`./build/${common.buildType}/test_general`);
// The second argument to `envCleanupWrap()` is an index into the global
// static string array named `env_cleanup_finalizer_messages` on the native
// side. A reverse mapping is reproduced here for clarity.
const finalizerMessages = {
'simple wrap': 0,
'wrap, removeWrap': 1,
'first wrap': 2,
'second wrap': 3,
};
// We attach the three objects we will test to `module.exports` to ensure they
// will not be garbage-collected before the process exits.
// Make sure the finalizer for a simple wrap will be called at env cleanup.
module.exports['simple wrap'] =
test_general.envCleanupWrap({}, finalizerMessages['simple wrap']);
// Make sure that a removed wrap does not result in a call to its finalizer at
// env cleanup.
module.exports['wrap, removeWrap'] =
test_general.envCleanupWrap({}, finalizerMessages['wrap, removeWrap']);
test_general.removeWrap(module.exports['wrap, removeWrap']);
// Make sure that only the latest attached version of a re-wrapped item's
// finalizer gets called at env cleanup.
module.exports['first wrap'] =
test_general.envCleanupWrap({}, finalizerMessages['first wrap']);
test_general.removeWrap(module.exports['first wrap']);
test_general.envCleanupWrap(module.exports['first wrap'],
finalizerMessages['second wrap']);
} else {
const assert = require('assert');
const { spawnSync } = require('child_process');
const child = spawnSync(process.execPath, [__filename, 'child'], {
stdio: [ process.stdin, 'pipe', process.stderr ],
});
// Grab the child's output and construct an object whose keys are the rows of
// the output and whose values are `true`, so we can compare the output while
// ignoring the order in which the lines of it were produced.
assert.deepStrictEqual(
child.stdout.toString().split(/\r\n|\r|\n/g).reduce((obj, item) =>
Object.assign(obj, item ? { [item]: true } : {}), {}), {
'finalize at env cleanup for simple wrap': true,
'finalize at env cleanup for second wrap': true,
});
// Ensure that the child exited successfully.
assert.strictEqual(child.status, 0);
}

View File

@ -0,0 +1,37 @@
'use strict';
// Flags: --expose-gc
const common = require('../../common');
const test_general = require(`./build/${common.buildType}/test_general`);
const assert = require('assert');
const { gcUntil } = require('../../common/gc');
let finalized = {};
const callback = common.mustCall(2);
// Add two items to be finalized and ensure the callback is called for each.
test_general.addFinalizerOnly(finalized, callback);
test_general.addFinalizerOnly(finalized, callback);
// Ensure attached items cannot be retrieved.
assert.throws(() => test_general.unwrap(finalized),
{ name: 'Error', message: 'Invalid argument' });
// Ensure attached items cannot be removed.
assert.throws(() => test_general.removeWrap(finalized),
{ name: 'Error', message: 'Invalid argument' });
finalized = null;
global.gc();
// Add an item to an object that is already wrapped, and ensure that its
// finalizer as well as the wrap finalizer gets called.
async function testFinalizeAndWrap() {
assert.strictEqual(test_general.derefItemWasCalled(), false);
let finalizeAndWrap = {};
test_general.wrap(finalizeAndWrap);
test_general.addFinalizerOnly(finalizeAndWrap, common.mustCall());
finalizeAndWrap = null;
await gcUntil('test finalize and wrap',
() => test_general.derefItemWasCalled());
}
testFinalizeAndWrap();

View File

@ -0,0 +1,8 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const test_globals = require(`./build/${common.buildType}/test_general`);
assert.strictEqual(test_globals.getUndefined(), undefined);
assert.strictEqual(test_globals.getNull(), null);

View File

@ -0,0 +1,95 @@
'use strict';
const common = require('../../common');
const fs = require('fs');
const assert = require('assert');
// Addon is referenced through the eval expression in testFile
const addon = require(`./build/${common.buildType}/test_general`);
const path = require('path');
// This test depends on a number of V8 tests.
const v8TestsDir = path.resolve(__dirname, '..', '..', '..', 'deps', 'v8',
'test', 'mjsunit');
const v8TestsDirExists = fs.existsSync(v8TestsDir);
// The following assert functions are referenced by v8's unit tests
// See for instance deps/v8/test/mjsunit/instanceof.js
// eslint-disable-next-line no-unused-vars
function assertTrue(assertion) {
return assert.strictEqual(assertion, true);
}
// eslint-disable-next-line no-unused-vars
function assertFalse(assertion) {
assert.strictEqual(assertion, false);
}
// eslint-disable-next-line no-unused-vars
function assertEquals(leftHandSide, rightHandSide) {
assert.strictEqual(leftHandSide, rightHandSide);
}
// eslint-disable-next-line no-unused-vars
function assertThrows(statement) {
assert.throws(function() {
eval(statement);
}, Error);
}
function testFile(fileName) {
try {
const contents = fs.readFileSync(fileName, { encoding: 'utf8' });
eval(contents.replace(/[(]([^\s(]+)\s+instanceof\s+([^)]+)[)]/g,
'(addon.doInstanceOf($1, $2))'));
} catch (err) {
// This test depends on V8 test files, which may not exist in downloaded
// archives. Emit a warning if the tests cannot be found instead of failing.
if (err.code === 'ENOENT' && !v8TestsDirExists)
process.emitWarning(`test file ${fileName} does not exist.`);
else
throw err;
}
}
testFile(path.join(v8TestsDir, 'instanceof.js'));
testFile(path.join(v8TestsDir, 'instanceof-2.js'));
// We can only perform this test if we have a working Symbol.hasInstance
if (typeof Symbol !== 'undefined' && 'hasInstance' in Symbol &&
typeof Symbol.hasInstance === 'symbol') {
function compareToNative(theObject, theConstructor) {
assert.strictEqual(
addon.doInstanceOf(theObject, theConstructor),
(theObject instanceof theConstructor),
);
}
function MyClass() {}
Object.defineProperty(MyClass, Symbol.hasInstance, {
value: function(candidate) {
return 'mark' in candidate;
},
});
function MySubClass() {}
MySubClass.prototype = new MyClass();
let x = new MySubClass();
let y = new MySubClass();
x.mark = true;
compareToNative(x, MySubClass);
compareToNative(y, MySubClass);
compareToNative(x, MyClass);
compareToNative(y, MyClass);
x = new MyClass();
y = new MyClass();
x.mark = true;
compareToNative(x, MySubClass);
compareToNative(y, MySubClass);
compareToNative(x, MyClass);
compareToNative(y, MyClass);
}

View File

@ -0,0 +1,14 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
// `addon` is referenced through the eval expression in testFile
const addon = require(`./build/${common.buildType}/test_general`);
const testCase = '(41.92 + 0.08);';
const expected = 42;
const actual = addon.testNapiRun(testCase);
assert.strictEqual(actual, expected);
assert.throws(() => addon.testNapiRun({ abc: 'def' }), /string was expected/);

View File

@ -0,0 +1,8 @@
'use strict';
const common = require('../../common');
const addon = require(`./build/${common.buildType}/test_general`);
const assert = require('assert');
addon.createNapiError();
assert(addon.testNapiErrorCleanup(), 'napi_status cleaned up for second call');

View File

@ -0,0 +1,315 @@
// we define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED here to
// validate that it can be used as a form of test itself. It is
// not related to any of the other tests
// defined in the file
#define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
#include <js_native_api.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "../common.h"
#include "../entry_point.h"
static napi_value testStrictEquals(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
bool bool_result;
napi_value result;
NODE_API_CALL(env, napi_strict_equals(env, args[0], args[1], &bool_result));
NODE_API_CALL(env, napi_get_boolean(env, bool_result, &result));
return result;
}
static napi_value testGetPrototype(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));
napi_value result;
NODE_API_CALL(env, napi_get_prototype(env, args[0], &result));
return result;
}
static napi_value testGetVersion(napi_env env, napi_callback_info info) {
uint32_t version;
napi_value result;
NODE_API_CALL(env, napi_get_version(env, &version));
NODE_API_CALL(env, napi_create_uint32(env, version, &result));
return result;
}
static napi_value doInstanceOf(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
bool instanceof;
NODE_API_CALL(env, napi_instanceof(env, args[0], args[1], &instanceof));
napi_value result;
NODE_API_CALL(env, napi_get_boolean(env, instanceof, &result));
return result;
}
static napi_value getNull(napi_env env, napi_callback_info info) {
napi_value result;
NODE_API_CALL(env, napi_get_null(env, &result));
return result;
}
static napi_value getUndefined(napi_env env, napi_callback_info info) {
napi_value result;
NODE_API_CALL(env, napi_get_undefined(env, &result));
return result;
}
static napi_value createNapiError(napi_env env, napi_callback_info info) {
napi_value value;
NODE_API_CALL(env, napi_create_string_utf8(env, "xyz", 3, &value));
double double_value;
napi_status status = napi_get_value_double(env, value, &double_value);
NODE_API_ASSERT(env, status != napi_ok, "Failed to produce error condition");
const napi_extended_error_info *error_info = 0;
NODE_API_CALL(env, napi_get_last_error_info(env, &error_info));
NODE_API_ASSERT(env, error_info->error_code == status,
"Last error info code should match last status");
NODE_API_ASSERT(env, error_info->error_message,
"Last error info message should not be null");
return NULL;
}
static napi_value testNapiErrorCleanup(napi_env env, napi_callback_info info) {
const napi_extended_error_info *error_info = 0;
NODE_API_CALL(env, napi_get_last_error_info(env, &error_info));
napi_value result;
bool is_ok = error_info->error_code == napi_ok;
NODE_API_CALL(env, napi_get_boolean(env, is_ok, &result));
return result;
}
static napi_value testNapiTypeof(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));
napi_valuetype argument_type;
NODE_API_CALL(env, napi_typeof(env, args[0], &argument_type));
napi_value result = NULL;
if (argument_type == napi_number) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "number", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_string) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "string", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_function) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "function", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_object) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "object", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_boolean) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "boolean", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_undefined) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "undefined", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_symbol) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "symbol", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_null) {
NODE_API_CALL(env, napi_create_string_utf8(
env, "null", NAPI_AUTO_LENGTH, &result));
}
return result;
}
static bool deref_item_called = false;
static void deref_item(napi_env env, void* data, void* hint) {
(void) hint;
NODE_API_ASSERT_RETURN_VOID(env, data == &deref_item_called,
"Finalize callback was called with the correct pointer");
deref_item_called = true;
}
static napi_value deref_item_was_called(napi_env env, napi_callback_info info) {
napi_value it_was_called;
NODE_API_CALL(env, napi_get_boolean(env, deref_item_called, &it_was_called));
return it_was_called;
}
static napi_value wrap_first_arg(napi_env env,
napi_callback_info info,
napi_finalize finalizer,
void* data) {
size_t argc = 1;
napi_value to_wrap;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &to_wrap, NULL, NULL));
NODE_API_CALL(env, napi_wrap(env, to_wrap, data, finalizer, NULL, NULL));
return to_wrap;
}
static napi_value wrap(napi_env env, napi_callback_info info) {
deref_item_called = false;
return wrap_first_arg(env, info, deref_item, &deref_item_called);
}
static napi_value unwrap(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value wrapped;
void* data;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &wrapped, NULL, NULL));
NODE_API_CALL(env, napi_unwrap(env, wrapped, &data));
return NULL;
}
static napi_value remove_wrap(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value wrapped;
void* data;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &wrapped, NULL, NULL));
NODE_API_CALL(env, napi_remove_wrap(env, wrapped, &data));
return NULL;
}
static bool finalize_called = false;
static void test_finalize(napi_env env, void* data, void* hint) {
finalize_called = true;
}
static napi_value test_finalize_wrap(napi_env env, napi_callback_info info) {
return wrap_first_arg(env, info, test_finalize, NULL);
}
static napi_value finalize_was_called(napi_env env, napi_callback_info info) {
napi_value it_was_called;
NODE_API_CALL(env, napi_get_boolean(env, finalize_called, &it_was_called));
return it_was_called;
}
static napi_value testAdjustExternalMemory(napi_env env, napi_callback_info info) {
napi_value result;
int64_t adjustedValue;
NODE_API_CALL(env, napi_adjust_external_memory(env, 1, &adjustedValue));
NODE_API_CALL(env, napi_create_double(env, (double)adjustedValue, &result));
return result;
}
static napi_value testNapiRun(napi_env env, napi_callback_info info) {
napi_value script, result;
size_t argc = 1;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &script, NULL, NULL));
NODE_API_CALL(env, napi_run_script(env, script, &result));
return result;
}
static void finalizer_only_callback(napi_env env, void* data, void* hint) {
napi_ref js_cb_ref = data;
napi_value js_cb, undefined;
NODE_API_CALL_RETURN_VOID(env, napi_get_reference_value(env, js_cb_ref, &js_cb));
NODE_API_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined));
NODE_API_CALL_RETURN_VOID(env,
napi_call_function(env, undefined, js_cb, 0, NULL, NULL));
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_cb_ref));
}
static napi_value add_finalizer_only(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value argv[2];
napi_ref js_cb_ref;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
NODE_API_CALL(env, napi_create_reference(env, argv[1], 1, &js_cb_ref));
NODE_API_CALL(env,
napi_add_finalizer(
env, argv[0], js_cb_ref, finalizer_only_callback, NULL, NULL));
return NULL;
}
static const char* env_cleanup_finalizer_messages[] = {
"simple wrap",
"wrap, removeWrap",
"first wrap",
"second wrap"
};
static void cleanup_env_finalizer(napi_env env, void* data, void* hint) {
(void) env;
(void) hint;
printf("finalize at env cleanup for %s\n",
env_cleanup_finalizer_messages[(uintptr_t)data]);
}
static napi_value env_cleanup_wrap(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value argv[2];
uint32_t value;
uintptr_t ptr_value;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
NODE_API_CALL(env, napi_get_value_uint32(env, argv[1], &value));
ptr_value = value;
return wrap_first_arg(env, info, cleanup_env_finalizer, (void*)ptr_value);
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("testStrictEquals", testStrictEquals),
DECLARE_NODE_API_PROPERTY("testGetPrototype", testGetPrototype),
DECLARE_NODE_API_PROPERTY("testGetVersion", testGetVersion),
DECLARE_NODE_API_PROPERTY("testNapiRun", testNapiRun),
DECLARE_NODE_API_PROPERTY("doInstanceOf", doInstanceOf),
DECLARE_NODE_API_PROPERTY("getUndefined", getUndefined),
DECLARE_NODE_API_PROPERTY("getNull", getNull),
DECLARE_NODE_API_PROPERTY("createNapiError", createNapiError),
DECLARE_NODE_API_PROPERTY("testNapiErrorCleanup", testNapiErrorCleanup),
DECLARE_NODE_API_PROPERTY("testNapiTypeof", testNapiTypeof),
DECLARE_NODE_API_PROPERTY("wrap", wrap),
DECLARE_NODE_API_PROPERTY("envCleanupWrap", env_cleanup_wrap),
DECLARE_NODE_API_PROPERTY("unwrap", unwrap),
DECLARE_NODE_API_PROPERTY("removeWrap", remove_wrap),
DECLARE_NODE_API_PROPERTY("addFinalizerOnly", add_finalizer_only),
DECLARE_NODE_API_PROPERTY("testFinalizeWrap", test_finalize_wrap),
DECLARE_NODE_API_PROPERTY("finalizeWasCalled", finalize_was_called),
DECLARE_NODE_API_PROPERTY("derefItemWasCalled", deref_item_was_called),
DECLARE_NODE_API_PROPERTY("testAdjustExternalMemory", testAdjustExternalMemory)
};
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
return exports;
}
EXTERN_C_END