KJS: implement array factory function intrinsics in Kotlin
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
package org.jetbrains.kotlin.js.backend.ast;
|
||||
|
||||
public final class JsStringLiteral extends JsLiteral.JsValueLiteral {
|
||||
public static JsStringLiteral createCharZero() {
|
||||
return new JsStringLiteral("\0");
|
||||
}
|
||||
|
||||
private final String value;
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.
|
||||
*/
|
||||
|
||||
// a package is omitted to get declarations directly under the module
|
||||
|
||||
external private fun <T> Array(size: Int): Array<T>
|
||||
|
||||
@JsName("newArray")
|
||||
fun <T> newArray(size: Int, initValue: T): Array<T> {
|
||||
return fillArray(Array(size), initValue)
|
||||
}
|
||||
|
||||
private fun <T> fillArray(array: Array<T>, value: T): Array<T> {
|
||||
for (i in 0..array.size - 1) {
|
||||
array[i] = value
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
@JsName("newArrayF")
|
||||
fun <T> arrayWithFun(size: Int, init: (Int) -> T): Array<T> {
|
||||
var result = Array<T>(size)
|
||||
for (i in 0..size - 1) {
|
||||
result[i] = init(i)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -278,46 +278,5 @@ Kotlin.primitiveArraySort = function(array) {
|
||||
array.sort(Kotlin.primitiveCompareTo)
|
||||
};
|
||||
|
||||
Kotlin.nullArray = function (size) {
|
||||
var res = [];
|
||||
var i = size;
|
||||
while (i > 0) {
|
||||
res[--i] = null;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
Kotlin.numberArrayOfSize = function (size) {
|
||||
return Kotlin.arrayFromFun(size, function () {
|
||||
return 0;
|
||||
});
|
||||
};
|
||||
|
||||
Kotlin.charArrayOfSize = function (size) {
|
||||
return Kotlin.arrayFromFun(size, function () {
|
||||
return '\0';
|
||||
});
|
||||
};
|
||||
|
||||
Kotlin.booleanArrayOfSize = function (size) {
|
||||
return Kotlin.arrayFromFun(size, function () {
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
Kotlin.longArrayOfSize = function (size) {
|
||||
return Kotlin.arrayFromFun(size, function () {
|
||||
return Kotlin.Long.ZERO;
|
||||
});
|
||||
};
|
||||
|
||||
Kotlin.arrayFromFun = function (size, initFun) {
|
||||
var result = new Array(size);
|
||||
for (var i = 0; i < size; i++) {
|
||||
result[i] = initFun(i);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
Kotlin.identityHashCode = getObjectHashCode;
|
||||
|
||||
|
||||
+8
-7
@@ -17,8 +17,7 @@
|
||||
package org.jetbrains.kotlin.js.translate.intrinsic.functions.factories;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsArrayAccess;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
|
||||
import org.jetbrains.kotlin.js.backend.ast.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||
@@ -125,12 +124,14 @@ public final class ArrayFIF extends CompositeFIF {
|
||||
add(pattern(ARRAYS, "<get-size>"), LENGTH_PROPERTY_INTRINSIC);
|
||||
add(pattern(ARRAYS, "iterator"), new KotlinFunctionIntrinsic("arrayIterator"));
|
||||
|
||||
add(pattern(NUMBER_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("numberArrayOfSize"));
|
||||
add(pattern(CHAR_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("charArrayOfSize"));
|
||||
add(pattern(BOOLEAN_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("booleanArrayOfSize"));
|
||||
add(pattern(LONG_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("longArrayOfSize"));
|
||||
add(pattern(NUMBER_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", JsNumberLiteral.ZERO));
|
||||
add(pattern(CHAR_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", JsStringLiteral.createCharZero()));
|
||||
add(pattern(BOOLEAN_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", JsLiteral.FALSE));
|
||||
add(pattern(LONG_ARRAY, "<init>(Int)"), new KotlinFunctionIntrinsic("newArray", new JsNameRef(Namer.LONG_ZERO, Namer.kotlinLong())));
|
||||
|
||||
add(pattern(ARRAYS, "<init>(Int,Function1)"), new KotlinFunctionIntrinsic("arrayFromFun"));
|
||||
add(pattern(ARRAYS, "<init>(Int,Function1)"), new KotlinFunctionIntrinsic("newArrayF"));
|
||||
|
||||
add(pattern("kotlin", "arrayOfNulls"), new KotlinFunctionIntrinsic("newArray", JsLiteral.NULL));
|
||||
|
||||
add(ARRAY_FACTORY_METHODS, ARRAY_INTRINSIC);
|
||||
}
|
||||
|
||||
+7
-1
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.js.translate.intrinsic.functions.factories;
|
||||
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsExpression;
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsInvocation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -31,9 +32,11 @@ import java.util.List;
|
||||
public class KotlinFunctionIntrinsic extends FunctionIntrinsic {
|
||||
@NotNull
|
||||
private final String functionName;
|
||||
private final JsExpression[] additionalArguments;
|
||||
|
||||
public KotlinFunctionIntrinsic(@NotNull String functionName) {
|
||||
public KotlinFunctionIntrinsic(@NotNull String functionName, JsExpression... additionalArguments) {
|
||||
this.functionName = functionName;
|
||||
this.additionalArguments = additionalArguments;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -44,6 +47,9 @@ public class KotlinFunctionIntrinsic extends FunctionIntrinsic {
|
||||
@NotNull TranslationContext context
|
||||
) {
|
||||
JsExpression function = JsAstUtils.pureFqn(functionName, Namer.kotlinObject());
|
||||
if (additionalArguments.length > 0) {
|
||||
arguments = CollectionsKt.plus(arguments, additionalArguments);
|
||||
}
|
||||
return new JsInvocation(function, receiver == null ? arguments : TranslationUtils.generateInvocationArguments(receiver, arguments));
|
||||
}
|
||||
}
|
||||
|
||||
-1
@@ -159,7 +159,6 @@ public final class TopLevelFIF extends CompositeFIF {
|
||||
add(HASH_CODE_IN_ANY, KOTLIN_HASH_CODE);
|
||||
add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), KOTLIN_EQUALS);
|
||||
add(pattern("String|Boolean|Char|Number.equals"), KOTLIN_EQUALS);
|
||||
add(pattern("kotlin", "arrayOfNulls"), new KotlinFunctionIntrinsic("nullArray"));
|
||||
add(pattern("kotlin", "iterator").isExtensionOf(FQ_NAMES.iterator.asString()), RETURN_RECEIVER_INTRINSIC);
|
||||
|
||||
add(pattern("kotlin.js", "Json", "get"), ArrayFIF.GET_INTRINSIC);
|
||||
|
||||
Reference in New Issue
Block a user