WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 3db2b40

Browse files
committed
wasm
1 parent 8303351 commit 3db2b40

File tree

9 files changed

+121
-2
lines changed

9 files changed

+121
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ build
66
.env
77
.wrangler
88
.dev.vars
9+
target

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"dev:demo": "( cd packages/demo ; npm run dev )",
1919
"build:demo": "( cd packages/demo ; npm run build )",
2020
"build:action": "( cd packages/action ; npm run build )"
21-
}
21+
},
22+
"trustedDependencies": [
23+
"wasm-pack"
24+
]
2225
}

packages/demo/demo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"outside",
66
"getPathToPose",
77
"getPathTo",
8-
"svg"
8+
"svg",
9+
"rust"
910
]

packages/demo/demo.rust.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "./menu";
2+
3+
(async () => {
4+
const api = await import("@snk/solver-r");
5+
console.log(api);
6+
})();

packages/demo/webpack.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const webpackConfiguration: WebpackConfiguration = {
4242
path: path.join(__dirname, "dist"),
4343
filename: "[contenthash].js",
4444
},
45+
experiments: {
46+
asyncWebAssembly: true,
47+
},
4548
module: {
4649
rules: [
4750
{

packages/solver-r/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
pkg

packages/solver-r/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "snk-solver-rust"
3+
version = "1.0.0"
4+
authors = ["platane"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib", "rlib"]
9+
10+
[features]
11+
default = ["console_error_panic_hook"]
12+
13+
[dependencies]
14+
wasm-bindgen = "0.2.100"
15+
16+
# The `console_error_panic_hook` crate provides better debugging of panics by
17+
# logging them with `console.error`. This is great for development, but requires
18+
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
19+
# code size when deploying.
20+
console_error_panic_hook = { version = "0.1.7", optional = true }
21+
22+
[dev-dependencies]
23+
wasm-bindgen-test = "0.3.34"

packages/solver-r/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@snk/solver-r",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"wasm-pack": "0.13.1"
6+
},
7+
"main": "./pkg/snk_solver_rust.js",
8+
"scripts": {
9+
"build": "wasm-pack build"
10+
}
11+
}

packages/solver-r/src/lib.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
extern "C" {
5+
fn alert(s: &str);
6+
}
7+
8+
#[wasm_bindgen]
9+
pub fn greet() {
10+
alert("Hello, wasm-game-of-life!");
11+
}
12+
13+
#[derive(Copy, Clone)]
14+
pub struct Point {
15+
x: i8,
16+
y: i8,
17+
}
18+
19+
#[wasm_bindgen]
20+
#[derive(Copy, Clone)]
21+
pub enum Cell {
22+
Empty,
23+
Color1,
24+
Color2,
25+
Color3,
26+
Color4,
27+
Color5,
28+
}
29+
30+
#[wasm_bindgen]
31+
pub struct Grid {
32+
width: i8,
33+
height: i8,
34+
cells: Vec<Cell>,
35+
}
36+
37+
#[wasm_bindgen]
38+
impl Grid {
39+
pub fn create(width: i8, height: i8) -> Grid {
40+
let cells = (0..width * height).map(|_| Cell::Empty).collect();
41+
42+
Grid {
43+
width,
44+
height,
45+
cells,
46+
}
47+
}
48+
}
49+
50+
type Snake = [Point; 5];
51+
52+
pub fn get_index(grid: &Grid, x: i8, y: i8) -> usize {
53+
return (x * grid.height + y) as usize;
54+
}
55+
56+
// pub fn setCell(grid:&Grid,x:i8,y:i8,c:Cell) {
57+
// // grid.data[getIndex(grid,x,y)]=c;
58+
// }
59+
60+
pub fn get_cell(grid: &Grid, p: &Point) -> Cell {
61+
let i = get_index(grid, p.x, p.y);
62+
63+
return grid.cells[i];
64+
}
65+
66+
#[test]
67+
fn it_works() {
68+
assert_eq!(2 + 2, 4);
69+
}

0 commit comments

Comments
 (0)