JS: support internal visibility from friend modules
Friend modules should be provided using the -Xfriend-modules flag in the same format as -libraries. No manual configuration required for JPS, Gradle and Maven plugins. Friend modules could be switched off using the -Xfriend-modules-disabled flag. Doing that will * prevent internal declarations from being exported, * values provided by -Xfriend-modules ignored, * raise a compilation error on attemps to use internal declarations from other modules Fixes #KT-15135 and #KT-16568.
This commit is contained in:
+9
@@ -124,6 +124,15 @@ class Kotlin2JsGradlePluginIT : BaseGradleIT() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCompilerTestAccessInternalProduction() {
|
||||
val project = Project("kotlin2JsInternalTest", "2.10")
|
||||
|
||||
project.build("runRhino") {
|
||||
assertSuccessful()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testJsCustomSourceSet() {
|
||||
val project = Project("kotlin2JsProjectWithCustomSourceset", "2.10")
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin2js'
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
|
||||
compile "org.mozilla:rhino:1.7.7.1"
|
||||
}
|
||||
|
||||
task runRhino(type: JavaExec) {
|
||||
classpath = sourceSets.main.runtimeClasspath
|
||||
workingDir = "${buildDir}/classes/"
|
||||
main = 'org.mozilla.javascript.tools.shell.Main'
|
||||
args = ["-opt", "-1", "-f", "kotlin.js", "-f", "main/kotlin2JsInternalTest_main.js", "-f", "test/kotlin2JsInternalTest_test.js", "-f", "check.js"]
|
||||
}
|
||||
|
||||
build.doLast {
|
||||
configurations.compile.each { File file ->
|
||||
copy {
|
||||
includeEmptyDirs = false
|
||||
|
||||
from zipTree(file.absolutePath)
|
||||
into "${buildDir}/classes/"
|
||||
include { fileTreeElement ->
|
||||
def path = fileTreeElement.path
|
||||
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
|
||||
}
|
||||
}
|
||||
}
|
||||
copy {
|
||||
from "."
|
||||
include "check.js"
|
||||
into "${buildDir}/classes/"
|
||||
}
|
||||
}
|
||||
|
||||
runRhino.dependsOn build
|
||||
+1
@@ -0,0 +1 @@
|
||||
kotlin2JsInternalTest_test.test();
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
internal val CONST = "CONST"
|
||||
|
||||
open class PublicClass {
|
||||
internal fun foo(): String = "foo"
|
||||
internal val bar: String = "bar"
|
||||
open internal fun baz(): String = "PublicClass.baz()"
|
||||
}
|
||||
|
||||
internal data class InternalDataClass(val x: Int, val y: Int)
|
||||
|
||||
internal fun box(): String {
|
||||
return "OK"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
class PublicClassHeir : PublicClass() {
|
||||
override internal fun baz(): String = "PublicClassHeir.baz()"
|
||||
}
|
||||
|
||||
fun <T> assertEquals(e: T, a: T) {
|
||||
if (e != a) throw Exception("Expected: $e, actual: $a")
|
||||
}
|
||||
|
||||
fun test() {
|
||||
assertEquals("CONST", CONST)
|
||||
|
||||
assertEquals("foo", PublicClass().foo())
|
||||
assertEquals("bar", PublicClass().bar)
|
||||
assertEquals("PublicClass.baz()", PublicClass().baz())
|
||||
|
||||
assertEquals("foo", PublicClassHeir().foo())
|
||||
assertEquals("bar", PublicClassHeir().bar)
|
||||
assertEquals("PublicClassHeir.baz()", PublicClassHeir().baz())
|
||||
|
||||
val data = InternalDataClass(10, 20)
|
||||
assertEquals(10, data.x)
|
||||
assertEquals(20, data.y)
|
||||
|
||||
assertEquals("OK", box())
|
||||
}
|
||||
+6
@@ -4,6 +4,12 @@ package org.jetbrains.kotlin.gradle.dsl
|
||||
|
||||
interface KotlinJsOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions {
|
||||
|
||||
/**
|
||||
* Disable internal declaration export
|
||||
* Default value: false
|
||||
*/
|
||||
var friendModulesDisabled: kotlin.Boolean
|
||||
|
||||
/**
|
||||
* Whether a main function should be called
|
||||
* Possible values: "call", "noCall"
|
||||
|
||||
+7
@@ -24,6 +24,11 @@ internal abstract class KotlinJsOptionsBase : org.jetbrains.kotlin.gradle.dsl.Ko
|
||||
get() = verboseField ?: false
|
||||
set(value) { verboseField = value }
|
||||
|
||||
private var friendModulesDisabledField: kotlin.Boolean? = null
|
||||
override var friendModulesDisabled: kotlin.Boolean
|
||||
get() = friendModulesDisabledField ?: false
|
||||
set(value) { friendModulesDisabledField = value }
|
||||
|
||||
private var mainField: kotlin.String? = null
|
||||
override var main: kotlin.String
|
||||
get() = mainField ?: "call"
|
||||
@@ -69,6 +74,7 @@ internal abstract class KotlinJsOptionsBase : org.jetbrains.kotlin.gradle.dsl.Ko
|
||||
languageVersionField?.let { args.languageVersion = it }
|
||||
suppressWarningsField?.let { args.suppressWarnings = it }
|
||||
verboseField?.let { args.verbose = it }
|
||||
friendModulesDisabledField?.let { args.friendModulesDisabled = it }
|
||||
mainField?.let { args.main = it }
|
||||
metaInfoField?.let { args.metaInfo = it }
|
||||
moduleKindField?.let { args.moduleKind = it }
|
||||
@@ -85,6 +91,7 @@ internal fun org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments.fil
|
||||
languageVersion = "1.1"
|
||||
suppressWarnings = false
|
||||
verbose = false
|
||||
friendModulesDisabled = false
|
||||
main = "call"
|
||||
metaInfo = true
|
||||
moduleKind = "plain"
|
||||
|
||||
+2
@@ -372,6 +372,8 @@ open class Kotlin2JsCompile() : AbstractKotlinCompile<K2JSCompilerArguments>(),
|
||||
null
|
||||
}
|
||||
|
||||
args.friendModules = friendDependency
|
||||
|
||||
logger.kotlinDebug("compiling with args ${ArgumentUtils.convertArgumentsToStringList(args)}")
|
||||
|
||||
val messageCollector = GradleMessageCollector(logger)
|
||||
|
||||
@@ -106,13 +106,6 @@
|
||||
<plugin>
|
||||
<artifactId>maven-invoker-plugin</artifactId>
|
||||
<version>1.9</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mozilla</groupId>
|
||||
<artifactId>rhino</artifactId>
|
||||
<version>1.7.7.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<projectsDirectory>src/it</projectsDirectory>
|
||||
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
this['test-js-accessToInternal-tests'].org.jetbrains.test();
|
||||
@@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>test-js-accessToInternal</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-js</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<goals>
|
||||
<goal>js</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<output>${project.basedir}/customOutput/</output>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<goals>
|
||||
<goal>test-js</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>2.10</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>unpack</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>unpack</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-js</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<type>jar</type>
|
||||
<overWrite>false</overWrite>
|
||||
<outputDirectory>${project.build.directory}/js/</outputDirectory>
|
||||
<includes>**/*.js</includes>
|
||||
</artifactItem>
|
||||
</artifactItems>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains
|
||||
|
||||
internal val CONST = "CONST"
|
||||
|
||||
open class PublicClass {
|
||||
internal fun foo(): String = "foo"
|
||||
internal val bar: String = "bar"
|
||||
open internal fun baz(): String = "PublicClass.baz()"
|
||||
}
|
||||
|
||||
internal data class InternalDataClass(val x: Int, val y: Int)
|
||||
|
||||
internal fun box(): String {
|
||||
return "OK"
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains
|
||||
|
||||
class PublicClassHeir : PublicClass() {
|
||||
override internal fun baz(): String = "PublicClassHeir.baz()"
|
||||
}
|
||||
|
||||
fun <T> assertEquals(e: T, a: T) {
|
||||
if (e != a) throw Exception("Expected: $e, actual: $a")
|
||||
}
|
||||
|
||||
fun test() {
|
||||
assertEquals("CONST", CONST)
|
||||
|
||||
assertEquals("foo", PublicClass().foo())
|
||||
assertEquals("bar", PublicClass().bar)
|
||||
assertEquals("PublicClass.baz()", PublicClass().baz())
|
||||
|
||||
assertEquals("foo", PublicClassHeir().foo())
|
||||
assertEquals("bar", PublicClassHeir().bar)
|
||||
assertEquals("PublicClassHeir.baz()", PublicClassHeir().baz())
|
||||
|
||||
val data = InternalDataClass(10, 20)
|
||||
assertEquals(10, data.x)
|
||||
assertEquals(20, data.y)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import java.io.*;
|
||||
import javax.script.*;
|
||||
|
||||
File file = new File(basedir, "target/js/test-js-accessToInternal.js");
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
throw new FileNotFoundException("Could not find generated JS : " + file);
|
||||
}
|
||||
|
||||
File testFile = new File(basedir, "target/test-js/test-js-accessToInternal-tests.js");
|
||||
if (!testFile.exists() || !testFile.isFile()) {
|
||||
throw new FileNotFoundException("Could not find generated JS : " + testFile);
|
||||
}
|
||||
|
||||
String basePath = basedir.getPath();
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
|
||||
engine.eval(new FileReader(basePath + "/target/js/kotlin.js"));
|
||||
engine.eval(new FileReader(basePath + "/target/js/test-js-accessToInternal.js"));
|
||||
engine.eval(new FileReader(basePath + "/target/test-js/test-js-accessToInternal-tests.js"));
|
||||
engine.eval(new FileReader(basePath + "/check.js"));
|
||||
@@ -1,5 +1,5 @@
|
||||
import java.io.*;
|
||||
import org.mozilla.javascript.tools.shell.Main;
|
||||
import javax.script.*;
|
||||
|
||||
File file = new File(basedir, "target/js/test-js-moduleKind.js");
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
@@ -7,9 +7,9 @@ if (!file.exists() || !file.isFile()) {
|
||||
}
|
||||
|
||||
String basePath = basedir.getPath();
|
||||
Main.main(new String[] {
|
||||
"-O", "-1",
|
||||
"-f", basePath + "/amd.js",
|
||||
"-f", basePath + "/target/js/kotlin.js",
|
||||
"-f", basePath + "/target/js/test-js-moduleKind.js",
|
||||
"-f", basePath + "/check.js"})
|
||||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
|
||||
|
||||
engine.eval(new FileReader(basePath + "/amd.js"));
|
||||
engine.eval(new FileReader(basePath + "/target/js/kotlin.js"));
|
||||
engine.eval(new FileReader(basePath + "/target/js/test-js-moduleKind.js"));
|
||||
engine.eval(new FileReader(basePath + "/check.js"));
|
||||
+20
-20
@@ -34,9 +34,11 @@ import org.jetbrains.kotlin.js.JavaScript;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentSkipListSet;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@@ -109,15 +111,11 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
|
||||
|
||||
arguments.sourceMap = sourceMap;
|
||||
|
||||
Set<String> collector = getOutputDirectoriesCollector();
|
||||
|
||||
if (outputFile != null) {
|
||||
collector.add(new File(outputFile).getParent());
|
||||
}
|
||||
if (metaInfo) {
|
||||
String output = com.google.common.base.Objects.firstNonNull(outputFile, ""); // fqname here because of J8 compatibility issues
|
||||
String metaFile = StringsKt.substringBeforeLast(output, JavaScript.DOT_EXTENSION, output) + KotlinJavascriptMetadataUtils.META_JS_SUFFIX;
|
||||
collector.add(new File(metaFile).getParent());
|
||||
ConcurrentMap<String, List<String>> collector = getOutputDirectoriesCollector();
|
||||
String key = project.getArtifactId();
|
||||
List<String> paths = collector.computeIfAbsent(key, k -> Collections.synchronizedList(new ArrayList<String>()));
|
||||
paths.add(new File(outputFile).getParent());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,14 +143,16 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
|
||||
}
|
||||
}
|
||||
|
||||
for (String path : getOutputDirectoriesCollector()) {
|
||||
File file = new File(path);
|
||||
for (List<String> paths : getOutputDirectoriesCollector().values()) {
|
||||
for (String path : paths) {
|
||||
File file = new File(path);
|
||||
|
||||
if (file.exists() && LibraryUtils.isKotlinJavascriptLibrary(file)) {
|
||||
libraries.add(file.getAbsolutePath());
|
||||
}
|
||||
else {
|
||||
getLog().debug("JS output directory missing: " + file);
|
||||
if (file.exists() && LibraryUtils.isKotlinJavascriptLibrary(file)) {
|
||||
libraries.add(file.getAbsolutePath());
|
||||
}
|
||||
else {
|
||||
getLog().debug("JS output directory missing: " + file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,12 +177,12 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Set<String> getOutputDirectoriesCollector() {
|
||||
protected ConcurrentMap<String, List<String>> getOutputDirectoriesCollector() {
|
||||
lock.lock();
|
||||
try {
|
||||
Set<String> collector = (Set<String>) getPluginContext().get(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME);
|
||||
ConcurrentMap<String, List<String>> collector = (ConcurrentMap<String, List<String>>) getPluginContext().get(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME);
|
||||
if (collector == null) {
|
||||
collector = new ConcurrentSkipListSet<String>();
|
||||
collector = new ConcurrentSkipListMap<String, List<String>>();
|
||||
getPluginContext().put(OUTPUT_DIRECTORIES_COLLECTOR_PROPERTY_NAME, collector);
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.maven;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.apache.maven.artifact.DependencyResolutionRequiredException;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
@@ -27,8 +28,9 @@ import org.apache.maven.project.MavenProject;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.Collections;
|
||||
/**
|
||||
* Converts Kotlin to JavaScript code
|
||||
*
|
||||
@@ -79,6 +81,8 @@ public class KotlinTestJSCompilerMojo extends K2JSCompilerMojo {
|
||||
|
||||
@Override
|
||||
protected void configureSpecificCompilerArguments(@NotNull K2JSCompilerArguments arguments) throws MojoExecutionException {
|
||||
List<String> friends = getOutputDirectoriesCollector().getOrDefault(project.getArtifactId(), Collections.emptyList());
|
||||
arguments.friendModules = StringUtil.join(friends, File.pathSeparator);
|
||||
output = testOutput;
|
||||
|
||||
super.configureSpecificCompilerArguments(arguments);
|
||||
|
||||
Reference in New Issue
Block a user