mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-17 08:27:45 +03:00
Adds fn Unsafe.leak that prevents destructor from being run on a value
This commit is contained in:
parent
eccd2103df
commit
209136847e
@ -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
12
test/unsafe.carp
Normal 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"))
|
Loading…
Reference in New Issue
Block a user