Skip to content

Commit

Permalink
fix SpecStringMutator memory bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hyp3rflow committed May 22, 2024
1 parent fefb7c3 commit 2ff0152
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/main/scala/esmeta/mutator/SpecStringMutator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import esmeta.util.BaseUtils.*
class SpecStringMutator(using cfg: CFG)(
val synBuilder: Synthesizer.Builder = RandomSynthesizer,
) extends Mutator
with Util.MultiplicativeListWalker {
with Walker {
import SpecStringMutator.*

val randomMutator = RandomMutator()
Expand All @@ -34,35 +34,37 @@ class SpecStringMutator(using cfg: CFG)(
val k = primaryCounter(ast)
if (k == 0) randomMutator(ast, n, target)
else
c = (n - 1) / k + 1
targetCondStr = target.flatMap(_._1.cond.elem match {
case esmeta.cfg.Branch(_, _, e, _, _) => findCondStr(e)
case _ => None
})
sample(ast, n)
}

/** parameter for sampler */
private var c = 0

/** string in target branch */
private var targetCondStr: Option[String] = None

/** sample n distinct asts using spec-strings */
private def sample(ast: Ast, n: Int) =
shuffle(walk(ast)).take(n).map((name, _))
private def sample(ast: Ast, n: Int): Seq[(String, Ast)] =
Set.tabulate[Ast](n)(_ => walk(ast)).map((name, _)).toSeq

/** ast walker */
override def walk(syn: Syntactic): List[Syntactic] =
override def walk(syn: Syntactic): Syntactic =
if (isPrimary(syn))
List.tabulate(c)(i => {
if (targetCondStr.isDefined && i == 0)
generateString(targetCondStr.get, syn.args)
else
generateObject(syn.args)
}) ++ super.walk(syn)
else
super.walk(syn)
if (targetCondStr.isDefined)
weightedChoose(
generateString(targetCondStr.get, syn.args) -> 1,
// Add more weight to generate more object
generateObject(syn.args) -> 3,
syn -> 1,
)
else
weightedChoose(
// Add more weight to generate more object
generateObject(syn.args) -> 4,
syn -> 1,
)
else super.walk(syn)

// convert the given string to primary expression
def generateString(str: String, args: List[Boolean]): Syntactic =
Expand Down

0 comments on commit 2ff0152

Please sign in to comment.