Skip to content

Commit

Permalink
Merge pull request #200 from user16332/fix-text
Browse files Browse the repository at this point in the history
Java string marshalling agnostic of `text` library's internal represe…
  • Loading branch information
facundominguez authored Nov 24, 2023
2 parents 85c6a3e + 0303152 commit 9395b0d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions jni/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
## Next

### Added

* Support to build with text >= 2 ([#200](https://github.com/tweag/inline-java/pull/200))

### Changed
### Removed

Expand Down
5 changes: 3 additions & 2 deletions jni/src/common/Foreign/JNI/Unsafe/Internal/Introspection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ module Foreign.JNI.Unsafe.Internal.Introspection

import Control.Exception (bracket)
import Control.Monad (forM)
import Data.ByteString as BS
import Data.Coerce
import Data.Maybe (catMaybes)
import Data.Singletons
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Text.Foreign as Text
import Foreign.Ptr
import Foreign.JNI.Unsafe.Internal
import Foreign.JNI.Types
import qualified Foreign.JNI.String as JNI
Expand Down Expand Up @@ -101,7 +102,7 @@ toText obj = bracket
(releaseStringChars obj) $
\cs -> do
sz <- fromIntegral <$> getStringLength obj
txt <- Text.fromPtr cs sz
txt <- Text.decodeUtf16LEWith (\_ _ -> Just '?') <$> BS.packCStringLen (castPtr cs, sz * 2)
return txt

-- | @getClassName c@ yields the name of class @c@
Expand Down
12 changes: 6 additions & 6 deletions jvm/src/common/Language/Java/Unsafe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import Control.Exception (Exception, throw, finally)
import Control.Monad
import Control.Monad.Catch (MonadCatch, MonadMask, bracket, onException)
import Control.Monad.IO.Class
import qualified Data.ByteString as BS
import Data.Char (chr, ord)
import qualified Data.Choice as Choice
import qualified Data.Coerce as Coerce
Expand All @@ -120,17 +121,16 @@ import Data.Proxy (Proxy(..))
import Data.Typeable (Typeable, TypeRep, typeOf)
import Data.Word
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Unsafe as BS
import Data.Kind (Type)
import Data.Singletons (SingI(..), SomeSing(..))
import qualified Data.Text.Foreign as Text
import qualified Data.Text.Encoding as Text
import Data.Text (Text)
import qualified Data.Vector.Storable as Vector
import Data.Vector.Storable (Vector)
import qualified Data.Vector.Storable.Mutable as MVector
import Data.Vector.Storable.Mutable (IOVector)
import Foreign (Ptr, Storable, withForeignPtr)
import Foreign (Ptr, Storable, withForeignPtr, castPtr)
import Foreign.Concurrent (newForeignPtr)
import Foreign.C (CChar)
import Foreign.JNI hiding (throw)
Expand Down Expand Up @@ -750,14 +750,14 @@ withStatic [d|
reify jobj = do
sz <- getStringLength jobj
cs <- getStringChars jobj
txt <- Text.fromPtr cs (fromIntegral sz)
txt <- Text.decodeUtf16LEWith (\_ _ -> Just '?') <$> BS.packCStringLen (castPtr cs, fromIntegral sz * 2)
releaseStringChars jobj cs
return txt

instance Reflect Text where
reflect x =
Text.useAsPtr x $ \ptr len ->
newString ptr (fromIntegral len)
BS.useAsCStringLen (Text.encodeUtf16LE x) $ \(ptr, len) ->
newString (castPtr ptr) (fromIntegral len `div` 2)

newtype W8Bool = W8Bool { fromW8Bool :: Word8 }
deriving (Enum, Eq, Integral, Num, Ord, Real, Show, Storable)
Expand Down
2 changes: 1 addition & 1 deletion nixpkgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# by going to a commit before the one introducing the regression.
args:
let pkgs = import (fetchTarball "https://github.com/tweag/nixpkgs/archive/73ad5f9e147.tar.gz") args;
let pkgs = import (fetchTarball "https://github.com/tweag/nixpkgs/archive/0cbe9f69c234a7700596e943bfae7ef27a31b735.tar.gz") args;
stack_ignore_global_hints = pkgs.writeScriptBin "stack" ''
#!${pkgs.stdenv.shell}
# Skips the --global-hints parameter to stack. This is
Expand Down

0 comments on commit 9395b0d

Please sign in to comment.