From 0cfacfa44a9c7239fad4f925ca2494eaf45da653 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 12 Apr 2016 16:30:40 +0300 Subject: [PATCH] KT-3008 Merge JS runtime modules in one kotlin.js --- build.xml | 35 ++++++++++++------- .../rhino/RhinoFunctionResultChecker.java | 2 +- .../kotlin/js/test/rhino/RhinoUtils.java | 24 ++----------- .../kotlin/js/facade/K2JSTranslator.java | 4 +-- .../js/translate/context/StandardClasses.java | 4 ++- .../js/translate/context/StaticContext.java | 10 ++---- .../js/translate/general/Translation.java | 9 ++--- .../intrinsic/objects/objectsIntrinsics.kt | 9 +++-- .../js/translate/utils/JsDescriptorUtils.java | 4 +-- js/js.translator/testData/closure-wrapper.txt | 12 +++++-- .../testData/jsTester/jsTester.js | 2 +- js/js.translator/testData/kotlin_lib.js | 32 +++++++++-------- js/js.translator/testData/kotlin_lib_ecma5.js | 23 ++++++------ js/js.translator/testData/maps.js | 15 ++++---- js/js.translator/testData/merge.js | 9 +++++ .../testData/native/cases/eval.kt | 2 +- .../cases/nativePropertyWithCustomName.kt | 2 +- .../testData/native/cases/print.kt | 12 +++---- .../testData/native/native/library.js | 4 +-- .../cases/nativePropertiesNameClashes.kt | 2 +- js/js.translator/testData/save-builtins.js | 2 ++ js/js.translator/testData/save-stdlib.js | 2 ++ .../cases/arrayFactoryMethods.kt | 2 +- js/js.translator/testData/test.html | 8 ++--- .../checkImplementationCharacteristics.kt | 2 +- 25 files changed, 119 insertions(+), 113 deletions(-) create mode 100644 js/js.translator/testData/merge.js create mode 100644 js/js.translator/testData/save-builtins.js create mode 100644 js/js.translator/testData/save-stdlib.js diff --git a/build.xml b/build.xml index 156d0e2aa92..08b67a4ee68 100644 --- a/build.xml +++ b/build.xml @@ -319,7 +319,7 @@ - + @@ -328,22 +328,22 @@ - + - + - - - - - - - + + + + + + + + + + + + + + + + + + @@ -378,10 +389,8 @@ - - - + 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 60f52b89c7c..55ef5293396 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 @@ -58,7 +58,7 @@ public class RhinoFunctionResultChecker implements RhinoResultChecker { protected String functionCallString() { StringBuilder sb = new StringBuilder(); if (packageName != null) { - sb.append("Kotlin.modules"); + sb.append("this"); if (moduleId.contains(".")) { sb.append("['").append(moduleId).append("']"); } else { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java index ea8ce836eb3..41a2f35e57f 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/rhino/RhinoUtils.java @@ -141,7 +141,7 @@ public final class RhinoUtils { ScriptableObject scope = getScope(ecmaVersion, context, jsLibraries); putGlobalVariablesIntoScope(scope, variables); - context.evaluateString(scope, "Kotlin.out = new Kotlin.BufferedOutput();", "setup Kotlin.out", 0, null); + context.evaluateString(scope, "kotlin.out = new kotlin.BufferedOutput();", "setup Kotlin.out", 0, null); for (String filename : fileNames) { runFileWithRhino(filename, context, scope); @@ -172,20 +172,6 @@ public final class RhinoUtils { parentScope = initScope(version, context, jsLibraries); versionToScope.put(version, parentScope); } - else { - NativeObject kotlin = (NativeObject) parentScope.get("Kotlin"); - assert kotlin != null; - NativeObject modules = (NativeObject) kotlin.get("modules"); - assert modules != null; - NativeObject stdlibModule = (NativeObject) modules.get(STDLIB_JS_MODULE_NAME); - NativeObject builtinsModule = (NativeObject) modules.get(BUILTINS_JS_MODULE_NAME); - - NativeObject newModules = new NativeObject(); - newModules.put(STDLIB_JS_MODULE_NAME, newModules, stdlibModule); - newModules.put(BUILTINS_JS_MODULE_NAME, newModules, builtinsModule); - - kotlin.put("modules", kotlin, newModules); - } return parentScope; } @@ -193,13 +179,7 @@ public final class RhinoUtils { private static ScriptableObject initScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List jsLibraries) { ScriptableObject scope = context.initStandardObjects(); try { - context.evaluateString(scope, "var Kotlin = {};", ".", 1, null); - runFileWithRhino(getKotlinLibFile(version), context, scope); - runFileWithRhino(TEST_DATA_DIR_PATH + "kotlin_lib.js", context, scope); - runFileWithRhino(TEST_DATA_DIR_PATH + "maps.js", context, scope); - runFileWithRhino(TEST_DATA_DIR_PATH + "long.js", context, scope); - runFileWithRhino(DIST_DIR_JS_PATH + BUILTINS_JS_FILE_NAME, context, scope); - runFileWithRhino(DIST_DIR_JS_PATH + STDLIB_JS_FILE_NAME, context, scope); + runFileWithRhino(DIST_DIR_JS_PATH + "kotlin.js", context, scope); //runFileWithRhino(pathToTestFilesRoot() + "jshint.js", context, scope); for (String jsLibrary : jsLibraries) { runFileWithRhino(jsLibrary, context, scope); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java index 2e267134f0c..35e79fedd3b 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/facade/K2JSTranslator.java @@ -43,8 +43,8 @@ import static org.jetbrains.kotlin.js.translate.utils.ExpandIsCallsKt.expandIsCa */ public final class K2JSTranslator { - public static final String FLUSH_SYSTEM_OUT = "Kotlin.out.flush();\n"; - public static final String GET_SYSTEM_OUT = "Kotlin.out.buffer;\n"; + public static final String FLUSH_SYSTEM_OUT = "kotlin.out.flush();\n"; + public static final String GET_SYSTEM_OUT = "kotlin.out.buffer;\n"; @NotNull private final JsConfig config; diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StandardClasses.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StandardClasses.java index 1f503498ac4..233d4315147 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StandardClasses.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StandardClasses.java @@ -112,7 +112,9 @@ public final class StandardClasses { standardClasses.declare().forFQ("kotlin.Comparable").kotlinClass("Comparable"); - standardClasses.declare().forFQ("koltin.Throwable").kotlinClass("Throwable"); + standardClasses.declare().forFQ("kotlin.Throwable").kotlinClass("Throwable"); + + standardClasses.declare().forFQ("kotlin.Annotation").kotlinClass("Annotation"); } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java index dbcd6976df2..3bd0ad0355a 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java @@ -545,13 +545,6 @@ public final class StaticContext { JsNameRef result = getQualifierForParentPackage(((PackageFragmentDescriptor) containingDescriptor).getFqName()); - // TODO: remove this dirty hack - if (descriptor instanceof ClassDescriptor && DescriptorUtils.isTopLevelDeclaration(descriptor)) { - if (DescriptorUtils.getFqNameFromTopLevelClass(descriptor).toString().equals("Annotation")) { - return result; - } - } - String moduleName = getExternalModuleName(descriptor); if (moduleName == null) { return result; @@ -561,7 +554,8 @@ public final class StaticContext { return null; } - JsName moduleId = importedModules.get(moduleName); + JsName moduleId = moduleName.equals(Namer.KOTLIN_LOWER_NAME) ? rootScope.declareName(Namer.KOTLIN_NAME) : + importedModules.get(moduleName); if (moduleId == null) { moduleId = rootScope.declareFreshName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(moduleName)); importedModules.put(moduleName, moduleId); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java index 6c84fa91cce..c826bd6782c 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java @@ -289,7 +289,7 @@ public final class Translation { JsBlock amdBody = new JsBlock(wrapAmd(moduleId, factoryName.makeRef(), importedModules, program)); JsBlock commonJsBody = new JsBlock(wrapCommonJs(factoryName.makeRef(), importedModules, program)); - JsInvocation plainInvocation = makePlainInvocation(factoryName.makeRef(), importedModules); + JsInvocation plainInvocation = makePlainInvocation(factoryName.makeRef(), importedModules, program); JsExpression plainExpr = moduleId != null ? JsAstUtils.assignment(new JsNameRef(moduleId, rootName.makeRef()), plainInvocation) : plainInvocation; @@ -356,7 +356,7 @@ public final class Translation { @NotNull List importedModules, @NotNull JsProgram program ) { - JsInvocation invocation = makePlainInvocation(function, importedModules); + JsInvocation invocation = makePlainInvocation(function, importedModules, program); JsStatement statement; if (moduleId == null) { @@ -370,11 +370,12 @@ public final class Translation { } @NotNull - private static JsInvocation makePlainInvocation(@NotNull JsExpression function, @NotNull List importedModules) { + private static JsInvocation makePlainInvocation(@NotNull JsExpression function, @NotNull List importedModules, + JsProgram program) { List invocationArgs = new ArrayList(importedModules.size()); for (ImportedModule importedModule : importedModules) { - invocationArgs.add(importedModule.name.makeRef()); + invocationArgs.add(program.getRootScope().declareName(importedModule.id).makeRef()); } return new JsInvocation(function, invocationArgs); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/objects/objectsIntrinsics.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/objects/objectsIntrinsics.kt index be12e69ba8a..b89c0f6cfdd 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/objects/objectsIntrinsics.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/intrinsic/objects/objectsIntrinsics.kt @@ -23,17 +23,16 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.js.config.LibrarySourcesConfig import org.jetbrains.kotlin.js.resolve.JsPlatform +import org.jetbrains.kotlin.js.translate.context.Namer import org.jetbrains.kotlin.js.translate.context.TranslationContext import org.jetbrains.kotlin.js.translate.utils.JsAstUtils import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name -class DefaultClassObjectIntrinsic(val fqName: FqName, val moduleName: String): ObjectIntrinsic { +class DefaultClassObjectIntrinsic(val fqName: FqName): ObjectIntrinsic { override fun apply(context: TranslationContext): JsExpression { val nameRef = context.getQualifiedReference(fqName) - return JsAstUtils.replaceRootReference( - nameRef, - JsArrayAccess(context.namer().kotlin("modules"), context.program().getStringLiteral(moduleName))) + return JsAstUtils.replaceRootReference(nameRef, Namer.kotlinObject()) } } @@ -46,7 +45,7 @@ class ObjectIntrinsics { val containingDeclaration = classDescriptor.containingDeclaration val name = Name.identifier(containingDeclaration.name.asString() + "CompanionObject") - return DefaultClassObjectIntrinsic(FqName("kotlin.js.internal").child(name), LibrarySourcesConfig.STDLIB_JS_MODULE_NAME) + return DefaultClassObjectIntrinsic(FqName("kotlin.js.internal").child(name)); } } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java index 9882299eb0b..44f1d1ec7dc 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/JsDescriptorUtils.java @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.js.config.LibrarySourcesConfig; import org.jetbrains.kotlin.js.descriptorUtils.DescriptorUtilsKt; +import org.jetbrains.kotlin.js.translate.context.Namer; import org.jetbrains.kotlin.js.translate.context.TranslationContext; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.KtExpression; @@ -38,7 +39,6 @@ import java.util.Collection; import java.util.List; import java.util.Set; -import static org.jetbrains.kotlin.js.config.LibrarySourcesConfig.BUILTINS_JS_MODULE_NAME; import static org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils.isNativeObject; import static org.jetbrains.kotlin.resolve.DescriptorToSourceUtils.descriptorToDeclaration; import static org.jetbrains.kotlin.resolve.DescriptorUtils.*; @@ -185,7 +185,7 @@ public final class JsDescriptorUtils { @Nullable public static String getExternalModuleName(@NotNull DeclarationDescriptor descriptor) { - if (isBuiltin(descriptor)) return BUILTINS_JS_MODULE_NAME; + if (isBuiltin(descriptor)) return Namer.KOTLIN_LOWER_NAME; PsiElement element = descriptorToDeclaration(descriptor); if (element == null && descriptor instanceof PropertyAccessorDescriptor) { diff --git a/js/js.translator/testData/closure-wrapper.txt b/js/js.translator/testData/closure-wrapper.txt index fe5a03e07b4..b6ab157c0f6 100644 --- a/js/js.translator/testData/closure-wrapper.txt +++ b/js/js.translator/testData/closure-wrapper.txt @@ -1,13 +1,19 @@ (function (root, factory) { if (typeof define === 'function' && define.amd) { - define('Kotlin', [], factory); + define('kotlin', [], factory); } else if (typeof exports === 'object') { module.exports = factory(); } else { - root.Kotlin = factory(); + root.kotlin = factory(); } }(this, function () { var Kotlin = {}; + var kotlin = Kotlin; + function require() { + return Kotlin; + } + var module = {}; %output% - return Kotlin; + Kotlin.modules.kotlin = mergedRoot; + return mergedRoot; })); \ No newline at end of file diff --git a/js/js.translator/testData/jsTester/jsTester.js b/js/js.translator/testData/jsTester/jsTester.js index 660ad3cbae6..52c2f02f158 100644 --- a/js/js.translator/testData/jsTester/jsTester.js +++ b/js/js.translator/testData/jsTester/jsTester.js @@ -32,7 +32,7 @@ var JsTests = (function () { var init = function () { init = function() {}; - Kotlin.modules["JS_TESTS"].kotlin.test.init(); + kotlin.modules.JS_TESTS.kotlin.test.init(); }; var test = function (testName, testFun) { diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index 1b2dc121f86..e5b3e2fdc5d 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -274,7 +274,7 @@ */ lazyInitClasses.ArrayIterator = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.MutableIterator]; + return [Kotlin.kotlin.collections.MutableIterator]; }, /** @constructs */ function (array) { @@ -306,7 +306,7 @@ */ lazyInitClasses.ListIterator = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.ListIterator]; // TODO: MutableListIterator + return [Kotlin.kotlin.collections.ListIterator]; // TODO: MutableListIterator }, /** @constructs */ function (list, index) { @@ -340,6 +340,8 @@ } }); + Kotlin.Annotation = Kotlin.createTraitNow(null); + Kotlin.Enum = Kotlin.createClassNow(null, function () { this.name$ = void 0; @@ -380,7 +382,7 @@ lazyInitClasses.AbstractCollection = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.MutableCollection]; + return [Kotlin.kotlin.collections.MutableCollection]; }, null, { addAll_wtfk93$: function (collection) { var modified = false; @@ -474,7 +476,7 @@ */ lazyInitClasses.AbstractList = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.MutableList, Kotlin.AbstractCollection]; + return [Kotlin.kotlin.collections.MutableList, Kotlin.AbstractCollection]; }, null, { iterator: function () { return new Kotlin.ListIterator(this); @@ -792,7 +794,7 @@ lazyInitClasses.RangeIterator = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.Iterator]; + return [Kotlin.kotlin.collections.Iterator]; }, function (start, end, step) { this.start = start; @@ -878,7 +880,7 @@ lazyInitClasses.NumberProgression = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.Iterable]; + return [Kotlin.kotlin.collections.Iterable]; }, function (start, end, step) { this.first = start; @@ -904,7 +906,7 @@ lazyInitClasses.NumberRange = Kotlin.createClass( function() { - return [Kotlin.modules['builtins'].kotlin.ranges.ClosedRange, Kotlin.NumberProgression] + return [Kotlin.kotlin.ranges.ClosedRange, Kotlin.NumberProgression] }, function $fun(start, endInclusive) { $fun.baseInitializer.call(this, start, endInclusive, 1); @@ -934,7 +936,7 @@ lazyInitClasses.LongRangeIterator = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.Iterator]; + return [Kotlin.kotlin.collections.Iterator]; }, function (start, end, step) { this.start = start; @@ -957,7 +959,7 @@ lazyInitClasses.LongProgression = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.Iterable]; + return [Kotlin.kotlin.collections.Iterable]; }, function (start, end, step) { this.first = start; @@ -983,7 +985,7 @@ lazyInitClasses.LongRange = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.ranges.ClosedRange, Kotlin.LongProgression]; + return [Kotlin.kotlin.ranges.ClosedRange, Kotlin.LongProgression]; }, function $fun(start, endInclusive) { $fun.baseInitializer.call(this, start, endInclusive, Kotlin.Long.ONE); @@ -1027,7 +1029,7 @@ lazyInitClasses.CharProgression = Kotlin.createClassNow( function () { - return [Kotlin.modules['builtins'].kotlin.collections.Iterable]; + return [Kotlin.kotlin.collections.Iterable]; }, function (start, end, step) { this.first = start; @@ -1056,7 +1058,7 @@ lazyInitClasses.CharRange = Kotlin.createClass( function() { - return [Kotlin.modules['builtins'].kotlin.ranges.ClosedRange, Kotlin.CharProgression] + return [Kotlin.kotlin.ranges.ClosedRange, Kotlin.CharProgression] }, function $fun(start, endInclusive) { $fun.baseInitializer.call(this, start, endInclusive, 1); @@ -1238,6 +1240,8 @@ } return obj1; }; - - Kotlin.createDefinition(lazyInitClasses, Kotlin); + + Kotlin.lazyInitClasses = Kotlin.lazyInitClasses || []; + Kotlin.lazyInitClasses.push(lazyInitClasses); })(Kotlin); + diff --git a/js/js.translator/testData/kotlin_lib_ecma5.js b/js/js.translator/testData/kotlin_lib_ecma5.js index 6ef1edda8c2..5f7cbdd12c3 100644 --- a/js/js.translator/testData/kotlin_lib_ecma5.js +++ b/js/js.translator/testData/kotlin_lib_ecma5.js @@ -186,7 +186,8 @@ //noinspection JSUnfilteredForInLoop Object.defineProperty(constructor, innerTypeName, { get: innerType, - configurable: true + configurable: true, + enumerable: true }); } } @@ -225,7 +226,7 @@ Kotlin.createClass = function (basesFun, constructor, properties, staticProperties) { function $o() { var klass = Kotlin.createClassNow(getBases(basesFun), constructor, properties, staticProperties); - Object.defineProperty(this, $o.className, {value: klass}); + Object.defineProperty(this, $o.className, {value: klass, enumerable: true}); if (staticProperties && staticProperties.object_initializer$) { staticProperties.object_initializer$(klass); } @@ -301,7 +302,7 @@ Kotlin.createTrait = function (basesFun, properties, staticProperties) { function $o() { var klass = Kotlin.createTraitNow(getBases(basesFun), properties, staticProperties); - Object.defineProperty(this, $o.className, {value: klass}); + Object.defineProperty(this, $o.className, {value: klass, enumerable: true}); return klass; } @@ -324,7 +325,7 @@ var obj = new klass(); var metadata = klass.$metadata$; metadata.type = Kotlin.TYPE.OBJECT; - Object.defineProperty(this, $o.className, {value: obj}); + Object.defineProperty(this, $o.className, {value: obj, enumerable: true}); defineNestedTypes(obj, klass.$metadata$.types); copyProperties(obj, metadata.staticMembers); if (metadata.baseClass != null) { @@ -511,7 +512,8 @@ members[p].className = p; Object.defineProperty(definition, p, { get: members[p], - configurable: true + configurable: true, + enumerable: true }); } else { @@ -536,11 +538,11 @@ Kotlin.definePackage = function (initializer, members) { var definition = createDefinition(members); if (initializer === null) { - return {value: definition}; + return {value: definition, enumerable: true}; } else { var getter = createPackageGetter(definition, initializer); - return {get: getter}; + return {get: getter, enumerable: true}; } }; @@ -561,11 +563,10 @@ * @param {Object} declaration */ Kotlin.defineModule = function (id, declaration) { - if (id in Kotlin.modules) { - throw new Error("Module " + id + " is already defined"); + if (typeof declaration.$initializer$ === "function") { + declaration.$initializer$.call(declaration); // TODO: temporary hack } - declaration.$initializer$.call(declaration); // TODO: temporary hack - Object.defineProperty(Kotlin.modules, id, {value: declaration}); + Kotlin.modules[id] = declaration; }; Kotlin.defineInlineFunction = function(tag, fun) { diff --git a/js/js.translator/testData/maps.js b/js/js.translator/testData/maps.js index af7477b7e84..e3b8fcebfbb 100644 --- a/js/js.translator/testData/maps.js +++ b/js/js.translator/testData/maps.js @@ -14,8 +14,6 @@ * limitations under the License. */ -var Kotlin = Kotlin || {}; - (function (Kotlin) { "use strict"; @@ -523,7 +521,7 @@ var Kotlin = Kotlin || {}; lazyInitClasses.HashMap = Kotlin.createClass( function () { - return [Kotlin.HashTable, Kotlin.modules['builtins'].kotlin.collections.MutableMap]; + return [Kotlin.HashTable, Kotlin.kotlin.collections.MutableMap]; }, function () { Kotlin.HashTable.call(this); @@ -543,7 +541,7 @@ var Kotlin = Kotlin || {}; */ lazyInitClasses.PrimitiveHashMapValuesIterator = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.Iterator]; + return [Kotlin.kotlin.collections.Iterator]; }, function (map, keys) { this.map = map; @@ -828,7 +826,7 @@ var Kotlin = Kotlin || {}; lazyInitClasses.LinkedHashSet = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.MutableSet, Kotlin.HashSet]; + return [Kotlin.kotlin.collections.MutableSet, Kotlin.HashSet]; }, /** @constructs */ function () { @@ -870,7 +868,7 @@ var Kotlin = Kotlin || {}; */ lazyInitClasses.SetIterator = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.MutableIterator]; + return [Kotlin.kotlin.collections.MutableIterator]; }, function (set) { this.set = set; @@ -1117,7 +1115,7 @@ var Kotlin = Kotlin || {}; lazyInitClasses.HashSet = Kotlin.createClass( function () { - return [Kotlin.modules['builtins'].kotlin.collections.MutableSet, Kotlin.AbstractCollection]; + return [Kotlin.kotlin.collections.MutableSet, Kotlin.AbstractCollection]; }, function () { HashSet.call(this); @@ -1126,5 +1124,6 @@ var Kotlin = Kotlin || {}; Object.defineProperty(Kotlin, "ComplexHashSet", { get : function () { return Kotlin.HashSet; }}); - Kotlin.createDefinition(lazyInitClasses, Kotlin); + Kotlin.lazyInitClasses = Kotlin.lazyInitClasses || []; + Kotlin.lazyInitClasses.push(lazyInitClasses); }(Kotlin)); diff --git a/js/js.translator/testData/merge.js b/js/js.translator/testData/merge.js new file mode 100644 index 00000000000..76c6b2703bb --- /dev/null +++ b/js/js.translator/testData/merge.js @@ -0,0 +1,9 @@ +var mergedRoot = Kotlin; +for (var propertyName in stdlib) { + Kotlin[propertyName] = stdlib[propertyName]; +} +(function () { + for (var i = 0; i < Kotlin.lazyInitClasses.length; ++i) { + Kotlin.createDefinition(Kotlin.lazyInitClasses[i], mergedRoot); + } +})(); diff --git a/js/js.translator/testData/native/cases/eval.kt b/js/js.translator/testData/native/cases/eval.kt index 2cf122fe9d6..222b7509d2f 100644 --- a/js/js.translator/testData/native/cases/eval.kt +++ b/js/js.translator/testData/native/cases/eval.kt @@ -9,7 +9,7 @@ fun box(): String { assertEquals(5, eval("3 + 2")) - val PACKAGE = "Kotlin.modules.JS_TESTS.foo" + val PACKAGE = "kotlin.modules.JS_TESTS.foo" assertEquals(top, eval("$PACKAGE.top")) return "OK" diff --git a/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt b/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt index f823ddf3fd0..9a25c9e7005 100644 --- a/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt +++ b/js/js.translator/testData/native/cases/nativePropertyWithCustomName.kt @@ -1,6 +1,6 @@ package foo -internal val PACKAGE = "Kotlin.modules.JS_TESTS.foo" +internal val PACKAGE = "kotlin.modules.JS_TESTS.foo" internal fun funToString(name: String) = eval("$PACKAGE.$name.toString()") as String diff --git a/js/js.translator/testData/native/cases/print.kt b/js/js.translator/testData/native/cases/print.kt index 464a51cdd58..2a2ba881368 100644 --- a/js/js.translator/testData/native/cases/print.kt +++ b/js/js.translator/testData/native/cases/print.kt @@ -23,7 +23,7 @@ var buffer: String = noImpl fun test(expected: String, initCode: String, getResult: () -> String) { buffer = "" - eval("Kotlin.out = new $initCode") + eval("kotlin.out = new $initCode") print("Hello") print(", World") @@ -40,19 +40,19 @@ fun test(expected: String, initCode: String, getResult: () -> String) { } fun box(): String { - test(EXPECTED, "Kotlin.NodeJsOutput(outputStream)") { + test(EXPECTED, "kotlin.NodeJsOutput(outputStream)") { buffer } - test(EXPECTED_NEWLINE_FOR_EACH, "Kotlin.OutputToConsoleLog()") { + test(EXPECTED_NEWLINE_FOR_EACH, "kotlin.OutputToConsoleLog()") { buffer } - test(EXPECTED, "Kotlin.BufferedOutput()") { - eval("Kotlin.out.buffer") as String + test(EXPECTED, "kotlin.BufferedOutput()") { + eval("kotlin.out.buffer") as String } - test(EXPECTED, "Kotlin.BufferedOutputToConsoleLog()") { + test(EXPECTED, "kotlin.BufferedOutputToConsoleLog()") { buffer } diff --git a/js/js.translator/testData/native/native/library.js b/js/js.translator/testData/native/native/library.js index e233e06b9e7..c3230b0ab61 100644 --- a/js/js.translator/testData/native/native/library.js +++ b/js/js.translator/testData/native/native/library.js @@ -1,7 +1,7 @@ (function () { var c = 0; - Kotlin.A = Kotlin.createClassNow(null, + kotlin.A = kotlin.createClassNow(null, function () { this.f = function (i) { if (i === undefined && c === 0) { @@ -13,7 +13,7 @@ } } ); - Kotlin.getResult = function () { + kotlin.getResult = function () { return c === 2; }; })(); diff --git a/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt b/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt index e4daf321568..eabd2294209 100644 --- a/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt +++ b/js/js.translator/testData/propertyAccess/cases/nativePropertiesNameClashes.kt @@ -1,6 +1,6 @@ package foo -val PACKAGE = "Kotlin.modules.JS_TESTS.foo" +val PACKAGE = "kotlin.modules.JS_TESTS.foo" class A @native val Any.__proto__: String get() = noImpl diff --git a/js/js.translator/testData/save-builtins.js b/js/js.translator/testData/save-builtins.js new file mode 100644 index 00000000000..bfbaae9dfd2 --- /dev/null +++ b/js/js.translator/testData/save-builtins.js @@ -0,0 +1,2 @@ +var builtins = module.exports; +delete Kotlin.modules.kotlin; \ No newline at end of file diff --git a/js/js.translator/testData/save-stdlib.js b/js/js.translator/testData/save-stdlib.js new file mode 100644 index 00000000000..f043c4178df --- /dev/null +++ b/js/js.translator/testData/save-stdlib.js @@ -0,0 +1,2 @@ +var stdlib = module.exports; +delete Kotlin.modules.kotlin; \ No newline at end of file diff --git a/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt b/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt index eb62da206ad..4050b2faf4a 100644 --- a/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt +++ b/js/js.translator/testData/standardClasses/cases/arrayFactoryMethods.kt @@ -15,7 +15,7 @@ fun box(): String { test("byteArrayOf", byteArrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4") test("shortArrayOf", shortArrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4") test("intArray,", intArrayOf(0, 1, 2, 3, 4), "0, 1, 2, 3, 4") - test("longArrayOf", longArrayOf(0, 1, 2, 3, 4), "Kotlin.Long.fromInt(0), Kotlin.Long.fromInt(1), Kotlin.Long.fromInt(2), Kotlin.Long.fromInt(3), Kotlin.Long.fromInt(4)") + test("longArrayOf", longArrayOf(0, 1, 2, 3, 4), "kotlin.Long.fromInt(0), kotlin.Long.fromInt(1), kotlin.Long.fromInt(2), kotlin.Long.fromInt(3), kotlin.Long.fromInt(4)") test("floatArrayOf", floatArrayOf(0.0f, 1.0f, 2.0f, 3.0f, 4.0f), "0.0, 1.0, 2.0, 3.0, 4.0") test("doubleArrayOf", doubleArrayOf(0.0, 1.1, 2.2, 3.3, 4.4), "0.0, 1.1, 2.2, 3.3, 4.4") } diff --git a/js/js.translator/testData/test.html b/js/js.translator/testData/test.html index c812f8405d6..aaa9fe9db83 100644 --- a/js/js.translator/testData/test.html +++ b/js/js.translator/testData/test.html @@ -1,13 +1,11 @@ - - - - + + diff --git a/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt b/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt index a97f6dc15d6..f65e6f02727 100644 --- a/js/js.translator/testData/trait/cases/checkImplementationCharacteristics.kt +++ b/js/js.translator/testData/trait/cases/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 = "kotlin.modules.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"