Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from max-niederman/wasm-no-std
Browse files Browse the repository at this point in the history
remove `std` dependency for WASM builds
  • Loading branch information
Gavin-Niederman authored Oct 21, 2023
2 parents 7654701 + e7204cf commit 9a80cc7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ target = "./armv7a-vexos-eabi.json"
[target.armv7a-vexos]
runner = "python runner.py"

[target.wasm32-unknown-unknown]
rustflags = [
"-Ctarget-feature=+atomics,+bulk-memory,+mutable-globals",
"-Clink-arg=--shared-memory",
]

[unstable]
build-std = ["core", "compiler_builtins", "alloc"]
3 changes: 3 additions & 0 deletions pros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ snafu = { version = "0.7.5", default-features = false, features = [
] }
no_std_io = { version = "0.6.0", features = ["alloc"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
dlmalloc = { version = "0.2.4", features = ["global"] }

[features]
lvgl = ["pros-sys/xapi"]
2 changes: 1 addition & 1 deletion pros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(error_in_core, stdsimd)]
#![cfg_attr(not(target_arch = "wasm32"), no_std)]
#![no_std]

extern crate alloc;

Expand Down
25 changes: 24 additions & 1 deletion pros/src/wasm_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@
extern crate alloc;

use core::panic::PanicInfo;

use alloc::{
alloc::{alloc, dealloc, handle_alloc_error, Layout},
alloc::{alloc, dealloc, handle_alloc_error, GlobalAlloc, Layout},
collections::BTreeMap,
ffi::CString,
format,
};

use dlmalloc::GlobalDlmalloc;

// no multithreading in wasm
static mut LAYOUTS: BTreeMap<*mut u8, Layout> = BTreeMap::new();

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
extern "C" {
fn sim_abort(msg: *const core::ffi::c_char) -> !;
}

let msg_str = format!("{info}");
let msg_c_str = CString::new(msg_str).unwrap();

unsafe {
sim_abort(msg_c_str.as_ptr());
}
}

#[no_mangle]
extern "C" fn wasm_memalign(alignment: usize, size: usize) -> *mut u8 {
if size == 0 {
Expand Down Expand Up @@ -38,3 +58,6 @@ extern "C" fn wasm_free(ptr: *mut u8) {
unsafe { dealloc(ptr, layout) };
}
}

#[global_allocator]
static ALLOCATOR: GlobalDlmalloc = GlobalDlmalloc;

0 comments on commit 9a80cc7

Please sign in to comment.