11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests that Node.js refuses to load snapshots built with incompatible
41cb0ef41Sopenharmony_ci// V8 configurations.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cirequire('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
91cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
101cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
111cb0ef41Sopenharmony_ciconst path = require('path');
121cb0ef41Sopenharmony_ciconst fs = require('fs');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'snapshot.blob');
161cb0ef41Sopenharmony_ciconst entry = fixtures.path('empty.js');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// The flag used can be any flag that makes a difference in
191cb0ef41Sopenharmony_ci// v8::ScriptCompiler::CachedDataVersionTag(). --harmony
201cb0ef41Sopenharmony_ci// is chosen here because it's stable enough and makes a difference.
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  // Build a snapshot with --harmony.
231cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
241cb0ef41Sopenharmony_ci    '--harmony',
251cb0ef41Sopenharmony_ci    '--snapshot-blob',
261cb0ef41Sopenharmony_ci    blobPath,
271cb0ef41Sopenharmony_ci    '--build-snapshot',
281cb0ef41Sopenharmony_ci    entry,
291cb0ef41Sopenharmony_ci  ], {
301cb0ef41Sopenharmony_ci    cwd: tmpdir.path
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci  if (child.status !== 0) {
331cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
341cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
351cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
381cb0ef41Sopenharmony_ci  assert(stats.isFile());
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci{
421cb0ef41Sopenharmony_ci  // Now load the snapshot without --harmony, which should fail.
431cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
441cb0ef41Sopenharmony_ci    '--snapshot-blob',
451cb0ef41Sopenharmony_ci    blobPath,
461cb0ef41Sopenharmony_ci  ], {
471cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
481cb0ef41Sopenharmony_ci    env: {
491cb0ef41Sopenharmony_ci      ...process.env,
501cb0ef41Sopenharmony_ci    }
511cb0ef41Sopenharmony_ci  });
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString().trim();
541cb0ef41Sopenharmony_ci  assert.match(stderr, /Failed to load the startup snapshot/);
551cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  // Load it again with --harmony and it should work.
601cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
611cb0ef41Sopenharmony_ci    '--harmony',
621cb0ef41Sopenharmony_ci    '--snapshot-blob',
631cb0ef41Sopenharmony_ci    blobPath,
641cb0ef41Sopenharmony_ci  ], {
651cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
661cb0ef41Sopenharmony_ci    env: {
671cb0ef41Sopenharmony_ci      ...process.env,
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  if (child.status !== 0) {
721cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
731cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
741cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci}
77