Skip to content

Commit 07f8555

Browse files
committed
std: Add some hacks to use libuv
1 parent a88c084 commit 07f8555

File tree

6 files changed

+508
-1
lines changed

6 files changed

+508
-1
lines changed

mk/rt.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ RUNTIME_CS_$(1) := \
4848
rt/rust_port.cpp \
4949
rt/rust_upcall.cpp \
5050
rt/rust_uv.cpp \
51+
rt/rust_uvtmp.cpp \
5152
rt/rust_log.cpp \
5253
rt/rust_timer.cpp \
5354
rt/circular_buffer.cpp \

src/libstd/std.rc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#[license = "MIT"];
88
#[crate_type = "lib"];
99

10-
export fs, io, net, run, uv;
10+
export fs, io, net, run, uv, uvtmp;
1111
export c_vec, four, tri, util;
1212
export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind;
1313
export rope;
@@ -25,6 +25,7 @@ mod net;
2525
#[path = "run_program.rs"]
2626
mod run;
2727
mod uv;
28+
mod uvtmp;
2829

2930

3031
// Utility modules

src/libstd/uvtmp.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Some temporary libuv hacks for servo
2+
3+
#[cfg(target_os = "linux")];
4+
#[cfg(target_os = "macos")];
5+
#[cfg(target_os = "freebsd")];
6+
7+
8+
#[nolink]
9+
native mod rustrt {
10+
fn rust_uvtmp_create_thread() -> thread;
11+
fn rust_uvtmp_start_thread(thread: thread);
12+
fn rust_uvtmp_join_thread(thread: thread);
13+
fn rust_uvtmp_delete_thread(thread: thread);
14+
fn rust_uvtmp_connect(
15+
thread: thread,
16+
ip: str::sbuf,
17+
chan: comm::chan<iomsg>);
18+
fn rust_uvtmp_close_connection(thread: thread, cd: connect_data);
19+
fn rust_uvtmp_write(
20+
thread: thread,
21+
cd: connect_data,
22+
buf: *u8,
23+
len: ctypes::size_t,
24+
chan: comm::chan<iomsg>);
25+
fn rust_uvtmp_read_start(
26+
thread: thread,
27+
cd: connect_data,
28+
chan: comm::chan<iomsg>);
29+
fn rust_uvtmp_delete_buf(buf: *u8);
30+
}
31+
32+
type thread = *ctypes::void;
33+
34+
type connect_data = *ctypes::void;
35+
36+
enum iomsg {
37+
whatever,
38+
connected(connect_data),
39+
wrote(connect_data),
40+
read(connect_data, *u8, ctypes::ssize_t)
41+
}
42+
43+
fn create_thread() -> thread {
44+
rustrt::rust_uvtmp_create_thread()
45+
}
46+
47+
fn start_thread(thread: thread) {
48+
rustrt::rust_uvtmp_start_thread(thread)
49+
}
50+
51+
fn join_thread(thread: thread) {
52+
rustrt::rust_uvtmp_join_thread(thread)
53+
}
54+
55+
fn delete_thread(thread: thread) {
56+
rustrt::rust_uvtmp_delete_thread(thread)
57+
}
58+
59+
fn connect(thread: thread, ip: str, ch: comm::chan<iomsg>) {
60+
str::as_buf(ip) {|ipbuf|
61+
rustrt::rust_uvtmp_connect(thread, ipbuf, ch)
62+
}
63+
}
64+
65+
fn close_connection(thread: thread, cd: connect_data) {
66+
rustrt::rust_uvtmp_close_connection(thread ,cd);
67+
}
68+
69+
fn write(thread: thread, cd: connect_data,bytes: [u8],
70+
chan: comm::chan<iomsg>) unsafe {
71+
rustrt::rust_uvtmp_write(
72+
thread, cd, vec::to_ptr(bytes), vec::len(bytes), chan);
73+
}
74+
75+
fn read_start(thread: thread, cd: connect_data,
76+
chan: comm::chan<iomsg>) {
77+
rustrt::rust_uvtmp_read_start(thread, cd, chan);
78+
}
79+
80+
fn delete_buf(buf: *u8) {
81+
rustrt::rust_uvtmp_delete_buf(buf);
82+
}
83+
84+
#[test]
85+
fn test_start_stop() {
86+
let thread = create_thread();
87+
start_thread(thread);
88+
join_thread(thread);
89+
delete_thread(thread);
90+
}
91+
92+
#[test]
93+
#[ignore]
94+
fn test_connect() {
95+
let thread = create_thread();
96+
start_thread(thread);
97+
let port = comm::port();
98+
let chan = comm::chan(port);
99+
connect(thread, "74.125.224.146", chan);
100+
alt comm::recv(port) {
101+
connected(cd) {
102+
close_connection(thread, cd);
103+
}
104+
}
105+
join_thread(thread);
106+
delete_thread(thread);
107+
}
108+
109+
#[test]
110+
#[ignore]
111+
fn test_http() {
112+
let thread = create_thread();
113+
start_thread(thread);
114+
let port = comm::port();
115+
let chan = comm::chan(port);
116+
connect(thread, "74.125.224.146", chan);
117+
alt comm::recv(port) {
118+
connected(cd) {
119+
write(thread, cd, str::bytes("GET / HTTP/1.0\n\n"), chan);
120+
alt comm::recv(port) {
121+
wrote(cd) {
122+
read_start(thread, cd, chan);
123+
let keep_going = true;
124+
while keep_going {
125+
alt comm::recv(port) {
126+
read(_, buf, -1) {
127+
keep_going = false;
128+
delete_buf(buf);
129+
}
130+
read(_, buf, len) {
131+
unsafe {
132+
log(error, len);
133+
let buf = vec::unsafe::from_buf(buf, len as uint);
134+
let str = str::unsafe_from_bytes(buf);
135+
#error("read something");
136+
io::println(str);
137+
}
138+
delete_buf(buf);
139+
}
140+
}
141+
}
142+
close_connection(thread, cd);
143+
}
144+
}
145+
}
146+
}
147+
join_thread(thread);
148+
delete_thread(thread);
149+
}

src/rt/rust_uv.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ extern "C" CDECL size_t
4848
rust_uv_size_of_idle_t() {
4949
return sizeof(uv_idle_t);
5050
}
51+

0 commit comments

Comments
 (0)