1'use strict'; 2// Flags: --expose-gc 3 4const common = require('../../common'); 5const test_general = require(`./build/${common.buildType}/test_general`); 6const assert = require('assert'); 7 8let finalized = {}; 9const callback = common.mustCall(2); 10 11// Add two items to be finalized and ensure the callback is called for each. 12test_general.addFinalizerOnly(finalized, callback); 13test_general.addFinalizerOnly(finalized, callback); 14 15// Ensure attached items cannot be retrieved. 16assert.throws(() => test_general.unwrap(finalized), 17 { name: 'Error', message: 'Invalid argument' }); 18 19// Ensure attached items cannot be removed. 20assert.throws(() => test_general.removeWrap(finalized), 21 { name: 'Error', message: 'Invalid argument' }); 22finalized = null; 23global.gc(); 24 25// Add an item to an object that is already wrapped, and ensure that its 26// finalizer as well as the wrap finalizer gets called. 27async function testFinalizeAndWrap() { 28 assert.strictEqual(test_general.derefItemWasCalled(), false); 29 let finalizeAndWrap = {}; 30 test_general.wrap(finalizeAndWrap); 31 test_general.addFinalizerOnly(finalizeAndWrap, common.mustCall()); 32 finalizeAndWrap = null; 33 await common.gcUntil('test finalize and wrap', 34 () => test_general.derefItemWasCalled()); 35} 36testFinalizeAndWrap(); 37