feat: configure WebAssembly port and Javascript bindings#53
Open
Codekill33 wants to merge 2 commits into
Open
Conversation
Contributor
|
@Codekill33 Please resolve conflict!! |
Author
|
Done! @Mkalbani |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Goal
Configure the quest-rust project to compile to WebAssembly so the game can run in a browser environment without any server-side execution, expanding the reach to players without requiring a local Rust binary.
🛠️ Changes Made
Cargo Configuration: Updated Cargo.toml to support the cdylib target and added a conditional wasm feature flag.
Time API Abstractions: Replaced hardcoded std::time::Instant and SystemTime calls with a new src/time.rs abstraction.
Native builds continue to use the standard library.
WASM builds use web_sys::window().performance().now() and js_sys::Date::now() to avoid standard library panics.
Persistence Layer: Created src/persistence.rs to abstract file operations.
Native builds use std::fs.
WASM builds read from and write to the browser's localStorage via web-sys.
JavaScript API Bindings: Introduced src/wasm.rs that maintains a thread-safe global GameState and exposes a clean API using wasm-bindgen:
startSession(puzzle_id, puzzle_json)
submitAnswer(answer)
revealHint()
getScore()
endSession()
Frontend Demo: Added a minimal web/index.html interface to demonstrate loading the WASM module and driving a complete game session purely in the browser.
CI/CD Integration: Updated .github/workflows/ci.yml to automatically verify both the native cargo build and the wasm-pack build --target web compilation on every push.
✅ Verification
Validated native builds (cargo check and cargo test) to ensure regressions were not introduced for existing targets.
Validated the WebAssembly compilation (cargo check --target wasm32-unknown-unknown --features wasm) ensuring that the feature flags correctly gate incompatible standard library APIs.
🚀 How to Test
Build the WebAssembly package:
bash
wasm-pack build --target web --features wasm
Serve the web/ directory using any local web server (e.g., python -m http.server 8000).
Open http://localhost:8000 in your browser and click "Start Session" to test the integration!
closes #49