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
jsW_vsnprintf(char * str,size_t size,const char * fmt,va_list ap)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 }
jsW_snprintf(char * str,size_t size,const char * fmt,...)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)
signbit(double x)46 static __inline int signbit(double x) {union{double d;__int64 i;}u;u.d=x;return u.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_newfunction(js_State *J, js_Function *function, js_Environment *scope);
97 void js_newscript(js_State *J, js_Function *function, js_Environment *scope);
98 void js_loadeval(js_State *J, const char *filename, const char *source);
99 
100 js_Regexp *js_toregexp(js_State *J, int idx);
101 int js_isarrayindex(js_State *J, const char *str, int *idx);
102 int js_runeat(js_State *J, const char *s, int i);
103 int js_utfptrtoidx(const char *s, const char *p);
104 const char *js_utfidxtoptr(const char *s, int i);
105 
106 void js_dup(js_State *J);
107 void js_dup2(js_State *J);
108 void js_rot2(js_State *J);
109 void js_rot3(js_State *J);
110 void js_rot4(js_State *J);
111 void js_rot2pop1(js_State *J);
112 void js_rot3pop2(js_State *J);
113 void js_dup1rot3(js_State *J);
114 void js_dup1rot4(js_State *J);
115 
116 void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text);
117 
118 void js_trap(js_State *J, int pc); /* dump stack and environment to stdout */
119 
120 struct js_StackTrace
121 {
122 	const char *name;
123 	const char *file;
124 	int line;
125 };
126 
127 /* Exception handling */
128 
129 struct js_Jumpbuf
130 {
131 	jmp_buf buf;
132 	js_Environment *E;
133 	int envtop;
134 	int tracetop;
135 	int top, bot;
136 	int strict;
137 	js_Instruction *pc;
138 };
139 
140 void *js_savetrypc(js_State *J, js_Instruction *pc);
141 
142 #define js_trypc(J, PC) \
143 	setjmp(js_savetrypc(J, PC))
144 
145 /* String buffer */
146 
147 typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer;
148 
149 void js_putc(js_State *J, js_Buffer **sbp, int c);
150 void js_puts(js_State *J, js_Buffer **sb, const char *s);
151 void js_putm(js_State *J, js_Buffer **sb, const char *s, const char *e);
152 
153 /* State struct */
154 
155 struct js_State
156 {
157 	void *actx;
158 	void *uctx;
159 	js_Alloc alloc;
160 	js_Report report;
161 	js_Panic panic;
162 
163 	js_StringNode *strings;
164 
165 	int default_strict;
166 	int strict;
167 
168 	/* parser input source */
169 	const char *filename;
170 	const char *source;
171 	int line;
172 
173 	/* lexer state */
174 	struct { char *text; int len, cap; } lexbuf;
175 	int lexline;
176 	int lexchar;
177 	int lasttoken;
178 	int newline;
179 
180 	/* parser state */
181 	int astdepth;
182 	int astline;
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 	int nextref; /* for js_ref use */
207 	js_Object *R; /* registry of hidden values */
208 	js_Object *G; /* the global object */
209 	js_Environment *E; /* current environment scope */
210 	js_Environment *GE; /* global environment scope (at the root) */
211 
212 	/* execution stack */
213 	int top, bot;
214 	js_Value *stack;
215 
216 	/* garbage collector list */
217 	int gcmark;
218 	int gccounter;
219 	js_Environment *gcenv;
220 	js_Function *gcfun;
221 	js_Object *gcobj;
222 	js_String *gcstr;
223 
224 	/* environments on the call stack but currently not in scope */
225 	int envtop;
226 	js_Environment *envstack[JS_ENVLIMIT];
227 
228 	/* debug info stack trace */
229 	int tracetop;
230 	js_StackTrace trace[JS_ENVLIMIT];
231 
232 	/* exception stack */
233 	int trytop;
234 	js_Jumpbuf trybuf[JS_TRYLIMIT];
235 };
236 
237 #endif
238