Skip to content

Commit

Permalink
Merge pull request #6670 from roc-lang/glue-abilities
Browse files Browse the repository at this point in the history
Add abilities for glue types
  • Loading branch information
lukewilliamboswell authored Apr 24, 2024
2 parents 02333fe + 87feeb5 commit d67ba43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 8 additions & 6 deletions crates/glue/platform/TypeId.roc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
interface TypeId
exposes [TypeId, fromU64, toU64]
exposes [TypeId, typeIDfromU64, typeIDtoU64]
imports []

TypeId := U64 implements [Eq, Hash]
TypeId := U64 implements [Eq, Hash, Inspect, Encoding]

toU64 : TypeId -> U64
toU64 = \@TypeId x -> x
# renamed here so we can import the functions directly as a workaround for
# https://github.com/roc-lang/roc/issues/5477
typeIDtoU64 : TypeId -> U64
typeIDtoU64 = \@TypeId x -> x

fromU64 : U64 -> TypeId
fromU64 = @TypeId
typeIDfromU64 : U64 -> TypeId
typeIDfromU64 = @TypeId
18 changes: 9 additions & 9 deletions crates/glue/platform/Types.roc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interface Types
exposes [Types, shape, size, alignment, target, walkShapes, entryPoints]
imports [Shape.{ Shape }, TypeId.{ TypeId }, Target.{ Target }, TypeId]
imports [Shape.{ Shape }, TypeId.{ TypeId, typeIDfromU64, typeIDtoU64 }, Target.{ Target }]

# TODO: switch AssocList uses to Dict once roc_std is updated.
Tuple1 : [T Str TypeId]
Expand All @@ -23,7 +23,7 @@ Types := {
## Names and types of the entry points of the program (e.g. mainForHost)
entrypoints : List Tuple1,
target : Target,
}
} implements [Inspect, Encoding]

target : Types -> Target
target = \@Types types -> types.target
Expand All @@ -34,33 +34,33 @@ entryPoints = \@Types { entrypoints } -> entrypoints
walkShapes : Types, state, (state, Shape, TypeId -> state) -> state
walkShapes = \@Types { types: shapes }, originalState, update ->
List.walkWithIndex shapes originalState \state, elem, index ->
id = TypeId.fromU64 index
id = typeIDfromU64 index

update state elem id

shape : Types, TypeId -> Shape
shape = \@Types types, id ->
when List.get types.types (TypeId.toU64 id) is
when List.get types.types (typeIDtoU64 id) is
Ok answer -> answer
Err OutOfBounds ->
idStr = Num.toStr (TypeId.toU64 id)
idStr = Num.toStr (typeIDtoU64 id)

crash "TypeId #$(idStr) was not found in Types. This should never happen, and means there was a bug in `roc glue`. If you have time, please open an issue at <https://github.com/roc-lang/roc/issues>"

alignment : Types, TypeId -> U32
alignment = \@Types types, id ->
when List.get types.aligns (TypeId.toU64 id) is
when List.get types.aligns (typeIDtoU64 id) is
Ok answer -> answer
Err OutOfBounds ->
idStr = Num.toStr (TypeId.toU64 id)
idStr = Num.toStr (typeIDtoU64 id)

crash "TypeId #$(idStr) was not found in Types. This should never happen, and means there was a bug in `roc glue`. If you have time, please open an issue at <https://github.com/roc-lang/roc/issues>"

size : Types, TypeId -> U32
size = \@Types types, id ->
when List.get types.sizes (TypeId.toU64 id) is
when List.get types.sizes (typeIDtoU64 id) is
Ok answer -> answer
Err OutOfBounds ->
idStr = Num.toStr (TypeId.toU64 id)
idStr = Num.toStr (typeIDtoU64 id)

crash "TypeId #$(idStr) was not found in Types. This should never happen, and means there was a bug in `roc glue`. If you have time, please open an issue at <https://github.com/roc-lang/roc/issues>"

0 comments on commit d67ba43

Please sign in to comment.