Skip to content

Commit

Permalink
use std in the runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrosen committed Jun 2, 2024
1 parent c78d37c commit 7616147
Showing 1 changed file with 5 additions and 37 deletions.
42 changes: 5 additions & 37 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#![no_std]

use core::arch::wasm32;
use core::ptr;
use core::mem;
use core::slice;
use std::mem;
use std::slice;

pub type ClockyFunc = extern "C" fn(*const Closure) -> *const ();

Expand Down Expand Up @@ -83,12 +79,6 @@ pub unsafe extern "C" fn since_last_tick_stream(clock: *const Clock) -> *const S
since_last_tick_closure(clos)
}

#[cfg(not(test))]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo<'_>) -> ! {
core::arch::wasm32::unreachable()
}

#[no_mangle]
pub extern "C" fn sin(x: f32) -> f32 {
libm::sinf(x)
Expand All @@ -99,31 +89,9 @@ pub extern "C" fn cos(x: f32) -> f32 {
libm::cosf(x)
}

static mut HEAP_PTR: *mut () = ptr::null_mut();
static mut HEAP_END: *mut () = ptr::null_mut();

// ??
const ALLOC_SIZE_PAGES: usize = 4;

#[no_mangle]
pub unsafe extern "C" fn alloc(n: u32) -> *mut () {
let old_heap_ptr = HEAP_PTR;
if let Some(new_heap_ptr) = (HEAP_PTR as usize).checked_add(n as usize) {
if new_heap_ptr < HEAP_END as usize {
HEAP_PTR = new_heap_ptr as *mut ();
old_heap_ptr
} else {
let old_size = wasm32::memory_grow(0, ALLOC_SIZE_PAGES);
if old_size < usize::MAX {
let allocation = (old_size * 65536) as *mut ();
HEAP_PTR = (old_size * 65536 + n as usize) as *mut ();
HEAP_END = ((old_size + ALLOC_SIZE_PAGES) * 65536) as *mut ();
allocation
} else {
panic!()
}
}
} else {
panic!()
}
// TODO: obviously so much. but right now i'm thinking about the
// alignment
std::alloc::alloc(std::alloc::Layout::from_size_align(n as usize, 4).unwrap()) as *mut ()
}

0 comments on commit 7616147

Please sign in to comment.