summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/vm.ts
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2024-04-09 21:24:25 +0530
committerGitHub <noreply@github.com>2024-04-09 21:24:25 +0530
commitfad12b7c2ebd87a2a11f63998f4c2549fd405eff (patch)
tree38c43f48365d2436dff47d2f91c7e13930932626 /ext/node/polyfills/vm.ts
parenta4f3e7436e81cd229f85657f2fee2caaa0e3b05e (diff)
fix(ext/node): `node:vm` contexts (#23202)
Implement contextified objects in `node:vm` Fixes https://github.com/denoland/deno/issues/23186 Fixes https://github.com/denoland/deno/issues/22395 Fixes https://github.com/denoland/deno/issues/20607 Fixes https://github.com/denoland/deno/issues/18299 Fixes https://github.com/denoland/deno/issues/19395 Fixes https://github.com/denoland/deno/issues/18315 Fixes https://github.com/denoland/deno/issues/18319 Fixes https://github.com/denoland/deno/issues/23183
Diffstat (limited to 'ext/node/polyfills/vm.ts')
-rw-r--r--ext/node/polyfills/vm.ts59
1 files changed, 30 insertions, 29 deletions
diff --git a/ext/node/polyfills/vm.ts b/ext/node/polyfills/vm.ts
index 10000b08c..3378e3886 100644
--- a/ext/node/polyfills/vm.ts
+++ b/ext/node/polyfills/vm.ts
@@ -1,46 +1,48 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-// TODO(petamoriken): enable prefer-primordials for node polyfills
-// deno-lint-ignore-file no-explicit-any prefer-primordials
+// deno-lint-ignore-file no-explicit-any
-import { core } from "ext:core/mod.js";
import { notImplemented } from "ext:deno_node/_utils.ts";
-import { op_vm_run_in_new_context } from "ext:core/ops";
+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 {
- code: string;
+ #inner;
+
constructor(code: string, _options = {}) {
- this.code = `${code}`;
+ this.#inner = op_vm_create_script(code);
}
runInThisContext(_options: any) {
- const [result, error] = core.evalContext(this.code, "data:");
- if (error) {
- throw error.thrown;
- }
- return result;
+ return op_vm_script_run_in_this_context(this.#inner);
}
- runInContext(_contextifiedObject: any, _options: any) {
- notImplemented("Script.prototype.runInContext");
+ runInContext(contextifiedObject: any, _options: any) {
+ return op_vm_script_run_in_context(this.#inner, contextifiedObject);
}
runInNewContext(contextObject: any, options: any) {
- if (options) {
- console.warn(
- "Script.runInNewContext options are currently not supported",
- );
- }
- return op_vm_run_in_new_context(this.code, contextObject);
+ const context = createContext(contextObject);
+ return this.runInContext(context, options);
}
createCachedData() {
- notImplemented("Script.prototyp.createCachedData");
+ notImplemented("Script.prototype.createCachedData");
}
}
-export function createContext(_contextObject: any, _options: any) {
- notImplemented("createContext");
+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) {
@@ -48,11 +50,11 @@ export function createScript(code: string, options: any) {
}
export function runInContext(
- _code: string,
- _contextifiedObject: any,
+ code: string,
+ contextifiedObject: any,
_options: any,
) {
- notImplemented("runInContext");
+ return createScript(code).runInContext(contextifiedObject);
}
export function runInNewContext(
@@ -63,7 +65,7 @@ export function runInNewContext(
if (options) {
console.warn("vm.runInNewContext options are currently not supported");
}
- return op_vm_run_in_new_context(code, contextObject);
+ return createScript(code).runInNewContext(contextObject);
}
export function runInThisContext(
@@ -73,9 +75,8 @@ export function runInThisContext(
return createScript(code, options).runInThisContext(options);
}
-export function isContext(_maybeContext: any) {
- // TODO(@littledivy): Currently we do not expose contexts so this is always false.
- return false;
+export function isContext(maybeContext: any) {
+ return op_vm_is_context(maybeContext);
}
export function compileFunction(_code: string, _params: any, _options: any) {