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

43 lines
1.3 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();
}
2024-11-16 13:48:31 -06:00
#[no_mangle]
unsafe extern "C" fn tok() {
let code = core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(OUTPUT).cast(), OUTPUT_LEN);
OUTPUT_LEN = fmt::get_token_kinds(code);
}
2024-10-10 01:35:17 -05:00
#[no_mangle]
unsafe extern "C" fn minify() {
2024-11-16 13:48:31 -06:00
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
}