Skip to content

Commit

Permalink
Rename and add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Apr 11, 2024
1 parent 018ca7b commit c13dddf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
19 changes: 12 additions & 7 deletions core/engine/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use std::hash::Hash;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use boa_engine::js_string;
use rustc_hash::FxHashSet;

use boa_engine::js_string;
use boa_gc::{Finalize, Gc, GcRefCell, Trace};
use boa_interner::Interner;
use boa_parser::source::ReadChar;
Expand All @@ -41,6 +41,7 @@ use source::SourceTextModule;
pub use synthetic::{SyntheticModule, SyntheticModuleInitializer};

use crate::{
builtins,
builtins::promise::{PromiseCapability, PromiseState},
environments::DeclarativeEnvironment,
object::{JsObject, JsPromise},
Expand Down Expand Up @@ -224,15 +225,19 @@ impl Module {
}

/// 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.
/// JSON string.
///
/// # Specification
/// This is a custom extension to the ECMAScript specification. The current proposal
/// for JSON modules is being considered in <https://github.com/tc39/proposal-json-modules>
/// and might differ from this implementation.
///
/// This method is provided as a convenience for hosts to create JSON modules.
///
/// # Errors
/// This will return an error if the JSON string is invalid or cannot be converted.
pub fn from_json_as_default(json: &str, context: &mut Context) -> JsResult<Self> {
let json_value = serde_json::from_str::<serde_json::Value>(json).map_err(|e| {
JsError::from_opaque(js_string!(format!("Failed to parse JSON: {}", e)).into())
})?;
let value: JsValue = JsValue::from_json(&json_value, context)?;
pub fn parse_json(json: JsString, context: &mut Context) -> JsResult<Self> {
let value = builtins::Json::parse(&JsValue::undefined(), &[json.into()], context)?;
Ok(Self::from_value_as_default(value, context))
}

Expand Down
16 changes: 10 additions & 6 deletions core/engine/tests/module.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![allow(unused_crate_dependencies, missing_docs)]

use std::rc::Rc;

use boa_engine::builtins::promise::PromiseState;
use boa_engine::module::{ModuleLoader, Referrer};
use boa_engine::{js_string, Context, JsResult, JsString, Module, Source};
use std::rc::Rc;

#[test]
fn test_json_module_from_str() {
struct TestModuleLoader(&'static str);
struct TestModuleLoader(JsString);
impl ModuleLoader for TestModuleLoader {
fn load_imported_module(
&self,
Expand All @@ -19,15 +20,15 @@ fn test_json_module_from_str() {
assert_eq!(specifier.to_std_string_escaped(), "basic");

finish_load(
Ok(Module::from_json_as_default(self.0, context).unwrap()),
Ok(Module::parse_json(self.0.clone(), context).unwrap()),
context,
);
}
}

let json_string = r#"{"key":"value","other":123}"#;
let json_string = js_string!(r#"{"key":"value","other":123}"#);
let mut context = Context::builder()
.module_loader(Rc::new(TestModuleLoader(json_string)))
.module_loader(Rc::new(TestModuleLoader(json_string.clone())))
.build()
.unwrap();

Expand Down Expand Up @@ -57,5 +58,8 @@ fn test_json_module_from_str() {
.get(js_string!("json"), &mut context)
.unwrap();

assert_eq!(json.to_json(&mut context).unwrap().to_string(), json_string);
assert_eq!(
JsString::from(json.to_json(&mut context).unwrap().to_string()),
json_string
);
}

0 comments on commit c13dddf

Please sign in to comment.