1'use strict'; 2 3require('../common'); 4const assert = require('assert'); 5 6const { createContext, runInContext, runInNewContext } = require('vm'); 7 8const WASM_BYTES = Buffer.from( 9 [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); 10 11{ 12 const ctx = createContext({ WASM_BYTES }); 13 const test = 'eval(""); new WebAssembly.Module(WASM_BYTES);'; 14 runInContext(test, ctx); 15 16 runInNewContext(test, { WASM_BYTES }, { 17 contextCodeGeneration: undefined, 18 }); 19} 20 21{ 22 const ctx = createContext({}, { 23 codeGeneration: { 24 strings: false, 25 }, 26 }); 27 28 const EvalError = runInContext('EvalError', ctx); 29 assert.throws(() => { 30 runInContext('eval("x")', ctx); 31 }, EvalError); 32} 33 34{ 35 const ctx = createContext({ WASM_BYTES }, { 36 codeGeneration: { 37 wasm: false, 38 }, 39 }); 40 41 const CompileError = runInContext('WebAssembly.CompileError', ctx); 42 assert.throws(() => { 43 runInContext('new WebAssembly.Module(WASM_BYTES)', ctx); 44 }, CompileError); 45} 46 47assert.throws(() => { 48 runInNewContext('eval("x")', {}, { 49 contextCodeGeneration: { 50 strings: false, 51 }, 52 }); 53}, { 54 name: 'EvalError' 55}); 56 57assert.throws(() => { 58 runInNewContext('new WebAssembly.Module(WASM_BYTES)', { WASM_BYTES }, { 59 contextCodeGeneration: { 60 wasm: false, 61 }, 62 }); 63}, { 64 name: 'CompileError' 65}); 66 67assert.throws(() => { 68 createContext({}, { 69 codeGeneration: { 70 strings: 0, 71 }, 72 }); 73}, { 74 code: 'ERR_INVALID_ARG_TYPE', 75}); 76 77assert.throws(() => { 78 runInNewContext('eval("x")', {}, { 79 contextCodeGeneration: { 80 wasm: 1, 81 }, 82 }); 83}, { 84 code: 'ERR_INVALID_ARG_TYPE' 85}); 86 87assert.throws(() => { 88 createContext({}, { 89 codeGeneration: 1, 90 }); 91}, { 92 code: 'ERR_INVALID_ARG_TYPE', 93}); 94 95assert.throws(() => { 96 createContext({}, { 97 codeGeneration: null, 98 }); 99}, { 100 code: 'ERR_INVALID_ARG_TYPE', 101}); 102