calog/src/berry/berryEngine.c

61 lines
1.9 KiB
C

// berryEngine.c -- bridges the Berry adapter to the actor layer's CalogEngineT vtable.
//
// Every hook runs on the owning context's thread (createInterpreter, runSource,
// destroyInterpreter), confining the Berry VM to one thread. createInterpreter builds
// the VM and exposes the registered natives; runSource evaluates a script and reports
// failure through the single error channel (result).
#include "calogInternal.h"
#include "berryAdapter.h"
static int32_t berryEngineCreate(CalogContextT *context, void **interpOut);
static void berryEngineDestroy(void *interp);
static int32_t berryEngineRun(void *interp, const char *source, CalogValueT *result);
static void berryExposeVisitor(const CalogEntryT *entry, void *ud);
static const char *const berryExtensions[] = { "be", NULL };
const CalogEngineT calogBerryEngine = {
"berry",
berryExtensions,
berryEngineCreate,
berryEngineDestroy,
berryEngineRun
};
static int32_t berryEngineCreate(CalogContextT *context, void **interpOut) {
CalogBerryT *be;
int32_t status;
*interpOut = NULL;
status = calogBerryCreate(&be, calogContextBroker(context), calogContextId(context), calogContextLimitState(context));
if (status != calogOkE) {
return status;
}
calogForEach(calogContextBroker(context), berryExposeVisitor, be);
*interpOut = be;
return calogOkE;
}
static void berryEngineDestroy(void *interp) {
calogBerryDestroy((CalogBerryT *)interp);
}
static int32_t berryEngineRun(void *interp, const char *source, CalogValueT *result) {
int32_t status;
calogValueNil(result);
status = calogBerryRun((CalogBerryT *)interp, source);
if (status != calogOkE) {
return calogFail(result, status, "berry script failed");
}
return calogOkE;
}
static void berryExposeVisitor(const CalogEntryT *entry, void *ud) {
calogBerryExpose((CalogBerryT *)ud, entry->name);
}