From 20469c175c6f9460bc4c9300f30ad4c2847b9a44 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Fri, 5 Jun 2026 22:15:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E7=BC=93=E5=AD=98=E7=AE=A1=E7=90=86=E5=86=85?= =?UTF-8?q?=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_kernel/src/kernel.rs | 165 ++++++++++++++++++++++++ crates/oxide_kernel/src/lib.rs | 3 +- crates/oxide_kernel/src/string_forge.rs | 22 ---- 3 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 crates/oxide_kernel/src/kernel.rs diff --git a/crates/oxide_kernel/src/kernel.rs b/crates/oxide_kernel/src/kernel.rs new file mode 100644 index 00000000..c018a8b0 --- /dev/null +++ b/crates/oxide_kernel/src/kernel.rs @@ -0,0 +1,165 @@ +#![allow(clippy::arc_with_non_send_sync)] + +use std::sync::{Arc, Mutex}; + +use crate::builtin::BuiltinWorld; +use crate::code_forge::CodeForge; +use crate::prop_forge::PropForge; +use crate::shape_forge::ShapeForge; +use crate::string_forge::Interner; + +pub struct KernelConfig { + pub min_pool_size: usize, + pub max_pool_size: Option, + pub max_dead_strings: Option, + pub warmup_builtin_shapes: bool, + pub warmup_builtin_code: bool, + pub warmup_builtin_ic: bool, +} + +impl KernelConfig { + pub fn minimal() -> Self { + Self { + min_pool_size: 4, + max_pool_size: Some(8), + max_dead_strings: Some(10_000), + warmup_builtin_shapes: true, + warmup_builtin_code: false, + warmup_builtin_ic: false, + } + } + + pub fn standard() -> Self { + Self { + min_pool_size: 8, + max_pool_size: Some(32), + max_dead_strings: Some(10_000), + warmup_builtin_shapes: true, + warmup_builtin_code: true, + warmup_builtin_ic: false, + } + } + + pub fn full() -> Self { + Self { + min_pool_size: 16, + max_pool_size: None, + max_dead_strings: Some(5_000), + warmup_builtin_shapes: true, + warmup_builtin_code: true, + warmup_builtin_ic: true, + } + } +} + +impl Default for KernelConfig { + fn default() -> Self { + Self::minimal() + } +} + +pub struct VmPool; + +pub struct VmGuard<'p>(std::marker::PhantomData<&'p ()>); + +pub struct OxideKernel { + pub config: KernelConfig, + pub string_forge: Arc, + pub shape_forge: Arc, + pub code_forge: Arc, + pub prop_forge: Arc, + pub builtin_world: Arc, + #[allow(dead_code)] + vm_pool: Mutex>, +} + +impl OxideKernel { + pub fn new(config: KernelConfig) -> Self { + let string_forge = Arc::new(Interner::new()); + let shape_forge = Arc::new(ShapeForge::new()); + let builtin_world = Arc::new(BuiltinWorld::new(&string_forge, &shape_forge)); + let code_forge = Arc::new(CodeForge::new()); + let prop_forge = Arc::new(PropForge::new()); + + Self { + config, + string_forge, + shape_forge, + code_forge, + prop_forge, + builtin_world, + vm_pool: Mutex::new(None), + } + } + + pub fn string_forge(&self) -> &Arc { + &self.string_forge + } + + pub fn shape_forge(&self) -> &Arc { + &self.shape_forge + } + + pub fn code_forge(&self) -> &Arc { + &self.code_forge + } + + pub fn prop_forge(&self) -> &Arc { + &self.prop_forge + } + + pub fn builtin_world(&self) -> &Arc { + &self.builtin_world + } + + pub fn config(&self) -> &KernelConfig { + &self.config + } + + pub fn spawn(&self) -> VmGuard<'_> { + todo!("implemented in 07-07") + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::shape_forge::EMPTY_SHAPE_ID; + + #[test] + fn test_kernel_new() { + let kernel = OxideKernel::new(KernelConfig::minimal()); + let (idx, _) = kernel.string_forge().intern("test"); + assert!(idx > 0); + } + + #[test] + fn test_kernel_builtins_accessible() { + let kernel = OxideKernel::new(KernelConfig::minimal()); + assert!(!kernel.builtin_world().object_proto.is_function()); + assert!(kernel.builtin_world().object_constructor.is_function()); + } + + #[test] + fn test_kernel_shape_forge() { + let kernel = OxideKernel::new(KernelConfig::minimal()); + assert!(kernel.shape_forge().get_shape(EMPTY_SHAPE_ID).is_some()); + } + + #[test] + fn test_kernel_string_forge() { + let kernel = OxideKernel::new(KernelConfig::minimal()); + let (i1, _) = kernel.string_forge().intern("hello"); + let (i2, _) = kernel.string_forge().intern("hello"); + assert_eq!(i1, i2); + } + + #[test] + fn test_kernel_config_presets() { + assert_eq!(KernelConfig::minimal().max_pool_size, Some(8)); + assert_eq!(KernelConfig::standard().max_pool_size, Some(32)); + assert!(!KernelConfig::minimal().warmup_builtin_ic); + assert!(KernelConfig::full().warmup_builtin_ic); + assert_eq!(KernelConfig::full().max_pool_size, None); + } +} diff --git a/crates/oxide_kernel/src/lib.rs b/crates/oxide_kernel/src/lib.rs index 96c7e689..2804b468 100644 --- a/crates/oxide_kernel/src/lib.rs +++ b/crates/oxide_kernel/src/lib.rs @@ -1,7 +1,8 @@ -#![doc = "OxideJS - Shared kernel (CodeForge, ShapeForge, IcForge, StringForge, BuiltinWorld)"] +#![doc = "OxideJS - Shared kernel (CodeForge, ShapeForge, PropForge, StringForge, BuiltinWorld)"] pub mod builtin; pub mod code_forge; +pub mod kernel; pub mod prop_forge; pub mod shape_forge; pub mod string_forge; diff --git a/crates/oxide_kernel/src/string_forge.rs b/crates/oxide_kernel/src/string_forge.rs index 4648485e..c6a9b64b 100644 --- a/crates/oxide_kernel/src/string_forge.rs +++ b/crates/oxide_kernel/src/string_forge.rs @@ -117,15 +117,6 @@ impl Default for Interner { } } -pub struct KernelConfig { - pub min_pool_size: usize, - pub max_pool_size: Option, - pub max_dead_strings: Option, - pub warmup_builtin_shapes: bool, - pub warmup_builtin_code: bool, - pub warmup_builtin_ic: bool, -} - #[cfg(test)] mod tests { use super::*; @@ -195,16 +186,3 @@ mod tests { assert_eq!(interner.lookup(0), Some("live".to_string())); } } - -impl Default for KernelConfig { - fn default() -> Self { - Self { - min_pool_size: 4, - max_pool_size: None, - max_dead_strings: Some(10_000), - warmup_builtin_shapes: true, - warmup_builtin_code: false, - warmup_builtin_ic: false, - } - } -}