🎉 merge: Arc<Mutex>革命をmainブランチに統合完了!

## 🔥 統合された革命的成果
- **全9種類のBox統一**: Arc<Mutex>パターンで内部可変性実現
- **&selfメソッド**: すべてのBoxで統一されたメソッドシグネチャ
- **スレッドセーフ**: マルチスレッド環境で安全動作保証
- **メモリ安全**: Rustの所有権システムと完全統合

##  統合されたBox実装
- ArrayBox: Arc<Mutex<Vec<dyn NyashBox>>>で配列操作
- BufferBox: Arc<Mutex<Vec<u8>>>でバイナリデータ処理
- RegexBox: Arc<Regex>で正規表現処理
- JSONBox: Arc<Mutex<Value>>でJSON解析・操作
- StreamBox: Arc<Mutex<Vec<u8>>>でストリーム処理
- HttpClientBox: HTTP通信(stub実装)
- ResultBox/FutureBox: エラー・非同期処理(確認済み)

## 🏗️ アーキテクチャ革新
- **"Everything is Box"哲学の完全実現**
- **GitHub Copilotとの協働成果をmainに統合**
- **統一されたArc<Mutex>パターン**
- **内部可変性と外部安全性の両立**

🎊 Arc<Mutex>革命 - mainブランチ統合完了記念日: 2025年8月10日

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-10 15:27:33 +09:00
27 changed files with 2076 additions and 257 deletions

View File

@ -8,7 +8,7 @@
use super::*;
use crate::ast::UnaryOperator;
use crate::boxes::array::ArrayBox;
use crate::boxes::{buffer::BufferBox, JSONBox, HttpClientBox, StreamBox, RegexBox};
// TODO: Fix NullBox import issue later
// use crate::NullBox;
@ -327,8 +327,11 @@ impl NyashInterpreter {
return self.execute_array_method(array_box, method, arguments);
}
// TODO: 以下のBoxはまだ実装されていない
/*
// BufferBox method calls
if let Some(buffer_box) = obj_value.as_any().downcast_ref::<BufferBox>() {
return self.execute_buffer_method(buffer_box, method, arguments);
}
// FileBox method calls
if let Some(file_box) = obj_value.as_any().downcast_ref::<FileBox>() {
return self.execute_file_method(file_box, method, arguments);
@ -348,7 +351,26 @@ impl NyashInterpreter {
if let Some(channel_box) = obj_value.as_any().downcast_ref::<ChannelBox>() {
return self.execute_channel_method(channel_box, method, arguments);
}
*/
// JSONBox method calls
if let Some(json_box) = obj_value.as_any().downcast_ref::<JSONBox>() {
return self.execute_json_method(json_box, method, arguments);
}
// HttpClientBox method calls
if let Some(http_box) = obj_value.as_any().downcast_ref::<HttpClientBox>() {
return self.execute_http_method(http_box, method, arguments);
}
// StreamBox method calls
if let Some(stream_box) = obj_value.as_any().downcast_ref::<StreamBox>() {
return self.execute_stream_method(stream_box, method, arguments);
}
// RegexBox method calls
if let Some(regex_box) = obj_value.as_any().downcast_ref::<RegexBox>() {
return self.execute_regex_method(regex_box, method, arguments);
}
// MathBox method calls
if let Some(math_box) = obj_value.as_any().downcast_ref::<MathBox>() {

View File

@ -0,0 +1,204 @@
/*!
* Data Processing Box Methods Module
*
* Contains method implementations for data processing Box types:
* - BufferBox (execute_buffer_method) - Binary data operations
* - JSONBox (execute_json_method) - JSON parsing and manipulation
* - RegexBox (execute_regex_method) - Regular expression operations
*/
use super::super::*;
use crate::box_trait::{NyashBox, StringBox, IntegerBox};
use crate::boxes::{buffer::BufferBox, JSONBox, RegexBox};
impl NyashInterpreter {
/// BufferBoxのメソッド呼び出しを実行
pub(in crate::interpreter) fn execute_buffer_method(&mut self, buffer_box: &BufferBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
match method {
"write" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("write() expects 1 argument, got {}", arguments.len()),
});
}
let data = self.execute_expression(&arguments[0])?;
Ok(buffer_box.write(data))
}
"readAll" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("readAll() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(buffer_box.readAll())
}
"read" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("read() expects 1 argument, got {}", arguments.len()),
});
}
let count = self.execute_expression(&arguments[0])?;
Ok(buffer_box.read(count))
}
"clear" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("clear() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(buffer_box.clear())
}
"length" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("length() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(buffer_box.length())
}
"append" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("append() expects 1 argument, got {}", arguments.len()),
});
}
let other = self.execute_expression(&arguments[0])?;
Ok(buffer_box.append(other))
}
"slice" => {
if arguments.len() != 2 {
return Err(RuntimeError::InvalidOperation {
message: format!("slice() expects 2 arguments, got {}", arguments.len()),
});
}
let start = self.execute_expression(&arguments[0])?;
let end = self.execute_expression(&arguments[1])?;
Ok(buffer_box.slice(start, end))
}
_ => Err(RuntimeError::InvalidOperation {
message: format!("Unknown method '{}' for BufferBox", method),
})
}
}
/// JSONBoxのメソッド呼び出しを実行
pub(in crate::interpreter) fn execute_json_method(&mut self, json_box: &JSONBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
match method {
"parse" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("parse() expects 1 argument, got {}", arguments.len()),
});
}
let data = self.execute_expression(&arguments[0])?;
Ok(JSONBox::parse(data))
}
"stringify" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("stringify() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(json_box.stringify())
}
"get" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("get() expects 1 argument, got {}", arguments.len()),
});
}
let key = self.execute_expression(&arguments[0])?;
Ok(json_box.get(key))
}
"set" => {
if arguments.len() != 2 {
return Err(RuntimeError::InvalidOperation {
message: format!("set() expects 2 arguments, got {}", arguments.len()),
});
}
let key = self.execute_expression(&arguments[0])?;
let value = self.execute_expression(&arguments[1])?;
Ok(json_box.set(key, value))
}
"has" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("has() expects 1 argument, got {}", arguments.len()),
});
}
let key = self.execute_expression(&arguments[0])?;
Ok(json_box.has(key))
}
"keys" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("keys() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(json_box.keys())
}
_ => Err(RuntimeError::InvalidOperation {
message: format!("Unknown method '{}' for JSONBox", method),
})
}
}
/// RegexBoxのメソッド呼び出しを実行
pub(in crate::interpreter) fn execute_regex_method(&mut self, regex_box: &RegexBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
match method {
"test" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("test() expects 1 argument, got {}", arguments.len()),
});
}
let text = self.execute_expression(&arguments[0])?;
Ok(regex_box.test(text))
}
"find" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("find() expects 1 argument, got {}", arguments.len()),
});
}
let text = self.execute_expression(&arguments[0])?;
Ok(regex_box.find(text))
}
"findAll" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("findAll() expects 1 argument, got {}", arguments.len()),
});
}
let text = self.execute_expression(&arguments[0])?;
Ok(regex_box.find_all(text))
}
"replace" => {
if arguments.len() != 2 {
return Err(RuntimeError::InvalidOperation {
message: format!("replace() expects 2 arguments, got {}", arguments.len()),
});
}
let text = self.execute_expression(&arguments[0])?;
let replacement = self.execute_expression(&arguments[1])?;
Ok(regex_box.replace(text, replacement))
}
"split" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("split() expects 1 argument, got {}", arguments.len()),
});
}
let text = self.execute_expression(&arguments[0])?;
Ok(regex_box.split(text))
}
_ => Err(RuntimeError::InvalidOperation {
message: format!("Unknown method '{}' for RegexBox", method),
})
}
}
}

View File

@ -19,8 +19,12 @@
pub mod basic_methods; // StringBox, IntegerBox, BoolBox, FloatBox
pub mod collection_methods; // ArrayBox, MapBox
pub mod io_methods; // FileBox, ResultBox
pub mod data_methods; // BufferBox, JSONBox, RegexBox
pub mod network_methods; // HttpClientBox, StreamBox
// Re-export methods for easy access
pub use basic_methods::*;
pub use collection_methods::*;
pub use io_methods::*;
pub use io_methods::*;
pub use data_methods::*;
pub use network_methods::*;

View File

@ -0,0 +1,124 @@
/*!
* Network and Communication Box Methods Module
*
* Contains method implementations for network-related Box types:
* - HttpClientBox (execute_http_method) - HTTP client operations
* - StreamBox (execute_stream_method) - Stream processing operations
*/
use super::super::*;
use crate::box_trait::{NyashBox, StringBox};
use crate::boxes::{HttpClientBox, StreamBox};
impl NyashInterpreter {
/// HttpClientBoxのメソッド呼び出しを実行
pub(in crate::interpreter) fn execute_http_method(&mut self, http_box: &HttpClientBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
match method {
"get" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("get() expects 1 argument, got {}", arguments.len()),
});
}
let url = self.execute_expression(&arguments[0])?;
Ok(http_box.http_get(url))
}
"post" => {
if arguments.len() != 2 {
return Err(RuntimeError::InvalidOperation {
message: format!("post() expects 2 arguments, got {}", arguments.len()),
});
}
let url = self.execute_expression(&arguments[0])?;
let body = self.execute_expression(&arguments[1])?;
Ok(http_box.post(url, body))
}
"put" => {
if arguments.len() != 2 {
return Err(RuntimeError::InvalidOperation {
message: format!("put() expects 2 arguments, got {}", arguments.len()),
});
}
let url = self.execute_expression(&arguments[0])?;
let body = self.execute_expression(&arguments[1])?;
Ok(http_box.put(url, body))
}
"delete" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("delete() expects 1 argument, got {}", arguments.len()),
});
}
let url = self.execute_expression(&arguments[0])?;
Ok(http_box.delete(url))
}
"request" => {
if arguments.len() != 3 {
return Err(RuntimeError::InvalidOperation {
message: format!("request() expects 3 arguments, got {}", arguments.len()),
});
}
let method_arg = self.execute_expression(&arguments[0])?;
let url = self.execute_expression(&arguments[1])?;
let options = self.execute_expression(&arguments[2])?;
Ok(http_box.request(method_arg, url, options))
}
_ => Err(RuntimeError::InvalidOperation {
message: format!("Unknown method '{}' for HttpClientBox", method),
})
}
}
/// StreamBoxのメソッド呼び出しを実行
pub(in crate::interpreter) fn execute_stream_method(&mut self, stream_box: &StreamBox, method: &str, arguments: &[ASTNode])
-> Result<Box<dyn NyashBox>, RuntimeError> {
match method {
"write" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("write() expects 1 argument, got {}", arguments.len()),
});
}
let data = self.execute_expression(&arguments[0])?;
Ok(stream_box.stream_write(data))
}
"read" => {
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("read() expects 1 argument, got {}", arguments.len()),
});
}
let count = self.execute_expression(&arguments[0])?;
Ok(stream_box.stream_read(count))
}
"position" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("position() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(stream_box.get_position())
}
"length" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("length() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(stream_box.get_length())
}
"reset" => {
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("reset() expects 0 arguments, got {}", arguments.len()),
});
}
Ok(stream_box.stream_reset())
}
_ => Err(RuntimeError::InvalidOperation {
message: format!("Unknown method '{}' for StreamBox", method),
})
}
}
}

View File

@ -9,7 +9,6 @@
use super::*;
use crate::boxes::null_box::NullBox;
use crate::boxes::console_box::ConsoleBox;
use crate::boxes::array::ArrayBox;
// use crate::boxes::intent_box_wrapper::IntentBoxWrapper;
use std::sync::Arc;
@ -30,8 +29,6 @@ impl NyashInterpreter {
// 🌍 革命的実装Environment tracking廃止
return Ok(array_box);
}
// TODO: 以下のBoxはまだ実装されていない
/*
"FileBox" => {
// FileBoxは引数1個ファイルパスで作成
if arguments.len() != 1 {
@ -62,7 +59,6 @@ impl NyashInterpreter {
// 🌍 革命的実装Environment tracking廃止
return Ok(result_box);
}
*/
"ErrorBox" => {
// ErrorBoxは引数2個エラータイプ、メッセージで作成
if arguments.len() != 2 {
@ -394,6 +390,78 @@ impl NyashInterpreter {
// 🌍 革命的実装Environment tracking廃止
return Ok(debug_box);
}
"BufferBox" => {
// BufferBoxは引数なしで作成
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("BufferBox constructor expects 0 arguments, got {}", arguments.len()),
});
}
let buffer_box = Box::new(crate::boxes::buffer::BufferBox::new()) as Box<dyn NyashBox>;
return Ok(buffer_box);
}
"RegexBox" => {
// RegexBoxは引数1個パターンで作成
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("RegexBox constructor expects 1 argument, got {}", arguments.len()),
});
}
let pattern_value = self.execute_expression(&arguments[0])?;
if let Some(pattern_str) = pattern_value.as_any().downcast_ref::<StringBox>() {
match crate::boxes::regex::RegexBox::new(&pattern_str.value) {
Ok(regex_box) => return Ok(Box::new(regex_box)),
Err(e) => return Err(RuntimeError::InvalidOperation {
message: format!("Invalid regex pattern: {}", e),
}),
}
} else {
return Err(RuntimeError::TypeError {
message: "RegexBox constructor requires string pattern argument".to_string(),
});
}
}
"JSONBox" => {
// JSONBoxは引数1個JSON文字列で作成
if arguments.len() != 1 {
return Err(RuntimeError::InvalidOperation {
message: format!("JSONBox constructor expects 1 argument, got {}", arguments.len()),
});
}
let json_value = self.execute_expression(&arguments[0])?;
if let Some(json_str) = json_value.as_any().downcast_ref::<StringBox>() {
match crate::boxes::json::JSONBox::from_str(&json_str.value) {
Ok(json_box) => return Ok(Box::new(json_box)),
Err(e) => return Err(RuntimeError::InvalidOperation {
message: format!("Invalid JSON: {}", e),
}),
}
} else {
return Err(RuntimeError::TypeError {
message: "JSONBox constructor requires string JSON argument".to_string(),
});
}
}
"StreamBox" => {
// StreamBoxは引数なしで作成
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("StreamBox constructor expects 0 arguments, got {}", arguments.len()),
});
}
let stream_box = Box::new(crate::boxes::stream::StreamBox::new()) as Box<dyn NyashBox>;
return Ok(stream_box);
}
"HTTPClientBox" => {
// HTTPClientBoxは引数なしで作成
if !arguments.is_empty() {
return Err(RuntimeError::InvalidOperation {
message: format!("HTTPClientBox constructor expects 0 arguments, got {}", arguments.len()),
});
}
let http_box = Box::new(crate::boxes::http::HttpClientBox::new()) as Box<dyn NyashBox>;
return Ok(http_box);
}
"MethodBox" => {
// MethodBoxは引数2個インスタンス、メソッド名で作成
if arguments.len() != 2 {
@ -623,9 +691,10 @@ impl NyashInterpreter {
// 基本的なビルトイン型
let is_builtin = matches!(type_name,
"IntegerBox" | "StringBox" | "BoolBox" | "ArrayBox" | "MapBox" |
"MathBox" |
"FileBox" | "ResultBox" | "FutureBox" | "ChannelBox" | "MathBox" |
"TimeBox" | "DateTimeBox" | "TimerBox" | "RandomBox" | "SoundBox" |
"DebugBox" | "MethodBox" | "NullBox" | "ConsoleBox"
"DebugBox" | "MethodBox" | "NullBox" | "ConsoleBox" | "FloatBox" |
"BufferBox" | "RegexBox" | "JSONBox" | "StreamBox" | "HTTPClientBox"
);
// Web専用BoxWASM環境のみ