summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorsnek <snek@deno.com>2024-08-06 14:52:53 +0200
committerGitHub <noreply@github.com>2024-08-06 12:52:53 +0000
commit897159dc6e1b2319cf2f5f09d8d6cecc0d3175fa (patch)
treecfe4a043d1fc102a4e051b99c7fcbef7b79bbb91 /tests
parentc0e9512b39a4ed3713d1fd9b28469d0edf68f578 (diff)
feat: vm rewrite (#24596)
rewrite vm implementation to increase compat. vm.Module+importModuleDynamically callbacks should be added in a followup.
Diffstat (limited to 'tests')
-rw-r--r--tests/node_compat/config.jsonc49
-rw-r--r--tests/node_compat/runner/TODO.md42
-rw-r--r--tests/node_compat/test.ts31
-rw-r--r--tests/node_compat/test/common/index.js7
-rw-r--r--tests/node_compat/test/parallel/test-vm-access-process-env.js40
-rw-r--r--tests/node_compat/test/parallel/test-vm-attributes-property-not-on-sandbox.js25
-rw-r--r--tests/node_compat/test/parallel/test-vm-codegen.js108
-rw-r--r--tests/node_compat/test/parallel/test-vm-context-async-script.js42
-rw-r--r--tests/node_compat/test/parallel/test-vm-context-property-forwarding.js72
-rw-r--r--tests/node_compat/test/parallel/test-vm-create-and-run-in-context.js57
-rw-r--r--tests/node_compat/test/parallel/test-vm-create-context-accessors.js56
-rw-r--r--tests/node_compat/test/parallel/test-vm-create-context-arg.js47
-rw-r--r--tests/node_compat/test/parallel/test-vm-create-context-circular-reference.js41
-rw-r--r--tests/node_compat/test/parallel/test-vm-createcacheddata.js29
-rw-r--r--tests/node_compat/test/parallel/test-vm-cross-context.js36
-rw-r--r--tests/node_compat/test/parallel/test-vm-data-property-writable.js35
-rw-r--r--tests/node_compat/test/parallel/test-vm-deleting-property.js22
-rw-r--r--tests/node_compat/test/parallel/test-vm-function-declaration.js56
-rw-r--r--tests/node_compat/test/parallel/test-vm-function-redefinition.js18
-rw-r--r--tests/node_compat/test/parallel/test-vm-getters.js31
-rw-r--r--tests/node_compat/test/parallel/test-vm-global-assignment.js22
-rw-r--r--tests/node_compat/test/parallel/test-vm-global-define-property.js54
-rw-r--r--tests/node_compat/test/parallel/test-vm-global-identity.js39
-rw-r--r--tests/node_compat/test/parallel/test-vm-global-setter.js168
-rw-r--r--tests/node_compat/test/parallel/test-vm-harmony-symbols.js44
-rw-r--r--tests/node_compat/test/parallel/test-vm-indexed-properties.js24
-rw-r--r--tests/node_compat/test/parallel/test-vm-inherited_properties.js45
-rw-r--r--tests/node_compat/test/parallel/test-vm-is-context.js53
-rw-r--r--tests/node_compat/test/parallel/test-vm-low-stack-space.js33
-rw-r--r--tests/node_compat/test/parallel/test-vm-new-script-new-context.js114
-rw-r--r--tests/node_compat/test/parallel/test-vm-not-strict.js44
-rw-r--r--tests/node_compat/test/parallel/test-vm-options-validation.js101
-rw-r--r--tests/node_compat/test/parallel/test-vm-parse-abort-on-uncaught-exception.js25
-rw-r--r--tests/node_compat/test/parallel/test-vm-preserves-property.js32
-rw-r--r--tests/node_compat/test/parallel/test-vm-property-not-on-sandbox.js44
-rw-r--r--tests/node_compat/test/parallel/test-vm-proxies.js25
-rw-r--r--tests/node_compat/test/parallel/test-vm-proxy-failure-CP.js22
-rw-r--r--tests/node_compat/test/parallel/test-vm-script-throw-in-tostring.js21
-rw-r--r--tests/node_compat/test/parallel/test-vm-set-property-proxy.js23
-rw-r--r--tests/node_compat/test/parallel/test-vm-set-proto-null-on-globalthis.js20
-rw-r--r--tests/node_compat/test/parallel/test-vm-source-map-url.js34
-rw-r--r--tests/node_compat/test/parallel/test-vm-strict-mode.js21
-rw-r--r--tests/node_compat/test/parallel/test-vm-symbols.js30
-rw-r--r--tests/node_compat/test/parallel/test-vm-timeout-escape-promise-2.js45
-rw-r--r--tests/node_compat/test/parallel/test-vm-timeout-escape-promise.js46
-rw-r--r--tests/node_compat/test/parallel/test-vm-timeout.js88
-rw-r--r--tests/unit_node/vm_test.ts2
47 files changed, 2006 insertions, 57 deletions
diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc
index d4953075a..72d872812 100644
--- a/tests/node_compat/config.jsonc
+++ b/tests/node_compat/config.jsonc
@@ -669,8 +669,50 @@
"test-util-types-exists.js",
"test-util-types.js",
"test-util.js",
+ "test-vm-access-process-env.js",
+ "test-vm-attributes-property-not-on-sandbox.js",
+ "test-vm-codegen.js",
+ "test-vm-context-async-script.js",
+ "test-vm-context-property-forwarding.js",
+ "test-vm-create-and-run-in-context.js",
+ "test-vm-create-context-accessors.js",
+ "test-vm-create-context-arg.js",
+ "test-vm-create-context-circular-reference.js",
+ "test-vm-createcacheddata.js",
+ "test-vm-cross-context.js",
+ "test-vm-data-property-writable.js",
+ "test-vm-deleting-property.js",
+ "test-vm-function-declaration.js",
+ "test-vm-function-redefinition.js",
+ "test-vm-getters.js",
+ "test-vm-global-assignment.js",
+ "test-vm-global-define-property.js",
+ "test-vm-global-identity.js",
+ "test-vm-global-setter.js",
+ "test-vm-harmony-symbols.js",
+ "test-vm-indexed-properties.js",
+ "test-vm-inherited_properties.js",
+ "test-vm-is-context.js",
+ "test-vm-low-stack-space.js",
+ "test-vm-new-script-new-context.js",
"test-vm-new-script-this-context.js",
+ "test-vm-not-strict.js",
+ "test-vm-options-validation.js",
+ "test-vm-parse-abort-on-uncaught-exception.js",
+ "test-vm-preserves-property.js",
+ "test-vm-property-not-on-sandbox.js",
+ "test-vm-proxies.js",
+ "test-vm-proxy-failure-CP.js",
+ "test-vm-script-throw-in-tostring.js",
+ "test-vm-set-property-proxy.js",
+ "test-vm-set-proto-null-on-globalthis.js",
+ "test-vm-source-map-url.js",
"test-vm-static-this.js",
+ "test-vm-strict-mode.js",
+ "test-vm-symbols.js",
+ "test-vm-timeout-escape-promise-2.js",
+ "test-vm-timeout-escape-promise.js",
+ "test-vm-timeout.js",
"test-webcrypto-sign-verify.js",
"test-whatwg-encoding-custom-api-basics.js",
"test-whatwg-encoding-custom-textdecoder-ignorebom.js",
@@ -716,7 +758,9 @@
"test-tty-stdout-end.js"
],
"pummel": [],
- "sequential": ["test-child-process-exit.js"]
+ "sequential": [
+ "test-child-process-exit.js"
+ ]
},
"windowsIgnore": {
"parallel": [
@@ -744,7 +788,8 @@
"test-net-server-listen-path.js",
"test-net-socket-close-after-end.js",
"test-util-inspect-long-running.js",
- "test-util-inspect.js"
+ "test-util-inspect.js",
+ "test-vm-low-stack-space.js"
]
},
"darwinIgnore": {
diff --git a/tests/node_compat/runner/TODO.md b/tests/node_compat/runner/TODO.md
index e49100981..1656729e2 100644
--- a/tests/node_compat/runner/TODO.md
+++ b/tests/node_compat/runner/TODO.md
@@ -2622,39 +2622,14 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
- [parallel/test-v8-version-tag.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-v8-version-tag.js)
- [parallel/test-validators.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-validators.js)
- [parallel/test-vfs.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vfs.js)
-- [parallel/test-vm-access-process-env.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-access-process-env.js)
- [parallel/test-vm-api-handles-getter-errors.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-api-handles-getter-errors.js)
-- [parallel/test-vm-attributes-property-not-on-sandbox.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-attributes-property-not-on-sandbox.js)
- [parallel/test-vm-basic.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-basic.js)
- [parallel/test-vm-cached-data.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-cached-data.js)
-- [parallel/test-vm-codegen.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-codegen.js)
-- [parallel/test-vm-context-async-script.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-context-async-script.js)
-- [parallel/test-vm-context-property-forwarding.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-context-property-forwarding.js)
- [parallel/test-vm-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-context.js)
-- [parallel/test-vm-create-and-run-in-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-create-and-run-in-context.js)
-- [parallel/test-vm-create-context-accessors.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-create-context-accessors.js)
-- [parallel/test-vm-create-context-arg.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-create-context-arg.js)
-- [parallel/test-vm-create-context-circular-reference.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-create-context-circular-reference.js)
-- [parallel/test-vm-createcacheddata.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-createcacheddata.js)
-- [parallel/test-vm-cross-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-cross-context.js)
-- [parallel/test-vm-data-property-writable.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-data-property-writable.js)
-- [parallel/test-vm-deleting-property.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-deleting-property.js)
- [parallel/test-vm-dynamic-import-callback-missing-flag.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-dynamic-import-callback-missing-flag.js)
-- [parallel/test-vm-function-declaration.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-function-declaration.js)
-- [parallel/test-vm-function-redefinition.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-function-redefinition.js)
-- [parallel/test-vm-getters.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-getters.js)
-- [parallel/test-vm-global-assignment.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-assignment.js)
-- [parallel/test-vm-global-define-property.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-define-property.js)
- [parallel/test-vm-global-get-own.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-get-own.js)
-- [parallel/test-vm-global-identity.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-identity.js)
- [parallel/test-vm-global-non-writable-properties.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-non-writable-properties.js)
- [parallel/test-vm-global-property-interceptors.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-property-interceptors.js)
-- [parallel/test-vm-global-setter.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-global-setter.js)
-- [parallel/test-vm-harmony-symbols.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-harmony-symbols.js)
-- [parallel/test-vm-indexed-properties.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-indexed-properties.js)
-- [parallel/test-vm-inherited_properties.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-inherited_properties.js)
-- [parallel/test-vm-is-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-is-context.js)
-- [parallel/test-vm-low-stack-space.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-low-stack-space.js)
- [parallel/test-vm-measure-memory-lazy.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-measure-memory-lazy.js)
- [parallel/test-vm-measure-memory-multi-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-measure-memory-multi-context.js)
- [parallel/test-vm-measure-memory.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-measure-memory.js)
@@ -2667,31 +2642,14 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
- [parallel/test-vm-module-link.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-module-link.js)
- [parallel/test-vm-module-reevaluate.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-module-reevaluate.js)
- [parallel/test-vm-module-synthetic.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-module-synthetic.js)
-- [parallel/test-vm-new-script-new-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-new-script-new-context.js)
- [parallel/test-vm-no-dynamic-import-callback.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-no-dynamic-import-callback.js)
-- [parallel/test-vm-not-strict.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-not-strict.js)
-- [parallel/test-vm-options-validation.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-options-validation.js)
-- [parallel/test-vm-parse-abort-on-uncaught-exception.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-parse-abort-on-uncaught-exception.js)
-- [parallel/test-vm-preserves-property.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-preserves-property.js)
-- [parallel/test-vm-property-not-on-sandbox.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-property-not-on-sandbox.js)
-- [parallel/test-vm-proxies.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-proxies.js)
-- [parallel/test-vm-proxy-failure-CP.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-proxy-failure-CP.js)
- [parallel/test-vm-run-in-new-context.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-run-in-new-context.js)
-- [parallel/test-vm-script-throw-in-tostring.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-script-throw-in-tostring.js)
-- [parallel/test-vm-set-property-proxy.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-set-property-proxy.js)
-- [parallel/test-vm-set-proto-null-on-globalthis.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-set-proto-null-on-globalthis.js)
- [parallel/test-vm-sigint-existing-handler.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-sigint-existing-handler.js)
- [parallel/test-vm-sigint.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-sigint.js)
-- [parallel/test-vm-source-map-url.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-source-map-url.js)
- [parallel/test-vm-strict-assign.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-strict-assign.js)
-- [parallel/test-vm-strict-mode.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-strict-mode.js)
-- [parallel/test-vm-symbols.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-symbols.js)
- [parallel/test-vm-syntax-error-message.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-syntax-error-message.js)
- [parallel/test-vm-syntax-error-stderr.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-syntax-error-stderr.js)
-- [parallel/test-vm-timeout-escape-promise-2.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-timeout-escape-promise-2.js)
- [parallel/test-vm-timeout-escape-promise-module.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-timeout-escape-promise-module.js)
-- [parallel/test-vm-timeout-escape-promise.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-timeout-escape-promise.js)
-- [parallel/test-vm-timeout.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-vm-timeout.js)
- [parallel/test-warn-sigprof.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-warn-sigprof.js)
- [parallel/test-warn-stream-wrap.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-warn-stream-wrap.js)
- [parallel/test-weakref.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-weakref.js)
diff --git a/tests/node_compat/test.ts b/tests/node_compat/test.ts
index c9ec1544d..b5c9514a3 100644
--- a/tests/node_compat/test.ts
+++ b/tests/node_compat/test.ts
@@ -45,6 +45,12 @@ const darwinIgnorePaths = new Set(
const decoder = new TextDecoder();
let testSerialId = 0;
+function parseFlags(source: string): string[] {
+ const line = /^\/\/ Flags: (.+)$/um.exec(source);
+ if (line == null) return [];
+ return line[1].split(" ");
+}
+
async function runTest(t: Deno.TestContext, path: string): Promise<void> {
// If filter patterns are given and any pattern doesn't match
// to the file path, then skip the case
@@ -69,14 +75,23 @@ async function runTest(t: Deno.TestContext, path: string): Promise<void> {
const v8Flags = ["--stack-size=4000"];
const testSource = await Deno.readTextFile(testCase);
const envVars: Record<string, string> = {};
- // TODO(kt3k): Parse `Flags` directive correctly
- if (testSource.includes("Flags: --expose_externalize_string")) {
- v8Flags.push("--expose-externalize-string");
- // TODO(bartlomieju): disable verifying globals if that V8 flag is
- // present. Even though we should be able to pass a list of globals
- // that are allowed, it doesn't work, because the list is expected to
- // contain actual JS objects, not strings :)).
- envVars["NODE_TEST_KNOWN_GLOBALS"] = "0";
+ const knownGlobals: string[] = [];
+ parseFlags(testSource).forEach((flag) => {
+ switch (flag) {
+ case "--expose_externalize_string":
+ v8Flags.push("--expose-externalize-string");
+ knownGlobals.push("createExternalizableString");
+ break;
+ case "--expose-gc":
+ v8Flags.push("--expose-gc");
+ knownGlobals.push("gc");
+ break;
+ default:
+ break;
+ }
+ });
+ if (knownGlobals.length > 0) {
+ envVars["NODE_TEST_KNOWN_GLOBALS"] = knownGlobals.join(",");
}
// TODO(nathanwhit): once we match node's behavior on executing
// `node:test` tests when we run a file, we can remove this
diff --git a/tests/node_compat/test/common/index.js b/tests/node_compat/test/common/index.js
index ebac56ac5..d2165aecd 100644
--- a/tests/node_compat/test/common/index.js
+++ b/tests/node_compat/test/common/index.js
@@ -16,7 +16,6 @@ const path = require("path");
const util = require("util");
const tmpdir = require("./tmpdir");
-
function platformTimeout(ms) {
return ms;
}
@@ -90,7 +89,7 @@ function allowGlobals(...allowlist) {
if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
if (process.env.NODE_TEST_KNOWN_GLOBALS) {
- const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
+ const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',').map((name) => global[name]);
allowGlobals(...knownFromEnv);
}
@@ -445,13 +444,13 @@ const pwdCommand = isWindows ?
function spawnPromisified(...args) {
let stderr = '';
let stdout = '';
-
+
const child = spawn(...args);
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => { stderr += data; });
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => { stdout += data; });
-
+
return new Promise((resolve, reject) => {
child.on('close', (code, signal) => {
resolve({
diff --git a/tests/node_compat/test/parallel/test-vm-access-process-env.js b/tests/node_compat/test/parallel/test-vm-access-process-env.js
new file mode 100644
index 000000000..95f555dac
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-access-process-env.js
@@ -0,0 +1,40 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+// Tests that node does neither crash nor throw an error when accessing
+// process.env when inside a VM context.
+// See https://github.com/nodejs/node-v0.x-archive/issues/7511.
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const context = vm.createContext({ process });
+const result = vm.runInContext('process.env["PATH"]', context);
+assert.notStrictEqual(undefined, result);
diff --git a/tests/node_compat/test/parallel/test-vm-attributes-property-not-on-sandbox.js b/tests/node_compat/test/parallel/test-vm-attributes-property-not-on-sandbox.js
new file mode 100644
index 000000000..940fd4e7f
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-attributes-property-not-on-sandbox.js
@@ -0,0 +1,25 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+// Assert that accessor descriptors are not flattened on the sandbox.
+// Issue: https://github.com/nodejs/node/issues/2734
+const sandbox = {};
+vm.createContext(sandbox);
+const code = `Object.defineProperty(
+ this,
+ 'foo',
+ { get: function() {return 17} }
+ );
+ var desc = Object.getOwnPropertyDescriptor(this, 'foo');`;
+
+vm.runInContext(code, sandbox);
+assert.strictEqual(typeof sandbox.desc.get, 'function');
diff --git a/tests/node_compat/test/parallel/test-vm-codegen.js b/tests/node_compat/test/parallel/test-vm-codegen.js
new file mode 100644
index 000000000..fff9c287f
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-codegen.js
@@ -0,0 +1,108 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+const { createContext, runInContext, runInNewContext } = require('vm');
+
+const WASM_BYTES = Buffer.from(
+ [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
+
+{
+ const ctx = createContext({ WASM_BYTES });
+ const test = 'eval(""); new WebAssembly.Module(WASM_BYTES);';
+ runInContext(test, ctx);
+
+ runInNewContext(test, { WASM_BYTES }, {
+ contextCodeGeneration: undefined,
+ });
+}
+
+{
+ const ctx = createContext({}, {
+ codeGeneration: {
+ strings: false,
+ },
+ });
+
+ const EvalError = runInContext('EvalError', ctx);
+ assert.throws(() => {
+ runInContext('eval("x")', ctx);
+ }, EvalError);
+}
+
+{
+ const ctx = createContext({ WASM_BYTES }, {
+ codeGeneration: {
+ wasm: false,
+ },
+ });
+
+ const CompileError = runInContext('WebAssembly.CompileError', ctx);
+ assert.throws(() => {
+ runInContext('new WebAssembly.Module(WASM_BYTES)', ctx);
+ }, CompileError);
+}
+
+assert.throws(() => {
+ runInNewContext('eval("x")', {}, {
+ contextCodeGeneration: {
+ strings: false,
+ },
+ });
+}, {
+ name: 'EvalError'
+});
+
+assert.throws(() => {
+ runInNewContext('new WebAssembly.Module(WASM_BYTES)', { WASM_BYTES }, {
+ contextCodeGeneration: {
+ wasm: false,
+ },
+ });
+}, {
+ name: 'CompileError'
+});
+
+assert.throws(() => {
+ createContext({}, {
+ codeGeneration: {
+ strings: 0,
+ },
+ });
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+});
+
+assert.throws(() => {
+ runInNewContext('eval("x")', {}, {
+ contextCodeGeneration: {
+ wasm: 1,
+ },
+ });
+}, {
+ code: 'ERR_INVALID_ARG_TYPE'
+});
+
+assert.throws(() => {
+ createContext({}, {
+ codeGeneration: 1,
+ });
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+});
+
+assert.throws(() => {
+ createContext({}, {
+ codeGeneration: null,
+ });
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+});
diff --git a/tests/node_compat/test/parallel/test-vm-context-async-script.js b/tests/node_compat/test/parallel/test-vm-context-async-script.js
new file mode 100644
index 000000000..271567ccf
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-context-async-script.js
@@ -0,0 +1,42 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const sandbox = { setTimeout };
+
+const ctx = vm.createContext(sandbox);
+
+vm.runInContext('setTimeout(function() { x = 3; }, 0);', ctx);
+setTimeout(common.mustCall(() => {
+ assert.strictEqual(sandbox.x, 3);
+ assert.strictEqual(ctx.x, 3);
+}), 1);
diff --git a/tests/node_compat/test/parallel/test-vm-context-property-forwarding.js b/tests/node_compat/test/parallel/test-vm-context-property-forwarding.js
new file mode 100644
index 000000000..f50737771
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-context-property-forwarding.js
@@ -0,0 +1,72 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const sandbox = { x: 3 };
+
+const ctx = vm.createContext(sandbox);
+
+assert.strictEqual(vm.runInContext('x;', ctx), 3);
+vm.runInContext('y = 4;', ctx);
+assert.strictEqual(sandbox.y, 4);
+assert.strictEqual(ctx.y, 4);
+
+// Test `IndexedPropertyGetterCallback` and `IndexedPropertyDeleterCallback`
+const x = { get 1() { return 5; } };
+const pd_expected = Object.getOwnPropertyDescriptor(x, 1);
+const ctx2 = vm.createContext(x);
+const pd_actual = Object.getOwnPropertyDescriptor(ctx2, 1);
+
+assert.deepStrictEqual(pd_actual, pd_expected);
+assert.strictEqual(ctx2[1], 5);
+delete ctx2[1];
+assert.strictEqual(ctx2[1], undefined);
+
+// https://github.com/nodejs/node/issues/33806
+{
+ const ctx = vm.createContext();
+
+ Object.defineProperty(ctx, 'prop', {
+ get() {
+ return undefined;
+ },
+ set(val) {
+ throw new Error('test error');
+ },
+ });
+
+ assert.throws(() => {
+ vm.runInContext('prop = 42', ctx);
+ }, {
+ message: 'test error',
+ });
+}
diff --git a/tests/node_compat/test/parallel/test-vm-create-and-run-in-context.js b/tests/node_compat/test/parallel/test-vm-create-and-run-in-context.js
new file mode 100644
index 000000000..0674c0b7b
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-create-and-run-in-context.js
@@ -0,0 +1,57 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+// Flags: --expose-gc
+require('../common');
+const assert = require('assert');
+
+const vm = require('vm');
+
+// Run in a new empty context
+let context = vm.createContext();
+let result = vm.runInContext('"passed";', context);
+assert.strictEqual(result, 'passed');
+
+// Create a new pre-populated context
+context = vm.createContext({ 'foo': 'bar', 'thing': 'lala' });
+assert.strictEqual(context.foo, 'bar');
+assert.strictEqual(context.thing, 'lala');
+
+// Test updating context
+result = vm.runInContext('var foo = 3;', context);
+assert.strictEqual(context.foo, 3);
+assert.strictEqual(context.thing, 'lala');
+
+// https://github.com/nodejs/node/issues/5768
+// Run in contextified sandbox without referencing the context
+const sandbox = { x: 1 };
+vm.createContext(sandbox);
+global.gc();
+vm.runInContext('x = 2', sandbox);
+// Should not crash.
diff --git a/tests/node_compat/test/parallel/test-vm-create-context-accessors.js b/tests/node_compat/test/parallel/test-vm-create-context-accessors.js
new file mode 100644
index 000000000..4b683d687
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-create-context-accessors.js
@@ -0,0 +1,56 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+let ctx = {};
+
+Object.defineProperty(ctx, 'getter', {
+ get: function() {
+ return 'ok';
+ }
+});
+
+let val;
+Object.defineProperty(ctx, 'setter', {
+ set: function(_val) {
+ val = _val;
+ },
+ get: function() {
+ return `ok=${val}`;
+ }
+});
+
+ctx = vm.createContext(ctx);
+
+const result = vm.runInContext('setter = "test";[getter,setter]', ctx);
+assert.strictEqual(result[0], 'ok');
+assert.strictEqual(result[1], 'ok=test');
diff --git a/tests/node_compat/test/parallel/test-vm-create-context-arg.js b/tests/node_compat/test/parallel/test-vm-create-context-arg.js
new file mode 100644
index 000000000..6eb0f7cf9
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-create-context-arg.js
@@ -0,0 +1,47 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+assert.throws(() => {
+ vm.createContext('string is not supported');
+}, {
+ code: 'ERR_INVALID_ARG_TYPE',
+ name: 'TypeError'
+});
+
+// Should not throw.
+vm.createContext({ a: 1 });
+vm.createContext([0, 1, 2, 3]);
+
+const sandbox = {};
+vm.createContext(sandbox);
+vm.createContext(sandbox);
diff --git a/tests/node_compat/test/parallel/test-vm-create-context-circular-reference.js b/tests/node_compat/test/parallel/test-vm-create-context-circular-reference.js
new file mode 100644
index 000000000..95056a3d9
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-create-context-circular-reference.js
@@ -0,0 +1,41 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+let sbx = {};
+sbx.window = sbx;
+
+sbx = vm.createContext(sbx);
+
+sbx.test = 123;
+
+assert.strictEqual(sbx.window.window.window.window.window.test, 123);
diff --git a/tests/node_compat/test/parallel/test-vm-createcacheddata.js b/tests/node_compat/test/parallel/test-vm-createcacheddata.js
new file mode 100644
index 000000000..0e786364d
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-createcacheddata.js
@@ -0,0 +1,29 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+
+const { Script } = require('vm');
+const assert = require('assert');
+
+const source = 'function x() {} const y = x();';
+
+const script = new Script(source);
+let cachedData = script.createCachedData();
+assert(cachedData instanceof Buffer);
+
+assert(!new Script(source, { cachedData }).cachedDataRejected);
+
+script.runInNewContext();
+
+for (let i = 0; i < 10; i += 1) {
+ cachedData = script.createCachedData();
+
+ assert(!new Script(source, { cachedData }).cachedDataRejected);
+}
diff --git a/tests/node_compat/test/parallel/test-vm-cross-context.js b/tests/node_compat/test/parallel/test-vm-cross-context.js
new file mode 100644
index 000000000..3a1f1678e
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-cross-context.js
@@ -0,0 +1,36 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+
+const vm = require('vm');
+const ctx = vm.createContext(global);
+
+// Should not throw.
+vm.runInContext('!function() { var x = console.log; }()', ctx);
diff --git a/tests/node_compat/test/parallel/test-vm-data-property-writable.js b/tests/node_compat/test/parallel/test-vm-data-property-writable.js
new file mode 100644
index 000000000..1ce764801
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-data-property-writable.js
@@ -0,0 +1,35 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// Refs: https://github.com/nodejs/node/issues/10223
+
+require('../common');
+const vm = require('vm');
+const assert = require('assert');
+
+const context = vm.createContext({});
+
+let code = `
+ Object.defineProperty(this, 'foo', {value: 5});
+ Object.getOwnPropertyDescriptor(this, 'foo');
+`;
+
+let desc = vm.runInContext(code, context);
+
+assert.strictEqual(desc.writable, false);
+
+// Check that interceptors work for symbols.
+code = `
+ const bar = Symbol('bar');
+ Object.defineProperty(this, bar, {value: 6});
+ Object.getOwnPropertyDescriptor(this, bar);
+`;
+
+desc = vm.runInContext(code, context);
+
+assert.strictEqual(desc.value, 6);
diff --git a/tests/node_compat/test/parallel/test-vm-deleting-property.js b/tests/node_compat/test/parallel/test-vm-deleting-property.js
new file mode 100644
index 000000000..df5ac859a
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-deleting-property.js
@@ -0,0 +1,22 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// Refs: https://github.com/nodejs/node/issues/6287
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const context = vm.createContext();
+const res = vm.runInContext(`
+ this.x = 'prop';
+ delete this.x;
+ Object.getOwnPropertyDescriptor(this, 'x');
+`, context);
+
+assert.strictEqual(res, undefined);
diff --git a/tests/node_compat/test/parallel/test-vm-function-declaration.js b/tests/node_compat/test/parallel/test-vm-function-declaration.js
new file mode 100644
index 000000000..209720c75
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-function-declaration.js
@@ -0,0 +1,56 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+
+const vm = require('vm');
+const o = vm.createContext({ console });
+
+// Function declaration and expression should both be copied to the
+// sandboxed context.
+let code = 'let a = function() {};\n';
+code += 'function b(){}\n';
+code += 'var c = function() {};\n';
+code += 'var d = () => {};\n';
+code += 'let e = () => {};\n';
+
+// Grab the global b function as the completion value, to ensure that
+// we are getting the global function, and not some other thing
+code += '(function(){return this})().b;\n';
+
+const res = vm.runInContext(code, o, 'test');
+assert.strictEqual(typeof res, 'function');
+assert.strictEqual(res.name, 'b');
+assert.strictEqual(typeof o.a, 'undefined');
+assert.strictEqual(typeof o.b, 'function');
+assert.strictEqual(typeof o.c, 'function');
+assert.strictEqual(typeof o.d, 'function');
+assert.strictEqual(typeof o.e, 'undefined');
+assert.strictEqual(res, o.b);
diff --git a/tests/node_compat/test/parallel/test-vm-function-redefinition.js b/tests/node_compat/test/parallel/test-vm-function-redefinition.js
new file mode 100644
index 000000000..7bc62ac0e
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-function-redefinition.js
@@ -0,0 +1,18 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// Refs: https://github.com/nodejs/node/issues/548
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+const context = vm.createContext();
+
+vm.runInContext('function test() { return 0; }', context);
+vm.runInContext('function test() { return 1; }', context);
+const result = vm.runInContext('test()', context);
+assert.strictEqual(result, 1);
diff --git a/tests/node_compat/test/parallel/test-vm-getters.js b/tests/node_compat/test/parallel/test-vm-getters.js
new file mode 100644
index 000000000..b9c28014a
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-getters.js
@@ -0,0 +1,31 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// Refs: https://github.com/nodejs/node/issues/2734
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+const sandbox = {};
+
+Object.defineProperty(sandbox, 'prop', {
+ get() {
+ return 'foo';
+ }
+});
+
+const descriptor = Object.getOwnPropertyDescriptor(sandbox, 'prop');
+const context = vm.createContext(sandbox);
+const code = 'Object.getOwnPropertyDescriptor(this, "prop");';
+const result = vm.runInContext(code, context);
+
+// Ref: https://github.com/nodejs/node/issues/11803
+
+assert.deepStrictEqual(Object.keys(result), Object.keys(descriptor));
+for (const prop of Object.keys(result)) {
+ assert.strictEqual(result[prop], descriptor[prop]);
+}
diff --git a/tests/node_compat/test/parallel/test-vm-global-assignment.js b/tests/node_compat/test/parallel/test-vm-global-assignment.js
new file mode 100644
index 000000000..c8fc516d6
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-global-assignment.js
@@ -0,0 +1,22 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// Regression test for https://github.com/nodejs/node/issues/10806
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+const ctx = vm.createContext({ open() { } });
+const window = vm.runInContext('this', ctx);
+const other = 123;
+
+assert.notStrictEqual(window.open, other);
+window.open = other;
+assert.strictEqual(window.open, other);
+window.open = other;
+assert.strictEqual(window.open, other);
diff --git a/tests/node_compat/test/parallel/test-vm-global-define-property.js b/tests/node_compat/test/parallel/test-vm-global-define-property.js
new file mode 100644
index 000000000..28f5070c2
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-global-define-property.js
@@ -0,0 +1,54 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+
+const vm = require('vm');
+
+const code =
+ 'Object.defineProperty(this, "f", {\n' +
+ ' get: function() { return x; },\n' +
+ ' set: function(k) { x = k; },\n' +
+ ' configurable: true,\n' +
+ ' enumerable: true\n' +
+ '});\n' +
+ 'g = f;\n' +
+ 'f;\n';
+
+const x = {};
+const o = vm.createContext({ console, x });
+
+const res = vm.runInContext(code, o, 'test');
+
+assert(res);
+assert.strictEqual(typeof res, 'object');
+assert.strictEqual(res, x);
+assert.strictEqual(o.f, res);
+assert.deepStrictEqual(Object.keys(o), ['console', 'x', 'f', 'g']);
diff --git a/tests/node_compat/test/parallel/test-vm-global-identity.js b/tests/node_compat/test/parallel/test-vm-global-identity.js
new file mode 100644
index 000000000..5413ca94a
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-global-identity.js
@@ -0,0 +1,39 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const ctx = vm.createContext();
+ctx.window = ctx;
+
+const thisVal = vm.runInContext('this;', ctx);
+const windowVal = vm.runInContext('window;', ctx);
+assert.strictEqual(thisVal, windowVal);
diff --git a/tests/node_compat/test/parallel/test-vm-global-setter.js b/tests/node_compat/test/parallel/test-vm-global-setter.js
new file mode 100644
index 000000000..8f1f862f3
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-global-setter.js
@@ -0,0 +1,168 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const getSetSymbolReceivingFunction = Symbol('sym-1');
+const getSetSymbolReceivingNumber = Symbol('sym-2');
+const symbolReceivingNumber = Symbol('sym-3');
+const unknownSymbolReceivingNumber = Symbol('sym-4');
+
+const window = createWindow();
+
+const descriptor1 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ 'getSetPropReceivingFunction'
+);
+assert.strictEqual(typeof descriptor1.get, 'function');
+assert.strictEqual(typeof descriptor1.set, 'function');
+assert.strictEqual(descriptor1.configurable, true);
+
+const descriptor2 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ 'getSetPropReceivingNumber'
+);
+assert.strictEqual(typeof descriptor2.get, 'function');
+assert.strictEqual(typeof descriptor2.set, 'function');
+assert.strictEqual(descriptor2.configurable, true);
+
+const descriptor3 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ 'propReceivingNumber'
+);
+assert.strictEqual(descriptor3.value, 44);
+
+const descriptor4 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ 'unknownPropReceivingNumber'
+);
+assert.strictEqual(descriptor4, undefined);
+
+const descriptor5 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ getSetSymbolReceivingFunction
+);
+assert.strictEqual(typeof descriptor5.get, 'function');
+assert.strictEqual(typeof descriptor5.set, 'function');
+assert.strictEqual(descriptor5.configurable, true);
+
+const descriptor6 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ getSetSymbolReceivingNumber
+);
+assert.strictEqual(typeof descriptor6.get, 'function');
+assert.strictEqual(typeof descriptor6.set, 'function');
+assert.strictEqual(descriptor6.configurable, true);
+
+const descriptor7 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ symbolReceivingNumber
+);
+assert.strictEqual(descriptor7.value, 48);
+
+const descriptor8 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ unknownSymbolReceivingNumber
+);
+assert.strictEqual(descriptor8, undefined);
+
+const descriptor9 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ 'getSetPropThrowing'
+);
+assert.strictEqual(typeof descriptor9.get, 'function');
+assert.strictEqual(typeof descriptor9.set, 'function');
+assert.strictEqual(descriptor9.configurable, true);
+
+const descriptor10 = Object.getOwnPropertyDescriptor(
+ window.globalProxy,
+ 'nonWritableProp'
+);
+assert.strictEqual(descriptor10.value, 51);
+assert.strictEqual(descriptor10.writable, false);
+
+// Regression test for GH-42962. This assignment should not throw.
+window.globalProxy.getSetPropReceivingFunction = () => {};
+assert.strictEqual(window.globalProxy.getSetPropReceivingFunction, 42);
+
+window.globalProxy.getSetPropReceivingNumber = 143;
+assert.strictEqual(window.globalProxy.getSetPropReceivingNumber, 43);
+
+window.globalProxy.propReceivingNumber = 144;
+assert.strictEqual(window.globalProxy.propReceivingNumber, 144);
+
+window.globalProxy.unknownPropReceivingNumber = 145;
+assert.strictEqual(window.globalProxy.unknownPropReceivingNumber, 145);
+
+window.globalProxy[getSetSymbolReceivingFunction] = () => {};
+assert.strictEqual(window.globalProxy[getSetSymbolReceivingFunction], 46);
+
+window.globalProxy[getSetSymbolReceivingNumber] = 147;
+assert.strictEqual(window.globalProxy[getSetSymbolReceivingNumber], 47);
+
+window.globalProxy[symbolReceivingNumber] = 148;
+assert.strictEqual(window.globalProxy[symbolReceivingNumber], 148);
+
+window.globalProxy[unknownSymbolReceivingNumber] = 149;
+assert.strictEqual(window.globalProxy[unknownSymbolReceivingNumber], 149);
+
+assert.throws(
+ () => (window.globalProxy.getSetPropThrowing = 150),
+ new Error('setter called')
+);
+assert.strictEqual(window.globalProxy.getSetPropThrowing, 50);
+
+assert.throws(
+ () => (window.globalProxy.nonWritableProp = 151),
+ new TypeError('Cannot redefine property: nonWritableProp')
+);
+assert.strictEqual(window.globalProxy.nonWritableProp, 51);
+
+function createWindow() {
+ const obj = {};
+ vm.createContext(obj);
+ Object.defineProperty(obj, 'getSetPropReceivingFunction', {
+ get: common.mustCall(() => 42),
+ set: common.mustCall(),
+ configurable: true,
+ });
+ Object.defineProperty(obj, 'getSetPropReceivingNumber', {
+ get: common.mustCall(() => 43),
+ set: common.mustCall(),
+ configurable: true,
+ });
+ obj.propReceivingNumber = 44;
+ Object.defineProperty(obj, getSetSymbolReceivingFunction, {
+ get: common.mustCall(() => 46),
+ set: common.mustCall(),
+ configurable: true,
+ });
+ Object.defineProperty(obj, getSetSymbolReceivingNumber, {
+ get: common.mustCall(() => 47),
+ set: common.mustCall(),
+ configurable: true,
+ });
+ obj[symbolReceivingNumber] = 48;
+ Object.defineProperty(obj, 'getSetPropThrowing', {
+ get: common.mustCall(() => 50),
+ set: common.mustCall(() => {
+ throw new Error('setter called');
+ }),
+ configurable: true,
+ });
+ Object.defineProperty(obj, 'nonWritableProp', {
+ value: 51,
+ writable: false,
+ });
+
+ obj.globalProxy = vm.runInContext('this', obj);
+
+ return obj;
+}
diff --git a/tests/node_compat/test/parallel/test-vm-harmony-symbols.js b/tests/node_compat/test/parallel/test-vm-harmony-symbols.js
new file mode 100644
index 000000000..d4713a2e2
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-harmony-symbols.js
@@ -0,0 +1,44 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+// The sandbox should have its own Symbol constructor.
+let sandbox = {};
+vm.runInNewContext('this.Symbol = Symbol', sandbox);
+assert.strictEqual(typeof sandbox.Symbol, 'function');
+assert.notStrictEqual(sandbox.Symbol, Symbol);
+
+// Unless we copy the Symbol constructor explicitly, of course.
+sandbox = { Symbol };
+vm.runInNewContext('this.Symbol = Symbol', sandbox);
+assert.strictEqual(typeof sandbox.Symbol, 'function');
+assert.strictEqual(sandbox.Symbol, Symbol);
diff --git a/tests/node_compat/test/parallel/test-vm-indexed-properties.js b/tests/node_compat/test/parallel/test-vm-indexed-properties.js
new file mode 100644
index 000000000..332905ff7
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-indexed-properties.js
@@ -0,0 +1,24 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const code = `Object.defineProperty(this, 99, {
+ value: 20,
+ enumerable: true
+ });`;
+
+
+const sandbox = {};
+const ctx = vm.createContext(sandbox);
+vm.runInContext(code, ctx);
+
+assert.strictEqual(sandbox[99], 20);
diff --git a/tests/node_compat/test/parallel/test-vm-inherited_properties.js b/tests/node_compat/test/parallel/test-vm-inherited_properties.js
new file mode 100644
index 000000000..4994a8cea
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-inherited_properties.js
@@ -0,0 +1,45 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+
+const vm = require('vm');
+const assert = require('assert');
+
+let base = {
+ propBase: 1
+};
+
+let sandbox = Object.create(base, {
+ propSandbox: { value: 3 }
+});
+
+const context = vm.createContext(sandbox);
+
+let result = vm.runInContext('Object.hasOwnProperty(this, "propBase");',
+ context);
+
+assert.strictEqual(result, false);
+
+// Ref: https://github.com/nodejs/node/issues/5350
+base = { __proto__: null };
+base.x = 1;
+base.y = 2;
+
+sandbox = { __proto__: base };
+sandbox.z = 3;
+
+assert.deepStrictEqual(Object.keys(sandbox), ['z']);
+
+const code = 'x = 0; z = 4;';
+result = vm.runInNewContext(code, sandbox);
+assert.strictEqual(result, 4);
+
+// Check that y is not an own property.
+assert.deepStrictEqual(Object.keys(sandbox), ['z', 'x']);
diff --git a/tests/node_compat/test/parallel/test-vm-is-context.js b/tests/node_compat/test/parallel/test-vm-is-context.js
new file mode 100644
index 000000000..fdbaa36d6
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-is-context.js
@@ -0,0 +1,53 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+for (const valToTest of [
+ 'string', null, undefined, 8.9, Symbol('sym'), true,
+]) {
+ assert.throws(() => {
+ vm.isContext(valToTest);
+ }, {
+ code: 'ERR_INVALID_ARG_TYPE',
+ name: 'TypeError'
+ });
+}
+
+assert.strictEqual(vm.isContext({}), false);
+assert.strictEqual(vm.isContext([]), false);
+
+assert.strictEqual(vm.isContext(vm.createContext()), true);
+assert.strictEqual(vm.isContext(vm.createContext([])), true);
+
+const sandbox = { foo: 'bar' };
+vm.createContext(sandbox);
+assert.strictEqual(vm.isContext(sandbox), true);
diff --git a/tests/node_compat/test/parallel/test-vm-low-stack-space.js b/tests/node_compat/test/parallel/test-vm-low-stack-space.js
new file mode 100644
index 000000000..d10137724
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-low-stack-space.js
@@ -0,0 +1,33 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+function a() {
+ try {
+ return a();
+ } catch {
+ // Throw an exception as near to the recursion-based RangeError as possible.
+ return vm.runInThisContext('() => 42')();
+ }
+}
+
+assert.strictEqual(a(), 42);
+
+function b() {
+ try {
+ return b();
+ } catch {
+ // This writes a lot of noise to stderr, but it still works.
+ return vm.runInNewContext('() => 42')();
+ }
+}
+
+assert.strictEqual(b(), 42);
diff --git a/tests/node_compat/test/parallel/test-vm-new-script-new-context.js b/tests/node_compat/test/parallel/test-vm-new-script-new-context.js
new file mode 100644
index 000000000..aada16262
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-new-script-new-context.js
@@ -0,0 +1,114 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+
+const assert = require('assert');
+
+const Script = require('vm').Script;
+
+{
+ const script = new Script('\'passed\';');
+ const result1 = script.runInNewContext();
+ const result2 = script.runInNewContext();
+ assert.strictEqual(result1, 'passed');
+ assert.strictEqual(result2, 'passed');
+}
+
+{
+ const script = new Script('throw new Error(\'test\');');
+ assert.throws(() => {
+ script.runInNewContext();
+ }, /^Error: test$/);
+}
+
+{
+ const script = new Script('foo.bar = 5;');
+ assert.throws(() => {
+ script.runInNewContext();
+ }, /^ReferenceError: foo is not defined$/);
+}
+
+{
+ global.hello = 5;
+ const script = new Script('hello = 2');
+ script.runInNewContext();
+ assert.strictEqual(global.hello, 5);
+
+ // Cleanup
+ delete global.hello;
+}
+
+{
+ global.code = 'foo = 1;' +
+ 'bar = 2;' +
+ 'if (baz !== 3) throw new Error(\'test fail\');';
+ global.foo = 2;
+ global.obj = { foo: 0, baz: 3 };
+ const script = new Script(global.code);
+ /* eslint-disable no-unused-vars */
+ const baz = script.runInNewContext(global.obj);
+ /* eslint-enable no-unused-vars */
+ assert.strictEqual(global.obj.foo, 1);
+ assert.strictEqual(global.obj.bar, 2);
+ assert.strictEqual(global.foo, 2);
+
+ // cleanup
+ delete global.code;
+ delete global.foo;
+ delete global.obj;
+}
+
+{
+ const script = new Script('f()');
+ function changeFoo() { global.foo = 100; }
+ script.runInNewContext({ f: changeFoo });
+ assert.strictEqual(global.foo, 100);
+
+ // cleanup
+ delete global.foo;
+}
+
+{
+ const script = new Script('f.a = 2');
+ const f = { a: 1 };
+ script.runInNewContext({ f });
+ assert.strictEqual(f.a, 2);
+
+ assert.throws(() => {
+ script.runInNewContext();
+ }, /^ReferenceError: f is not defined$/);
+}
+
+{
+ const script = new Script('');
+ assert.throws(() => {
+ script.runInNewContext.call('\'hello\';');
+ }, /^TypeError: this\.runInContext is not a function$/);
+}
diff --git a/tests/node_compat/test/parallel/test-vm-not-strict.js b/tests/node_compat/test/parallel/test-vm-not-strict.js
new file mode 100644
index 000000000..c5dee3a2c
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-not-strict.js
@@ -0,0 +1,44 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+/* eslint-disable strict, no-var, no-delete-var, no-undef, node-core/required-modules, node-core/require-common-first */
+// Importing common would break the execution. Indeed running `vm.runInThisContext` alters the global context
+// when declaring new variables with `var`. The other rules (strict, no-var, no-delete-var) have been disabled
+// in order to be able to test this specific not-strict case playing with `var` and `delete`.
+// Related to bug report: https://github.com/nodejs/node/issues/43129
+var assert = require('assert');
+var vm = require('vm');
+
+var data = [];
+var a = 'direct';
+delete a;
+data.push(a);
+
+var item2 = vm.runInThisContext(`
+var unusedB = 1;
+var data = [];
+var b = "this";
+delete b;
+data.push(b);
+data[0]
+`);
+data.push(item2);
+
+vm.runInContext(
+ `
+var unusedC = 1;
+var c = "new";
+delete c;
+data.push(c);
+`,
+ vm.createContext({ data: data })
+);
+
+assert.deepStrictEqual(data, ['direct', 'this', 'new']);
+
+assert.strictEqual(typeof unusedB, 'number'); // Declared within runInThisContext
+assert.strictEqual(typeof unusedC, 'undefined'); // Declared within runInContext
diff --git a/tests/node_compat/test/parallel/test-vm-options-validation.js b/tests/node_compat/test/parallel/test-vm-options-validation.js
new file mode 100644
index 000000000..d1b215ed7
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-options-validation.js
@@ -0,0 +1,101 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const invalidArgType = {
+ name: 'TypeError',
+ code: 'ERR_INVALID_ARG_TYPE'
+};
+
+const outOfRange = {
+ name: 'RangeError',
+ code: 'ERR_OUT_OF_RANGE'
+};
+
+assert.throws(() => {
+ new vm.Script('void 0', 42);
+}, invalidArgType);
+
+[null, {}, [1], 'bad', true].forEach((value) => {
+ assert.throws(() => {
+ new vm.Script('void 0', { lineOffset: value });
+ }, invalidArgType);
+
+ assert.throws(() => {
+ new vm.Script('void 0', { columnOffset: value });
+ }, invalidArgType);
+});
+
+[0.1, 2 ** 32].forEach((value) => {
+ assert.throws(() => {
+ new vm.Script('void 0', { lineOffset: value });
+ }, outOfRange);
+
+ assert.throws(() => {
+ new vm.Script('void 0', { columnOffset: value });
+ }, outOfRange);
+});
+
+assert.throws(() => {
+ new vm.Script('void 0', { lineOffset: Number.MAX_SAFE_INTEGER });
+}, outOfRange);
+
+assert.throws(() => {
+ new vm.Script('void 0', { columnOffset: Number.MAX_SAFE_INTEGER });
+}, outOfRange);
+
+assert.throws(() => {
+ new vm.Script('void 0', { filename: 123 });
+}, invalidArgType);
+
+assert.throws(() => {
+ new vm.Script('void 0', { produceCachedData: 1 });
+}, invalidArgType);
+
+[[0], {}, true, 'bad', 42].forEach((value) => {
+ assert.throws(() => {
+ new vm.Script('void 0', { cachedData: value });
+ }, invalidArgType);
+});
+
+{
+ const script = new vm.Script('void 0');
+ const sandbox = vm.createContext();
+
+ function assertErrors(options, errCheck) {
+ assert.throws(() => {
+ script.runInThisContext(options);
+ }, errCheck);
+
+ assert.throws(() => {
+ script.runInContext(sandbox, options);
+ }, errCheck);
+
+ assert.throws(() => {
+ script.runInNewContext({}, options);
+ }, errCheck);
+ }
+
+ [null, 'bad', 42].forEach((value) => {
+ assertErrors(value, invalidArgType);
+ });
+ [{}, [1], 'bad', null].forEach((value) => {
+ assertErrors({ timeout: value }, invalidArgType);
+ });
+ [-1, 0, NaN].forEach((value) => {
+ assertErrors({ timeout: value }, outOfRange);
+ });
+ [{}, [1], 'bad', 1, null].forEach((value) => {
+ assertErrors({ displayErrors: value }, invalidArgType);
+ assertErrors({ breakOnSigint: value }, invalidArgType);
+ });
+}
diff --git a/tests/node_compat/test/parallel/test-vm-parse-abort-on-uncaught-exception.js b/tests/node_compat/test/parallel/test-vm-parse-abort-on-uncaught-exception.js
new file mode 100644
index 000000000..e8cae690a
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-parse-abort-on-uncaught-exception.js
@@ -0,0 +1,25 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Flags: --abort-on-uncaught-exception
+'use strict';
+require('../common');
+const vm = require('vm');
+
+// Regression test for https://github.com/nodejs/node/issues/13258
+
+try {
+ new vm.Script({ toString() { throw new Error('foo'); } }, {});
+} catch {
+ // Continue regardless of error.
+}
+
+try {
+ new vm.Script('[', {});
+} catch {
+ // Continue regardless of error.
+}
diff --git a/tests/node_compat/test/parallel/test-vm-preserves-property.js b/tests/node_compat/test/parallel/test-vm-preserves-property.js
new file mode 100644
index 000000000..28f662a1a
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-preserves-property.js
@@ -0,0 +1,32 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+const vm = require('vm');
+
+const x = {};
+Object.defineProperty(x, 'prop', {
+ configurable: false,
+ enumerable: false,
+ writable: false,
+ value: 'val'
+});
+const o = vm.createContext(x);
+
+const code = 'Object.getOwnPropertyDescriptor(this, "prop")';
+const res = vm.runInContext(code, o, 'test');
+
+assert(res);
+assert.strictEqual(typeof res, 'object');
+assert.strictEqual(res.value, 'val');
+assert.strictEqual(res.configurable, false);
+assert.strictEqual(res.enumerable, false);
+assert.strictEqual(res.writable, false);
diff --git a/tests/node_compat/test/parallel/test-vm-property-not-on-sandbox.js b/tests/node_compat/test/parallel/test-vm-property-not-on-sandbox.js
new file mode 100644
index 000000000..360a5dabf
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-property-not-on-sandbox.js
@@ -0,0 +1,44 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+// This, admittedly contrived, example tests an edge cases of the vm module.
+//
+// The GetterCallback explicitly checks the global_proxy() if a property is
+// not found on the sandbox. In the following test, the explicit check
+// inside the callback yields different results than deferring the
+// check until after the callback. The check is deferred if the
+// callback does not intercept, i.e., if args.GetReturnValue().Set() is
+// not called.
+
+// Check that the GetterCallback explicitly calls GetRealNamedProperty()
+// on the global proxy if the property is not found on the sandbox.
+//
+// foo is not defined on the sandbox until we call CopyProperties().
+// In the GetterCallback, we do not find the property on the sandbox and
+// get the property from the global proxy. Since the return value is
+// the sandbox, we replace it by
+// the global_proxy to keep the correct identities.
+//
+// This test case is partially inspired by
+// https://github.com/nodejs/node/issues/855
+const sandbox = { console };
+sandbox.document = { defaultView: sandbox };
+vm.createContext(sandbox);
+const code = `Object.defineProperty(
+ this,
+ 'foo',
+ { get: function() {return document.defaultView} }
+ );
+ var result = foo === this;`;
+
+vm.runInContext(code, sandbox);
+assert.strictEqual(sandbox.result, true);
diff --git a/tests/node_compat/test/parallel/test-vm-proxies.js b/tests/node_compat/test/parallel/test-vm-proxies.js
new file mode 100644
index 000000000..c485e0a62
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-proxies.js
@@ -0,0 +1,25 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+// src/node_contextify.cc filters out the Proxy object from the parent
+// context. Make sure that the new context has a Proxy object of its own.
+let sandbox = {};
+vm.runInNewContext('this.Proxy = Proxy', sandbox);
+assert.strictEqual(typeof sandbox.Proxy, 'function');
+assert.notStrictEqual(sandbox.Proxy, Proxy);
+
+// Unless we copy the Proxy object explicitly, of course.
+sandbox = { Proxy };
+vm.runInNewContext('this.Proxy = Proxy', sandbox);
+assert.strictEqual(typeof sandbox.Proxy, 'function');
+assert.strictEqual(sandbox.Proxy, Proxy);
diff --git a/tests/node_compat/test/parallel/test-vm-proxy-failure-CP.js b/tests/node_compat/test/parallel/test-vm-proxy-failure-CP.js
new file mode 100644
index 000000000..2f503bd31
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-proxy-failure-CP.js
@@ -0,0 +1,22 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+require('../common');
+const vm = require('vm');
+
+// Check that we do not accidentally query attributes.
+// Issue: https://github.com/nodejs/node/issues/11902
+const handler = {
+ getOwnPropertyDescriptor: (target, prop) => {
+ throw new Error('whoops');
+ }
+};
+const sandbox = new Proxy({ foo: 'bar' }, handler);
+const context = vm.createContext(sandbox);
+
+vm.runInContext('', context);
diff --git a/tests/node_compat/test/parallel/test-vm-script-throw-in-tostring.js b/tests/node_compat/test/parallel/test-vm-script-throw-in-tostring.js
new file mode 100644
index 000000000..c13573086
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-script-throw-in-tostring.js
@@ -0,0 +1,21 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+const vm = require('vm');
+
+assert.throws(() => {
+ new vm.Script({
+ toString() {
+ throw new Error();
+ }
+ });
+}, Error);
diff --git a/tests/node_compat/test/parallel/test-vm-set-property-proxy.js b/tests/node_compat/test/parallel/test-vm-set-property-proxy.js
new file mode 100644
index 000000000..61f80902c
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-set-property-proxy.js
@@ -0,0 +1,23 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+const common = require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+// Regression test for https://github.com/nodejs/node/issues/34606
+
+const handler = {
+ getOwnPropertyDescriptor: common.mustCallAtLeast(() => {
+ return {};
+ })
+};
+
+const proxy = new Proxy({}, handler);
+assert.throws(() => vm.runInNewContext('p = 6', proxy),
+ /getOwnPropertyDescriptor/);
diff --git a/tests/node_compat/test/parallel/test-vm-set-proto-null-on-globalthis.js b/tests/node_compat/test/parallel/test-vm-set-proto-null-on-globalthis.js
new file mode 100644
index 000000000..779864668
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-set-proto-null-on-globalthis.js
@@ -0,0 +1,20 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+require('../common');
+
+// Setting __proto__ on vm context's globalThis should not cause a crash
+// Regression test for https://github.com/nodejs/node/issues/47798
+
+const vm = require('vm');
+const context = vm.createContext();
+
+const contextGlobalThis = vm.runInContext('this', context);
+
+// Should not crash.
+contextGlobalThis.__proto__ = null; // eslint-disable-line no-proto
diff --git a/tests/node_compat/test/parallel/test-vm-source-map-url.js b/tests/node_compat/test/parallel/test-vm-source-map-url.js
new file mode 100644
index 000000000..fb91ff1fc
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-source-map-url.js
@@ -0,0 +1,34 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+function checkSourceMapUrl(source, expectedSourceMapURL) {
+ const script = new vm.Script(source);
+ assert.strictEqual(script.sourceMapURL, expectedSourceMapURL);
+}
+
+// No magic comment
+checkSourceMapUrl(`
+function myFunc() {}
+`, undefined);
+
+// Malformed magic comment
+checkSourceMapUrl(`
+function myFunc() {}
+// sourceMappingURL=sourcemap.json
+`, undefined);
+
+// Expected magic comment
+checkSourceMapUrl(`
+function myFunc() {}
+//# sourceMappingURL=sourcemap.json
+`, 'sourcemap.json');
diff --git a/tests/node_compat/test/parallel/test-vm-strict-mode.js b/tests/node_compat/test/parallel/test-vm-strict-mode.js
new file mode 100644
index 000000000..e759bd2c4
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-strict-mode.js
@@ -0,0 +1,21 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// https://github.com/nodejs/node/issues/12300
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const ctx = vm.createContext({ x: 42 });
+
+// This might look as if x has not been declared, but x is defined on the
+// sandbox and the assignment should not throw.
+vm.runInContext('"use strict"; x = 1', ctx);
+
+assert.strictEqual(ctx.x, 1);
diff --git a/tests/node_compat/test/parallel/test-vm-symbols.js b/tests/node_compat/test/parallel/test-vm-symbols.js
new file mode 100644
index 000000000..fbaff6a82
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-symbols.js
@@ -0,0 +1,30 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+const vm = require('vm');
+
+const symbol = Symbol();
+
+function Document() {
+ this[symbol] = 'foo';
+}
+
+Document.prototype.getSymbolValue = function() {
+ return this[symbol];
+};
+
+const context = new Document();
+vm.createContext(context);
+
+assert.strictEqual(context.getSymbolValue(), 'foo');
+
+assert.strictEqual(vm.runInContext('this.getSymbolValue()', context), 'foo');
diff --git a/tests/node_compat/test/parallel/test-vm-timeout-escape-promise-2.js b/tests/node_compat/test/parallel/test-vm-timeout-escape-promise-2.js
new file mode 100644
index 000000000..19ba0a682
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-timeout-escape-promise-2.js
@@ -0,0 +1,45 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+// https://github.com/nodejs/node/issues/3020
+// Promises used to allow code to escape the timeout
+// set for runInContext, runInNewContext, and runInThisContext.
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const NS_PER_MS = 1000000n;
+
+const hrtime = process.hrtime.bigint;
+
+function loop() {
+ const start = hrtime();
+ while (1) {
+ const current = hrtime();
+ const span = (current - start) / NS_PER_MS;
+ if (span >= 2000n) {
+ throw new Error(
+ `escaped timeout at ${span} milliseconds!`);
+ }
+ }
+}
+
+assert.throws(() => {
+ vm.runInNewContext(
+ 'Promise.resolve().then(() => loop());',
+ {
+ hrtime,
+ loop
+ },
+ { timeout: 10, microtaskMode: 'afterEvaluate' }
+ );
+}, {
+ code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
+ message: 'Script execution timed out after 10ms'
+});
diff --git a/tests/node_compat/test/parallel/test-vm-timeout-escape-promise.js b/tests/node_compat/test/parallel/test-vm-timeout-escape-promise.js
new file mode 100644
index 000000000..c2393a852
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-timeout-escape-promise.js
@@ -0,0 +1,46 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+// https://github.com/nodejs/node/issues/3020
+// Promises used to allow code to escape the timeout
+// set for runInContext, runInNewContext, and runInThisContext.
+
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+const NS_PER_MS = 1000000n;
+
+const hrtime = process.hrtime.bigint;
+
+function loop() {
+ const start = hrtime();
+ while (1) {
+ const current = hrtime();
+ const span = (current - start) / NS_PER_MS;
+ if (span >= 2000n) {
+ throw new Error(
+ `escaped timeout at ${span} milliseconds!`);
+ }
+ }
+}
+
+assert.throws(() => {
+ vm.runInNewContext(
+ 'Promise.resolve().then(() => loop()); loop();',
+ {
+ hrtime,
+ loop
+ },
+ { timeout: 5, microtaskMode: 'afterEvaluate' }
+ );
+}, {
+ code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
+ message: 'Script execution timed out after 5ms'
+});
diff --git a/tests/node_compat/test/parallel/test-vm-timeout.js b/tests/node_compat/test/parallel/test-vm-timeout.js
new file mode 100644
index 000000000..d34520640
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-vm-timeout.js
@@ -0,0 +1,88 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+require('../common');
+const assert = require('assert');
+const vm = require('vm');
+
+// Timeout of 100ms executing endless loop
+assert.throws(
+ function() {
+ vm.runInThisContext('while(true) {}', { timeout: 100 });
+ },
+ {
+ code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
+ message: 'Script execution timed out after 100ms'
+ });
+
+// Timeout of 1000ms, script finishes first
+vm.runInThisContext('', { timeout: 1000 });
+
+// Nested vm timeouts, inner timeout propagates out
+assert.throws(
+ function() {
+ const context = {
+ log: console.log,
+ runInVM: function(timeout) {
+ vm.runInNewContext('while(true) {}', context, { timeout });
+ }
+ };
+ vm.runInNewContext('runInVM(10)', context, { timeout: 10000 });
+ throw new Error('Test 5 failed');
+ },
+ {
+ code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
+ message: 'Script execution timed out after 10ms'
+ });
+
+// Nested vm timeouts, outer timeout is shorter and fires first.
+assert.throws(
+ function() {
+ const context = {
+ runInVM: function(timeout) {
+ vm.runInNewContext('while(true) {}', context, { timeout });
+ }
+ };
+ vm.runInNewContext('runInVM(10000)', context, { timeout: 100 });
+ throw new Error('Test 6 failed');
+ },
+ {
+ code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
+ message: 'Script execution timed out after 100ms'
+ });
+
+// Nested vm timeouts, inner script throws an error.
+assert.throws(function() {
+ const context = {
+ runInVM: function(timeout) {
+ vm.runInNewContext('throw new Error(\'foobar\')', context, { timeout });
+ }
+ };
+ vm.runInNewContext('runInVM(10000)', context, { timeout: 100000 });
+}, /foobar/);
diff --git a/tests/unit_node/vm_test.ts b/tests/unit_node/vm_test.ts
index 4c370931d..85b955663 100644
--- a/tests/unit_node/vm_test.ts
+++ b/tests/unit_node/vm_test.ts
@@ -128,7 +128,7 @@ Deno.test({
const obj = {};
assertEquals(isContext(obj), false);
assertEquals(isContext(globalThis), false);
- const sandbox = runInNewContext("{}");
+ const sandbox = runInNewContext("({})");
assertEquals(isContext(sandbox), false);
},
});