JS: enable translation of primitive arrays to TypedArray's by default (KT-17137)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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() {
|
||||
|
||||
+4
-4
@@ -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);
|
||||
}
|
||||
|
||||
+3
-3
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user