Skip to content

Commit 993e9e4

Browse files
authored
refactor:Migrate StdlibSpec to Lib from Library (#108)
Fixes #101
1 parent ec17a36 commit 993e9e4

File tree

10 files changed

+194
-141
lines changed

10 files changed

+194
-141
lines changed

core/src/fr/hammons/slinc/Alias.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ trait Alias[T] extends DescriptorOf[T]:
66
val name: String
77
val aliases: PartialFunction[(OS, Arch), RealTypeDescriptor]
88
lazy val descriptor: TypeDescriptor { type Inner >: T <: T } =
9-
new AliasDescriptor[T](
9+
AliasDescriptor[T](
1010
aliases.applyOrElse(
1111
os -> arch,
1212
_ =>
1313
throw new Error(
1414
s"Alias for $name is not defined on platform $os - $arch"
1515
)
1616
)
17-
):
18-
19-
val reader = (rwm, _) ?=> (mem, bytes) => rwm.readAlias(mem, bytes, real)
20-
val writer = (rwm, _) ?=>
21-
(mem, bytes, a) => rwm.writeAlias(mem, bytes, real, a)
17+
)

core/src/fr/hammons/slinc/Bytes.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.hammons.slinc
22

33
import scala.quoted.{ToExpr, Quotes}
4+
import types.SizeT
45

56
opaque type Bytes = Long
67

@@ -15,6 +16,7 @@ object Bytes:
1516
inline def -(b: Bytes): Bytes = a - b
1617
inline def toLong: Long = a
1718
inline def toBits: Long = a * 8
19+
def sizeT = SizeT.maybe(a).get
1820

1921
given Numeric[Bytes] = Numeric.LongIsIntegral
2022
given ToExpr[Bytes] with

core/src/fr/hammons/slinc/CFunctionBindingGenerator.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,17 @@ object CFunctionBindingGenerator:
8888
(sym, inputs) =>
8989
def inputExprs(alloc: Expr[Allocator])(using q: Quotes) =
9090
val prefix = if allocatingReturn then List(alloc.asTerm) else Nil
91-
val toTransform = if varArg then inputs.init else inputs
91+
val toTransform =
92+
if varArg && inputs.nonEmpty then inputs.init
93+
else inputs
9294
LambdaInputs.choose(
9395
prefix
9496
.concat(toTransform)
9597
.map(_.asExpr)
9698
.zipWithIndex
9799
.map: (exp, i) =>
98100
'{ $inputTransitions(${ Expr(i) })($alloc, $exp) },
99-
varArg
101+
varArg && inputs.nonEmpty
100102
)(inputs.last.asExprOf[Seq[Variadic]])
101103

102104
'{

core/src/fr/hammons/slinc/CFunctionDescriptor.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ object CFunctionDescriptor:
3333
s"C Function analog ${methodSymbol.fullName} has unsupported type ${t.show}"
3434
)
3535

36-
val isVariadic = argumentTypes.last match
37-
case typ if typ =:= TypeRepr.of[Seq[Variadic]] =>
36+
val isVariadic = argumentTypes.lastOption match
37+
case Some(typ) if typ =:= TypeRepr.of[Seq[Variadic]] =>
3838
true
3939
case _ => false
4040

core/src/fr/hammons/slinc/TypeDescriptor.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,23 @@ trait StructDescriptor(
146146
val transform: Tuple => Product
147147
) extends RealTypeDescriptor
148148

149-
trait AliasDescriptor[A](val real: RealTypeDescriptor) extends TypeDescriptor:
149+
case class AliasDescriptor[A](val real: RealTypeDescriptor)
150+
extends TypeDescriptor:
150151
type Inner = A
152+
type RealInner = real.Inner
151153

152-
given bkwd: Conversion[Inner, real.Inner] with
154+
given bkwd: Conversion[Inner, RealInner] with
153155
def apply(x: Inner): real.Inner = x.asInstanceOf[real.Inner]
154156

155-
given fwd: Conversion[real.Inner, Inner] with
157+
given fwd: Conversion[RealInner, Inner] with
156158
def apply(x: real.Inner): Inner = x.asInstanceOf[Inner]
157159

160+
val reader: (ReadWriteModule, DescriptorModule) ?=> Reader[Inner] =
161+
(rwm, _) ?=> (mem, bytes) => rwm.readAlias(mem, bytes, real)
162+
163+
val writer: (ReadWriteModule, DescriptorModule) ?=> Writer[Inner] =
164+
(rwm, _) ?=> (mem, bytes, a) => rwm.writeAlias(mem, bytes, real, a)
165+
158166
override def size(using dm: DescriptorModule): Bytes = dm.sizeOf(real)
159167
override def alignment(using dm: DescriptorModule): Bytes =
160168
dm.alignmentOf(real)

core/src/fr/hammons/slinc/types/CLong.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ object CLong:
1414
case i: Int => IntegralAlias.transform[CLong](i)
1515

1616
def maybe(maybeFits: Long): Option[CLong] =
17-
if (maybeFits <= Int.MaxValue && maybeFits >= Int.MinValue) || IntegralAlias
18-
.range[CLong]
19-
.contains(maybeFits)
17+
if (maybeFits <= Int.MaxValue && maybeFits >= Int.MinValue) || (IntegralAlias
18+
.min[CLong] <= maybeFits && maybeFits <= IntegralAlias.max[CLong])
2019
then Some(IntegralAlias.transform[CLong](maybeFits))
2120
else None
2221

core/src/fr/hammons/slinc/types/IntegralAlias.scala

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,59 @@ import fr.hammons.slinc.ShortDescriptor
77
import fr.hammons.slinc.IntDescriptor
88

99
object IntegralAlias:
10+
def min[T](using a: Alias[T]): Long = a.aliases.applyOrElse(
11+
(os, arch),
12+
_ => throw new Error(s"Alias for ${a.name} not defined for this platform")
13+
) match
14+
case ByteDescriptor => Byte.MinValue.toLong
15+
case ShortDescriptor => Short.MinValue.toLong
16+
case IntDescriptor => Int.MinValue.toLong
17+
case LongDescriptor => Long.MinValue
18+
case _ =>
19+
throw new Error(
20+
s"${a.name} is not an alias for an integral type on $os $arch"
21+
)
22+
23+
def max[T](using a: Alias[T]): Long = a.aliases.applyOrElse(
24+
(os, arch),
25+
_ => throw new Error(s"Alias for ${a.name} not defined for this platform")
26+
) match
27+
case ByteDescriptor => Byte.MaxValue.toLong
28+
case ShortDescriptor => Short.MaxValue.toLong
29+
case IntDescriptor => Int.MaxValue.toLong
30+
case LongDescriptor => Long.MaxValue
31+
case _ =>
32+
throw new Error(
33+
s"${a.name} is not an alias for an integral type on $os $arch"
34+
)
35+
1036
def range[T](using a: Alias[T]) = a.aliases.applyOrElse(
1137
(os, arch),
1238
_ => throw new Error(s"Alias for ${a.name} not defined for this platform")
1339
) match
14-
case ByteDescriptor => Byte.MinValue to Byte.MaxValue
15-
case ShortDescriptor => Short.MinValue to Short.MaxValue
16-
case IntDescriptor => Int.MinValue to Int.MaxValue
17-
case LongDescriptor => Long.MinValue to Long.MaxValue
40+
case ByteDescriptor => Range.Long.inclusive(Byte.MinValue, Byte.MaxValue, 1)
41+
case ShortDescriptor =>
42+
Range.Long.inclusive(Short.MinValue, Short.MaxValue, 1)
43+
case IntDescriptor => Range.Long.inclusive(Int.MinValue, Int.MaxValue, 1)
44+
case LongDescriptor => Long.MinValue to Long.MaxValue
1845
case _ =>
1946
throw new Error(
2047
s"${a.name} is not an alias for an integral type on $os $arch"
2148
)
2249

50+
def toLong[T](value: T)(using a: Alias[T]) = a.aliases.applyOrElse(
51+
(os, arch),
52+
_ => throw new Error(s"Alias for ${a.name} not defined for this platform")
53+
) match
54+
case ByteDescriptor => value.asInstanceOf[Byte].toLong
55+
case ShortDescriptor => value.asInstanceOf[Short].toLong
56+
case IntDescriptor => value.asInstanceOf[Int].toLong
57+
case LongDescriptor => value.asInstanceOf[Long].toLong
58+
case _ =>
59+
throw new Error(
60+
s"${a.name} is not an alias for an integral type on $os - $arch"
61+
)
62+
2363
def transform[T]: Transform[T] = Transform[T]
2464
class Transform[T]:
2565
def apply[U](value: U)(using a: Alias[T], n: Numeric[U]) =

core/src/fr/hammons/slinc/types/SizeT.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import fr.hammons.slinc.LongDescriptor
55

66
opaque type SizeT = AnyVal
77

8-
given Alias[SizeT] with
9-
val name = "SizeT"
10-
val aliases = { case (OS.Linux | OS.Darwin | OS.Windows, Arch.X64) =>
11-
LongDescriptor
12-
}
13-
148
object SizeT:
159
def apply(value: Short | Byte): SizeT = value match
1610
case s: Short => IntegralAlias.transform[SizeT](s)
@@ -24,3 +18,9 @@ object SizeT:
2418
if value < 65536 || IntegralAlias.range[SizeT].contains(value) then
2519
Some(IntegralAlias.transform[SizeT](value))
2620
else None
21+
22+
given Alias[SizeT] with
23+
val name = "SizeT"
24+
val aliases = { case (OS.Linux | OS.Darwin | OS.Windows, Arch.X64) =>
25+
LongDescriptor
26+
}

core/src/fr/hammons/slinc/types/TimeT.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import fr.hammons.slinc.LongDescriptor
55

66
opaque type TimeT = Any
77

8-
given Alias[TimeT] with
9-
val name: String = "TimeT"
10-
val aliases = { case (OS.Windows | OS.Linux | OS.Darwin, Arch.X64) =>
11-
LongDescriptor
12-
}
13-
148
object TimeT:
159
def maybe(value: Byte | Short | Int | Long): Option[TimeT] =
1610
val upcast = value match
@@ -22,3 +16,9 @@ object TimeT:
2216
if IntegralAlias.range[TimeT].contains(upcast) then
2317
Some(IntegralAlias.transform[TimeT](upcast))
2418
else None
19+
20+
given Alias[TimeT] with
21+
val name: String = "TimeT"
22+
val aliases = { case (OS.Windows | OS.Linux | OS.Darwin, Arch.X64) =>
23+
LongDescriptor
24+
}

0 commit comments

Comments
 (0)