Skip to content

Commit

Permalink
fixing JS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kushti committed Sep 24, 2024
1 parent a072123 commit cb9e9eb
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/sigma/SigmaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,6 @@ trait SigmaDslBuilder {
def xor(l: Coll[Byte], r: Coll[Byte]): Coll[Byte]

/** Deserializes provided `bytes` into a value of type `T`. **/
def deserializeTo[T](tpe: SType, bytes: Coll[Byte])(implicit cT: RType[T]): T
def deserializeTo[T](bytes: Coll[Byte])(implicit cT: RType[T]): T
}

Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ object ReflectionData {
mkMethod(clazz, "decodePoint", Array[Class[_]](cColl)) { (obj, args) =>
obj.asInstanceOf[SigmaDslBuilder].decodePoint(args(0).asInstanceOf[Coll[Byte]])
},
mkMethod(clazz, "deserializeTo", Array[Class[_]](classOf[SType], cColl, classOf[RType[_]])) { (obj, args) =>
obj.asInstanceOf[SigmaDslBuilder].deserializeTo(args(0).asInstanceOf[SType], args(1).asInstanceOf[Coll[Byte]])(args(2).asInstanceOf[RType[_]])
mkMethod(clazz, "deserializeTo", Array[Class[_]](cColl, classOf[RType[_]])) { (obj, args) =>
obj.asInstanceOf[SigmaDslBuilder].deserializeTo(args(0).asInstanceOf[Coll[Byte]])(args(1).asInstanceOf[RType[_]])
}
)
)
Expand Down
5 changes: 5 additions & 0 deletions data/shared/src/main/scala/sigma/SigmaDataReflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ object SigmaDataReflection {
obj.asInstanceOf[SGlobalMethods.type].serialize_eval(args(0).asInstanceOf[MethodCall],
args(1).asInstanceOf[SigmaDslBuilder],
args(2).asInstanceOf[SType#WrappedType])(args(3).asInstanceOf[ErgoTreeEvaluator])
},
mkMethod(clazz, "deserializeTo_eval", Array[Class[_]](classOf[MethodCall], classOf[SigmaDslBuilder], classOf[Coll[_]], classOf[ErgoTreeEvaluator])) { (obj, args) =>
obj.asInstanceOf[SGlobalMethods.type].deserializeTo_eval(args(0).asInstanceOf[MethodCall],
args(1).asInstanceOf[SigmaDslBuilder],
args(2).asInstanceOf[Coll[Byte]])(args(3).asInstanceOf[ErgoTreeEvaluator])
}
)
)
Expand Down
7 changes: 3 additions & 4 deletions data/shared/src/main/scala/sigma/ast/methods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1687,11 +1687,10 @@ case object SGlobalMethods extends MonoTypeMethods {
private val deserializeCostKind = PerItemCost(
baseCost = JitCost(20), perChunkCost = JitCost(7), chunkSize = 128)

lazy val desJava = ownerType.reprClass.getMethod("deserializeTo", classOf[SType], classOf[Coll[Byte]], classOf[RType[_]])

lazy val deserializeToMethod = SMethod(
this, "deserializeTo", SFunc(Array(SGlobal, SByteArray), tT, Array(paramT)), 4, deserializeCostKind, Seq(tT))
.withIRInfo(MethodCallIrBuilder, desJava)
.withIRInfo(MethodCallIrBuilder,
javaMethodOf[SigmaDslBuilder, Coll[Byte], RType[_]]("deserializeTo"))
.withInfo(MethodCall, "Deserialize provided bytes into an object of requested type",
ArgInfo("first", "Bytes to deserialize"))

Expand All @@ -1709,7 +1708,7 @@ case object SGlobalMethods extends MonoTypeMethods {
val tpe = mc.tpe
val cT = stypeToRType(tpe)
E.addSeqCost(deserializeCostKind, bytes.length, deserializeToMethod.opDesc) { () =>
G.deserializeTo(tpe, bytes)(cT)
G.deserializeTo(bytes)(cT)
}
}

Expand Down
4 changes: 3 additions & 1 deletion data/shared/src/main/scala/sigma/data/CSigmaDslBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.ergoplatform.validation.ValidationRules
import scorex.crypto.hash.{Blake2b256, Sha256}
import scorex.util.serialization.VLQByteBufferReader
import scorex.utils.Longs
import sigma.Evaluation.rtypeToSType
import sigma.ast.{AtLeast, SType, SubstConstants}
import sigma.crypto.{CryptoConstants, EcPointType, Ecp}
import sigma.eval.Extensions.EvalCollOps
Expand Down Expand Up @@ -211,7 +212,8 @@ class CSigmaDslBuilder extends SigmaDslBuilder { dsl =>
Colls.fromArray(w.toBytes)
}

def deserializeTo[T](tpe: SType, bytes: Coll[Byte])(implicit cT: RType[T]): T = {
def deserializeTo[T](bytes: Coll[Byte])(implicit cT: RType[T]): T = {
val tpe = rtypeToSType(cT)
val reader = new SigmaByteReader(new VLQByteBufferReader(ByteBuffer.wrap(bytes.toArray)), new ConstantStore(), false)
val res = DataSerializer.deserialize(tpe, reader)
res.asInstanceOf[T]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sigma.compiler.ir

import sigma.Coll
import sigma.ast.SType
import sigma.compiler.ir.primitives.Thunks
import sigma.data.RType
import sigma.reflection.ReflectionData.registerClassEntry
Expand Down Expand Up @@ -510,6 +512,9 @@ object GraphIRReflection {
},
mkMethod(clazz, "serialize", Array[Class[_]](classOf[Base#Ref[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.SigmaDslBuilder].serialize(args(0).asInstanceOf[ctx.Ref[Any]])
},
mkMethod(clazz, "deserializeTo", Array[Class[_]](classOf[Base#Ref[_]], classOf[TypeDescs#Elem[_]])) { (obj, args) =>
obj.asInstanceOf[ctx.SigmaDslBuilder].deserializeTo(args(0).asInstanceOf[ctx.Ref[ctx.Coll[Byte]]])(args(1).asInstanceOf[ctx.Elem[SType]])
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {
asRep[T](mkMethodCall(self,
SigmaDslBuilderClass.getMethod("deserializeTo", classOf[Sym], classOf[Elem[T]]),
Array[AnyRef](l, cT),
true, false, element[T]))
true, false, element[T](cT), Map(tT -> Evaluation.rtypeToSType(cT.sourceType))))
}
}

Expand Down Expand Up @@ -2147,9 +2147,9 @@ object SigmaDslBuilder extends EntityObject("SigmaDslBuilder") {

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

Expand Down
6 changes: 3 additions & 3 deletions sc/shared/src/test/scala/sigma/LanguageSpecificationV6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>
property("Global.deserializeTo - group element") {
def deserializeTo: Feature[GroupElement, Boolean] = {
newFeature(
{ (x: GroupElement) => CSigmaDslBuilder.deserializeTo[GroupElement](SGroupElement, x.getEncoded) == x},
{ (x: GroupElement) => CSigmaDslBuilder.deserializeTo[GroupElement](x.getEncoded) == x},
"{ (x: GroupElement) => Global.deserializeTo[GroupElement](x.getEncoded) == x }",
FuncValue(
Array((1, SGroupElement)),
Expand Down Expand Up @@ -1568,7 +1568,7 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>

def deserializeTo: Feature[Header, Boolean] = {
newFeature(
{ (x: Header) => CSigmaDslBuilder.deserializeTo[Header](SHeader, CSigmaDslBuilder.serialize(x)) == x},
{ (x: Header) => CSigmaDslBuilder.deserializeTo[Header](CSigmaDslBuilder.serialize(x)) == x},
"{ (x: Header) => Global.deserializeTo[Header](serialize(x)) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
Expand All @@ -1588,7 +1588,7 @@ class LanguageSpecificationV6 extends LanguageSpecificationBase { suite =>

def deserializeTo: Feature[BigInt, Boolean] = {
newFeature(
{ (x: BigInt) => CSigmaDslBuilder.deserializeTo[BigInt](SBigInt, CSigmaDslBuilder.serialize(x)) == x},
{ (x: BigInt) => CSigmaDslBuilder.deserializeTo[BigInt](CSigmaDslBuilder.serialize(x)) == x},
"{ (x: BigInt) => Global.deserializeTo[BigInt](serialize(x)) == x }",
sinceVersion = VersionContext.V6SoftForkVersion
)
Expand Down
2 changes: 1 addition & 1 deletion sdk/js/src/main/scala/org/ergoplatform/sdk/js/Isos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import sigma.data.js.{Isos => DataIsos}
import sigma.data.CHeader
import sigma.interpreter.{ContextExtension, ProverResult}
import sigma.js.AvlTree
import sigmastate.eval.{CHeader, CPreHeader}
import sigmastate.eval.CPreHeader
import sigmastate.fleetSdkCommon.distEsmTypesBoxesMod.Box
import sigmastate.fleetSdkCommon.distEsmTypesRegistersMod.NonMandatoryRegisters
import sigmastate.fleetSdkCommon.distEsmTypesTokenMod.TokenAmount
Expand Down

0 comments on commit cb9e9eb

Please sign in to comment.