Adds fn Pointer.to-value to get a value out of Ptr

This commit is contained in:
Tim Dévé 2020-04-09 17:09:33 +01:00 committed by Tim Deve
parent 84403988f1
commit eccd2103df
3 changed files with 41 additions and 0 deletions

View File

@ -329,6 +329,26 @@
</p>
</div>
<div class="binder">
<a class="anchor" href="#to-value">
<h3 id="to-value">
to-value
</h3>
</a>
<div class="description">
template
</div>
<p class="sig">
(λ [(Ptr a)] a)
</p>
<span>
</span>
<p class="doc">
<p>converts a pointer to a value. The user will have to ensure themselves that this is a safe operation.</p>
</p>
</div>
<div class="binder">
<a class="anchor" href="#width">
<h3 id="width">

View File

@ -56,6 +56,7 @@ pointerModule = Env { envBindings = bindings
where bindings = Map.fromList [ templatePointerCopy
, templatePointerEqual
, templatePointerToRef
, templatePointerToValue
, templatePointerAdd
, templatePointerSub
, templatePointerWidth
@ -96,6 +97,18 @@ templatePointerToRef = defineTemplate
,"}"])
(const [])
-- | A template function for converting pointers to values (it's up to the user of this function to make sure that is a safe operation).
templatePointerToValue = defineTemplate
(SymPath ["Pointer"] "to-value")
(FuncTy [PointerTy (VarTy "p")] (VarTy "p") StaticLifetimeTy)
"converts a pointer to a value. The user will have to ensure themselves that this is a safe operation."
(toTemplate "$p $NAME ($p *p)")
(toTemplate $ unlines ["$DECL {"
," return *p;"
,"}"])
(const [])
templatePointerAdd = defineTemplate
(SymPath ["Pointer"] "add")
(FuncTy [PointerTy (VarTy "p"), LongTy] (PointerTy (VarTy "p")) StaticLifetimeTy)

View File

@ -7,6 +7,9 @@
(def xa (to-long x))
(def w (width x))
(defn ref-to-ptr [r]
(the (Ptr a) (Unsafe.coerce (the (Ref a) r))))
; these tests are sadly a little unsafe
(deftest test
(assert-equal test
@ -35,4 +38,9 @@
(to-long (dec x))
"Pointer.dec works as expected"
)
(assert-equal test
(to-value (ref-to-ptr &123))
123
"Pointer.to-value works as expected"
)
)