wasm object interaction test

This commit is contained in:
2026-08-20 00:20:02 +02:00
parent 63c19c3baa
commit c6e8849b28
6 changed files with 41 additions and 3 deletions
+2
View File
@@ -0,0 +1,2 @@
((nil
(lsp-rust-features . ["wasm-bindgen"])))
+1
View File
@@ -75,6 +75,7 @@
};
devShells.default = pkgs.mkShell {
RA_LOG = "rust_analyzer=info";
buildInputs = [
rust-toolchain
wasm-bindgen
+18
View File
@@ -1,10 +1,28 @@
use wasm_bindgen::prelude::*;
use crate::{solver, word_list::WordList};
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, wasm-on-web!");
}
#[wasm_bindgen]
struct Solver(solver::Solver);
#[wasm_bindgen]
impl Solver {
pub fn new() -> Solver {
let wl = WordList::from(["test", "yooo"].iter());
Solver(solver::Solver::from_single_word_list(wl))
}
pub fn stats(&self) -> String {
self.0.tabular_format()
}
}
+12
View File
@@ -16,6 +16,18 @@ pub struct WordList {
words: HashSet<String>,
}
impl<I, S> From<I> for WordList
where
I: Iterator<Item = S>,
S: AsRef<str>,
{
fn from(value: I) -> Self {
WordList {
words: value.into_iter().map(|s| s.as_ref().to_owned()).collect(),
}
}
}
impl WordList {
pub fn len(&self) -> usize {
self.words.len()
+3 -1
View File
@@ -5,7 +5,9 @@
<title>WASM Demo</title>
</head>
<body>
<button id="my-button">Do greet</button>
<button id="my-button">Test Solver</button>
<textarea id="out"></textarea>
<script type="module" src="/src/main.ts"></script>
</body>
+5 -2
View File
@@ -1,7 +1,10 @@
import { greet } from "wordle-solver";
import { Solver } from "wordle-solver";
const button = document.getElementById("my-button")!;
const txt = document.getElementById("out")!;
button.addEventListener("click", () => {
greet();
var s = Solver.new();
txt.innerText = s.stats();
s.free();
});