diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt index 7301fde3fe2..b6d1acacbef 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSCompilerArguments.kt @@ -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") diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java index d508554061c..4a0c58824f8 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java @@ -376,9 +376,7 @@ public class K2JSCompiler extends CLICompiler { 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()); diff --git a/compiler/testData/codegen/box/arrays/primitiveArrays.kt b/compiler/testData/codegen/box/arrays/primitiveArrays.kt index 0948cacd472..86e2b17cb5a 100644 --- a/compiler/testData/codegen/box/arrays/primitiveArrays.kt +++ b/compiler/testData/codegen/box/arrays/primitiveArrays.kt @@ -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))) diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index cb2701119c0..d9c4e2898d5 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -1503,7 +1503,7 @@ fun main(args: Array) { model("codegen/boxInline/defaultValues/", targetBackend = TargetBackend.JS) } - testClass { + testClass { model("codegen/box/arrays", targetBackend = TargetBackend.JS) } } diff --git a/js/js.libraries/src/js/arrayUtils.js b/js/js.libraries/src/js/arrayUtils.js index c9e24be32ce..015075492d7 100644 --- a/js/js.libraries/src/js/arrayUtils.js +++ b/js/js.libraries/src/js/arrayUtils.js @@ -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); diff --git a/js/js.libraries/src/js/polyfills.js b/js/js.libraries/src/js/polyfills.js index 47ee982a247..f393d433b60 100644 --- a/js/js.libraries/src/js/polyfills.js +++ b/js/js.libraries/src/js/polyfills.js @@ -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__; - }; -} \ No newline at end of file +(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); + } + }); + } + } +})(); \ No newline at end of file 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 a3eca406728..6417f6da46d 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 @@ -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() { diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsTypedArraysBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsTypedArraysBoxTestGenerated.java index 4806644ee10..dba1ebe0809 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsTypedArraysBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsTypedArraysBoxTestGenerated.java @@ -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); } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstractClassesForGeneratedTests.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstractClassesForGeneratedTests.kt index a30d3992c57..989277bb96b 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstractClassesForGeneratedTests.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/abstractClassesForGeneratedTests.kt @@ -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( diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt index a677f4f3d5f..017376af171 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptions.kt @@ -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 } diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptionsBase.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptionsBase.kt index 3eb529bb36c..164b75024b1 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptionsBase.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/dsl/KotlinJsOptionsBase.kt @@ -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 }