diff --git a/src/enviroment.rs b/src/enviroment.rs index 618f9fa..c30ef47 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, io::{BufRead, BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::Duration}; +use std::{collections::HashMap, io::{BufReader, Read, Write}, net::{self, TcpListener, TcpStream}, process, thread::sleep, time::Duration}; use crate::{errors::{create_error, print_error, ErrorSubType, ErrorType}, virtualmachine::{DecompiledOperation, Machine, TableValue, VMMemory, VMMemoryNativeFunction, VMMemoryNull, VMMemoryNumber, VMMemoryString, VMMemoryTable}}; @@ -238,6 +238,43 @@ fn kabel_irj(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { + arg_expect(&args, 0, "table", machine, op); + let id = match &args[0] { + VMMemory::Table(tbl) => { + get_mem_tbl_val(tbl, VMMemory::String(VMMemoryString { value: String::from("id"), variable_id: 0 })) + }, + _ => None + }; + if id.is_none() { + error(String::from("Stream not found"), machine, op); + } + let real_id = match &id.unwrap() { + VMMemory::Number(n) => n.value as usize, + _ => { + error(String::from("Stream is not a number"), machine, op); + 0 + } + }; + let stored_stream = machine.storage.get(real_id); + if stored_stream.is_none() { + error(String::from("Stream not found in storage"), machine, op); + } + let boxed_stream = stored_stream.unwrap(); + let stream = boxed_stream.downcast_ref::(); + if stream.is_none() { + error(String::from("Stream is not a TcpStream"), machine, op); + } + let listener = stream.unwrap(); + match listener.shutdown(net::Shutdown::Both) { + Ok(_) => {}, + Err(e) => { + error(format!("Failed to close stream: {}", e), machine, op); + return VMMemory::Null(VMMemoryNull { variable_id: 0 }); + } + }; + return VMMemory::Null(VMMemoryNull { variable_id: 0 }); +} fn kabel_keres(machine: &mut Machine, op: &DecompiledOperation, args: Vec) -> VMMemory { arg_expect(&args, 0, "table", machine, op); let id = match &args[0] { @@ -278,6 +315,10 @@ fn kabel_keres(machine: &mut Machine, op: &DecompiledOperation, args: Vec