11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst { createContext, runInContext, runInNewContext } = require('vm');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst WASM_BYTES = Buffer.from(
91cb0ef41Sopenharmony_ci  [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  const ctx = createContext({ WASM_BYTES });
131cb0ef41Sopenharmony_ci  const test = 'eval(""); new WebAssembly.Module(WASM_BYTES);';
141cb0ef41Sopenharmony_ci  runInContext(test, ctx);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  runInNewContext(test, { WASM_BYTES }, {
171cb0ef41Sopenharmony_ci    contextCodeGeneration: undefined,
181cb0ef41Sopenharmony_ci  });
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  const ctx = createContext({}, {
231cb0ef41Sopenharmony_ci    codeGeneration: {
241cb0ef41Sopenharmony_ci      strings: false,
251cb0ef41Sopenharmony_ci    },
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const EvalError = runInContext('EvalError', ctx);
291cb0ef41Sopenharmony_ci  assert.throws(() => {
301cb0ef41Sopenharmony_ci    runInContext('eval("x")', ctx);
311cb0ef41Sopenharmony_ci  }, EvalError);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  const ctx = createContext({ WASM_BYTES }, {
361cb0ef41Sopenharmony_ci    codeGeneration: {
371cb0ef41Sopenharmony_ci      wasm: false,
381cb0ef41Sopenharmony_ci    },
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const CompileError = runInContext('WebAssembly.CompileError', ctx);
421cb0ef41Sopenharmony_ci  assert.throws(() => {
431cb0ef41Sopenharmony_ci    runInContext('new WebAssembly.Module(WASM_BYTES)', ctx);
441cb0ef41Sopenharmony_ci  }, CompileError);
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciassert.throws(() => {
481cb0ef41Sopenharmony_ci  runInNewContext('eval("x")', {}, {
491cb0ef41Sopenharmony_ci    contextCodeGeneration: {
501cb0ef41Sopenharmony_ci      strings: false,
511cb0ef41Sopenharmony_ci    },
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci}, {
541cb0ef41Sopenharmony_ci  name: 'EvalError'
551cb0ef41Sopenharmony_ci});
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciassert.throws(() => {
581cb0ef41Sopenharmony_ci  runInNewContext('new WebAssembly.Module(WASM_BYTES)', { WASM_BYTES }, {
591cb0ef41Sopenharmony_ci    contextCodeGeneration: {
601cb0ef41Sopenharmony_ci      wasm: false,
611cb0ef41Sopenharmony_ci    },
621cb0ef41Sopenharmony_ci  });
631cb0ef41Sopenharmony_ci}, {
641cb0ef41Sopenharmony_ci  name: 'CompileError'
651cb0ef41Sopenharmony_ci});
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciassert.throws(() => {
681cb0ef41Sopenharmony_ci  createContext({}, {
691cb0ef41Sopenharmony_ci    codeGeneration: {
701cb0ef41Sopenharmony_ci      strings: 0,
711cb0ef41Sopenharmony_ci    },
721cb0ef41Sopenharmony_ci  });
731cb0ef41Sopenharmony_ci}, {
741cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
751cb0ef41Sopenharmony_ci});
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciassert.throws(() => {
781cb0ef41Sopenharmony_ci  runInNewContext('eval("x")', {}, {
791cb0ef41Sopenharmony_ci    contextCodeGeneration: {
801cb0ef41Sopenharmony_ci      wasm: 1,
811cb0ef41Sopenharmony_ci    },
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci}, {
841cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE'
851cb0ef41Sopenharmony_ci});
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciassert.throws(() => {
881cb0ef41Sopenharmony_ci  createContext({}, {
891cb0ef41Sopenharmony_ci    codeGeneration: 1,
901cb0ef41Sopenharmony_ci  });
911cb0ef41Sopenharmony_ci}, {
921cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
931cb0ef41Sopenharmony_ci});
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciassert.throws(() => {
961cb0ef41Sopenharmony_ci  createContext({}, {
971cb0ef41Sopenharmony_ci    codeGeneration: null,
981cb0ef41Sopenharmony_ci  });
991cb0ef41Sopenharmony_ci}, {
1001cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
1011cb0ef41Sopenharmony_ci});
102