Skip to content

Commit

Permalink
lib(rust): Use packed(u64) tuple when +multivalue feature not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSensor committed Sep 11, 2023
1 parent 4636bf0 commit 8faa43f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 13 additions & 1 deletion libs/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@ mod host {
}

pub mod num {
use crate::types::{number::Type, JSNumber, Null, Number};
use crate::types::{ffi::CTuple, number::Type, JSNumber, Null, Number};
use core::ffi::c_void;
pub type Setter = extern "C" fn(Number, JSNumber);
pub type Getter = extern "C" fn(Number) -> JSNumber;

#[link(wasm_import_module = "nusa")]
#[allow(improper_ctypes)]
extern "C" {
#[cfg(target_feature = "multivalue")]
#[link_name = "num.allocate"]
pub fn allocate(ty: Type, len: u16, nullable: bool) -> (Number, Null);

#[cfg(not(target_feature = "multivalue"))]
#[link_name = "num.cABIallocate"]
pub fn allocate(ty: Type, len: u16, nullable: bool) -> CTuple<Number, Null>;

#[cfg(target_feature = "multivalue")]
#[link_name = "num.accessor"]
pub fn accessor(ty: Type) -> (Getter, Setter);

#[cfg(not(target_feature = "multivalue"))]
#[link_name = "num.cABIaccessor"]
pub fn accessor(ty: Type) -> CTuple<*const c_void, *const c_void>;
}

#[link(wasm_import_module = "nusa")]
Expand Down
8 changes: 4 additions & 4 deletions libs/rust/src/number.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{host, types, Accessor, Series};
use core::primitive;
use core::{intrinsics::transmute, primitive};
use types::number::{JSNumber, Type};

macro_rules! bridge {
Expand All @@ -19,7 +19,7 @@ macro_rules! bridge {
}

fn allocate(len: host::Size) -> (usize, usize) {
let (number, null) = unsafe { host::num::allocate(Type::$Ty, len, false) };
let (number, null) = unsafe { host::num::allocate(Type::$Ty, len, false).into() };
(number.addr, null.addr)
}

Expand All @@ -33,8 +33,8 @@ macro_rules! bridge {
pub fn new() -> Self {
let len = unsafe { host::scope::size() };
let (addr, _) = Self::allocate(len);
let (getter, setter) = unsafe { host::num::accessor(Type::$Ty) };
let accr = (getter, setter);
let (getter, setter) = unsafe { host::num::accessor(Type::$Ty).into() };
let accr = unsafe { (transmute(getter), transmute(setter)) };
$ty { len, addr, accr }
}
}
Expand Down

0 comments on commit 8faa43f

Please sign in to comment.