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:
Anton Bannykh
2017-03-30 15:03:38 +03:00
parent 7bbf9861d0
commit 2e9a59819a
48 changed files with 765 additions and 133 deletions
@@ -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>
@@ -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"
}
@@ -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"));