[JS SCRIPTING] create compilers for scripting
This commit is contained in:
committed by
romanart
parent
9b4d92cc07
commit
62885ba497
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.scripting.repl.js
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.*
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
|
||||
class JsDebuggerCompiler(
|
||||
environment: KotlinCoreEnvironment,
|
||||
loadNames: NameTables
|
||||
) : ReplCompiler {
|
||||
// TODO: configure dependencies
|
||||
private val compiler = CoreScriptingJsCompiler(
|
||||
environment,
|
||||
loadNames,
|
||||
readLibrariesFromConfiguration(environment.configuration)
|
||||
)
|
||||
|
||||
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> {
|
||||
return JsState(lock)
|
||||
}
|
||||
|
||||
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult {
|
||||
return ReplCheckResult.Ok()
|
||||
}
|
||||
|
||||
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult {
|
||||
return compiler.compile(codeLine)
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.scripting.repl.js
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.*
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
|
||||
class JsReplCompiler(
|
||||
environment: KotlinCoreEnvironment
|
||||
) : ReplCompiler {
|
||||
private val nameTables: NameTables = NameTables(emptyList())
|
||||
private val dependencies: List<ModuleDescriptor> = readLibrariesFromConfiguration(environment.configuration)
|
||||
|
||||
val scriptDependencyBinary = ScriptDependencyCompiler(environment, nameTables).compile(dependencies).first
|
||||
private val compiler = CoreScriptingJsCompiler(environment, nameTables, dependencies)
|
||||
|
||||
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> {
|
||||
return JsState(lock)
|
||||
}
|
||||
|
||||
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult {
|
||||
return ReplCheckResult.Ok()
|
||||
}
|
||||
|
||||
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult {
|
||||
return compiler.compile(codeLine)
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.scripting.repl.js
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.ReplCompileResult.*
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
|
||||
import kotlin.script.experimental.api.*
|
||||
|
||||
class JsScriptCompiler(
|
||||
environment: KotlinCoreEnvironment
|
||||
) : ScriptCompiler {
|
||||
private val nameTables = NameTables(emptyList())
|
||||
private val dependencies: List<ModuleDescriptor> = readLibrariesFromConfiguration(environment.configuration)
|
||||
private val compiler = CoreScriptingJsCompiler(environment, nameTables, dependencies)
|
||||
|
||||
val scriptDependencyBinary = ScriptDependencyCompiler(environment, nameTables).compile(dependencies).first
|
||||
|
||||
override suspend fun invoke(
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
val compileResult = compiler.compile(makeReplCodeLine(0, script.text))
|
||||
return when (compileResult) {
|
||||
is CompiledClasses -> ResultWithDiagnostics.Success(
|
||||
CompiledToJsScript(compileResult.data as String, scriptCompilationConfiguration)
|
||||
)
|
||||
is Incomplete -> ResultWithDiagnostics.Failure(
|
||||
ScriptDiagnostic("Incomplete code")
|
||||
)
|
||||
is Error -> ResultWithDiagnostics.Failure(
|
||||
ScriptDiagnostic(
|
||||
message = compileResult.message,
|
||||
severity = ScriptDiagnostic.Severity.ERROR
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.scripting.repl.js
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.repl.ReplCompileResult
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.backend.js.emptyLoggingContext
|
||||
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrLinker
|
||||
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsMangler
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
|
||||
class ScriptDependencyCompiler(
|
||||
val environment: KotlinCoreEnvironment,
|
||||
private val nameTables: NameTables = NameTables(emptyList())
|
||||
) {
|
||||
fun compile(dependencies: List<ModuleDescriptor>): Pair<String, NameTables> {
|
||||
val compiler = CoreScriptingJsCompiler(
|
||||
environment,
|
||||
nameTables,
|
||||
dependencies,
|
||||
::createDeserializer
|
||||
)
|
||||
val compileResult = compiler.compile(makeReplCodeLine(0, ""))
|
||||
return (compileResult as ReplCompileResult.CompiledClasses).data as String to nameTables
|
||||
}
|
||||
|
||||
private fun createDeserializer(m: ModuleDescriptor, symbolTable: SymbolTable, irBuiltIns: IrBuiltIns): DeserializerWithDependencies {
|
||||
val deserializer = JsIrLinker(m, JsMangler, emptyLoggingContext, irBuiltIns, symbolTable)
|
||||
val deserializedModuleFragments = m.allDependencyModules.map {
|
||||
deserializer.deserializeFullModule(it)
|
||||
}
|
||||
|
||||
return DeserializerWithDependencies(deserializer, deserializedModuleFragments)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user