11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that weak references work across serialization.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cirequire('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst fs = require('fs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'snapshot.blob');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cifunction runTest(entry) {
171cb0ef41Sopenharmony_ci  console.log('running test with', entry);
181cb0ef41Sopenharmony_ci  {
191cb0ef41Sopenharmony_ci    const child = spawnSync(process.execPath, [
201cb0ef41Sopenharmony_ci      '--expose-internals',
211cb0ef41Sopenharmony_ci      '--expose-gc',
221cb0ef41Sopenharmony_ci      '--snapshot-blob',
231cb0ef41Sopenharmony_ci      blobPath,
241cb0ef41Sopenharmony_ci      '--build-snapshot',
251cb0ef41Sopenharmony_ci      entry,
261cb0ef41Sopenharmony_ci    ], {
271cb0ef41Sopenharmony_ci      cwd: tmpdir.path
281cb0ef41Sopenharmony_ci    });
291cb0ef41Sopenharmony_ci    if (child.status !== 0) {
301cb0ef41Sopenharmony_ci      console.log(child.stderr.toString());
311cb0ef41Sopenharmony_ci      console.log(child.stdout.toString());
321cb0ef41Sopenharmony_ci      assert.strictEqual(child.status, 0);
331cb0ef41Sopenharmony_ci    }
341cb0ef41Sopenharmony_ci    const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
351cb0ef41Sopenharmony_ci    assert(stats.isFile());
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  {
391cb0ef41Sopenharmony_ci    const child = spawnSync(process.execPath, [
401cb0ef41Sopenharmony_ci      '--expose-internals',
411cb0ef41Sopenharmony_ci      '--expose-gc',
421cb0ef41Sopenharmony_ci      '--snapshot-blob',
431cb0ef41Sopenharmony_ci      blobPath,
441cb0ef41Sopenharmony_ci    ], {
451cb0ef41Sopenharmony_ci      cwd: tmpdir.path,
461cb0ef41Sopenharmony_ci      env: {
471cb0ef41Sopenharmony_ci        ...process.env,
481cb0ef41Sopenharmony_ci      }
491cb0ef41Sopenharmony_ci    });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    const stdout = child.stdout.toString().trim();
521cb0ef41Sopenharmony_ci    const stderr = child.stderr.toString().trim();
531cb0ef41Sopenharmony_ci    console.log(`[stdout]:\n${stdout}\n`);
541cb0ef41Sopenharmony_ci    console.log(`[stderr]:\n${stderr}\n`);
551cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_cirunTest(fixtures.path('snapshot', 'weak-reference.js'));
601cb0ef41Sopenharmony_cirunTest(fixtures.path('snapshot', 'weak-reference-gc.js'));
61