sys — Layer 0 of napi-go
1:1 bindings for all Node-API 1–10 functions (154 functions, node-api-headers 1.9.0),
generated by internal/gen. This is the foundation for Layer 1 (napi, Phase 2); use it
directly only when raw ABI access is needed.
func init() {
sys.SetModuleInitHandler(func(env sys.Env, exports sys.Value) sys.Value {
var fn sys.Value
sys.CreateFunction(env, name, sys.AutoLength, sys.CallbackTrampoline, 0, &fn)
sys.SetNamedProperty(env, exports, name, fn)
return exports
})
sys.SetCallbackHandler(func(env sys.Env, info sys.CallbackInfo, data uintptr, this sys.Value, args []sys.Value) sys.Value {
// data = 0: the function registered above
})
}
Complete example: tests/e2e/addons/sysbasic.
Structure
| File | Contents |
|---|---|
include/ |
vendored Node-API headers (types only) and symbols.js |
zz_api.h, zz_api.c |
generated: gn_api[] function pointer table and static inline C wrappers |
zz_api.go |
generated: Go wrappers, Fn enum, #cgo noescape directives |
zz_enums.go, zz_enums_check.go |
generated: Node-API enums and compile-time value checks against the C compiler |
zz_nocallback*.go |
generated: #cgo nocallback directives, per build tag |
types.go |
handles, function pointers, mirror structs with compile-time layout checks |
handlers.go |
C → Go entry trampolines and handlers, //export |
version_*.go |
declared Node-API version, per build tag |
loader.c |
runtime symbol resolution from the host process (dlsym / GetProcAddress) |
module.c |
NAPI_MODULE_INIT: symbol loading, runtime guard, module registration, signal handler repair and SA_ONSTACK hook |
trampoline.c |
C trampolines (callbacks, finalizers, TSFN, async work, cleanup hooks) |
guard.c |
guard against a second Go runtime on the same thread (macOS x64 only) |
sigfix.c |
restores SA_RESETHAND on node::SignalExit (macOS) |
typed.c, typed.go |
typed callback trampoline for Layer 2: primitive arguments and results converted in C, this unwrapped in C, one cgo transition per call |
onstack.c |
adds SA_ONSTACK to the signal handlers Node/libuv install after the module loads (Linux, GOT hook of sigaction) |
To regenerate: go generate ./sys. CI verifies that generated files are up to date with
go run ./internal/gen -check. Decisions that cannot be inferred from the headers (parameter
overrides, noescape/nocallback, Go enum names) live in internal/gen/policy.go.
Design Choices
Runtime-resolved symbols. No import library and no undefined symbols at link time: the
.node binary does not link against node.exe/node and compiles with go build/go test
without Node installed. Calling a function missing from the host Node process returns
StatusUnavailable; Available(Fn…) checks availability in advance. Functions up to the
declared Node-API version are required: if any are missing, module loading fails with
ERR_NAPI_GO_MISSING_FUNCTIONS.
By-value outputs. cgo avoids moving a Go pointer passed to C onto the heap only if the
function has both noescape and nocallback. Functions that may execute JS (getters,
Proxies, call_function, ...) cannot be marked nocallback, so &result would escape to the
heap on every call. Therefore, each C wrapper returns outputs by value in a struct: the Go
signature stays 1:1 (pointers, nil = NULL), but no Go output pointer crosses cgo.
Measured result in e2e tests: 0 allocations per call, even for GetNamedProperty.
Opaque data as uintptr. void* values retained by Node-API (data, hint, context, ...) are
exposed as uintptr: cgo rules forbid handing Go pointers to C for long-term storage, and the
type makes this explicit.
noescape / nocallback. Still useful for input buffers (strings, argument arrays).
nocallback is applied only to functions that never execute JS; if one were to re-enter Go, the
Go runtime would terminate the process with an explicit fatal error. With napi_go_experimental,
finalizers can run synchronously during V8 GC (verified: all run inside gc()), so
nocallback is restricted to functions that never allocate on the V8 heap. The cost is
measurable: stack buffers passed to e.g. GetValueStringUTF8 escape to the heap (1 allocation
per iteration in test benchmarks, vs. 0 with versions 8–10).
Declared Node-API Version
| Build tag | DeclaredVersion |
Required functions |
|---|---|---|
| (none) | 8 | Node-API 1–8 |
napi_go_v9 |
9 | 1–9 |
napi_go_v10 |
10 | 1–10 |
napi_go_experimental |
NAPI_VERSION_EXPERIMENTAL |
1–10 |
napi_go_test_version_mismatch |
NAPI_VERSION_EXPERIMENTAL - 1 |
test only: Node.js rejects the module |
node-api-headers does not include experimental APIs: napi_go_experimental only changes the
declared version (and with it Node's runtime behavior, e.g. synchronous finalizers).
napi_go_test_version_mismatch declares a version no Node.js supports and is used solely to
test module rejection (tests/nodesuite, test_module_version_mismatch).
An addon that does not register a ModuleInitHandler fails to load: require() throws
ERR_NAPI_GO_NO_HANDLER with Node.js's standard message for a module without an entry point
("Module has no declared entry point.").
Known Limitations
- macOS x64: one Go addon per process. On darwin/amd64 all Go runtimes store
gin the fixed TLS slot0x30(GS)(golang/go#23617).guard.crejects entry into a second Go runtime withERR_NAPI_GO_FOREIGN_RUNTIMEinstead of corrupting memory. However, the rejected runtime has already initialized and its signal handlers remain installed; this still needs further investigation. - musl/Alpine is unsupported: Go releases (≤ 1.27) cannot load
c-sharedlibraries on musl. - Windows: each JS → Go entry costs ~1 µs (missing
x_cgo_bindmin the Go runtime).
Details and benchmarks: spike/README.md (Phase 0). The spike was removed after its tests moved to
tests/e2e; its last version is in commit ad50e4c.