diff --git a/compiler/testData/integration/ant/js/simpleWithStdlibAndJsFileAsAnotherLibModuleKind/amd.js b/compiler/testData/integration/ant/js/simpleWithStdlibAndJsFileAsAnotherLibModuleKind/amd.js index 4c0dc3b8768..127f8988792 100644 --- a/compiler/testData/integration/ant/js/simpleWithStdlibAndJsFileAsAnotherLibModuleKind/amd.js +++ b/compiler/testData/integration/ant/js/simpleWithStdlibAndJsFileAsAnotherLibModuleKind/amd.js @@ -17,5 +17,10 @@ } define.amd = {}; + function require(name) { + return modules[name]; + } + global.define = define; + global.require = require; })(this); \ No newline at end of file diff --git a/js/js.libraries/src/js/kotlin_lib_ecma5.js b/js/js.libraries/src/js/kotlin_lib_ecma5.js index 2cf01256ce5..f2ed04bec1f 100644 --- a/js/js.libraries/src/js/kotlin_lib_ecma5.js +++ b/js/js.libraries/src/js/kotlin_lib_ecma5.js @@ -272,14 +272,11 @@ ////////////////////////////////// packages & modules ////////////////////////////// - Kotlin.modules = {}; - /** * @param {string} id * @param {Object} declaration */ Kotlin.defineModule = function (id, declaration) { - Kotlin.modules[id] = declaration; }; Kotlin.defineInlineFunction = function(tag, fun) { @@ -303,7 +300,7 @@ return object == null || fn(object); } }; - + Kotlin.andPredicate = function (a, b) { return function (object) { return a(object) && b(object); diff --git a/js/js.tests/test/org/jetbrains/kotlin/integration/AntTaskJsTest.java b/js/js.tests/test/org/jetbrains/kotlin/integration/AntTaskJsTest.java index 73d00da2936..734257fa687 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/integration/AntTaskJsTest.java +++ b/js/js.tests/test/org/jetbrains/kotlin/integration/AntTaskJsTest.java @@ -49,6 +49,10 @@ public class AntTaskJsTest extends AbstractAntTaskTest { } private void doJsAntTest(String... jsFiles) throws Exception { + doJsAntTest(false, jsFiles); + } + + private void doJsAntTest(boolean withModuleSystem, String... jsFiles) throws Exception { doTest(); List fileNames = new ArrayList(Arrays.asList(jsFiles)); @@ -61,7 +65,7 @@ public class AntTaskJsTest extends AbstractAntTaskTest { } }); - RhinoUtils.runRhinoTest(filePaths, new RhinoFunctionResultChecker("out", "foo", "box", "OK")); + RhinoUtils.runRhinoTest(filePaths, new RhinoFunctionResultChecker("out", "foo", "box", "OK", withModuleSystem)); } private void doJsAntTestForPostfixPrefix(@Nullable String prefix, @Nullable String postfix) throws Exception { @@ -121,7 +125,7 @@ public class AntTaskJsTest extends AbstractAntTaskTest { } public void testSimpleWithStdlibAndJsFileAsAnotherLibModuleKind() throws Exception { - doJsAntTest("amd.js", "jslib-example.js"); + doJsAntTest(true, "amd.js", "jslib-example.js"); } public void testSimpleWithStdlibAndTwoJsFilesAsLibraries() throws Exception { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt index ed49adbb93b..b1b46a6035a 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/BasicBoxTest.kt @@ -99,7 +99,6 @@ abstract class BasicBoxTest( val mainModuleName = if (TEST_MODULE in modules) TEST_MODULE else DEFAULT_MODULE val mainModule = modules[mainModuleName]!! - val checker = RhinoFunctionResultChecker(mainModuleName, testFactory.testPackage, TEST_FUNCTION, "OK") val globalCommonFiles = JsTestUtils.getFilesInDirectoryByExtension( TEST_DATA_DIR_PATH + COMMON_FILES_DIR, JavaScript.EXTENSION) val localCommonFile = file.parent + "/" + COMMON_FILES_NAME + JavaScript.DOT_EXTENSION @@ -118,9 +117,13 @@ abstract class BasicBoxTest( } val additionalFiles = mutableListOf() - if ((modules.size > 1 || MODULE_KIND_PATTERN.matcher(expectedText).find()) && - !NO_MODULE_SYSTEM_PATTERN.matcher(expectedText).find() - ) { + + val moduleKindMatcher = MODULE_KIND_PATTERN.matcher(expectedText) + val moduleKind = if (moduleKindMatcher.find()) ModuleKind.valueOf(moduleKindMatcher.group(1)) else ModuleKind.PLAIN + + val withModuleSystem = moduleKind != ModuleKind.PLAIN && !NO_MODULE_SYSTEM_PATTERN.matcher(expectedText).find() + + if (withModuleSystem) { additionalFiles += MODULE_EMULATION_FILE } @@ -138,6 +141,7 @@ abstract class BasicBoxTest( FileUtil.writeToFile(File(nodeRunnerName), nodeRunnerText) } + val checker = RhinoFunctionResultChecker(mainModuleName, testFactory.testPackage, TEST_FUNCTION, "OK", withModuleSystem) RhinoUtils.runRhinoTest(allJsFiles, checker) } } @@ -153,7 +157,7 @@ abstract class BasicBoxTest( val fileName = FileUtil.getRelativePath(dir, File(file))!! sb.append("text += fs.readFileSync(__dirname + \"/$fileName\") + \"\\n\";\n") } - sb.append("text += 'return kotlin.modules.$moduleName;';\n") + sb.append("text += 'return $moduleName;';\n") sb.append("text += \"};\";\n") val fqn = testPackage?.let { ".$it" } ?: "" diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoFunctionResultChecker.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoFunctionResultChecker.java index 4094d6ffcbe..8c6033818db 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoFunctionResultChecker.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoFunctionResultChecker.java @@ -30,12 +30,24 @@ public class RhinoFunctionResultChecker implements RhinoResultChecker { private final String packageName; private final String functionName; private final Object expectedResult; + private final boolean withModuleSystem; public RhinoFunctionResultChecker(@NotNull String moduleId, @Nullable String packageName, @NotNull String functionName, @NotNull Object expectedResult) { + this(moduleId, packageName, functionName, expectedResult, false); + } + + public RhinoFunctionResultChecker( + @NotNull String moduleId, + @Nullable String packageName, + @NotNull String functionName, + @NotNull Object expectedResult, + boolean withModuleSystem + ) { this.moduleId = moduleId; this.packageName = packageName; this.functionName = functionName; this.expectedResult = expectedResult; + this.withModuleSystem = withModuleSystem; } @Override @@ -56,12 +68,17 @@ public class RhinoFunctionResultChecker implements RhinoResultChecker { private String functionCallString() { StringBuilder sb = new StringBuilder(); - sb.append("kotlin.modules"); - if (moduleId.contains(".")) { - sb.append("['").append(moduleId).append("']"); - } else { - sb.append(".").append(moduleId); + + if (withModuleSystem) { + sb.append("require('").append(moduleId).append("')"); } + else if (moduleId.contains(".")) { + sb.append("this['").append(moduleId).append("']"); + } + else { + sb.append(moduleId); + } + if (packageName != null) { sb.append('.').append(packageName); } diff --git a/js/js.translator/testData/box/jsName/avoidNameClash.kt b/js/js.translator/testData/box/jsName/avoidNameClash.kt index fdec988e604..6d8589faccd 100644 --- a/js/js.translator/testData/box/jsName/avoidNameClash.kt +++ b/js/js.translator/testData/box/jsName/avoidNameClash.kt @@ -7,7 +7,7 @@ object A { } fun test() = js(""" -var a = Kotlin.modules.JS_TESTS.foo.A; +var a = JS_TESTS.foo.A; return a.js_method() + ";" + a.js_property; """) diff --git a/js/js.translator/testData/box/jsName/classes.kt b/js/js.translator/testData/box/jsName/classes.kt index f414347b75f..a6ff65e9222 100644 --- a/js/js.translator/testData/box/jsName/classes.kt +++ b/js/js.translator/testData/box/jsName/classes.kt @@ -9,12 +9,12 @@ package foo } fun testA() = js(""" -var a = Kotlin.modules.JS_TESTS.foo.AA; +var a = JS_TESTS.foo.AA; return a.foo(); """) fun testB() = js(""" -var b = new Kotlin.modules.JS_TESTS.foo.BB(); +var b = new JS_TESTS.foo.BB(); return b.foo(); """) diff --git a/js/js.translator/testData/box/jsName/overriddenMethod.kt b/js/js.translator/testData/box/jsName/overriddenMethod.kt index 55d5e634bc4..1261ea926a3 100644 --- a/js/js.translator/testData/box/jsName/overriddenMethod.kt +++ b/js/js.translator/testData/box/jsName/overriddenMethod.kt @@ -9,7 +9,7 @@ class B : A() { } fun test() = js(""" -var module = Kotlin.modules.JS_TESTS.foo; +var module = JS_TESTS.foo; return new (module.A)().js_f(23) + ";" + new (module.B)().js_f(42); """) diff --git a/js/js.translator/testData/box/jsName/overridenFromInterface.kt b/js/js.translator/testData/box/jsName/overridenFromInterface.kt index 482977f9def..217de4c9b1a 100644 --- a/js/js.translator/testData/box/jsName/overridenFromInterface.kt +++ b/js/js.translator/testData/box/jsName/overridenFromInterface.kt @@ -9,7 +9,7 @@ class B : A { } fun test() = js(""" -var module = Kotlin.modules.JS_TESTS.foo; +var module = JS_TESTS.foo; return new (module.B)().js_f(23); """) diff --git a/js/js.translator/testData/box/jsName/privateMethod.kt b/js/js.translator/testData/box/jsName/privateMethod.kt index b64197c5f99..46c05a0d218 100644 --- a/js/js.translator/testData/box/jsName/privateMethod.kt +++ b/js/js.translator/testData/box/jsName/privateMethod.kt @@ -5,7 +5,7 @@ object A { } fun test() = js(""" -return Kotlin.modules.JS_TESTS.foo.A.js_f(23); +return JS_TESTS.foo.A.js_f(23); """) fun box(): String { diff --git a/js/js.translator/testData/box/jsName/secondaryConstructor.kt b/js/js.translator/testData/box/jsName/secondaryConstructor.kt index 38c1230bb4f..8902fcaa6fa 100644 --- a/js/js.translator/testData/box/jsName/secondaryConstructor.kt +++ b/js/js.translator/testData/box/jsName/secondaryConstructor.kt @@ -5,7 +5,7 @@ class A(val x: String) { } fun test() = js(""" -return Kotlin.modules.JS_TESTS.foo.A_int(23).x; +return JS_TESTS.foo.A_int(23).x; """) fun box(): String { diff --git a/js/js.translator/testData/box/jsName/simpleJsName.kt b/js/js.translator/testData/box/jsName/simpleJsName.kt index 57ad4eac342..6da124c507f 100644 --- a/js/js.translator/testData/box/jsName/simpleJsName.kt +++ b/js/js.translator/testData/box/jsName/simpleJsName.kt @@ -11,7 +11,7 @@ object A { } fun test() = js(""" -var a = Kotlin.modules.JS_TESTS.foo.A; +var a = JS_TESTS.foo.A; return a.js_f(23) + ";" + a.js_g(42) + ";" + a.js_p + ";" + a.js_q; """) diff --git a/js/js.translator/testData/box/native/eval.kt b/js/js.translator/testData/box/native/eval.kt index 222b7509d2f..463abf9abea 100644 --- a/js/js.translator/testData/box/native/eval.kt +++ b/js/js.translator/testData/box/native/eval.kt @@ -9,7 +9,7 @@ fun box(): String { assertEquals(5, eval("3 + 2")) - val PACKAGE = "kotlin.modules.JS_TESTS.foo" + val PACKAGE = "JS_TESTS.foo" assertEquals(top, eval("$PACKAGE.top")) return "OK" diff --git a/js/js.translator/testData/box/propertyAccess/accessorsWithJsName.kt b/js/js.translator/testData/box/propertyAccess/accessorsWithJsName.kt index d5dc063caad..38b4d626954 100644 --- a/js/js.translator/testData/box/propertyAccess/accessorsWithJsName.kt +++ b/js/js.translator/testData/box/propertyAccess/accessorsWithJsName.kt @@ -14,7 +14,7 @@ class A { val A.z: Int @JsName("getZ_") get() = 42 -fun getPackage() = js("return Kotlin.modules.JS_TESTS.foo") +fun getPackage() = js("return JS_TESTS.foo") fun box(): String { val a = A() diff --git a/js/js.translator/testData/box/propertyAccess/staticAccessorsWithJsName.kt b/js/js.translator/testData/box/propertyAccess/staticAccessorsWithJsName.kt index bc75565d35f..9cb8950b3d0 100644 --- a/js/js.translator/testData/box/propertyAccess/staticAccessorsWithJsName.kt +++ b/js/js.translator/testData/box/propertyAccess/staticAccessorsWithJsName.kt @@ -10,7 +10,7 @@ var y: Int = 0 } -fun getPackage() = js("return Kotlin.modules.JS_TESTS.foo") +fun getPackage() = js("return JS_TESTS.foo") fun box(): String { assertEquals(23, x) diff --git a/js/js.translator/testData/box/propertyAccess/subclassAccessorsWithJsNameInSuper.kt b/js/js.translator/testData/box/propertyAccess/subclassAccessorsWithJsNameInSuper.kt index 308b338fd1b..26d9238a8f3 100644 --- a/js/js.translator/testData/box/propertyAccess/subclassAccessorsWithJsNameInSuper.kt +++ b/js/js.translator/testData/box/propertyAccess/subclassAccessorsWithJsNameInSuper.kt @@ -28,7 +28,7 @@ class B : A(), C { override val z = 55 } -fun getPackage() = js("return Kotlin.modules.JS_TESTS.foo") +fun getPackage() = js("return JS_TESTS.foo") fun box(): String { val a = B() diff --git a/js/js.translator/testData/box/trait/checkImplementationCharacteristics.kt b/js/js.translator/testData/box/trait/checkImplementationCharacteristics.kt index bbe65c1c525..95c62d22c36 100644 --- a/js/js.translator/testData/box/trait/checkImplementationCharacteristics.kt +++ b/js/js.translator/testData/box/trait/checkImplementationCharacteristics.kt @@ -25,7 +25,7 @@ fun box(): String { if (!hasProp(b, "foo")) return "B hasn't foo" if (!hasProp(b, "boo")) return "B hasn't boo" - val PREFIX = "kotlin.modules.JS_TESTS.foo" + val PREFIX = "JS_TESTS.foo" if (eval("$PREFIX.A") == null) return "$PREFIX.A not found" if (eval("$PREFIX.B") == null) return "$PREFIX.B not found" if (eval("$PREFIX.A === $PREFIX.B") as Boolean) return "A and B refer to the same object" diff --git a/js/js.translator/testData/jsTester/jsTester.js b/js/js.translator/testData/jsTester/jsTester.js index 52c2f02f158..096abbd4b0a 100644 --- a/js/js.translator/testData/jsTester/jsTester.js +++ b/js/js.translator/testData/jsTester/jsTester.js @@ -30,14 +30,7 @@ var JsTests = (function () { throw failedTest; }; - var init = function () { - init = function() {}; - kotlin.modules.JS_TESTS.kotlin.test.init(); - }; - var test = function (testName, testFun) { - init(); - reporter.testStart(testName); try { testFun(); diff --git a/js/js.translator/testData/jsTester/jsTester.kt b/js/js.translator/testData/jsTester/jsTester.kt index 3ed9bd6ac72..0339cd64b0d 100644 --- a/js/js.translator/testData/jsTester/jsTester.kt +++ b/js/js.translator/testData/jsTester/jsTester.kt @@ -1,6 +1,6 @@ package kotlin.test -fun init() { +val init = run { asserter = JsTestsAsserter() }