From d456a18c3a373d269c730e73a7c15a09b1391340 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 27 Jul 2023 15:35:20 -0400 Subject: [PATCH] feat: try to implement first FFI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Running into challenges. The Rust part compiles, but when building with Webpack, I get this error: ``` ERROR in ./pkg/index_bg.wasm Import "__wbindgen_bigint_from_u64" from "./index_bg.js" with Non-JS-compatible Func Sigurature (i64 as parameter) can only be used for direct wasm to wasm dependencies @ ./pkg/index.js @ ./js/index.js ℹ 「wdm」: Failed to compile. ``` This issue (https://github.com/rustwasm/wasm-bindgen/issues/3095) talks about the same problem. The fix (https://github.com/webpack/webpack/pull/16339) was merged but not backported into Webpack 4, so I'm going to have to try and upgrade to fix. --- Cargo.lock | 33 +++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ js/index.js | 6 ++++-- src/defs.rs | 13 ++++++++++--- src/helpers.rs | 11 +++++++++++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efd62a5..5d5912d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -131,6 +131,8 @@ dependencies = [ "futures", "js-sys", "ndarray", + "serde", + "serde-wasm-bindgen", "wasm-bindgen", "wasm-bindgen-futures 0.3.27", "wasm-bindgen-test", @@ -158,6 +160,37 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" +[[package]] +name = "serde" +version = "1.0.177" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63ba2516aa6bf82e0b19ca8b50019d52df58455d3cf9bdaf6315225fdd0c560a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde-wasm-bindgen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + +[[package]] +name = "serde_derive" +version = "1.0.177" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "401797fe7833d72109fedec6bfcbe67c0eed9b99772f26eb8afd261f0abc6fd3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "2.0.27" diff --git a/Cargo.toml b/Cargo.toml index 7e97d09..fdf4341 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ default = ["console_error_panic_hook"] ndarray = "0.15.6" wasm-bindgen ="0.2.84" console_error_panic_hook = { version = "0.1.7", optional = true} +serde = { version = "1.0", features = ["derive"]} +serde-wasm-bindgen = "0.4" [dependencies.web-sys] version = "0.3.22" diff --git a/js/index.js b/js/index.js index f81cc30..d94c2ba 100644 --- a/js/index.js +++ b/js/index.js @@ -22,6 +22,10 @@ function fill_grid_square(x, y, z){ } +function test_get_points_for_orientation(){ + +} + function setupGeometry() { const xy_plane = new THREE.GridHelper(7, 6); xy_plane.rotateX(THREE.MathUtils.DEG2RAD * 90); @@ -39,8 +43,6 @@ function setupGeometry() { scene.add(yz_plane); - - const geometry = new THREE.SphereGeometry(0.5); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const sphere = new THREE.Mesh(geometry, material); diff --git a/src/defs.rs b/src/defs.rs index 9f28863..6151c31 100644 --- a/src/defs.rs +++ b/src/defs.rs @@ -1,3 +1,6 @@ +use serde::{Deserialize, Serialize}; +use wasm_bindgen::prelude::*; + pub const BOARD_SIZE: usize = 6; pub const PIECE_COUNT: usize = 54; pub const THREAD_COUNT: usize = 8; @@ -5,19 +8,23 @@ pub const THREAD_COUNT: usize = 8; pub type Solution = Vec; pub type Board = ndarray::Array3; -#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] +#[wasm_bindgen] pub struct Point { pub x: usize, pub y: usize, pub z: usize, } -#[derive(Debug, PartialEq, Eq, Clone, Copy)] + +#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub struct Position { pub center: Point, pub orientation: Orientation, } -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[wasm_bindgen] +#[repr(u8)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub enum Orientation { // viewed from the top FlatUp, // ┴ diff --git a/src/helpers.rs b/src/helpers.rs index 3b45eae..c05a000 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,3 +1,6 @@ +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; +use web_sys::console; + use crate::defs::*; // Checks to see if a given point and orientation overhangs an empty point. If so, we don't want to place a piece there. @@ -93,6 +96,14 @@ pub fn inbounds_and_clear(board: &Board, point: &Point) -> bool { point.x < BOARD_SIZE && point.y < BOARD_SIZE && point.z < BOARD_SIZE && unsafe { !board.uget([point.x, point.y, point.z]) } } +#[wasm_bindgen] +pub fn wasm_get_points_for_orientation(point: Point, orientation: Orientation) -> JsValue { + console::log_1(&JsValue::from_str("this is from the wasm call")); + let result_points = get_points_for_orientation(&point, &orientation); + + serde_wasm_bindgen::to_value(&result_points).unwrap() +} + pub fn get_points_for_orientation(point: &Point, orientation: &Orientation) -> [Point; 4] { let mut points: [Point; 4] = [*point; 4];