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

Improve the quality of the emscripten_set_main_loop abstraction #608

Merged
merged 5 commits into from
Aug 5, 2021
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
29 changes: 28 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set toolchain versions
run: |
echo "::set-output name=rust::$(cat rust-toolchain)"
echo "::set-output name=emscripten::$(cat emscripten-toolchain)"
id: toolchain_versions

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
components: rustfmt, clippy
target: wasm32-unknown-emscripten

- uses: Swatinem/rust-cache@v1

Expand All @@ -60,7 +67,21 @@ jobs:
ruby-version: ".ruby-version"
bundler-cache: true

- uses: Swatinem/rust-cache@v1
- name: Cache emsdk
uses: actions/cache@v2
id: cache
with:
path: "emsdk-cache"
key: emscripten-emsdk-${{ runner.os }}-emsdk-${{ steps.toolchain_versions.outputs.emscripten }}-clippy

- name: Install Emscripten toolchain
uses: mymindstorm/setup-emsdk@v10
with:
version: ${{ steps.toolchain_versions.outputs.emscripten }}
actions-cache-folder: "emsdk-cache"

- name: Verify emcc version
run: emcc -v

- name: Check formatting
run: cargo fmt -- --check --color=auto
Expand All @@ -71,6 +92,12 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
args: --workspace --all-features --all-targets

- name: Lint with Clippy on emscripten target
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --workspace --all-features --all-targets --target wasm32-unknown-emscripten

ruby:
name: Lint and format Ruby
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/playground.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
key: emscripten-emsdk-${{ runner.os }}-emsdk-${{ steps.toolchain_versions.outputs.emscripten }}

- name: Install Emscripten toolchain
uses: mymindstorm/setup-emsdk@v7
uses: mymindstorm/setup-emsdk@v10
with:
version: ${{ steps.toolchain_versions.outputs.emscripten }}
actions-cache-folder: "emsdk-cache"
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace :lint do
desc 'Lint Rust sources with Clippy'
task :clippy do
sh 'cargo clippy --workspace --all-features --all-targets'
sh 'cargo clippy --workspace --all-features --all-targets --target wasm32-unknown-emscripten'
end

desc 'Lint Rust sources with Clippy restriction pass (unenforced lints)'
Expand Down
45 changes: 15 additions & 30 deletions playground/src/emscripten.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,38 @@
#![cfg(target_os = "emscripten")]
#![allow(dead_code)]
#![allow(trivial_casts)]
#![allow(unused_imports)]

// taken from https://github.com/Gigoteur/PX8/blob/master/src/px8/emscripten.rs
// taken from https://github.com/gliheng/rust-wasm/blob/d89a71f4a68101c7b4e2944973b39a2c20b21ebe/sdl2-drag/src/emscripten.rs

use std::cell::RefCell;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_float, c_int, c_void};
use std::ptr::null_mut;
use std::os::raw::c_int;

#[allow(non_camel_case_types)]
type em_callback_func = unsafe extern "C" fn();

extern "C" {
// void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop)
pub fn emscripten_set_main_loop(
func: em_callback_func,
fps: c_int,
simulate_infinite_loop: c_int,
);

pub fn emscripten_cancel_main_loop();
pub fn emscripten_pause_main_loop();
pub fn emscripten_get_now() -> c_float;
fn emscripten_set_main_loop(func: em_callback_func, fps: c_int, simulate_infinite_loop: c_int);
}

thread_local!(static MAIN_LOOP_CALLBACK: RefCell<*mut c_void> = RefCell::new(null_mut()));
thread_local!(static MAIN_LOOP_CALLBACK: RefCell<Option<Box<dyn FnMut()>>> = RefCell::new(None));

unsafe extern "C" fn wrapper() {
MAIN_LOOP_CALLBACK.with(|z| {
if let Some(main_loop) = &mut *z.borrow_mut() {
main_loop();
}
});
}

pub fn set_main_loop_callback<F>(callback: F)
where
F: FnMut(),
F: FnMut() + 'static,
{
MAIN_LOOP_CALLBACK.with(|log| {
*log.borrow_mut() = &callback as *const _ as *mut c_void;
MAIN_LOOP_CALLBACK.with(|z| {
z.borrow_mut().replace(Box::new(callback));
});

unsafe {
emscripten_set_main_loop(wrapper::<F>, -1, 1);
}

unsafe extern "C" fn wrapper<F>()
where
F: FnMut(),
{
MAIN_LOOP_CALLBACK.with(|z| {
let closure = *z.borrow_mut() as *mut F;
(*closure)();
});
emscripten_set_main_loop(wrapper, -1, 1);
}
}