Skip to content

Commit

Permalink
refactor: better organization, cleanup (#5)
Browse files Browse the repository at this point in the history
* cleanup: start reorganizing

* ci

* fix: shadowed variable name

* refactor: add more binary types, move memory code to its own file

* docs: show example with non-string input

* cleanup: hlint

* cleanup: reorganize

* fix: don't free errors

* cleanup: reorganize

* cleanup: bring back Memory.store functions, remove unused imports

* docs: add missing doc comments

* docs: add link

* cleanup: add explicit return code

* cleanup: add tryInput

* cleanup: count vowels example

* fix: http example
  • Loading branch information
zshipko authored Sep 29, 2023
1 parent 4293ec7 commit a215b5d
Show file tree
Hide file tree
Showing 13 changed files with 610 additions and 344 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for haskell-wasm
# Revision history for extism-pdk

## 0.1.0.0 -- YYYY-mm-dd
## 0.2.0.0

* API redesign, add automatic Haskell encoding using `ToMemory` and `FromMemory` classes

## 0.1.0.0 -- 2023-09-28

* First version. Released on an unsuspecting world.

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
all: hello.example http_get.example count_vowels.example

update:
wasm32-wasi-cabal update

build:
wasm32-wasi-cabal build

Expand Down
19 changes: 12 additions & 7 deletions examples/CountVowels.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module CountVowels where
import Extism.PDK
import Extism.PDK.JSON

isVowel c =
c == 'a' || c == 'A' ||
c == 'e' || c == 'E' ||
c == 'i' || c == 'I' ||
c == 'o' || c == 'O' ||
c == 'u' || c == 'U'
isVowel c =
c == 'a'
|| c == 'A'
|| c == 'e'
|| c == 'E'
|| c == 'i'
|| c == 'I'
|| c == 'o'
|| c == 'O'
|| c == 'u'
|| c == 'U'

countVowels = do
-- Get input string from Extism host
Expand All @@ -18,4 +23,4 @@ countVowels = do
-- Return a JSON object {"count": count} back to the host
output $ JSONValue $ object ["count" .= count]

foreign export ccall "count_vowels" countVowels :: IO ()
foreign export ccall "count_vowels" countVowels :: IO ()
23 changes: 17 additions & 6 deletions examples/HTTPGet.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
module HTTPGet where

import Data.Int
import Extism.PDK
import Extism.PDK.HTTP
import Extism.PDK.Memory

getInput = do
req <- tryInput
case req of
Right (JSONValue x) -> return x
Left e -> do
putStrLn e
url <- inputString
return $ newRequest url

httpGet = do
-- Get URL from the host
url <- input
-- Create a new 'Request'
let req = newRequest url
-- Get URL or JSON encoded request from host
req <- getInput
-- Send the request, get a 'Response'
res <- sendRequest req Nothing
res <- sendRequest req (Nothing :: Maybe String)
-- Save response body to memory
outputMemory (memory res)
-- Return code
return 0

foreign export ccall "http_get" httpGet :: IO ()
foreign export ccall "http_get" httpGet :: IO Int32
8 changes: 4 additions & 4 deletions examples/Hello.hs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module Hello where

import Extism.PDK
import Data.Maybe
import Extism.PDK
import Foreign.C.Types

defaultGreeting = "Hello"

greet g n =
output $ g ++ ", " ++ n

testing = do
-- Get a name from the Extism runtime
name <- input
name <- inputString
-- Get configured greeting
greeting <- getConfig "greeting"
-- Greet the user, if no greeting is configured then "Hello" is used
greet (fromMaybe defaultGreeting greeting) name

foreign export ccall "testing" testing :: IO ()
foreign export ccall "testing" testing :: IO ()
18 changes: 13 additions & 5 deletions extism-pdk.cabal
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cabal-version: 3.0
name: extism-pdk
version: 0.1.0.0
version: 0.2.0.0

-- A short (one-line) description of the package.
synopsis: Extism Plugin Development Kit

-- A longer description of the package.
description: Haskell bindings to the Extism runtime
description: Haskell bindings to the Extism runtime for use with wasm32-wasi-ghc

-- A URL where users can report bugs.
bug-reports: https://github.com/extism/haskell-pdk
Expand All @@ -20,7 +20,14 @@ category: WASM, plugins
extra-doc-files: CHANGELOG.md

library
exposed-modules: Extism.PDK Extism.PDK.Bindings Extism.PDK.HTTP Extism.PDK.JSON Extism.PDK.MsgPack
exposed-modules:
Extism.PDK
Extism.PDK.Bindings
Extism.PDK.HTTP
Extism.PDK.JSON
Extism.PDK.MsgPack
Extism.PDK.Util
Extism.PDK.Memory

-- Modules included in this executable, other than Main.
-- other-modules:
Expand All @@ -34,7 +41,8 @@ library
containers >= 0.6.7 && < 0.7,
extism-manifest >= 0.3.0 && <= 1.0.0,
json >= 0.11 && < 0.12,
messagepack >= 0.5.5 && < 0.6
messagepack >= 0.5.5 && < 0.6,
binary >= 0.8.9 && < 0.9.0

hs-source-dirs: src
default-language: Haskell2010
Expand All @@ -45,7 +53,7 @@ executable hello
build-depends: base, extism-pdk
default-language: Haskell2010
ghc-options:
-optl -Wl,--export=testing
-optl -Wl,--export=testing

executable http_get
scope: private
Expand Down
Loading

0 comments on commit a215b5d

Please sign in to comment.