diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/TopLevelFIF.java b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/TopLevelFIF.java index 1deee00a1d5..a1602b05763 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/TopLevelFIF.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/TopLevelFIF.java @@ -19,7 +19,9 @@ package org.jetbrains.k2js.translate.intrinsic.functions.factories; import com.google.dart.compiler.backend.js.ast.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.psi.JetQualifiedExpression; @@ -29,6 +31,7 @@ import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue; import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.PrimitiveType; import org.jetbrains.k2js.translate.callTranslator.CallInfo; import org.jetbrains.k2js.translate.context.Namer; @@ -39,7 +42,9 @@ import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate; import org.jetbrains.k2js.translate.utils.AnnotationsUtils; import org.jetbrains.k2js.translate.utils.BindingUtils; import org.jetbrains.k2js.translate.utils.JsDescriptorUtils; +import org.jetbrains.k2js.translate.utils.TranslationUtils; +import java.util.Collection; import java.util.List; import static org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic.CallParametersAwareFunctionIntrinsic; @@ -77,8 +82,8 @@ public final class TopLevelFIF extends CompositeFIF { private static final FunctionIntrinsic NATIVE_MAP_GET = new NativeMapGetSet() { @NotNull @Override - protected String operation() { - return "get_s9cetl$"; + protected String operationName() { + return "get"; } @Nullable @@ -101,8 +106,8 @@ public final class TopLevelFIF extends CompositeFIF { private static final FunctionIntrinsic NATIVE_MAP_SET = new NativeMapGetSet() { @NotNull @Override - protected String operation() { - return "put_5yfy9u$"; + protected String operationName() { + return "put"; } @Nullable @@ -122,6 +127,13 @@ public final class TopLevelFIF extends CompositeFIF { } }; + @NotNull + private static String getStableMangledBuiltInName(@NotNull ClassDescriptor descriptor, @NotNull String functionName) { + Collection functions = descriptor.getDefaultType().getMemberScope().getFunctions(Name.identifier(functionName)); + assert functions.size() == 1 : "Can't select a single function: " + functionName + " in " + descriptor; + return TranslationUtils.getMangledName(functions.iterator().next()); + } + private static final FunctionIntrinsic PROPERTY_METADATA_IMPL = new FunctionIntrinsic() { @NotNull @Override @@ -162,7 +174,7 @@ public final class TopLevelFIF extends CompositeFIF { private abstract static class NativeMapGetSet extends CallParametersAwareFunctionIntrinsic { @NotNull - protected abstract String operation(); + protected abstract String operationName(); @Nullable protected abstract ExpressionReceiver getExpressionReceiver(@NotNull ResolvedCall resolvedCall); @@ -201,7 +213,9 @@ public final class TopLevelFIF extends CompositeFIF { } } - return new JsInvocation(new JsNameRef(operation(), thisOrReceiver), arguments); + String mangledName = getStableMangledBuiltInName(KotlinBuiltIns.getInstance().getMutableMap(), operationName()); + + return new JsInvocation(new JsNameRef(mangledName, thisOrReceiver), arguments); } } diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index e922cf92c9b..7b44d3e7acd 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -39,8 +39,8 @@ String.prototype.contains = function (s) { return Kotlin.arrayEquals(obj1, obj2); } - if (typeof obj1 == "object" && obj1.equals_s9cetl$ !== undefined) { - return obj1.equals_s9cetl$(obj2); + if (typeof obj1 == "object" && obj1.equals_za3rmp$ !== undefined) { + return obj1.equals_za3rmp$(obj2); } return obj1 === obj2; @@ -168,7 +168,7 @@ String.prototype.contains = function (s) { } } enumEntryList.values$ = values; - enumEntryList.valueOf_e51dcq$ = valueOf; + enumEntryList.valueOf_61zpoe$ = valueOf; enumEntryList.values = getValues; return enumEntryList; }; @@ -181,41 +181,41 @@ String.prototype.contains = function (s) { ); Kotlin.AbstractCollection = Kotlin.createClassNow(Kotlin.Collection, null, { - addAll_5ib00d$: function (collection) { + addAll_xeylzf$: function (collection) { var modified = false; var it = collection.iterator(); while (it.hasNext()) { - if(this.add_s9cetl$(it.next())) + if(this.add_za3rmp$(it.next())) modified = true; } return modified }, - removeAll_5ib00d$: function (c) { + removeAll_xeylzf$: function (c) { var modified = false; var it = this.iterator(); while (it.hasNext()) { - if(c.contains_s9cetl$(it.next())) { + if(c.contains_za3rmp$(it.next())) { it.remove(); modified = true; } } return modified }, - retainAll_5ib00d$: function (c) { + retainAll_xeylzf$: function (c) { var modified = false; var it = this.iterator(); while (it.hasNext()) { - if(!c.contains_s9cetl$(it.next())) { + if(!c.contains_za3rmp$(it.next())) { it.remove(); modified = true; } } return modified }, - containsAll_5ib00d$ : function (c) { + containsAll_xeylzf$ : function (c) { var it = c.iterator(); while (it.hasNext()) { - if (!this.contains_s9cetl$(it.next())) return false; + if (!this.contains_za3rmp$(it.next())) return false; } return true; }, @@ -225,7 +225,7 @@ String.prototype.contains = function (s) { iterator: function () { return new ArrayIterator(this.toArray()); }, - equals_s9cetl$: function (o) { + equals_za3rmp$: function (o) { if (this.size() !== o.size()) return false; var iterator1 = this.iterator(); @@ -265,16 +265,16 @@ String.prototype.contains = function (s) { iterator: function () { return new ListIterator(this); }, - remove_s9cetl$: function (o) { - var index = this.indexOf_s9cetl$(o); + remove_za3rmp$: function (o) { + var index = this.indexOf_za3rmp$(o); if (index !== -1) { - this.remove_s9c8w6$(index); + this.remove_za3lpa$(index); return true; } return false; }, - contains_s9cetl$: function (o) { - return this.indexOf_s9cetl$(o) !== -1; + contains_za3rmp$: function (o) { + return this.indexOf_za3rmp$(o) !== -1; } }); @@ -283,11 +283,11 @@ String.prototype.contains = function (s) { function () { this.array = []; }, { - get_s9c8w6$: function (index) { + get_za3lpa$: function (index) { this.checkRange(index); return this.array[index]; }, - set_bar457$: function (index, value) { + set_vux3hl$: function (index, value) { this.checkRange(index); this.array[index] = value; }, @@ -297,27 +297,27 @@ String.prototype.contains = function (s) { iterator: function () { return Kotlin.arrayIterator(this.array); }, - add_s9cetl$: function (element) { + add_za3rmp$: function (element) { this.array.push(element); return true; }, - add_bar457$: function (index, element) { + add_vux3hl$: function (index, element) { this.array.splice(index, 0, element); }, - addAll_5ib00d$: function (collection) { + addAll_xeylzf$: function (collection) { var it = collection.iterator(); for (var i = this.array.length, n = collection.size(); n-- > 0;) { this.array[i++] = it.next(); } }, - remove_s9c8w6$: function (index) { + remove_za3lpa$: function (index) { this.checkRange(index); return this.array.splice(index, 1)[0]; }, clear: function () { this.array.length = 0; }, - indexOf_s9cetl$: function (o) { + indexOf_za3rmp$: function (o) { for (var i = 0; i < this.array.length; i++) { if (Kotlin.equals(this.array[i], o)) { return i; @@ -325,7 +325,7 @@ String.prototype.contains = function (s) { } return -1; }, - lastIndexOf_s9cetl$: function (o) { + lastIndexOf_za3rmp$: function (o) { for (var i = this.array.length - 1; i >= 0; i--) { if (Kotlin.equals(this.array[i], o)) { return i; @@ -525,7 +525,7 @@ String.prototype.contains = function (s) { array.sort(boundComparator); for (var i = 0, n = array.length; i < n; i++) { - mutableList.set_bar457$(i, array[i]); + mutableList.set_vux3hl$(i, array[i]); } }; diff --git a/js/js.translator/testFiles/maps.js b/js/js.translator/testFiles/maps.js index 0a3c8e7387f..7db55c9ed77 100644 --- a/js/js.translator/testFiles/maps.js +++ b/js/js.translator/testFiles/maps.js @@ -160,9 +160,9 @@ } }, - containsKey_s9cetl$: createBucketSearcher(EXISTENCE), + containsKey_za3rmp$: createBucketSearcher(EXISTENCE), - containsValue_s9cetl$: function (value) { + containsValue_za3rmp$: function (value) { var i = this.entries.length; while (i--) { if (value === this.entries[i][1]) { @@ -205,7 +205,7 @@ var hashingFunction = (typeof hashingFunctionParam == FUNCTION) ? hashingFunctionParam : hashObject; var equalityFunction = (typeof equalityFunctionParam == FUNCTION) ? equalityFunctionParam : null; - this.put_5yfy9u$ = function (key, value) { + this.put_wn2jw4$ = function (key, value) { checkKey(key); checkValue(value); var hash = hashingFunction(key), bucket, bucketEntry, oldValue = null; @@ -234,7 +234,7 @@ return oldValue; }; - this.get_s9cetl$ = function (key) { + this.get_za3rmp$ = function (key) { checkKey(key); var hash = hashingFunction(key); @@ -252,21 +252,21 @@ return null; }; - this.containsKey_s9cetl$ = function (key) { + this.containsKey_za3rmp$ = function (key) { checkKey(key); var bucketKey = hashingFunction(key); // Check if a bucket exists for the bucket key var bucket = getBucketForHash(bucketsByHash, bucketKey); - return bucket ? bucket.containsKey_s9cetl$(key) : false; + return bucket ? bucket.containsKey_za3rmp$(key) : false; }; - this.containsValue_s9cetl$ = function (value) { + this.containsValue_za3rmp$ = function (value) { checkValue(value); var i = buckets.length; while (i--) { - if (buckets[i].containsValue_s9cetl$(value)) { + if (buckets[i].containsValue_za3rmp$(value)) { return true; } } @@ -301,12 +301,12 @@ var i = values.length; var result = new Kotlin.ArrayList(); while (i--) { - result.add_s9cetl$(values[i]); + result.add_za3rmp$(values[i]); } return result; }; - this.remove_s9cetl$ = function (key) { + this.remove_za3rmp$ = function (key) { checkKey(key); var hash = hashingFunction(key), bucketIndex, oldValue = null; @@ -347,7 +347,7 @@ }; - this.putAll_s9c68p$ = function (hashtable, conflictCallback) { + this.putAll_za3j1t$ = function (hashtable, conflictCallback) { var entries = hashtable._entries(); var entry, key, value, thisValue, i = entries.length; var hasConflictCallback = (typeof conflictCallback == FUNCTION); @@ -360,13 +360,13 @@ if (hasConflictCallback && (thisValue = that.get(key))) { value = conflictCallback(key, thisValue, value); } - that.put_5yfy9u$(key, value); + that.put_wn2jw4$(key, value); } }; this.clone = function () { var clone = new Hashtable(hashingFunctionParam, equalityFunctionParam); - clone.putAll_s9c68p$(that); + clone.putAll_za3j1t$(that); return clone; }; @@ -375,7 +375,7 @@ var keys = this._keys(); var i = keys.length; while (i--) { - res.add_s9cetl$(keys[i]); + res.add_za3rmp$(keys[i]); } return res; }; @@ -422,7 +422,7 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; return this.map.$size === 0; }, contains: function (o) { - return this.map.containsValue_s9cetl$(o); + return this.map.containsValue_za3rmp$(o); } }); @@ -437,10 +437,10 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; isEmpty: function () { return this.$size === 0; }, - containsKey_s9cetl$: function (key) { + containsKey_za3rmp$: function (key) { return this.map[key] !== undefined; }, - containsValue_s9cetl$: function (value) { + containsValue_za3rmp$: function (value) { var map = this.map; for (var key in map) { if (map.hasOwnProperty(key) && map[key] === value) { @@ -450,10 +450,10 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; return false; }, - get_s9cetl$: function (key) { + get_za3rmp$: function (key) { return this.map[key]; }, - put_5yfy9u$: function (key, value) { + put_wn2jw4$: function (key, value) { var prevValue = this.map[key]; this.map[key] = value === undefined ? null : value; if (prevValue === undefined) { @@ -461,7 +461,7 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; } return prevValue; }, - remove_s9cetl$: function (key) { + remove_za3rmp$: function (key) { var prevValue = this.map[key]; if (prevValue !== undefined) { delete this.map[key]; @@ -473,7 +473,7 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; this.$size = 0; this.map = {}; }, - putAll_s9c68p$: function (fromMap) { + putAll_za3j1t$: function (fromMap) { var map = fromMap.map; for (var key in map) { if (map.hasOwnProperty(key)) { @@ -487,7 +487,7 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; var map = this.map; for (var key in map) { if (map.hasOwnProperty(key)) { - result.add_s9cetl$(key); + result.add_za3rmp$(key); } } @@ -517,7 +517,7 @@ var SetIterator = Kotlin.createClassNow(Kotlin.Iterator, return this.index < this.keys.length; }, remove: function() { - this.set.remove_s9cetl$(this.keys[this.index - 1]); + this.set.remove_za3rmp$(this.keys[this.index - 1]); } }); @@ -526,13 +526,13 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, this.$size = 0; this.map = {}; }, { - contains_s9cetl$: function (key) { + contains_za3rmp$: function (key) { return this.map[key] === true; }, iterator: function() { return new SetIterator(this); }, - add_s9cetl$: function (element) { + add_za3rmp$: function (element) { var prevElement = this.map[element]; this.map[element] = true; if (prevElement === true) { @@ -543,7 +543,7 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, return true; } }, - remove_s9cetl$: function (element) { + remove_za3rmp$: function (element) { if (this.map[element] === true) { delete this.map[element]; this.$size--; @@ -566,13 +566,13 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, function HashSet(hashingFunction, equalityFunction) { var hashTable = new Kotlin.HashTable(hashingFunction, equalityFunction); - this.addAll_5ib00d$ = Kotlin.AbstractCollection.prototype.addAll_5ib00d$; - this.removeAll_5ib00d$ = Kotlin.AbstractCollection.prototype.removeAll_5ib00d$; - this.retainAll_5ib00d$ = Kotlin.AbstractCollection.prototype.retainAll_5ib00d$; - this.containsAll_5ib00d$ = Kotlin.AbstractCollection.prototype.containsAll_5ib00d$; + this.addAll_xeylzf$ = Kotlin.AbstractCollection.prototype.addAll_xeylzf$; + this.removeAll_xeylzf$ = Kotlin.AbstractCollection.prototype.removeAll_xeylzf$; + this.retainAll_xeylzf$ = Kotlin.AbstractCollection.prototype.retainAll_xeylzf$; + this.containsAll_xeylzf$ = Kotlin.AbstractCollection.prototype.containsAll_xeylzf$; - this.add_s9cetl$ = function (o) { - return !hashTable.put_5yfy9u$(o, true); + this.add_za3rmp$ = function (o) { + return !hashTable.put_wn2jw4$(o, true); }; this.toArray = function () { @@ -583,12 +583,12 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, return new SetIterator(this); }; - this.remove_s9cetl$ = function (o) { - return hashTable.remove_s9cetl$(o) != null; + this.remove_za3rmp$ = function (o) { + return hashTable.remove_za3rmp$(o) != null; }; - this.contains_s9cetl$ = function (o) { - return hashTable.containsKey_s9cetl$(o); + this.contains_za3rmp$ = function (o) { + return hashTable.containsKey_za3rmp$(o); }; this.clear = function () { @@ -605,7 +605,7 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, this.clone = function () { var h = new HashSet(hashingFunction, equalityFunction); - h.addAll_5ib00d$(hashTable.keys()); + h.addAll_xeylzf$(hashTable.keys()); return h; }; @@ -650,8 +650,8 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, var values = hashSet.values(), i = values.length, val; while (i--) { val = values[i]; - if (hashTable.containsKey_s9cetl$(val)) { - intersection.add_s9cetl$(val); + if (hashTable.containsKey_za3rmp$(val)) { + intersection.add_za3rmp$(val); } } return intersection; @@ -662,8 +662,8 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, var values = hashSet.values(), i = values.length, val; while (i--) { val = values[i]; - if (!hashTable.containsKey_s9cetl$(val)) { - union.add_s9cetl$(val); + if (!hashTable.containsKey_za3rmp$(val)) { + union.add_za3rmp$(val); } } return union; @@ -672,7 +672,7 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection, this.isSubsetOf = function (hashSet) { var values = hashTable.keys(), i = values.length; while (i--) { - if (!hashSet.contains_s9cetl$(values[i])) { + if (!hashSet.contains_za3rmp$(values[i])) { return false; } }