node/test/addons-napi/test_general/test.js
Gabriel Schulhof cb94905774 n-api: stop creating references to primitives
The binding testing napi_wrap() creates references to primitives passed
into the binding in its second parameter. This is unnecessary and not
at all the point of the test. Additionally, creating persistent
references to primitive values may not be supported by all VMs, since
primitives are best persisted in their native form.

Instead, the point of the test is to make sure that the finalize
callback gets called when it should get called, that it gets called
with the correct pointer, and that it does not get called when it
should not get called. Creating persistent references is not necessary
for verifying this.

PR-URL: https://github.com/nodejs/node/pull/15289
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Re: https://github.com/nodejs/node-chakracore/issues/380
2017-09-13 06:19:50 +03:00

99 lines
2.9 KiB
JavaScript

'use strict';
// Flags: --expose-gc
const common = require('../../common');
const test_general = require(`./build/${common.buildType}/test_general`);
const assert = require('assert');
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));
assert.ok(test_general.testGetPrototype(baseObject) !==
test_general.testGetPrototype(extendedObject),
'Prototypes for base and extended should be different');
// test version management functions
// expected version is currently 1
assert.strictEqual(test_general.testGetVersion(), 1);
const [ major, minor, patch, release ] = test_general.testGetNodeVersion();
assert.strictEqual(process.version.split('-')[0],
`v${major}.${minor}.${patch}`);
assert.strictEqual(release, process.release.name);
[
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');
// Ensure that garbage collecting an object with a wrapped native item results
// in the finalize callback being called.
let w = {};
test_general.wrap(w);
w = null;
global.gc();
assert.strictEqual(test_general.derefItemWasCalled(), true,
'deref_item() was called upon garbage collecting a ' +
'wrapped object');
// Assert that wrapping twice fails.
const x = {};
test_general.wrap(x);
assert.throws(function() {
test_general.wrap(x);
}, Error);
// Ensure that wrapping, removing the wrap, and then wrapping again works.
const y = {};
test_general.wrap(y);
test_general.removeWrap(y);
assert.doesNotThrow(function() {
test_general.wrap(y);
}, Error, 'Wrapping twice succeeds if a remove_wrap() separates the instances');
// 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;
global.gc();
assert.strictEqual(test_general.finalizeWasCalled(), false,
'finalize callback was not called upon garbage collection');
// test napi_adjust_external_memory
const adjustedValue = test_general.testAdjustExternalMemory();
assert.strictEqual(typeof adjustedValue, 'number');
assert(adjustedValue > 0);