Module registry
How native modules reach a root - generated registration, dependencies passed as constructor arguments, three layers, and the boot check that refuses to start a root with a missing module.
JS calls native code through native.<Module>. Each root (one embedded Octane screen) has its
own module registry: the set of native modules that root's bundle can call. This page explains
how the registry is filled, what the host has to do, and what happens when something is missing.
To write the module itself, see Native modules. To decide whether a feature should be a module at all, see Where a feature lives.
How it works
-
Generated code registers the modules.
bake buildwrites the registration of every module the bundles call. Nobody writes a registration table by hand, and nothing is looked up by name or found by reflection. -
Dependencies are constructor arguments. A module that needs something from the host (a view controller, an Activity, an app service) takes it as a plain constructor argument. There is no special API for view controllers or Activities, and no lookup by key.
-
A missing module does not compile. The generated code includes a factory function that takes every module that needs such a dependency as a required parameter. The host creates those modules and passes them in. Leaving one out is a compile error in the host, not a failure at launch.
-
Three layers. A root resolves its modules from three layers. A later layer replaces a module of the same name from an earlier one:
Layer What is in it Owner Built-in modules Platform,Storage,Haptics(iOS and macOS; Android and Windows:Platform)The host view, unless turned off with builtinModules: falseApp The first-party modules the bundles use and the app's modules that need nothing from a screen The app, created once and shared by its roots Root Modules created for one screen, such as one that holds that screen's view controller The screen that opens the root There are no globals: the app owns its app-layer registry and hands it to each root.
-
Final before the bundle runs. When a root launches, its registry is fixed, and the core installs every module before the first line of JS. Every module is there from the bundle's first line, and nothing is added while the bundle runs.
The built-in modules are different from the built-in features fetch, <img> and <video>.
Those are not modules and are never in a module registry
(Where a feature lives).
Write a module that needs the host
A module that needs the screen around it takes the screen in its constructor and holds it weakly, so the module never keeps the screen alive.
The module is declared once, in a spec (native/order-screen/OrderScreen.native.ts):
import type { Int, NativeModule } from '@bake-native/octane';
export interface OrderScreen extends NativeModule {
/** The tip in dollars (0: none). */
setTip(dollars: Int): void;
}Each platform implements the contract generated from it (Native modules):
final class OrderScreenModule: OrderScreenSpec {
private weak var screen: OrderViewController?
init(screen: OrderViewController) { self.screen = screen }
func setTip(dollars: Int) {
DispatchQueue.main.async { [weak self] in MainActor.assumeIsolated { self?.screen?.setTip(dollars) } }
}
}Sync members run on the JS thread. Queue the work on the main thread before you touch UIKit, AppKit or Android views, and never wait for it: the main thread may be waiting for JS.
Register the modules
bake build writes the registration. A screen that opens a root calls the generated factory
with the modules that need it and passes the registry it returns.
// Each screen: one registry, the modules the generated code creates itself (the registry
// modules) plus the screen's own, one parameter each.
let section = BakeNativeView(bundle: bundle,
modules: BakeNativeBundleModules.root(orderScreen: OrderScreenModule(screen: self)))root(...) has one parameter per app module the bundles call, typed by the module's spec. A screen
that leaves out a module the bundle calls unguarded does not compile:
error: missing argument for parameter 'orderScreen' in callModules the bundle never names in code, such as one reached through dynamic access or one used only in tests, can still be registered on a root directly.
Required and optional modules
bake build reads the app's own sources and records every native.<Module> they call in the
bundle's manifest.json:
- A module called unguarded is required (
requires.nativeModules). - A module whose calls are guarded with
hasNative('<Module>')or'<Module>' in nativeis optional. A root without it still starts. - First-party modules are covered by
requires.modulesinstead, and dynamic access (native[name]) is checked by the JS proxy at call time.
An optional module is an optional parameter of the generated factory (orderScreen: (any OrderScreenSpec)? = nil in Swift, = null in Kotlin, Option<impl OrderScreenSpec> in Rust): a
screen that has it passes it, the others leave it out.
One bundle shared by two screens with different modules guards the per-screen ones:
import { hasNative, native } from '@bake-native/octane';
// In a handler, never during render: the build-time snapshot has no native modules.
function pickTip(dollars: number) {
if (hasNative('OrderScreen')) native.OrderScreen.setTip(dollars);
}Boot check
Before the bundle runs, the root checks one thing: every module in the bundle's
requires.nativeModules is in the root's registry, the built-in modules included. So a root opened
with builtinModules: false fails when the bundle calls native.Haptics (iOS, macOS). A failed check stops the
launch, and the bundle never starts. The message names each missing module and what the root
registers:
native.OrderScreen: the bundle calls this module and this root does not register it (registered: Haptics, Platform, Storage); register it before launchA bundle built before requires.nativeModules existed is not checked.
A hot updater runs the same check before it switches to a downloaded bundle:
root.modules.missing(for: BakeNativeBundle(directory: download)) (Swift) or
bakeNativeView.modules.missing(BakeNativeModules.requiredNativeModules(manifest)) (Kotlin) returns the
missing module names; empty means the bundle can run.
Native to JS
Calls from JS to native always go through a module the screen registers. The other direction, native to JS, is a host event:
view.sendEvent("speed", json: "\"express\"") // payload: JSON textimport { host } from '@bake-native/octane';
useEffect(() => host.on('speed', (s) => setSpeed(s)), []);host.emit is removed. Using it is a build error (host-emit) and a type error. Replace it with a
function on a module the screen registers, such as native.OrderScreen.setTip(2).
Embedded views: height from content
A brownfield screen often shows Octane as one section of a native screen, between native rows.
With fitsContent the view's height follows its page:
- The view does not scroll; the native screen around it scrolls.
- The view is as tall as the page. The host reads the height and resizes the cell or frame.
- The core lays the page out with a 1 pt viewport height, so the height it reports is the page's
own.
vhunits andposition: fixedhave no useful meaning in this mode, and neither doeshost.on('scroll'), so virtual lists do not apply: the whole page is laid out.
let view = BakeNativeView(bundle: bundle, options: BakeNativeView.Options(fitsContent: true))
view.onContentHeight = { [weak self] height in
self?.invalidateIntrinsicContentSize() // a wrapper that returns view.intrinsicContentSize
}
view.launch()intrinsicContentSize is the page's height. onContentHeight is called on the main thread,
outside the layout pass, when it changes.
Example: a brownfield order screen
The order screen of a UIKit app embeds an Octane delivery card. The card's tip buttons change the native order total in the header above it. The full walk-through is in Brownfield.
Where a feature lives
The three tiers of native features - built into the framework, added to the app from a source registry, or written by the app itself - and how to pick one.
Modules with bake add
Copy native modules from a registry into your app, keep them up to date with three-way merges, and write your own from a spec.