Skip to main content

Module native_cmds

Module native_cmds 

Source
Expand description

native_cmds submodule — builtins contributed by the linking binary (the fat zshrs-native build registers git / arb / stryke here). Host-registered native commands — builtins contributed by the binary rather than by this library.

EXT_BUILTIN_NAMES (extensions/ext_builtins.rs) and the daemon’s ZSHRS_BUILTIN_NAMES are both compile-time lists owned by this crate. A fat binary that links sibling runtimes into the shell’s address space — zshrs-native links zvcs (git), arblang (arb) and strykelang (stryke) — has no way to extend either: they are const arrays, and the runtimes cannot be dependencies of this crate (zvcs depends on its own vendored gitoxide by path, which makes any dependent unpublishable).

So the binary registers them here, once, before the shell starts. A registered name dispatches in-process on a direct function call: no fork, no execve, no PATH walk, no dynamic loader — the same treatment cat and sort already get from reg_overridable!.

§Dispatch order

Registration does not jump the queue. zsh resolves a command word as alias → function → builtin → external (c:Src/exec.c:3038-3068), and a native command sits in the builtin slot, after the ported builtin table:

  • a user git() { … } still wins, exactly as it shadows cat today;
  • command git still reaches whatever git is on PATH, because the forced-external path never consults this registry;
  • builtin git reaches the native one.

§Registration is one-shot and start-up only

The table is written once by the binary’s main before the shell runs and is read from every command dispatch after that, including from the worker threads. It is therefore an RwLock whose write side is expected to be uncontended: registering after startup is allowed but pointless, and no path ever removes an entry — a name that answered whence -w one moment must not vanish the next.

Structs§

ForcedExternalGuard
Restores the previous command-prefix state on drop, so a nested dispatch (a command inside a function a native command ran) unwinds correctly instead of leaving the flag stuck on.

Functions§

dispatch
Run name with argv (argv[0] included) if it is registered.
force_external
Mark the current invocation as command-forced for as long as the returned guard lives.
is_enabled
Registered and not masked by disable NAME.
is_forced_external
True while a command NAME precommand is dispatching NAME, i.e. while the user has explicitly asked for the PATH binary rather than this table.
is_registered
Is name a host-registered native command?
names
Every registered name, sorted. Feeds the builtins magic assoc (${(k)builtins}), whence -m, and compsys’s command-position completion, so the shell reports the same set it will actually dispatch.
register
Register name as a native command backed by f.

Type Aliases§

NativeCmd
A native command body: the full argv (argv[0] is the command name, as invoked) in, a wait-status-style exit code out.