Skip to content

Commit

Permalink
Add ABI support for [JsValue] slices
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Apr 26, 2019
1 parent 6e1aa5a commit d863ebc
Showing 1 changed file with 2 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/convert/slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::str;

use crate::convert::{FromWasmAbi, IntoWasmAbi, RefFromWasmAbi, RefMutFromWasmAbi, WasmAbi};
use crate::convert::{OptionIntoWasmAbi, Stack};
use crate::JsValue;

if_std! {
use core::mem;
Expand Down Expand Up @@ -123,7 +124,7 @@ macro_rules! vectors {
}

vectors! {
u8 i8 u16 i16 u32 i32 u64 i64 usize isize f32 f64
u8 i8 u16 i16 u32 i32 u64 i64 usize isize f32 f64 JsValue
}

if_std! {
Expand Down Expand Up @@ -202,41 +203,3 @@ impl RefFromWasmAbi for str {
str::from_utf8_unchecked(<[u8]>::ref_from_abi(js, extra))
}
}

if_std! {
use crate::JsValue;

impl IntoWasmAbi for Box<[JsValue]> {
type Abi = WasmSlice;

#[inline]
fn into_abi(self, extra: &mut Stack) -> WasmSlice {
let ptr = self.as_ptr();
let len = self.len();
mem::forget(self);
WasmSlice {
ptr: ptr.into_abi(extra),
len: len as u32,
}
}
}

impl OptionIntoWasmAbi for Box<[JsValue]> {
fn none() -> WasmSlice { null_slice() }
}

impl FromWasmAbi for Box<[JsValue]> {
type Abi = WasmSlice;

#[inline]
unsafe fn from_abi(js: WasmSlice, extra: &mut Stack) -> Self {
let ptr = <*mut JsValue>::from_abi(js.ptr, extra);
let len = js.len as usize;
Vec::from_raw_parts(ptr, len, len).into_boxed_slice()
}
}

impl OptionFromWasmAbi for Box<[JsValue]> {
fn is_none(slice: &WasmSlice) -> bool { slice.ptr == 0 }
}
}

0 comments on commit d863ebc

Please sign in to comment.