Skip to content

Commit 16d564c

Browse files
authored
Merge pull request #103 from tweag/fd/fixrrlists
Fix Reify and reflect for lists of references. Fixes #102
2 parents 6c5e35a + cde66ae commit 16d564c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

jvm/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2222
* The `Reify`/`Reflect` instances for `()` is now mapped to
2323
a *serializable* small JVM object. This is a more useful instance
2424
for sparkle users.
25+
* `Reify`/`Reflect` were given an additional invariant.
26+
The result and argument of `reflect` and `reify` shouldn't share any
27+
direct JVM object references. This fixes memory errors in the
28+
instances of `[J ty]`.
29+
[#102](https://github.com/tweag/inline-java/pull/102)
2530

2631
## [0.3.0] - 2017-08-31
2732

jvm/src/Language/Java.hs

+6-2
Original file line numberDiff line numberDiff line change
@@ -469,19 +469,23 @@ class (SingI (Interp a), IsReferenceType (Interp a)) => Interpretation (a :: k)
469469
-- say, unmarshall a Java object to a Haskell value. Unlike coercing, in general
470470
-- reifying induces allocations and copies.
471471
class Interpretation a => Reify a where
472+
-- | Invariant: The result and the argument share no direct JVM object
473+
-- references.
472474
reify :: J (Interp a) -> IO a
473475

474476
default reify :: (Coercible a, Interp a ~ Ty a) => J (Interp a) -> IO a
475-
reify x = return (unsafeUncoerce (JObject x))
477+
reify x = unsafeUncoerce . JObject <$> (newLocalRef x :: IO (J (Ty a)))
476478

477479
-- | Inject a concrete Haskell value into the space of Java objects. That is to
478480
-- say, marshall a Haskell value to a Java object. Unlike coercing, in general
479481
-- reflection induces allocations and copies.
480482
class Interpretation a => Reflect a where
483+
-- | Invariant: The result and the argument share no direct JVM object
484+
-- references.
481485
reflect :: a -> IO (J (Interp a))
482486

483487
default reflect :: (Coercible a, Interp a ~ Ty a) => a -> IO (J (Interp a))
484-
reflect x = return (jobject x)
488+
reflect x = newLocalRef (jobject x)
485489

486490
#if ! (__GLASGOW_HASKELL__ == 800 && __GLASGOW_HASKELL_PATCHLEVEL1__ == 1)
487491
reifyMVector

0 commit comments

Comments
 (0)