Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7641 views
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
15
/* Microsoft Visual C */
16
#ifdef _MSC_VER
17
#pragma warning(disable:4996) /* _CRT_SECURE_NO_WARNINGS */
18
#pragma warning(disable:4244) /* implicit conversion from double to int */
19
#pragma warning(disable:4267) /* implicit conversion of int to smaller int */
20
#define inline __inline
21
#define snprintf _snprintf
22
#define vsnprintf _vsnprintf
23
#if _MSC_VER < 1800
24
#define round(x) floor((x) < 0 ? (x) - 0.5 : (x) + 0.5)
25
#define isnan(x) _isnan(x)
26
#define isinf(x) (!_finite(x))
27
#define isfinite(x) _finite(x)
28
static __inline int signbit(double x) {union{double d;__int64 i;}u;u.d=x;return u.i>>63;}
29
#define INFINITY (DBL_MAX+DBL_MAX)
30
#define NAN (INFINITY-INFINITY)
31
#endif /* old MSVC */
32
#endif
33
34
#define nelem(a) (sizeof (a) / sizeof (a)[0])
35
36
void *js_malloc(js_State *J, unsigned int size);
37
void *js_realloc(js_State *J, void *ptr, unsigned int size);
38
void js_free(js_State *J, void *ptr);
39
40
typedef struct js_Regexp js_Regexp;
41
typedef struct js_Value js_Value;
42
typedef struct js_Object js_Object;
43
typedef struct js_String js_String;
44
typedef struct js_Ast js_Ast;
45
typedef struct js_Function js_Function;
46
typedef struct js_Environment js_Environment;
47
typedef struct js_StringNode js_StringNode;
48
typedef struct js_Jumpbuf js_Jumpbuf;
49
typedef struct js_StackTrace js_StackTrace;
50
51
/* Limits */
52
53
#define JS_STACKSIZE 256 /* value stack size */
54
#define JS_ENVLIMIT 64 /* environment stack size */
55
#define JS_TRYLIMIT 64 /* exception stack size */
56
#define JS_GCLIMIT 10000 /* run gc cycle every N allocations */
57
58
/* instruction size -- change to unsigned int if you get integer overflow syntax errors */
59
typedef unsigned short js_Instruction;
60
61
/* String interning */
62
63
const char *js_intern(js_State *J, const char *s);
64
void jsS_dumpstrings(js_State *J);
65
void jsS_freestrings(js_State *J);
66
67
/* Portable strtod and printf float formatting */
68
69
void js_fmtexp(char *p, int e);
70
void js_dtoa(double f, char *digits, int *exp, int *neg, int *ndigits);
71
double js_strtod(const char *as, char **aas);
72
73
/* Private stack functions */
74
75
void js_newfunction(js_State *J, js_Function *function, js_Environment *scope);
76
void js_newscript(js_State *J, js_Function *function, js_Environment *scope);
77
void js_loadeval(js_State *J, const char *filename, const char *source);
78
79
js_Regexp *js_toregexp(js_State *J, int idx);
80
int js_isarrayindex(js_State *J, const char *str, unsigned int *idx);
81
int js_runeat(js_State *J, const char *s, int i);
82
int js_utfptrtoidx(const char *s, const char *p);
83
const char *js_utfidxtoptr(const char *s, int i);
84
85
void js_dup(js_State *J);
86
void js_dup2(js_State *J);
87
void js_rot2(js_State *J);
88
void js_rot3(js_State *J);
89
void js_rot4(js_State *J);
90
void js_rot2pop1(js_State *J);
91
void js_rot3pop2(js_State *J);
92
void js_dup1rot3(js_State *J);
93
void js_dup1rot4(js_State *J);
94
95
void js_pushundefinedthis(js_State *J); /* push 'global' if non-strict, undefined if strict */
96
97
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text);
98
99
void js_trap(js_State *J, int pc); /* dump stack and environment to stdout */
100
101
struct js_StackTrace
102
{
103
const char *name;
104
const char *file;
105
int line;
106
};
107
108
/* Exception handling */
109
110
struct js_Jumpbuf
111
{
112
jmp_buf buf;
113
js_Environment *E;
114
int envtop;
115
int tracetop;
116
int top, bot;
117
js_Instruction *pc;
118
};
119
120
void js_savetry(js_State *J, js_Instruction *pc);
121
122
#define js_trypc(J, PC) \
123
(js_savetry(J, PC), setjmp(J->trybuf[J->trytop++].buf))
124
125
#define js_try(J) \
126
(js_savetry(J, NULL), setjmp(J->trybuf[J->trytop++].buf))
127
128
#define js_endtry(J) \
129
(--J->trytop)
130
131
/* State struct */
132
133
struct js_State
134
{
135
void *actx;
136
void *uctx;
137
js_Alloc alloc;
138
js_Panic panic;
139
140
js_StringNode *strings;
141
142
int strict;
143
144
/* parser input source */
145
const char *filename;
146
const char *source;
147
int line;
148
149
/* lexer state */
150
struct { char *text; unsigned int len, cap; } lexbuf;
151
int lexline;
152
int lexchar;
153
int lasttoken;
154
int newline;
155
156
/* parser state */
157
int astline;
158
int lookahead;
159
const char *text;
160
double number;
161
js_Ast *gcast; /* list of allocated nodes to free after parsing */
162
163
/* runtime environment */
164
js_Object *Object_prototype;
165
js_Object *Array_prototype;
166
js_Object *Function_prototype;
167
js_Object *Boolean_prototype;
168
js_Object *Number_prototype;
169
js_Object *String_prototype;
170
js_Object *RegExp_prototype;
171
js_Object *Date_prototype;
172
173
js_Object *Error_prototype;
174
js_Object *EvalError_prototype;
175
js_Object *RangeError_prototype;
176
js_Object *ReferenceError_prototype;
177
js_Object *SyntaxError_prototype;
178
js_Object *TypeError_prototype;
179
js_Object *URIError_prototype;
180
181
int nextref; /* for js_ref use */
182
js_Object *R; /* registry of hidden values */
183
js_Object *G; /* the global object */
184
js_Environment *E; /* current environment scope */
185
js_Environment *GE; /* global environment scope (at the root) */
186
187
/* execution stack */
188
int top, bot;
189
js_Value *stack;
190
191
/* garbage collector list */
192
int gcmark;
193
int gccounter;
194
js_Environment *gcenv;
195
js_Function *gcfun;
196
js_Object *gcobj;
197
js_String *gcstr;
198
199
200
/* environments on the call stack but currently not in scope */
201
int envtop;
202
js_Environment *envstack[JS_ENVLIMIT];
203
204
/* debug info stack trace */
205
int tracetop;
206
js_StackTrace trace[JS_ENVLIMIT];
207
208
/* exception stack */
209
int trytop;
210
js_Jumpbuf trybuf[JS_TRYLIMIT];
211
};
212
213
#endif
214
215