11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Test API calls for instance data.
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (module !== require.main) {
71cb0ef41Sopenharmony_ci  // When required as a module, run the tests.
81cb0ef41Sopenharmony_ci  const test_instance_data =
91cb0ef41Sopenharmony_ci    require(`./build/${common.buildType}/test_instance_data`);
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  // Test that instance data can be used in an async work callback.
121cb0ef41Sopenharmony_ci  new Promise((resolve) => test_instance_data.asyncWorkCallback(resolve))
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci    // Test that the buffer finalizer can access the instance data.
151cb0ef41Sopenharmony_ci    .then(() => new Promise((resolve) => {
161cb0ef41Sopenharmony_ci      test_instance_data.testBufferFinalizer(resolve);
171cb0ef41Sopenharmony_ci      global.gc();
181cb0ef41Sopenharmony_ci    }))
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    // Test that the thread-safe function can access the instance data.
211cb0ef41Sopenharmony_ci    .then(() => new Promise((resolve) =>
221cb0ef41Sopenharmony_ci      test_instance_data.testThreadsafeFunction(common.mustCall(),
231cb0ef41Sopenharmony_ci                                                common.mustCall(resolve))));
241cb0ef41Sopenharmony_ci} else {
251cb0ef41Sopenharmony_ci  // When launched as a script, run tests in either a child process or in a
261cb0ef41Sopenharmony_ci  // worker thread.
271cb0ef41Sopenharmony_ci  const assert = require('assert');
281cb0ef41Sopenharmony_ci  const requireAs = require('../../common/require-as');
291cb0ef41Sopenharmony_ci  const runOptions = { stdio: ['inherit', 'pipe', 'inherit'] };
301cb0ef41Sopenharmony_ci  const { spawnSync } = require('child_process');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  // Run tests in a child process.
331cb0ef41Sopenharmony_ci  requireAs(__filename, ['--expose-gc'], runOptions, 'child');
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  // Run tests in a worker thread in a child process.
361cb0ef41Sopenharmony_ci  requireAs(__filename, ['--expose-gc'], runOptions, 'worker');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  function testProcessExit(addonName) {
391cb0ef41Sopenharmony_ci    // Make sure that process exit is clean when the instance data has
401cb0ef41Sopenharmony_ci    // references to JS objects.
411cb0ef41Sopenharmony_ci    const path = require
421cb0ef41Sopenharmony_ci      .resolve(`./build/${common.buildType}/${addonName}`)
431cb0ef41Sopenharmony_ci      // Replace any backslashes with double backslashes because they'll be re-
441cb0ef41Sopenharmony_ci      // interpreted back to single backslashes in the command line argument
451cb0ef41Sopenharmony_ci      // to the child process. Windows needs this.
461cb0ef41Sopenharmony_ci      .replace(/\\/g, '\\\\');
471cb0ef41Sopenharmony_ci    const child = spawnSync(process.execPath, ['-e', `require('${path}');`]);
481cb0ef41Sopenharmony_ci    assert.strictEqual(child.signal, null);
491cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
501cb0ef41Sopenharmony_ci    assert.strictEqual(child.stderr.toString(), 'addon_free');
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  testProcessExit('test_ref_then_set');
541cb0ef41Sopenharmony_ci  testProcessExit('test_set_then_ref');
551cb0ef41Sopenharmony_ci}
56