use std::{
cell::{Cell, RefCell},
ffi::{CString, c_int},
ptr,
sync::Arc,
};
use nsh_core::{
Command, EventKind, Op,
ui::{ComponentSpec, ModuleSpec, PropKind},
};
use rquickjs::{Ctx, Value, qjs};
use rustc_hash::FxHashMap;
use crate::{
dom::{self, string_arg},
reactive::{self, arg},
};
type JsValue = qjs::JSValue;
type JsContext = qjs::JSContext;
const UNDEFINED: JsValue = qjs::JS_UNDEFINED;
const HOST_PROPS: &[&str] = &["class", "classList", "style", "testID", "onLayout"];
#[derive(Default)]
pub(crate) struct Components {
specs: RefCell<Vec<Arc<ComponentSpec>>>,
pending: RefCell<FxHashMap<u32, (JsValue, JsValue)>>,
calls: Cell<u32>,
root_listens: Cell<bool>,
}
impl Components {
pub(crate) fn free_all(&mut self, raw: *mut JsContext) {
for (_, (resolve, reject)) in self.pending.borrow_mut().drain() {
unsafe {
qjs::JS_FreeValue(raw, resolve);
qjs::JS_FreeValue(raw, reject);
}
}
}
}
fn components<'a>(ctx: *mut JsContext) -> &'a Components {
unsafe { &(*crate::state::state(ctx)).components }
}
fn new_string(ctx: *mut JsContext, s: &str) -> JsValue {
unsafe { qjs::JS_NewStringLen(ctx, s.as_ptr().cast(), s.len() as _) }
}
fn get(ctx: *mut JsContext, obj: JsValue, key: &str) -> JsValue {
let k = CString::new(key).unwrap_or_default();
unsafe {
if !qjs::JS_IsObject(obj) {
return UNDEFINED;
}
qjs::JS_GetPropertyStr(ctx, obj, k.as_ptr())
}
}
fn has(ctx: *mut JsContext, obj: JsValue, key: &str) -> bool {
unsafe {
if !qjs::JS_IsObject(obj) {
return false;
}
let k = CString::new(key).unwrap_or_default();
let atom = qjs::JS_NewAtom(ctx, k.as_ptr());
let r = qjs::JS_HasProperty(ctx, obj, atom);
qjs::JS_FreeAtom(ctx, atom);
r == 1
}
}
fn handler_name(event: &str) -> String {
let mut c = event.chars();
match c.next() {
Some(first) => format!("on{}{}", first.to_ascii_uppercase(), c.as_str()),
None => "on".to_owned(),
}
}
fn call_args(
ctx: *mut JsContext,
call: u32,
args: &[JsValue],
) -> Result<nsh_core::JsonValue, JsValue> {
let mut a = Vec::with_capacity(args.len());
for &v in args {
a.push(match dom::node_id(ctx, v) {
Some(id) => serde_json::json!({ "$node": id }),
None => {
let cx = unsafe { Ctx::from_raw(ptr::NonNull::new_unchecked(ctx)) };
let value = unsafe { Value::from_raw(cx, qjs::JS_DupValue(ctx, v)) };
match crate::protocol::to_json(&value) {
Ok(json) => json,
Err(_) => return Err(qjs::JS_EXCEPTION),
}
}
});
}
Ok(serde_json::json!({ "c": call, "a": a }))
}
fn send(ctx: *mut JsContext, id: u32, name: &str, args: &[JsValue], returns: bool) -> JsValue {
let call = if returns { next_call(ctx) } else { 0 };
let json = match call_args(ctx, call, args) {
Ok(json) => json,
Err(e) => return e,
};
let op = Op::Command {
id,
command: Command::Native {
name: name.into(),
args: json,
},
};
if let Err(e) = dom::push_op(ctx, op) {
return e;
}
if !returns {
return UNDEFINED;
}
promise(ctx, call, id == 1)
}
fn next_call(ctx: *mut JsContext) -> u32 {
let c = components(ctx);
let n = c.calls.get().wrapping_add(1).max(1);
c.calls.set(n);
n
}
fn promise(ctx: *mut JsContext, call: u32, on_root: bool) -> JsValue {
let c = components(ctx);
if on_root && !c.root_listens.replace(true) {
let settle = reactive::new_function(ctx, c"settle", settle_root, 1);
let r = dom::set_handler(ctx, 1, EventKind::NativeEvent, settle);
unsafe { qjs::JS_FreeValue(ctx, settle) };
if let Err(e) = r {
return e;
}
}
let mut funcs = [UNDEFINED; 2];
let promise = unsafe { qjs::JS_NewPromiseCapability(ctx, funcs.as_mut_ptr()) };
c.pending.borrow_mut().insert(call, (funcs[0], funcs[1]));
promise
}
pub(crate) fn measure(ctx: *mut JsContext, id: u32) -> JsValue {
let call = next_call(ctx);
if let Err(e) = dom::push_op(
ctx,
Op::Command {
id,
command: Command::Measure { call },
},
) {
return e;
}
promise(ctx, call, true)
}
fn settle(ctx: *mut JsContext, event: JsValue) -> Result<bool, JsValue> {
let name = get(ctx, event, "name");
let is_return = string_arg(ctx, name).as_deref() == Some("$return");
unsafe { qjs::JS_FreeValue(ctx, name) };
if !is_return {
return Ok(false);
}
let data = get(ctx, event, "data");
let c = get(ctx, data, "c");
let mut call = 0f64;
unsafe {
qjs::JS_ToFloat64(ctx, &mut call, c);
qjs::JS_FreeValue(ctx, c);
}
let pending = components(ctx).pending.borrow_mut().remove(&(call as u32));
let result = match pending {
Some((resolve, reject)) => unsafe {
let (f, value) = if has(ctx, data, "e") {
let message = get(ctx, data, "e");
let error = qjs::JS_NewError(ctx);
qjs::JS_SetPropertyStr(ctx, error, c"message".as_ptr(), message);
(reject, error)
} else {
(resolve, get(ctx, data, "v"))
};
let mut args = [value];
let r = qjs::JS_Call(ctx, f, UNDEFINED, 1, args.as_mut_ptr());
qjs::JS_FreeValue(ctx, value);
qjs::JS_FreeValue(ctx, resolve);
qjs::JS_FreeValue(ctx, reject);
if qjs::JS_IsException(r) {
Err(r)
} else {
qjs::JS_FreeValue(ctx, r);
Ok(true)
}
},
None => Ok(true),
};
unsafe { qjs::JS_FreeValue(ctx, data) };
result
}
unsafe extern "C" fn settle_root(
ctx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
) -> JsValue {
settle(ctx, arg(argc, argv, 0)).err().unwrap_or(UNDEFINED)
}
fn call_name(module: &ModuleSpec, namespace: Option<&str>, function: &str) -> String {
match namespace {
Some(ns) => format!("{ns}:{}.{function}", module.name),
None => format!("{}.{function}", module.name),
}
}
pub(crate) fn module_function(
ctx: *mut JsContext,
module: &ModuleSpec,
namespace: Option<&str>,
function: &str,
) -> JsValue {
let returns = module
.functions
.iter()
.find(|f| f.name == function)
.is_some_and(|f| f.returns.is_some());
let mut data = [
new_string(ctx, &call_name(module, namespace, function)),
if returns { qjs::JS_TRUE } else { qjs::JS_FALSE },
];
unsafe {
let f = qjs::JS_NewCFunctionData(ctx, Some(call_function), 0, 0, 2, data.as_mut_ptr());
qjs::JS_FreeValue(ctx, data[0]);
f
}
}
unsafe extern "C" fn call_function(
ctx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (name, returns) = unsafe { (*data, qjs::JS_ToBool(ctx, *data.add(1)) == 1) };
let Some(name) = string_arg(ctx, name) else {
return qjs::JS_EXCEPTION;
};
let args: Vec<JsValue> = (0..argc.max(0) as usize)
.map(|i| arg(argc, argv, i))
.collect();
send(ctx, 1, &name, &args, returns)
}
pub(crate) fn module(ctx: *mut JsContext, module: &ModuleSpec, namespace: Option<&str>) -> JsValue {
let obj = unsafe { qjs::JS_NewObject(ctx) };
add_functions(ctx, obj, module, namespace);
obj
}
fn add_functions(ctx: *mut JsContext, obj: JsValue, module: &ModuleSpec, namespace: Option<&str>) {
for f in &module.functions {
let func = module_function(ctx, module, namespace, f.name);
let name = CString::new(f.name).unwrap_or_default();
unsafe { qjs::JS_SetPropertyStr(ctx, obj, name.as_ptr(), func) };
}
}
pub(crate) fn runtime_module(
ctx: *mut JsContext,
specifier: &str,
module: &str,
) -> Option<JsValue> {
let registry = nsh_core::runtime::registries()
.iter()
.find(|r| r.specifier() == Some(specifier))?;
let spec = registry
.module_specs()
.into_iter()
.find(|m| m.name == module)?;
Some(self::module(ctx, &spec, registry.namespace()))
}
pub(crate) fn add_runtime_functions(
ctx: *mut JsContext,
obj: JsValue,
specifier: &str,
module: &str,
) {
let Some(registry) = nsh_core::runtime::registries()
.iter()
.find(|r| r.specifier() == Some(specifier))
else {
return;
};
if let Some(spec) = registry
.module_specs()
.into_iter()
.find(|m| m.name == module)
{
add_functions(ctx, obj, &spec, registry.namespace());
}
}
pub(crate) fn component(
ctx: *mut JsContext,
spec: Arc<ComponentSpec>,
module: Option<(Arc<ModuleSpec>, Option<&'static str>)>,
) -> JsValue {
let c = components(ctx);
let index = {
let mut specs = c.specs.borrow_mut();
specs.push(spec);
specs.len() - 1
};
let mut data = [qjs::JS_NewFloat64(index as f64)];
let f = unsafe { qjs::JS_NewCFunctionData(ctx, Some(render), 1, 0, 1, data.as_mut_ptr()) };
if let Some((module, namespace)) = module {
add_functions(ctx, f, &module, namespace);
}
f
}
fn spec_of(ctx: *mut JsContext, data: *mut JsValue) -> Arc<ComponentSpec> {
let mut index = 0f64;
unsafe { qjs::JS_ToFloat64(ctx, &mut index, *data) };
components(ctx).specs.borrow()[index as usize].clone()
}
fn prop_reader(ctx: *mut JsContext, props: JsValue, key: &str) -> JsValue {
let mut data = [props, new_string(ctx, key)];
unsafe {
let f = qjs::JS_NewCFunctionData(ctx, Some(read_prop), 0, 0, 2, data.as_mut_ptr());
qjs::JS_FreeValue(ctx, data[1]);
f
}
}
unsafe extern "C" fn read_prop(
ctx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (props, key) = unsafe { (*data, *data.add(1)) };
match string_arg(ctx, key) {
Some(key) => get(ctx, props, &key),
None => qjs::JS_EXCEPTION,
}
}
unsafe extern "C" fn render(
ctx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let spec = spec_of(ctx, data);
let props = arg(argc, argv, 0);
match render_component(ctx, &spec, props, data) {
Ok(node) => reactive::finish_value(ctx, node),
Err(e) => e,
}
}
fn render_component(
ctx: *mut JsContext,
spec: &ComponentSpec,
props: JsValue,
spec_data: *mut JsValue,
) -> Result<JsValue, JsValue> {
let node = dom::create_element(ctx, nsh_core::Tag::View)?;
let guard = Owned(ctx, node);
let name = new_string(ctx, spec.name);
let r = dom::set_prop_value(ctx, node, "component", name);
unsafe { qjs::JS_FreeValue(ctx, name) };
r?;
let mut anchor: Option<Owned> = None;
for slot in spec.slots() {
if !has(ctx, props, slot) {
continue;
}
let holder = Owned(ctx, dom::create_element(ctx, nsh_core::Tag::View)?);
let slot_name = new_string(ctx, slot);
let r = dom::set_prop_value(ctx, holder.1, "componentSlot", slot_name);
unsafe { qjs::JS_FreeValue(ctx, slot_name) };
r?;
let reader = Owned(ctx, prop_reader(ctx, props, slot));
dom::insert_value_at(ctx, holder.1, reader.1, None)?;
dom::insert_node(ctx, node, holder.1, None)?;
if anchor.is_none() {
anchor = Some(holder);
}
}
if let Some(items) = spec.items() {
let forwarded = Owned(ctx, unsafe { qjs::JS_NewObject(ctx) });
for key in crate::list::ITEMS_PROPS {
if !has(ctx, props, key) {
continue;
}
let getter = prop_reader(ctx, props, key);
let k = CString::new(*key).unwrap_or_default();
unsafe {
let atom = qjs::JS_NewAtom(ctx, k.as_ptr());
qjs::JS_DefineProperty(
ctx,
forwarded.1,
atom,
UNDEFINED,
getter,
UNDEFINED,
(qjs::JS_PROP_HAS_GET | qjs::JS_PROP_HAS_ENUMERABLE | qjs::JS_PROP_ENUMERABLE)
as c_int,
);
qjs::JS_FreeAtom(ctx, atom);
qjs::JS_FreeValue(ctx, getter);
}
}
let holder = Owned(ctx, crate::list::render(ctx, forwarded.1)?);
let slot_name = new_string(ctx, items);
let r = dom::set_prop_value(ctx, holder.1, "componentSlot", slot_name);
unsafe { qjs::JS_FreeValue(ctx, slot_name) };
r?;
dom::insert_node(ctx, node, holder.1, None)?;
if anchor.is_none() {
anchor = Some(holder);
}
}
let api = if spec.methods.is_empty() {
None
} else {
let api = Owned(ctx, unsafe { qjs::JS_NewObject(ctx) });
unsafe {
qjs::JS_SetPropertyStr(ctx, api.1, c"node".as_ptr(), qjs::JS_DupValue(ctx, node));
}
for m in &spec.methods {
let mut mdata = [
node,
new_string(ctx, m.name),
if m.returns.is_some() {
qjs::JS_TRUE
} else {
qjs::JS_FALSE
},
];
let name = CString::new(m.name).unwrap_or_default();
unsafe {
let f =
qjs::JS_NewCFunctionData(ctx, Some(call_method), 0, 0, 3, mdata.as_mut_ptr());
qjs::JS_FreeValue(ctx, mdata[1]);
qjs::JS_SetPropertyStr(ctx, api.1, name.as_ptr(), f);
}
}
Some(api)
};
let handles_events = spec
.events
.iter()
.any(|e| has(ctx, props, &handler_name(e.name)));
let lazy = (spec.lazy_children && spec.takes_children() && has(ctx, props, "children"))
.then(|| Owned(ctx, reactive::new_signal(ctx, qjs::JS_FALSE)));
if handles_events || lazy.is_some() || spec.methods.iter().any(|m| m.returns.is_some()) {
let mut edata = [props, lazy.as_ref().map_or(UNDEFINED, |l| l.1)];
let on = Owned(ctx, unsafe {
qjs::JS_NewCFunctionData(ctx, Some(on_native_event), 1, 0, 2, edata.as_mut_ptr())
});
dom::set_handler(
ctx,
dom::node_id(ctx, node).unwrap_or(0),
EventKind::NativeEvent,
on.1,
)?;
}
let mut pdata = [
node,
props,
unsafe { qjs::JS_NewArray(ctx) },
unsafe { qjs::JS_DupValue(ctx, *spec_data) },
];
let func = unsafe {
let f = qjs::JS_NewCFunctionData(ctx, Some(props_run), 0, 0, 4, pdata.as_mut_ptr());
qjs::JS_FreeValue(ctx, pdata[2]);
f
};
let r = reactive::render_effect(ctx, func);
unsafe { qjs::JS_FreeValue(ctx, r) };
let ref_ = get(ctx, props, "ref");
if unsafe { qjs::JS_IsFunction(ctx, ref_) } {
let mut args = [api.as_ref().map_or(node, |a| a.1)];
let r = reactive::untracked_with(ctx, || unsafe {
qjs::JS_Call(ctx, ref_, UNDEFINED, 1, args.as_mut_ptr())
});
unsafe {
qjs::JS_FreeValue(ctx, ref_);
if qjs::JS_IsException(r) {
return Err(r);
}
qjs::JS_FreeValue(ctx, r);
}
} else {
unsafe { qjs::JS_FreeValue(ctx, ref_) };
}
if spec.takes_children() && has(ctx, props, "children") {
let reader = Owned(ctx, prop_reader(ctx, props, "children"));
let children = match &lazy {
Some(shown) => {
let c = Owned(ctx, dom::cond_value(ctx, shown.1, reader.1));
if unsafe { qjs::JS_IsException(c.1) } {
return Err(c.take());
}
c
}
None => reader,
};
dom::insert_value_at(ctx, node, children.1, anchor.as_ref().map(|a| a.1))?;
}
Ok(guard.take())
}
struct Owned(*mut JsContext, JsValue);
impl Owned {
fn take(self) -> JsValue {
let v = self.1;
std::mem::forget(self);
v
}
}
impl Drop for Owned {
fn drop(&mut self) {
unsafe { qjs::JS_FreeValue(self.0, self.1) };
}
}
unsafe extern "C" fn props_run(
ctx: *mut JsContext,
_this: JsValue,
_argc: c_int,
_argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (node, props, prev, index) = unsafe { (*data, *data.add(1), *data.add(2), data.add(3)) };
let spec = spec_of(ctx, index);
let keys = spec
.props
.iter()
.filter(|p| p.kind == PropKind::Value)
.map(|p| (p.name, true))
.chain(HOST_PROPS.iter().map(|&h| (h, false)));
for (i, (key, own)) in keys.enumerate() {
let value = get(ctx, props, key);
unsafe {
if qjs::JS_IsException(value) {
return value;
}
let old = qjs::JS_GetPropertyUint32(ctx, prev, i as u32);
let same = qjs::JS_IsStrictEqual(ctx, old, value);
let result = if same {
Ok(())
} else {
let wire = if own {
format!("${key}")
} else {
key.to_owned()
};
dom::set_prop_with_prev(ctx, node, &wire, value, old)
};
qjs::JS_FreeValue(ctx, old);
qjs::JS_SetPropertyUint32(ctx, prev, i as u32, value);
if let Err(e) = result {
return e;
}
}
}
UNDEFINED
}
unsafe extern "C" fn call_method(
ctx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (node, name, returns) =
unsafe { (*data, *data.add(1), qjs::JS_ToBool(ctx, *data.add(2)) == 1) };
let (Some(id), Some(name)) = (dom::node_id(ctx, node), string_arg(ctx, name)) else {
return qjs::JS_EXCEPTION;
};
let args: Vec<JsValue> = (0..argc.max(0) as usize)
.map(|i| arg(argc, argv, i))
.collect();
send(ctx, id, &name, &args, returns)
}
unsafe extern "C" fn on_native_event(
ctx: *mut JsContext,
_this: JsValue,
argc: c_int,
argv: *mut JsValue,
_magic: c_int,
data: *mut JsValue,
) -> JsValue {
let (props, lazy) = unsafe { (*data, *data.add(1)) };
let event = arg(argc, argv, 0);
match settle(ctx, event) {
Ok(true) => return UNDEFINED,
Ok(false) => {}
Err(e) => return e,
}
let name = get(ctx, event, "name");
if string_arg(ctx, name).as_deref() == Some("$children") {
unsafe { qjs::JS_FreeValue(ctx, name) };
if !unsafe { qjs::JS_IsUndefined(lazy) } {
reactive::set_signal(ctx, lazy, get(ctx, event, "data"));
}
return UNDEFINED;
}
let handler = string_arg(ctx, name).map(|n| get(ctx, props, &handler_name(&n)));
unsafe {
qjs::JS_FreeValue(ctx, name);
let Some(handler) = handler else {
return qjs::JS_EXCEPTION;
};
if !qjs::JS_IsFunction(ctx, handler) {
qjs::JS_FreeValue(ctx, handler);
return UNDEFINED;
}
let mut args = [get(ctx, event, "data")];
let r = qjs::JS_Call(ctx, handler, UNDEFINED, 1, args.as_mut_ptr());
qjs::JS_FreeValue(ctx, args[0]);
qjs::JS_FreeValue(ctx, handler);
r
}
}