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
use ::std;
use ::for_other_imageflow_crates::preludes::external_without_std::*;
use ::ffi;
use ::job::Job;
use ::{FlowErr,FlowError, Result, JsonResponse};

use ::imageflow_types::collections::AddRemoveSet;
use ::ffi::ImageflowJsonResponse;

pub struct Context{
    jobs: AddRemoveSet<Job>,
    c_ctx: *mut ffi::ImageflowContext,
    error: RefCell<ErrorBuffer>,
}

#[derive(Copy,Clone,Debug)]
pub struct ErrorBuffer{
    c_ctx: *mut ffi::ImageflowContext,
}
impl Context {

    pub fn create() -> Result<Box<Context>>{
        Context::abi_create_boxed()
    }

    /// Used by abi; should not panic
    pub fn abi_create_boxed() -> Result<Box<Context>> {
        std::panic::catch_unwind(|| {
            // Upgrade backtraces
            imageflow_helpers::debug::upgrade_panic_hook_once_if_backtraces_wanted();

            let inner = unsafe { ffi::flow_context_create() };
            if inner.is_null() {
                Err(FlowError::Oom)
            } else {
                Ok(Box::new(Context {
                    c_ctx: inner,
                    error: RefCell::new(ErrorBuffer {c_ctx: inner}),
                    jobs: AddRemoveSet::with_capacity(2)
                }))
            }
        }).unwrap_or(Err(FlowError::Oom))
    }

    /// Used by abi; should not panic
    pub fn abi_begin_terminate(&mut self) -> bool {
        self.jobs.mut_clear();
        unsafe {
            ffi::flow_context_begin_terminate(self.c_ctx)
        }
    }

    pub fn error_mut(&self) -> RefMut<ErrorBuffer>{
        self.error.borrow_mut()
    }
    pub fn error(&self) -> Ref<ErrorBuffer>{
        self.error.try_borrow().expect("Another scope has mutably borrowed the ErrorBuffer; readonly access failed.")
    }
    pub unsafe fn unsafe_c_context_pointer(&self) -> *mut ffi::ImageflowContext{
        self.c_ctx
    }

    pub fn message(&mut self, method: &str, json: &[u8]) -> Result<JsonResponse> {
        ::context_methods::CONTEXT_ROUTER.invoke(self, method, json)
    }

    pub fn create_job(&self) -> RefMut<Job>{
        self.jobs.add_mut(Job::internal_use_only_create(self))
    }

    pub fn abi_try_remove_job(&self, job: *const Job) -> bool{
        self.jobs.try_remove(job).unwrap_or(false)
    }
    pub fn flow_c(&self) -> *mut ffi::ImageflowContext{
        self.c_ctx
    }

    pub fn c_error(&self) -> Option<FlowError>{
        self.error().get_error_copy()
    }



    pub fn create_io_from_copy_of_slice<'a>(&'a self, bytes: &'a [u8]) -> Result<*mut ::ffi::ImageflowJobIo> {
        unsafe {
            let buf: *mut u8 =
            ::ffi::flow_context_calloc(self.flow_c(),
                                       1,
                                       bytes.len(),
                                       ptr::null(),
                                       self.flow_c() as *const libc::c_void,
                                       ptr::null(),
                                       0) as *mut u8;

            if buf.is_null() {
                return Err(FlowError::Oom);
            }
            ptr::copy_nonoverlapping(bytes.as_ptr(), buf, bytes.len());

            let io_ptr = ::ffi::flow_io_create_from_memory(self.flow_c(),
                                                           ::ffi::IoMode::ReadSeekable,
                                                           buf,
                                                           bytes.len(),
                                                           self.flow_c() as *const libc::c_void,
                                                           ptr::null());

            if io_ptr.is_null() {
                Err(self.c_error().unwrap())
            } else {
                Ok(io_ptr)
            }
        }
    }
    pub fn create_io_from_slice<'a>(&'a self, bytes: &'a [u8]) -> Result<*mut ::ffi::ImageflowJobIo> {
        unsafe {
            let p = ::ffi::flow_io_create_from_memory(self.flow_c(),
                                                      ::ffi::IoMode::ReadSeekable,
                                                      bytes.as_ptr(),
                                                      bytes.len(),
                                                      self.flow_c() as *const libc::c_void,
                                                      ptr::null());
            if p.is_null() {
                Err(self.c_error().unwrap())
            } else {
                Ok(p)
            }
        }
    }

    pub fn create_io_from_filename(&self, path: &str, dir: ::IoDirection) -> Result<*mut ::ffi::ImageflowJobIo> {
        unsafe {
            // TODO: character sets matter!
            let mode = match dir {
                s::IoDirection::In => ::ffi::IoMode::ReadSeekable,
                s::IoDirection::Out => ::ffi::IoMode::WriteSequential,
            };

            let mut vec = Vec::new();
            vec.extend_from_slice(path.as_bytes());
            vec.push(0);

            let c_path = std::ffi::CStr::from_bytes_with_nul(vec.as_slice()).unwrap();
            //TODO: make filename lifetime last as long as context

            let p = ::ffi::flow_io_create_for_file(self.flow_c(),
                                                        mode,
                                                        c_path.as_ptr(),
                                                   self.flow_c() as *const libc::c_void);
            if p.is_null() {
                Err(self.c_error().unwrap())
            } else {
                Ok(p)
            }
        }
    }

    pub fn create_io_output_buffer(&self) -> Result<(*mut ::ffi::ImageflowJobIo)> {
        unsafe {
            let p =
            ::ffi::flow_io_create_for_output_buffer(self.flow_c(),
                                                    self.flow_c() as *const libc::c_void);
            if p.is_null() {
                Err(self.c_error().unwrap())
            } else {
                Ok(p)
            }
        }
    }

    pub fn todo_remove_set_floatspace(&self, b: ::ffi::Floatspace){
        unsafe {
            ::ffi::flow_context_set_floatspace(self.flow_c(),
                                               b,
                                               0f32,
                                               0f32,
                                               0f32)
        }
    }

    pub fn create_abi_json_response(&mut self,
                                    json_bytes: std::borrow::Cow<[u8]>,
                                    status_code: i64)
                                    -> *const ImageflowJsonResponse {
        unsafe {
            let sizeof_struct = std::mem::size_of::<ImageflowJsonResponse>();

            let pointer = ::ffi::flow_context_calloc(self.flow_c(),
                                                     1,
                                                     sizeof_struct + json_bytes.len(),
                                                     ptr::null(),
                                                     self.flow_c() as *mut libc::c_void,
                                                     ptr::null(),
                                                     0) as *mut u8;
            // Return null on OOM
            if pointer.is_null() {
                return ::std::ptr::null();
            }
            let pointer_to_final_buffer =
            pointer.offset(sizeof_struct as isize) as *mut libc::uint8_t;
            let imageflow_response = &mut (*(pointer as *mut ImageflowJsonResponse));
            imageflow_response.buffer_utf8_no_nulls = pointer_to_final_buffer;
            imageflow_response.buffer_size = json_bytes.len();
            imageflow_response.status_code = status_code;

            let mut out_json_bytes = std::slice::from_raw_parts_mut(pointer_to_final_buffer,
                                                                    json_bytes.len());

            out_json_bytes.clone_from_slice(&json_bytes);

            imageflow_response as *const ImageflowJsonResponse
        }
    }




    pub fn destroy_allowing_panics(mut self) {
        if !self.abi_begin_terminate(){
            self.c_error().unwrap().panic_with("Error during context shutdown");
        }
    }



    pub fn abi_destroy(mut self) -> bool{
        self.jobs.mut_clear();
        true
    }

}

type ErrorCode = i32;
impl ErrorBuffer{

    /// Prints the error messages and stacktrace to the given buffer in UTF-8 form; writes a null
    /// character to terminate the string, and *ALSO* returns the number of bytes written.
    ///
    ///
    /// Happy(ish) path: Returns the length of the error message written to the buffer.
    /// Sad path: Returns -1 if slice was too small or buffer was nullptr.
    /// full_file_path, if true, will display the directory associated with the files in each stack frame.
//    pub fn write_to_slice_as_c_str(&self, slice: &mut [u8], prefer_full_paths: bool) -> i64{
//        // ffi::flow_context_error_and_stacktrace(context, buffer as *mut u8, buffer_length, full_file_path)
//    }
//
//
    /// Used by abi; should not panic
    pub fn abi_has_error(&self) -> bool{
        unsafe{
            ffi::flow_context_has_error(self.c_ctx)
        }
    }
    /// Used by abi; should not panic
    pub fn abi_clear_error(&mut self){
        unsafe {
            ffi::flow_context_clear_error(self.c_ctx)
        }
    }
    /// Used by abi; should not panic
    pub fn abi_error_code(&self) -> ErrorCode{
        unsafe {
            ffi::flow_context_error_reason(self.c_ctx)
        }
    }

    /// Used by abi; should not panic
    pub fn abi_abort_and_print_on_error(&self) -> bool{
        unsafe {
            ffi::flow_context_print_and_exit_if_err(self.c_ctx)
        }
    }

    /// # Expectations
    ///
    /// * Strings `message` and `function_name`, and `filename` should be null-terminated UTF-8 strings.
    /// * The lifetime of `message` is expected to exceed the duration of this function call.
    /// * The lifetime of `filename` and `function_name` (if provided), is expected to match or exceed the lifetime of `context`.
    /// * You may provide a null value for `filename` or `function_name`, but for the love of puppies,
    /// don't provide a dangling or invalid pointer, that will segfault... a long time later.
    ///
    /// # Caveats
    ///
    /// * You cannot raise a second error until the first has been cleared with
    ///  `imageflow_context_clear_error`. You'll be ignored, as will future
    ///   `imageflow_add_to_callstack` invocations.
    /// * If you provide an error code of zero (why?!), a different error code will be provided.
    pub fn abi_raise_error_c_style(&mut self,
                                   error_code: ErrorCode,
                                   message: Option<&CStr>,
                                   filename: Option<&'static CStr>,
                                   line: Option<i32>,
                                   function_name: Option<&'static CStr>)
                                   -> bool {
        unsafe {
            ffi::flow_context_raise_error(self.c_ctx, error_code,
                                          message.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null()),
                                          filename.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null()),
                                          line.unwrap_or(-1),
                                          function_name.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null()))
        }
    }


    ///
    /// Adds the given UTF-8 filename, line number, and function name to the call stack.
    ///
    /// Returns `true` if add was successful.
    ///
    /// # Will fail and return false if...
    ///
    /// * You haven't raised an error
    /// * You tried to raise a second error without clearing the first one. Call will be ignored.
    /// * You've exceeded the capacity of the call stack (which, at one point, was 14). But this
    ///   category of failure is acceptable.
    pub fn abi_add_to_callstack_c_style(&mut self, filename: Option<&'static CStr>,
                                        line: Option<i32>,
                                        function_name: Option<&'static CStr>)
                                        -> bool {


        unsafe {
            ffi::flow_context_add_to_callstack(self.c_ctx,
                                               filename.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null()),
                                               line.unwrap_or(-1),
                                               function_name.map(|cstr| cstr.as_ptr()).unwrap_or(ptr::null()))
        }
    }

    pub fn assert_ok(&self) {
        if let Some(e) = self.get_error_copy() {
            e.panic_time();
        }
    }


    pub fn get_error_copy(&self) -> Option<FlowError> {
        if self.abi_has_error(){
            match self.abi_error_code(){
                0 => panic!("Inconsistent errors"),
                10 => Some(FlowError::Oom),
                _ => Some(FlowError::Err(unsafe { ErrorBuffer::get_flow_err(self.c_ctx) })),
            }
        }else{
            None
        }
    }
    unsafe fn get_flow_err(c: *mut ::ffi::ImageflowContext) -> FlowErr {


        let code = ::ffi::flow_context_error_reason(c);
        let mut buf = vec![0u8; 2048];


        let chars_written =
        ::ffi::flow_context_error_and_stacktrace(c, buf.as_mut_ptr(), buf.len(), false);

        if chars_written < 0 {
            panic!("Error msg doesn't fit in 2kb");
        } else {
            buf.resize(chars_written as usize, 0u8);
        }

        FlowErr {
            code: code,
            message_and_stack: String::from_utf8(buf).unwrap(),
        }

    }


}
impl Drop for Context {
    /// Used by abi; should not panic
    fn drop(&mut self) {
        if !self.c_ctx.is_null() {
            unsafe {
                ffi::flow_context_destroy(self.c_ctx);
            }
        }
        self.c_ctx = ptr::null_mut();
        self.error.borrow_mut().c_ctx = ptr::null_mut();
    }
}