This commit is contained in:
platane
2025-03-22 15:58:46 +01:00
parent 83033510f0
commit 3db2b4069e
9 changed files with 121 additions and 2 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ build
.env
.wrangler
.dev.vars
target

View File

@@ -18,5 +18,8 @@
"dev:demo": "( cd packages/demo ; npm run dev )",
"build:demo": "( cd packages/demo ; npm run build )",
"build:action": "( cd packages/action ; npm run build )"
}
},
"trustedDependencies": [
"wasm-pack"
]
}

View File

@@ -5,5 +5,6 @@
"outside",
"getPathToPose",
"getPathTo",
"svg"
"svg",
"rust"
]

View File

@@ -0,0 +1,6 @@
import "./menu";
(async () => {
const api = await import("@snk/solver-r");
console.log(api);
})();

View File

@@ -42,6 +42,9 @@ const webpackConfiguration: WebpackConfiguration = {
path: path.join(__dirname, "dist"),
filename: "[contenthash].js",
},
experiments: {
asyncWebAssembly: true,
},
module: {
rules: [
{

2
packages/solver-r/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
target
pkg

View File

@@ -0,0 +1,23 @@
[package]
name = "snk-solver-rust"
version = "1.0.0"
authors = ["platane"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.100"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"

View File

@@ -0,0 +1,11 @@
{
"name": "@snk/solver-r",
"version": "1.0.0",
"devDependencies": {
"wasm-pack": "0.13.1"
},
"main": "./pkg/snk_solver_rust.js",
"scripts": {
"build": "wasm-pack build"
}
}

View File

@@ -0,0 +1,69 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, wasm-game-of-life!");
}
#[derive(Copy, Clone)]
pub struct Point {
x: i8,
y: i8,
}
#[wasm_bindgen]
#[derive(Copy, Clone)]
pub enum Cell {
Empty,
Color1,
Color2,
Color3,
Color4,
Color5,
}
#[wasm_bindgen]
pub struct Grid {
width: i8,
height: i8,
cells: Vec<Cell>,
}
#[wasm_bindgen]
impl Grid {
pub fn create(width: i8, height: i8) -> Grid {
let cells = (0..width * height).map(|_| Cell::Empty).collect();
Grid {
width,
height,
cells,
}
}
}
type Snake = [Point; 5];
pub fn get_index(grid: &Grid, x: i8, y: i8) -> usize {
return (x * grid.height + y) as usize;
}
// pub fn setCell(grid:&Grid,x:i8,y:i8,c:Cell) {
// // grid.data[getIndex(grid,x,y)]=c;
// }
pub fn get_cell(grid: &Grid, p: &Point) -> Cell {
let i = get_index(grid, p.x, p.y);
return grid.cells[i];
}
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}