Skip to content

Commit

Permalink
Initialize argument variables first, then RootBodyTag node
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Aug 18, 2024
1 parent c7a6228 commit 41b3473
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.graalvm.polyglot.Source;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class InsightForEnsoTest {
Expand Down Expand Up @@ -66,7 +65,6 @@ public void disposeContext() throws Exception {
ctx = null;
}

@Ignore
@Test
public void computeFactorial() throws Exception {
var code =
Expand Down Expand Up @@ -95,9 +93,9 @@ public void computeFactorial() throws Exception {
assertNotEquals("4th step: " + msgs, -1, msgs.indexOf("n=2 v=60 acc=function"));

assertNotEquals(
"Uninitialized variables are seen as JavaScript null: " + msgs,
"Uninitialized variables (seen as JavaScript null) aren't there: " + msgs,
-1,
msgs.indexOf("n=null v=null acc=function"));
msgs.indexOf("null"));
}

@Test
Expand Down Expand Up @@ -125,16 +123,19 @@ private void doInstantiateConstructor(boolean useAutoscoping, boolean lazy) thro
Source.newBuilder(
"enso",
"""
id x = x
init_first_switch_arg x = x
type Complex
Number re im
switch n:Complex = Complex.Number n.im n.re
switch_lazy (~n:Complex) = Complex.Number n.im n.re
switch f=(init_first_switch_arg id) n:Complex = Complex.Number (f n.im) (f n.re)
switch_lazy f=(init_first_switch_arg id) (~n:Complex) = Complex.Number (f n.im) (f n.re)
alloc1 a b = Complex.switch (Complex.Number a b)
alloc2 a b = Complex.switch (..Number a b)
alloc3 a b = Complex.switch_lazy (Complex.Number a b)
alloc4 a b = Complex.switch_lazy (..Number a b)
alloc1 a b = Complex.switch n=(Complex.Number a b)
alloc2 a b = Complex.switch n=(..Number a b)
alloc3 a b = Complex.switch_lazy n=(Complex.Number a b)
alloc4 a b = Complex.switch_lazy n=(..Number a b)
""",
"complex.enso")
.build();
Expand All @@ -155,19 +156,27 @@ private void doInstantiateConstructor(boolean useAutoscoping, boolean lazy) thro

var firstCons = msgs.indexOf("complex::complex.Complex::Number");
var secondCons = msgs.lastIndexOf("complex::complex.Complex::Number");
var switchCall = msgs.indexOf("complex::complex.Complex.type::switch");
var switchInitCall = msgs.indexOf("complex::complex::init_first_switch_arg");

assertNotEquals(msgs, -1, switchCall);
assertNotEquals(msgs, -1, switchInitCall);
assertNotEquals(msgs, -1, firstCons);
assertNotEquals(msgs, -1, secondCons);
assertTrue(
"First constructor call must be sooner than second:\n" + msgs, firstCons < secondCons);

if (useAutoscoping || lazy) {
assertTrue("Switch call first and then both constructors:\n" + msgs, switchCall < firstCons);
assertTrue(
"Switch call ("
+ switchInitCall
+ ") first and then both constructors ("
+ firstCons
+ "):\n"
+ msgs,
switchInitCall < firstCons);
} else {
assertTrue("First constructor sooner than switch call:\n" + msgs, firstCons < switchCall);
assertTrue("Switch call sooner than second constructor:\n" + msgs, switchCall < secondCons);
assertTrue("First constructor sooner than switch call:\n" + msgs, firstCons < switchInitCall);
assertTrue(
"Switch call sooner than second constructor:\n" + msgs, switchInitCall < secondCons);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2160,8 +2160,12 @@ class IrToTruffle(
lazy val argsExpr = computeArgsAndExpression()

def args(): Array[ArgumentDefinition] = slots._2
def bodyNode(): RuntimeExpression =
BlockNode.buildRoot(argsExpr._1.toArray, argsExpr._2)
def bodyNode(): RuntimeExpression = {
val body = BlockNode.buildRoot(Array(), argsExpr._2)
val initVariablesAndThenBody =
BlockNode.buildSilent(argsExpr._1.toArray, body)
initVariablesAndThenBody
}

private def computeArgsAndExpression()
: (Array[RuntimeExpression], RuntimeExpression) = {
Expand Down

0 comments on commit 41b3473

Please sign in to comment.