Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Last active November 13, 2019 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerrorJack/bccc8ad05d65461d26ca748214d147ec to your computer and use it in GitHub Desktop.
Save TerrorJack/bccc8ad05d65461d26ca748214d147ec to your computer and use it in GitHub Desktop.
{-# 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