/* js_mu: the subset of the MuJS C API that js_browser.c uses, implemented on * top of QuickJS. * * The browser's DOM layer was written against MuJS's value-stack API. This * layer keeps that API (a stack of values, a registry, userdata objects with * has/put/delete hooks, js_try/js_throw) and maps it onto a QuickJS runtime, * so the page scripts get a modern ECMAScript engine while the DOM bindings * stay unchanged. * * Semantics follow MuJS: C functions see `this` at index 0 and their * arguments at 1..n (padded with undefined up to the declared length), and * return the value on top of the stack. js_typeerror()/js_throw() longjmp * to the innermost js_try() or C-function boundary; at a boundary the error * becomes a QuickJS exception. */ #ifndef JS_MU_H #define JS_MU_H #include typedef struct js_State js_State; typedef void (*js_CFunction)(js_State *J); typedef void (*js_Finalize)(js_State *J, void *p); typedef int (*js_HasProperty)(js_State *J, void *p, const char *name); typedef int (*js_Put)(js_State *J, void *p, const char *name); typedef int (*js_Delete)(js_State *J, void *p, const char *name); typedef void (*js_Report)(js_State *J, const char *message); typedef void (*js_Panic)(js_State *J); typedef void *(*js_Alloc)(void *actx, void *ptr, int size); enum { JS_READONLY = 1, JS_DONTENUM = 2, JS_DONTCONF = 4, }; js_State *js_newstate(js_Alloc alloc, void *actx, int flags); void js_freestate(js_State *J); void js_setreport(js_State *J, js_Report report); js_Panic js_atpanic(js_State *J, js_Panic panic); /* Wall-clock budget for the next entry into JavaScript, in milliseconds * (QuickJS's interrupt handler raises "interrupted" when it is spent). */ void js_setlimit(js_State *J, int limit_ms, int memlimit); /* Run queued Promise jobs (then/await continuations). */ void js_runjobs(js_State *J); /* ES modules. resolve() turns a specifier into an absolute URL relative to * the importing module (return 0 on success); load() returns the module * source as a malloc()ed NUL-terminated string (NULL on failure). */ typedef int (*js_ModuleResolve)(const char *base, const char *name, char *out, unsigned int cap); typedef char *(*js_ModuleLoad)(const char *url, unsigned int *len); void js_setmodulehost(js_State *J, js_ModuleResolve resolve, js_ModuleLoad load); /* Compile, link and evaluate a module and run the resulting jobs. Returns * 0 (undefined pushed) or 1 with the error value pushed. */ int js_ploadmodule(js_State *J, const char *filename, const char *source); int js_ploadstring(js_State *J, const char *filename, const char *source); void js_loadstring(js_State *J, const char *filename, const char *source); int js_pcall(js_State *J, int n); jmp_buf *js_savetry(js_State *J); #define js_try(J) setjmp(*js_savetry(J)) void js_endtry(js_State *J); void js_throw(js_State *J); #if defined(__GNUC__) __attribute__((noreturn, format(printf, 2, 3))) #endif void js_typeerror(js_State *J, const char *fmt, ...); void js_getglobal(js_State *J, const char *name); void js_pushglobal(js_State *J); void js_defglobal(js_State *J, const char *name, int atts); void js_getregistry(js_State *J, const char *name); void js_setregistry(js_State *J, const char *name); void js_delregistry(js_State *J, const char *name); const char *js_ref(js_State *J); void js_unref(js_State *J, const char *ref); void js_getproperty(js_State *J, int idx, const char *name); void js_setproperty(js_State *J, int idx, const char *name); void js_defaccessor(js_State *J, int idx, const char *name, int atts); void js_setindex(js_State *J, int idx, int i); void js_newobject(js_State *J); void js_newarray(js_State *J); void js_newcfunction(js_State *J, js_CFunction fun, const char *name, int length); void js_newcfunctionx(js_State *J, js_CFunction fun, const char *name, int length, void *data, js_Finalize finalize); void *js_currentfunctiondata(js_State *J); void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete del, js_Finalize finalize); void js_pushundefined(js_State *J); void js_pushnull(js_State *J); void js_pushboolean(js_State *J, int v); void js_pushnumber(js_State *J, double v); void js_pushstring(js_State *J, const char *v); void js_pushlstring(js_State *J, const char *v, int n); void js_copy(js_State *J, int idx); void js_pop(js_State *J, int n); int js_gettop(js_State *J); int js_isdefined(js_State *J, int idx); int js_isundefined(js_State *J, int idx); int js_isnull(js_State *J, int idx); int js_isboolean(js_State *J, int idx); int js_isnumber(js_State *J, int idx); int js_isstring(js_State *J, int idx); int js_isobject(js_State *J, int idx); int js_iscallable(js_State *J, int idx); int js_isuserdata(js_State *J, int idx, const char *tag); int js_toboolean(js_State *J, int idx); double js_tonumber(js_State *J, int idx); int js_tointeger(js_State *J, int idx); const char *js_tostring(js_State *J, int idx); const char *js_trystring(js_State *J, int idx, const char *error); void *js_touserdata(js_State *J, int idx, const char *tag); /* Identity for objects (listener removal), value equality otherwise. */ int js_equal(js_State *J); #endif