Skip to content

Commit

Permalink
feat: add imports, use decorator for exports (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko authored Sep 17, 2024
1 parent c94aaf3 commit b5aad15
Show file tree
Hide file tree
Showing 19 changed files with 866 additions and 104 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "cargo" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
71 changes: 71 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: CI

on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- name: linux
os: ubuntu-latest
# - name: windows
# os: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: default
toolchain: stable
target: wasm32-wasi
default: true

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.x'

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Update deps (Linux)
run: |
go install github.com/extism/cli/extism@c1eb1fc
cd /tmp
# get just wasm-merge and wasm-opt
curl -L https://github.com/WebAssembly/binaryen/releases/download/version_116/binaryen-version_116-x86_64-linux.tar.gz > binaryen.tar.gz
tar xvzf binaryen.tar.gz
sudo cp binaryen-version_116/bin/wasm-merge /usr/local/bin
sudo cp binaryen-version_116/bin/wasm-opt /usr/local/bin
if: runner.os != 'Windows'

- name: Update deps (Windows)
run: |
powershell -executionpolicy bypass -File .\install-wasi-sdk.ps1
go install github.com/extism/cli/extism@c1eb1fc
Remove-Item -Recurse -Path "c:\Program files\Binaryen" -Force -ErrorAction SilentlyContinue > $null 2>&1
New-Item -ItemType Directory -Force -Path "c:\Program files\Binaryen" -ErrorAction Stop > $null 2>&1
Invoke-WebRequest -Uri "https://github.com/WebAssembly/binaryen/releases/download/version_116/binaryen-version_116-x86_64-windows.tar.gz" -OutFile "$env:TMP\binaryen-version_116-x86_64-windows.tar.gz"
7z x "$env:TMP\binaryen-version_116-x86_64-windows.tar.gz" -o"$env:TMP\" >$null 2>&1
7z x -ttar "$env:TMP\binaryen-version_116-x86_64-windows.tar" -o"$env:TMP\" >$null 2>&1
Copy-Item -Path "$env:TMP\binaryen-version_116\bin\wasm-opt.exe" -Destination "c:\Program files\Binaryen" -ErrorAction Stop > $null 2>&1
Copy-Item -Path "$env:TMP\binaryen-version_116\bin\wasm-merge.exe" -Destination "c:\Program files\Binaryen" -ErrorAction Stop > $null 2>&1
if: runner.os == 'Windows'

- name: Run Tests (Linux)
run: |
make
make test
if: runner.os != 'Windows'

- name: Run Tests (Windows)
run: |
set PATH="c:\Program files\Binaryen\";%PATH%
make
make test
shell: cmd
if: runner.os == 'Windows'

160 changes: 160 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
name: Publish

on:
workflow_dispatch:
release:
types:
- published

jobs:
compile_core:
name: Compile Core
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: default
toolchain: stable
target: wasm32-wasi
default: true

- name: Get wasm-opt
run: |
curl -L https://github.com/WebAssembly/binaryen/releases/download/version_116/binaryen-version_116-x86_64-linux.tar.gz > binaryen.tar.gz
tar xvzf binaryen.tar.gz
sudo cp binaryen-version_116/bin/wasm-opt /usr/local/bin
sudo chmod +x /usr/local/bin/wasm-opt
- name: Make core
run: make core

- name: Opt core
run: wasm-opt --enable-reference-types --enable-bulk-memory --strip -O3 lib/target/wasm32-wasi/release/core.wasm -o lib/target/wasm32-wasi/release/core.wasm

- name: Upload core binary to artifacts
uses: actions/upload-artifact@v2
with:
name: engine
path: lib/target/wasm32-wasi/release/core.wasm

compile_cli:
name: Compile CLI
needs: compile_core
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- name: linux
os: ubuntu-latest
path: target/x86_64-unknown-linux-gnu/release/extism-py
asset_name: extism-py-x86_64-linux-${{ github.event.release.tag_name }}
shasum_cmd: sha256sum
target: x86_64-unknown-linux-gnu
- name: linux-arm64
os: ubuntu-latest
path: target/aarch64-unknown-linux-gnu/release/extism-py
asset_name: extism-py-aarch64-linux-${{ github.event.release.tag_name }}
shasum_cmd: sha256sum
target: aarch64-unknown-linux-gnu
- name: macos
os: macos-latest
path: target/x86_64-apple-darwin/release/extism-py
asset_name: extism-py-x86_64-macos-${{ github.event.release.tag_name }}
shasum_cmd: shasum -a 256
target: x86_64-apple-darwin
- name: macos-arm64
os: macos-latest
path: target/aarch64-apple-darwin/release/extism-py
asset_name: extism-py-aarch64-macos-${{ github.event.release.tag_name }}
shasum_cmd: shasum -a 256
target: aarch64-apple-darwin
# - name: windows
# os: windows-latest
# path: target\x86_64-pc-windows-msvc\release\extism-py.exe
# asset_name: extism-py-x86_64-windows-${{ github.event.release.tag_name }}
# target: x86_64-pc-windows-msvc
# - name: windows-arm64
# os: windows-latest
# path: target\aarch64-pc-windows-msvc\release\extism-py.exe
# asset_name: extism-py-aarch64-windows-${{ github.event.release.tag_name }}
# target: aarch64-pc-windows-msvc

steps:
- uses: actions/checkout@v1

- uses: actions/download-artifact@v2
with:
name: engine
path: bin

- name: ls
run: ls -R
working-directory: bin

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: default
toolchain: stable
target: ${{ matrix.target }}
default: true

- name: Install gnu gcc
run: |
sudo apt-get update
sudo apt-get install g++-aarch64-linux-gnu
sudo apt-get install gcc-aarch64-linux-gnu
if: matrix.os == 'ubuntu-latest'

- name: Build CLI ${{ matrix.os }}
env:
EXTISM_ENGINE_PATH: core.wasm
run: cargo build --release --target ${{ matrix.target }} --package extism-py

- name: Archive assets
run: gzip -k -f ${{ matrix.path }} && mv ${{ matrix.path }}.gz ${{ matrix.asset_name }}.gz

- name: Upload assets to artifacts
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.asset_name }}.gz
path: ${{ matrix.asset_name }}.gz

- name: Upload assets to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./${{ matrix.asset_name }}.gz
asset_name: ${{ matrix.asset_name }}.gz
asset_content_type: application/gzip

- name: Generate asset hash (Linux/MacOS)
run: ${{ matrix.shasum_cmd }} ${{ matrix.asset_name }}.gz | awk '{ print $1 }' > ${{ matrix.asset_name }}.gz.sha256
if: runner.os != 'Windows'

- name: Generate asset hash (Windows)
run: Get-FileHash -Path ${{ matrix.asset_name }}.gz -Algorithm SHA256 | Select-Object -ExpandProperty Hash > ${{ matrix.asset_name }}.gz.sha256
shell: pwsh
if: runner.os == 'Windows'

- name: Upload asset hash to artifacts
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.asset_name }}.gz.sha256
path: ${{ matrix.asset_name }}.gz.sha256

- name: Upload asset hash to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ github.event.release.upload_url }}
asset_path: ./${{ matrix.asset_name }}.gz.sha256
asset_name: ${{ matrix.asset_name }}.gz.sha256
asset_content_type: plain/text

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
extism-py
examples/*.wasm
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ check:
uv run ruff check lib/src/prelude.py
uv run ruff check bin/src/invoke.py
uv run ruff check build.py

clean:
rm -rf bin/target lib/target

core:
cd lib && cargo build --release

test: examples
EXTISM_ENABLE_WASI_OUTPUT=1 extism call ./examples/count-vowels.wasm count_vowels --wasi --input "this is a test"
EXTISM_ENABLE_WASI_OUTPUT=1 extism call ./examples/imports.wasm count_vowels --wasi --input "this is a test" --link example=./examples/imports_example.wasm


.PHONY: examples
examples: build
./extism-py -o examples/count-vowels.wasm examples/count-vowels.py
./extism-py -o examples/imports.wasm examples/imports.py
./extism-py -o examples/imports_example.wasm examples/imports_example.py

24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ Let's write a simple program that exports a `greet` function which will take a n
```python
import extism

__all__ = ["greet"]

@extism.plugin_fn
def greet():
name = extism.input_str()
extism.output_str(f"Hello, {name}")
```

Some things to note about this code:

1. We can export functions by name using `__all__`. This allows the host to invoke this function.
1. We can export functions by name using the `extism.plugin_fn` decorator. This allows the host to invoke this function.
3. In this PDK we code directly to the ABI. We get input from the using using `extism.input*` functions and we return data back with the `extism.output*` functions.

Let's compile this to Wasm now using the `extism-py` tool:
Expand Down Expand Up @@ -82,8 +81,7 @@ We catch any exceptions thrown and return them as errors to the host. Suppose we
```python
import extism

__all__ = ["greet"]

@extism.plugin_fn
def greet():
name = extism.input_str()
if name == "Benjamin":
Expand Down Expand Up @@ -112,8 +110,7 @@ echo $?
```python
import extism

__all__ = ["sum"]

@extism.plugin_fn
def sum():
params = extism.input_json()
extism.output_json({"sum": params['a'] + params['b']})
Expand All @@ -132,8 +129,7 @@ plug-in. These can be useful to statically configure the plug-in with some data
```python
import extism

__all__ = ["greet"]

@extism.plugin_fn
def greet():
user = extism.Config.get("user")
extism.output_str(f"Hello, {user}!")
Expand All @@ -154,8 +150,7 @@ At the current time, calling `console.log` emits an `info` log. Please file an i
```python
import extism

__all__ = ["log_stuff"]

@extism.plugin_fn
def log_stuff():
extism.log(Extism.LogLevel.Info, "Hello, world!")
```
Expand All @@ -174,9 +169,8 @@ Before compiling the compiler, you need to install prerequisites.

1. Install Rust using [rustup](https://rustup.rs)
2. Install the WASI target platform via `rustup target add --toolchain stable wasm32-wasi`
3. Install the wasi sdk using the makefile command: `make download-wasi-sdk`
4. Install [CMake](https://cmake.org/install/) (on macOS with homebrew, `brew install cmake`)
6. Install [Binaryen](https://github.com/WebAssembly/binaryen/) and add it's install location to your PATH (only wasm-opt is required for build process)
3. Install [CMake](https://cmake.org/install/) (on macOS with homebrew, `brew install cmake`)
4. Install [Binaryen](https://github.com/WebAssembly/binaryen/) and add it's install location to your PATH (only wasm-opt is required for build process)
5. Install [7zip](https://www.7-zip.org/)(only for Windows)


Expand All @@ -185,7 +179,7 @@ Before compiling the compiler, you need to install prerequisites.
Run make to compile the core crate (the engine) and the cli:

```
./build.sh
./build.py
```

To test the built compiler (ensure you have Extism installed):
Expand Down
9 changes: 1 addition & 8 deletions bin/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,5 @@ fn main() {
println!("cargo::rerun-if-changed=../lib/target/wasm32-wasi/release/core.wasm");

let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("core.wasm");
std::process::Command::new("wasm-opt")
.arg("--disable-reference-types")
.arg("-O2")
.arg("../lib/target/wasm32-wasi/release/core.wasm")
.arg("-o")
.arg(out)
.status()
.unwrap();
std::fs::copy("../lib/target/wasm32-wasi/release/core.wasm", out).unwrap();
}
Loading

0 comments on commit b5aad15

Please sign in to comment.