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
use crate::eval::MrbEval;
use crate::load::MrbLoadSources;
use crate::{Mrb, MrbError};
pub fn init(interp: &Mrb) -> Result<(), MrbError> {
interp
.borrow_mut()
.def_class::<Thread>("Thread", None, None);
interp.borrow_mut().def_class::<Mutex>("Mutex", None, None);
interp.def_rb_source_file("thread.rb", include_str!("thread.rb"))?;
interp.eval("require 'thread'")?;
Ok(())
}
pub struct Thread;
pub struct Mutex;
#[cfg(test)]
mod tests {
#![allow(clippy::shadow_unrelated)]
use crate::convert::TryFromMrb;
use crate::eval::MrbEval;
#[test]
fn thread_required_by_default() {
let interp = crate::interpreter().expect("mrb init");
let result = interp
.eval("Object.const_defined?(:Thread)")
.expect("thread");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
}
#[test]
fn thread_current_is_main() {
let interp = crate::interpreter().expect("mrb init");
let spec = "Thread.current == Thread.main";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = "Thread.new { Thread.current == Thread.main }.join.value == false";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
}
#[test]
fn thread_join_value() {
let interp = crate::interpreter().expect("mrb init");
let spec = "Thread.new { 2 + 3 }.join.value == 5";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = "Thread.new { 2 + Thread.new { 3 }.join.value }.join.value == 5";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
}
#[test]
fn thread_main_is_running() {
let interp = crate::interpreter().expect("mrb init");
let spec = "Thread.current.status == 'run'";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = "Thread.current.alive?";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
}
#[test]
fn thread_spawn() {
let interp = crate::interpreter().expect("mrb init");
let spec = "Thread.new { Thread.current }.join.value != Thread.current";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = "Thread.new { Thread.current.name }.join.value != Thread.current.name";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = "Thread.new { Thread.current }.join.value.alive? == false";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = "Thread.new { Thread.current }.join.value.status == false";
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
}
#[test]
fn thread_locals() {
let interp = crate::interpreter().expect("mrb init");
let spec = r#"
Thread.current[:local] = 42
Thread.new { Thread.current.keys.empty? }.join.value
"#;
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = r#"
Thread.current[:local] = 42
Thread.new { Thread.current[:local] = 96 }.join
Thread.current[:local] == 42
"#;
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = r#"
Thread.current.thread_variable_set(:local, 42)
Thread.new { Thread.current.thread_variables.empty? }.join.value
"#;
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
let spec = r#"
Thread.current.thread_variable_set(:local, 42)
Thread.new { Thread.current.thread_variable_set(:local, 96) }.join
Thread.current.thread_variable_get(:local) == 42
"#;
let result = interp.eval(spec).expect("spec");
assert!(unsafe { bool::try_from_mrb(&interp, result) }.expect("convert"));
}
#[test]
fn thread_abort_on_exception() {
let interp = crate::interpreter().expect("mrb init");
let spec = r#"
Thread.abort_on_exception = true
Thread.new { raise 'failboat' }.join
"#;
let result = interp.eval(spec);
assert!(result.is_err());
let spec = r#"
Thread.abort_on_exception = true
Thread.new do
begin
Thread.new { raise 'failboat' }.join
rescue StandardError
# swallow errors
end
end.join
"#;
let result = interp.eval(spec);
assert!(result.is_err());
let spec = r#"
Thread.abort_on_exception = false
Thread.new do
begin
Thread.new do
Thread.current.abort_on_exception = true
raise 'failboat'
end.join
rescue StandardError
# swallow errors
end
end.join
"#;
let result = interp.eval(spec);
assert!(result.is_err());
}
}