Skip to main content

Crate nsh_engine

Crate nsh_engine 

Source
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 nsh_package as package;

Modules§

geometry
Units and geometry, typed as GPUI’s gpui::geometry: a length is Points (GPUI’s Pixels), Rems, a Percentage (a fraction: 0.5 is 50%) or one of the lengths styles give (AbsoluteLength, DefiniteLength, Length); an angle is Radians; shapes are generic over their unit (Point, Size, Bounds, Edges, Corners). Public API takes and returns these, never a bare f32 for a length, an angle or a fraction (held by the unit_types test; docs/dev/geometry.md).
perf
Performance monitoring: what a surface’s frames and commits cost while it runs, for an on-screen monitor (--fps) or the app’s own tooling.

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, as crate::MountSink::report_error receives it.
Bounds
A rectangle: its top left corner (origin) and its size. A view’s frame is its bounds relative to its parent.
Bundle
An app bundle: its code plus, in development, a source map.
ByteSize
A size in bytes, made from mebibytes or gibibytes (ByteSize::mib(256)): how much a cache keeps at most.
CacheOptions
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::default and set the fields to grant; new capabilities default to denied.
Chunk
A chunk of a split bundle (nsh_bundler::Chunk): app code that import() 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.
EngineConfig
Engine settings.
EngineContext
Typed values a host gives one engine (EngineConfig::context), at most one per type, which its Rust code reads with engine_context.
EngineControl
A cheap, cloneable handle for sending input to the engine from any thread.
Environment
What the platform knows about the surface.
EnvironmentPins
Values of the environment an engine keeps whatever its platform reports (EngineConfig::pinned); None leaves the platform’s.
FileStorage
An app’s storage kept in storage.json of a directory: the app’s in crate::EngineConfig::data_dir, or one the embedding app chooses.
HostCall
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.
HostChannel
The embedding app’s side of the channel with the apps one of its views runs: launch parameters, events to the app (host.on in TSX), and handlers of the app’s calls (host.call) and posts (host.post).
HostEnvironment
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’s Intl, locale(), fontScale() and text follow them like a system change.
HostLimits
The limits an embedding app sets for the apps it embeds (NshLimits on iOS, NshView.setLimits on Android), and those nsh run --limits runs an app with as such an app would: each one it leaves out keeps its default (Limits::default).
HostReply
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).
HttpHead
The status line and headers of a response, before its body.
HttpRequest
An HTTP request for the platform’s network stack (made by JS fetch and by Rust components’ crate::element::fetch).
Limits
Resource limits of one VM.
LoadedPackage
A verified package, unpacked (load_package, verify_package).
LocalEngine
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 EngineControl is handled at the next LocalEngine::pump.
Locale
A BCP 47 language tag (en-US, zh-Hans-CN), held inline so that the environment stays Copy: the language, script and region subtags fit; variants and extensions are dropped.
OpenUrlRequest
A URL the app opens (openUrl of nsh/device), as the embedding app’s URL handler gets it (HostChannel::handle_open_url). Answered once, from any thread: OpenUrlRequest::handled when the embedding app took it, OpenUrlRequest::decline for the system to open it (also what dropping it unanswered does).
PackageInfo
The signed package an app came from (crate::Bundle::package): which release of it ran, for reports.
Point
A point: x to the right, y down, 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, CSS px on the web, DIPs on Windows). Style strings’ px and bare numbers are points. Device pixels are DevicePixels / 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.
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.
AppErrorKind
What went wrong (AppError::kind).
BundleCode
The code of a bundle.
CacheLocation
The root directory of an app’s caches.
CachePolicy
How much one cache keeps.
ColorScheme
The system appearance.
FetchEvent
What happens to a request, in order: (for a streamed body) any number of BodyWanted; then Head, Data… and End — or Failed at 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).
HttpBody
The body of an HttpRequest.
Idiom
The kind of device (platform.idiom).
JsonValue
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’s AppState terms.
Os
The operating system (platform.os).
StorageChange
A change to the entries, for the backend to keep.

Constants§

BYTECODE_VERSION
Identifies the bytecode format produced by compile_bytecode and accepted by crate::Host::eval_bytecode: the QuickJS-NG release and revision, its bytecode format (BC_VERSION) and the byte order written.

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 of asset:/ URLs (fetch) from it.
FetchTask
A request in flight, driven from the engine thread.
MountSink
Receives mount batches on the engine thread. Implementations forward them to the UI thread.
Network
The platform’s HTTP stack, used by JS fetch.
StorageBackend
Where an app’s entries are kept: the platform’s.

Functions§

accept_package
Records the build of package (verify_package) in trust: 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 id in a data directory: the id, or default for an app without one, or one that is no directory name.
data_dir
The system’s directory for apps’ data: Application Support on Apple platforms, %APPDATA% on Windows, $XDG_DATA_HOME (~/.local/share) elsewhere. None on Android, whose files directory only the platform knows, and without a home directory. An app of its own keeps its data in <data_dir>/<app id> (crate::EngineConfig::data_dir).
load_package
Verifies the package bytes with trust, writes its assets under assets_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, then accept_package.
local_storage_prefix
The prefix of the localStorage keys of app app_id on the page at url (document.baseURI): nsh:com.example.todo:; without an id, nsh:/apps/todo/: for https://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).
symbolicate
Rewrites every bundle.js:LINE:COL in text (a reported error and its stack) to the app source position source_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_package without 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) calls accept_package; if it fails before, the app can go back to the package it ran before, which still verifies.

Type Aliases§

FetchEvents
Receives what happens to a request started by Network::fetch, in order, on any thread. After FetchTask::cancel it may still be called (the engine then ignores it).