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
10 changes: 9 additions & 1 deletion implementors/node/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ globalThis.experimentalFeatures = {
// causing addons that reference them to fail at dlopen time.
sharedArrayBuffer: major >= 25 || (major === 24 && minor >= 9),
createObjectWithProperties: true,
setPrototype: true,
// node_api_set_prototype was added in Node.js v25.4.0 and v24.13.1, and not
// backported to v20.x or v22.x. Earlier versions do not export the symbol,
// so an addon referencing it fails at dlopen time.
setPrototype:
major > 25 ||
(major === 25 && minor >= 4) ||
(major === 24 && (minor > 13 || (minor === 13 && patch >= 1))),
// node_api_post_finalizer has been available since v20.10.0, which is at or
// below every Node.js version this harness supports.
postFinalizer: true,
};

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_set_prototype is gated behind NAPI_EXPERIMENTAL, so it builds as a
# separate addon that only runtimes exporting it need to load.
add_node_api_cts_experimental_addon(test_general_set_prototype test_general_set_prototype.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(),
);
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',
);
20 changes: 20 additions & 0 deletions tests/js-native-api/test_general/testSetPrototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// node_api_set_prototype is gated behind NAPI_EXPERIMENTAL, so it lives in its
// own addon and the stable test_general addon stays loadable everywhere.
if (!experimentalFeatures.setPrototype) {
skipTest();
}

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

const nullProtoObject = { __proto__: null };
assert.strictEqual(Object.getPrototypeOf(nullProtoObject), null);

test_general_set_prototype.testSetPrototype(nullProtoObject, Object.prototype);

assert.strictEqual(Object.getPrototypeOf(nullProtoObject), Object.prototype);
// napi_get_prototype must agree with the prototype just installed.
assert.strictEqual(
test_general.testGetPrototype(nullProtoObject),
Object.prototype,
);
Loading
Loading