From 575513e0568ce9156f663720402f7b4f915e01ca Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 18 Mar 2024 18:05:43 +0530 Subject: [PATCH] fix(ext/node): add vm.createContext --- ext/node/lib.rs | 1 + ext/node/ops/v8.rs | 19 ++++++++++++------- ext/node/polyfills/vm.ts | 6 +++--- tests/unit_node/vm_test.ts | 7 ++++++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/ext/node/lib.rs b/ext/node/lib.rs index f4541f8866..fa14587a1e 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -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, diff --git a/ext/node/ops/v8.rs b/ext/node/ops/v8.rs index a93627a29f..40ec1ddda7 100644 --- a/ext/node/ops/v8.rs +++ b/ext/node/ops/v8.rs @@ -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, - + allow_code_generation: bool, ) -> Result, 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()) } diff --git a/ext/node/polyfills/vm.ts b/ext/node/polyfills/vm.ts index 10000b08c7..d584360b89 100644 --- a/ext/node/polyfills/vm.ts +++ b/ext/node/polyfills/vm.ts @@ -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) { diff --git a/tests/unit_node/vm_test.ts b/tests/unit_node/vm_test.ts index f8bc11b823..681c62aa4f 100644 --- a/tests/unit_node/vm_test.ts +++ b/tests/unit_node/vm_test.ts @@ -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); +});