-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathspidermonkey.c
320 lines (284 loc) · 9.32 KB
/
spidermonkey.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* author Kevin Smith <[email protected]>
copyright 2009-2010 Basho Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <erl_driver.h>
#include "driver_comm.h"
#include "spidermonkey.h"
#include "erl_compatibility.h"
void free_error(spidermonkey_state *state);
/* The class of the global object. */
static JSClass global_class = {
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
NULL /* checkAccess */, NULL /* call */, NULL /* hasInstance */, NULL /* construct */, NULL,
{ NULL }
};
char *copy_string(const char *source) {
size_t size = strlen(source);
char *retval = ejs_alloc(size + 1);
strncpy(retval, source, size);
retval[size] = '\0';
return retval;
}
char *copy_jsstring(JSContext *cx, JSString *source) {
char *buf = JS_EncodeString(cx, source);
char *retval = copy_string(buf);
JS_free(cx, buf);
return retval;
}
void begin_request(spidermonkey_vm *vm) {
//XXX: not available
//JS_SetContextThread(vm->context);
JS_BeginRequest(vm->context);
}
void end_request(spidermonkey_vm *vm) {
JS_EndRequest(vm->context);
//XXX: not available
//JS_ClearContextThread(vm->context);
}
void on_error(JSContext *context, const char *message, JSErrorReport *report) {
if (report->flags & JSREPORT_EXCEPTION) {
spidermonkey_error *sm_error = ejs_alloc(sizeof(spidermonkey_error));
if (message != NULL) {
sm_error->msg = copy_string(message);
}
else {
sm_error->msg = copy_string("undefined error");
}
sm_error->lineno = report->lineno;
if (report->linebuf != NULL) {
sm_error->offending_source = copy_string(report->linebuf);
}
else {
sm_error->offending_source = copy_string("unknown");
}
spidermonkey_state *state = (spidermonkey_state *) JS_GetContextPrivate(context);
state->error = sm_error;
JS_SetContextPrivate(context, state);
}
}
JSBool on_branch(JSContext *context) {
JSBool return_value = JS_TRUE;
spidermonkey_state *state = (spidermonkey_state *) JS_GetContextPrivate(context);
state->branch_count++;
if (state->terminate) {
return_value = JS_FALSE;
}
else if (state->branch_count == 550) {
JS_GC(JS_GetRuntime(context));
state->branch_count = 0;
}
else if(state->branch_count % 100 == 0) {
JS_MaybeGC(context);
}
return return_value;
}
void write_timestamp(FILE *fd) {
struct tm *tmp;
time_t t;
t = time(NULL);
tmp = localtime(&t); /* or gmtime, if you want GMT^H^H^HUTC */
fprintf(fd, "%02d/%02d/%04d (%02d:%02d:%02d): ",
tmp->tm_mon+1, tmp->tm_mday, tmp->tm_year+1900,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
}
// under mozjs17, 'uintN' has been deprecated in favor of 'unsigned'
// http://sourceforge.net/p/mediatomb/discussion/440751/thread/9945a07d/#177d
JSBool js_log(JSContext *cx, unsigned argc, jsval *vp) {
if (argc != 2) {
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
}
else {
jsval *argv = JS_ARGV(cx, vp);
jsval jsfilename = argv[0];
jsval jsoutput = argv[1];
char *filename = JS_EncodeString(cx, JS_ValueToString(cx, jsfilename));
char *output = JS_EncodeString(cx, JS_ValueToString(cx, jsoutput));
FILE *fd = fopen(filename, "a+");
if (fd != NULL) {
write_timestamp(fd);
fwrite(output, 1, strlen(output), fd);
fwrite("\n", 1, strlen("\n"), fd);
fclose(fd);
JS_SET_RVAL(cx, vp, JSVAL_TRUE);
}
else {
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
}
}
return JS_TRUE;
}
void sm_configure_locale(void) {
JS_SetCStringsAreUTF8();
}
spidermonkey_vm *sm_initialize(long thread_stack, long heap_size) {
spidermonkey_vm *vm = ejs_alloc(sizeof(spidermonkey_vm));
spidermonkey_state *state = ejs_alloc(sizeof(spidermonkey_state));
state->branch_count = 0;
state->error = NULL;
state->terminate = 0;
int gc_size = (int) heap_size * 0.25;
vm->runtime = JS_NewRuntime(MAX_GC_SIZE);
JS_SetGCParameter(vm->runtime, JSGC_MAX_BYTES, heap_size);
JS_SetGCParameter(vm->runtime, JSGC_MAX_MALLOC_BYTES, gc_size);
vm->context = JS_NewContext(vm->runtime, 8192);
// XXX: changed from JS_SetNativeStackQuota, don't know if it's ok
JS_SetNativeStackQuota(JS_GetRuntime(vm->context), thread_stack);
begin_request(vm);
JS_SetOptions(vm->context, JSOPTION_VAROBJFIX);
JS_SetOptions(vm->context, JSOPTION_STRICT);
JS_SetOptions(vm->context, JSOPTION_COMPILE_N_GO);
JS_SetOptions(vm->context, JSVERSION_LATEST);
vm->global = JS_NewGlobalObject(vm->context, &global_class, NULL);
JS_InitStandardClasses(vm->context, vm->global);
JS_SetErrorReporter(vm->context, on_error);
JS_SetOperationCallback(vm->context, on_branch);
JS_SetContextPrivate(vm->context, state);
JSNative funptr = (JSNative) &js_log;
JS_DefineFunction(vm->context, JS_GetGlobalObject(vm->context), "ejsLog", funptr,
0, 0);
end_request(vm);
return vm;
}
void sm_stop(spidermonkey_vm *vm) {
begin_request(vm);
spidermonkey_state *state = (spidermonkey_state *) JS_GetContextPrivate(vm->context);
state->terminate = 1;
JS_SetContextPrivate(vm->context, state);
//Wait for any executing function to stop
//before beginning to free up any memory.
while (JS_IsRunning(vm->context)) {
sleep(1);
}
end_request(vm);
//Now we should be free to proceed with
//freeing up memory without worrying about
//crashing the VM.
if (state != NULL) {
if (state->error != NULL) {
free_error(state);
}
driver_free(state);
}
JS_SetContextPrivate(vm->context, NULL);
JS_DestroyContext(vm->context);
JS_DestroyRuntime(vm->runtime);
driver_free(vm);
}
void sm_shutdown(void) {
JS_ShutDown();
}
char *escape_quotes(char *text) {
size_t bufsize = strlen(text) * 2;
char *buf = ejs_alloc(bufsize);
memset(buf, 0, bufsize);
int i = 0;
int x = 0;
int escaped = 0;
for (i = 0; i < strlen(text); i++) {
if (text[i] == '"') {
if(!escaped) {
memcpy(&buf[x], (char *) "\\\"", 2);
x += 2;
}
else {
memcpy(&buf[x], &text[i], 1);
x++;
}
}
else {
if(text[i] =='\\') {
escaped = 1;
}
else {
escaped = 0;
}
memcpy(&buf[x], &text[i], 1);
x++;
}
}
size_t buf_size = strlen(buf);
char *retval = ejs_alloc(buf_size + 1);
strncpy(retval, buf, buf_size);
retval[buf_size] = '\0';
driver_free(buf);
return retval;
}
char *error_to_json(const spidermonkey_error *error) {
char *escaped_source = escape_quotes(error->offending_source);
/* size = length(escaped source) + length(error msg) + JSON formatting */
size_t size = strlen(escaped_source) + strlen(error->msg) + 80;
char *retval = ejs_alloc(size);
snprintf(retval, size, "{\"error\": {\"lineno\": %d, \"message\": \"%s\", \"source\": \"%s\"}}",
error->lineno, error->msg, escaped_source);
driver_free(escaped_source);
return retval;
}
void free_error(spidermonkey_state *state) {
driver_free(state->error->offending_source);
driver_free(state->error->msg);
driver_free(state->error);
state->error = NULL;
}
char *sm_eval(spidermonkey_vm *vm, const char *filename, const char *code, int handle_retval) {
char *retval = NULL;
JSScript *script;
jsval result;
if (code == NULL) {
return NULL;
}
begin_request(vm);
script = JS_CompileScript(vm->context,
vm->global,
code, strlen(code),
filename, 1);
spidermonkey_state *state = (spidermonkey_state *) JS_GetContextPrivate(vm->context);
if (state->error == NULL) {
JS_ClearPendingException(vm->context);
JS_ExecuteScript(vm->context, vm->global, script, &result);
state = (spidermonkey_state *) JS_GetContextPrivate(vm->context);
if (state->error == NULL) {
if (handle_retval) {
if (JSVAL_IS_STRING(result)) {
JSString *str = JS_ValueToString(vm->context, result);
retval = copy_jsstring(vm->context, str);
}
else {
char *tmp = JS_EncodeString(vm->context, JS_ValueToString(vm->context, result));
if(strcmp(tmp, "undefined") == 0) {
retval = copy_string("{\"error\": \"Expression returned undefined\", \"lineno\": 0, \"source\": \"unknown\"}");
}
else {
retval = copy_string("{\"error\": \"non-JSON return value\", \"lineno\": 0, \"source\": \"unknown\"}");
}
JS_free(vm->context, tmp);
}
}
}
else {
retval = error_to_json(state->error);
free_error(state);
JS_SetContextPrivate(vm->context, state);
}
}
else {
retval = error_to_json(state->error);
free_error(state);
JS_SetContextPrivate(vm->context, state);
}
end_request(vm);
return retval;
}