fix(ext/node): add vm.createContext

This commit is contained in:
Divy Srivastava 2024-03-18 18:05:43 +05:30
parent 429bea17a2
commit 575513e056
4 changed files with 22 additions and 11 deletions

View file

@ -260,6 +260,7 @@ deno_core::extension!(deno_node,
ops::v8::op_v8_cached_data_version_tag,
ops::v8::op_v8_get_heap_statistics,
ops::v8::op_vm_run_in_new_context,
ops::v8::op_vm_create_context,
ops::idna::op_node_idna_domain_to_ascii,
ops::idna::op_node_idna_domain_to_unicode,
ops::idna::op_node_idna_punycode_to_ascii,

View file

@ -79,12 +79,11 @@ pub fn op_vm_run_in_new_context<'a>(
})
}
#[op2]
pub fn op_vm_create_context<'a>(
scope: &mut v8::HandleScope<'a>,
sandbox_obj: v8::Local<v8::Value>,
allow_code_generation: bool,
) -> Result<v8::Local<'a, v8::Value>, AnyError> {
let ctx_obj = if sandbox_obj.is_undefined() || sandbox_obj.is_null() {
v8::Object::new(scope)
@ -94,12 +93,18 @@ pub fn op_vm_create_context<'a>(
let ctx = make_context(scope);
// TODO(@littledivy): rusty_v8 api required.
//
// const NODE_CONTEXT_INDEX: usize = 37;
// ctx.set_embedder_data(NODE_CONTEXT_INDEX, ctx_obj.into());
// ctx.allow_code_generation(allow_code_generation);
let scope = &mut v8::ContextScope::new(scope, ctx);
let global_obj = ctx.global(scope);
scope.
// TODO(@littledivy): create constructor name is not present.
let contextify_wrapper_template = v8::ObjectTemplate::new(scope);
let wrapper = contextify_wrapper_template.new_instance(scope).unwrap();
let global_obj = ctx.global(scope);
Ok(ctx.into())
Ok(wrapper.into())
}

View file

@ -5,7 +5,7 @@
import { core } from "ext:core/mod.js";
import { notImplemented } from "ext:deno_node/_utils.ts";
import { op_vm_run_in_new_context } from "ext:core/ops";
import { op_vm_run_in_new_context, op_vm_create_context } from "ext:core/ops";
export class Script {
code: string;
@ -39,8 +39,8 @@ export class Script {
}
}
export function createContext(_contextObject: any, _options: any) {
notImplemented("createContext");
export function createContext(contextObject: any, _options: any) {
return op_vm_create_context(contextObject, true);
}
export function createScript(code: string, options: any) {

View file

@ -1,5 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { isContext, runInNewContext } from "node:vm";
import { createContext, isContext, runInNewContext } from "node:vm";
import { assertEquals, assertThrows } from "@std/assert/mod.ts";
Deno.test({
@ -64,3 +64,8 @@ Deno.test({
assertEquals(isContext(sandbox), false);
},
});
Deno.test(function v8CreateContext() {
const context = { globalVar: 1 };
const contextified = createContext(context);
});