name: WebAssembly CI

on: [push, pull_request]

permissions: {}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

env:
  SPEC_SPLIT_DOTS: 160

jobs:
  wasm32-test:
    runs-on: ubuntu-24.04
    container: crystallang/crystal:1.19.1-build
    steps:
      - name: Download Crystal source
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Install wasmtime
        uses: mwilliamson/setup-wasmtime-action@bf814d7d8fc3c3a77dfe114bd9fb8a2c575f6ad6 #v2
        with:
          wasmtime-version: "2.0.0"

      - name: Install LLVM
        run: |
          apt-get update
          apt-get remove -y 'llvm-*' 'libllvm*'
          apt-get install -y curl software-properties-common
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          apt-add-repository -y deb http://apt.llvm.org/noble/ llvm-toolchain-noble-18 main
          apt-get install -y llvm-18-dev lld-18
          ln -s "$(which wasm-ld-18)" /usr/bin/wasm-ld

      - name: Download wasm32 libs
        run: |
          mkdir wasm32-wasi-libs
          curl -LO https://github.com/lbguilherme/wasm-libs/releases/download/0.0.3/wasm32-wasi-libs.tar.gz
          echo "cd36f319f8f9f9cd08f723d10e6ec2b92f2e44d3ce3b20344b8041386d85c261  wasm32-wasi-libs.tar.gz" | sha256sum -c -
          tar -f wasm32-wasi-libs.tar.gz -C wasm32-wasi-libs -xz
          rm wasm32-wasi-libs.tar.gz

      - name: Build spec/wasm32_std_spec.cr
        run: bin/crystal build spec/wasm32_std_spec.cr -o wasm32_std_spec.wasm --target wasm32-wasi -Dwithout_iconv -Dwithout_openssl
        env:
          CRYSTAL_LIBRARY_PATH: ${{ github.workspace }}/wasm32-wasi-libs

      - name: Run wasm32_std_spec.wasm
        run: |
          wasmtime run wasm32_std_spec.wasm
