1'use strict';
2// Test API calls for instance data.
3
4const common = require('../../common');
5const assert = require('assert');
6
7if (module !== require.main) {
8  // When required as a module, run the tests.
9  const test_instance_data =
10    require(`./build/${common.buildType}/test_instance_data`);
11
12  // Print to stdout when the environment deletes the instance data. This output
13  // is checked by the parent process.
14  test_instance_data.setPrintOnDelete();
15
16  // Test that instance data can be accessed from a binding.
17  assert.strictEqual(test_instance_data.increment(), 42);
18
19  // Test that the instance data can be accessed from a finalizer.
20  test_instance_data.objectWithFinalizer(common.mustCall());
21  global.gc();
22} else {
23  // When launched as a script, run tests in either a child process or in a
24  // worker thread.
25  const requireAs = require('../../common/require-as');
26  const runOptions = { stdio: ['inherit', 'pipe', 'inherit'] };
27
28  function checkOutput(child) {
29    assert.strictEqual(child.status, 0);
30    assert.strictEqual(
31      (child.stdout.toString().split(/\r\n?|\n/) || [])[0],
32      'deleting addon data');
33  }
34
35  // Run tests in a child process.
36  checkOutput(requireAs(__filename, ['--expose-gc'], runOptions, 'child'));
37
38  // Run tests in a worker thread in a child process.
39  checkOutput(requireAs(__filename, ['--expose-gc'], runOptions, 'worker'));
40}
41