Expand description
The engine thread of one surface.
The engine owns a nsh_core::Core on a dedicated thread and runs one
app (App): a Rust app’s root component, or a JS bundle in a QuickJS
VM (nsh_js::Host, feature js-core; js adds the standard modules
zlib and crypto). Each wake-up (input event, timer,
resize) runs the app’s code (component tasks; JS and its microtasks),
applies what JS committed, lays out, and hands one MountBatch to the
platform through a MountSink. The platform thread never blocks on
the engine.
Re-exports§
pub use storage::FileStorage;pub use storage::resolve_data_dir;pub use nsh_package as package;
Modules§
- geometry
- Units and geometry, typed as GPUI’s
gpui::geometry: a length isPoints(GPUI’sPixels),Rems, aPercentage(a fraction:0.5is 50%) or one of the lengths styles give (AbsoluteLength,DefiniteLength,Length); an angle isRadians; shapes are generic over their unit (Point,Size,Bounds,Edges,Corners). Public API takes and returns these, never a baref32for a length, an angle or a fraction (held by theunit_typestest; docs/dev/geometry.md). - logging
- The logger of the desktop release runners (
nsh-macos-app,nsh-windows-app,nsh-linux-app): one filter, one rolling file in the app’s data directory, and the platform’s own place for each line. - perf
- Performance monitoring: what a surface’s frames and commits cost while it
runs, for an on-screen monitor (
--fps,NSH_FPS=1, the app’ssetFpsEnabled) or the app’s own tooling. - shader
- The native backends’ shader API without the renderer (feature
shaderoff): no GPU is ever found (Renderer::newisNone), so shader views draw nothing of their own, and wgpu is not linked. The backends’ code stays the same either way. - updates
- Over-the-air updates of a packaged app’s code (
docs/dev/updater.md, “Packages”): signed packages (.nshpkg) the app downloaded withnsh/updater’sdownloadPackagerun instead of the bundle it shipped with, when they are newer, verify against the roots itsnsh.jsontrusts and proved they run.
Structs§
- Access
- What an app may do: its declaration and the embedder’s grant (when the embedder limits it); an action must be allowed by both.
- AppError
- An error of the app an engine runs. Its text (
fmt::Display) is the message and the stack, ascrate::MountSink::report_errorreceives it. - Bounds
- A rectangle: its top left corner (
origin) and itssize. A view’s frame is its bounds relative to its parent. - Bundle
- An app bundle: its code plus, in development, a source map.
- Byte
Size - A size in bytes, made from mebibytes or gibibytes (
ByteSize::mib(256)): how much a cache keeps at most. - Cache
Options - Where an app’s caches are kept and how much each keeps. The default: the system’s cache directory, each cache at its default size.
- Capabilities
- What an app may do (one declaration, or one grant).
Start with
Self::defaultand set the fields to grant; new capabilities default to denied. - Chunk
- A chunk of a split bundle (
nsh_bundler::Chunk): app code thatimport()loads on demand. - Clock
- The time an engine runs on: the system’s, or a manual clock that moves only when told, shared by the threads that drive the engine (a test’s).
- Edges
- A value for each edge of a box: insets, paddings, borders.
- Engine
- A running engine. Dropping it stops the thread.
- Engine
Config - Engine settings.
- Engine
Context - Typed values a host gives one engine (
EngineConfig::context), at most one per type, which its Rust code reads withengine_context. - Engine
Control - A cheap, cloneable handle for sending input to the engine from any thread.
- Environment
- What the platform knows about the surface.
- Environment
Pins - Values of the environment an engine keeps whatever its platform reports
(
EngineConfig::pinned);Noneleaves the platform’s. - File
Storage - An app’s storage kept in
storage.jsonof a directory: the app’s data directory (crate::EngineConfig::data_dir), or one the embedding app chooses. - Host
Call - A call (
host.call(name, data)) or a post (host.post(name, data)) of the app, as the embedding app’s handler gets it. A call is answered once, from any thread; dropped unanswered, it fails in the app. - Host
Channel - The embedding app’s side of the channel with the apps one of its views
runs: launch parameters, events to the app (
host.onin TSX), and handlers of the app’s calls (host.call) and posts (host.post). - Host
Environment - The settings an embedding app gives its surfaces in place of the
system’s (
None: the system’s): its own language and text size, for an app inside a native app that lets its users switch them. The platforms write them over what they read from the system (Self::apply), so the app’sIntl,locale(),localeSettings()’s languages (its language first),fontScale()and text follow them like a system change. - Host
Limits - The limits an embedding app sets for the apps it embeds (
NshLimitson iOS,NshView.setLimitson Android), and thosensh run --limitsruns an app with as such an app would: each one it leaves out keeps its default (Limits::default). - Host
Reply - How a call is answered (a
HostCall’s): once, from any thread. Dropped unanswered, the call fails in the app. - HotUpdate
- A development update that replaces the app’s changed modules in place,
keeping the JS context, the views and the state of everything else
(made by
nsh_bundler::HotBundle::patch). - Http
Head - The status line and headers of a response, before its body.
- Http
Request - An HTTP request for the platform’s network stack (made by JS
fetchand by Rust components’crate::element::fetch). - Languages
- The user’s preferred languages, most preferred first, as BCP 47 tags
(
localeSettings()’slanguages): never empty, at most eight, each once. Held inline so that the environment staysCopy. - Limits
- Resource limits of one VM.
- Loaded
Package - A verified package, unpacked (
load_package,verify_package). - Local
Engine - An engine driven by its host’s event loop instead of a thread of its
own: the browser’s main thread in WebAssembly, where there are no
threads to block. Input sent through its
EngineControlis handled at the nextLocalEngine::pump. - Locale
- A BCP 47 language tag (
en-US,zh-Hans-CN), held inline so that the environment staysCopy: the language, script and region subtags fit; variants and extensions are dropped. - Locale
Settings - The user’s locale settings beside the locale (
locale_settings(); TSlocaleSettings()): the preferred languages, the time zone and the regional preferences the systems keep beside the language (Language & Region on Apple platforms, Android’s regional preferences, Windows’ region settings). They change together, as the user changes the system’s settings or travels. Where the user set no regional preference, it is the region’s. The names areIntl’s and Foundation’s, so they pass intoIntl’s options as they are. - Main
Thread Task - A function to run once on the platform’s main thread. The platform calls
MainThreadTask::runthere. - Open
UrlRequest - A URL the app opens (
openUrlofnsh/device), as the embedding app’s URL handler gets it (HostChannel::handle_open_url). Answered once, from any thread:OpenUrlRequest::handledwhen the embedding app took it,OpenUrlRequest::declinefor the system to open it (also what dropping it unanswered does). - Package
Info - The signed package an app came from (
crate::Bundle::package): which release of it ran, for reports. - Point
- A point:
xto the right,ydown, from the origin of what it is relative to. - Points
- A length in points, the unit NSH lays out in (GPUI’s
Pixels): the platform’s density-independent unit (iOS / macOS points, Android dp, CSSpxon the web, DIPs on Windows). Style strings’pxand bare numbers are points. Device pixels areDevicePixels/ScaledPixels, met only where a platform draws or reports them. - Policy
- What a bundle’s code may do.
- Root
- The root of a Rust app: makes the app’s top component (on the engine
thread, where elements live). The engine mounts it as the content of the
surface (
nsh_engine::App::Rust). - Size
- A width and a height.
- Time
Zone - A time zone’s name as the system gives it: an IANA name
(
Europe/Berlin,America/Argentina/Buenos_Aires,UTC), or an offset zone where the system has one (Android’sGMT+01:00). Held inline so that the environment staysCopy. - Tokens
- Design tokens by name: colors with a value per appearance, numbers and font families. The default has none.
Enums§
- App
- The app an engine runs.
- AppError
Kind - What went wrong (
AppError::kind). - Bundle
Code - The code of a bundle.
- Cache
Location - The root directory of an app’s caches.
- Cache
Policy - How much one cache keeps.
- Color
Scheme - The system appearance.
- Fetch
Event - What happens to a request, in order: (for a streamed body) any number
of
BodyWanted; thenHead,Data… andEnd— orFailedat any point. - FetchOp
- What JS asks of a request it made (the JS host queues these for the engine, which hands them to the stack).
- Hour
Cycle - How hours are counted, as
Intl’shourCyclenames it. - Http
Body - The body of an
HttpRequest. - Idiom
- The kind of device (
platform.idiom). - Json
Value - The values registered native components take and emit (props, events, command arguments): JSON. Represents any valid JSON value.
- Lifecycle
- Whether the app is in use (
lifecycle()), in React Native’sAppStateterms. - Measurement
System - The units of lengths, weights and volumes.
- Os
- The operating system (
platform.os). - Storage
Change - A change to the entries, for the backend to keep.
- Temperature
Unit - The unit of temperatures.
- Weekday
- A day of the week.
Constants§
- BYTECODE_
VERSION - Identifies the bytecode format produced by
compile_bytecodeand accepted bycrate::Host::eval_bytecode: the QuickJS-NG release and revision, its bytecode format (BC_VERSION) and the byte order written. - TRIAL_
PERIOD - How long an app on
Bundle::trialruns past its first frame without a fatal error to be healthy.
Traits§
- Assets
- Reads the app’s bundled files (
asset:/…URIs): from the app’s resources, or from the dev server during development. The engine reads the fonts it registers with it, and answers the app’s requests ofasset:/URLs (fetch) from it. - Fetch
Task - A request in flight, driven from the engine thread.
- Mount
Sink - Receives mount batches on the engine thread. Implementations forward them to the UI thread.
- Network
- The platform’s HTTP stack, used by JS
fetch. - Storage
Backend - Where an app’s entries are kept: the platform’s.
- Trial
- A run on trial (
Bundle::trial): the engine tells it once whether the app is healthy or failed (later calls are its to ignore). A crash of the process tells it nothing.
Functions§
- accept_
package - Records the build of
package(verify_package) intrust: older builds of its app are refused from now on. Refused if the package or its release key was revoked since it was verified. - app_
dir_ name - The directory of app
idin a data directory: the id, ordefaultfor an app without one, or one that is no directory name. - load_
package - Verifies the package
byteswithtrust, writes its assets underassets_root(<root>/<app id>/<package hash>/, once per package) and records its build (older builds of the app are refused from now on):verify_package, thenaccept_package. - local_
storage_ prefix - The prefix of the
localStoragekeys of appapp_idon the page aturl(document.baseURI):nsh:com.example.todo:; without an id,nsh:/apps/todo/:forhttps://x.com/apps/todo/index.html?a#b. - parse_
capabilities - Parses capabilities in their JSON form (
capabilities.json, the dev protocol, an embedder’s grant). A newer NSH may have written them: what this runtime does not know is not granted (a warning says what), so a bundle built by a newer toolchain still runs here and never gets more than it declared. - resolve_
data_ dir - The data directory
nsh.json"dataDir"names (template), which the desktop runners give the engine (crate::EngineConfig::data_dir): an absolute path, or${homeDir}followed by one under this user’s home directory ($HOME;%USERPROFILE%on Windows).Nonefor anything else (a relative path,.., another${...}) and for${homeDir}without a home: the app then keeps its data where it would without"dataDir", rather than somewhere relative to where it started. - symbolicate
- Rewrites every
bundle.js:LINE:COLintext(a reported error and its stack) to the app source positionsource_map(the bundle’s) gives, as the engine does with a bundle’s own map: for stacks reported by packages published without their map (nsh symbolicate). - verify_
package load_packagewithout recording the build: the package is verified as strictly (signature, certificate, revocations, hashes, and its build not older than the highest accepted) and its assets written, but the builds accepted before keep verifying. An embedding app runs it, and once it decided the package is healthy (its first frame drawn, no fatal error for a while) callsaccept_package; if it fails before, the app can go back to the package it ran before, which still verifies.
Type Aliases§
- Fetch
Events - Receives what happens to a request started by
Network::fetch, in order, on any thread. AfterFetchTask::cancelit may still be called (the engine then ignores it).