This commit is contained in:
2025-11-26 14:27:12 -07:00
parent 1579d9f905
commit b03f32da00
4 changed files with 47 additions and 227 deletions

View File

@@ -1,34 +1,58 @@
use compiler::Compiler;
use parser::Parser;
use safer_ffi::ffi_export;
use std::io::BufWriter;
use std::{
ffi::{CStr, CString},
io::BufWriter,
};
use tokenizer::Tokenizer;
#[ffi_export]
fn compile_from_string(
input: &safer_ffi::string::String,
output: &mut safer_ffi::string::String,
) -> bool {
let mut writer = BufWriter::new(Vec::new());
/// Takes a raw pointer to a string and compiles the `slang` code into valid IC10
/// # Safety
/// This must be called with a valid string pointer from C# (or wherever is calling this function)
#[no_mangle]
pub unsafe extern "C" fn compile_from_string(
input_ptr: *const std::os::raw::c_char,
) -> *mut std::os::raw::c_char {
if input_ptr.is_null() {
return std::ptr::null_mut();
}
let tokenizer = Tokenizer::from(input.to_string());
let c_str = unsafe { CStr::from_ptr(input_ptr) };
let Ok(input_str) = c_str.to_str() else {
return std::ptr::null_mut();
};
let mut writer = BufWriter::new(Vec::new());
let tokenizer = Tokenizer::from(input_str);
let parser = Parser::new(tokenizer);
let compiler = Compiler::new(parser, &mut writer, None);
let Ok(()) = compiler.compile() else {
return false;
return std::ptr::null_mut();
};
let Ok(buffer) = writer.into_inner() else {
return false;
return std::ptr::null_mut();
};
let Ok(output_string) = String::from_utf8(buffer) else {
return false;
};
let c_string = CString::from_vec_unchecked(buffer);
*output = output_string.into();
return true;
c_string.into_raw()
}
/// Takes ownership of the string pointer and drops it, freeing the memory
/// # Safety
/// Must be called with a valid string pointer
#[no_mangle]
pub unsafe extern "C" fn free_slang_string(input_ptr: *mut std::os::raw::c_char) {
if input_ptr.is_null() {
return;
}
unsafe {
// Takes ownership of the input string, and then drops it immediately
let _ = CString::from_raw(input_ptr);
}
}