forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.ts
77 lines (62 loc) · 1.67 KB
/
vm.ts
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
import { createContext, isContext, Script, runInNewContext, runInThisContext, compileFunction, measureMemory, MemoryMeasurement } from 'vm';
import { inspect } from 'util';
{
const sandbox = {
animal: 'cat',
count: 2
};
const context = createContext(sandbox, {
name: 'test',
origin: 'file://test.js',
codeGeneration: {
strings: true,
wasm: true,
},
});
console.log(isContext(context));
const script = new Script('count += 1; name = "kitty"');
for (let i = 0; i < 10; ++i) {
script.runInContext(context);
}
console.log(inspect(sandbox));
runInNewContext('count += 1; name = "kitty"', sandbox);
console.log(inspect(sandbox));
}
{
const sandboxes = [{}, {}, {}];
const script = new Script('globalVar = "set"');
sandboxes.forEach((sandbox) => {
script.runInNewContext(sandbox);
script.runInThisContext();
});
console.log(inspect(sandboxes));
const localVar = 'initial value';
runInThisContext('localVar = "vm";');
console.log(localVar);
}
{
runInThisContext('console.log("hello world"', './my-file.js');
}
{
const fn: Function = compileFunction('console.log("test")', [], {
parsingContext: createContext(),
contextExtensions: [{
a: 1,
}],
produceCachedData: false,
cachedData: Buffer.from('nope'),
});
}
{
const usage = measureMemory({
mode: 'detailed',
context: createContext(),
}).then((data: MemoryMeasurement) => { });
}
{
runInNewContext(
'blah',
{ },
{ timeout: 5, microtaskMode: 'afterEvaluate' }
);
}