Adds fn Unsafe.leak that prevents destructor from being run on a value

This commit is contained in:
Tim Dévé 2020-04-09 17:10:43 +01:00 committed by Tim Deve
parent eccd2103df
commit 209136847e
2 changed files with 24 additions and 1 deletions

View File

@ -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 =

12
test/unsafe.carp Normal file
View File

@ -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"))