From 209136847e7a382240650febd1452e8f5d77fa5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20De=CC=81ve=CC=81?= Date: Thu, 9 Apr 2020 17:10:43 +0100 Subject: [PATCH] Adds fn Unsafe.leak that prevents destructor from being run on a value --- src/StartingEnv.hs | 13 ++++++++++++- test/unsafe.carp | 12 ++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/unsafe.carp diff --git a/src/StartingEnv.hs b/src/StartingEnv.hs index 620cbfae..7c3e465f 100644 --- a/src/StartingEnv.hs +++ b/src/StartingEnv.hs @@ -386,7 +386,7 @@ unsafeModule = Env { envBindings = bindings , envUseModules = [] , envMode = ExternalEnv , envFunctionNestingLevel = 0 } - where bindings = Map.fromList [ templateCoerce ] + where bindings = Map.fromList [ templateCoerce, templateLeak ] -- | A template for coercing (casting) a type to another type templateCoerce :: (String, Binder) @@ -400,6 +400,17 @@ templateCoerce = defineTemplate ,"}"]) (const []) +-- | A template function for preventing destructor from being run on a value (it's up to the user of this function to make sure that memory is freed). +templateLeak = defineTemplate + (SymPath ["Unsafe"] "leak") + (FuncTy [(VarTy "a")] UnitTy StaticLifetimeTy) + "prevents a destructor from being run on a value a." + (toTemplate "void $NAME ($a a)") + (toTemplate $ unlines ["$DECL {" + ," // Leak" + ,"}"]) + (const []) + -- | The global environment before any code is run. startingGlobalEnv :: Bool -> Env startingGlobalEnv noArray = diff --git a/test/unsafe.carp b/test/unsafe.carp new file mode 100644 index 00000000..6e4aba2e --- /dev/null +++ b/test/unsafe.carp @@ -0,0 +1,12 @@ +(load "Test.carp") +(use Test) + +(deftest test + (assert-equal test + 1l + (do + (Debug.reset-memory-balance!) + (let [s @"String"] + (Unsafe.leak s)) + (Debug.memory-balance)) + "Unsafe.leak should stop Carp from freeing allocated memory"))