Skip to content

Commit

Permalink
Merge pull request #13 from ZakFarmer/feature/wasm
Browse files Browse the repository at this point in the history
Feature/wasm
  • Loading branch information
ZakFarmer authored Oct 26, 2023
2 parents 1b05599 + 3214910 commit e770a5f
Show file tree
Hide file tree
Showing 10 changed files with 7,718 additions and 4 deletions.
218 changes: 215 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = [ "opcode", "compiler", "lexer", "parser", "evaluator", "object", "interpreter", "vm"]
members = [ "opcode", "compiler", "lexer", "parser", "evaluator", "object", "interpreter", "vm", "wasm"]
3 changes: 3 additions & 0 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/dist
/node_modules
/pkg
47 changes: 47 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "wasm"
version = "0.1.0"
description = "WASM implementation of php-rs"
homepage = "https://github.com/ZakFarmer/php-rs"
repository = "https://github.com/ZakFarmer/php-rs"
edition = "2021"
license = "MIT"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
compiler = { path = "../compiler" }
lexer = { path = "../lexer" }
object = { path = "../object" }
parser = { path = "../parser" }
vm = { path = "../vm" }

wasm-bindgen = "0.2.80"

# 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 }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
web-sys = { version = "0.3.64", features = ["console"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.24"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
23 changes: 23 additions & 0 deletions wasm/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import("../pkg/index.js")
.then((wasm) => {
wasm.init_state();

const inputElement = document.getElementById("input");
const outputElement = document.getElementById("output");

outputElement.innerHTML += `> php-rs interpreter 0.1.0\n`;
outputElement.innerHTML += `Loaded WASM bundle.\n\n`;

inputElement.addEventListener("keydown", (event) => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();

const input = inputElement.value;
const output = wasm.parse(input);

outputElement.innerHTML += `> ${input}\n${output}\n`;
inputElement.value = ""; // Clear the input
}
});
})
.catch(console.error);
Loading

0 comments on commit e770a5f

Please sign in to comment.