| 1 | #ifndef jsi_h |
| 2 | #define jsi_h |
| 3 | |
| 4 | #include "mujs.h" |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdarg.h> |
| 10 | #include <string.h> |
| 11 | #include <setjmp.h> |
| 12 | #include <math.h> |
| 13 | #include <float.h> |
| 14 | #include <limits.h> |
| 15 | |
| 16 | /* Microsoft Visual C */ |
| 17 | #ifdef _MSC_VER |
| 18 | #pragma warning(disable:4996) /* _CRT_SECURE_NO_WARNINGS */ |
| 19 | #pragma warning(disable:4244) /* implicit conversion from double to int */ |
| 20 | #pragma warning(disable:4267) /* implicit conversion of int to smaller int */ |
| 21 | #define inline __inline |
| 22 | #if _MSC_VER < 1900 /* MSVC 2015 */ |
| 23 | #define snprintf jsW_snprintf |
| 24 | #define vsnprintf jsW_vsnprintf |
| 25 | static int jsW_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |
| 26 | { |
| 27 | int n; |
| 28 | n = _vsnprintf(str, size, fmt, ap); |
| 29 | str[size-1] = 0; |
| 30 | return n; |
| 31 | } |
| 32 | static int jsW_snprintf(char *str, size_t size, const char *fmt, ...) |
| 33 | { |
| 34 | int n; |
| 35 | va_list ap; |
| 36 | va_start(ap, fmt); |
| 37 | n = jsW_vsnprintf(str, size, fmt, ap); |
| 38 | va_end(ap); |
| 39 | return n; |
| 40 | } |
| 41 | #endif |
| 42 | #if _MSC_VER <= 1700 /* <= MSVC 2012 */ |
| 43 | #define isnan(x) _isnan(x) |
| 44 | #define isinf(x) (!_finite(x)) |
| 45 | #define isfinite(x) _finite(x) |
| 46 | static __inline int signbit(double x) { __int64 i; memcpy(&i, &x, 8); return i>>63; } |
| 47 | #define INFINITY (DBL_MAX+DBL_MAX) |
| 48 | #define NAN (INFINITY-INFINITY) |
| 49 | #endif |
| 50 | #endif |
| 51 | |
| 52 | #define soffsetof(x,y) ((int)offsetof(x,y)) |
| 53 | #define nelem(a) (int)(sizeof (a) / sizeof (a)[0]) |
| 54 | |
| 55 | void *js_malloc(js_State *J, int size); |
| 56 | void *js_realloc(js_State *J, void *ptr, int size); |
| 57 | void js_free(js_State *J, void *ptr); |
| 58 | |
| 59 | typedef struct js_Regexp js_Regexp; |
| 60 | typedef struct js_Value js_Value; |
| 61 | typedef struct js_Object js_Object; |
| 62 | typedef struct js_String js_String; |
| 63 | typedef struct js_Ast js_Ast; |
| 64 | typedef struct js_Function js_Function; |
| 65 | typedef struct js_Environment js_Environment; |
| 66 | typedef struct js_StringNode js_StringNode; |
| 67 | typedef struct js_Jumpbuf js_Jumpbuf; |
| 68 | typedef struct js_StackTrace js_StackTrace; |
| 69 | |
| 70 | /* Limits */ |
| 71 | |
| 72 | #define JS_STACKSIZE 256 /* value stack size */ |
| 73 | #define JS_ENVLIMIT 64 /* environment stack size */ |
| 74 | #define JS_TRYLIMIT 64 /* exception stack size */ |
| 75 | #define JS_GCLIMIT 10000 /* run gc cycle every N allocations */ |
| 76 | #define JS_ASTLIMIT 100 /* max nested expressions */ |
| 77 | |
| 78 | /* instruction size -- change to int if you get integer overflow syntax errors */ |
| 79 | typedef unsigned short js_Instruction; |
| 80 | |
| 81 | /* String interning */ |
| 82 | |
| 83 | char *js_strdup(js_State *J, const char *s); |
| 84 | const char *js_intern(js_State *J, const char *s); |
| 85 | void jsS_dumpstrings(js_State *J); |
| 86 | void jsS_freestrings(js_State *J); |
| 87 | |
| 88 | /* Portable strtod and printf float formatting */ |
| 89 | |
| 90 | void js_fmtexp(char *p, int e); |
| 91 | int js_grisu2(double v, char *buffer, int *K); |
| 92 | double js_strtod(const char *as, char **aas); |
| 93 | |
| 94 | /* Private stack functions */ |
| 95 | |
| 96 | void js_newarguments(js_State *J); |
| 97 | void js_newfunction(js_State *J, js_Function *function, js_Environment *scope); |
| 98 | void js_newscript(js_State *J, js_Function *function, js_Environment *scope); |
| 99 | void js_loadeval(js_State *J, const char *filename, const char *source); |
| 100 | |
| 101 | js_Regexp *js_toregexp(js_State *J, int idx); |
| 102 | int js_isarrayindex(js_State *J, const char *str, int *idx); |
| 103 | int js_runeat(js_State *J, const char *s, int i); |
| 104 | int js_utfptrtoidx(const char *s, const char *p); |
| 105 | const char *js_utfidxtoptr(const char *s, int i); |
| 106 | |
| 107 | void js_dup(js_State *J); |
| 108 | void js_dup2(js_State *J); |
| 109 | void js_rot2(js_State *J); |
| 110 | void js_rot3(js_State *J); |
| 111 | void js_rot4(js_State *J); |
| 112 | void js_rot2pop1(js_State *J); |
| 113 | void js_rot3pop2(js_State *J); |
| 114 | void js_dup1rot3(js_State *J); |
| 115 | void js_dup1rot4(js_State *J); |
| 116 | |
| 117 | void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text); |
| 118 | |
| 119 | void js_trap(js_State *J, int pc); /* dump stack and environment to stdout */ |
| 120 | |
| 121 | struct js_StackTrace |
| 122 | { |
| 123 | const char *name; |
| 124 | const char *file; |
| 125 | int line; |
| 126 | }; |
| 127 | |
| 128 | /* Exception handling */ |
| 129 | |
| 130 | struct js_Jumpbuf |
| 131 | { |
| 132 | jmp_buf buf; |
| 133 | js_Environment *E; |
| 134 | int envtop; |
| 135 | int tracetop; |
| 136 | int top, bot; |
| 137 | int strict; |
| 138 | js_Instruction *pc; |
| 139 | }; |
| 140 | |
| 141 | void *js_savetrypc(js_State *J, js_Instruction *pc); |
| 142 | |
| 143 | #define js_trypc(J, PC) \ |
| 144 | setjmp(js_savetrypc(J, PC)) |
| 145 | |
| 146 | /* String buffer */ |
| 147 | |
| 148 | typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer; |
| 149 | |
| 150 | void js_putc(js_State *J, js_Buffer **sbp, int c); |
| 151 | void js_puts(js_State *J, js_Buffer **sb, const char *s); |
| 152 | void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e); |
| 153 | |
| 154 | /* State struct */ |
| 155 | |
| 156 | struct js_State |
| 157 | { |
| 158 | void *actx; |
| 159 | void *uctx; |
| 160 | js_Alloc alloc; |
| 161 | js_Report report; |
| 162 | js_Panic panic; |
| 163 | |
| 164 | js_StringNode *strings; |
| 165 | |
| 166 | int default_strict; |
| 167 | int strict; |
| 168 | |
| 169 | /* parser input source */ |
| 170 | const char *filename; |
| 171 | const char *source; |
| 172 | int line; |
| 173 | |
| 174 | /* lexer state */ |
| 175 | struct { char *text; int len, cap; } lexbuf; |
| 176 | int lexline; |
| 177 | int lexchar; |
| 178 | int lasttoken; |
| 179 | int newline; |
| 180 | |
| 181 | /* parser state */ |
| 182 | int astdepth; |
| 183 | int lookahead; |
| 184 | const char *text; |
| 185 | double number; |
| 186 | js_Ast *gcast; /* list of allocated nodes to free after parsing */ |
| 187 | |
| 188 | /* runtime environment */ |
| 189 | js_Object *Object_prototype; |
| 190 | js_Object *Array_prototype; |
| 191 | js_Object *Function_prototype; |
| 192 | js_Object *Boolean_prototype; |
| 193 | js_Object *Number_prototype; |
| 194 | js_Object *String_prototype; |
| 195 | js_Object *RegExp_prototype; |
| 196 | js_Object *Date_prototype; |
| 197 | |
| 198 | js_Object *Error_prototype; |
| 199 | js_Object *EvalError_prototype; |
| 200 | js_Object *RangeError_prototype; |
| 201 | js_Object *ReferenceError_prototype; |
| 202 | js_Object *SyntaxError_prototype; |
| 203 | js_Object *TypeError_prototype; |
| 204 | js_Object *URIError_prototype; |
| 205 | |
| 206 | unsigned int seed; /* Math.random seed */ |
| 207 | |
| 208 | int nextref; /* for js_ref use */ |
| 209 | js_Object *R; /* registry of hidden values */ |
| 210 | js_Object *G; /* the global object */ |
| 211 | js_Environment *E; /* current environment scope */ |
| 212 | js_Environment *GE; /* global environment scope (at the root) */ |
| 213 | |
| 214 | /* execution stack */ |
| 215 | int top, bot; |
| 216 | js_Value *stack; |
| 217 | |
| 218 | /* garbage collector list */ |
| 219 | int gcpause; |
| 220 | int gcmark; |
| 221 | int gccounter; |
| 222 | js_Environment *gcenv; |
| 223 | js_Function *gcfun; |
| 224 | js_Object *gcobj; |
| 225 | js_String *gcstr; |
| 226 | |
| 227 | /* environments on the call stack but currently not in scope */ |
| 228 | int envtop; |
| 229 | js_Environment *envstack[JS_ENVLIMIT]; |
| 230 | |
| 231 | /* debug info stack trace */ |
| 232 | int tracetop; |
| 233 | js_StackTrace trace[JS_ENVLIMIT]; |
| 234 | |
| 235 | /* exception stack */ |
| 236 | int trytop; |
| 237 | js_Jumpbuf trybuf[JS_TRYLIMIT]; |
| 238 | }; |
| 239 | |
| 240 | #endif |
| 241 | |