node/test/js-native-api/test_general/testInstanceOf.js
Vladimir Morozov 77d81979a3
node-api: use local files for instanceof test
PR-URL: https://github.com/nodejs/node/pull/60190
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2025-10-28 11:29:04 +00:00

47 lines
1.2 KiB
JavaScript

'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`);
// 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);
}