|
| 1 | +use core::str; |
| 2 | +use std::{ |
| 3 | + ffi::c_int, |
| 4 | + slice, |
| 5 | + sync::{Arc, Mutex, OnceLock}, |
| 6 | +}; |
| 7 | + |
| 8 | +use breakwater_parser::{OriginalParser, Parser, SimpleFrameBuffer}; |
| 9 | +use libc::size_t; |
| 10 | + |
| 11 | +static ORIGINAL_PARSER: OnceLock<Mutex<OriginalParser<SimpleFrameBuffer>>> = OnceLock::new(); |
| 12 | + |
| 13 | +#[unsafe(no_mangle)] |
| 14 | +pub extern "C" fn breakwater_init_original_parser(width: c_int, height: c_int) { |
| 15 | + ORIGINAL_PARSER.get_or_init(|| { |
| 16 | + // let fb = Arc::new(NoopFrameBuffer::new( |
| 17 | + // width.try_into().unwrap(), |
| 18 | + // height.try_into().unwrap(), |
| 19 | + // )); |
| 20 | + let fb = Arc::new(SimpleFrameBuffer::new( |
| 21 | + width.try_into().unwrap(), |
| 22 | + height.try_into().unwrap(), |
| 23 | + )); |
| 24 | + |
| 25 | + Mutex::new(OriginalParser::new(fb)) |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +#[unsafe(no_mangle)] |
| 30 | +pub extern "C" fn breakwater_original_parser_parser_lookahead() -> size_t { |
| 31 | + let parser = ORIGINAL_PARSER |
| 32 | + .get() |
| 33 | + .expect("Call breakwater_init_original_parser first!"); |
| 34 | + |
| 35 | + parser |
| 36 | + .lock() |
| 37 | + .unwrap() |
| 38 | + .parser_lookahead() |
| 39 | + .try_into() |
| 40 | + .unwrap() |
| 41 | +} |
| 42 | + |
| 43 | +#[unsafe(no_mangle)] |
| 44 | +pub extern "C" fn breakwater_original_parser_parse(buffer: *mut u8, buffer_len: size_t) -> size_t { |
| 45 | + let parser = ORIGINAL_PARSER |
| 46 | + .get() |
| 47 | + .expect("Call breakwater_init_original_parser first!"); |
| 48 | + |
| 49 | + let buffer = unsafe { slice::from_raw_parts(buffer, buffer_len.try_into().unwrap()) }; |
| 50 | + |
| 51 | + // FIXME: Somehow return the response to the C side |
| 52 | + let mut response = Vec::new(); |
| 53 | + let parsed = { parser.lock().unwrap().parse(buffer, &mut response) }; |
| 54 | + dbg!(str::from_utf8(&response).unwrap()); |
| 55 | + |
| 56 | + parsed.try_into().unwrap() |
| 57 | +} |
0 commit comments