README.md 964 B

erlang + Zig external port example

erl 25
zig 0.13.0
(l)ubuntu 22.04 lts



$ zig build-exe porttest.zig -O ReleaseFast -femit-bin=porttest

$ printf '\x00\x05hello' | ./porttest | hexdump -C
00000000  00 0c 48 65 6c 6c 6f 20  77 6f 72 6c 64 21        |..Hello world!|
0000000e

$ printf '\x00\x0b\x66\x61\x63\x74\x6f\x72\x69\x61\x6c\x20\x35' | ./porttest | hexdump -C
00000000  00 03 31 32 30                                    |..120|
00000005

$ erl
Erlang/OTP 25 [erts-13.2] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit:ns]

Eshell V13.2  (abort with ^G)
1> c(porttest).
{ok,porttest}
2> porttest:start_link().
{ok,<0.93.0>}
3> porttest:hello().
"Hello world!"
4> porttest:factorial(5).
120
5> porttest:factorial(20).
2432902008176640000
6> porttest:factorial(34).
295232799039604140847618609643520000000
7> porttest:factorial(35).
{error,<<"input too large, max is 34">>}
8> porttest:factorial(-5).
{error,badarg}
9> q().
ok