wasm
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@ build
|
||||
.env
|
||||
.wrangler
|
||||
.dev.vars
|
||||
target
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -5,5 +5,6 @@
|
||||
"outside",
|
||||
"getPathToPose",
|
||||
"getPathTo",
|
||||
"svg"
|
||||
"svg",
|
||||
"rust"
|
||||
]
|
||||
|
||||
6
packages/demo/demo.rust.ts
Normal file
6
packages/demo/demo.rust.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import "./menu";
|
||||
|
||||
(async () => {
|
||||
const api = await import("@snk/solver-r");
|
||||
console.log(api);
|
||||
})();
|
||||
@@ -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
2
packages/solver-r/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target
|
||||
pkg
|
||||
23
packages/solver-r/Cargo.toml
Normal file
23
packages/solver-r/Cargo.toml
Normal 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"
|
||||
11
packages/solver-r/package.json
Normal file
11
packages/solver-r/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
69
packages/solver-r/src/lib.rs
Normal file
69
packages/solver-r/src/lib.rs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user