[JS SCRIPTING] moved the javascript engines to a separate module
This commit is contained in:
committed by
romanart
parent
bf0b4f6878
commit
f13e05de7d
@@ -35,10 +35,28 @@ dependencies {
|
||||
testCompile(project(":js:js.translator"))
|
||||
testCompile(project(":js:js.serializer"))
|
||||
testCompile(project(":js:js.dce"))
|
||||
testCompile(project(":js:js.engines"))
|
||||
testCompile(commonDep("junit:junit"))
|
||||
testCompile(projectTests(":kotlin-build-common"))
|
||||
testCompile(projectTests(":generators:test-generator"))
|
||||
|
||||
val currentOs = OperatingSystem.current()
|
||||
|
||||
val j2v8idString = when {
|
||||
currentOs.isWindows -> {
|
||||
val suffix = if (currentOs.toString().endsWith("64")) "_64" else ""
|
||||
"com.eclipsesource.j2v8:j2v8_win32_x86$suffix:4.6.0"
|
||||
}
|
||||
currentOs.isMacOsX -> "com.eclipsesource.j2v8:j2v8_macosx_x86_64:4.6.0"
|
||||
currentOs.run { isLinux || isUnix } -> "com.eclipsesource.j2v8:j2v8_linux_x86_64:4.8.0"
|
||||
else -> {
|
||||
logger.error("unsupported platform $currentOs - can not compile com.eclipsesource.j2v8 dependency")
|
||||
"j2v8:$currentOs"
|
||||
}
|
||||
}
|
||||
|
||||
testCompile(j2v8idString)
|
||||
|
||||
testRuntime(kotlinStdlib())
|
||||
testJsRuntime(kotlinStdlib("js"))
|
||||
testJsRuntime(project(":kotlin-test:kotlin-test-js")) // to be sure that kotlin-test-js built before tests runned
|
||||
@@ -46,18 +64,6 @@ dependencies {
|
||||
testRuntime(project(":kotlin-preloader")) // it's required for ant tests
|
||||
testRuntime(project(":compiler:backend-common"))
|
||||
testRuntime(commonDep("org.fusesource.jansi", "jansi"))
|
||||
|
||||
val currentOs = OperatingSystem.current()
|
||||
|
||||
when {
|
||||
currentOs.isWindows -> {
|
||||
val suffix = if (currentOs.toString().endsWith("64")) "_64" else ""
|
||||
testCompile("com.eclipsesource.j2v8:j2v8_win32_x86$suffix:4.6.0")
|
||||
}
|
||||
currentOs.isMacOsX -> testCompile("com.eclipsesource.j2v8:j2v8_macosx_x86_64:4.6.0")
|
||||
currentOs.run { isLinux || isUnix } -> testCompile("com.eclipsesource.j2v8:j2v8_linux_x86_64:4.8.0")
|
||||
else -> logger.error("unsupported platform $currentOs - can not compile com.eclipsesource.j2v8 dependency")
|
||||
}
|
||||
|
||||
antLauncherJar(commonDep("org.apache.ant", "ant"))
|
||||
antLauncherJar(toolsJar())
|
||||
|
||||
@@ -35,6 +35,8 @@ import org.jetbrains.kotlin.js.config.SourceMapSourceEmbedding
|
||||
import org.jetbrains.kotlin.js.dce.DeadCodeElimination
|
||||
import org.jetbrains.kotlin.js.dce.InputFile
|
||||
import org.jetbrains.kotlin.js.dce.InputResource
|
||||
import org.jetbrains.kotlin.js.engine.ScriptEngineNashorn
|
||||
import org.jetbrains.kotlin.js.engine.ScriptEngineV8Lazy
|
||||
import org.jetbrains.kotlin.js.facade.*
|
||||
import org.jetbrains.kotlin.js.parser.parse
|
||||
import org.jetbrains.kotlin.js.parser.sourcemaps.SourceMapError
|
||||
@@ -43,8 +45,6 @@ import org.jetbrains.kotlin.js.parser.sourcemaps.SourceMapParser
|
||||
import org.jetbrains.kotlin.js.parser.sourcemaps.SourceMapSuccess
|
||||
import org.jetbrains.kotlin.js.sourceMap.SourceFilePathResolver
|
||||
import org.jetbrains.kotlin.js.sourceMap.SourceMap3Builder
|
||||
import org.jetbrains.kotlin.js.test.interop.ScriptEngineNashorn
|
||||
import org.jetbrains.kotlin.js.test.interop.ScriptEngineV8Lazy
|
||||
import org.jetbrains.kotlin.js.test.utils.*
|
||||
import org.jetbrains.kotlin.js.util.TextOutputImpl
|
||||
import org.jetbrains.kotlin.metadata.DebugProtoBuf
|
||||
@@ -869,6 +869,8 @@ abstract class BasicBoxTest(
|
||||
private val OLD_MODULE_SUFFIX = "-old"
|
||||
|
||||
const val KOTLIN_TEST_INTERNAL = "\$kotlin_test_internal\$"
|
||||
private val engineForMinifier = if (runTestInNashorn) ScriptEngineNashorn() else ScriptEngineV8Lazy()
|
||||
private val engineForMinifier =
|
||||
if (runTestInNashorn) ScriptEngineNashorn()
|
||||
else ScriptEngineV8Lazy(KotlinTestUtils.tmpDirForReusableFolder("j2v8_library_path").path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.test
|
||||
|
||||
import org.jetbrains.kotlin.js.test.interop.ScriptEngine
|
||||
import org.jetbrains.kotlin.js.test.interop.ScriptEngineNashorn
|
||||
import org.jetbrains.kotlin.js.test.interop.ScriptEngineV8
|
||||
import org.jetbrains.kotlin.js.engine.ScriptEngine
|
||||
import org.jetbrains.kotlin.js.engine.ScriptEngineNashorn
|
||||
import org.jetbrains.kotlin.js.engine.ScriptEngineV8
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.junit.Assert
|
||||
|
||||
fun createScriptEngine(): ScriptEngine {
|
||||
@@ -180,7 +181,7 @@ object V8JsTestChecker : AbstractV8JsTestChecker() {
|
||||
}
|
||||
|
||||
private fun createV8Engine(): ScriptEngineV8 {
|
||||
val v8 = ScriptEngineV8()
|
||||
val v8 = ScriptEngineV8(KotlinTestUtils.tmpDirForReusableFolder("j2v8_library_path").path)
|
||||
|
||||
listOf(
|
||||
BasicBoxTest.DIST_DIR_JS_PATH + "kotlin.js",
|
||||
@@ -202,7 +203,7 @@ object V8JsTestChecker : AbstractV8JsTestChecker() {
|
||||
}
|
||||
|
||||
object V8IrJsTestChecker : AbstractV8JsTestChecker() {
|
||||
override val engine get() = ScriptEngineV8()
|
||||
override val engine get() = ScriptEngineV8(KotlinTestUtils.tmpDirForReusableFolder("j2v8_library_path").path)
|
||||
|
||||
override fun run(files: List<String>, f: ScriptEngine.() -> Any?): Any? {
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.js.test.interop
|
||||
|
||||
interface ScriptEngine {
|
||||
fun <T> eval(script: String): T
|
||||
fun evalVoid(script: String)
|
||||
fun <T> callMethod(obj: Any, name: String, vararg args: Any?): T
|
||||
fun loadFile(path: String)
|
||||
fun release()
|
||||
fun <T> releaseObject(t: T)
|
||||
|
||||
fun saveState()
|
||||
fun restoreState()
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.js.test.interop
|
||||
|
||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory
|
||||
import jdk.nashorn.internal.runtime.ScriptRuntime
|
||||
import javax.script.Invocable
|
||||
|
||||
class ScriptEngineNashorn : ScriptEngine {
|
||||
private var savedState: Map<String, Any?>? = null
|
||||
|
||||
// TODO use "-strict"
|
||||
private val myEngine = NashornScriptEngineFactory().getScriptEngine("--language=es5", "--no-java", "--no-syntax-extensions")
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> eval(script: String): T {
|
||||
return myEngine.eval(script) as T
|
||||
}
|
||||
|
||||
override fun evalVoid(script: String) {
|
||||
myEngine.eval(script)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> callMethod(obj: Any, name: String, vararg args: Any?): T {
|
||||
return (myEngine as Invocable).invokeMethod(obj, name, *args) as T
|
||||
}
|
||||
|
||||
override fun loadFile(path: String) {
|
||||
evalVoid("load('${path.replace('\\', '/')}');")
|
||||
}
|
||||
|
||||
override fun release() {}
|
||||
override fun <T> releaseObject(t: T) {}
|
||||
|
||||
|
||||
private fun getGlobalState(): MutableMap<String, Any?> = eval("this")
|
||||
|
||||
override fun saveState() {
|
||||
savedState = getGlobalState().toMap()
|
||||
}
|
||||
|
||||
override fun restoreState() {
|
||||
val globalState = getGlobalState()
|
||||
val originalState = savedState!!
|
||||
for (key in globalState.keys) {
|
||||
globalState[key] = originalState[key] ?: ScriptRuntime.UNDEFINED
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.js.test.interop
|
||||
|
||||
import com.eclipsesource.v8.V8
|
||||
import com.eclipsesource.v8.V8Array
|
||||
import com.eclipsesource.v8.V8Object
|
||||
import com.eclipsesource.v8.utils.V8ObjectUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
|
||||
class ScriptEngineV8 : ScriptEngine {
|
||||
companion object {
|
||||
// It's important that this is not created per test, but rather per process.
|
||||
val LIBRARY_PATH_BASE = KotlinTestUtils.tmpDirForReusableFolder("j2v8_library_path").path
|
||||
}
|
||||
|
||||
override fun <T> releaseObject(t: T) {
|
||||
(t as? V8Object)?.release()
|
||||
}
|
||||
|
||||
private var savedState: List<String>? = null
|
||||
|
||||
override fun restoreState() {
|
||||
val scriptBuilder = StringBuilder()
|
||||
|
||||
val globalState = getGlobalPropertyNames()
|
||||
val originalState = savedState!!
|
||||
for (key in globalState) {
|
||||
if (key !in originalState) {
|
||||
scriptBuilder.append("this['$key'] = void 0;\n")
|
||||
}
|
||||
}
|
||||
evalVoid(scriptBuilder.toString())
|
||||
}
|
||||
|
||||
private fun getGlobalPropertyNames(): List<String> {
|
||||
val v8Array = eval<V8Array>("Object.getOwnPropertyNames(this)")
|
||||
@Suppress("UNCHECKED_CAST") val javaArray = V8ObjectUtils.toList(v8Array) as List<String>
|
||||
v8Array.release()
|
||||
return javaArray
|
||||
}
|
||||
|
||||
override fun saveState() {
|
||||
if (savedState == null) {
|
||||
savedState = getGlobalPropertyNames()
|
||||
}
|
||||
}
|
||||
|
||||
private val myRuntime: V8 = V8.createV8Runtime("global", LIBRARY_PATH_BASE)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> eval(script: String): T {
|
||||
return myRuntime.executeScript(script) as T
|
||||
}
|
||||
|
||||
override fun evalVoid(script: String) {
|
||||
return myRuntime.executeVoidScript(script)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> callMethod(obj: Any, name: String, vararg args: Any?): T {
|
||||
if (obj !is V8Object) {
|
||||
throw Exception("InteropV8 can deal only with V8Object")
|
||||
}
|
||||
|
||||
val runtimeArray = V8Array(myRuntime)
|
||||
val result = obj.executeFunction(name, runtimeArray) as T
|
||||
runtimeArray.release()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun loadFile(path: String) {
|
||||
myRuntime.executeVoidScript(File(path).bufferedReader().use { it.readText() }, path, 0)
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
myRuntime.release()
|
||||
}
|
||||
}
|
||||
|
||||
class ScriptEngineV8Lazy : ScriptEngine {
|
||||
override fun <T> eval(script: String) = engine.eval<T>(script)
|
||||
|
||||
override fun saveState() = engine.saveState()
|
||||
|
||||
override fun evalVoid(script: String) = engine.evalVoid(script)
|
||||
|
||||
override fun <T> callMethod(obj: Any, name: String, vararg args: Any?) = engine.callMethod<T>(obj, name, args)
|
||||
|
||||
override fun loadFile(path: String) = engine.loadFile(path)
|
||||
|
||||
override fun release() = engine.release()
|
||||
|
||||
override fun <T> releaseObject(t: T) = engine.releaseObject(t)
|
||||
|
||||
override fun restoreState() = engine.restoreState()
|
||||
|
||||
private val engine by lazy { ScriptEngineV8() }
|
||||
}
|
||||
Reference in New Issue
Block a user