Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include "jsi.h"
2
#include "jsparse.h"
3
#include "jscompile.h"
4
#include "jsvalue.h"
5
#include "jsrun.h"
6
#include "jsbuiltin.h"
7
8
#include <assert.h>
9
10
static void *js_defaultalloc(void *actx, void *ptr, unsigned int size)
11
{
12
if (size == 0) {
13
free(ptr);
14
return NULL;
15
}
16
if (!ptr)
17
return malloc(size);
18
return realloc(ptr, size);
19
}
20
21
static void js_defaultpanic(js_State *J)
22
{
23
fprintf(stderr, "uncaught exception: %s\n", js_tostring(J, -1));
24
/* return to javascript to abort */
25
}
26
27
int js_ploadstring(js_State *J, const char *filename, const char *source)
28
{
29
if (js_try(J))
30
return 1;
31
js_loadstring(J, filename, source);
32
js_endtry(J);
33
return 0;
34
}
35
36
int js_ploadfile(js_State *J, const char *filename)
37
{
38
if (js_try(J))
39
return 1;
40
js_loadfile(J, filename);
41
js_endtry(J);
42
return 0;
43
}
44
45
static void js_loadstringx(js_State *J, const char *filename, const char *source, int iseval)
46
{
47
js_Ast *P;
48
js_Function *F;
49
50
if (js_try(J)) {
51
jsP_freeparse(J);
52
js_throw(J);
53
}
54
55
P = jsP_parse(J, filename, source);
56
F = jsC_compile(J, P);
57
jsP_freeparse(J);
58
js_newscript(J, F, iseval ? (J->strict ? J->E : NULL) : J->GE);
59
60
js_endtry(J);
61
}
62
63
void js_loadeval(js_State *J, const char *filename, const char *source)
64
{
65
js_loadstringx(J, filename, source, 1);
66
}
67
68
void js_loadstring(js_State *J, const char *filename, const char *source)
69
{
70
js_loadstringx(J, filename, source, 0);
71
}
72
73
void js_loadfile(js_State *J, const char *filename)
74
{
75
FILE *f;
76
char *s;
77
int n, t;
78
79
f = fopen(filename, "rb");
80
if (!f) {
81
js_error(J, "cannot open file: '%s'", filename);
82
}
83
84
if (fseek(f, 0, SEEK_END) < 0) {
85
fclose(f);
86
js_error(J, "cannot seek in file: '%s'", filename);
87
}
88
89
n = ftell(f);
90
if (n < 0) {
91
fclose(f);
92
js_error(J, "cannot tell in file: '%s'", filename);
93
}
94
95
if (fseek(f, 0, SEEK_SET) < 0) {
96
fclose(f);
97
js_error(J, "cannot seek in file: '%s'", filename);
98
}
99
100
s = js_malloc(J, n + 1); /* add space for string terminator */
101
if (!s) {
102
fclose(f);
103
js_error(J, "cannot allocate storage for file contents: '%s'", filename);
104
}
105
106
t = fread(s, 1, n, f);
107
if (t != n) {
108
js_free(J, s);
109
fclose(f);
110
js_error(J, "cannot read data from file: '%s'", filename);
111
}
112
113
s[n] = 0; /* zero-terminate string containing file data */
114
115
if (js_try(J)) {
116
js_free(J, s);
117
fclose(f);
118
js_throw(J);
119
}
120
121
js_loadstring(J, filename, s);
122
123
js_free(J, s);
124
fclose(f);
125
js_endtry(J);
126
}
127
128
int js_dostring(js_State *J, const char *source, int report)
129
{
130
if (js_try(J)) {
131
fprintf(stderr, "%s\n", js_tostring(J, -1));
132
js_pop(J, 1);
133
return 1;
134
}
135
js_loadstring(J, "[string]", source);
136
js_pushglobal(J);
137
js_call(J, 0);
138
if (report)
139
if (js_isdefined(J, -1))
140
printf("%s\n", js_tostring(J, -1));
141
js_pop(J, 1);
142
js_endtry(J);
143
return 0;
144
}
145
146
int js_dofile(js_State *J, const char *filename)
147
{
148
if (js_try(J)) {
149
fprintf(stderr, "%s\n", js_tostring(J, -1));
150
js_pop(J, 1);
151
return 1;
152
}
153
js_loadfile(J, filename);
154
js_pushglobal(J);
155
js_call(J, 0);
156
js_pop(J, 1);
157
js_endtry(J);
158
return 0;
159
}
160
161
js_Panic js_atpanic(js_State *J, js_Panic panic)
162
{
163
js_Panic old = J->panic;
164
J->panic = panic;
165
return old;
166
}
167
168
void js_setcontext(js_State *J, void *uctx)
169
{
170
J->uctx = uctx;
171
}
172
173
void *js_getcontext(js_State *J)
174
{
175
return J->uctx;
176
}
177
178
js_State *js_newstate(js_Alloc alloc, void *actx, int flags)
179
{
180
js_State *J;
181
182
assert(sizeof(js_Value) == 16);
183
assert(offsetof(js_Value, type) == 15);
184
185
if (!alloc)
186
alloc = js_defaultalloc;
187
188
J = alloc(actx, NULL, sizeof *J);
189
if (!J)
190
return NULL;
191
memset(J, 0, sizeof(*J));
192
J->actx = actx;
193
J->alloc = alloc;
194
195
if (flags & JS_STRICT)
196
J->strict = 1;
197
198
J->trace[0].name = "?";
199
J->trace[0].file = "[C]";
200
J->trace[0].line = 0;
201
202
J->panic = js_defaultpanic;
203
204
J->stack = alloc(actx, NULL, JS_STACKSIZE * sizeof *J->stack);
205
if (!J->stack) {
206
alloc(actx, NULL, 0);
207
return NULL;
208
}
209
210
J->gcmark = 1;
211
J->nextref = 0;
212
213
J->R = jsV_newobject(J, JS_COBJECT, NULL);
214
J->G = jsV_newobject(J, JS_COBJECT, NULL);
215
J->E = jsR_newenvironment(J, J->G, NULL);
216
J->GE = J->E;
217
218
jsB_init(J);
219
220
return J;
221
}
222
223