Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PORTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Tests covering the engine-specific part of Node-API, defined in `js_native_api.h
| `test_exception` | Ported ✅ | Medium |
| `test_finalizer` | Ported ✅ | Medium |
| `test_function` | Ported ✅ | Medium |
| `test_general` | Not ported | Hard |
| `test_general` | Partial | Hard |
| `test_handle_scope` | Ported ✅ | Easy |
| `test_instance_data` | Not ported | Medium |
| `test_new_target` | Ported ✅ | Easy |
Expand Down
5 changes: 5 additions & 0 deletions tests/js-native-api/test_general/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_node_api_cts_addon(test_general test_general.c)

# node_api_post_finalizer is gated behind NAPI_EXPERIMENTAL, so the finalizer
# cases build as a separate addon that only runtimes exporting it need to load.
add_node_api_cts_experimental_addon(test_general_finalizer test_general_finalizer.c)
96 changes: 96 additions & 0 deletions tests/js-native-api/test_general/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const test_general = loadAddon('test_general');

const val1 = '1';
const val2 = 1;
const val3 = 1;

class BaseClass {
}

class ExtendedClass extends BaseClass {
}

const baseObject = new BaseClass();
const extendedObject = new ExtendedClass();

// 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));

// 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),
);

// napi_get_version. Upstream pins this to Node.js's own Node-API version;
// portably, the addon must report whatever version the runtime declares.
assert.strictEqual(test_general.testGetVersion(), napiVersion);

// napi_typeof
[
123,
'test string',
function() {},
new Object(),
true,
undefined,
Symbol(),
].forEach((val) => {
assert.strictEqual(test_general.testNapiTypeof(val), typeof val);
});

// typeof null is 'object' in JS, so napi_null gets its own case.
assert.strictEqual(test_general.testNapiTypeof(null), 'null');

// Wrapping the same object 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);

// Wrapping twice succeeds if a removeWrap() separates the instances.
const y = {};
test_general.wrap(y);
test_general.removeWrap(y);
test_general.wrap(y);
// Clean up here, otherwise derefItemWasCalled() will be polluted.
test_general.removeWrap(y);

// napi_adjust_external_memory
const adjustedValue = test_general.testAdjustExternalMemory();
assert.strictEqual(typeof adjustedValue, 'number');
assert.ok(adjustedValue > 0);

// Garbage collecting a wrapped object calls the finalizer.
assert.strictEqual(test_general.derefItemWasCalled(), false);

(() => test_general.wrap({}))();
await gcUntil(
'deref_item() was called upon garbage collecting a wrapped object.',
() => test_general.derefItemWasCalled(),
);

// Removing a wrap and then garbage collecting does not call the finalizer.
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(),
);
48 changes: 48 additions & 0 deletions tests/js-native-api/test_general/testFinalizer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// addFinalizerOnly calls back into JS from a finalizer, which is only legal
// via node_api_post_finalizer. That API is experimental, so it lives in its
// own addon and the stable test_general addon stays loadable everywhere.
if (!experimentalFeatures.postFinalizer) {
skipTest();
}

const test_general = loadAddon('test_general');
const test_general_finalizer = loadAddon('test_general_finalizer');

// Two finalizers on one object: both must fire.
let calls = 0;
const callback = mustCall(() => {
calls++;
}, 2);

let finalized = {};
test_general_finalizer.addFinalizerOnly(finalized, callback);
test_general_finalizer.addFinalizerOnly(finalized, callback);

// A finalizer-only attachment is not a wrap, so the attached item can be
// neither retrieved nor removed.
assert.throws(
() => test_general.unwrap(finalized),
{ name: 'Error', message: 'Invalid argument' },
);
assert.throws(
() => test_general.removeWrap(finalized),
{ name: 'Error', message: 'Invalid argument' },
);

finalized = null;
// The callbacks are posted rather than run inline during GC, so wait for them
// instead of assuming a single collection is enough.
await gcUntil('finalizer-only callbacks ran', () => calls === 2);

// An item added to an already-wrapped object gets its own finalizer, and the
// wrap's finalizer still runs too.
assert.strictEqual(test_general.derefItemWasCalled(), false);

let finalizeAndWrap = {};
test_general.wrap(finalizeAndWrap);
test_general_finalizer.addFinalizerOnly(finalizeAndWrap, mustCall());
finalizeAndWrap = null;
await gcUntil(
'finalize and wrap',
() => test_general.derefItemWasCalled(),
);
4 changes: 4 additions & 0 deletions tests/js-native-api/test_general/testGlobals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test_general = loadAddon('test_general');

assert.strictEqual(test_general.getUndefined(), undefined);
assert.strictEqual(test_general.getNull(), null);
7 changes: 7 additions & 0 deletions tests/js-native-api/test_general/testNapiRun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const test_general = loadAddon('test_general');

assert.strictEqual(test_general.testNapiRun('(41.92 + 0.08);'), 42);
assert.throws(
() => test_general.testNapiRun({ abc: 'def' }),
/string was expected/,
);
10 changes: 10 additions & 0 deletions tests/js-native-api/test_general/testNapiStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const test_general = loadAddon('test_general');

// createNapiError provokes a failing call, then checks that
// napi_get_last_error_info reports that failure. The next successful call must
// reset the recorded status back to napi_ok.
test_general.createNapiError();
assert.ok(
test_general.testNapiErrorCleanup(),
'napi_status cleaned up for second call',
);
Loading
Loading