2017-05-15 20:18:50 -04:00
|
|
|
'use strict';
|
2017-07-27 20:59:29 +03:00
|
|
|
// Flags: --expose-gc
|
2017-05-15 20:18:50 -04:00
|
|
|
|
|
|
|
const common = require('../../common');
|
2018-01-23 21:49:25 +11:00
|
|
|
const test_general = require(`./build/${common.buildType}/test_general`);
|
2017-05-15 20:18:50 -04:00
|
|
|
const assert = require('assert');
|
2024-08-13 18:28:21 +08:00
|
|
|
const { gcUntil } = require('../../common/gc');
|
2017-05-15 20:18:50 -04:00
|
|
|
|
|
|
|
const val1 = '1';
|
|
|
|
const val2 = 1;
|
|
|
|
const val3 = 1;
|
|
|
|
|
|
|
|
class BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExtendedClass extends BaseClass {
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseObject = new BaseClass();
|
|
|
|
const extendedObject = new ExtendedClass();
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Test napi_strict_equals
|
2017-05-15 20:18:50 -04:00
|
|
|
assert.ok(test_general.testStrictEquals(val1, val1));
|
|
|
|
assert.strictEqual(test_general.testStrictEquals(val1, val2), false);
|
|
|
|
assert.ok(test_general.testStrictEquals(val2, val3));
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Test napi_get_prototype
|
2017-05-15 20:18:50 -04:00
|
|
|
assert.strictEqual(test_general.testGetPrototype(baseObject),
|
|
|
|
Object.getPrototypeOf(baseObject));
|
|
|
|
assert.strictEqual(test_general.testGetPrototype(extendedObject),
|
|
|
|
Object.getPrototypeOf(extendedObject));
|
2018-01-27 14:20:13 -05:00
|
|
|
// Prototypes for base and extended should be different.
|
|
|
|
assert.notStrictEqual(test_general.testGetPrototype(baseObject),
|
|
|
|
test_general.testGetPrototype(extendedObject));
|
2017-05-24 16:58:03 -04:00
|
|
|
|
2020-03-12 14:52:34 -04:00
|
|
|
// Test version management functions
|
2024-12-31 08:29:50 -08:00
|
|
|
assert.strictEqual(test_general.testGetVersion(), 10);
|
2017-06-29 13:07:03 -04:00
|
|
|
|
|
|
|
[
|
|
|
|
123,
|
|
|
|
'test string',
|
|
|
|
function() {},
|
|
|
|
new Object(),
|
|
|
|
true,
|
|
|
|
undefined,
|
2021-03-26 08:51:08 -07:00
|
|
|
Symbol(),
|
2017-06-29 13:07:03 -04:00
|
|
|
].forEach((val) => {
|
|
|
|
assert.strictEqual(test_general.testNapiTypeof(val), typeof val);
|
|
|
|
});
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// Since typeof in js return object need to validate specific case
|
2017-06-29 13:07:03 -04:00
|
|
|
// for null
|
|
|
|
assert.strictEqual(test_general.testNapiTypeof(null), 'null');
|
2017-06-22 14:10:05 +03:00
|
|
|
|
|
|
|
// Assert that wrapping twice fails.
|
2017-07-27 20:59:29 +03:00
|
|
|
const x = {};
|
2017-09-09 12:15:14 +03:00
|
|
|
test_general.wrap(x);
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => test_general.wrap(x),
|
|
|
|
{ name: 'Error', message: 'Invalid argument' });
|
2020-07-15 15:45:53 -07:00
|
|
|
// Clean up here, otherwise derefItemWasCalled() will be polluted.
|
|
|
|
test_general.removeWrap(x);
|
2017-07-27 20:59:29 +03:00
|
|
|
|
|
|
|
// Ensure that wrapping, removing the wrap, and then wrapping again works.
|
|
|
|
const y = {};
|
2017-09-09 12:15:14 +03:00
|
|
|
test_general.wrap(y);
|
2017-07-27 20:59:29 +03:00
|
|
|
test_general.removeWrap(y);
|
2018-02-01 15:32:41 +05:30
|
|
|
// Wrapping twice succeeds if a remove_wrap() separates the instances
|
2018-02-09 02:32:04 +01:00
|
|
|
test_general.wrap(y);
|
2020-07-15 15:45:53 -07:00
|
|
|
// Clean up here, otherwise derefItemWasCalled() will be polluted.
|
|
|
|
test_general.removeWrap(y);
|
2017-06-27 20:56:15 -07:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Test napi_adjust_external_memory
|
2017-06-27 20:56:15 -07:00
|
|
|
const adjustedValue = test_general.testAdjustExternalMemory();
|
|
|
|
assert.strictEqual(typeof adjustedValue, 'number');
|
|
|
|
assert(adjustedValue > 0);
|
2020-07-15 15:45:53 -07:00
|
|
|
|
|
|
|
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({}))();
|
2024-08-13 18:28:21 +08:00
|
|
|
await gcUntil('deref_item() was called upon garbage collecting a ' +
|
2020-07-15 15:45:53 -07:00
|
|
|
'wrapped object.',
|
2024-08-13 18:28:21 +08:00
|
|
|
() => test_general.derefItemWasCalled());
|
2020-07-15 15:45:53 -07:00
|
|
|
|
|
|
|
// 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;
|
2024-08-13 18:28:21 +08:00
|
|
|
await gcUntil(
|
2020-07-15 15:45:53 -07:00
|
|
|
'finalize callback was not called upon garbage collection.',
|
|
|
|
() => (!test_general.finalizeWasCalled()));
|
|
|
|
}
|
|
|
|
runGCTests();
|