use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use crate::classes::object_with;
use crate::runtime::{Detail, Runtime, RuntimeError, RuntimeFailure, SharedCell, TypeName, Value};
const STATE: &str = "clipboard state";
const CONSTRUCTOR: &str = "ClipboardAll.Prototype.__New";
pub(crate) fn construct(
arguments: &[Value],
_rt: &mut Runtime,
line: u32,
) -> Result<Value, RuntimeFailure> {
if arguments.is_empty() {
let bytes = _rt.desktop().clipboard_read_all().map_err(|reason| {
RuntimeFailure::Error(RuntimeError {
message: reason,
line,
})
})?;
return Ok(object_with("clipboardall", vec![(STATE, stored(&bytes))]));
}
let data = &arguments[0];
match data {
Value::Text(_) => {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: crate::runtime::requires(1, CONSTRUCTOR, TypeName::Number, data),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: data.to_text(),
},
));
}
Value::Int(_) | Value::Float(_) => {
let address = data.to_number().unwrap_or(0.0);
if address == 0.0 || crate::runtime::fract(address) != 0.0 {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: format!("Parameter #1 of {CONSTRUCTOR} is invalid."),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: data.to_text(),
},
));
}
if arguments.len() < 2 {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: format!(
"Parameter #2 of {CONSTRUCTOR} must not be omitted in this case."
),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: String::new(),
},
));
}
let size_value = &arguments[1];
let size = size_value.to_number().unwrap_or(-1.0);
if size < 0.0 {
return Err(RuntimeFailure::Error(RuntimeError {
message: "Out of memory.".to_string(),
line,
}));
}
if crate::runtime::fract(size) != 0.0 {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: format!("Parameter #2 of {CONSTRUCTOR} is invalid."),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: size_value.to_text(),
},
));
}
let bytes = _rt
.desktop()
.read_memory(address as i64, size as i64)
.map_err(|reason| {
RuntimeFailure::Error(RuntimeError {
message: reason,
line,
})
})?;
return Ok(object_with("clipboardall", vec![(STATE, stored(&bytes))]));
}
Value::Object(object) => {
if object.borrow().class.eq_ignore_ascii_case("buffer") {
let mut bytes = crate::classes::buffer::raw_bytes(data, line)?;
if let Some(size_value) = arguments.get(1) {
if !matches!(size_value, Value::Unset) {
let size = size_value.to_number().unwrap_or(-1.0);
if size < 0.0 {
return Err(RuntimeFailure::Error(RuntimeError {
message: "Out of memory.".to_string(),
line,
}));
}
if crate::runtime::fract(size) != 0.0 {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: format!(
"Parameter #2 of {CONSTRUCTOR} is invalid."
),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: size_value.to_text(),
},
));
}
bytes.resize(size as usize, 0);
}
}
return Ok(object_with("clipboardall", vec![(STATE, stored(&bytes))]));
}
}
_ => {}
}
Err(RuntimeFailure::Error(RuntimeError {
message: _rt.the_message_of_no_property(data, "Ptr", line),
line,
}))
}
pub(crate) fn property(
name: &str,
this: &Value,
_rt: &mut Runtime,
line: u32,
) -> Option<Result<Value, RuntimeFailure>> {
match name.to_lowercase().as_str() {
"size" | "length" => Some(match the_bytes(this) {
Some(bytes) => Ok(Value::Int(bytes.len() as i64)),
None => Err(RuntimeFailure::Error(RuntimeError {
message: "the clipboard data is not there.".to_string(),
line,
})),
}),
"ptr" => Some(match the_ptr_of(this) {
Some(address) => Ok(Value::Int(address)),
None => Err(RuntimeFailure::Error(RuntimeError {
message: "the clipboard data is not there.".to_string(),
line,
})),
}),
_ => None,
}
}
fn the_ptr_of(this: &Value) -> Option<i64> {
let Value::Object(object) = this else {
return None;
};
let object = object.borrow();
let state = object.properties.get(STATE)?;
let Value::Object(state_obj) = &state.value else {
return None;
};
Some(SharedCell::as_ptr(state_obj) as usize as i64)
}
fn stored(bytes: &[u8]) -> Value {
let list = Value::List(bytes.iter().map(|byte| Value::Int(*byte as i64)).collect());
object_with("clipboard-state", vec![("bytes", list)])
}
pub(crate) fn the_bytes(this: &Value) -> Option<Vec<u8>> {
let Value::Object(object) = this else {
return None;
};
let object = object.borrow();
let state = object.properties.get(STATE)?;
let Value::Object(state_obj) = &state.value else {
return None;
};
let state_obj = state_obj.borrow();
let bytes = state_obj.properties.get("bytes")?;
let Value::List(list) = &bytes.value else {
return None;
};
Some(
list.iter()
.map(|value| value.to_number().unwrap_or(0.0) as u8)
.collect(),
)
}