From 25354fb8c957537654920de75c30fdcac5a65adb Mon Sep 17 00:00:00 2001 From: Teymour Aldridge Date: Sat, 6 Jun 2020 07:23:51 +0100 Subject: [PATCH 1/4] Implement `IntoWasmAbi` for `Option<&Closure>. --- src/convert/closures.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/convert/closures.rs b/src/convert/closures.rs index 3887e46100a..f3b09e389b5 100644 --- a/src/convert/closures.rs +++ b/src/convert/closures.rs @@ -136,6 +136,28 @@ where } } +impl<'a, 'b, A, R> IntoWasmAbi for Option<&'a (dyn Fn(&A) -> R + 'b)> +where + A: RefFromWasmAbi, + R: ReturnWasmAbi, +{ + type Abi = WasmSlice; + fn into_abi(self) -> Self::Abi { + unsafe { + match self { + Some(some_closure) => { + let (a, b): (usize, usize) = mem::transmute(some_closure); + WasmSlice { + ptr: a as u32, + len: b as u32, + } + } + None => WasmSlice { ptr: 0, len: 0 }, + } + } + } +} + #[allow(non_snake_case)] unsafe extern "C" fn invoke1_ref( a: usize, From 9d9f9d6ffa94dfba525bf6a1cdffac1cc2875e7e Mon Sep 17 00:00:00 2001 From: Teymour Aldridge Date: Sat, 6 Jun 2020 07:25:50 +0100 Subject: [PATCH 2/4] Move `unsafe` block to encapsulate less code. --- src/convert/closures.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/convert/closures.rs b/src/convert/closures.rs index f3b09e389b5..8643ccf54fa 100644 --- a/src/convert/closures.rs +++ b/src/convert/closures.rs @@ -143,17 +143,15 @@ where { type Abi = WasmSlice; fn into_abi(self) -> Self::Abi { - unsafe { - match self { - Some(some_closure) => { - let (a, b): (usize, usize) = mem::transmute(some_closure); - WasmSlice { - ptr: a as u32, - len: b as u32, - } + match self { + Some(some_closure) => unsafe { + let (a, b): (usize, usize) = mem::transmute(some_closure); + WasmSlice { + ptr: a as u32, + len: b as u32, } - None => WasmSlice { ptr: 0, len: 0 }, - } + }, + None => WasmSlice { ptr: 0, len: 0 }, } } } From 968ba4e15c9893615229937a50370d6b51d68e04 Mon Sep 17 00:00:00 2001 From: Teymour Aldridge Date: Sat, 6 Jun 2020 07:29:43 +0100 Subject: [PATCH 3/4] Implement the correct trait. --- src/convert/closures.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/convert/closures.rs b/src/convert/closures.rs index 8643ccf54fa..da0fc3be2d3 100644 --- a/src/convert/closures.rs +++ b/src/convert/closures.rs @@ -2,7 +2,7 @@ use core::mem; use crate::convert::slices::WasmSlice; use crate::convert::RefFromWasmAbi; -use crate::convert::{FromWasmAbi, IntoWasmAbi, ReturnWasmAbi}; +use crate::convert::{FromWasmAbi, IntoWasmAbi, OptionIntoWasmAbi, ReturnWasmAbi}; use crate::describe::{inform, WasmDescribe, FUNCTION}; use crate::throw_str; @@ -136,23 +136,14 @@ where } } -impl<'a, 'b, A, R> IntoWasmAbi for Option<&'a (dyn Fn(&A) -> R + 'b)> +impl<'a, 'b, A, R> OptionIntoWasmAbi for Option<&'a (dyn Fn(&A) -> R + 'b)> where A: RefFromWasmAbi, R: ReturnWasmAbi, { type Abi = WasmSlice; - fn into_abi(self) -> Self::Abi { - match self { - Some(some_closure) => unsafe { - let (a, b): (usize, usize) = mem::transmute(some_closure); - WasmSlice { - ptr: a as u32, - len: b as u32, - } - }, - None => WasmSlice { ptr: 0, len: 0 }, - } + fn none() -> Self::Abi { + WasmSlice { ptr: 0, len: 0 } } } From a9ff155a601ebab4ac5640cec612f9bc9839a24f Mon Sep 17 00:00:00 2001 From: Teymour Aldridge Date: Sat, 6 Jun 2020 07:33:11 +0100 Subject: [PATCH 4/4] Fix implementation. --- src/convert/closures.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/convert/closures.rs b/src/convert/closures.rs index da0fc3be2d3..0ac760b8c86 100644 --- a/src/convert/closures.rs +++ b/src/convert/closures.rs @@ -136,12 +136,11 @@ where } } -impl<'a, 'b, A, R> OptionIntoWasmAbi for Option<&'a (dyn Fn(&A) -> R + 'b)> +impl<'a, 'b, A, R> OptionIntoWasmAbi for &'a (dyn Fn(&A) -> R + 'b) where A: RefFromWasmAbi, R: ReturnWasmAbi, { - type Abi = WasmSlice; fn none() -> Self::Abi { WasmSlice { ptr: 0, len: 0 } }