Skip to content

Commit

Permalink
wasm_bind_gen
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 22, 2023
1 parent ecfb018 commit 3f7860e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ jobs:
- run: npm ci
- run: npm test
- run: npm run json

test-wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: taiki-e/install-action@wasm-pack
- run: wasm-pack test --node --release
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ repository = "https://github.com/functionalscript/nanvm"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
[dev-dependencies]
wasm-bindgen-test = "0.3.38"
8 changes: 8 additions & 0 deletions src/bit_subset64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ impl BitSubset64 {

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;

use crate::const_assert::const_assert;

use super::BitSubset64;
Expand All @@ -110,6 +112,7 @@ mod test {
const _: () = const_assert(AS.1.superposition() == 0);

#[test]
#[wasm_bindgen_test]
fn test_a() {
assert_eq!(A.superposition(), 0b001);
assert_eq!(A.tag, 0b010);
Expand All @@ -131,19 +134,22 @@ mod test {
const _: () = const_assert(_UBCS.1.tag == 0b001100);

#[test]
#[wasm_bindgen_test]
fn test_ubc() {
assert_eq!(UBC.superposition(), 0b011011);
assert_eq!(UBC.tag, 0b000100);
assert_eq!(UBC.union(), 0b011111);
}

#[test]
#[wasm_bindgen_test]
#[should_panic]
fn test_ibc() {
B.and(C);
}

#[test]
#[wasm_bindgen_test]
#[should_panic]
fn test_split_fail() {
UBC.split(0b100);
Expand All @@ -161,13 +167,15 @@ mod test {
const _: () = const_assert(IDE.union() == 0b00111);

#[test]
#[wasm_bindgen_test]
fn test_ude() {
assert_eq!(UDE.superposition(), 0b01011);
assert_eq!(UDE.tag, 0b00100);
assert_eq!(UDE.union(), 0b01111);
}

#[test]
#[wasm_bindgen_test]
fn test_ide() {
assert_eq!(IDE.superposition(), 0b00001);
assert_eq!(IDE.tag, 0b00110);
Expand Down
49 changes: 34 additions & 15 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,39 @@ const fn compatible(t: usize, i: Layout) {
}

struct ContainableLayout {
align: usize,
size: usize,
align: usize,
item_size: usize,
}

impl ContainableLayout {
const fn layout(&self, size: usize) -> Layout {
unsafe { Layout::from_size_align_unchecked(self.size + self.item_size * size, self.align) }
unsafe {
Layout::from_size_align_unchecked(
self.size + self.item_size * size,
self.align,
)
}
}
}

const fn max(a: usize, b: usize) -> usize {
if a > b {
a
} else {
b
}
}

const fn layout<T: Containable>() -> ContainableLayout {
const_assert(true);
let t = Layout::new::<Container<T>>();
let i = Layout::new::<T::Item>();
let align = t.align();
let size = t.size();
compatible(align, i);
compatible(size, i);
let i_align = align_of::<T::Item>();
let c = Layout::new::<Container<T>>();
let align = max(c.align(), i_align);
let size = (c.size() + i_align - 1) / i_align * i_align;
ContainableLayout {
align,
size,
item_size: i.size(),
item_size: size_of::<T::Item>(),
}
}

Expand Down Expand Up @@ -77,6 +87,8 @@ impl<T: Containable> Container<T> {

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;

struct DebugClean(*mut usize);
Expand All @@ -90,10 +102,11 @@ mod test {
}

impl Containable for DebugClean {
type Item = usize;
type Item = u8;
}

#[test]
#[wasm_bindgen_test]
fn test() {
unsafe {
let p = Container::<DebugClean>::alloc(0);
Expand All @@ -106,19 +119,25 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
fn test_layout() {
let x = Container::<DebugClean>::LAYOUT.layout(10);
let cl = Container::<DebugClean>::LAYOUT;
let x = cl.layout(9);
let r = Layout::new::<Container<DebugClean>>()
.extend(Layout::array::<usize>(10).unwrap())
.extend(Layout::array::<u8>(9).unwrap())
.unwrap();
assert_eq!(r.0, x);
let rt =
Layout::from_size_align(cl.size + cl.item_size * 9, cl.align).unwrap();
assert_eq!(x, rt);
}

#[test]
#[wasm_bindgen_test]
fn test2() {
unsafe {
let p = Container::<DebugClean>::alloc(10);
assert_eq!((*p).size, 10);
let p = Container::<DebugClean>::alloc(9);
assert_eq!((*p).size, 9);
let mut i = 0;
(*p).value.0 = &mut i;
Container::update::<true>(p);
Expand Down
6 changes: 6 additions & 0 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ pub const fn check(v: u64) {

#[cfg(test)]
mod test {
use wasm_bindgen_test::wasm_bindgen_test;

use super::*;

#[test]
#[wasm_bindgen_test]
fn test_nan() {
assert_eq!(f64::INFINITY.to_bits(), INFINITY);
assert_ne!(f64::NAN, f64::NAN);
Expand All @@ -28,6 +31,7 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
fn test_check() {
check(0);
check(1);
Expand All @@ -38,12 +42,14 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
#[should_panic]
fn test_nan_panic() {
check(0x7FF0_00F0_0500_0001);
}

#[test]
#[wasm_bindgen_test]
#[should_panic]
fn test_nan_panic2() {
check(0xFFFA_FF96_5534_5781);
Expand Down
8 changes: 8 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ impl Value {
mod test {
use std::rc::Rc;

use wasm_bindgen_test::wasm_bindgen_test;

use super::*;
use crate::{const_assert::const_assert, number::NAN};

Expand All @@ -147,6 +149,7 @@ mod test {
const _: () = const_assert(BOOL.has(EXTENSION.mask));

#[test]
#[wasm_bindgen_test]
fn test_unsized() {
let _x: Rc<[u8]> = Rc::new([1, 3]);
// let _y: Rc<(u8, [u8])> = Rc::new((5, [1, 3]));
Expand All @@ -156,6 +159,7 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
fn test_number() {
assert_eq!(Value::from_number(1.0).get_number(), Some(1.0));
assert_eq!(Value::from_number(-1.0).get_number(), Some(-1.0));
Expand All @@ -174,6 +178,7 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
fn test_bool() {
assert_eq!(Value::from_bool(true).get_bool(), Some(true));
assert_eq!(Value::from_bool(false).get_bool(), Some(false));
Expand All @@ -183,6 +188,7 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
fn test_null() {
assert!(Value::null().is_null());
//
Expand All @@ -191,11 +197,13 @@ mod test {
}

#[test]
#[wasm_bindgen_test]
fn test_object() {
assert!(Value::null().is_object());
}

#[test]
#[wasm_bindgen_test]
fn test_type() {
assert_eq!(Value::from_number(15.0).get_type(), ValueType::Number);
assert_eq!(Value::from_bool(true).get_type(), ValueType::Bool);
Expand Down

0 comments on commit 3f7860e

Please sign in to comment.