Skip to content

Commit

Permalink
feat: try to implement first FFI
Browse files Browse the repository at this point in the history
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 (rustwasm/wasm-bindgen#3095) talks
about the same problem. The fix
(webpack/webpack#16339) was merged but not
backported into Webpack 4, so I'm going to have to try and upgrade to
fix.
  • Loading branch information
jcbhl committed Jul 27, 2023
1 parent 6ba77a0 commit d456a18
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions js/index.js
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions src/defs.rs
@@ -1,23 +1,30 @@
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;

pub type Solution = Vec<Position>;
pub type Board = ndarray::Array3<bool>;

#[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, // ┴
Expand Down
11 changes: 11 additions & 0 deletions 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.
Expand Down Expand Up @@ -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];

Expand Down

0 comments on commit d456a18

Please sign in to comment.