From 9cccd309708cdbaeaa3ea34c08ca66d15fd8965b Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Wed, 10 Apr 2024 16:34:39 -0700 Subject: [PATCH] Move json_module to Module::from_value_as_default --- core/engine/src/module/mod.rs | 19 +++++++++++++++++++ core/interop/src/modules/json.rs | 20 +------------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/core/engine/src/module/mod.rs b/core/engine/src/module/mod.rs index 9bf195f35ff..297110119c2 100644 --- a/core/engine/src/module/mod.rs +++ b/core/engine/src/module/mod.rs @@ -27,6 +27,7 @@ use std::hash::Hash; use std::path::{Path, PathBuf}; use std::rc::Rc; +use boa_engine::js_string; use rustc_hash::FxHashSet; use boa_gc::{Finalize, Gc, GcRefCell, Trace}; @@ -204,6 +205,24 @@ impl Module { } } + /// Create a [`Module`] from a `JsValue`, exporting that value as the default export. + /// This will clone the module everytime it is initialized. + pub fn from_value_as_default(value: JsValue, context: &mut Context) -> Self { + Module::synthetic( + &[js_string!("default")], + SyntheticModuleInitializer::from_copy_closure_with_captures( + move |m, value, _ctx| { + m.set_export(&js_string!("default"), value.clone())?; + Ok(()) + }, + value, + ), + None, + None, + context, + ) + } + /// Gets the realm of this `Module`. #[inline] #[must_use] diff --git a/core/interop/src/modules/json.rs b/core/interop/src/modules/json.rs index 6d1f6b7dbe7..09779f56fe9 100644 --- a/core/interop/src/modules/json.rs +++ b/core/interop/src/modules/json.rs @@ -2,26 +2,8 @@ //! as the default export. #![allow(clippy::module_name_repetitions)] -use boa_engine::module::SyntheticModuleInitializer; use boa_engine::{js_string, Context, JsValue, Module}; -/// Create a module that exports a single JSON value as the default export. -pub fn json_module(value: JsValue, context: &mut Context) -> Module { - Module::synthetic( - &[js_string!("default")], - SyntheticModuleInitializer::from_copy_closure_with_captures( - move |m, s, _ctx| { - m.set_export(&js_string!("default"), s.clone())?; - Ok(()) - }, - value, - ), - None, - None, - context, - ) -} - /// Create a module that exports a single JSON value as the default export, from its /// JSON string. This required the `json` feature to be enabled. /// @@ -33,7 +15,7 @@ pub fn json_string_module(json: &str, context: &mut Context) -> boa_engine::JsRe boa_engine::JsError::from_opaque(js_string!(format!("Failed to parse JSON: {}", e)).into()) })?; let value: JsValue = JsValue::from_json(&json_value, context)?; - Ok(json_module(value, context)) + Ok(Module::from_value_as_default(value, context)) } #[cfg(feature = "json")]