use std::cell::Cell;
use std::ffi::c_void;
use sk_runtime::host::global;
use sk_runtime::host_fn;
use sk_runtime::redjs::ffi::{redjs_protect, redjs_unprotect};
use sk_runtime::redjs::glue;
use sk_runtime::redjs::webapi::util::*;
use sk_runtime::JsRuntime;
thread_local! {
static LOOKUP: Cell<(usize, u64)> = const { Cell::new((0, UNDEF)) };
}
pub(crate) unsafe fn node_id(g: *mut c_void, this: u64) -> Option<u32> {
if !is_cell(this) {
return None;
}
dec_f64(get(g, this, "__skId"))
.filter(|&id| id > 0.0)
.map(|id| id as u32)
}
pub(crate) unsafe fn node_by_id(g: *mut c_void, id: u32) -> u64 {
let lookup = match LOOKUP.get() {
(ctx, f) if ctx == g as usize && id != 0 => f,
_ => return NULL,
};
match call(g, lookup, UNDEF, &[enc_f64(f64::from(id))]) {
Ok(v) if is_cell(v) => v,
_ => NULL,
}
}
host_fn!(fn set_node_lookup(g, args) {
let f = args.arg(0).0;
let (ctx, old) = LOOKUP.replace((g as usize, if is_cell(f) { f } else { UNDEF }));
unsafe {
if is_cell(f) {
redjs_protect(g, f);
}
if is_cell(old) && ctx == g as usize {
redjs_unprotect(g, old);
}
}
UNDEF
});
pub(crate) unsafe fn proto(g: *mut c_void, name: &str) -> u64 {
let ctor = get(g, global(g), name);
if is_cell(ctor) {
get(g, ctor, "prototype")
} else {
UNDEF
}
}
pub(crate) unsafe fn method(g: *mut c_void, obj: u64, name: &str, arity: u32, f: HostFn) {
define(g, obj, name, make_fn(g, name, arity, f), 2);
}
pub(crate) unsafe fn accessor(
g: *mut c_void,
obj: u64,
name: &str,
get_fn: HostFn,
set_fn: Option<HostFn>,
) {
let a = atom(g, name);
let setter = set_fn.map_or(UNDEF, |f| make_fn(g, &format!("set {name}"), 1, f));
glue::rjs_define_accessor(
g,
obj,
a,
make_fn(g, &format!("get {name}"), 0, get_fn),
setter,
2,
);
glue::rjs_atom_free(g, a);
}
pub(crate) fn install(rt: &JsRuntime) {
rt.register("__sk_set_node_lookup", 1, set_node_lookup as *const c_void);
}