11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst vm = require('vm');
51cb0ef41Sopenharmony_ciconst spawnSync = require('child_process').spawnSync;
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifunction getSource(tag) {
81cb0ef41Sopenharmony_ci  return `(function ${tag}() { return '${tag}'; })`;
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction produce(source, count) {
121cb0ef41Sopenharmony_ci  if (!count)
131cb0ef41Sopenharmony_ci    count = 1;
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const out = spawnSync(process.execPath, [ '-e', `
161cb0ef41Sopenharmony_ci    'use strict';
171cb0ef41Sopenharmony_ci    const assert = require('assert');
181cb0ef41Sopenharmony_ci    const vm = require('vm');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    var data;
211cb0ef41Sopenharmony_ci    for (var i = 0; i < ${count}; i++) {
221cb0ef41Sopenharmony_ci      var script = new vm.Script(process.argv[1], {
231cb0ef41Sopenharmony_ci        produceCachedData: true
241cb0ef41Sopenharmony_ci      });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci      assert(!script.cachedDataProduced || script.cachedData instanceof Buffer);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci      if (script.cachedDataProduced)
291cb0ef41Sopenharmony_ci        data = script.cachedData.toString('base64');
301cb0ef41Sopenharmony_ci    }
311cb0ef41Sopenharmony_ci    console.log(data);
321cb0ef41Sopenharmony_ci  `, source]);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  assert.strictEqual(out.status, 0, String(out.stderr));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  return Buffer.from(out.stdout.toString(), 'base64');
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifunction testProduceConsume() {
401cb0ef41Sopenharmony_ci  const source = getSource('original');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  const data = produce(source);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  for (const cachedData of common.getArrayBufferViews(data)) {
451cb0ef41Sopenharmony_ci    // It should consume code cache
461cb0ef41Sopenharmony_ci    const script = new vm.Script(source, {
471cb0ef41Sopenharmony_ci      cachedData
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci    assert(!script.cachedDataRejected);
501cb0ef41Sopenharmony_ci    assert.strictEqual(script.runInThisContext()(), 'original');
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_citestProduceConsume();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cifunction testProduceMultiple() {
561cb0ef41Sopenharmony_ci  const source = getSource('original');
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  produce(source, 3);
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_citestProduceMultiple();
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cifunction testRejectInvalid() {
631cb0ef41Sopenharmony_ci  const source = getSource('invalid');
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  const data = produce(source);
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  // It should reject invalid code cache
681cb0ef41Sopenharmony_ci  const script = new vm.Script(getSource('invalid_1'), {
691cb0ef41Sopenharmony_ci    cachedData: data
701cb0ef41Sopenharmony_ci  });
711cb0ef41Sopenharmony_ci  assert(script.cachedDataRejected);
721cb0ef41Sopenharmony_ci  assert.strictEqual(script.runInThisContext()(), 'invalid_1');
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_citestRejectInvalid();
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_cifunction testRejectSlice() {
771cb0ef41Sopenharmony_ci  const source = getSource('slice');
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  const data = produce(source).slice(4);
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  const script = new vm.Script(source, {
821cb0ef41Sopenharmony_ci    cachedData: data
831cb0ef41Sopenharmony_ci  });
841cb0ef41Sopenharmony_ci  assert(script.cachedDataRejected);
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_citestRejectSlice();
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci// It should throw on non-Buffer cachedData
891cb0ef41Sopenharmony_ciassert.throws(() => {
901cb0ef41Sopenharmony_ci  new vm.Script('function abc() {}', {
911cb0ef41Sopenharmony_ci    cachedData: 'ohai'
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci}, {
941cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
951cb0ef41Sopenharmony_ci  name: 'TypeError',
961cb0ef41Sopenharmony_ci  message: /must be an instance of Buffer, TypedArray, or DataView/
971cb0ef41Sopenharmony_ci});
98