Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: replace FromPointer with FromBytes #7

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extism.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: extism
version: 0.5.0
version: 1.0.0-alpha-0
license: BSD-3-Clause
maintainer: [email protected]
author: Extism authors
Expand All @@ -22,7 +22,7 @@ library
base >= 4.16.1 && < 5,
bytestring >= 0.11.3 && <= 0.12,
json >= 0.10 && <= 0.11,
extism-manifest >= 0.0.0 && < 0.4.0,
extism-manifest >= 0.0.0 && <= 1.0.0,
uuid >= 1.3 && < 2

test-suite extism-example
Expand Down
2 changes: 1 addition & 1 deletion manifest/extism-manifest.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: extism-manifest
version: 0.3.0
version: 1.0.0-alpha-0
license: BSD-3-Clause
maintainer: [email protected]
author: Extism authors
Expand Down
46 changes: 21 additions & 25 deletions src/Extism.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module Extism (
pluginID,
unwrap,
ToBytes(..),
FromPointer(..),
Encoding,
FromBytes(..),
JSONValue(..)
) where

Expand Down Expand Up @@ -149,26 +150,24 @@ class ToBytes a where
toBytes :: a -> B.ByteString

-- Used to read a value from linear memory
class FromPointer a where
fromPointer :: CString -> Int -> IO (Result a)
class FromBytes a where
fromBytes :: B.ByteString -> Result a

-- Encoding is used to indicate a type implements both `ToBytes` and `FromBytes`
class (ToBytes a, FromBytes a) => Encoding a where

instance ToBytes B.ByteString where
toBytes x = x

instance FromPointer B.ByteString where
fromPointer (Ptr ptr) len = do
x <- unsafePackLenAddress (fromIntegral len) ptr
return $ Right x
instance FromBytes B.ByteString where
fromBytes = Right

instance ToBytes [Char] where
toBytes = toByteString

instance FromPointer [Char] where
fromPointer ptr len = do
bs <- fromPointer ptr len
case bs of
Left e -> return $ Left e
Right bs -> return $ Right $ fromByteString bs
instance FromBytes [Char] where
fromBytes bs =
Right $ fromByteString bs

-- Wraps a `JSON` value for input/output
newtype JSONValue x = JSONValue x
Expand All @@ -178,19 +177,15 @@ instance Extism.JSON.JSON a => ToBytes (JSONValue a) where
toByteString $ Text.JSON.encode x


instance Extism.JSON.JSON a => FromPointer (JSONValue a) where
fromPointer ptr len = do
s <- fromPointer ptr len
case s of
Left e -> return $ Left e
Right s ->
case Text.JSON.decode s of
Text.JSON.Error x -> return $ Left (ExtismError x)
Text.JSON.Ok x -> return $ Right (JSONValue x)
instance Extism.JSON.JSON a => FromBytes (JSONValue a) where
fromBytes bs = do
case Text.JSON.decode (fromByteString bs) of
Text.JSON.Error x -> Left (ExtismError x)
Text.JSON.Ok x -> Right (JSONValue x)


--- | Call a function provided by the given plugin
call :: (ToBytes a, FromPointer b) => Plugin -> String -> a -> IO (Result b)
call :: (ToBytes a, FromBytes b) => Plugin -> String -> a -> IO (Result b)
call (Plugin plugin) name inp =
let input = toBytes inp in
let length' = fromIntegral (B.length input) in
Expand All @@ -205,8 +200,9 @@ call (Plugin plugin) name inp =
else if rc == 0
then do
len <- extism_plugin_output_length plugin'
ptr <- extism_plugin_output_data plugin'
fromPointer (castPtr ptr) (fromIntegral len)
Ptr ptr <- extism_plugin_output_data plugin'
x <- unsafePackLenAddress (fromIntegral len) ptr
return $ fromBytes x
else return $ Left (ExtismError "Call failed"))

-- | Create a new 'CancelHandle' that can be used to cancel a running plugin
Expand Down
9 changes: 4 additions & 5 deletions src/Extism/HostFunction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ memoryString plugin offs = do
fromByteString <$> memoryBytes plugin offs

-- | Access the data associated with a handle and convert it into a Haskell type
memoryGet :: FromPointer a => CurrentPlugin -> MemoryHandle -> IO (Result a)
memoryGet :: FromBytes a => CurrentPlugin -> MemoryHandle -> IO (Result a)
memoryGet plugin offs = do
ptr <- memoryOffset plugin offs
len <- memoryLength plugin offs
fromPointer (castPtr ptr) (fromIntegral len)
x <- memoryBytes plugin offs
return $ fromBytes x

-- | Allocate memory and copy an existing 'ByteString' into it
allocBytes :: CurrentPlugin -> B.ByteString -> IO MemoryHandle
Expand Down Expand Up @@ -172,7 +171,7 @@ output !p !index !x =
if index >= len then return ()
else pokeElemOff res index (toI64 mem)

input :: FromPointer a => CurrentPlugin -> Int -> IO (Result a)
input :: FromBytes a => CurrentPlugin -> Int -> IO (Result a)
input plugin index =
let (CurrentPlugin _ params _ _) = plugin in
let x = fromI64 (params !! index) :: Maybe Word64 in
Expand Down
Loading