]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/SafeDynamic.scl
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / SafeDynamic.scl
1 import "Prelude"
2 import "JavaBuiltin" as Java
3
4 @JavaType "org.simantics.scl.compiler.dynamic.SafeDynamic"
5 data SafeDynamic =
6     @FieldNames [type_, value]
7     SafeDynamic Type Dynamic
8
9 instance Show SafeDynamic where
10     sb <+ SafeDynamic t v = sb << "SafeDynamic " <+ t
11
12 toSafeDynamic :: Typeable a => a -> SafeDynamic
13 toSafeDynamic val = SafeDynamic (typeOf val) (toDynamic val)
14
15 fromSafeDynamic :: Typeable a => SafeDynamic -> Maybe a
16 fromSafeDynamic (SafeDynamic type_ value) =
17     if type_ == typeOfProxy (TypeProxy :: TypeProxy a)
18     then Just (Java.unsafeCoerce value)
19     else Nothing
20
21 forgetType :: SafeDynamic -> Dynamic
22 forgetType (SafeDynamic _ value) = value
23
24 typeOfSafeDynamic :: SafeDynamic -> Type
25 typeOfSafeDynamic (SafeDynamic t _) = t