mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-17 08:27:45 +03:00
Adds fn Pointer.to-value to get a value out of Ptr
This commit is contained in:
parent
84403988f1
commit
eccd2103df
@ -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">
|
||||
|
@ -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)
|
||||
|
@ -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"
|
||||
)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user