calog/src/tcl/tclAdapter.h
2026-10-15 17:45:12 -05:00

31 lines
1.7 KiB
C

// tclAdapter.h -- Tcl (Tool Command Language) engine adapter for the broker.
//
// Exposes broker-registered natives into a Tcl interpreter as commands routed through one
// trampoline (the command name is objv[0], the context is per-interp assoc data). Marshals values
// between Tcl_Obj and CalogValueT: int64 (Tcl_WideInt), double, list<->list, dict<->map, and
// BINARY-SAFE strings via byte-array objects. Tcl is "everything is a string", so egress recovers
// the canonical type from the Tcl_Obj internal representation (typePtr), coercing an untyped
// numeric string to int/real and otherwise treating it as a string; nil has no Tcl equivalent and
// round-trips to the empty string, and bool round-trips to 0/1 (documented EIAS losses).
//
// Callbacks both ways ride on commands: a foreign CalogFnT pushed IN becomes a command (carrying
// the CalogFnT as clientData, released by its delete proc) whose NAME is the value handed to the
// script (call it as `$fn a b`); a Tcl command prefix wrapped with `calogCallback` becomes a
// CalogFnT the same way, so it marshals back OUT as a function value (tclToValue recognizes such a
// command by its trampoline). Tcl file/socket IO is intentionally absent -- scripts use calog's
// fs*/net* natives; the adapter provides puts/print.
#ifndef TCL_ADAPTER_H
#define TCL_ADAPTER_H
#include "calogInternal.h"
typedef struct CalogTclT CalogTclT;
int32_t calogTclCreate(CalogTclT **out, CalogT *broker, uint64_t ctxId, CalogLimitStateT *limits, bool sandboxed);
void calogTclDestroy(CalogTclT *context);
int32_t calogTclExport(CalogTclT *context, const char *name, CalogFnT **out);
int32_t calogTclExpose(CalogTclT *context, const char *name);
int32_t calogTclRun(CalogTclT *context, const char *source);
#endif