forked from pcwalton/rust-spidermonkey
/
test.rs
321 lines (292 loc) · 10.5 KB
/
test.rs
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
use spidermonkey;
import spidermonkey::js;
import ctypes::size_t;
import comm::{ port, chan, recv, send };
use std;
import std::{ io, os, treemap, uvtmp };
enum child_message {
set_log(chan<js::log_message>),
set_err(chan<js::error_report>),
log_msg(js::log_message),
err_msg(js::error_report),
io_cb(u32, str, u32),
stdout(str),
stderr(str),
spawn(str, str),
cast(str, str),
load_url(str),
exitproc,
done,
}
enum ioop {
op_stdout = 0,
op_stderr = 1,
op_spawn = 2,
op_cast = 3,
op_connect = 4,
op_recv = 5,
op_send = 6,
op_close = 7,
op_time = 8,
op_exit = 9
}
fn populate_global_scope(cx : js::context, global : js::object, script : str) {
js::begin_request(*cx);
alt std::io::read_whole_file("xmlhttprequest.js") {
result::ok(file) {
let script = js::compile_script(
cx, global, file, "xmlhttprequest.js", 0u);
js::execute_script(cx, global, script);
}
_ { fail }
}
alt std::io::read_whole_file("dom.js") {
result::ok(file) {
let script = js::compile_script(
cx, global, file, "dom.js", 0u);
js::execute_script(cx, global, script);
}
_ { fail }
}
alt std::io::read_whole_file_str(script) {
result::ok(file) {
let script = js::compile_script(
cx, global, str::bytes(#fmt("try { %s } catch (e) { print('Error: ', e, e.stack) }", file)), script, 0u);
js::execute_script(cx, global, script);
}
_ {
log(core::error, #fmt("File not found: %s", script));
js::ext::rust_exit_now(0);
}
}
js::end_request(*cx);
}
fn make_children(msg_chan : chan<child_message>, senduv_chan: chan<chan<uvtmp::iomsg>>) {
task::spawn {||
let log_port = port::<js::log_message>();
send(msg_chan, set_log(chan(log_port)));
while true {
let msg = recv(log_port);
if msg.level == 9u32 {
send(msg_chan, exitproc);
break;
} else {
send(msg_chan, log_msg(msg));
}
}
};
task::spawn {||
let uv_port = port::<uvtmp::iomsg>();
send(senduv_chan, chan(uv_port));
while true {
let msg = recv(uv_port);
alt msg {
uvtmp::connected(cd) {
send(msg_chan, io_cb(0u32, "onconnect", uvtmp::get_req_id(cd)));
}
uvtmp::wrote(cd) {
send(msg_chan, io_cb(1u32, "onsend", uvtmp::get_req_id(cd)));
}
uvtmp::read(cd, buf, len) {
if len == -1 {
send(msg_chan, io_cb(3u32, "onclose", uvtmp::get_req_id(cd)));
} else {
unsafe {
let vecbuf = vec::unsafe::from_buf(buf, len as uint);
let bufstr = str::from_bytes(vecbuf);
send(msg_chan, io_cb(2u32, bufstr, uvtmp::get_req_id(cd)));
uvtmp::delete_buf(buf);
}
}
}
uvtmp::timer(req_id) {
send(msg_chan, io_cb(4u32, "ontimer", req_id));
}
uvtmp::whatever {
}
uvtmp::exit {
send(msg_chan, done);
break;
}
}
}
};
}
fn make_actor(myid : int, myurl : str, thread : uvtmp::thread, maxbytes : u32, out : chan<child_message>, sendchan : chan<(int, chan<child_message>)>) {
task::spawn {||
let rt = js::get_thread_runtime(maxbytes);
let msg_port = port::<child_message>();
let msg_chan = chan(msg_port);
send(sendchan, (myid, msg_chan));
let senduv_port = port::<chan<uvtmp::iomsg>>();
make_children(chan(msg_port), chan(senduv_port));
let uv_chan = recv(senduv_port);
let cx = js::new_context(rt, maxbytes as size_t);
js::set_options(cx, js::options::varobjfix | js::options::methodjit);
js::set_version(cx, 185u);
let clas = js::new_class({ name: "global", flags: 0x47700u32 });
let global = js::new_compartment_and_global_object(
cx, clas, js::null_principals());
js::init_standard_classes(cx, global);
js::ext::init_rust_library(cx, global);
let exit = false;
let setup = 0;
let childid = 0;
while !exit {
let msg = recv(msg_port);
alt msg {
set_log(ch) {
js::ext::set_log_channel(
cx, global, ch);
setup += 1;
}
load_url(x) {
js::begin_request(*cx);
js::set_data_property(cx, global, x);
let code = "_resume(5, _data, 0); _data = undefined";
let script = js::compile_script(cx, global, str::bytes(code), "io", 0u);
js::execute_script(cx, global, script);
js::end_request(*cx);
}
log_msg(m) {
// messages from javascript
alt m.level{
0u32 { // stdout
send(out, stdout(
#fmt("[Actor %d] %s",
myid, m.message)));
}
1u32 { // stderr
send(out, stderr(
#fmt("[ERROR %d] %s",
myid, m.message)));
}
2u32 { // spawn
send(out, spawn(
#fmt("%d:%d", myid, childid),
m.message));
childid = childid + 1;
}
3u32 { // cast
}
4u32 { // CONNECT
uvtmp::connect(
thread, m.tag, m.message, uv_chan);
}
5u32 { // SEND
uvtmp::write(
thread, m.tag,
str::bytes("GET / HTTP/1.0\n\n"),
uv_chan);
}
6u32 { // RECV
uvtmp::read_start(thread, m.tag, uv_chan);
}
7u32 { // CLOSE
//log(core::error, "close");
uvtmp::close_connection(thread, m.tag);
}
8u32 { // SETTIMEOUT
uvtmp::timer_start(thread, m.timeout, m.tag, uv_chan);
}
_ {
log(core::error, "...");
}
}
}
io_cb(a1, a2, a3) {
js::begin_request(*cx);
js::set_data_property(cx, global, a2);
let code = #fmt("_resume(%u, _data, %u); _data = undefined; XMLHttpRequest.requests_outstanding", a1 as uint, a3 as uint);
let script = js::compile_script(cx, global, str::bytes(code), "io", 0u);
js::execute_script(cx, global, script);
js::end_request(*cx);
}
exitproc {
send(uv_chan, uvtmp::exit);
}
done {
exit = true;
send(out, done);
}
_ { fail "unexpected case" }
}
if setup == 1 {
setup = 2;
if str::byte_len(myurl) > 4u && str::eq(str::slice(myurl, 0u, 4u), "http") {
populate_global_scope(cx, global, "");
send(msg_chan, load_url(myurl));
} else {
populate_global_scope(cx, global, myurl);
}
let checkwait = js::compile_script(
cx, global, str::bytes("if (XMLHttpRequest.requests_outstanding === 0) jsrust_exit();"), "test.js", 0u);
js::execute_script(cx, global, checkwait);
}
}
};
}
fn main(args : [str]) {
let maxbytes = 8u32 * 1024u32 * 1024u32;
let thread = uvtmp::create_thread();
uvtmp::start_thread(thread);
let stdoutport = port::<child_message>();
let stdoutchan = chan(stdoutport);
let sendchanport = port::<(int, chan<child_message>)>();
let sendchanchan = chan(sendchanport);
let map = treemap::init();
let argc = vec::len(args);
let argv = if argc == 1u {
["test.js"]
} else {
vec::slice(args, 1u, argc)
};
let left = 0;
for x in argv {
left += 1;
make_actor(left, x, thread, maxbytes, stdoutchan, sendchanchan);
}
let actorid = left;
for _x in argv {
let (theid, thechan) = recv(sendchanport);
treemap::insert(map, theid, thechan);
}
while true {
alt recv(stdoutport) {
stdout(x) { log(core::error, x); }
stderr(x) { log(core::error, x); }
spawn(id, src) {
log(core::error, ("spawn", id, src));
actorid = actorid + 1;
left = left + 1;
task::spawn {||
make_actor(actorid, src, thread, maxbytes, stdoutchan, sendchanchan);
};
}
cast(id, msg) {}
exitproc {
left = left - 1;
if left == 0 {
let n = @mutable 0;
fn t(n: @mutable int, &&_k: int, &&v: chan<child_message>) {
send(v, exitproc);
*n += 1;
}
treemap::traverse(map, bind t(n, _, _));
left = *n;
}
}
done {
left = left - 1;
if left == 0 {
break;
}
}
_ { fail "unexpected case" }
}
}
// temp hack: join never returns right now
js::ext::rust_exit_now(0);
uvtmp::join_thread(thread);
uvtmp::delete_thread(thread);
}