Files
fivenode_go/app/capi_test.prg
Charles KWON OhJun 4a959156ce feat(bridge_capi): port fivenode_capi.c / fivenode_buffers.c to Go RTL
Seven HB_FUNCs that fivenode's bridge_*.prg layer relies on:
  _CTX_SET_JSON / _CTX_GET_JSON  — per-request context payload
  _OUT_APPEND   / _OUT_GET / _OUT_CLEAR — response body buffer
  _BRIDGE_SET_RESULT / _BRIDGE_GET_RESULT — fast-path response

Crucially per-thread, not process-global like the original C
implementation. fivenode runs single-threaded under N-API so a static
buffer per process was fine; fivenode_go runs one *hbrt.Thread per
HTTP request goroutine, so the state is keyed by *hbrt.Thread in a
sync.Map. The HTTP dispatcher will call CleanupThread once per
request to keep the map bounded (sub-phase 1a.3-3).

Also exposes Go-side helpers (OutputBytes, Result, SetContextJSON,
CleanupThread) so the dispatcher can seed the context and harvest
the response without bouncing back through PRG.

Verified with app/capi_test.prg: all seven functions behave as
expected; combined with the Five hb_jsonDecode byref fix, ctx_get()
now correctly returns hash values rather than the fallback default.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 10:44:13 +09:00

45 lines
1.3 KiB
Plaintext

// app/capi_test.prg — verifies bridge_capi RTL in isolation
// (single thread, no HTTP). Multi-thread isolation will be exercised
// once the dispatcher is wired in 1a.3-3.
FUNCTION Main()
LOCAL cJson, cOut, cResult
? "=== _CTX_SET_JSON / _CTX_GET_JSON ==="
_CTX_SET_JSON( '{"user":"alice","role":"admin"}' )
cJson := _CTX_GET_JSON()
? "ctx json:", cJson
? "ctx_get user:", ctx_get( "user", "?" )
? "ctx_get role:", ctx_get( "role", "?" )
? "ctx_get missing:", ctx_get( "nope", "DEFAULT" )
? ""
? "=== _OUT_APPEND / _OUT_GET / _OUT_CLEAR ==="
_OUT_APPEND( "Hello, " )
_OUT_APPEND( "world! " )
_OUT_APPEND( "Three." )
cOut := _OUT_GET()
? "out before clear: [" + cOut + "]"
_OUT_CLEAR()
? "out after clear: [" + _OUT_GET() + "]"
? ""
? "=== _BRIDGE_SET_RESULT / _BRIDGE_GET_RESULT ==="
_BRIDGE_SET_RESULT( "fast-path payload" )
cResult := _BRIDGE_GET_RESULT()
? "result:", cResult
RETURN NIL
// Need ctx_get here because bridge_context.prg isn't linked in yet.
FUNCTION ctx_get( cKey, xDefault )
LOCAL cJson := _CTX_GET_JSON()
LOCAL hCtx
IF Empty( cJson )
RETURN xDefault
ENDIF
hb_jsonDecode( cJson, @hCtx )
IF ValType( hCtx ) == "H" .AND. hb_HHasKey( hCtx, cKey )
RETURN hCtx[ cKey ]
ENDIF
RETURN xDefault