JS: enable translation of primitive arrays to TypedArray's by default (KT-17137)

This commit is contained in:
Anton Bannykh
2017-08-23 16:22:45 +03:00
parent cc8fbf6078
commit 22dc36a596
11 changed files with 87 additions and 32 deletions
@@ -98,9 +98,9 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
// Advanced options
@GradleOption(DefaultValues.BooleanFalseDefault::class)
@GradleOption(DefaultValues.BooleanTrueDefault::class)
@Argument(value = "-Xtyped-arrays", description = "Translate primitive arrays to JS typed arrays")
var typedArrays: Boolean by FreezableVar(false)
var typedArrays: Boolean by FreezableVar(true)
@GradleOption(DefaultValues.BooleanFalseDefault::class)
@Argument(value = "-Xfriend-modules-disabled", description = "Disable internal declaration export")
@@ -376,9 +376,7 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
configuration.put(JSConfigurationKeys.META_INFO, true);
}
if (arguments.getTypedArrays()) {
configuration.put(JSConfigurationKeys.TYPED_ARRAYS_ENABLED, true);
}
configuration.put(JSConfigurationKeys.TYPED_ARRAYS_ENABLED, arguments.getTypedArrays());
configuration.put(JSConfigurationKeys.FRIEND_PATHS_DISABLED, arguments.getFriendModulesDisabled());
+1 -4
View File
@@ -1,9 +1,6 @@
// WITH_RUNTIME
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.*
fun box(): String {
assertTrue(eqBoolean(booleanArrayOf(false), BooleanArray(1)))
@@ -1503,7 +1503,7 @@ fun main(args: Array<String>) {
model("codegen/boxInline/defaultValues/", targetBackend = TargetBackend.JS)
}
testClass<AbstractJsTypedArraysBoxTest> {
testClass<AbstractJsLegacyPrimitiveArraysBoxTest> {
model("codegen/box/arrays", targetBackend = TargetBackend.JS)
}
}
+4 -6
View File
@@ -55,16 +55,14 @@ Kotlin.isArrayish = function (a) {
};
Kotlin.arrayToString = function (a) {
return "[" + a.map(Kotlin.toString).join(", ") + "]";
var toString = Kotlin.isCharArray(a) ? String.fromCharCode : Kotlin.toString;
return "[" + Array.prototype.map.call(a, function(e) { return toString(e); }).join(", ") + "]";
};
Kotlin.arrayDeepToString = function (a, visited) {
visited = visited || [a];
var toString = Kotlin.toString;
if (Kotlin.isCharArray(a)) {
toString = String.fromCharCode;
}
return "[" + a.map(function (e) {
var toString = Kotlin.isCharArray(a) ? String.fromCharCode : Kotlin.toString;
return "[" + Array.prototype.map.call(a, function (e) {
if (Kotlin.isArrayish(e) && visited.indexOf(e) < 0) {
visited.push(e);
var result = Kotlin.arrayDeepToString(e, visited);
+67 -5
View File
@@ -32,8 +32,70 @@ if (typeof String.prototype.endsWith === "undefined") {
};
}
// For HtmlUnit and PhantomJs
if (typeof ArrayBuffer.isView === "undefined") {
ArrayBuffer.isView = function(a) {
return a != null && a.__proto__ != null && a.__proto__.__proto__ === Int8Array.prototype.__proto__;
};
}
(function() {
if (typeof ArrayBuffer.isView === "undefined") {
ArrayBuffer.isView = function(a) {
return a != null && a.__proto__ != null && a.__proto__.__proto__ === Int8Array.prototype.__proto__;
};
}
function normalizeOffset(offset, length) {
if (offset < 0) return Math.max(0, offset + length);
return Math.min(offset, length);
}
function typedArraySlice(begin, end) {
if (typeof end === "undefined") {
end = this.length;
}
begin = normalizeOffset(begin || 0, this.length);
end = Math.max(begin, normalizeOffset(end, this.length));
return new this.constructor(this.subarray(begin, end));
}
var arrays = [Int8Array, Int16Array, Uint16Array, Int32Array, Float32Array, Float64Array];
for (var i = 0; i < arrays.length; ++i) {
var TypedArray = arrays[i];
if (typeof TypedArray.prototype.slice === "undefined") {
Object.defineProperty(TypedArray.prototype, 'slice', {
value: typedArraySlice
});
}
}
// Patch apply to work with TypedArrays if needed.
try {
(function() {}).apply(null, new Int32Array(0))
} catch (e) {
var apply = Function.prototype.apply;
Object.defineProperty(Function.prototype, 'apply', {
value: function(self, array) {
return apply.call(this, self, [].slice.call(array));
}
});
}
// Patch map to work with TypedArrays if needed.
for (var i = 0; i < arrays.length; ++i) {
var TypedArray = arrays[i];
if (typeof TypedArray.prototype.map === "undefined") {
Object.defineProperty(TypedArray.prototype, 'map', {
value: function(callback, self) {
return typedArraySlice.call([].map.call(this, callback, self));
}
});
}
}
// Patch sort to work with TypedArrays if needed.
for (var i = 0; i < arrays.length; ++i) {
var TypedArray = arrays[i];
if (typeof TypedArray.prototype.sort === "undefined") {
Object.defineProperty(TypedArray.prototype, 'sort', {
value: function(compareFunction) {
return Array.prototype.sort.call(this, compareFunction);
}
});
}
}
})();
@@ -72,7 +72,7 @@ import java.util.regex.Pattern
abstract class BasicBoxTest(
protected val pathToTestDir: String,
private val pathToOutputDir: String,
private val typedArraysEnabled: Boolean = false,
private val typedArraysEnabled: Boolean = true,
private val generateSourceMap: Boolean = false,
private val generateNodeJsRunner: Boolean = true
) : KotlinTestWithEnvironment() {
@@ -31,7 +31,7 @@ import java.util.regex.Pattern;
@TestMetadata("compiler/testData/codegen/box/arrays")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class JsTypedArraysBoxTestGenerated extends AbstractJsTypedArraysBoxTest {
public class JsTypedArraysBoxTestGenerated extends AbstractJsLegacyPrimitiveArraysBoxTest {
public void testAllFilesPresentInArrays() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@@ -441,7 +441,7 @@ public class JsTypedArraysBoxTestGenerated extends AbstractJsTypedArraysBoxTest
@TestMetadata("compiler/testData/codegen/box/arrays/multiDecl")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class MultiDecl extends AbstractJsTypedArraysBoxTest {
public static class MultiDecl extends AbstractJsLegacyPrimitiveArraysBoxTest {
public void testAllFilesPresentInMultiDecl() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays/multiDecl"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@@ -497,7 +497,7 @@ public class JsTypedArraysBoxTestGenerated extends AbstractJsTypedArraysBoxTest
@TestMetadata("compiler/testData/codegen/box/arrays/multiDecl/int")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Int extends AbstractJsTypedArraysBoxTest {
public static class Int extends AbstractJsLegacyPrimitiveArraysBoxTest {
public void testAllFilesPresentInInt() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays/multiDecl/int"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@@ -530,7 +530,7 @@ public class JsTypedArraysBoxTestGenerated extends AbstractJsTypedArraysBoxTest
@TestMetadata("compiler/testData/codegen/box/arrays/multiDecl/long")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Long extends AbstractJsTypedArraysBoxTest {
public static class Long extends AbstractJsLegacyPrimitiveArraysBoxTest {
public void testAllFilesPresentInLong() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/arrays/multiDecl/long"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
}
@@ -51,10 +51,10 @@ abstract class AbstractJsCodegenBoxTest : BasicBoxTest(
BasicBoxTest.TEST_DATA_DIR_PATH + "out/codegen/box/"
)
abstract class AbstractJsTypedArraysBoxTest : BasicBoxTest(
abstract class AbstractJsLegacyPrimitiveArraysBoxTest : BasicBoxTest(
"compiler/testData/codegen/box/arrays/",
BasicBoxTest.TEST_DATA_DIR_PATH + "out/codegen/box/arrays-typedarrays/",
typedArraysEnabled = true
BasicBoxTest.TEST_DATA_DIR_PATH + "out/codegen/box/arrays-legacy-primitivearrays/",
typedArraysEnabled = false
)
abstract class AbstractSourceMapGenerationSmokeTest : BasicBoxTest(
@@ -70,7 +70,7 @@ interface KotlinJsOptions : org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
/**
* Translate primitive arrays to JS typed arrays
* Default value: false
* Default value: true
*/
var typedArrays: kotlin.Boolean
}
@@ -76,7 +76,7 @@ internal abstract class KotlinJsOptionsBase : org.jetbrains.kotlin.gradle.dsl.Ko
private var typedArraysField: kotlin.Boolean? = null
override var typedArrays: kotlin.Boolean
get() = typedArraysField ?: false
get() = typedArraysField ?: true
set(value) { typedArraysField = value }
internal open fun updateArguments(args: org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments) {
@@ -113,5 +113,5 @@ internal fun org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments.fil
sourceMapEmbedSources = "inlining"
sourceMapPrefix = null
target = "v5"
typedArrays = false
typedArrays = true
}