From 309354d9e15dae15ce4aecbd3a19a6018ffe156c Mon Sep 17 00:00:00 2001 From: Ivan Zvonimir Horvat Date: Fri, 20 Nov 2020 18:43:54 +0100 Subject: [PATCH] 1272: bash script; simple gcd example (#2421) --- docs/lang-bash.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/lang-bash.md b/docs/lang-bash.md index 4ba858521a..fba5f9a741 100644 --- a/docs/lang-bash.md +++ b/docs/lang-bash.md @@ -1,3 +1,42 @@ # Using WebAssembly from Bash -... more coming soon +## Getting started and simple example + +First up you'll want to start a new module: + +```text +$ mkdir -p gcd-bash +$ cd gcd-bash +$ touch gcd.wat gcd.sh +``` + +Next, copy this example WebAssembly text module into your project. It exports a function for calculating the greatest common denominator of two numbers. + +## `gcd.wat` + +```wat +{{#include ../examples/gcd.wat}} +``` + +Create a bash script that will invoke GCD three times. + +## `gcd.sh` + +```bash +#!/bin/bash + +function gcd() { + # Cast to number; default = 0 + local x=$(($1)) + local y=$(($2)) + # Invoke GCD from module; suppress stderr + local result=$(wasmtime examples/gcd.wat --invoke gcd $x $y 2>/dev/null) + echo "$result" +} + +# main +for num in "27 6" "6 27" "42 12"; do + set -- $num + echo "gcd($1, $2) = $(gcd "$1" "$2")" +done +```