Skip to content

Write and run a plug-in

Official documentation: https://extism.org/docs/category/write-a-plug-in

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package main

import (
    "github.com/extism/go-pdk"
)

//export hello
func hello() {
    // read function argument from the shared memory
    input := pdk.Input()
    output := "๐Ÿ‘‹ Hello " + string(input)

    // copy output to shared memory
    mem := pdk.AllocateString(output)
    pdk.OutputMemory(mem)
}

func main() {}
1
2
3
4
5
6
7
8
9
use extism_pdk::*;

#[plugin_fn]
pub fn hello(input: String) -> FnResult<String> {

    let output : String = "๐Ÿ‘‹ Hello ".to_string() + &input;

    Ok(output)
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function hello() {

    // read function argument from the memory
    let input = Host.inputString()

    let output = "๐Ÿ‘‹ Hello " + input
    // copy output to host memory
    Host.outputString(output)
}

module.exports = {hello}

Build

1
2
3
4
#!/bin/bash
tinygo build -scheduler=none --no-debug \
-o hello.wasm \
-target wasi main.go
1
2
3
4
5
#!/bin/bash
cargo clean
cargo build --release --target wasm32-wasi
ls -lh ./target/wasm32-wasi/release/*.wasm
cp ./target/wasm32-wasi/release/*.wasm .
1
2
#!/bin/bash
extism-js index.js -o hello.wasm

Run

1
2
#!/bin/bash
./slingshot run --wasm=./hello.wasm --handler=hello --input="Bob ๐Ÿค“"

Output

1
๐Ÿ‘‹ Hello Bob ๐Ÿค“