Lines 15-21
Link Here
|
15 |
#include <string.h> |
15 |
#include <string.h> |
16 |
#include <jsapi.h> |
16 |
#include <jsapi.h> |
17 |
#include "config.h" |
17 |
#include "config.h" |
18 |
|
|
|
19 |
#include "utf8.h" |
18 |
#include "utf8.h" |
20 |
#include "http.h" |
19 |
#include "http.h" |
21 |
|
20 |
|
Lines 33-40
int gExitCode = 0;
Link Here
|
33 |
#define FINISH_REQUEST(cx) |
32 |
#define FINISH_REQUEST(cx) |
34 |
#endif |
33 |
#endif |
35 |
|
34 |
|
|
|
35 |
static JSClass global_class = { |
36 |
"GlobalClass", |
37 |
JSCLASS_GLOBAL_FLAGS | JSCLASS_HAS_PRIVATE, |
38 |
JS_PropertyStub, |
39 |
JS_PropertyStub, |
40 |
JS_PropertyStub, |
41 |
JS_StrictPropertyStub, |
42 |
JS_EnumerateStub, |
43 |
JS_ResolveStub, |
44 |
JS_ConvertStub, |
45 |
JS_FinalizeStub, |
46 |
JSCLASS_NO_OPTIONAL_MEMBERS |
47 |
}; |
48 |
|
36 |
static JSBool |
49 |
static JSBool |
37 |
evalcx(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) |
50 |
evalcx(JSContext *cx, uintN argc, jsval *vp) |
38 |
{ |
51 |
{ |
39 |
JSString *str; |
52 |
JSString *str; |
40 |
JSObject *sandbox; |
53 |
JSObject *sandbox; |
Lines 45-55
evalcx(JSContext *cx, JSObject *obj, uin
Link Here
|
45 |
jsval v; |
58 |
jsval v; |
46 |
|
59 |
|
47 |
sandbox = NULL; |
60 |
sandbox = NULL; |
48 |
if(!JS_ConvertArguments(cx, argc, argv, "S / o", &str, &sandbox)) |
61 |
if(!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "S / o", &str, &sandbox)) |
49 |
{ |
62 |
{ |
50 |
return JS_FALSE; |
63 |
return JS_FALSE; |
51 |
} |
64 |
} |
52 |
|
65 |
|
|
|
66 |
|
53 |
subcx = JS_NewContext(JS_GetRuntime(cx), 8L * 1024L); |
67 |
subcx = JS_NewContext(JS_GetRuntime(cx), 8L * 1024L); |
54 |
if(!subcx) |
68 |
if(!subcx) |
55 |
{ |
69 |
{ |
Lines 57-80
evalcx(JSContext *cx, JSObject *obj, uin
Link Here
|
57 |
return JS_FALSE; |
71 |
return JS_FALSE; |
58 |
} |
72 |
} |
59 |
|
73 |
|
|
|
74 |
JSObject *global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); |
75 |
if (!global) return 1; |
76 |
JS_SetGlobalObject(subcx, global); |
77 |
|
60 |
SETUP_REQUEST(subcx); |
78 |
SETUP_REQUEST(subcx); |
61 |
|
79 |
|
62 |
src = JS_GetStringChars(str); |
80 |
src = JS_GetStringCharsZ(cx, str); |
63 |
srclen = JS_GetStringLength(str); |
81 |
srclen = JS_GetStringLength(str); |
64 |
|
82 |
|
65 |
if(!sandbox) |
83 |
if(!sandbox) |
66 |
{ |
84 |
{ |
67 |
sandbox = JS_NewObject(subcx, NULL, NULL, NULL); |
85 |
sandbox = JS_NewObject(subcx, NULL, NULL, NULL); |
68 |
if(!sandbox || !JS_InitStandardClasses(subcx, sandbox)) goto done; |
86 |
if(!sandbox || !JS_InitStandardClasses(subcx, global)) goto done; |
69 |
} |
87 |
} |
70 |
|
88 |
|
71 |
if(srclen == 0) |
89 |
if(srclen == 0) |
72 |
{ |
90 |
{ |
73 |
*rval = OBJECT_TO_JSVAL(sandbox); |
91 |
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(sandbox)); |
74 |
} |
92 |
} |
75 |
else |
93 |
else |
76 |
{ |
94 |
{ |
77 |
JS_EvaluateUCScript(subcx, sandbox, src, srclen, NULL, 0, rval); |
95 |
jsval rval; |
|
|
96 |
JS_EvaluateUCScript(subcx, sandbox, src, srclen, NULL, 0, &rval); |
97 |
JS_SET_RVAL(cx, vp, rval); |
78 |
} |
98 |
} |
79 |
|
99 |
|
80 |
ret = JS_TRUE; |
100 |
ret = JS_TRUE; |
Lines 86-106
done:
Link Here
|
86 |
} |
106 |
} |
87 |
|
107 |
|
88 |
static JSBool |
108 |
static JSBool |
89 |
gc(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) |
109 |
gc(JSContext *cx, uintN argc, jsval *argv) |
90 |
{ |
110 |
{ |
91 |
JS_GC(cx); |
111 |
JS_GC(cx); |
92 |
return JS_TRUE; |
112 |
return JS_TRUE; |
93 |
} |
113 |
} |
94 |
|
114 |
|
95 |
static JSBool |
115 |
static JSBool |
96 |
print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) |
116 |
print(JSContext *cx, uintN argc, jsval *argv) |
97 |
{ |
117 |
{ |
98 |
uintN i; |
118 |
uintN i; |
99 |
char *bytes; |
119 |
char *bytes; |
100 |
|
|
|
101 |
for(i = 0; i < argc; i++) |
120 |
for(i = 0; i < argc; i++) |
102 |
{ |
121 |
{ |
103 |
bytes = enc_string(cx, argv[i], NULL); |
122 |
bytes = enc_string(cx, JS_ARGV(cx, argv)[i], NULL); |
104 |
if(!bytes) return JS_FALSE; |
123 |
if(!bytes) return JS_FALSE; |
105 |
|
124 |
|
106 |
fprintf(stdout, "%s%s", i ? " " : "", bytes); |
125 |
fprintf(stdout, "%s%s", i ? " " : "", bytes); |
Lines 113-121
print(JSContext *cx, JSObject *obj, uint
Link Here
|
113 |
} |
132 |
} |
114 |
|
133 |
|
115 |
static JSBool |
134 |
static JSBool |
116 |
quit(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) |
135 |
quit(JSContext *cx, uintN argc, jsval *argv) |
117 |
{ |
136 |
{ |
118 |
JS_ConvertArguments(cx, argc, argv, "/ i", &gExitCode); |
137 |
JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "/ i", &gExitCode); |
119 |
return JS_FALSE; |
138 |
return JS_FALSE; |
120 |
} |
139 |
} |
121 |
|
140 |
|
Lines 123-163
static char*
Link Here
|
123 |
readfp(JSContext* cx, FILE* fp, size_t* buflen) |
142 |
readfp(JSContext* cx, FILE* fp, size_t* buflen) |
124 |
{ |
143 |
{ |
125 |
char* bytes = NULL; |
144 |
char* bytes = NULL; |
126 |
char* tmp = NULL; |
|
|
127 |
size_t used = 0; |
128 |
size_t byteslen = 256; |
145 |
size_t byteslen = 256; |
129 |
size_t readlen = 0; |
146 |
ssize_t readlen; |
130 |
|
147 |
|
131 |
bytes = JS_malloc(cx, byteslen); |
148 |
bytes = JS_malloc(cx, byteslen); |
132 |
if(bytes == NULL) return NULL; |
149 |
readlen = getline(&bytes, &byteslen, fp); |
133 |
|
150 |
if (readlen <= 0) { |
134 |
while((readlen = js_fgets(bytes+used, byteslen-used, stdin)) > 0) |
151 |
*buflen = 0; |
135 |
{ |
152 |
return bytes; |
136 |
used += readlen; |
153 |
} |
137 |
|
154 |
if (bytes[readlen-1] == '\n') |
138 |
if(bytes[used-1] == '\n') |
155 |
bytes[readlen-1] = '\0'; |
139 |
{ |
156 |
*buflen = readlen; |
140 |
bytes[used-1] = '\0'; |
|
|
141 |
break; |
142 |
} |
143 |
|
144 |
// Double our buffer and read more. |
145 |
byteslen *= 2; |
146 |
tmp = JS_realloc(cx, bytes, byteslen); |
147 |
if(!tmp) |
148 |
{ |
149 |
JS_free(cx, bytes); |
150 |
return NULL; |
151 |
} |
152 |
bytes = tmp; |
153 |
} |
154 |
|
155 |
*buflen = used; |
156 |
return bytes; |
157 |
return bytes; |
157 |
} |
158 |
} |
158 |
|
159 |
|
159 |
static JSBool |
160 |
static JSBool |
160 |
readline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { |
161 |
readline(JSContext *cx, uintN argc, jsval *argv) { |
161 |
jschar *chars; |
162 |
jschar *chars; |
162 |
JSString *str; |
163 |
JSString *str; |
163 |
char* bytes; |
164 |
char* bytes; |
Lines 173-179
readline(JSContext *cx, JSObject *obj, u
Link Here
|
173 |
/* Treat the empty string specially */ |
174 |
/* Treat the empty string specially */ |
174 |
if(byteslen == 0) |
175 |
if(byteslen == 0) |
175 |
{ |
176 |
{ |
176 |
*rval = JS_GetEmptyStringValue(cx); |
177 |
JS_SET_RVAL(cx, argv, JS_GetEmptyStringValue(cx)); |
177 |
JS_free(cx, bytes); |
178 |
JS_free(cx, bytes); |
178 |
return JS_TRUE; |
179 |
return JS_TRUE; |
179 |
} |
180 |
} |
Lines 191-218
readline(JSContext *cx, JSObject *obj, u
Link Here
|
191 |
JS_free(cx, bytes); |
192 |
JS_free(cx, bytes); |
192 |
|
193 |
|
193 |
if(!str) return JS_FALSE; |
194 |
if(!str) return JS_FALSE; |
194 |
|
195 |
JS_SET_RVAL(cx, argv, STRING_TO_JSVAL(str)); |
195 |
*rval = STRING_TO_JSVAL(str); |
|
|
196 |
|
196 |
|
197 |
return JS_TRUE; |
197 |
return JS_TRUE; |
198 |
} |
198 |
} |
199 |
|
199 |
|
200 |
static JSBool |
200 |
static JSBool |
201 |
seal(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { |
201 |
seal(JSContext *cx, uintN argc, jsval *argv) { |
202 |
JSObject *target; |
202 |
JSObject *target; |
203 |
JSBool deep = JS_FALSE; |
203 |
JSBool deep = JS_FALSE; |
204 |
|
204 |
|
205 |
if (!JS_ConvertArguments(cx, argc, argv, "o/b", &target, &deep)) |
205 |
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "o/b", &target, &deep)) |
206 |
return JS_FALSE; |
206 |
return JS_FALSE; |
207 |
if (!target) |
207 |
if (!target) |
208 |
return JS_TRUE; |
208 |
return JS_TRUE; |
209 |
return JS_SealObject(cx, target, deep); |
209 |
return JS_FreezeObject(cx, target); |
210 |
} |
210 |
} |
211 |
|
211 |
|
212 |
static void |
212 |
static void |
213 |
execute_script(JSContext *cx, JSObject *obj, const char *filename) { |
213 |
execute_script(JSContext *cx, JSObject *obj, const char *filename) { |
214 |
FILE *file; |
214 |
FILE *file; |
215 |
JSScript *script; |
215 |
JSObject *script; |
216 |
jsval result; |
216 |
jsval result; |
217 |
|
217 |
|
218 |
if(!filename || strcmp(filename, "-") == 0) |
218 |
if(!filename || strcmp(filename, "-") == 0) |
Lines 234-240
execute_script(JSContext *cx, JSObject *
Link Here
|
234 |
if(script) |
234 |
if(script) |
235 |
{ |
235 |
{ |
236 |
JS_ExecuteScript(cx, obj, script, &result); |
236 |
JS_ExecuteScript(cx, obj, script, &result); |
237 |
JS_DestroyScript(cx, script); |
|
|
238 |
} |
237 |
} |
239 |
} |
238 |
} |
240 |
|
239 |
|
Lines 248-274
printerror(JSContext *cx, const char *me
Link Here
|
248 |
} |
247 |
} |
249 |
|
248 |
|
250 |
static JSFunctionSpec global_functions[] = { |
249 |
static JSFunctionSpec global_functions[] = { |
251 |
{"evalcx", evalcx, 0, 0, 0}, |
250 |
{"evalcx", evalcx, 0, 0}, |
252 |
{"gc", gc, 0, 0, 0}, |
251 |
{"gc", gc, 0, 0}, |
253 |
{"print", print, 0, 0, 0}, |
252 |
{"print", print, 0, 0}, |
254 |
{"quit", quit, 0, 0, 0}, |
253 |
{"quit", quit, 0, 0}, |
255 |
{"readline", readline, 0, 0, 0}, |
254 |
{"readline", readline, 0, 0}, |
256 |
{"seal", seal, 0, 0, 0}, |
255 |
{"seal", seal, 0, 0}, |
257 |
{0, 0, 0, 0, 0} |
256 |
{0, 0, 0, 0} |
258 |
}; |
|
|
259 |
|
260 |
static JSClass global_class = { |
261 |
"GlobalClass", |
262 |
JSCLASS_GLOBAL_FLAGS, |
263 |
JS_PropertyStub, |
264 |
JS_PropertyStub, |
265 |
JS_PropertyStub, |
266 |
JS_PropertyStub, |
267 |
JS_EnumerateStub, |
268 |
JS_ResolveStub, |
269 |
JS_ConvertStub, |
270 |
JS_FinalizeStub, |
271 |
JSCLASS_NO_OPTIONAL_MEMBERS |
272 |
}; |
257 |
}; |
273 |
|
258 |
|
274 |
int |
259 |
int |
Lines 291-297
main(int argc, const char * argv[])
Link Here
|
291 |
|
276 |
|
292 |
SETUP_REQUEST(cx); |
277 |
SETUP_REQUEST(cx); |
293 |
|
278 |
|
294 |
global = JS_NewObject(cx, &global_class, NULL, NULL); |
279 |
global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL); |
295 |
if (!global) return 1; |
280 |
if (!global) return 1; |
296 |
if (!JS_InitStandardClasses(cx, global)) return 1; |
281 |
if (!JS_InitStandardClasses(cx, global)) return 1; |
297 |
|
282 |
|