Skip to content
pi-c Artifact [37c3bc7d31]
Login

Artifact [37c3bc7d31]

Artifact 37c3bc7d31654e707e64acaec8d6eca6d0c4b6833f238575cb341d1b20611a9a:


/* The JavaScript engine codemode runs scripts with: one per build. Hosted builds compile
 * jsengine_quickjs.c (Bellard's QuickJS, run in codemode's worker process); the no-OS build
 * compiles jsengine_micro.c (Micro QuickJS, in pic's own process, ES5 and no promises). The
 * script reaches its host through one global function, __host(KIND, ...), whose arguments and
 * answer are strings, so the codemode prelude and the host are the same on both. */
#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