[K/JS] Introduce v2015 target

This commit is contained in:
Artem Kobzar
2024-03-06 12:21:38 +00:00
committed by Space Team
parent e989e6d397
commit 77f0cba23f
19 changed files with 115 additions and 84 deletions
@@ -33,6 +33,7 @@ annotation class GradleOption(
enum class DefaultValue {
BOOLEAN_FALSE_DEFAULT,
BOOLEAN_TRUE_DEFAULT,
BOOLEAN_NULL_DEFAULT,
STRING_NULL_DEFAULT,
EMPTY_STRING_LIST_DEFAULT,
EMPTY_STRING_ARRAY_DEFAULT,
@@ -166,7 +166,7 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
gradleInputType = GradleInputTypes.INPUT,
shouldGenerateDeprecatedKotlinOptions = true,
)
@Argument(value = "-target", valueDescription = "{ v5 }", description = "Generate JS files for the specified ECMA version.")
@Argument(value = "-target", valueDescription = "{ es5, es2015 }", description = "Generate JS files for the specified ECMA version.")
var target: String? = null
set(value) {
checkFrozen()
@@ -192,12 +192,12 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
@Argument(
value = "-module-kind",
valueDescription = "{plain|amd|commonjs|umd|es}",
description = "The kind of JS module generated by the compiler."
description = "The kind of JS module generated by the compiler. ES modules are enabled by default in case of ES2015 target usage"
)
var moduleKind: String? = MODULE_PLAIN
var moduleKind: String? = null
set(value) {
checkFrozen()
field = if (value.isNullOrEmpty()) MODULE_PLAIN else value
field = value
}
@GradleOption(
@@ -460,15 +460,15 @@ In combination with '-meta-info', this generates both IR and pre-IR versions of
}
@GradleOption(
value = DefaultValue.BOOLEAN_FALSE_DEFAULT,
value = DefaultValue.BOOLEAN_NULL_DEFAULT,
gradleInputType = GradleInputTypes.INPUT,
shouldGenerateDeprecatedKotlinOptions = true,
)
@Argument(
value = "-Xes-classes",
description = "Let generated JavaScript code use ES2015 classes."
description = "Let generated JavaScript code use ES2015 classes. Enabled by default in case of ES2015 target usage"
)
var useEsClasses = false
var useEsClasses: Boolean? = null
set(value) {
checkFrozen()
field = value
@@ -486,9 +486,9 @@ In combination with '-meta-info', this generates both IR and pre-IR versions of
@Argument(
value = "-Xes-generators",
description = "Enable ES2015 generator functions usage inside the compiled code"
description = "Enable ES2015 generator functions usage inside the compiled code. Enabled by default in case of ES2015 target usage"
)
var useEsGenerators = false
var useEsGenerators: Boolean? = null
set(value) {
checkFrozen()
field = value
@@ -657,7 +657,7 @@ In combination with '-meta-info', this generates both IR and pre-IR versions of
collector.deprecationWarn(irBaseClassInMetadata, false, "-Xir-base-class-in-metadata")
collector.deprecationWarn(irNewIr2Js, true, "-Xir-new-ir2js")
if (irPerFile && moduleKind != MODULE_ES) {
if (irPerFile && (moduleKind != MODULE_ES && target != ES_2015)) {
collector.report(
CompilerMessageSeverity.ERROR,
"Per-file compilation can't be used with any `moduleKind` except `es` (ECMAScript Modules)"
@@ -26,6 +26,8 @@ public interface K2JsArgumentConstants {
String MODULE_UMD = "umd";
String MODULE_ES = "es";
String ES_2015 = "es2015";
String GRANULARITY_WHOLE_PROGRAM = "whole-program";
String GRANULARITY_PER_MODULE = "per-module";
String GRANULARITY_PER_FILE = "per-file";
@@ -145,6 +145,15 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
}
private val K2JSCompilerArguments.targetVersion: EcmaVersion?
get() {
val targetString = target
return when {
targetString != null -> EcmaVersion.entries.firstOrNull { it.name == targetString }
else -> EcmaVersion.defaultVersion()
}
}
override fun doExecute(
arguments: K2JSCompilerArguments,
configuration: CompilerConfiguration,
@@ -153,6 +162,15 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
): ExitCode {
val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
val targetVersion = arguments.targetVersion?.also {
configuration.put(JSConfigurationKeys.TARGET, it)
}
if (targetVersion == null) {
messageCollector.report(ERROR, "Unsupported ECMA version: ${arguments.target}")
return COMPILATION_ERROR
}
val pluginLoadResult = loadPlugins(paths, arguments, configuration)
if (pluginLoadResult != OK) return pluginLoadResult
@@ -182,7 +200,6 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
configuration.put(JSConfigurationKeys.WASM_USE_TRAPS_INSTEAD_OF_EXCEPTIONS, arguments.wasmUseTrapsInsteadOfExceptions)
configuration.putIfNotNull(JSConfigurationKeys.WASM_TARGET, arguments.wasmTarget?.let(WasmTarget::fromName))
configuration.put(JSConfigurationKeys.USE_ES6_CLASSES, arguments.useEsClasses)
configuration.put(JSConfigurationKeys.OPTIMIZE_GENERATED_JS, arguments.optimizeGeneratedJs)
val commonSourcesArray = arguments.commonSources
@@ -206,14 +223,21 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
val projectJs = environmentForJS.project
val configurationJs = environmentForJS.configuration
val sourcesFiles = environmentForJS.getSourceFiles()
val isES2015 = targetVersion == EcmaVersion.es2015
val moduleKind = configuration[JSConfigurationKeys.MODULE_KIND]
?: moduleKindMap[arguments.moduleKind]
?: ModuleKind.ES.takeIf { isES2015 }
?: ModuleKind.UMD
configurationJs.put(JSConfigurationKeys.MODULE_KIND, moduleKind)
configurationJs.put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage)
configurationJs.put(CLIConfigurationKeys.RENDER_DIAGNOSTIC_INTERNAL_NAME, arguments.renderInternalDiagnosticNames)
configurationJs.put(JSConfigurationKeys.PROPERTY_LAZY_INITIALIZATION, arguments.irPropertyLazyInitialization)
configurationJs.put(JSConfigurationKeys.GENERATE_POLYFILLS, arguments.generatePolyfills)
configurationJs.put(JSConfigurationKeys.GENERATE_DTS, arguments.generateDts)
configurationJs.put(JSConfigurationKeys.COMPILE_SUSPEND_AS_JS_GENERATOR, arguments.useEsGenerators)
configurationJs.put(JSConfigurationKeys.GENERATE_INLINE_ANONYMOUS_FUNCTIONS, arguments.irGenerateInlineAnonymousFunctions)
configurationJs.put(JSConfigurationKeys.USE_ES6_CLASSES, arguments.useEsClasses ?: isES2015)
configurationJs.put(JSConfigurationKeys.COMPILE_SUSPEND_AS_JS_GENERATOR, arguments.useEsGenerators ?: isES2015)
arguments.platformArgumentsProviderJsExpression?.let {
configurationJs.put(JSConfigurationKeys.DEFINE_PLATFORM_MAIN_FUNCTION_ARGUMENTS, it)
@@ -294,8 +318,6 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
}
if (arguments.irProduceJs) {
val moduleKind = configurationJs[JSConfigurationKeys.MODULE_KIND] ?: error("cannot get 'module kind' from configuration")
messageCollector.report(INFO, "Produce executable: $outputDirPath")
messageCollector.report(INFO, "Cache directory: ${arguments.cacheDirectory}")
@@ -686,11 +708,6 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
) {
val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
if (arguments.target != null) {
assert("v5" == arguments.target) { "Unsupported ECMA version: " + arguments.target!! }
}
configuration.put(JSConfigurationKeys.TARGET, EcmaVersion.defaultVersion())
if (arguments.sourceMap) {
configuration.put(JSConfigurationKeys.SOURCE_MAP, true)
if (arguments.sourceMapPrefix != null) {
@@ -733,19 +750,10 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
configuration.put(JSConfigurationKeys.FRIEND_PATHS, friendPaths)
}
val moduleKindName = arguments.moduleKind
var moduleKind: ModuleKind? = if (moduleKindName != null) moduleKindMap[moduleKindName] else ModuleKind.PLAIN
if (moduleKind == null) {
messageCollector.report(
ERROR, "Unknown module kind: $moduleKindName. Valid values are: plain, amd, commonjs, umd, es", null
)
moduleKind = ModuleKind.PLAIN
}
if (arguments.wasm) {
// K/Wasm support ES modules only.
moduleKind = ModuleKind.ES
configuration.put(JSConfigurationKeys.MODULE_KIND, ModuleKind.ES)
}
configuration.put(JSConfigurationKeys.MODULE_KIND, moduleKind)
configuration.putIfNotNull(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER, services[IncrementalDataProvider::class.java])
configuration.putIfNotNull(JSConfigurationKeys.INCREMENTAL_RESULTS_CONSUMER, services[IncrementalResultsConsumer::class.java])