diff --git a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala index 7880c801ae..a624589396 100644 --- a/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala +++ b/sc/js/src/main/scala/sigmastate/lang/js/SigmaCompiler.scala @@ -58,7 +58,7 @@ object SigmaCompiler extends js.Object { * @return SigmaCompiler instance */ private def create(networkPrefix: Byte): SigmaCompiler = { - val compiler = new sigmastate.lang.SigmaCompiler(networkPrefix) + val compiler = sigmastate.lang.SigmaCompiler(networkPrefix) new SigmaCompiler(compiler) } } diff --git a/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala b/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala index 906266f6b9..9ce000448d 100644 --- a/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala +++ b/sc/shared/src/main/scala/org/ergoplatform/ErgoScriptPredef.scala @@ -12,7 +12,7 @@ object ErgoScriptPredef { /** Compiles the given ErgoScript `code` into ErgoTree expression. */ def compileWithCosting(env: ScriptEnv, code: String, networkPrefix: NetworkPrefix)(implicit IR: IRContext): Value[SType] = { - val compiler = new SigmaCompiler(networkPrefix) + val compiler = SigmaCompiler(networkPrefix) val res = compiler.compile(env, code) res.buildTree } diff --git a/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala b/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala index a598bf643f..865d58df51 100644 --- a/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala +++ b/sc/shared/src/main/scala/sigmastate/lang/SigmaCompiler.scala @@ -5,6 +5,7 @@ import fastparse.Parsed.Success import sigma.kiama.rewriting.Rewriter.{everywherebu, rewrite, rule} import org.ergoplatform.ErgoAddressEncoder.NetworkPrefix import org.ergoplatform.Global +import scalan.GraphIRReflection import sigmastate.Values.{SValue, Value} import sigmastate.eval.IRContext import sigmastate.interpreter.Interpreter.ScriptEnv @@ -14,7 +15,7 @@ import sigmastate.lang.syntax.ParserException import sigmastate.utxo._ import sigma.ast._ import sigmastate.SCollectionMethods.{ExistsMethod, ForallMethod, MapMethod} -import sigmastate.{Exponentiate, MultiplyGroup, SCollectionMethods, SGlobalMethods, SGroupElementMethods, Xor} +import sigmastate.{Exponentiate, InterpreterReflection, MultiplyGroup, SCollectionMethods, SGlobalMethods, SGroupElementMethods, Xor} /** * @param networkPrefix network prefix to decode an ergo address from string (PK op) @@ -51,9 +52,9 @@ case class CompilerResult[Ctx <: IRContext]( /** Compiler which compiles ErgoScript source code into ErgoTree. * @param settings compilation parameters \ */ -class SigmaCompiler(settings: CompilerSettings) { +class SigmaCompiler private(settings: CompilerSettings) { /** Constructs an instance for the given network type and with default settings. */ - def this(networkPrefix: Byte) = this( + private def this(networkPrefix: Byte) = this( CompilerSettings(networkPrefix, TransformingSigmaBuilder, lowerMethodCalls = true) ) @@ -141,6 +142,14 @@ class SigmaCompiler(settings: CompilerSettings) { } object SigmaCompiler { + /** Force initialization of reflection before any instance of SigmaCompiler is used. */ + val _ = (InterpreterReflection, GraphIRReflection) + + /** Constructs an instance for the given settings. */ def apply(settings: CompilerSettings): SigmaCompiler = new SigmaCompiler(settings) + + /** Constructs an instance for the given network type. */ + def apply(networkPrefix: Byte): SigmaCompiler = + new SigmaCompiler(networkPrefix) } diff --git a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala index c84ab8748f..7be159757c 100644 --- a/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala +++ b/sc/shared/src/test/scala/sigmastate/eval/ErgoScriptTestkit.scala @@ -31,7 +31,7 @@ trait ErgoScriptTestkit extends ContractsTestkit with LangTests import Context._ import Liftables._ - override lazy val compiler = new SigmaCompiler(CompilerSettings( + override lazy val compiler = SigmaCompiler(CompilerSettings( TestnetNetworkPrefix, IR.builder, lowerMethodCalls = true