feat: some shit

This commit is contained in:
2026-01-23 13:40:31 +01:00
parent 30afb30341
commit 4cb24e8ff9
13 changed files with 249 additions and 41 deletions

View File

@@ -0,0 +1,68 @@
use core::alloc::{GlobalAlloc, Layout};
use core::sync::atomic::{AtomicUsize, Ordering};
extern "C" {
fn __wasm_memory_size() -> usize;
fn __nodarium_manual_end() -> usize;
}
#[allow(dead_code)]
const WASM_PAGE_SIZE: usize = 64 * 1024;
pub struct DownwardBumpAllocator {
heap_top: AtomicUsize,
}
impl Default for DownwardBumpAllocator {
fn default() -> Self {
Self::new()
}
}
impl DownwardBumpAllocator {
pub const fn new() -> Self {
Self {
heap_top: AtomicUsize::new(0),
}
}
#[allow(dead_code)]
pub fn init(&self) {
let pages = unsafe { __wasm_memory_size() };
let mem_size = pages * WASM_PAGE_SIZE;
self.heap_top.store(mem_size, Ordering::Relaxed);
}
}
#[global_allocator]
pub static ALLOCATOR: DownwardBumpAllocator = DownwardBumpAllocator::new();
unsafe impl GlobalAlloc for DownwardBumpAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let align = layout.align();
let size = layout.size();
let mut current = self.heap_top.load(Ordering::Relaxed);
loop {
let aligned = (current - size) & !(align - 1);
let manual_end = unsafe { __nodarium_manual_end() };
if aligned < manual_end {
return core::ptr::null_mut();
}
match self.heap_top.compare_exchange(
current,
aligned,
Ordering::SeqCst,
Ordering::Relaxed,
) {
Ok(_) => return aligned as *mut u8,
Err(next) => current = next,
}
}
}
unsafe fn dealloc(&self, _: *mut u8, _: Layout) {}
}

View File

@@ -1,3 +1,5 @@
use crate::log;
pub fn encode_float(f: f32) -> i32 {
// Convert f32 to u32 using to_bits, then safely cast to i32
let bits = f.to_bits();
@@ -12,6 +14,7 @@ pub fn decode_float(bits: i32) -> f32 {
#[inline]
pub fn read_i32(ptr: i32) -> i32 {
log!("read_i32 ptr: {:?}", ptr);
unsafe {
let _ptr = ptr as *const i32;
*_ptr

View File

@@ -1,3 +1,4 @@
pub mod allocator;
mod encoding;
mod nodes;
mod tree;
@@ -11,7 +12,7 @@ extern "C" {
pub fn __nodarium_log(ptr: *const u8, len: usize);
}
#[cfg(debug_assertions)]
// #[cfg(debug_assertions)]
#[macro_export]
macro_rules! log {
($($t:tt)*) => {{
@@ -25,13 +26,13 @@ macro_rules! log {
}}
}
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {{
// This will expand to nothing in release builds
}};
}
// #[cfg(not(debug_assertions))]
// #[macro_export]
// macro_rules! log {
// ($($arg:tt)*) => {{
// // This will expand to nothing in release builds
// }};
// }
#[allow(dead_code)]
#[rustfmt::skip]

View File

@@ -1,11 +1,13 @@
interface NodariumExports extends WebAssembly.Exports {
memory: WebAssembly.Memory;
execute: (outputPos: number, ...args: number[]) => number;
init_allocator: () => void;
}
export function createWasmWrapper(buffer: ArrayBuffer, memory: WebAssembly.Memory) {
let exports: NodariumExports;
let end = 0;
const importObject = {
env: {
memory: memory,
@@ -25,14 +27,11 @@ export function createWasmWrapper(buffer: ArrayBuffer, memory: WebAssembly.Memor
const module = new WebAssembly.Module(buffer);
const instance = new WebAssembly.Instance(module, importObject);
exports = instance.exports as NodariumExports;
exports.init_allocator();
function execute(outputPos: number, args: number[]): number {
try {
return exports.execute(outputPos, ...args);
} catch (e) {
console.log(e);
return -1;
}
end = outputPos;
return exports.execute(outputPos, ...args);
}
function get_definition() {