/* The JavaScript engine codemode runs scripts with, behind a seam so codemode does not depend on
* it: jsengine_quickjs.c (Bellard's QuickJS), run in codemode's worker process on hosted builds and
* in pic's own process on the no-OS build. The script reaches its host through one global
* function, __host(KIND, ...), whose arguments and answer are strings. */
#ifndef PIC_JSENGINE_H
#define PIC_JSENGINE_H
#include <stddef.h>
typedef struct pic_js pic_js;
/* The script's __host(KIND, A, B, ...): ARGV[0..ARGC) its arguments as strings (NULL for
* undefined or null). The answer (malloc'd) is the call's value, NULL for undefined; with
* *THROWN set it is thrown as an Error with that message instead. */
typedef char *(*pic_js_host)(void *ctx, int argc, const char *const *argv, int *thrown);
/* An engine with at most HEAP bytes of memory and STACK bytes of stack. INTERRUPT is polled
* while JavaScript runs; nonzero ends the script with an error it cannot catch, whose message is
* "interrupted". NULL when it cannot be made. */
pic_js *pic_js_new(size_t heap, size_t stack, int (*interrupt)(void *ctx), pic_js_host host, void *ctx);
/* Evaluates SRC as a global script named FILE: 0, or -1 with the error, "Name: message" and its
* stack, in *ERR (malloc'd). */
int pic_js_eval(pic_js *js, const char *src, const char *file, char **err);
/* Runs queued promise jobs until none is left: 0, or -1 when the interrupt cut one off or a job
* failed in a way no script can catch. */
int pic_js_pump(pic_js *js);
void pic_js_free(pic_js *js);
/* Whether scripts have promises and async functions. */
int pic_js_async(void);
/* The engine's name and version, for the docs and errors. */
const char *pic_js_engine(void);
#endif