Skip to content

Commit b74a493

Browse files
committed
Simplify thunks / laziness in ValScope.extend() and Evaluator.visitBindings()
1 parent fd038d8 commit b74a493

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

sjsonnet/src/sjsonnet/Evaluator.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -699,18 +699,16 @@ class Evaluator(
699699

700700
def visitBindings(
701701
bindings: Array[Bind],
702-
scope: (Val.Obj, Val.Obj) => ValScope): Array[(Val.Obj, Val.Obj) => Lazy] = {
703-
val arrF = new Array[(Val.Obj, Val.Obj) => Lazy](bindings.length)
702+
scope: => ValScope): Array[Lazy] = {
703+
val arrF = new Array[Lazy](bindings.length)
704704
var i = 0
705705
while (i < bindings.length) {
706706
val b = bindings(i)
707707
arrF(i) = b.args match {
708708
case null =>
709-
(self: Val.Obj, sup: Val.Obj) =>
710-
new LazyWithComputeFunc(() => visitExpr(b.rhs)(scope(self, sup)))
709+
new LazyWithComputeFunc(() => visitExpr(b.rhs)(scope))
711710
case argSpec =>
712-
(self: Val.Obj, sup: Val.Obj) =>
713-
new LazyWithComputeFunc(() => visitMethod(b.rhs, argSpec, b.pos)(scope(self, sup)))
711+
new LazyWithComputeFunc(() => visitMethod(b.rhs, argSpec, b.pos)(scope))
714712
}
715713
i += 1
716714
}
@@ -851,7 +849,7 @@ class Evaluator(
851849
new Val.Obj.Member(e.plus, Visibility.Normal) {
852850
def invoke(self: Val.Obj, sup: Val.Obj, fs: FileScope, ev: EvalScope): Val = {
853851
lazy val newScope: ValScope = s.extend(newBindings, self, sup)
854-
lazy val newBindings = visitBindings(binds, (self, sup) => newScope)
852+
lazy val newBindings = visitBindings(binds, newScope)
855853
visitExpr(e.value)(newScope)
856854
}
857855
}

sjsonnet/src/sjsonnet/ValScope.scala

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,14 @@ final class ValScope private (val bindings: Array[Lazy]) extends AnyVal {
1919
def length: Int = bindings.length
2020

2121
def extend(
22-
newBindingsF: Array[(Val.Obj, Val.Obj) => Lazy] = null,
22+
newBindings: Array[Lazy],
2323
newSelf: Val.Obj,
2424
newSuper: Val.Obj): ValScope = {
25-
val by = if (newBindingsF == null) 2 else newBindingsF.length + 2
25+
val by = newBindings.length + 2
2626
val b = Arrays.copyOf(bindings, bindings.length + by)
2727
b(bindings.length) = newSelf
2828
b(bindings.length + 1) = newSuper
29-
if (newBindingsF != null) {
30-
var i = 0
31-
var j = bindings.length + 2
32-
while (i < newBindingsF.length) {
33-
b(j) = newBindingsF(i).apply(newSelf, newSuper)
34-
i += 1
35-
j += 1
36-
}
37-
}
29+
System.arraycopy(newBindings, 0, b, bindings.length + 2, newBindings.length)
3830
new ValScope(b)
3931
}
4032

0 commit comments

Comments
 (0)