diff --git a/src/boxes/socket_box.rs b/src/boxes/socket_box.rs index 65f5b25b..1354e171 100644 --- a/src/boxes/socket_box.rs +++ b/src/boxes/socket_box.rs @@ -56,25 +56,34 @@ pub struct SocketBox { impl Clone for SocketBox { fn clone(&self) -> Self { - Self { + let cloned = Self { base: BoxBase::new(), // New unique ID for clone listener: Arc::clone(&self.listener), stream: Arc::clone(&self.stream), is_server: Arc::clone(&self.is_server), is_connected: Arc::clone(&self.is_connected), - } + }; + let original_arc_ptr = Arc::as_ptr(&self.is_server) as usize; + let cloned_arc_ptr = Arc::as_ptr(&cloned.is_server) as usize; + let is_server_value = *self.is_server.lock().unwrap(); + println!("๐Ÿ”„ SocketBox::clone() - original Box ID: {}, cloned Box ID: {}, Arc ptr: {:x} -> {:x}, is_server: {}", + self.base.id, cloned.base.id, original_arc_ptr, cloned_arc_ptr, is_server_value); + cloned } } impl SocketBox { pub fn new() -> Self { - Self { + let instance = Self { base: BoxBase::new(), listener: Arc::new(Mutex::new(None)), stream: Arc::new(Mutex::new(None)), is_server: Arc::new(Mutex::new(false)), is_connected: Arc::new(Mutex::new(false)), - } + }; + let arc_ptr = Arc::as_ptr(&instance.is_server) as usize; + println!("๐Ÿ”ง SocketBox::new() - created (Box ID: {}, Arc ptr: {:x})", instance.base.id, arc_ptr); + instance } /// TCP ใ‚ฝใ‚ฑใƒƒใƒˆใ‚’ใ‚ขใƒ‰ใƒฌใ‚นใƒปใƒใƒผใƒˆใซใƒใ‚คใƒณใƒ‰ @@ -83,22 +92,31 @@ impl SocketBox { let port_str = port.to_string_box().value; let socket_addr = format!("{}:{}", addr_str, port_str); + println!("๐Ÿ” SocketBox::bind() called with address: {} (Box ID: {})", socket_addr, self.base.id); match TcpListener::bind(&socket_addr) { Ok(listener) => { + println!("โœ… SocketBox::bind() - TcpListener created successfully (Box ID: {})", self.base.id); match self.listener.lock() { Ok(mut listener_guard) => { *listener_guard = Some(listener); + println!("โœ… SocketBox::bind() - Listener stored successfully (Box ID: {})", self.base.id); }, Err(_) => { + println!("๐Ÿšจ SocketBox::bind() - Failed to acquire listener lock (Box ID: {})", self.base.id); return Box::new(BoolBox::new(false)); } } match self.is_server.lock() { Ok(mut is_server_guard) => { *is_server_guard = true; + let arc_ptr = Arc::as_ptr(&self.is_server) as usize; + // Verify the value was actually set + let verify_value = *is_server_guard; + println!("โœ… SocketBox::bind() - is_server set to true (Box ID: {}, Arc ptr: {:x}, verify: {})", self.base.id, arc_ptr, verify_value); }, Err(_) => { + println!("๐Ÿšจ SocketBox::bind() - Failed to acquire is_server lock (Box ID: {})", self.base.id); // Non-critical error, continue } } @@ -114,26 +132,36 @@ impl SocketBox { /// ๆŒ‡ๅฎšใ—ใŸ backlog ใงๆŽฅ็ถšๅพ…ๆฉŸ้–‹ๅง‹ pub fn listen(&self, backlog: Box) -> Box { let _backlog_num = backlog.to_string_box().value.parse::().unwrap_or(128); + println!("๐Ÿ” SocketBox::listen() called (Box ID: {})", self.base.id); // Check if listener exists and is properly bound let listener_guard = match self.listener.lock() { Ok(guard) => guard, - Err(_) => return Box::new(BoolBox::new(false)), + Err(_) => { + println!("๐Ÿšจ SocketBox::listen() - Failed to acquire listener lock (Box ID: {})", self.base.id); + return Box::new(BoolBox::new(false)); + }, }; if let Some(ref listener) = *listener_guard { + println!("โœ… SocketBox::listen() - Listener found (Box ID: {})", self.base.id); + let arc_ptr = Arc::as_ptr(&self.is_server) as usize; + println!("๐Ÿ” SocketBox::listen() - Arc ptr: {:x}", arc_ptr); // Try to get the local address to confirm the listener is working match listener.local_addr() { Ok(_addr) => { + println!("โœ… SocketBox::listen() - Listener is valid (Box ID: {})", self.base.id); // Listener is properly set up and can accept connections Box::new(BoolBox::new(true)) }, Err(_) => { + println!("๐Ÿšจ SocketBox::listen() - Listener exists but has issues (Box ID: {})", self.base.id); // Listener exists but has issues Box::new(BoolBox::new(false)) } } } else { + println!("๐Ÿšจ SocketBox::listen() - No listener bound (Box ID: {})", self.base.id); // No listener bound - this is expected behavior for now // HTTPServerBox will handle binding separately Box::new(BoolBox::new(false)) @@ -314,7 +342,11 @@ impl SocketBox { /// ใ‚ตใƒผใƒใƒผใƒขใƒผใƒ‰็ขบ่ช pub fn is_server(&self) -> Box { - Box::new(BoolBox::new(*self.is_server.lock().unwrap())) + let is_server_value = *self.is_server.lock().unwrap(); + let arc_ptr = Arc::as_ptr(&self.is_server) as usize; + println!("๐Ÿ” SocketBox::is_server() called - returning {} (Box ID: {}, Arc ptr: {:x})", + is_server_value, self.base.id, arc_ptr); + Box::new(BoolBox::new(is_server_value)) } } diff --git a/src/interpreter/expressions.rs b/src/interpreter/expressions.rs index 310d9aa9..57c5b4aa 100644 --- a/src/interpreter/expressions.rs +++ b/src/interpreter/expressions.rs @@ -328,6 +328,18 @@ impl NyashInterpreter { // ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’่ฉ•ไพก๏ผˆ้€šๅธธใฎใƒกใ‚ฝใƒƒใƒ‰ๅ‘ผใณๅ‡บใ—๏ผ‰ let obj_value = self.execute_expression(object)?; + // Debug: Print object type and Box ID for SocketBox debugging + if method == "bind" || method == "listen" || method == "isServer" { + println!("๐Ÿ” DEBUG: Method '{}' called on object type: {} (Box ID: {})", + method, + obj_value.type_name(), + if let Some(socket_box) = obj_value.as_any().downcast_ref::() { + socket_box.box_id().to_string() + } else { + "N/A".to_string() + }); + } + // StringBox method calls if let Some(string_box) = obj_value.as_any().downcast_ref::() { return self.execute_string_method(string_box, method, arguments);