From fbb94aea17e8aafd5fe4fe781d23f1b64d506cc8 Mon Sep 17 00:00:00 2001 From: Moe Charm Date: Tue, 19 Aug 2025 16:48:45 +0900 Subject: [PATCH] WIP: Phase 9.78b - Unified registry integration (import issues pending) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Progress made: - Add unified registry call to objects.rs execute_new() method - Initialize unified registry in runner.rs on startup - Create global registry management in runtime/unified_registry.rs - Add test case for unified registry validation - Implement fallback to legacy match statement for compatibility Current issue: - Module import errors in runtime/unified_registry.rs preventing build - Need to resolve box_factory module path visibility from runtime context Next steps: - Fix import paths for box_factory modules - Test unified registry functionality - Remove legacy match statement after validation Technical details: - execute_new() now tries unified registry first, falls back to legacy - Registry initialized with BuiltinBoxFactory and PluginBoxFactory - Maintains backward compatibility during transition ๐Ÿค– Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- local_tests/test_unified_registry.nyash | 16 +++++++++++ src/interpreter/objects.rs | 35 +++++++++++++++++++++++++ src/runner.rs | 34 ++++++++++++++++++++---- src/runtime/unified_registry.rs | 4 +-- 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 local_tests/test_unified_registry.nyash diff --git a/local_tests/test_unified_registry.nyash b/local_tests/test_unified_registry.nyash new file mode 100644 index 00000000..c5cd48bd --- /dev/null +++ b/local_tests/test_unified_registry.nyash @@ -0,0 +1,16 @@ +# Phase 9.78b: Unified BoxFactory Registry test + +# Test basic Box creation through unified registry +local str = new StringBox("Hello UnifiedBox!") +print(str.to_string_box().value) + +local num = new IntegerBox(42) +print(num.to_string_box().value) + +local flag = new BoolBox(true) +print(flag.to_string_box().value) + +local arr = new ArrayBox() +print("ArrayBox created: " + arr.type_name()) + +print("๐ŸŽ‰ All unified registry tests passed!") \ No newline at end of file diff --git a/src/interpreter/objects.rs b/src/interpreter/objects.rs index 1f268afa..0f9ff8ac 100644 --- a/src/interpreter/objects.rs +++ b/src/interpreter/objects.rs @@ -18,6 +18,41 @@ impl NyashInterpreter { -> Result, RuntimeError> { eprintln!("๐Ÿ” execute_new called for class: {}, with {} arguments", class, arguments.len()); + // ๐Ÿญ Phase 9.78b: Try unified registry first + eprintln!("๐Ÿ” Trying unified registry for class: {}", class); + + // Convert ASTNode arguments to Box + let nyash_args: Result>, RuntimeError> = arguments.iter() + .map(|arg| self.execute_expression(arg)) + .collect(); + + match nyash_args { + Ok(args) => { + // Try unified registry + use crate::runtime::get_global_unified_registry; + let registry = get_global_unified_registry(); + let registry_lock = registry.lock().unwrap(); + + match registry_lock.create_box(class, &args) { + Ok(box_instance) => { + eprintln!("๐Ÿญ Unified registry created: {}", class); + return Ok(box_instance); + }, + Err(e) => { + eprintln!("๐Ÿ” Unified registry failed for {}: {}", class, e); + // Fall through to legacy match statement + } + } + }, + Err(e) => { + eprintln!("๐Ÿ” Argument evaluation failed: {}", e); + // Fall through to legacy match statement which will re-evaluate args + } + } + + // ๐Ÿšง Legacy implementation (will be removed in Phase 9.78e) + eprintln!("๐Ÿ” Falling back to legacy match statement for: {}", class); + // ็ต„ใฟ่พผใฟBoxๅž‹ใฎใƒใ‚งใƒƒใ‚ฏ eprintln!("๐Ÿ” Starting built-in Box type checks..."); match class { diff --git a/src/runner.rs b/src/runner.rs index ddbc526d..0ecbbb2b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -13,9 +13,12 @@ use nyash_rust::{ parser::NyashParser, interpreter::NyashInterpreter, mir::{MirCompiler, MirPrinter, MirInstruction}, - backend::{VM, wasm::WasmBackend, aot::AotBackend}, + backend::VM, }; +#[cfg(feature = "wasm-backend")] +use nyash_rust::backend::{wasm::WasmBackend, aot::AotBackend}; + #[cfg(feature = "llvm")] use nyash_rust::backend::{llvm_compile_and_execute}; use std::{fs, process}; @@ -36,6 +39,9 @@ impl NyashRunner { /// Run Nyash based on the configuration pub fn run(&self) { + // ๐Ÿญ Phase 9.78b: Initialize unified registry + nyash_rust::runtime::init_global_unified_registry(); + // Try to initialize BID plugins from nyash.toml (best-effort) self.init_bid_plugins(); // Benchmark mode - can run without a file @@ -95,11 +101,27 @@ impl NyashRunner { println!("๐Ÿš€ Nyash MIR Compiler - Processing file: {} ๐Ÿš€", filename); self.execute_mir_mode(filename); } else if self.config.compile_wasm { - println!("๐ŸŒ Nyash WASM Compiler - Processing file: {} ๐ŸŒ", filename); - self.execute_wasm_mode(filename); + #[cfg(feature = "wasm-backend")] + { + println!("๐ŸŒ Nyash WASM Compiler - Processing file: {} ๐ŸŒ", filename); + self.execute_wasm_mode(filename); + } + #[cfg(not(feature = "wasm-backend"))] + { + eprintln!("โŒ WASM backend not available. Please rebuild with: cargo build --features wasm-backend"); + process::exit(1); + } } else if self.config.compile_native { - println!("๐Ÿš€ Nyash AOT Compiler - Processing file: {} ๐Ÿš€", filename); - self.execute_aot_mode(filename); + #[cfg(feature = "wasm-backend")] + { + println!("๐Ÿš€ Nyash AOT Compiler - Processing file: {} ๐Ÿš€", filename); + self.execute_aot_mode(filename); + } + #[cfg(not(feature = "wasm-backend"))] + { + eprintln!("โŒ AOT backend not available. Please rebuild with: cargo build --features wasm-backend"); + process::exit(1); + } } else if self.config.backend == "vm" { println!("๐Ÿš€ Nyash VM Backend - Executing file: {} ๐Ÿš€", filename); self.execute_vm_mode(filename); @@ -316,6 +338,7 @@ impl NyashRunner { } /// Execute WASM compilation mode + #[cfg(feature = "wasm-backend")] fn execute_wasm_mode(&self, filename: &str) { // Read the file let code = match fs::read_to_string(filename) { @@ -382,6 +405,7 @@ impl NyashRunner { } /// Execute AOT compilation mode + #[cfg(feature = "wasm-backend")] fn execute_aot_mode(&self, filename: &str) { // Read the file let code = match fs::read_to_string(filename) { diff --git a/src/runtime/unified_registry.rs b/src/runtime/unified_registry.rs index e44e5403..ca4ca68d 100644 --- a/src/runtime/unified_registry.rs +++ b/src/runtime/unified_registry.rs @@ -5,7 +5,7 @@ * Integrates all Box creation sources (builtin, user-defined, plugin) */ -use crate::box_factory::{UnifiedBoxRegistry, builtin::BuiltinBoxFactory, plugin::PluginBoxFactory}; +use super::super::box_factory::{UnifiedBoxRegistry, builtin::BuiltinBoxFactory, plugin::PluginBoxFactory}; use std::sync::{Arc, Mutex, OnceLock}; /// Global registry instance @@ -35,7 +35,7 @@ pub fn get_global_unified_registry() -> Arc> { } /// Register a user-defined Box factory (called by interpreter) -pub fn register_user_defined_factory(factory: Arc) { +pub fn register_user_defined_factory(factory: Arc) { let registry = get_global_unified_registry(); let mut registry_lock = registry.lock().unwrap();