From 8fca7bf45cd020f3f19f75b5a8e90fb8d2c6522a Mon Sep 17 00:00:00 2001 From: Dean Srebnik <49134864+load1n9@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:43:48 -0400 Subject: [PATCH] feat: json as a feature --- nova_vm/Cargo.toml | 5 ++-- .../ecmascript/builtins/structured_data.rs | 1 + nova_vm/src/ecmascript/execution/realm.rs | 23 +++++++++++-------- .../ecmascript/execution/realm/intrinsics.rs | 9 +++++--- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/nova_vm/Cargo.toml b/nova_vm/Cargo.toml index 18a493bcf..36410849b 100644 --- a/nova_vm/Cargo.toml +++ b/nova_vm/Cargo.toml @@ -19,12 +19,13 @@ oxc_syntax = { workspace = true } rand = { workspace = true } ryu-js = { workspace = true } small_string = { path = "../small_string" } -sonic-rs = { workspace = true } +sonic-rs = { workspace = true, optional = true} wtf8 = { workspace = true } [features] -default = ["math"] +default = ["math", "json"] math = [] +json = ["sonic-rs"] typescript = [] diff --git a/nova_vm/src/ecmascript/builtins/structured_data.rs b/nova_vm/src/ecmascript/builtins/structured_data.rs index 6c50aee60..f7d3dd392 100644 --- a/nova_vm/src/ecmascript/builtins/structured_data.rs +++ b/nova_vm/src/ecmascript/builtins/structured_data.rs @@ -5,5 +5,6 @@ pub(crate) mod array_buffer_objects; pub(crate) mod atomics_object; pub(crate) mod data_view_objects; +#[cfg(feature = "json")] pub(crate) mod json_object; pub(crate) mod shared_array_buffer_objects; diff --git a/nova_vm/src/ecmascript/execution/realm.rs b/nova_vm/src/ecmascript/execution/realm.rs index 3da76e673..f4442b144 100644 --- a/nova_vm/src/ecmascript/execution/realm.rs +++ b/nova_vm/src/ecmascript/execution/realm.rs @@ -977,16 +977,19 @@ pub(crate) fn set_default_global_bindings( define_property_or_throw(agent, global, name, desc)?; // 19.4.2 JSON - let name = PropertyKey::from(BUILTIN_STRING_MEMORY.JSON); - let value = agent.get_realm(realm_id).intrinsics().json(); - let desc = PropertyDescriptor { - value: Some(value.into_value()), - writable: Some(true), - enumerable: Some(false), - configurable: Some(true), - ..Default::default() - }; - define_property_or_throw(agent, global, name, desc)?; + #[cfg(feature = "json")] + { + let name = PropertyKey::from(BUILTIN_STRING_MEMORY.JSON); + let value = agent.get_realm(realm_id).intrinsics().json(); + let desc = PropertyDescriptor { + value: Some(value.into_value()), + writable: Some(true), + enumerable: Some(false), + configurable: Some(true), + ..Default::default() + }; + define_property_or_throw(agent, global, name, desc)?; + } // 19.4.3 Math #[cfg(feature = "math")] diff --git a/nova_vm/src/ecmascript/execution/realm/intrinsics.rs b/nova_vm/src/ecmascript/execution/realm/intrinsics.rs index e9cf3c079..cbf6b7f69 100644 --- a/nova_vm/src/ecmascript/execution/realm/intrinsics.rs +++ b/nova_vm/src/ecmascript/execution/realm/intrinsics.rs @@ -82,7 +82,6 @@ use crate::{ data_view_constructor::DataViewConstructor, data_view_prototype::DataViewPrototype, }, - json_object::JSONObject, shared_array_buffer_objects::{ shared_array_buffer_constructor::SharedArrayBufferConstructor, shared_array_buffer_prototype::SharedArrayBufferPrototype, @@ -142,10 +141,11 @@ use crate::{ }, }; +use super::RealmIdentifier; #[cfg(feature = "math")] use crate::ecmascript::builtins::numbers_and_dates::math_object::MathObject; - -use super::RealmIdentifier; +#[cfg(feature = "json")] +use crate::ecmascript::builtins::structured_data::json_object::JSONObject; #[derive(Debug, Clone)] pub(crate) struct Intrinsics { @@ -294,6 +294,7 @@ impl Intrinsics { DataViewPrototype::create_intrinsic(agent, realm); DataViewConstructor::create_intrinsic(agent, realm); AtomicsObject::create_intrinsic(agent, realm); + #[cfg(feature = "json")] JSONObject::create_intrinsic(agent, realm); WeakRefPrototype::create_intrinsic(agent, realm); WeakRefConstructor::create_intrinsic(agent, realm); @@ -940,6 +941,7 @@ impl Intrinsics { .into() } + #[cfg(feature = "json")] /// %JSON% pub(crate) fn json(&self) -> OrdinaryObject { IntrinsicObjectIndexes::JSONObject @@ -1546,6 +1548,7 @@ impl HeapMarkAndSweep for Intrinsics { self.is_finite().mark_values(queues); self.is_nan().mark_values(queues); self.iterator_prototype().mark_values(queues); + #[cfg(feature = "json")] self.json().mark_values(queues); self.map_prototype_entries().mark_values(queues); self.map_prototype().mark_values(queues);