use std::time::Duration;
pub const NATIVE_MODULES: &[&str] = &[
"nsh",
"nsh:rt",
"nsh/device",
"nsh/accessibility",
"nsh/android",
"nsh/ios",
"nsh/desktop",
"nsh/webview",
"nsh/video",
"nsh/native",
"nsh/gesture",
"nsh/animated",
"nsh/canvas",
"nsh/list",
"nsh/image",
"nsh/keymap",
];
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Limits {
pub memory_bytes: usize,
pub stack_bytes: usize,
pub load_budget: Duration,
pub call_budget: Duration,
pub max_op_values: usize,
pub max_canvas_ops: usize,
pub max_nodes: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
memory_bytes: 256 << 20,
stack_bytes: 4 << 20,
load_budget: Duration::from_secs(10),
call_budget: Duration::from_secs(2),
max_op_values: 8_000_000,
max_canvas_ops: 1_000_000,
max_nodes: 200_000,
}
}
}
macro_rules! codes {
($table:ident: $($(#[doc = $doc:literal])* $name:ident = $value:literal),* $(,)?) => {
$(
#[doc = concat!("`", stringify!($name), "`.")]
$(#[doc = $doc])*
pub const $name: i32 = $value;
)*
pub const $table: &[(&str, i32, &str)] = &[$((stringify!($name), $value, concat!($($doc),*))),*];
};
}
pub mod animation;
pub mod canvas;
pub mod host;
pub(crate) mod host_values;
pub mod ops;
pub mod protocol;
pub mod style;
#[cfg(test)]
mod tests {
#[test]
fn the_declarations_match_the_tables() {
let canvas = super::canvas::declarations();
assert!(
canvas.contains(" moveTo(x: CanvasNumber, y: CanvasNumber): void;"),
"{canvas}"
);
assert!(canvas.contains("counterclockwise?: boolean): void;"));
assert!(canvas.contains(" lineCap: LineCap;"));
assert!(canvas.contains("export type LineCap = \"butt\" | \"round\" | \"square\";"));
let style = super::style::declarations();
for (name, ts) in [
("boxShadow", "string"),
("gridTemplateColumns", "string"),
("transform", "TransformStep[]"),
("tintColor", "ColorValue"),
("display", "\"flex\" | \"grid\" | \"block\" | \"none\""),
] {
assert!(style.contains(&format!(" {name}?: {ts};")), "{name}");
}
let text = style.find("export interface TextStyle").unwrap();
assert!(style[text..].contains(" textDecorationStyle?:"));
let elements = super::host::declarations();
for (interface, prop) in [
("HostProps", " pointerEvents?: PointerEvents;"),
("HostProps", " onPress?: () => void;"),
("HostProps", " onLayout?: (e: LayoutEvent) => void;"),
("AndroidHostProps", " elevation?: number;"),
("InputHostProps", " keyboardType?: KeyboardType;"),
("InputHostProps", " onChangeText?: (text: string) => void;"),
("IOSInputHostProps", " inputAccessoryViewID?: string;"),
(
"IOSInputHostProps",
" keyboardAppearance?: KeyboardAppearance;",
),
("TextHostProps", " selectionColor?: ColorValue;"),
("InputHostProps", " selectionColor?: ColorValue;"),
(
"ImageHostProps",
" onLoad?: (e: { width: number; height: number }) => void;",
),
(
"VideoHostProps",
" onLoad?: (e: { duration: number; width: number; height: number }) => void;",
),
(
"ScrollingHostProps",
" decelerationRate?: \"normal\" | \"fast\" | number;",
),
(
"ScrollingHostProps",
" onScroll?: (e: ScrollEvent) => void;",
),
(
"ListHostProps",
" contentContainerStyle?: ListContentStyle;",
),
(
"IOSModalHostProps",
" presentationStyle?: ModalPresentation;",
),
(
"DesktopModalHostProps",
" presentationStyle?: ModalPresentation;",
),
("DesktopScreenHostProps", " headerBackTitle?: string;"),
(
"DesktopWebViewHostProps",
" allowsBackForwardNavigationGestures?: boolean;",
),
("ModalHostProps", " onRequestClose?: () => void;"),
] {
let at = elements
.find(&format!("export interface {interface} {{"))
.unwrap_or_else(|| panic!("{interface}"));
let end = at + elements[at..].find("\n}").unwrap();
assert!(elements[at..end].contains(prop), "{interface}: {prop}");
}
assert!(elements.contains(
"export type KeyboardType = \"default\" | \"number-pad\" | \"decimal-pad\" | \"numeric\" | \"numbers-and-punctuation\""
));
assert!(
elements
.contains("export type KeyboardAppearance = \"default\" | \"light\" | \"dark\";")
);
let animation = super::animation::declarations();
assert!(animation.contains(" back(s?: number): EasingCurve;"));
assert!(
animation.contains("export type LayoutAnimationType = \"easeInEaseOut\" | \"spring\"")
);
assert!(animation.contains(" readonly scaleXY: \"scaleXY\";"));
for internal in ["splice?", "cellIndex?", "tailwindTheme?", "headerSlot?"] {
assert!(!elements.contains(internal), "{internal}");
}
}
#[test]
fn every_event_is_typed_or_internal() {
use super::host::{EVENTS, INTERNAL_EVENTS};
for kind in crate::EventKind::ALL {
let typed = EVENTS.iter().any(|e| e.0 == *kind);
let internal = INTERNAL_EVENTS.contains(kind);
assert!(
typed != internal,
"{}: give it a handler type in `EVENTS` or list it in `INTERNAL_EVENTS`",
kind.prop_name()
);
}
}
}