Skip to content

Commit

Permalink
feat: implement blit on ByteOrder (carp-lang#1410)
Browse files Browse the repository at this point in the history
* feat: add box templates and box type

This commit adds an implementation of Boxes, memory manged heap
allocated values.

Boxes are implemented as C pointers, with no additional structure but
are treated as structs in Carp. To facilitate this, we need to add them
as a clause to our special type emissions (TypesToC) as they'd otherwise
be emitted like other struct types.

Co-authored-by: Veit Heller <[email protected]>

* fix: slight memory management fix for Box

Make sure we free the box!

* test: add tests for box (including memory checks)

* Revert "fix: Ignore clang nitpick"

This reverts commit 70ec6d4.

* fix: update example/functor.carp

Now that a builtin type named Box exists, the definitions in this file
cause a conflict. I've renamed the "Box" type in the functor example to
remove the conflict.

* feat: add Box.peek

Box.peek allows users to transform a reference to a box into a a
reference to the box's contained value. The returned reference will have
the same lifetime as the box. This function allows callers to manipulate
the value in a box without re-allocation, for example:

```clojure
(deftype Num [val Int])

(let-do [box (Box.init (Num.init 0))]
  (Num.set-val! (Box.peek &box) 1)
  @(Num.val (Box.peek &box)))
```

This commit also includes tests for Box.peek.

Co-authored-by: TimDeve <[email protected]>

* feat: implement blit on ByteOrder

ByteOrder is effectively just an enum that indicates desired endianness.
We can freely copy it around without penalty.

Co-authored-by: Veit Heller <[email protected]>
Co-authored-by: Erik Svedäng <[email protected]>
Co-authored-by: TimDeve <[email protected]>
  • Loading branch information
4 people committed Apr 6, 2022
1 parent 8e8a67d commit a9e806f
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/Binary.carp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
significant byte occurs first in a given byte sequence.")
(deftype ByteOrder LittleEndian BigEndian)

(defmodule ByteOrder
(defn blit [x] (the ByteOrder x))
(implements blit blit)
)

(doc Binary "provides various helper functions to work with bits and bytes.")
(defmodule Binary
(register to-int16 (λ [Byte Byte] Uint16))
Expand Down

0 comments on commit a9e806f

Please sign in to comment.