2024-10-08 17:17:13 -05:00
|
|
|
#![no_std]
|
|
|
|
#![feature(slice_take)]
|
|
|
|
#![feature(str_from_raw_parts)]
|
2024-10-10 01:35:17 -05:00
|
|
|
#![feature(alloc_error_handler)]
|
|
|
|
|
|
|
|
use {
|
|
|
|
core::{
|
|
|
|
alloc::{GlobalAlloc, Layout},
|
|
|
|
cell::UnsafeCell,
|
|
|
|
},
|
|
|
|
hblang::parser::ParserCtx,
|
|
|
|
};
|
2024-10-08 17:17:13 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
const ARENA_SIZE: usize = 128 * 1024;
|
|
|
|
const MAX_OUTPUT_SIZE: usize = 1024 * 10;
|
|
|
|
const MAX_INPUT_SIZE: usize = 1024 * 4;
|
2024-10-08 17:17:13 -05:00
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[panic_handler]
|
2024-10-10 01:35:17 -05:00
|
|
|
pub fn handle_panic(_info: &core::panic::PanicInfo) -> ! {
|
|
|
|
//unsafe {
|
|
|
|
// use core::fmt::Write;
|
|
|
|
// let mut f = Write(&mut PANIC_MESSAGE[..]);
|
|
|
|
// _ = writeln!(f, "{}", info);
|
|
|
|
// PANIC_MESSAGE_LEN = 1024 - f.0.len();
|
|
|
|
//}
|
|
|
|
|
|
|
|
core::arch::wasm32::unreachable();
|
2024-10-08 17:17:13 -05:00
|
|
|
}
|
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
#[global_allocator]
|
|
|
|
static ALLOCATOR: ArenaAllocator = ArenaAllocator::new();
|
2024-10-08 17:17:13 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[alloc_error_handler]
|
|
|
|
fn alloc_error(_: core::alloc::Layout) -> ! {
|
|
|
|
core::arch::wasm32::unreachable()
|
|
|
|
}
|
2024-10-08 17:17:13 -05:00
|
|
|
|
|
|
|
#[repr(C, align(32))]
|
2024-10-10 01:35:17 -05:00
|
|
|
struct ArenaAllocator {
|
2024-10-08 17:17:13 -05:00
|
|
|
arena: UnsafeCell<[u8; ARENA_SIZE]>,
|
|
|
|
head: UnsafeCell<*mut u8>,
|
|
|
|
}
|
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
impl ArenaAllocator {
|
2024-10-08 17:17:13 -05:00
|
|
|
const fn new() -> Self {
|
2024-10-10 01:35:17 -05:00
|
|
|
ArenaAllocator {
|
2024-10-08 17:17:13 -05:00
|
|
|
arena: UnsafeCell::new([0; ARENA_SIZE]),
|
|
|
|
head: UnsafeCell::new(core::ptr::null_mut()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn reset(&self) {
|
|
|
|
(*self.head.get()) = self.arena.get().cast::<u8>().add(ARENA_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
unsafe impl Sync for ArenaAllocator {}
|
2024-10-08 17:17:13 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
unsafe impl GlobalAlloc for ArenaAllocator {
|
2024-10-08 17:17:13 -05:00
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
|
|
let size = layout.size();
|
|
|
|
let align = layout.align();
|
|
|
|
|
|
|
|
let until = self.arena.get() as *mut u8;
|
|
|
|
|
|
|
|
let new_head = (*self.head.get()).sub(size);
|
2024-10-10 01:35:17 -05:00
|
|
|
let aligned_head = (new_head as usize & !(1 << (align - 1))) as *mut u8;
|
2024-10-08 17:17:13 -05:00
|
|
|
|
|
|
|
if until > aligned_head {
|
|
|
|
return core::ptr::null_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
*self.head.get() = aligned_head;
|
|
|
|
aligned_head
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
|
|
|
/* lol */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
struct Write<'a>(&'a mut [u8]);
|
2024-10-08 17:17:13 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
impl core::fmt::Write for Write<'_> {
|
|
|
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
|
|
|
if let Some(m) = self.0.take_mut(..s.len()) {
|
|
|
|
m.copy_from_slice(s.as_bytes());
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(core::fmt::Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#[no_mangle]
|
|
|
|
//static mut PANIC_MESSAGE: [u8; 1024] = unsafe { core::mem::zeroed() };
|
|
|
|
//#[no_mangle]
|
|
|
|
//static mut PANIC_MESSAGE_LEN: usize = 0;
|
2024-10-08 17:17:13 -05:00
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
static mut OUTPUT: [u8; MAX_OUTPUT_SIZE] = unsafe { core::mem::zeroed() };
|
|
|
|
#[no_mangle]
|
|
|
|
static mut OUTPUT_LEN: usize = 0;
|
|
|
|
|
|
|
|
#[no_mangle]
|
2024-10-10 01:35:17 -05:00
|
|
|
static MAX_INPUT: usize = MAX_INPUT_SIZE;
|
|
|
|
#[no_mangle]
|
|
|
|
static mut INPUT: [u8; MAX_INPUT_SIZE] = unsafe { core::mem::zeroed() };
|
|
|
|
#[no_mangle]
|
|
|
|
static mut INPUT_LEN: usize = 0;
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn fmt() {
|
2024-10-08 17:17:13 -05:00
|
|
|
ALLOCATOR.reset();
|
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
let code = core::str::from_raw_parts(core::ptr::addr_of!(INPUT).cast(), INPUT_LEN);
|
2024-10-08 17:17:13 -05:00
|
|
|
|
2024-10-10 06:04:17 -05:00
|
|
|
let arena =
|
|
|
|
hblang::parser::Arena::with_capacity(code.len() * hblang::parser::SOURCE_TO_AST_FACTOR);
|
2024-10-08 17:17:13 -05:00
|
|
|
let mut ctx = ParserCtx::default();
|
2024-10-10 08:48:08 -05:00
|
|
|
let exprs =
|
|
|
|
hblang::parser::Parser::parse(&mut ctx, code, "source.hb", &mut |_, _| Ok(0), &arena);
|
2024-10-08 17:17:13 -05:00
|
|
|
|
2024-10-10 01:35:17 -05:00
|
|
|
let mut f = Write(&mut OUTPUT[..]);
|
2024-10-08 17:17:13 -05:00
|
|
|
hblang::fmt::fmt_file(exprs, code, &mut f).unwrap();
|
2024-10-10 01:35:17 -05:00
|
|
|
OUTPUT_LEN = MAX_OUTPUT_SIZE - f.0.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn minify() {
|
|
|
|
let code = core::str::from_raw_parts_mut(core::ptr::addr_of_mut!(OUTPUT).cast(), OUTPUT_LEN);
|
|
|
|
OUTPUT_LEN = hblang::fmt::minify(code);
|
2024-10-08 17:17:13 -05:00
|
|
|
}
|