From 41b3473437deaa8c17f5841b9c1fa74f6503918f Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Sun, 18 Aug 2024 17:04:26 +0200 Subject: [PATCH] Initialize argument variables first, then RootBodyTag node --- .../interpreter/test/InsightForEnsoTest.java | 39 ++++++++++++------- .../interpreter/runtime/IrToTruffle.scala | 8 +++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/InsightForEnsoTest.java b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/InsightForEnsoTest.java index 4c93b630291c..4f6466d63706 100644 --- a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/InsightForEnsoTest.java +++ b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/InsightForEnsoTest.java @@ -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 { @@ -66,7 +65,6 @@ public void disposeContext() throws Exception { ctx = null; } - @Ignore @Test public void computeFactorial() throws Exception { var code = @@ -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 @@ -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(); @@ -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); } } } diff --git a/engine/runtime/src/main/scala/org/enso/interpreter/runtime/IrToTruffle.scala b/engine/runtime/src/main/scala/org/enso/interpreter/runtime/IrToTruffle.scala index d315e12b902c..454be08d758e 100644 --- a/engine/runtime/src/main/scala/org/enso/interpreter/runtime/IrToTruffle.scala +++ b/engine/runtime/src/main/scala/org/enso/interpreter/runtime/IrToTruffle.scala @@ -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) = {