From a2f44c3cafbc6060a4f8523ae44f6b9fa08d0fb4 Mon Sep 17 00:00:00 2001 From: Anton Bannykh Date: Fri, 10 Aug 2018 16:21:35 +0300 Subject: [PATCH] JS: Recreate Nashorn script engine occasionally Otherwise execution slows down significantly. --- .../kotlin/js/test/BasicIrBoxTest.kt | 3 +- .../kotlin/js/test/NashornJsTestChecker.kt | 67 ++++++++++++++----- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt index d3057b6cb1f..c0431da232c 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicIrBoxTest.kt @@ -125,8 +125,7 @@ abstract class BasicIrBoxTest( ) { // TODO: should we do anything special for module systems? // TODO: return list of js from translateFiles and provide then to this function with other js files - // TODO: cache runtime.js and don't cache kotlin.js for IR BE tests - super.runGeneratedCode(listOf(runtimeFile.absolutePath) + jsFiles, null, null, testFunction, expectedResult, false) + NashornIrJsTestChecker.check(jsFiles, null, null, testFunction, expectedResult, false) } } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt index ce11f532f94..c29741655ff 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/NashornJsTestChecker.kt @@ -67,6 +67,7 @@ fun ScriptEngine.runAndRestoreContext( val after = globalObject.toMapWithAllMembers() val diff = after.entries - before.entries + diff.forEach { globalObject.put(it.key, before[it.key] ?: ScriptRuntime.UNDEFINED) } @@ -75,11 +76,14 @@ fun ScriptEngine.runAndRestoreContext( private fun ScriptObjectMirror.toMapWithAllMembers(): Map = getOwnKeys(true).associate { it to this[it] } -object NashornJsTestChecker { - val SETUP_KOTLIN_OUTPUT = "kotlin.kotlin.io.output = new kotlin.kotlin.io.BufferedOutput();" - private val GET_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.buffer;" +abstract class AbstractNashornJsTestChecker { - private val engine = createScriptEngineForTest() + private var engineUsageCnt = 0 + + private var engineCache: ScriptEngine? = null + + protected val engine + get() = engineCache ?: createScriptEngineForTest().also { engineCache = it } fun check( files: List, @@ -93,12 +97,6 @@ object NashornJsTestChecker { Assert.assertEquals(expectedResult, actualResult) } - fun checkStdout(files: List, expectedResult: String) { - run(files) - val actualResult = engine.eval(GET_KOTLIN_OUTPUT) - Assert.assertEquals(expectedResult, actualResult) - } - fun run(files: List) { run(files) { null } } @@ -113,24 +111,50 @@ object NashornJsTestChecker { runTestFunction(testModuleName, testPackageName, testFunctionName, withModuleSystem) } + protected open fun beforeRun() {} + private fun run( files: List, f: ScriptEngine.() -> Any? ): Any? { - engine.eval(SETUP_KOTLIN_OUTPUT) + // Recreate the engine once in a while + if (engineUsageCnt++ > 100) { + engineUsageCnt = 0 + engineCache = createScriptEngineForTest() + } + + beforeRun() + return engine.runAndRestoreContext { files.forEach(engine::loadFile) engine.f() } } - private fun createScriptEngineForTest(): ScriptEngine { + abstract protected fun createScriptEngineForTest(): ScriptEngine +} + +object NashornJsTestChecker: AbstractNashornJsTestChecker() { + val SETUP_KOTLIN_OUTPUT = "kotlin.kotlin.io.output = new kotlin.kotlin.io.BufferedOutput();" + private val GET_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.buffer;" + + override fun beforeRun() { + engine.eval(SETUP_KOTLIN_OUTPUT) + } + + fun checkStdout(files: List, expectedResult: String) { + run(files) + val actualResult = engine.eval(GET_KOTLIN_OUTPUT) + Assert.assertEquals(expectedResult, actualResult) + } + + override fun createScriptEngineForTest(): ScriptEngine { val engine = createScriptEngine() listOf( - BasicBoxTest.TEST_DATA_DIR_PATH + "nashorn-polyfills.js", - BasicBoxTest.DIST_DIR_JS_PATH + "kotlin.js", - BasicBoxTest.DIST_DIR_JS_PATH + "kotlin-test.js" + BasicBoxTest.TEST_DATA_DIR_PATH + "nashorn-polyfills.js", + BasicBoxTest.DIST_DIR_JS_PATH + "kotlin.js", + BasicBoxTest.DIST_DIR_JS_PATH + "kotlin-test.js" ).forEach(engine::loadFile) engine.overrideAsserter() @@ -138,3 +162,16 @@ object NashornJsTestChecker { return engine } } + +object NashornIrJsTestChecker: AbstractNashornJsTestChecker() { + override fun createScriptEngineForTest(): ScriptEngine { + val engine = createScriptEngine() + + listOf( + BasicBoxTest.TEST_DATA_DIR_PATH + "nashorn-polyfills.js", + "js/js.translator/testData/out/irBox/testRuntime.js" + ).forEach(engine::loadFile) + + return engine + } +}