use crate::js_cast::JsCast;
use crate::link::Browser;
use crate::protocol::{DEL, GET, SET};
use std::fmt::Write;
pub struct JsValue {
pub(crate) id: u64,
pub(crate) browser: Browser,
}
impl Drop for JsValue {
fn drop(&mut self) {
let self_id = self.id;
let mut link = self.browser.0.lock().unwrap();
writeln!(link.raw_commands_buf(), "{DEL}({self_id});",).unwrap();
link.wake_outgoing_lazy();
}
}
impl Clone for JsValue {
fn clone(&self) -> Self {
let self_id = self.id;
let out_id = {
let mut link = self.browser.0.lock().unwrap();
let out_id = link.get_new_id();
writeln!(link.raw_commands_buf(), "{SET}({out_id},{GET}({self_id}));").unwrap();
link.wake_outgoing_lazy();
out_id
};
Self {
id: out_id,
browser: self.browser.clone(),
}
}
}
impl JsValue {
pub fn browser(&self) -> &Browser {
&self.browser
}
}
impl AsRef<JsValue> for JsValue {
fn as_ref(&self) -> &JsValue {
self
}
}
impl JsCast for JsValue {
fn unchecked_from_js(val: JsValue) -> Self {
val
}
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
val
}
}