Skip to content

Commit

Permalink
Add (non-working) cpp-to-wasm test
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Aug 18, 2021
1 parent f4aa662 commit ad74055
Show file tree
Hide file tree
Showing 11 changed files with 6,931 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ffi/capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ all-features = true
# Omit most optional dependency features from permutation testing
skip_optional_dependencies = true
# Bench feature gets tested separately and is only relevant for CI.
# wearos/freertos/x86tiny are not relevant in normal environments,
# wearos/freertos/x86tiny/wasm_embedding are not relevant in normal environments,
# and smaller_static gets tested on the FFI job anyway
denylist = ["bench", "wearos", "freertos", "x86tiny", "smaller_static"]
denylist = ["bench", "wearos", "freertos", "x86tiny", "smaller_static", "wasm_embedding"]

[lib]
crate-type = ["staticlib", "rlib", "cdylib"]
Expand All @@ -56,6 +56,9 @@ x86tiny = ["dlmalloc"]
# Enables no_std builds for freertos
freertos = ["freertos-rust", "cortex-m"]

# Enables things that enable Rust to be linked to C++ (etc) in WASM
wasm_embedding = []

[dependencies]
fixed_decimal = { path = "../../utils/fixed_decimal" }
icu_decimal = { path = "../../components/decimal/" }
Expand Down
2 changes: 2 additions & 0 deletions ffi/capi/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod ffi {
/// Construct an [`ICU4XLocale`] from an locale identifier.
/// See [the Rust docs](https://unicode-org.github.io/icu4x-docs/doc/icu/locid/struct.Locale.html#method.from_bytes) for more information.
pub fn create(name: &str) -> Option<Box<ICU4XLocale>> {
let x = alloc::format!("a {}", "b");
panic!("hi");
Locale::from_str(name)
.ok()
.map(|l| Box::new(ICU4XLocale(l)))
Expand Down
42 changes: 42 additions & 0 deletions ffi/capi/src/wasm_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,45 @@ pub unsafe extern "C" fn icu4x_init() {
.map(|()| log::set_max_level(LevelFilter::Debug))
.unwrap();
}

#[cfg(feature = "wasm_embedding")]
mod allocator {
use alloc::alloc::GlobalAlloc;
use alloc::alloc::Layout;

// The default global allocator for Rust works fine when Rust
// is controlling the entire WASM stack, however when linked to
// C++ it does not, so we fall back to malloc/free
#[global_allocator]
static GLOBAL: WasmMalloc = WasmMalloc;

pub struct WasmMalloc;

extern "C" {
fn malloc(size: usize) -> *mut u8;
fn free(ptr: *mut u8);
fn calloc(num: usize, size: usize) -> *mut u8;
fn realloc(ptr: *mut u8, size: usize) -> *mut u8;
}
unsafe impl GlobalAlloc for WasmMalloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
malloc(layout.size())
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
free(ptr)
}

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
calloc(layout.size(), 1)
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
realloc(ptr, new_size)
}
}
}
1 change: 1 addition & 0 deletions ffi/cpp/examples/fixeddecimal-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a.out
25 changes: 25 additions & 0 deletions ffi/cpp/examples/fixeddecimal-wasm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is part of ICU4X. For terms of use, please see the file
# called LICENSE at the top level of the ICU4X source tree
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

.DEFAULT_GOAL := test
.PHONY: build

ALL_HEADERS := $(wildcard ../../include/*.hpp) $(wildcard ../../../capi/include/*.h)
ALL_RUST := $(wildcard ../../../capi//src/*.rs)

$(ALL_RUST):

$(ALL_HEADERS):




../../../../target/wasm32-unknown-unknown/debug/libicu_capi.a: $(ALL_RUST)
cargo +nightly build -p icu_capi --target wasm32-unknown-unknown --features wasm_embedding

# Currently this doesn't run successfully, however it's the status quo of compiling ICU4X in C++ to WASM
main.html: ../../../../target/wasm32-unknown-unknown/debug/libicu_capi.a $(ALL_HEADERS) test.cpp
emcc -std=c++17 test.cpp ../../../../target/wasm32-unknown-unknown/debug/libicu_capi.a -ldl -lpthread -lm -g -o main.html --emrun -sWASM=1

build: main.html
1,300 changes: 1,300 additions & 0 deletions ffi/cpp/examples/fixeddecimal-wasm/main.html

Large diffs are not rendered by default.

Loading

0 comments on commit ad74055

Please sign in to comment.