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
#[doc(no_inline)]
pub use failure::{Error, ResultExt};
use failure_derive::Fail;
use std::mem;
use std::result;
use std::thread;
use super::IntoLisp;
use super::{Env, Value};
use emacs_module::*;
const RETURN: emacs_funcall_exit = emacs_funcall_exit_emacs_funcall_exit_return;
const SIGNAL: emacs_funcall_exit = emacs_funcall_exit_emacs_funcall_exit_signal;
const THROW: emacs_funcall_exit = emacs_funcall_exit_emacs_funcall_exit_throw;
#[derive(Debug)]
pub struct TempValue {
raw: emacs_value,
}
const WRONG_TYPE_USER_PTR: &str = "rust-wrong-type-user-ptr";
const ERROR: &str = "rust-error";
const PANIC: &str = "rust-panic";
#[derive(Debug, Fail)]
pub enum ErrorKind {
#[fail(display = "Non-local signal: symbol={:?} data={:?}", symbol, data)]
Signal { symbol: TempValue, data: TempValue },
#[fail(display = "Non-local throw: tag={:?} value={:?}", tag, value)]
Throw { tag: TempValue, value: TempValue },
#[fail(display = "expected: {}", expected)]
WrongTypeUserPtr { expected: &'static str },
}
pub type Result<T> = result::Result<T, Error>;
impl TempValue {
unsafe fn new(raw: emacs_value) -> Self {
Self { raw }
}
pub unsafe fn value<'e>(&self, env: &'e Env) -> Value<'e> {
Value::new_protected(self.raw, env)
}
}
unsafe impl Send for TempValue {}
unsafe impl Sync for TempValue {}
impl Env {
pub(crate) fn handle_exit<T>(&self, result: T) -> Result<T> {
let mut symbol = unsafe { mem::uninitialized() };
let mut data = unsafe { mem::uninitialized() };
let status = self.non_local_exit_get(&mut symbol, &mut data);
match (status, symbol, data) {
(RETURN, ..) => Ok(result),
(SIGNAL, symbol, data) => {
self.non_local_exit_clear();
Err(ErrorKind::Signal {
symbol: unsafe { TempValue::new(symbol) },
data: unsafe { TempValue::new(data) },
}
.into())
}
(THROW, tag, value) => {
self.non_local_exit_clear();
Err(ErrorKind::Throw {
tag: unsafe { TempValue::new(tag) },
value: unsafe { TempValue::new(value) },
}
.into())
}
_ => panic!("Unexpected non local exit status {}", status),
}
}
pub(crate) unsafe fn maybe_exit(&self, result: Result<Value<'_>>) -> emacs_value {
match result {
Ok(v) => v.raw,
Err(error) => match error.downcast_ref::<ErrorKind>() {
Some(&ErrorKind::Signal { ref symbol, ref data }) => {
self.signal(symbol.raw, data.raw)
}
Some(&ErrorKind::Throw { ref tag, ref value }) => self.throw(tag.raw, value.raw),
Some(&ErrorKind::WrongTypeUserPtr { .. }) => self
.signal_str(WRONG_TYPE_USER_PTR, &format!("{}", error))
.expect(&format!("Failed to signal {}", error)),
_ => self
.signal_str(ERROR, &format!("{}", error))
.expect(&format!("Failed to signal {}", error)),
},
}
}
pub(crate) fn handle_panic(&self, result: thread::Result<emacs_value>) -> emacs_value {
match result {
Ok(v) => v,
Err(error) => {
self.signal_str(PANIC, &format!("{:#?}", error))
.expect(&format!("Fail to signal panic {:#?}", error))
}
}
}
pub(crate) fn define_errors(&self) -> Result<()> {
self.define_error(PANIC, "Rust panic", "error")?;
self.define_error(ERROR, "Rust error", "error")?;
self.define_error(WRONG_TYPE_USER_PTR, "Wrong type user-ptr", ERROR)?;
Ok(())
}
fn signal_str(&self, symbol: &str, message: &str) -> Result<emacs_value> {
let message = message.into_lisp(&self)?;
let data = self.list(&[message])?;
let symbol = self.intern(symbol)?;
unsafe { Ok(self.signal(symbol.raw, data.raw)) }
}
fn define_error(&self, name: &str, message: &str, parent: &str) -> Result<Value<'_>> {
self.call(
"define-error",
&[self.intern(name)?, message.into_lisp(self)?, self.intern(parent)?],
)
}
fn non_local_exit_get(
&self,
symbol: &mut emacs_value,
data: &mut emacs_value,
) -> emacs_funcall_exit {
raw_call_no_exit!(
self,
non_local_exit_get,
symbol as *mut emacs_value,
data as *mut emacs_value
)
}
fn non_local_exit_clear(&self) {
raw_call_no_exit!(self, non_local_exit_clear)
}
#[allow(unused_unsafe)]
unsafe fn throw(&self, tag: emacs_value, value: emacs_value) -> emacs_value {
raw_call_no_exit!(self, non_local_exit_throw, tag, value);
tag
}
#[allow(unused_unsafe)]
unsafe fn signal(&self, symbol: emacs_value, data: emacs_value) -> emacs_value {
raw_call_no_exit!(self, non_local_exit_signal, symbol, data);
symbol
}
}