Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/extism/assemblyscript-pdk

Extism Plug-in Development Kit (PDK) for AssemblyScript
https://github.com/extism/assemblyscript-pdk

Last synced: about 2 months ago
JSON representation

Extism Plug-in Development Kit (PDK) for AssemblyScript

Awesome Lists containing this project

README

        

# Extism AssemblyScript PDK

### Installation

```sh
npm install @extism/[email protected] --save
```

### Compiling to WebAssembly

Use the AssemblyScript compiler, `asc`:

```sh
npx asc example.ts --outFile example.wasm --use abort=example/myAbort
```

### Example Usage

```typescript title=example.ts
import { Host, Var, Config } from '@extism/as-pdk';

function myAbort(
message: string | null,
fileName: string | null,
lineNumber: u32,
columnNumber: u32
): void { }

export function count_vowels(): i32 {
let str = Host.inputString();
var count = 0;
for (var i = 0; i < str.length; i++) {
let x: string = str[i];
if (x == 'a' || x == 'A' ||
x == 'e' || x == 'E' ||
x == 'i' || x == 'I' ||
x == 'o' || x == 'O' ||
x == 'u' || x == 'U') {
count += 1;
}
}

// Additional plug-in APIs:

// persistent variables (scoped to individual plugin)
var a = Uint8Array.wrap(String.UTF8.encode("this is var a"))
Var.set('a', a);

let data = Var.get('a');
let var_a = (data == null) ? "null" : String.UTF8.decode(data.buffer);

// config, provided by the host
const thing = Config.get("thing");

// write data back to host for use in program
var out = '{"count": ' + count.toString() + ', "config": "' + (thing == null ? "null" : thing) + '", "a": "' + var_a + '"}';
Host.outputString(out);
Var.remove('a');

return 0;
}
```