// wrenAdapter.h -- Wren engine adapter for the broker.
//
// Wren has no bare function calls, so natives are reached through one foreign method:
// scripts call Calog.call("name", [args]). The adapter marshals values between Wren and
// CalogValueT by value (binary-safe strings; scalars; aggregates both ways -- OUT as a
// Wren List, or a Map when keyed so a record reads as user["name"]; IN a List or a Map,
// the Map via a small calog patch to wren.c that enumerates map keys, which upstream's C
// API cannot). Functions cross both ways: OUT as a refcounted CalogFnT over a retained
// WrenHandle; a foreign CalogFnT pushed IN becomes a CalogFn instance the script calls as
// f.call([...]). Numbers are IEEE doubles, so int64 magnitudes above 2^53 lose precision.
//
// Lifetime note (as with Lua/JS): release every CalogFnT obtained from calogWrenExport
// (and drop every function value marshalled out) BEFORE calogWrenDestroy, since the
// release touches the Wren VM.

#ifndef WREN_ADAPTER_H
#define WREN_ADAPTER_H

#include "calogInternal.h"

typedef struct CalogWrenT CalogWrenT;

int32_t calogWrenCreate(CalogWrenT **out, CalogT *broker, uint64_t ctxId);
void    calogWrenDestroy(CalogWrenT *context);
int32_t calogWrenExport(CalogWrenT *context, const char *globalName, CalogFnT **out);
int32_t calogWrenExpose(CalogWrenT *context, const char *name);
int32_t calogWrenRun(CalogWrenT *context, const char *source);

#endif
