feat(napi P3): PRG -> real Node/npm in-process via N-API object dispatch

hbrtl_ext/napibridge reimplements the C++ fivenode TFNModule in Go: it calls
the addon's registered C callbacks (npmRequire/npmCall/npmMethods/npmEnd) over
cgo. FN_REQUIRE(module) gets a handle, enumerates its methods, and builds a PRG
object whose method closures proxy oObj:method(args) to Node; results decode as
scalar / native array|hash / nested object handle (e.g. Buffer). The capi shim
forwards tfn_register_callbacks -> napibridge.StoreCallbacks. Calls run on the
node main thread (sync handleRequest), so the direct tfn_npm_* path needs no
marshaling.

Verified end-to-end (node -> addon -> Go libfivenode -> PRG):
- builtin: oOs:hostname()/platform()/arch() -> real values; oOs:cpus() -> native
  array, Len()=10.
- real npm: oQR:imageSync() on qr-image (which goja could NOT run) -> Buffer
  handle -> :toString("utf8") -> valid 1138-byte SVG.

Full npm is now reachable in-process. P4 (async/Promise via worker + npmAwait)
remains for async methods like qrcode.toString.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
CharlesKWON
2026-06-15 23:04:04 +09:00
parent ce39c6c8e0
commit 959b37e9cd
3 changed files with 311 additions and 13 deletions

View File

@@ -210,6 +210,7 @@ import (
"five/hbrt"
"five/hbrtl"
"fivenode_go/hbrtl_ext/napibridge"
)
var (
@@ -218,7 +219,6 @@ var (
capiMu sync.Mutex
capiReq string
capiErr string
capiNpm unsafe.Pointer
)
// FN_NAPI_REQ is registered via HB_FUNC (same mechanism as core/ext RTL) so
@@ -288,7 +288,7 @@ func hb_bridge_free(p *C.char) {
}
//export tfn_register_callbacks
func tfn_register_callbacks(cb unsafe.Pointer) { capiNpm = cb }
func tfn_register_callbacks(cb unsafe.Pointer) { napibridge.StoreCallbacks(cb) }
func main() {}
`