Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework the WebAssembly Targets #263

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/build_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ main() {
$cargo build -p livesplit --target $TARGET --release $FEATURES
;;
wasm32-unknown-unknown)
$cargo build -p cdylib --target $TARGET --release $FEATURES
$cargo build -p cdylib --target $TARGET $release_flag $FEATURES
;;
wasm32-wasi)
$cargo build -p cdylib --target $TARGET $release_flag $FEATURES
;;
*)
$cargo build -p staticlib --target $TARGET $release_flag $FEATURES
Expand Down
28 changes: 24 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ jobs:
- Bare Metal RISC-V 64 gc
- Bare Metal RISC-V 64 imac

# Web
# WebAssembly
# FIXME: emscripten is broken at the moment. cross' emscripten is too old.
# - Web asmjs
# - Web wasm32 emscripten
- Web wasm32
- WebAssembly Unknown
- WebAssembly Web
- WebAssembly WASI

# Windows
- Windows aarch64
Expand Down Expand Up @@ -253,7 +255,7 @@ jobs:
no_std: true
install_target: true

# Web
# WebAssembly
# - label: Web asmjs
# target: asmjs-unknown-emscripten
# os: ubuntu-latest
Expand All @@ -269,9 +271,27 @@ jobs:
# tests: skip
# dylib: skip

- label: Web wasm32
- label: WebAssembly Unknown
target: wasm32-unknown-unknown
os: ubuntu-latest
cross: skip
tests: skip
dylib: skip
install_target: true

- label: WebAssembly Web
target: wasm32-unknown-unknown
os: ubuntu-latest
cross: skip
tests: skip
dylib: skip
install_target: true
features: "--features wasm-web"

- label: WebAssembly WASI
target: wasm32-wasi
os: ubuntu-latest
cross: skip
tests: skip
dylib: skip
install_target: true
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ smallvec = { version = "1.0.0", default-features = false, optional = true }
euc = { version = "0.4.2", default-features = false, optional = true }
vek = { version = "0.9.8", default-features = false, optional = true }

# WebAssembly in the Web
web-sys = { version = "0.3.28", default-features = false, features = ["Performance", "Window"], optional = true }

[dev-dependencies]
memmem = "0.1.1"
criterion = "0.3.0"
Expand All @@ -83,6 +86,7 @@ more-image-formats = ["image/webp", "image/pnm", "image/ico", "image/jpeg", "ima
image-shrinking = ["more-image-formats"]
rendering = ["std", "more-image-formats", "euclid", "livesplit-title-abbreviations", "lyon", "rusttype", "smallvec"]
software-rendering = ["rendering", "euc", "vek", "derive_more/mul"]
wasm-web = ["std", "web-sys", "chrono/wasmbind", "livesplit-hotkey/wasm-web"]

# FIXME: Some targets don't have atomics, but we can't test for this properly
# yet. So there's a feature you explicitly have to opt into to deactivate the
Expand Down
1 change: 1 addition & 0 deletions capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ serde_json = "1.0.8"
[features]
default = ["image-shrinking"]
image-shrinking = ["livesplit-core/image-shrinking"]
wasm-web = ["livesplit-core/wasm-web"]
1 change: 1 addition & 0 deletions capi/cdylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ crate-type = ["cdylib"]
[features]
default = ["image-shrinking"]
image-shrinking = ["livesplit-core-capi/image-shrinking"]
wasm-web = ["livesplit-core-capi/wasm-web"]
4 changes: 2 additions & 2 deletions capi/src/hotkey_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The configuration to use for a Hotkey System. It describes with keys to use
//! as hotkeys for the different actions.

use super::{output_vec, str, Json, get_file, release_file};
use super::{get_file, output_vec, release_file, str, Json};
use crate::setting_value::OwnedSettingValue;
use livesplit_core::HotkeyConfig;
use serde_json;
Expand Down Expand Up @@ -59,7 +59,7 @@ pub unsafe extern "C" fn HotkeyConfig_parse_json(settings: Json) -> NullableOwne
HotkeyConfig::from_json(settings).ok().map(Box::new)
}

/// Attempts to parse a hotkey configuration from a given file. <NULL> is
/// Attempts to parse a hotkey configuration from a given file. <NULL> is
/// returned it couldn't be parsed. This will not close the file descriptor /
/// handle.
#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion capi/src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A Layout allows you to combine multiple components together to visualize a
//! variety of information the runner is interested in.

use super::{output_vec, str, Json, get_file, release_file};
use super::{get_file, output_vec, release_file, str, Json};
use crate::component::OwnedComponent;
use livesplit_core::layout::{parser, LayoutSettings};
use livesplit_core::{Layout, Timer};
Expand Down
27 changes: 26 additions & 1 deletion capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ unsafe fn str(s: *const c_char) -> &'static str {
}
}

//raw file descriptor handling
// raw file descriptor handling
#[cfg(unix)]
unsafe fn get_file(fd: i64) -> File {
use std::os::unix::io::FromRawFd;
Expand Down Expand Up @@ -153,3 +153,28 @@ unsafe fn release_file(file: File) {

#[cfg(not(any(windows, unix)))]
unsafe fn release_file(_: File) {}

/// Allocate memory.
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi", feature = "wasm-web")),
))]
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *mut u8 {
let mut buf = Vec::with_capacity(size);
let ptr = buf.as_mut_ptr();
core::mem::forget(buf);
ptr
}

/// Deallocate memory.
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi", feature = "wasm-web")),
))]
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut u8, cap: usize) {
unsafe {
let _buf = Vec::from_raw_parts(ptr, 0, cap);
}
}
2 changes: 1 addition & 1 deletion capi/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A Run stores the split times for a specific game and category of a runner.

use super::{output_str, output_time_span, output_vec, str, get_file, release_file};
use super::{get_file, output_str, output_time_span, output_vec, release_file, str};
use crate::parse_run_result::OwnedParseRunResult;
use crate::segment::OwnedSegment;
use livesplit_core::run::{parser, saver};
Expand Down
1 change: 1 addition & 0 deletions capi/staticlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ crate-type = ["staticlib"]
[features]
default = ["image-shrinking"]
image-shrinking = ["livesplit-core-capi/image-shrinking"]
wasm-web = ["livesplit-core-capi/wasm-web"]
7 changes: 6 additions & 1 deletion crates/livesplit-hotkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ promising-future = { version = "0.2.4", optional = true }
stdweb = { version = "0.3.0", optional = true }
parking_lot = { version = "0.9.0", optional = true }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
wasm-bindgen = { version = "0.2.54", optional = true }
web-sys = { version = "0.3.28", default-features = false, features = ["KeyboardEvent", "EventTarget", "Window"], optional = true }

[dependencies]
cfg-if = "0.1.10"
snafu = { version = "0.6.0", default-features = false }
serde = { version = "1.0.98", default-features = false, features = ["derive", "alloc"] }
snafu = { version = "0.6.0", default-features = false }

[features]
default = ["std"]
std = ["snafu/std", "serde/std", "stdweb", "parking_lot", "x11-dl", "mio", "promising-future", "winapi", "parking_lot"]
wasm-web = ["wasm-bindgen", "web-sys"]
11 changes: 9 additions & 2 deletions crates/livesplit-hotkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ cfg_if::cfg_if! {
#[macro_use]
extern crate stdweb;
} else if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
pub mod wasm;
pub use crate::wasm::*;
cfg_if::cfg_if! {
if #[cfg(feature = "wasm-web")] {
mod wasm_web;
pub use self::wasm_web::*;
} else {
mod wasm_unknown;
pub use self::wasm_unknown::*;
}
}
} else {
pub mod other;
pub use crate::other::*;
Expand Down
Loading