Last active
November 13, 2019 15:22
-
-
Save TerrorJack/bccc8ad05d65461d26ca748214d147ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE MagicHash #-} | |
{-# LANGUAGE StrictData #-} | |
{-# LANGUAGE UnboxedTuples #-} | |
{-# OPTIONS_GHC -Wall -O2 #-} | |
import Data.Foldable | |
import GHC.Exts | |
import GHC.Types | |
import System.Mem | |
data JSVal | |
= JSVal Int (MutVar# RealWorld ()) | |
fin :: Int -> State# RealWorld -> (# State# RealWorld, () #) | |
fin i = case putStrLn $ "FIN " <> show i of | |
IO m -> m | |
newJSVal :: Int -> IO JSVal | |
newJSVal i = IO $ \s0 -> case newMutVar# () s0 of | |
(# s1, v #) -> case mkWeak# v () (fin i) s1 of | |
(# s2, _ #) -> (# s2, JSVal i v #) | |
main :: IO () | |
main = do | |
for_ [0 .. 8] newJSVal | |
x <- newJSVal 233 | |
performGC | |
let JSVal v _ = x | |
print v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment