Skip to content

Commit 717ec90

Browse files
committed
Examples
1 parent 26a0773 commit 717ec90

6 files changed

Lines changed: 107 additions & 28 deletions

File tree

docs/writing-functions.md

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,8 @@ installation path added to the `PATH` environment variable.
5151

5252
### Examples
5353

54-
**Wasm file smaller than 2MB**: assuming we have a `hello.wasm` file inside the
55-
`/home/user/code/` directory
54+
See `examples/wasi`.
5655

57-
A function can be created using this command:
58-
59-
serverledge-cli create -f func-name \
60-
--runtime wasi \
61-
--src /home/user/code/hello.wasm \
62-
--custom_image hello
63-
64-
**Python** (using the official build): assuming the `.tar` is hosted on
65-
`localhost:8000/python.tar` and inside the `.tar` file there is a `func.py`
66-
67-
At the time of writing, the official build is provided by the maintainer of the
68-
WASI platform in Python at the following [link](https://github.com/brettcannon/cpython-wasi-build/releases/tag/v3.13.0)
69-
70-
Serverledge assumes the structure of the `python.tar` file to be the following:
71-
72-
- `func.py`: the main source file; the name must be specified in the `handler` argument of the function creation
73-
- `python.wasm` (obtained through the link above)
74-
- `lib/` (obtained through the link above)
75-
76-
A function can be created using this command:
77-
78-
serverledge-cli create -f func-name \
79-
--runtime wasi \
80-
--src http://localhost:8000/python.tar \
81-
--custom_image python \
82-
--handler func.py
8356

8457
## Custom function runtimes
8558

examples/wasi/Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WASI-CC=$${HOME}/wasi-sdk/bin/clang
2+
WASI-LD=$${HOME}/wasi-sdk/bin/wasm-ld
3+
4+
CFLAGS=--sysroot=$${HOME}/wasi-sdk/share/wasi-sysroot
5+
6+
all:
7+
$(WASI-CC) $(CFLAGS) fibonacci.c -o fibonacci.wasm
8+
9+
clean:
10+
@rm -rf fibonacci.wasm
11+
12+
13+
.PHONY: all clean

examples/wasi/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## C
2+
3+
Download WASI SDK [here](https://github.com/WebAssembly/wasi-sdk/releases).
4+
At the time of writing the latest release is WASI SDK 25
5+
6+
Update the location of the downloaded SDK in `Makefile`.
7+
8+
The example `fibonacci.c` is compiled to `fibonacci.wasm`:
9+
10+
make
11+
12+
The function can be created using this command:
13+
14+
serverledge-cli create -f fib-wasi \
15+
--runtime wasi \
16+
--src fibonacci.wasm \
17+
--custom_image fibonacci
18+
19+
## Python
20+
21+
See the `python` directory.

examples/wasi/fibonacci.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
4+
uint32_t fib(uint32_t n) {
5+
if (n <= 1)
6+
return n;
7+
return fib(n - 1) + fib(n - 2);
8+
}
9+
10+
int main(int argc, char **argv) {
11+
uint32_t n = 30;
12+
13+
uint32_t result = fib(n);
14+
printf("%u\n", result);
15+
return 0;
16+
}

examples/wasi/python/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
**Python** (using the official build): assuming the `.tar` is hosted on
3+
`localhost:8000/python.tar` and inside the `.tar` file there is a `sieve.py`
4+
5+
At the time of writing, the official build is provided by the maintainer of the
6+
WASI platform in Python at the following [link](https://github.com/brettcannon/cpython-wasi-build/releases/tag/v3.13.0)
7+
8+
Serverledge assumes the structure of the `python.tar` file to be the following:
9+
10+
- `sieve.py`: the main source file; the name must be specified in the `handler` argument of the function creation
11+
- `python.wasm` (obtained through the link above)
12+
- `lib/` (obtained through the link above)
13+
14+
A function can be created using this command:
15+
16+
serverledge-cli create -f func-name \
17+
--runtime wasi \
18+
--src http://localhost:8000/python.tar \
19+
--custom_image python \
20+
--handler sieve.py

examples/wasi/python/sieve.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
import json
3+
4+
5+
def sieve(limit: int) -> list[int]:
6+
sieve: list[bool] = [False] * (limit + 1)
7+
primes: list[int] = list()
8+
for i in range(2, limit + 1):
9+
if not sieve[i]:
10+
primes.append(i)
11+
for j in range(i << 1, limit + 1, i):
12+
sieve[j] = True
13+
return primes
14+
15+
16+
if __name__ == "__main__":
17+
limit = 100000
18+
if len(sys.argv) > 1:
19+
params = json.loads(sys.argv[1])
20+
if "limit" in params:
21+
limit = int(params["limit"])
22+
primes = sieve(limit)
23+
24+
print(primes)
25+
26+
27+
def handler(params, context):
28+
limit = 100000
29+
if len(sys.argv) > 1:
30+
params = json.loads(sys.argv[1])
31+
if "limit" in params:
32+
limit = int(params["limit"])
33+
primes = sieve(limit)
34+
35+
print(primes)
36+
return primes

0 commit comments

Comments
 (0)