summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/vm.ts
blob: 3378e38862663a094f6fb366d1714ec16b91df23 (plain)
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

// deno-lint-ignore-file no-explicit-any

import { notImplemented } from "ext:deno_node/_utils.ts";
import {
  op_vm_create_context,
  op_vm_create_script,
  op_vm_is_context,
  op_vm_script_run_in_context,
  op_vm_script_run_in_this_context,
} from "ext:core/ops";

export class Script {
  #inner;

  constructor(code: string, _options = {}) {
    this.#inner = op_vm_create_script(code);
  }

  runInThisContext(_options: any) {
    return op_vm_script_run_in_this_context(this.#inner);
  }

  runInContext(contextifiedObject: any, _options: any) {
    return op_vm_script_run_in_context(this.#inner, contextifiedObject);
  }

  runInNewContext(contextObject: any, options: any) {
    const context = createContext(contextObject);
    return this.runInContext(context, options);
  }

  createCachedData() {
    notImplemented("Script.prototype.createCachedData");
  }
}

export function createContext(contextObject: any = {}, _options: any) {
  if (isContext(contextObject)) {
    return contextObject;
  }

  op_vm_create_context(contextObject);
  return contextObject;
}

export function createScript(code: string, options: any) {
  return new Script(code, options);
}

export function runInContext(
  code: string,
  contextifiedObject: any,
  _options: any,
) {
  return createScript(code).runInContext(contextifiedObject);
}

export function runInNewContext(
  code: string,
  contextObject: any,
  options: any,
) {
  if (options) {
    console.warn("vm.runInNewContext options are currently not supported");
  }
  return createScript(code).runInNewContext(contextObject);
}

export function runInThisContext(
  code: string,
  options: any,
) {
  return createScript(code, options).runInThisContext(options);
}

export function isContext(maybeContext: any) {
  return op_vm_is_context(maybeContext);
}

export function compileFunction(_code: string, _params: any, _options: any) {
  notImplemented("compileFunction");
}

export function measureMemory(_options: any) {
  notImplemented("measureMemory");
}

export default {
  Script,
  createContext,
  createScript,
  runInContext,
  runInNewContext,
  runInThisContext,
  isContext,
  compileFunction,
  measureMemory,
};