Skip to content

Commit

Permalink
Use coverage(off)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 19, 2024
1 parent f5a7516 commit a81f944
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 22 deletions.
24 changes: 24 additions & 0 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl ToTokens for ast::Struct {
(quote! {
#[automatically_derived]
impl #wasm_bindgen::describe::WasmDescribe for #name {
#[coverage(off)]
fn describe() {
use #wasm_bindgen::__wbindgen_if_not_std;
__wbindgen_if_not_std! {
Expand Down Expand Up @@ -203,6 +204,7 @@ impl ToTokens for ast::Struct {
impl #wasm_bindgen::convert::FromWasmAbi for #name {
type Abi = u32;

#[coverage(off)]
unsafe fn from_abi(js: u32) -> Self {
use #wasm_bindgen::__rt::std::boxed::Box;
use #wasm_bindgen::__rt::{assert_not_null, WasmRefCell};
Expand All @@ -225,10 +227,12 @@ impl ToTokens for ast::Struct {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
extern "C" {
#[coverage(off)]
fn #new_fn(ptr: u32) -> u32;
}

#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
#[coverage(off)]
unsafe fn #new_fn(_: u32) -> u32 {
panic!("cannot convert to JsValue outside of the wasm target")
}
Expand All @@ -245,6 +249,7 @@ impl ToTokens for ast::Struct {
const _: () = {
#[no_mangle]
#[doc(hidden)]
#[coverage(off)]
pub unsafe extern "C" fn #free_fn(ptr: u32) {
let _ = <#name as #wasm_bindgen::convert::FromWasmAbi>::from_abi(ptr); //implicit `drop()`
}
Expand Down Expand Up @@ -307,10 +312,12 @@ impl ToTokens for ast::Struct {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
extern "C" {
#[coverage(off)]
fn #unwrap_fn(ptr: u32) -> u32;
}

#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
#[coverage(off)]
unsafe fn #unwrap_fn(_: u32) -> u32 {
panic!("cannot convert from JsValue outside of the wasm target")
}
Expand All @@ -331,6 +338,7 @@ impl ToTokens for ast::Struct {
}

impl #wasm_bindgen::describe::WasmDescribeVector for #name {
#[coverage(off)]
fn describe_vector() {
use #wasm_bindgen::describe::*;
inform(VECTOR);
Expand Down Expand Up @@ -408,6 +416,7 @@ impl ToTokens for ast::StructField {
const _: () = {
#[cfg_attr(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))), no_mangle)]
#[doc(hidden)]
#[coverage(off)]
pub unsafe extern "C" fn #getter(js: u32)
-> #wasm_bindgen::convert::WasmRet<<#ty as #wasm_bindgen::convert::IntoWasmAbi>::Abi>
{
Expand Down Expand Up @@ -449,6 +458,7 @@ impl ToTokens for ast::StructField {
const _: () = {
#[no_mangle]
#[doc(hidden)]
#[coverage(off)]
pub unsafe extern "C" fn #setter(
js: u32,
#(#args,)*
Expand Down Expand Up @@ -693,6 +703,7 @@ impl TryToTokens for ast::Export {
all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))),
export_name = #export_name,
)]
#[coverage(off)]
pub unsafe extern "C" fn #generated_name(#(#args),*) -> #wasm_bindgen::convert::WasmRet<#projection::Abi> {
#start_check

Expand Down Expand Up @@ -835,6 +846,7 @@ impl ToTokens for ast::ImportType {
use #wasm_bindgen::__rt::core;

impl WasmDescribe for #rust_name {
#[coverage(off)]
fn describe() {
#description
}
Expand Down Expand Up @@ -943,9 +955,11 @@ impl ToTokens for ast::ImportType {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
extern "C" {
#[coverage(off)]
fn #instanceof_shim(val: u32) -> u32;
}
#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
#[coverage(off)]
unsafe fn #instanceof_shim(_: u32) -> u32 {
panic!("cannot check instanceof on non-wasm targets");
}
Expand Down Expand Up @@ -1082,6 +1096,7 @@ impl ToTokens for ast::ImportEnum {
// It should really be using &str for all of these, but that requires some major changes to cli-support
#[automatically_derived]
impl #wasm_bindgen::describe::WasmDescribe for #name {
#[coverage(off)]
fn describe() {
<#wasm_bindgen::JsValue as #wasm_bindgen::describe::WasmDescribe>::describe()
}
Expand All @@ -1101,6 +1116,7 @@ impl ToTokens for ast::ImportEnum {
impl #wasm_bindgen::convert::FromWasmAbi for #name {
type Abi = <#wasm_bindgen::JsValue as #wasm_bindgen::convert::FromWasmAbi>::Abi;

#[coverage(off)]
unsafe fn from_abi(js: Self::Abi) -> Self {
let s = <#wasm_bindgen::JsValue as #wasm_bindgen::convert::FromWasmAbi>::from_abi(js);
#name::from_js_value(&s).unwrap_or(#name::__Nonexhaustive)
Expand Down Expand Up @@ -1419,6 +1435,7 @@ impl ToTokens for ast::Enum {
impl #wasm_bindgen::convert::FromWasmAbi for #enum_name {
type Abi = u32;

#[coverage(off)]
#[inline]
unsafe fn from_abi(js: u32) -> Self {
#(#cast_clauses else)* {
Expand All @@ -1441,6 +1458,7 @@ impl ToTokens for ast::Enum {

#[automatically_derived]
impl #wasm_bindgen::describe::WasmDescribe for #enum_name {
#[coverage(off)]
fn describe() {
use #wasm_bindgen::describe::*;
inform(ENUM);
Expand Down Expand Up @@ -1477,6 +1495,7 @@ impl ToTokens for ast::Enum {
}

impl #wasm_bindgen::describe::WasmDescribeVector for #enum_name {
#[coverage(off)]
fn describe_vector() {
use #wasm_bindgen::describe::*;
inform(VECTOR);
Expand Down Expand Up @@ -1532,10 +1551,12 @@ impl ToTokens for ast::ImportStatic {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
extern "C" {
#[coverage(off)]
fn #shim_name() -> #abi_ret;
}

#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
#[coverage(off)]
unsafe fn #shim_name() -> #abi_ret {
panic!("cannot access imported statics on non-wasm targets")
}
Expand Down Expand Up @@ -1606,6 +1627,7 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
#(#attrs)*
#[no_mangle]
#[doc(hidden)]
#[coverage(off)]
pub extern "C" fn #name() {
use #wasm_bindgen::describe::*;
// See definition of `link_mem_intrinsics` for what this is doing
Expand All @@ -1630,10 +1652,12 @@ fn extern_fn(
#(#attrs)*
#[link(wasm_import_module = "__wbindgen_placeholder__")]
extern "C" {
#[coverage(off)]
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
}

#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
#[coverage(off)]
unsafe fn #import_name(#(#abi_arguments),*) -> #abi_ret {
#(
drop(#abi_argument_names);
Expand Down
5 changes: 5 additions & 0 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-macro/0.2")]
#![feature(allow_internal_unstable)]
#![allow(internal_features)]

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_attribute]
#[allow_internal_unstable(coverage_attribute)]
pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
match wasm_bindgen_macro_support::expand(attr.into(), input.into()) {
Ok(tokens) => {
Expand All @@ -32,6 +35,7 @@ pub fn wasm_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
/// let worker = Worker::new(&wasm_bindgen::link_to!(module = "/src/worker.js"));
/// ```
#[proc_macro]
#[allow_internal_unstable(coverage_attribute)]
pub fn link_to(input: TokenStream) -> TokenStream {
match wasm_bindgen_macro_support::expand_link_to(input.into()) {
Ok(tokens) => {
Expand All @@ -48,6 +52,7 @@ pub fn link_to(input: TokenStream) -> TokenStream {
}

#[proc_macro_attribute]
#[allow_internal_unstable(coverage_attribute)]
pub fn __wasm_bindgen_class_marker(attr: TokenStream, input: TokenStream) -> TokenStream {
match wasm_bindgen_macro_support::expand_class_marker(attr.into(), input.into()) {
Ok(tokens) => {
Expand Down
5 changes: 5 additions & 0 deletions crates/test-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! See the README for `wasm-bindgen-test` for a bit more info about what's
//! going on here.

#![feature(allow_internal_unstable)]
#![allow(internal_features)]

extern crate proc_macro;

use proc_macro2::*;
Expand All @@ -12,6 +15,7 @@ use std::sync::atomic::*;
static CNT: AtomicUsize = AtomicUsize::new(0);

#[proc_macro_attribute]
#[allow_internal_unstable(coverage_attribute)]
pub fn wasm_bindgen_test(
attr: proc_macro::TokenStream,
body: proc_macro::TokenStream,
Expand Down Expand Up @@ -102,6 +106,7 @@ pub fn wasm_bindgen_test(
tokens.extend(
quote! {
#[no_mangle]
#[coverage(off)]
pub extern "C" fn #name(cx: &#wasm_bindgen_path::__rt::Context) {
let test_name = ::core::concat!(::core::module_path!(), "::", ::core::stringify!(#ident));
#test_body
Expand Down
23 changes: 1 addition & 22 deletions crates/wasm-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ impl Frame<'_> {
match instr {
Instr::Const(c) => match c.value {
Value::I32(n) => stack.push(n),
Value::I64(_) if coverage => stack.push(0),
_ => panic!("non-i32 constant. If you're compiling with profiling information you might want to set WASM_BINDGEN_UNSTABLE_TEST_COVERAGE (--coverage flag in wasm-pack)"),
},
Instr::LocalGet(e) => stack.push(self.locals.get(&e.local).cloned().unwrap_or(0)),
Expand All @@ -295,13 +294,6 @@ impl Frame<'_> {
// Support simple arithmetic, mainly for the stack pointer
// manipulation
Instr::Binop(e) => {
if let BinaryOp::I64Add = e.op {
if coverage {
stack.pop();
// The other one is 0 anyway
return;
}
}
let rhs = stack.pop().unwrap();
let lhs = stack.pop().unwrap();
stack.push(match e.op {
Expand All @@ -315,25 +307,12 @@ impl Frame<'_> {
// mode where there's some traffic on the linear stack even when in
// theory there doesn't need to be.
Instr::Load(e) => {
if let LoadKind::I64 { .. } = e.kind {
if coverage {
stack.push(0);
return;
}
}
let address = stack.pop().unwrap();
let address = address as u32 + e.arg.offset;
assert!(address % 4 == 0);
stack.push(self.interp.mem[address as usize / 4])
}
Instr::Store(e) => {
if let StoreKind::I64 { .. } = e.kind {
if coverage {
stack.pop(); // value
stack.pop(); // address
return;
}
}
let value = stack.pop().unwrap();
let address = stack.pop().unwrap();
let address = address as u32 + e.arg.offset;
Expand Down Expand Up @@ -386,7 +365,7 @@ impl Frame<'_> {
.get(e.func)
.name
.as_ref()
.is_some_and(|n| n.starts_with("__wasm_call_ctors"))
.is_some_and(|n| n.starts_with("__wasm_call_ctors") | n.starts_with("__llvm_profile_register_functions"))
{
return;
}
Expand Down
14 changes: 14 additions & 0 deletions src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ where
// See crates/cli-support/src/js/closures.rs for a more information
// about what's going on here.

#[coverage(off)]
extern "C" fn describe<T: WasmClosure + ?Sized>() {
inform(CLOSURE);
T::describe()
Expand Down Expand Up @@ -465,6 +466,7 @@ impl<T> WasmDescribe for Closure<T>
where
T: WasmClosure + ?Sized,
{
#[coverage(off)]
fn describe() {
inform(EXTERNREF);
}
Expand Down Expand Up @@ -565,8 +567,10 @@ macro_rules! doit {
where $($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static,
{
#[coverage(off)]
fn describe() {
#[allow(non_snake_case)]
#[coverage(off)]
unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
Expand Down Expand Up @@ -596,6 +600,7 @@ macro_rules! doit {

inform(invoke::<$($var,)* R> as u32);

#[coverage(off)]
unsafe extern fn destroy<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
Expand All @@ -622,8 +627,10 @@ macro_rules! doit {
where $($var: FromWasmAbi + 'static,)*
R: ReturnWasmAbi + 'static,
{
#[coverage(off)]
fn describe() {
#[allow(non_snake_case)]
#[coverage(off)]
unsafe extern "C" fn invoke<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
Expand Down Expand Up @@ -654,6 +661,7 @@ macro_rules! doit {

inform(invoke::<$($var,)* R> as u32);

#[coverage(off)]
unsafe extern fn destroy<$($var: FromWasmAbi,)* R: ReturnWasmAbi>(
a: usize,
b: usize,
Expand Down Expand Up @@ -763,8 +771,10 @@ where
A: RefFromWasmAbi,
R: ReturnWasmAbi + 'static,
{
#[coverage(off)]
fn describe() {
#[allow(non_snake_case)]
#[coverage(off)]
unsafe extern "C" fn invoke<A: RefFromWasmAbi, R: ReturnWasmAbi>(
a: usize,
b: usize,
Expand All @@ -789,6 +799,7 @@ where

inform(invoke::<A, R> as u32);

#[coverage(off)]
unsafe extern "C" fn destroy<A: RefFromWasmAbi, R: ReturnWasmAbi>(a: usize, b: usize) {
// See `Fn()` above for why we simply return
if a == 0 {
Expand All @@ -809,8 +820,10 @@ where
A: RefFromWasmAbi,
R: ReturnWasmAbi + 'static,
{
#[coverage(off)]
fn describe() {
#[allow(non_snake_case)]
#[coverage(off)]
unsafe extern "C" fn invoke<A: RefFromWasmAbi, R: ReturnWasmAbi>(
a: usize,
b: usize,
Expand All @@ -836,6 +849,7 @@ where

inform(invoke::<A, R> as u32);

#[coverage(off)]
unsafe extern "C" fn destroy<A: RefFromWasmAbi, R: ReturnWasmAbi>(a: usize, b: usize) {
// See `Fn()` above for why we simply return
if a == 0 {
Expand Down
Loading

0 comments on commit a81f944

Please sign in to comment.