Files
hakorune/src/boxes/console_box.rs

228 lines
5.5 KiB
Rust
Raw Normal View History

/*! 📟 ConsoleBox - コンソール出力Box
*
* ## 📝
* Webブラウザのコンソール機能を統合したBox
* WASM環境ではブラウザコンソール
*
* ## 🛠
* - `log(message)` -
* - `warn(message)` -
* - `error(message)` -
* - `clear()` -
*
* ## 💡 使
* ```nyash
* local console
* console = new ConsoleBox()
*
* console.log("Hello, Nyash!") // 通常ログ
* console.warn("This is a warning") // 警告
* console.error("Something went wrong") // エラー
* console.clear() // クリア
*
* // デバッグ用途
* local value
* value = 42
* console.log("Debug: value = " + value.toString())
* ```
*
* ## 🌐
* - **WASM環境**:
* - ****:
*
* ## 🔍
* ```nyash
* // エラーハンドリング
* if (error_condition) {
* console.error("Critical error occurred!")
* return null
* }
*
* // 実行トレース
* console.log("Function start")
* // 処理...
* console.log("Function end")
* ```
*/
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
use std::any::Any;
use std::fmt::Display;
// 🌐 Browser console access Box
#[cfg(target_arch = "wasm32")]
#[derive(Debug, Clone)]
pub struct ConsoleBox {
base: BoxBase,
}
#[cfg(target_arch = "wasm32")]
impl ConsoleBox {
pub fn new() -> Self {
Self { base: BoxBase::new() }
}
/// Log messages to browser console
pub fn log(&self, message: &str) {
web_sys::console::log_1(&message.into());
}
/// Log warning to browser console
pub fn warn(&self, message: &str) {
web_sys::console::warn_1(&message.into());
}
/// Log error to browser console
pub fn error(&self, message: &str) {
web_sys::console::error_1(&message.into());
}
/// Clear browser console
pub fn clear(&self) {
web_sys::console::clear();
}
}
#[cfg(target_arch = "wasm32")]
impl BoxCore for ConsoleBox {
fn box_id(&self) -> u64 {
self.base.id
}
fn parent_type_id(&self) -> Option<std::any::TypeId> {
self.base.parent_type_id
}
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[ConsoleBox - Browser Console Interface]")
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
#[cfg(target_arch = "wasm32")]
impl NyashBox for ConsoleBox {
fn to_string_box(&self) -> StringBox {
StringBox::new("[ConsoleBox - Browser Console Interface]")
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
BoolBox::new(other.as_any().is::<ConsoleBox>())
}
fn type_name(&self) -> &'static str {
"ConsoleBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
🔧 Phase 9.75D: Fix 74 compilation errors - complete share_box() trait implementation ## Summary - Fixed 74 compilation errors related to missing/misplaced share_box() methods - Implemented complete NyashBox trait for all Box types across the codebase - Updated extern_box.rs to modern trait structure ## Changes Made ### Core trait fixes (17 files): - ✅ Fixed syntax errors: moved share_box() methods to correct positions - ✅ Added missing share_box() implementations in 17 files - ✅ Updated extern_box.rs with proper BoxCore and NyashBox implementations ### Files modified: **Core trait system:** - src/box_trait.rs: Added share_box() for 7 basic Box types - src/box_arithmetic.rs: Added share_box() for 4 arithmetic Box types - src/instance.rs, src/channel_box.rs, src/exception_box.rs: Added missing methods - src/method_box.rs, src/type_box.rs: Complete trait implementations **Box implementations (20+ files):** - All boxes in src/boxes/ directory: Fixed share_box() positioning - extern_box.rs: Modernized to current trait structure - Web boxes: Fixed WASM-specific implementations ### Implementation pattern: ```rust /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() } ``` ## Result - ✅ `cargo check` now passes successfully (only warnings remain) - ✅ All NyashBox trait implementations complete - ✅ Ready for Phase 9.75D VM/WASM backend work - ✅ "Everything is Box" philosophy maintained 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-15 14:29:47 +09:00
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
}
}
// Non-WASM版 - モックアップ実装
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone)]
pub struct ConsoleBox {
base: BoxBase,
}
#[cfg(not(target_arch = "wasm32"))]
impl ConsoleBox {
pub fn new() -> Self {
Self { base: BoxBase::new() }
}
/// Mock log method for non-WASM environments
pub fn log(&self, message: &str) {
println!("[Console LOG] {}", message);
}
pub fn warn(&self, message: &str) {
println!("[Console WARN] {}", message);
}
pub fn error(&self, message: &str) {
println!("[Console ERROR] {}", message);
}
pub fn clear(&self) {
println!("[Console CLEAR]");
}
}
#[cfg(not(target_arch = "wasm32"))]
impl BoxCore for ConsoleBox {
fn box_id(&self) -> u64 {
self.base.id
}
fn parent_type_id(&self) -> Option<std::any::TypeId> {
self.base.parent_type_id
}
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[ConsoleBox - Mock Implementation]")
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
#[cfg(not(target_arch = "wasm32"))]
impl NyashBox for ConsoleBox {
fn to_string_box(&self) -> StringBox {
StringBox::new("[ConsoleBox - Mock Implementation]")
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
BoolBox::new(other.as_any().is::<ConsoleBox>())
}
fn type_name(&self) -> &'static str {
"ConsoleBox"
}
fn clone_box(&self) -> Box<dyn NyashBox> {
🔧 Phase 9.75D: Fix 74 compilation errors - complete share_box() trait implementation ## Summary - Fixed 74 compilation errors related to missing/misplaced share_box() methods - Implemented complete NyashBox trait for all Box types across the codebase - Updated extern_box.rs to modern trait structure ## Changes Made ### Core trait fixes (17 files): - ✅ Fixed syntax errors: moved share_box() methods to correct positions - ✅ Added missing share_box() implementations in 17 files - ✅ Updated extern_box.rs with proper BoxCore and NyashBox implementations ### Files modified: **Core trait system:** - src/box_trait.rs: Added share_box() for 7 basic Box types - src/box_arithmetic.rs: Added share_box() for 4 arithmetic Box types - src/instance.rs, src/channel_box.rs, src/exception_box.rs: Added missing methods - src/method_box.rs, src/type_box.rs: Complete trait implementations **Box implementations (20+ files):** - All boxes in src/boxes/ directory: Fixed share_box() positioning - extern_box.rs: Modernized to current trait structure - Web boxes: Fixed WASM-specific implementations ### Implementation pattern: ```rust /// 仮実装: clone_boxと同じ(後で修正) fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() } ``` ## Result - ✅ `cargo check` now passes successfully (only warnings remain) - ✅ All NyashBox trait implementations complete - ✅ Ready for Phase 9.75D VM/WASM backend work - ✅ "Everything is Box" philosophy maintained 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-15 14:29:47 +09:00
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
}
}
// Display implementations for both WASM and non-WASM versions
#[cfg(target_arch = "wasm32")]
impl Display for ConsoleBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt_box(f)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Display for ConsoleBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt_box(f)
}
}