Skip to content

Commit

Permalink
initial some test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Oct 7, 2024
1 parent 83ba4a4 commit 08b02c7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/shared/src/main/scala/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -767,5 +767,9 @@ trait SigmaDslBuilder {

/** Returns a number decoded from provided big-endian bytes array. */
def fromBigEndianBytes[T](bytes: Coll[Byte])(implicit cT: RType[T]): T

def some[T](value: T)(implicit cT: RType[T]): Option[T]

def none[T]()(implicit cT: RType[T]): Option[T]
}

17 changes: 16 additions & 1 deletion data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1835,13 +1835,28 @@ case object SGlobalMethods extends MonoTypeMethods {
Colls.fromArray(w.toBytes)
}

lazy val someMethod = SMethod(this, "some",
SFunc(Array(SGlobal, tT), SOption(tT), Array(paramT)), 8, FixedCost(JitCost(5)), Seq(tT)) // todo: cost
.withIRInfo(MethodCallIrBuilder,
javaMethodOf[SigmaDslBuilder, Any, RType[_]]("some"),
{ mtype => Array(mtype.tRange) })
.withInfo(MethodCall, "",
ArgInfo("value", "value to be serialized"))

lazy val noneMethod = SMethod(this, "none",
SFunc(Array(SGlobal, tT), SOption(tT), Array(paramT)), 9, FixedCost(JitCost(5))) // todo: cost
.withIRInfo(MethodCallIrBuilder)
.withInfo(MethodCall, "")

protected override def getMethods() = super.getMethods() ++ {
if (VersionContext.current.isV6SoftForkActivated) {
Seq(
groupGeneratorMethod,
xorMethod,
serializeMethod,
fromBigEndianBytesMethod
fromBigEndianBytesMethod,
someMethod,
noneMethod
)
} else {
Seq(
Expand Down
8 changes: 8 additions & 0 deletions data/shared/src/main/scala/sigma/data/CSigmaDslBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ class CSigmaDslBuilder extends SigmaDslBuilder { dsl =>
DataSerializer.serialize(value.asInstanceOf[SType#WrappedType], tpe, w)
Colls.fromArray(w.toBytes)
}

override def some[T](value: T)(implicit cT: RType[T]): Option[T] = {
Some(value)
}

override def none[T]()(implicit cT: RType[T]): Option[T] = {
None
}
}

/** Default singleton instance of Global object, which implements global ErgoTree functions. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,10 @@ trait GraphBuilding extends Base with DefRewriting { IR: IRContext =>
val bytes = asRep[Coll[Byte]](argsV(0))
val cT = stypeToElem(method.stype.tRange.withSubstTypes(typeSubst))
g.fromBigEndianBytes(bytes)(cT)
case SGlobalMethods.someMethod.name =>
val value = asRep[tT.WrappedType](argsV(0))
val cT = stypeToElem(typeSubst.apply(tT)).asInstanceOf[Elem[tT.WrappedType]]
g.some(value)(cT)
case _ => throwError()
}
case (x: Ref[tNum], _: SNumericTypeMethods) => method.name match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ import scalan._
def xor(l: Ref[Coll[Byte]], r: Ref[Coll[Byte]]): Ref[Coll[Byte]]
def serialize[T](value: Ref[T]): Ref[Coll[Byte]]
def fromBigEndianBytes[T](bytes: Ref[Coll[Byte]])(implicit cT: Elem[T]): Ref[T]
def some[T](value: Ref[T])(implicit cT: Elem[T]): Ref[WOption[T]]
def none[T]()(implicit cT: Elem[T]): Ref[WOption[T]]
};
trait CostModelCompanion;
trait BigIntCompanion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,20 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
true, false, cT))
}

override def some[T](value: Ref[T])(implicit cT: Elem[T]): Ref[WOption[T]] = {
asRep[WOption[T]](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("some", classOf[Sym], classOf[Elem[T]]),
Array[AnyRef](value, cT, Map(tT -> Evaluation.rtypeToSType(cT.sourceType))),
true, false, element[WOption[T]]))
}

override def none[T]()(implicit cT: Elem[T]): Ref[WOption[T]] = {
asRep[WOption[T]](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("none", classOf[Elem[T]]),
Array[AnyRef](cT, Map(tT -> Evaluation.rtypeToSType(cT.sourceType))),
true, false, element[WOption[T]]))
}

}

implicit object LiftableSigmaDslBuilder
Expand Down Expand Up @@ -2151,6 +2165,21 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
Array[AnyRef](bytes, cT),
true, true, cT, Map(tT -> Evaluation.rtypeToSType(cT.sourceType))))
}

def some[T](value: Ref[T])(implicit cT: Elem[T]): Ref[WOption[T]] = {
asRep[WOption[T]](mkMethodCall(source,
SigmaDslBuilderClass.getMethod("some", classOf[Sym], classOf[Elem[T]]),
Array[AnyRef](value, cT),
true, true, element[WOption[T]], Map(tT -> Evaluation.rtypeToSType(cT.sourceType))))
}

def none[T]()(implicit cT: Elem[T]): Ref[WOption[T]] = {
asRep[WOption[T]](mkMethodCall(source,
SigmaDslBuilderClass.getMethod("none", classOf[Elem[T]]),
Array[AnyRef](cT),
true, true, element[WOption[T]], Map(tT -> Evaluation.rtypeToSType(cT.sourceType))))
}

}

// entityUnref: single unref method for each type family
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1734,4 +1734,25 @@ class BasicOpsSpecification extends CompilerTestingCommons
}
}

property("Global.some") {
val ext: Seq[VarBinding] = Seq(
(intVar1, IntConstant(0))
)
def someTest(): Assertion = {
test("some", env, ext,
"""{
| val xo = Global.some[Int](5)
| xo.get == 5
|}""".stripMargin,
null
)
}

if (VersionContext.current.isV6SoftForkActivated) {
someTest()
} else {
an[Exception] should be thrownBy someTest()
}
}

}

0 comments on commit 08b02c7

Please sign in to comment.