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
extern crate libc;
extern crate emacs_module;
use std::borrow::Borrow;
use std::ffi::CString;
use std::ptr;
use libc::ptrdiff_t;
use emacs_module::{emacs_runtime, emacs_env, emacs_value};
use self::error::HandleExit;
#[macro_use]
mod macros;
pub mod func;
pub mod error;
pub mod raw;
pub use emacs_module::EmacsSubr;
pub use self::error::{Result, Error, ErrorKind};
pub use self::func::HandleFunc;
#[repr(C)]
#[derive(Debug)]
pub struct Env {
pub(crate) raw: *mut emacs_env,
}
#[repr(C)]
#[derive(Debug)]
pub struct Value {
pub(crate) raw: emacs_value,
}
pub trait ToLisp {
fn to_lisp(&self, env: &Env) -> Result<Value>;
}
pub trait FromLisp: Sized {
fn from_lisp<T: Borrow<Value>>(env: &Env, value: T) -> Result<Self>;
}
pub trait Transfer: Sized {
unsafe extern "C" fn finalizer(ptr: *mut libc::c_void) {
#[cfg(build = "debug")]
println!("Finalizing {} {:#?}", Self::type_name(), ptr);
Box::from_raw(ptr as *mut Self);
}
fn type_name() -> &'static str;
}
pub trait IntoLisp {
fn into_lisp(self, env: &Env) -> Result<Value>;
}
pub type Finalizer = unsafe extern "C" fn(ptr: *mut libc::c_void);
impl Env {
pub fn raw(&self) -> *mut emacs_env {
self.raw
}
fn strip_trailing_zero_bytes(bytes: &mut Vec<u8>) {
let mut len = bytes.len();
while len > 0 && bytes[len - 1] == 0 {
bytes.pop();
len -= 1;
}
}
fn string_bytes(&self, value: &Value) -> Result<Vec<u8>> {
let mut len: isize = 0;
let mut bytes = unsafe {
let copy_string_contents = raw_fn!(self, copy_string_contents)?;
let ok: bool = self.handle_exit(copy_string_contents(
self.raw, value.raw, ptr::null_mut(), &mut len))?;
if !ok {
panic!("Emacs failed to give string's length but did not raise a signal");
}
let mut bytes = vec![0u8; len as usize];
let ok: bool = self.handle_exit(copy_string_contents(
self.raw, value.raw, bytes.as_mut_ptr() as *mut i8, &mut len))?;
if !ok {
panic!("Emacs failed to copy string but did not raise a signal");
}
bytes
};
Self::strip_trailing_zero_bytes(&mut bytes);
Ok(bytes)
}
pub fn intern(&self, name: &str) -> Result<Value> {
raw_call!(self, intern, CString::new(name)?.as_ptr())
}
pub fn type_of(&self, value: Value) -> Result<Value> {
raw_call!(self, type_of, value.raw)
}
pub fn call(&self, name: &str, args: &[Value]) -> Result<Value> {
let symbol = self.intern(name)?;
let mut args: Vec<emacs_value> = args.iter().map(|v| v.raw).collect();
raw_call!(self, funcall, symbol.raw, args.len() as ptrdiff_t, args.as_mut_ptr())
}
pub fn clone_to_lisp<T, U>(&self, value: U) -> Result<Value> where T: ToLisp, U: Borrow<T> {
value.borrow().to_lisp(self)
}
pub fn move_to_lisp<T>(&self, value: T) -> Result<Value> where T: IntoLisp {
value.into_lisp(self)
}
pub fn get_owned<T, U>(&self, lisp_value: U) -> Result<T> where T: FromLisp, U: Borrow<Value> {
lisp_value.borrow().to_owned(self)
}
pub fn get_ref<'v, T>(&self, lisp_value: &'v Value) -> Result<&'v T> where T: Transfer {
lisp_value.to_ref(self)
}
pub fn get_mut<T>(&self, lisp_value: Value) -> Result<&mut T> where T: Transfer {
lisp_value.into_mut(self)
}
fn get_raw_pointer<T: Transfer>(&self, value: emacs_value) -> Result<*mut T> {
match raw_call!(self, get_user_finalizer, value)? {
Some::<Finalizer>(fin) if fin == T::finalizer => {
let ptr: *mut libc::c_void = raw_call!(self, get_user_ptr, value)?;
Ok(ptr as *mut T)
},
Some(_) => {
let expected = T::type_name();
Err(ErrorKind::UserPtrHasWrongType { expected }.into())
},
None => {
let expected = T::type_name();
Err(ErrorKind::UnknownUserPtr { expected }.into())
}
}
}
pub fn is_not_nil(&self, value: Value) -> Result<bool> {
raw_call!(self, is_not_nil, value.raw)
}
pub fn eq(&self, a: Value, b: Value) -> Result<bool> {
raw_call!(self, eq, a.raw, b.raw)
}
pub fn list(&self, args: &[Value]) -> Result<Value> {
self.call("list", args)
}
pub fn provide(&self, name: &str) -> Result<Value> {
let name = self.intern(name)?;
self.call("provide", &[name])
}
pub fn message(&self, text: &str) -> Result<Value> {
let text = text.to_lisp(self)?;
self.call("message", &[text])
}
}
impl From<*mut emacs_env> for Env {
fn from(raw: *mut emacs_env) -> Env {
Env { raw }
}
}
impl From<*mut emacs_runtime> for Env {
fn from(runtime: *mut emacs_runtime) -> Env {
let raw = unsafe {
let get_env = (*runtime).get_environment.expect("Cannot get Emacs environment");
get_env(runtime)
};
Env { raw }
}
}
impl Value {
pub fn to_owned<T: FromLisp>(&self, env: &Env) -> Result<T> {
FromLisp::from_lisp(env, self)
}
pub fn to_ref<T: Transfer>(&self, env: &Env) -> Result<&T> {
env.get_raw_pointer(self.raw).map(|r| unsafe {
&*r
})
}
pub fn into_mut<T: Transfer>(self, env: &Env) -> Result<&mut T> {
env.get_raw_pointer(self.raw).map(|r| unsafe {
&mut *r
})
}
}
impl From<emacs_value> for Value {
fn from(raw: emacs_value) -> Self {
Self { raw }
}
}
impl ToLisp for i64 {
fn to_lisp(&self, env: &Env) -> Result<Value> {
raw_call!(env, make_integer, *self)
}
}
impl ToLisp for str {
fn to_lisp(&self, env: &Env) -> Result<Value> {
let cstring = CString::new(self)?;
let ptr = cstring.as_ptr();
raw_call!(env, make_string, ptr, libc::strlen(ptr) as ptrdiff_t)
}
}
impl FromLisp for i64 {
fn from_lisp<T: Borrow<Value>>(env: &Env, value: T) -> Result<Self> {
raw_call!(env, extract_integer, value.borrow().raw)
}
}
impl FromLisp for String {
fn from_lisp<T: Borrow<Value>>(env: &Env, value: T) -> Result<Self> {
let bytes = env.string_bytes(value.borrow())?;
Ok(String::from_utf8(bytes).unwrap())
}
}
impl<T: Transfer> IntoLisp for Box<T> {
fn into_lisp(self, env: &Env) -> Result<Value> {
let raw = Box::into_raw(self);
let ptr = raw as *mut libc::c_void;
raw_call!(env, make_user_ptr, Some(T::finalizer), ptr)
}
}