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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use {call};
use emacs_gen::{EmacsEnv, EmacsSubr, EmacsVal};
use regex;
use regex::Regex;
use std::os::raw;
use std::ffi::{CString, FromBytesWithNulError, IntoStringError, NulError};
use std::io;
use std::io::ErrorKind;
use std::num::ParseIntError;
use std::ops::Range;
use std::ptr;
use std::string::FromUtf8Error;
use std::str::Utf8Error;

pub unsafe extern "C" fn destruct<T>(arg: *mut raw::c_void) {
    if arg.is_null() {
        // println!("WARNING: Dropping nullptr @ {:p}", arg);
        return;
    }
    let ptr = arg as *mut T;
    drop(Box::from_raw(ptr));
    // println!("Dropped value @ {:p}", ptr);
}

pub type ConvResult<T> = Result<T, ConvErr>;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConvErr {
    CoreFnMissing(String),
    Nullptr(String),
    FailedToFetchLength,
    FailedToCopy,
    InvalidArgCount(usize),
    Other(String),
    WrongEmacsValueType { expected: String, got: Option<EmacsVal> },

    IoErr { kind: ErrorKind, msg: String },
    RegexSyntaxErr(String),
    RegexTooLarge(usize),

    FromUtf8Error { valid_up_to: usize,  bytes: Vec<u8> },
    Utf8Error { valid_up_to: usize },
    ParseIntError(ParseIntError),

    FoundInteriorNulByte { pos: usize, bytes: Option<Vec<u8>> },
    NotNulTerminated,
}


impl From<io::Error> for ConvErr {
    fn from(err: io::Error) -> ConvErr {
        ConvErr::IoErr { kind: err.kind(),  msg: format!("{}", err) }
    }
}

impl From<FromUtf8Error> for ConvErr {
    fn from(err: FromUtf8Error) -> ConvErr {
        ConvErr::FromUtf8Error {
            valid_up_to: err.utf8_error().valid_up_to(),
            bytes: err.into_bytes()
        }
    }
}

impl From<Utf8Error> for ConvErr {
    fn from(err: Utf8Error) -> ConvErr {
        ConvErr::Utf8Error { valid_up_to: err.valid_up_to() }
    }
}

impl From<regex::Error> for ConvErr {
    fn from(err: regex::Error) -> ConvErr {
        match err {
            regex::Error::Syntax(msg) => ConvErr::RegexSyntaxErr(msg),
            regex::Error::CompiledTooBig(size) => ConvErr::RegexTooLarge(size),
            regex_err => ConvErr::Other(format!("{:#?}", regex_err)),
        }
    }
}

impl From<NulError> for ConvErr {
    fn from(err: NulError) -> ConvErr {
        ConvErr::FoundInteriorNulByte {
            pos: err.nul_position(),
            bytes: Some(err.into_vec()),
        }
    }
}

impl From<FromBytesWithNulError> for ConvErr {
    fn from(err: FromBytesWithNulError) -> ConvErr {
        lazy_static! {
            /// This is an example for using doc comment attributes
            static ref RE_FROM_BYTES_WITH_NUL_ERROR: Regex = Regex::new(
                r"Err(FromBytesWithNulError { kind: InteriorNul(\d+) })"
            ).expect("Failed to init regex RE_FROM_BYTES_WITH_NUL_ERROR");
        }
        let err_string = format!("{:?}", err);
        if "Err(FromBytesWithNulError { kind: NotNulTerminated })" == err_string {
            return ConvErr::NotNulTerminated;
        }
        for cap in RE_FROM_BYTES_WITH_NUL_ERROR.captures_iter(&err_string) {
            let pos = match cap[1].parse() {
                Ok(pos) => pos,
                Err(parse_int_err) => return ConvErr::from(parse_int_err),
            };
            return ConvErr::FoundInteriorNulByte { pos: pos, bytes: None };
        }
        ConvErr::Other(err_string)
    }
}

impl From<IntoStringError> for ConvErr {
    fn from(err: IntoStringError) -> ConvErr {
        ConvErr::from(err.utf8_error())
    }
}

impl From<ParseIntError> for ConvErr {
    fn from(err: ParseIntError) -> ConvErr {
        ConvErr::ParseIntError(err)
    }
}


pub mod elisp2native {
    use emacs_gen::{EmacsEnv, EmacsVal};
    use hlapi::{ConvErr, ConvResult};
    use std::ffi::{CString};
    use std::ptr;

    pub fn pointer<T>(env: *mut EmacsEnv, args: *mut EmacsVal, index: usize)
                      -> ConvResult<*mut T> {
        if args.is_null() { return Err(ConvErr::Nullptr(String::from("args"))) }
        // TODO: verify that `index` is within bounds
        unsafe { match (*env).get_user_ptr {
            None => Err(ConvErr::CoreFnMissing(String::from("get_user_ptr"))),
            Some(get_user_ptr) => {
                let arg = *args.offset(index as isize);
                Ok(get_user_ptr(env, arg) as *mut T)
            },
        }}
    }

    pub fn mut_ref<'t, T>(env: *mut EmacsEnv, args: *mut EmacsVal, index: usize)
                          -> ConvResult<&'t mut T> {
        pointer(env, args, index).map(|raw: *mut T| unsafe { &mut *raw })
    }

    pub fn string(env: *mut EmacsEnv, val: EmacsVal) -> ConvResult<String> {
        let mut bytes: Vec<u8> = self::string_bytes(env, val)?;
        strip_trailing_zero_bytes(&mut bytes);
        Ok(String::from_utf8(bytes)?)
    }

    pub fn cstring(env: *mut EmacsEnv, val: EmacsVal) -> ConvResult<CString> {
        let mut bytes: Vec<u8> = self::string_bytes(env, val)?;
        strip_trailing_zero_bytes(&mut bytes);
        Ok(CString::new(bytes)?)
    }

    fn strip_trailing_zero_bytes(bytes: &mut Vec<u8>) {
        let mut len = bytes.len();
        while len > 0 && bytes[len - 1] == 0 {
            bytes.pop(); // strip trailing 0-byte(s)
            len -= 1;
        }
    }

    pub fn string_bytes(env: *mut EmacsEnv, val: EmacsVal)
                        -> ConvResult<Vec<u8>> {
        let mut len: isize = 0;
        unsafe {
            // Fetch Elisp path string length
            let copy_string_contents = (*env).copy_string_contents.ok_or_else(
                || ConvErr::CoreFnMissing(String::from("copy_string_contents"))
            )?;
            let ok = copy_string_contents(env, val, ptr::null_mut(), &mut len);
            if !ok { return Err(ConvErr::FailedToFetchLength); }

            // Copy the Elisp path string to a Rust Vec, based on its length
            let mut bytes = vec![0u8; len as usize];
            let bytes_ptr = bytes.as_mut_ptr() as *mut i8;
            let ok = copy_string_contents(env, val, bytes_ptr, &mut len);
            if !ok { return Err(ConvErr::FailedToCopy); }
            Ok(bytes)
        }
    }

    pub fn integer(env: *mut EmacsEnv, args: *mut EmacsVal, index: usize)
                   -> ConvResult<i64> {
        if args.is_null() { return Err(ConvErr::Nullptr(String::from("args"))) }
        // TODO: verify that `index` is within bounds
        unsafe {
            int_value(env, *args.offset(index as isize))
        }
    }

    pub fn int_value(env: *mut EmacsEnv, val: EmacsVal) -> ConvResult<i64> {
        if val.is_null() { return Err(ConvErr::Nullptr(String::from("val"))) }
        unsafe {
            let extract_int = (*env).extract_integer.ok_or_else(
                || ConvErr::CoreFnMissing(String::from("extract_integer"))
            )?;
            Ok(extract_int(env, val))
        }
    }


    pub fn list(env: *mut EmacsEnv, arg: EmacsVal) -> ConvResult<Vec<EmacsVal>> {
        let nil: EmacsVal = ::native2elisp::symbol(env, "nil")?;
        let is_list: EmacsVal = ::call(env, "listp", &mut [arg]);
        if ::eq(env, is_list, nil)? {
            return Err(ConvErr::WrongEmacsValueType {
                expected: String::from("list"),
                got: Some(arg)
            });
        }
        let length: EmacsVal = ::call(env, "length", &mut [arg]);
        let length: i64 = int_value(env, length)?;
        let mut list: Vec<EmacsVal> = vec![];
        for i in 0 .. length {
            let index: EmacsVal = ::native2elisp::integer(env, i)?;
            let element: EmacsVal = ::call(env, "nth", &mut [index, arg]);
            list.push(element);
        }
        Ok(list)
    }

}

pub mod native2elisp {
    use {call};
    use emacs_gen::{Dtor, EmacsEnv, EmacsSubr, EmacsVal};
    use hlapi::{ConvErr, ConvResult};
    use libc;
    use std::ffi::CString;
    use std::os::raw;

    pub fn integer(env: *mut EmacsEnv, num: i64) -> ConvResult<EmacsVal> {
        unsafe {
            let make_integer = (*env).make_integer.ok_or_else(
                || ConvErr::CoreFnMissing(String::from("make_integer"))
            )?;
            Ok(make_integer(env, num))
        }
    }

    /// Convert a Rust String/&str into an Elisp string.
    pub fn string<S>(env: *mut EmacsEnv, string: S) -> ConvResult<EmacsVal>
        where S: Into<Vec<u8>>
    {
        unsafe {
            let string: Vec<u8> = string.into();
            let cstring = CString::new(string)?;
            let c_string: *const libc::c_char = cstring.as_ptr();
            let strlen: usize = libc::strlen(c_string);
            let make_string = (*env).make_string
                .ok_or(ConvErr::CoreFnMissing(String::from("make_string")))?;
            Ok(make_string(env, c_string, strlen as isize))
        }
    }

    /// Intern a new Elisp symbol.
    pub fn symbol(env: *mut EmacsEnv, name: &str) -> ConvResult<EmacsVal> {
        Ok(call(env, "intern", &mut [string(env, name)?]))
    }

    pub fn function(env: *mut EmacsEnv,
                    min_arity: isize,
                    max_arity: isize,
                    function: Option<EmacsSubr>,
                    documentation: *const libc::c_char,
                    data: *mut raw::c_void)
                    -> ConvResult<EmacsVal> {
        unsafe {
            let make_fn = (*env).make_function.ok_or_else(
                || ConvErr::CoreFnMissing(String::from("make_function"))
            )?;
            Ok(make_fn(env, min_arity, max_arity, function, documentation, data))
        }
    }

    /// Transform a `Box<T>` into a `*mut EmacsVal`.
    pub fn boxed<T>(env: *mut EmacsEnv, value: T, dtor: Dtor)
                    -> ConvResult<EmacsVal> {
        let ptr = Box::into_raw(Box::new(value)) as *mut raw::c_void;
        // println!("Transferred Box<T> @ {:p} to Elisp", ptr);
        unsafe {
            let make_user_ptr = (*env).make_user_ptr.ok_or_else(
                || ConvErr::CoreFnMissing(String::from("make_user_ptr"))
            )?;
            Ok(make_user_ptr(env, Some(dtor), ptr))
        }
    }

    pub fn string_list<S>(env: *mut EmacsEnv, strings: &[S]) -> ConvResult<EmacsVal>
        where S: AsRef<str>
    {
        let mut list: EmacsVal = ::call(env, "list", &mut []);
        for entry in strings.iter().rev() {
            list = ::call(env, "cons", &mut [
                ::native2elisp::string(env, entry.as_ref())?,
                list
            ]);
        }
        Ok(list)
    }
}


/// A μDSL to cut away boilerplate when defining Emacs subrs,
/// which are of course defined in Rust rather than C here.
/// One thing enforced at compile time is that some semblance
/// of a legal Elisp value is returned.
#[macro_export]
macro_rules! emacs_subrs {
    ($($name:ident($env:ident, $nargs:ident, $args:ident, $data:ident, $tag:ident)
       $body:expr;)*) => {
        $(
            #[allow(non_snake_case, unused_variables)]
            unsafe extern "C" fn $name($env: *mut EmacsEnv,
                                       $nargs: libc::ptrdiff_t,
                                       $args: *mut EmacsVal,
                                       $data: *mut raw::c_void)
                                       -> EmacsVal {
                // NOTE: The inner `fun` fn provides type checking for Emacs
                // subrs -- especially their output -- while also allowing each
                // subr to just use `?` for error handling. This is much nicer
                // than any alternative would be. It does mean that those errors
                // need to be dealt with here. For now it calls `expect()` which
                // still means a panic. However at least the cause should be
                // clear because of a useful backtrace, as well as proper error
                // handling in the subrs themselves. This in turn aids debugging.
                //
                // Inlining the inner fn means there's no runtime penalty at the
                // cost of slightly higher compile times.
                #[inline(always)]
                unsafe fn fun($env: *mut EmacsEnv,
                              $nargs: libc::ptrdiff_t,
                              $args: *mut EmacsVal,
                              $data: *mut raw::c_void,
                              $tag: &str) -> ConvResult<EmacsVal> { $body }

                let $tag = format!("[{}]", stringify!($name));
                match fun($env, $nargs, $args, $data, &$tag) {
                    Ok(value) => value,
                    Err(conv_err) => {
                        let msg = format!("{} ConvErr::{:?}", $tag, conv_err);
                        $crate::hlapi::native2elisp::string($env, msg)
                        // TODO: implement sans panic using the ?-operator
                            .expect("Error string creation failed")
                    }
                }
            }
        )*
    };
}

#[macro_export]
macro_rules! init_module {
    (($env:ident) $body:expr ) => {
        #[no_mangle]
        pub extern "C" fn emacs_module_init(runtime: *mut EmacsRT)
                                            -> libc::c_int {{
            #[inline(always)]
            fn fun($env: *mut EmacsEnv) -> ConvResult<EmacsVal> { $body }

            let $env = emacs::get_environment(runtime);
            let result: ConvResult<EmacsVal> = fun($env);
            result.expect("$body should yield an Ok value");
            0
        }}
    };
}



#[macro_export]
macro_rules! message {
    ($env:expr, $fmt:expr $(, $args:expr)*) => {{
        $crate::hlapi::message($env, format!($fmt $(, $args)*))
    }};
}

/// Log a message to the *Messages* buffer.
pub fn message<S>(env: *mut EmacsEnv, text: S) -> ConvResult<EmacsVal>
    where S: Into<String>
{
    let string = native2elisp::string(env, text.into())?;
    Ok(call(env, "message", &mut [string]))
}

/// Basic Elisp equality check.
pub fn eq(env: *mut EmacsEnv, left: EmacsVal, right: EmacsVal)
          -> ConvResult<bool> {
    unsafe {
        let eq = (*env).eq.ok_or_else(
            || ConvErr::CoreFnMissing(String::from("eq"))
        )?;
        Ok(eq(env, left, right))
    }
}

pub fn is_nil(env: *mut EmacsEnv, value: EmacsVal) -> ConvResult<bool> {
    eq(env, value, native2elisp::symbol(env, "nil")?)
}

/// Register an Emacs `subr`, so that it can be accessed from Elisp.
pub fn register(env: *mut EmacsEnv,
                elisp_sym: &str,
                native_sym: EmacsSubr,
                nargs_range: Range<usize>,
                docstring: &str,
                /* user_ptr: *mut libc::c_void*/)
                -> ConvResult<EmacsVal> {
    let doc = CString::new(docstring)?.as_ptr();
    let func = native2elisp::function(env,
                                      nargs_range.start as isize,
                                      nargs_range.end as isize + 1,
                                      Some(native_sym),
                                      doc,
                                      ptr::null_mut(/* user_ptr */))?;
    let elisp_symbol = native2elisp::symbol(env, elisp_sym)?;
    call(env, "fset", &mut [elisp_symbol, func]);
    message!(env, "Registered function {}", elisp_sym)?;
    native2elisp::symbol(env, "t")
}

//  LocalWords:  nullptr