Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): basic vm.runInNewContext implementation #21527

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ deno_core::extension!(deno_node,
ops::winerror::op_node_sys_to_uv_error,
ops::v8::op_v8_cached_data_version_tag,
ops::v8::op_v8_get_heap_statistics,
ops::v8::op_vm_run_in_new_context,
ops::idna::op_node_idna_domain_to_ascii,
ops::idna::op_node_idna_domain_to_unicode,
ops::idna::op_node_idna_punycode_decode,
Expand Down
24 changes: 24 additions & 0 deletions ext/node/ops/v8.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::v8;

Expand Down Expand Up @@ -30,3 +31,26 @@ pub fn op_v8_get_heap_statistics(
buffer[12] = stats.used_global_handles_size() as f64;
buffer[13] = stats.external_memory() as f64;
}

#[op2]
pub fn op_vm_run_in_new_context<'a>(
scope: &mut v8::HandleScope<'a>,
script: v8::Local<v8::String>,
ctx_val: v8::Local<v8::Value>,
) -> Result<v8::Local<'a, v8::Value>, AnyError> {
let _ctx_obj = if ctx_val.is_undefined() || ctx_val.is_null() {
v8::Object::new(scope)
} else {
ctx_val.try_into()?
};

let template = v8::ObjectTemplate::new(scope);
let ctx = v8::Context::new_from_template(scope, template);

let scope = &mut v8::ContextScope::new(scope, ctx);

let script = v8::Script::compile(scope, script, None).unwrap();
let result = script.run(scope).unwrap();

Ok(result)
}
11 changes: 6 additions & 5 deletions ext/node/polyfills/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { notImplemented } from "ext:deno_node/_utils.ts";

const { core } = globalThis.__bootstrap;
const ops = core.ops;

export class Script {
code: string;
Expand All @@ -25,8 +26,8 @@ export class Script {
notImplemented("Script.prototype.runInContext");
}

runInNewContext(_contextObject: any, _options: any) {
notImplemented("Script.prototype.runInNewContext");
runInNewContext(contextObject: any, _options: any) {
littledivy marked this conversation as resolved.
Show resolved Hide resolved
return ops.op_vm_run_in_new_context(this.code, contextObject);
}

createCachedData() {
Expand All @@ -51,11 +52,11 @@ export function runInContext(
}

export function runInNewContext(
_code: string,
_contextObject: any,
code: string,
contextObject: any,
_options: any,
littledivy marked this conversation as resolved.
Show resolved Hide resolved
) {
notImplemented("runInNewContext");
return ops.op_vm_run_in_new_context(code, contextObject);
}

export function runInThisContext(
Expand Down