aboutsummaryrefslogtreecommitdiff
path: root/test/js/node/vm/vm.test.ts
blob: 4e291ac9f99a167f42e9c58bde35ce6d67ba8dcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { describe, test, expect } from "bun:test";
import { createContext, runInContext, runInNewContext, runInThisContext, Script } from "node:vm";

function capture(_: any, _1?: any) {}
describe("runInContext()", () => {
  testRunInContext(runInContext, { isIsolated: true });
});

describe("runInNewContext()", () => {
  testRunInContext(runInNewContext, { isIsolated: true, isNew: true });
});

describe("runInThisContext()", () => {
  testRunInContext(runInThisContext);
});

describe("Script", () => {
  describe("runInContext()", () => {
    testRunInContext(
      (code, context, options) => {
        const script = new Script(code, options);
        return script.runInContext(context);
      },
      { isIsolated: true },
    );
  });
  describe("runInNewContext()", () => {
    testRunInContext(
      (code, context, options) => {
        const script = new Script(code, options);
        return script.runInNewContext(context);
      },
      { isIsolated: true, isNew: true },
    );
  });
  describe("runInThisContext()", () => {
    testRunInContext((code, context, options) => {
      const script = new Script(code, options);
      return script.runInThisContext(context);
    });
  });
});

function testRunInContext(
  fn: typeof runInContext,
  {
    isIsolated,
    isNew,
  }: {
    isIsolated?: boolean;
    isNew?: boolean;
  } = {},
) {
  test("can do nothing", () => {
    const context = createContext({});
    const result = fn("", context);
    expect(result).toBeUndefined();
  });
  test("can return a value", () => {
    const context = createContext({});
    const result = fn("1 + 1;", context);
    expect(result).toBe(2);
  });
  test("can return a complex value", () => {
    const context = createContext({});
    const result = fn("new Set([1, 2, 3]);", context);
    expect(result).toStrictEqual(new Set([1, 2, 3]));
  });
  test("can return the last value", () => {
    const context = createContext({});
    const result = fn("1 + 1; 2 * 2; 3 / 3", context);
    expect(result).toBe(1);
  });

  for (let View of [
    ArrayBuffer,
    SharedArrayBuffer,
    Uint8Array,
    Int8Array,
    Uint16Array,
    Int16Array,
    Uint32Array,
    Int32Array,
    Float32Array,
    Float64Array,
    BigInt64Array,
    BigUint64Array,
  ]) {
    test(`new ${View.name}() in VM context doesn't crash`, () => {
      const context = createContext({});
      expect(fn(`new ${View.name}(2)`, context)).toHaveLength(2);
    });
  }

  test("can return a function", () => {
    const context = createContext({});
    const result = fn("() => 'bar';", context);
    expect(typeof result).toBe("function");
    expect(result()).toBe("bar");
  });
  test("can throw a syntax error", () => {
    const context = createContext({});
    const result = () => fn("!?", context);
    expect(result).toThrow({
      name: "SyntaxError",
      message: "Unexpected token '?'",
    });
  });
  test("can throw an error", () => {
    const context = createContext({});
    const result = () => fn("throw new TypeError('Oops!');", context);
    expect(result).toThrow({
      name: "TypeError",
      message: "Oops!",
    });
  });
  test("can resolve a promise", async () => {
    const context = createContext({});
    const result = fn("Promise.resolve(true);", context);
    expect(await result).toBe(true);
  });
  test("can reject a promise", () => {
    const context = createContext({});
    const result = fn("Promise.reject(new TypeError('Oops!'));", context);
    expect(async () => await result).toThrow({
      name: "TypeError",
      message: "Oops!",
    });
  });
  test("can access the context", () => {
    const context = createContext({
      foo: "bar",
      fizz: (n: number) => "buzz".repeat(n),
    });
    const result = fn("foo + fizz(2);", context);
    expect(result).toBe("barbuzzbuzz");
  });
  test("can modify the context", () => {
    const context = createContext({
      foo: "bar",
      baz: ["a", "b", "c"],
    });
    const result = fn("foo = 'baz'; delete baz[0];", context);
    expect(context.foo).toBe("baz");
    expect(context.baz).toEqual([undefined, "b", "c"]);
    expect(result).toBe(true);
  });
  test("can access `globalThis`", () => {
    const context = createContext({});
    const result = fn("typeof globalThis;", context);
    expect(result).toBe("object");
  });
  test("cannot access local scope", () => {
    var foo = "bar"; // intentionally unused
    capture(foo, foo);
    const context = createContext({});
    const result = fn("typeof foo;", context);
    expect(result).toBe("undefined");
  });
  if (isIsolated) {
    test("cannot access `process`", () => {
      const context = createContext({});
      const result = fn("typeof process;", context);
      expect(result).toBe("undefined");
    });
    test("cannot access this context", () => {
      const prop = randomProp();
      // @ts-expect-error
      globalThis[prop] = "fizz";
      try {
        const context = createContext({});
        const result = fn(`typeof ${prop};`, context);
        expect(result).toBe("undefined");
      } finally {
        // @ts-expect-error
        delete globalThis[prop];
      }
    });
  } else {
    test("can access `process`", () => {
      const context = createContext({});
      const result = fn("typeof process;", context);
      expect(result).toBe("object");
    });
    test("can access this context", () => {
      const prop = randomProp();
      // @ts-expect-error
      globalThis[prop] = "fizz";
      try {
        const context = createContext({});
        const result = fn(`${prop};`, context);
        expect(result).toBe("fizz");
      } finally {
        // @ts-expect-error
        delete globalThis[prop];
      }
    });
    test.skip("can specify an error on SIGINT", () => {
      const context = createContext({});
      const result = () =>
        fn("process.kill(process.pid, 'SIGINT');", context, {
          breakOnSigint: true,
        });
      // TODO: process.kill() is not implemented
      expect(result).toThrow();
    });
  }
  test("can specify a filename", () => {
    const context = createContext({});
    const result = fn("new Error().stack;", context, {
      filename: "foo.js",
    });
    expect(result).toContain("foo.js");
  });
  test.skip("can specify a line offset", () => {
    // TODO: use test.todo
  });
  test.skip("can specify a column offset", () => {
    // TODO: use test.todo
  });
  test.skip("can specify a timeout", () => {
    const context = createContext({});
    const result = () =>
      fn("while (true) {};", context, {
        timeout: 1,
      });
    expect(result).toThrow(); // TODO: does not timeout
  });
}

function randomProp() {
  return "prop" + crypto.randomUUID().replace(/-/g, "");
}