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

Add basic wasm32-unknown-unknown support #148

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,43 @@ jobs:
with:
command: run
args: --example default

wasm-browser-test:
name: test wasm support for browser
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
browser:
- firefox
- chrome
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install wasm-pack
uses: actions-rs/install@v0.1
with:
crate: wasm-pack
version: latest
use-tool-cache: true

- name: Test on browser
run: wasm-pack test --headless --${{ matrix.browser }} -- --no-default-features --test web

wasm-node-test:
name: test wasm support for node
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install wasm-pack
uses: actions-rs/install@v0.1
with:
crate: wasm-pack
version: latest
use-tool-cache: true

- name: Test on node
run: wasm-pack test --node -- --no-default-features --test node
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ termcolor = { version = "1.0.2", optional = true }
humantime = { version = "2.0.0", optional = true }
atty = { version = "0.2.5", optional = true }

[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = "^0.2.64"

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "^0.3.19"

[[test]]
name = "regexp_filter"
harness = false
Expand Down
1 change: 1 addition & 0 deletions src/fmt/writer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod atty;
mod termcolor;
mod wasm;

use self::atty::{is_stderr, is_stdout};
use self::termcolor::BufferWriter;
Expand Down
6 changes: 6 additions & 0 deletions src/fmt/writer/termcolor/shim_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

#[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
use crate::fmt::writer::wasm;
use crate::fmt::{Target, WriteStyle};

pub(in crate::fmt::writer) mod glob {}
Expand Down Expand Up @@ -33,6 +35,10 @@ impl BufferWriter {
// This is so their output can be captured by `cargo test`
let log = String::from_utf8_lossy(&buf.0);

#[cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
wasm::print(&log, self.target);

#[cfg(not(all(target_arch = "wasm32", target_vendor = "unknown")))]
match self.target {
Target::Stderr => eprint!("{}", log),
Target::Stdout => print!("{}", log),
Expand Down
37 changes: 37 additions & 0 deletions src/fmt/writer/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! logging functions from wasm-bindgen.
//!
//! Here use the one-param logging functions, all messages should be transformed
//! to string before passing to the functions. Note that we only need this
//! module for `wasm32-unknown-unknown` target
#![cfg(not(feature = "termcolor"))]
#![cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]

// use log::Level;
use wasm_bindgen::prelude::*;

use crate::fmt::glob::Target;

pub(in crate::fmt::writer) fn print(msg: &str, t: Target) {
// work around for unused variable
let _ = t;

log(&msg);
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace=console)]
fn error(text: &str);

#[wasm_bindgen(js_namespace=console)]
fn info(text: &str);

#[wasm_bindgen(js_namespace=console)]
fn warn(text: &str);

#[wasm_bindgen(js_namespace=console)]
fn debug(text: &str);

#[wasm_bindgen(js_namespace=console)]
fn log(text: &str);
}
25 changes: 25 additions & 0 deletions tests/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]
#![cfg(test)]

use env_logger::Env;
use log::{debug, error, info, trace, warn};

use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn main() {
// The `Env` lets us tweak what the environment
// variables to read are and what the default
// value is if they're missing
let env = Env::default()
.filter_or("MY_LOG_LEVEL", "trace")
.write_style_or("MY_LOG_STYLE", "always");

env_logger::init_from_env(env);

trace!("some trace log");
debug!("some debug log");
info!("some information log");
warn!("some warning log");
error!("some error log");
}
27 changes: 27 additions & 0 deletions tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![cfg(test)]
#![cfg(all(target_arch = "wasm32", target_vendor = "unknown"))]

use env_logger::Env;
use log::{debug, error, info, trace, warn};

use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn main() {
// The `Env` lets us tweak what the environment
// variables to read are and what the default
// value is if they're missing
let env = Env::default()
.filter_or("MY_LOG_LEVEL", "trace")
.write_style_or("MY_LOG_STYLE", "always");

env_logger::init_from_env(env);

trace!("some trace log");
debug!("some debug log");
info!("some information log");
warn!("some warning log");
error!("some error log");
}