Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import mill.contrib.scoverage.{ScoverageModule, ScoverageReport}
object scoverage extends BaseModule with ScoverageReport

trait BaseModule extends ScoverageModule with ScalafmtModule {
def scalaVersion = "3.3.0-RC5"
def scalaVersion = "3.3.0-RC6"
def scoverageVersion = "2.0.7"

val munitVersion = "1.0.0-M7"
Expand Down
5 changes: 3 additions & 2 deletions core/src/fr/hammons/slinc/DescriptorOf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fr.hammons.slinc.container.*
import scala.quoted.*
import scala.compiletime.{summonInline, erasedValue, constValue}
import scala.NonEmptyTuple
import scala.reflect.ClassTag

/** Typeclass that summons TypeDescriptors
*/
Expand Down Expand Up @@ -78,8 +79,8 @@ object DescriptorOf:
CUnionDescriptor(helper[A])
.asInstanceOf[CUnionDescriptor { type Inner = CUnion[A] }]

inline given [A, B <: Int](using
innerDesc: DescriptorOf[A]
inline given [A, B <: Int](using innerDesc: DescriptorOf[A])(using
classTag: ClassTag[innerDesc.descriptor.Inner]
): DescriptorOf[SetSizeArray[A, B]] = new DescriptorOf[SetSizeArray[A, B]]:
val descriptor: TypeDescriptor { type Inner = SetSizeArray[A, B] } =
SetSizeArrayDescriptor(innerDesc.descriptor, constValue[B]).asInstanceOf[
Expand Down
2 changes: 1 addition & 1 deletion core/src/fr/hammons/slinc/SetSizeArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import scala.compiletime.ops.int.{`*`, `-`, `<=`, `+`, `<`}
import scala.compiletime.constValue
import scala.quoted.*
import scala.language.experimental.erasedDefinitions
import scala.annotation.experimental

class SetSizeArray[A, B <: Int] private[slinc] (private val array: Array[A])
extends AnyVal:
Expand All @@ -16,6 +15,7 @@ class SetSizeArray[A, B <: Int] private[slinc] (private val array: Array[A])
): SetSizeArray[C, B * D] =
new SetSizeArray[C, B * D](array.flatMap(fn.andThen(_.array)))
def toSeq: Seq[A] = array.toSeq
def toArray: Array[A] = array
inline def take[C <: Int](using
C <= B =:= true,
0 <= C =:= true
Expand Down
18 changes: 13 additions & 5 deletions core/src/fr/hammons/slinc/TypeDescriptor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import fr.hammons.slinc.modules.TransitionModule
import fr.hammons.slinc.modules.{ArgumentTransition, ReturnTransition}
import scala.NonEmptyTuple
import scala.language.implicitConversions
import dotty.tools.dotc.transform.patmat.Typ

/** Describes types used by C interop
*/
sealed trait TypeDescriptor:
self =>
type Inner
given DescriptorOf[Inner] with
val descriptor = self
def size(using dm: DescriptorModule): Bytes = dm.sizeOf(this)
def alignment(using dm: DescriptorModule): Bytes = dm.alignmentOf(this)
def toCarrierType(using dm: DescriptorModule): Class[?] =
Expand Down Expand Up @@ -220,13 +222,19 @@ case class CUnionDescriptor(possibleTypes: Set[TypeDescriptor])
case class SetSizeArrayDescriptor(
val contained: TypeDescriptor,
val number: Int
) extends TypeDescriptor:
)(using ClassTag[contained.Inner])
extends TypeDescriptor:

override val reader: (ReadWriteModule, DescriptorModule) ?=> Reader[Inner] =
???
(mem, offset) =>
new SetSizeArray(
summon[ReadWriteModule].readArray[contained.Inner](mem, offset, number)
)

override val writer: (ReadWriteModule, DescriptorModule) ?=> Writer[Inner] =
???
(mem, offset, value) =>
summon[ReadWriteModule]
.writeArray[contained.Inner](mem, offset, value.toArray)

override val argumentTransition
: (TransitionModule, ReadWriteModule, Allocator) ?=> ArgumentTransition[
Expand All @@ -236,4 +244,4 @@ case class SetSizeArrayDescriptor(
override val returnTransition
: (TransitionModule, ReadWriteModule) ?=> ReturnTransition[Inner] = ???

type Inner = SetSizeArray[?, ?]
type Inner = SetSizeArray[contained.Inner, ?]
26 changes: 26 additions & 0 deletions core/test/src/fr/hammons/slinc/TransferSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ trait TransferSpec[ThreadException <: Throwable](val slinc: Slinc)(using

case class F(u: CUnion[(CInt, CFloat)]) derives Struct

case class G(arr: SetSizeArray[CLong, 2]) derives Struct

test("can read and write jvm ints") {
Scope.global {
val mem = Ptr.blank[Int]
Expand Down Expand Up @@ -353,3 +355,27 @@ trait TransferSpec[ThreadException <: Throwable](val slinc: Slinc)(using
}

assertEquals(fReturn.u.get[CFloat], union.get[CFloat])

test("can copy SetSizeArray[Int, 2] to native memory"):
val ssa = SetSizeArray(1, 2)

Scope.confined {
val ptr = Ptr.copy(ssa)
assertEquals((!ptr)[0], 1)
}

test("can copy SetSizeArray[CLong, 2] to native memory"):
val ssa = SetSizeArray(CLong(1), CLong(2))

Scope.confined {
val ptr = Ptr.copy(ssa)
assertEquals((!ptr)[0], CLong(1))
}

test("can copy G to native memory and back"):
val g = G(SetSizeArray(CLong(1), CLong(2)))

Scope.confined {
val ptr = Ptr.copy(g)
assertEquals((!ptr).arr[0], CLong(1))
}