mujs.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #ifndef mujs_h
  2. #define mujs_h
  3. #include <setjmp.h> /* required for setjmp in fz_try macro */
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define JS_VERSION_MAJOR 1
  8. #define JS_VERSION_MINOR 3
  9. #define JS_VERSION_PATCH 5
  10. #define JS_VERSION (JS_VERSION_MAJOR * 10000 + JS_VERSION_MINOR * 100 + JS_VERSION_PATCH)
  11. #define JS_CHECKVERSION(x,y,z) (JS_VERSION >= ((x) * 10000 + (y) * 100 + (z)))
  12. /* noreturn is a GCC extension */
  13. #ifdef __GNUC__
  14. #define JS_NORETURN __attribute__((noreturn))
  15. #else
  16. #ifdef _MSC_VER
  17. #define JS_NORETURN __declspec(noreturn)
  18. #else
  19. #define JS_NORETURN
  20. #endif
  21. #endif
  22. /* GCC can do type checking of printf strings */
  23. #ifdef __printflike
  24. #define JS_PRINTFLIKE __printflike
  25. #else
  26. #if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
  27. #define JS_PRINTFLIKE(fmtarg, firstvararg) \
  28. __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
  29. #else
  30. #define JS_PRINTFLIKE(fmtarg, firstvararg)
  31. #endif
  32. #endif
  33. typedef struct js_State js_State;
  34. typedef void *(*js_Alloc)(void *memctx, void *ptr, int size);
  35. typedef void (*js_Panic)(js_State *J);
  36. typedef void (*js_CFunction)(js_State *J);
  37. typedef void (*js_Finalize)(js_State *J, void *p);
  38. typedef int (*js_HasProperty)(js_State *J, void *p, const char *name);
  39. typedef int (*js_Put)(js_State *J, void *p, const char *name);
  40. typedef int (*js_Delete)(js_State *J, void *p, const char *name);
  41. typedef void (*js_Report)(js_State *J, const char *message);
  42. /* Basic functions */
  43. js_State *js_newstate(js_Alloc alloc, void *actx, int flags);
  44. void js_setcontext(js_State *J, void *uctx);
  45. void *js_getcontext(js_State *J);
  46. void js_setreport(js_State *J, js_Report report);
  47. js_Panic js_atpanic(js_State *J, js_Panic panic);
  48. void js_freestate(js_State *J);
  49. void js_gc(js_State *J, int report);
  50. int js_dostring(js_State *J, const char *source);
  51. int js_dofile(js_State *J, const char *filename);
  52. int js_ploadstring(js_State *J, const char *filename, const char *source);
  53. int js_ploadfile(js_State *J, const char *filename);
  54. int js_pcall(js_State *J, int n);
  55. int js_pconstruct(js_State *J, int n);
  56. /* Exception handling */
  57. void *js_savetry(js_State *J); /* returns a jmp_buf */
  58. #define js_try(J) \
  59. setjmp(js_savetry(J))
  60. void js_endtry(js_State *J);
  61. /* State constructor flags */
  62. enum {
  63. JS_STRICT = 1,
  64. };
  65. /* RegExp flags */
  66. enum {
  67. JS_REGEXP_G = 1,
  68. JS_REGEXP_I = 2,
  69. JS_REGEXP_M = 4,
  70. };
  71. /* Property attribute flags */
  72. enum {
  73. JS_READONLY = 1,
  74. JS_DONTENUM = 2,
  75. JS_DONTCONF = 4,
  76. };
  77. /* enum for js_type() */
  78. enum {
  79. JS_ISUNDEFINED,
  80. JS_ISNULL,
  81. JS_ISBOOLEAN,
  82. JS_ISNUMBER,
  83. JS_ISSTRING,
  84. JS_ISFUNCTION,
  85. JS_ISOBJECT
  86. };
  87. void js_report(js_State *J, const char *message);
  88. void js_newerror(js_State *J, const char *message);
  89. void js_newevalerror(js_State *J, const char *message);
  90. void js_newrangeerror(js_State *J, const char *message);
  91. void js_newreferenceerror(js_State *J, const char *message);
  92. void js_newsyntaxerror(js_State *J, const char *message);
  93. void js_newtypeerror(js_State *J, const char *message);
  94. void js_newurierror(js_State *J, const char *message);
  95. JS_NORETURN void js_error(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  96. JS_NORETURN void js_evalerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  97. JS_NORETURN void js_rangeerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  98. JS_NORETURN void js_referenceerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  99. JS_NORETURN void js_syntaxerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  100. JS_NORETURN void js_typeerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  101. JS_NORETURN void js_urierror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
  102. JS_NORETURN void js_throw(js_State *J);
  103. void js_loadstring(js_State *J, const char *filename, const char *source);
  104. void js_loadfile(js_State *J, const char *filename);
  105. void js_eval(js_State *J);
  106. void js_call(js_State *J, int n);
  107. void js_construct(js_State *J, int n);
  108. const char *js_ref(js_State *J);
  109. void js_unref(js_State *J, const char *ref);
  110. void js_getregistry(js_State *J, const char *name);
  111. void js_setregistry(js_State *J, const char *name);
  112. void js_delregistry(js_State *J, const char *name);
  113. void js_getglobal(js_State *J, const char *name);
  114. void js_setglobal(js_State *J, const char *name);
  115. void js_defglobal(js_State *J, const char *name, int atts);
  116. void js_delglobal(js_State *J, const char *name);
  117. int js_hasproperty(js_State *J, int idx, const char *name);
  118. void js_getproperty(js_State *J, int idx, const char *name);
  119. void js_setproperty(js_State *J, int idx, const char *name);
  120. void js_defproperty(js_State *J, int idx, const char *name, int atts);
  121. void js_delproperty(js_State *J, int idx, const char *name);
  122. void js_defaccessor(js_State *J, int idx, const char *name, int atts);
  123. int js_getlength(js_State *J, int idx);
  124. void js_setlength(js_State *J, int idx, int len);
  125. int js_hasindex(js_State *J, int idx, int i);
  126. void js_getindex(js_State *J, int idx, int i);
  127. void js_setindex(js_State *J, int idx, int i);
  128. void js_delindex(js_State *J, int idx, int i);
  129. void js_currentfunction(js_State *J);
  130. void *js_currentfunctiondata(js_State *J);
  131. void js_pushglobal(js_State *J);
  132. void js_pushundefined(js_State *J);
  133. void js_pushnull(js_State *J);
  134. void js_pushboolean(js_State *J, int v);
  135. void js_pushnumber(js_State *J, double v);
  136. void js_pushstring(js_State *J, const char *v);
  137. void js_pushlstring(js_State *J, const char *v, int n);
  138. void js_pushliteral(js_State *J, const char *v);
  139. void js_newobjectx(js_State *J);
  140. void js_newobject(js_State *J);
  141. void js_newarray(js_State *J);
  142. void js_newboolean(js_State *J, int v);
  143. void js_newnumber(js_State *J, double v);
  144. void js_newstring(js_State *J, const char *v);
  145. void js_newcfunction(js_State *J, js_CFunction fun, const char *name, int length);
  146. void js_newcfunctionx(js_State *J, js_CFunction fun, const char *name, int length, void *data, js_Finalize finalize);
  147. void js_newcconstructor(js_State *J, js_CFunction fun, js_CFunction con, const char *name, int length);
  148. void js_newuserdata(js_State *J, const char *tag, void *data, js_Finalize finalize);
  149. void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete del, js_Finalize finalize);
  150. void js_newregexp(js_State *J, const char *pattern, int flags);
  151. void js_pushiterator(js_State *J, int idx, int own);
  152. const char *js_nextiterator(js_State *J, int idx);
  153. int js_isdefined(js_State *J, int idx);
  154. int js_isundefined(js_State *J, int idx);
  155. int js_isnull(js_State *J, int idx);
  156. int js_isboolean(js_State *J, int idx);
  157. int js_isnumber(js_State *J, int idx);
  158. int js_isstring(js_State *J, int idx);
  159. int js_isprimitive(js_State *J, int idx);
  160. int js_isobject(js_State *J, int idx);
  161. int js_isarray(js_State *J, int idx);
  162. int js_isregexp(js_State *J, int idx);
  163. int js_iscoercible(js_State *J, int idx);
  164. int js_iscallable(js_State *J, int idx);
  165. int js_isuserdata(js_State *J, int idx, const char *tag);
  166. int js_iserror(js_State *J, int idx);
  167. int js_isnumberobject(js_State *J, int idx);
  168. int js_isstringobject(js_State *J, int idx);
  169. int js_isbooleanobject(js_State *J, int idx);
  170. int js_isdateobject(js_State *J, int idx);
  171. int js_toboolean(js_State *J, int idx);
  172. double js_tonumber(js_State *J, int idx);
  173. const char *js_tostring(js_State *J, int idx);
  174. void *js_touserdata(js_State *J, int idx, const char *tag);
  175. const char *js_trystring(js_State *J, int idx, const char *error);
  176. double js_trynumber(js_State *J, int idx, double error);
  177. int js_tryinteger(js_State *J, int idx, int error);
  178. int js_tryboolean(js_State *J, int idx, int error);
  179. int js_tointeger(js_State *J, int idx);
  180. int js_toint32(js_State *J, int idx);
  181. unsigned int js_touint32(js_State *J, int idx);
  182. short js_toint16(js_State *J, int idx);
  183. unsigned short js_touint16(js_State *J, int idx);
  184. int js_gettop(js_State *J);
  185. void js_pop(js_State *J, int n);
  186. void js_rot(js_State *J, int n);
  187. void js_copy(js_State *J, int idx);
  188. void js_remove(js_State *J, int idx);
  189. void js_insert(js_State *J, int idx);
  190. void js_replace(js_State* J, int idx);
  191. void js_dup(js_State *J);
  192. void js_dup2(js_State *J);
  193. void js_rot2(js_State *J);
  194. void js_rot3(js_State *J);
  195. void js_rot4(js_State *J);
  196. void js_rot2pop1(js_State *J);
  197. void js_rot3pop2(js_State *J);
  198. void js_concat(js_State *J);
  199. int js_compare(js_State *J, int *okay);
  200. int js_equal(js_State *J);
  201. int js_strictequal(js_State *J);
  202. int js_instanceof(js_State *J);
  203. const char *js_typeof(js_State *J, int idx);
  204. int js_type(js_State *J, int idx);
  205. void js_repr(js_State *J, int idx);
  206. const char *js_torepr(js_State *J, int idx);
  207. const char *js_tryrepr(js_State *J, int idx, const char *error);
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. #endif