Skip to content

Instantly share code, notes, and snippets.

@TerrorJack

TerrorJack/jsval-gc.hs

Last active Nov 13, 2019
Embed
What would you like to do?
{-# 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
You can’t perform that action at this time.