Add argument validation in copyOf(newSize) in JS

#KT-19489
This commit is contained in:
Ilya Gorbunov
2018-08-04 01:27:34 +03:00
parent 93540e476e
commit 58a3b64baf
3 changed files with 28 additions and 1 deletions
@@ -453,6 +453,7 @@ object ArrayOps : TemplateGroupBase() {
- If [newSize] is greater than the size of the original array, the extra elements in the copy array are filled with ${primitive.zero} values.
"""
}
val newSizeCheck = """require(newSize >= 0) { "Invalid new array size: ${'$'}newSize." }"""
specialFor(ArraysOfPrimitives) {
sample("samples.collections.Arrays.CopyOfOperations.resizedPrimitiveCopyOf")
returns("SELF")
@@ -467,6 +468,7 @@ object ArrayOps : TemplateGroupBase() {
else ->
body { "return fillFrom(this, ${primitive}Array(newSize))" }
}
body { newSizeCheck + "\n" + body }
}
}
@@ -476,7 +478,12 @@ object ArrayOps : TemplateGroupBase() {
on(Platform.JS) {
family = ArraysOfObjects
suppress("ACTUAL_WITHOUT_EXPECT") // TODO: KT-21937
body { "return arrayCopyResize(this, newSize, null)" }
body {
"""
$newSizeCheck
return arrayCopyResize(this, newSize, null)
"""
}
}
}
on(Platform.JVM) {