js_mu.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* js_mu: the subset of the MuJS C API that js_browser.c uses, implemented on
  2. * top of QuickJS.
  3. *
  4. * The browser's DOM layer was written against MuJS's value-stack API. This
  5. * layer keeps that API (a stack of values, a registry, userdata objects with
  6. * has/put/delete hooks, js_try/js_throw) and maps it onto a QuickJS runtime,
  7. * so the page scripts get a modern ECMAScript engine while the DOM bindings
  8. * stay unchanged.
  9. *
  10. * Semantics follow MuJS: C functions see `this` at index 0 and their
  11. * arguments at 1..n (padded with undefined up to the declared length), and
  12. * return the value on top of the stack. js_typeerror()/js_throw() longjmp
  13. * to the innermost js_try() or C-function boundary; at a boundary the error
  14. * becomes a QuickJS exception. */
  15. #ifndef JS_MU_H
  16. #define JS_MU_H
  17. #include <setjmp.h>
  18. typedef struct js_State js_State;
  19. typedef void (*js_CFunction)(js_State *J);
  20. typedef void (*js_Finalize)(js_State *J, void *p);
  21. typedef int (*js_HasProperty)(js_State *J, void *p, const char *name);
  22. typedef int (*js_Put)(js_State *J, void *p, const char *name);
  23. typedef int (*js_Delete)(js_State *J, void *p, const char *name);
  24. typedef void (*js_Report)(js_State *J, const char *message);
  25. typedef void (*js_Panic)(js_State *J);
  26. typedef void *(*js_Alloc)(void *actx, void *ptr, int size);
  27. enum {
  28. JS_READONLY = 1,
  29. JS_DONTENUM = 2,
  30. JS_DONTCONF = 4,
  31. };
  32. js_State *js_newstate(js_Alloc alloc, void *actx, int flags);
  33. void js_freestate(js_State *J);
  34. void js_setreport(js_State *J, js_Report report);
  35. js_Panic js_atpanic(js_State *J, js_Panic panic);
  36. /* Wall-clock budget for the next entry into JavaScript, in milliseconds
  37. * (QuickJS's interrupt handler raises "interrupted" when it is spent). */
  38. void js_setlimit(js_State *J, int limit_ms, int memlimit);
  39. /* Run queued Promise jobs (then/await continuations). */
  40. void js_runjobs(js_State *J);
  41. int js_ploadstring(js_State *J, const char *filename, const char *source);
  42. void js_loadstring(js_State *J, const char *filename, const char *source);
  43. int js_pcall(js_State *J, int n);
  44. jmp_buf *js_savetry(js_State *J);
  45. #define js_try(J) setjmp(*js_savetry(J))
  46. void js_endtry(js_State *J);
  47. void js_throw(js_State *J);
  48. #if defined(__GNUC__)
  49. __attribute__((noreturn, format(printf, 2, 3)))
  50. #endif
  51. void js_typeerror(js_State *J, const char *fmt, ...);
  52. void js_getglobal(js_State *J, const char *name);
  53. void js_pushglobal(js_State *J);
  54. void js_defglobal(js_State *J, const char *name, int atts);
  55. void js_getregistry(js_State *J, const char *name);
  56. void js_setregistry(js_State *J, const char *name);
  57. void js_delregistry(js_State *J, const char *name);
  58. const char *js_ref(js_State *J);
  59. void js_unref(js_State *J, const char *ref);
  60. void js_getproperty(js_State *J, int idx, const char *name);
  61. void js_setproperty(js_State *J, int idx, const char *name);
  62. void js_defaccessor(js_State *J, int idx, const char *name, int atts);
  63. void js_setindex(js_State *J, int idx, int i);
  64. void js_newobject(js_State *J);
  65. void js_newarray(js_State *J);
  66. void js_newcfunction(js_State *J, js_CFunction fun, const char *name,
  67. int length);
  68. void js_newcfunctionx(js_State *J, js_CFunction fun, const char *name,
  69. int length, void *data, js_Finalize finalize);
  70. void *js_currentfunctiondata(js_State *J);
  71. void js_newuserdatax(js_State *J, const char *tag, void *data,
  72. js_HasProperty has, js_Put put, js_Delete del,
  73. js_Finalize finalize);
  74. void js_pushundefined(js_State *J);
  75. void js_pushnull(js_State *J);
  76. void js_pushboolean(js_State *J, int v);
  77. void js_pushnumber(js_State *J, double v);
  78. void js_pushstring(js_State *J, const char *v);
  79. void js_pushlstring(js_State *J, const char *v, int n);
  80. void js_copy(js_State *J, int idx);
  81. void js_pop(js_State *J, int n);
  82. int js_gettop(js_State *J);
  83. int js_isdefined(js_State *J, int idx);
  84. int js_isundefined(js_State *J, int idx);
  85. int js_isnull(js_State *J, int idx);
  86. int js_isboolean(js_State *J, int idx);
  87. int js_isnumber(js_State *J, int idx);
  88. int js_isstring(js_State *J, int idx);
  89. int js_isobject(js_State *J, int idx);
  90. int js_iscallable(js_State *J, int idx);
  91. int js_isuserdata(js_State *J, int idx, const char *tag);
  92. int js_toboolean(js_State *J, int idx);
  93. double js_tonumber(js_State *J, int idx);
  94. int js_tointeger(js_State *J, int idx);
  95. const char *js_tostring(js_State *J, int idx);
  96. const char *js_trystring(js_State *J, int idx, const char *error);
  97. void *js_touserdata(js_State *J, int idx, const char *tag);
  98. /* Identity for objects (listener removal), value equality otherwise. */
  99. int js_equal(js_State *J);
  100. #endif