diff --git a/Formula/w/wasm-bindgen.rb b/Formula/w/wasm-bindgen.rb new file mode 100644 index 000000000000..af942c0e8be8 --- /dev/null +++ b/Formula/w/wasm-bindgen.rb @@ -0,0 +1,89 @@ +class WasmBindgen < Formula + desc "Facilitating high-level interactions between Wasm modules and JavaScript" + homepage "https://wasm-bindgen.github.io/wasm-bindgen/" + url "https://github.com/wasm-bindgen/wasm-bindgen/archive/refs/tags/0.2.106.tar.gz" + sha256 "596b8014ba94cb9229fe15fc622078c02986ea5ec5b4e018f3116ad77cb63a85" + license any_of: ["Apache-2.0", "MIT"] + + bottle do + sha256 cellar: :any_skip_relocation, arm64_tahoe: "ce0138f72e4c65eef5878783008916b13c03b74428c9d9daa6420e46c111140c" + sha256 cellar: :any_skip_relocation, arm64_sequoia: "e33000f10e77d8b28bbf62ab4929e4961b88c0c77eb81057a9b7535198bab409" + sha256 cellar: :any_skip_relocation, arm64_sonoma: "3d22c972e25ab976c789573f14ff6879d5625c680338d388c3503462679776ba" + sha256 cellar: :any_skip_relocation, sonoma: "ec411ae0cc7f8247d20a5a550364a3c905bd855da100d8f81c72560a3526ddae" + sha256 cellar: :any_skip_relocation, arm64_linux: "8635674aeef13e03e638fba036156d62db0d60d93a45bff2119adba04549b0f1" + sha256 cellar: :any_skip_relocation, x86_64_linux: "8594ea3a6468de75435121d38d0a72278d1f9554dae4a245b7162be34240dae6" + end + + depends_on "rust" => :build + depends_on "node" => :test + depends_on "rustup" => :test + + def install + system "cargo", "install", *std_cargo_args(path: "crates/cli") + end + + test do + assert_match version.to_s, shell_output("#{bin}/wasm-bindgen --version") + + (testpath/"src/lib.rs").write <<~RUST + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + extern "C" { + fn alert(s: &str); + } + + #[wasm_bindgen] + pub fn greet(name: &str) { + alert(&format!("Hello, {name}!")); + } + RUST + (testpath/"Cargo.toml").write <<~TOML + [package] + authors = ["The wasm-bindgen Developers"] + edition = "2021" + name = "hello_world" + publish = false + version = "0.0.0" + + [lib] + crate-type = ["cdylib"] + + [dependencies] + wasm-bindgen = "#{version}" + TOML + (testpath/"package.json").write <<~JSON + { + "name": "hello_world", + "version": "0.0.0", + "type": "module" + } + JSON + (testpath/"index.js").write <<~JS + globalThis.alert = console.log; + import { greet } from './pkg/hello_world.js'; + + greet('World'); + JS + + # Show that we can use a different toolchain than the one provided by the `rust` formula. + # https://github.com/Homebrew/homebrew-core/pull/134074#pullrequestreview-1484979359 + ENV.prepend_path "PATH", Formula["rustup"].bin + system "rustup", "set", "profile", "minimal" + system "rustup", "default", "stable" + system "rustup", "target", "add", "wasm32-unknown-unknown" + + # Prevent Homebrew/CI AArch64 CPU features from bleeding into wasm32 builds + ENV.delete "RUSTFLAGS" + ENV.delete "CARGO_ENCODED_RUSTFLAGS" + + # Explicitly enable reference-types to resolve "failed to find intrinsics" error + ENV["RUSTFLAGS"] = "-C target-feature=+reference-types" + system "cargo", "build", "--target", "wasm32-unknown-unknown", "--manifest-path", "Cargo.toml" + system bin/"wasm-bindgen", "target/wasm32-unknown-unknown/debug/hello_world.wasm", + "--out-dir", "pkg", "--reference-types" + + output = shell_output("node index.js") + assert_match "Hello, World!", output.strip + end +end