holey-bytes/depell/wasm-fmt/src/lib.rs

35 lines
1.1 KiB
Rust
Raw Normal View History

2024-10-08 17:17:13 -05:00
#![no_std]
#![feature(str_from_raw_parts)]
2024-10-10 01:35:17 -05:00
#![feature(alloc_error_handler)]
2024-10-12 06:07:49 -05:00
use hblang::{fmt, parser};
2024-10-08 17:17:13 -05:00
2024-10-12 06:07:49 -05:00
wasm_rt::decl_runtime!(128 * 1024, 1024 * 4);
2024-10-08 17:17:13 -05:00
2024-10-12 06:07:49 -05:00
const MAX_OUTPUT_SIZE: usize = 1024 * 10;
wasm_rt::decl_buffer!(MAX_OUTPUT_SIZE, MAX_OUTPUT, OUTPUT, OUTPUT_LEN);
2024-10-08 17:17:13 -05:00
2024-10-12 06:07:49 -05:00
const MAX_INPUT_SIZE: usize = 1024 * 4;
wasm_rt::decl_buffer!(MAX_INPUT_SIZE, MAX_INPUT, INPUT, INPUT_LEN);
2024-10-10 01:35:17 -05:00
#[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-12 06:07:49 -05:00
let arena = parser::Arena::with_capacity(code.len() * parser::SOURCE_TO_AST_FACTOR);
let mut ctx = parser::Ctx::default();
2024-10-13 08:22:16 -05:00
let exprs = parser::Parser::parse(&mut ctx, code, "source.hb", &mut parser::no_loader, &arena);
2024-10-08 17:17:13 -05:00
2024-10-12 06:07:49 -05:00
let mut f = wasm_rt::Write(&mut OUTPUT[..]);
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);
2024-10-12 06:07:49 -05:00
OUTPUT_LEN = fmt::minify(code);
2024-10-08 17:17:13 -05:00
}