From 548966f5bd69ad53c690dd4e42d9f8747053fa32 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Tue, 16 Jun 2020 01:50:20 +0700 Subject: [PATCH 1/2] Implement copy for DataViewMemory --- .../jsMain/kotlin/scientifik/memory/DataViewMemory.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt b/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt index 59945efb9..22cf2bac1 100644 --- a/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt +++ b/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt @@ -11,16 +11,15 @@ class DataViewMemory(val view: DataView) : Memory { override fun view(offset: Int, length: Int): Memory { require(offset >= 0) { "offset shouldn't be negative: $offset" } require(length >= 0) { "length shouldn't be negative: $length" } - if (offset + length > size) { + + if (offset + length > size) throw IndexOutOfBoundsException("offset + length > size: $offset + $length > $size") - } + return DataViewMemory(DataView(view.buffer, view.byteOffset + offset, length)) } - override fun copy(): Memory { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. - } + override fun copy(): Memory = DataViewMemory(DataView(view.buffer.slice(0))) private val reader = object : MemoryReader { override val memory: Memory get() = this@DataViewMemory @@ -94,4 +93,4 @@ actual fun Memory.Companion.allocate(length: Int): Memory { actual fun Memory.Companion.wrap(array: ByteArray): Memory { @Suppress("CAST_NEVER_SUCCEEDS") val int8Array = array as Int8Array return DataViewMemory(DataView(int8Array.buffer, int8Array.byteOffset, int8Array.length)) -} \ No newline at end of file +} -- 2.34.1 From 19c3f03cc6b5b4f8d6fe40ef204355d9e90ddfd4 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Tue, 16 Jun 2020 01:53:12 +0700 Subject: [PATCH 2/2] Specify types explicitly --- .../src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt b/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt index 22cf2bac1..38ec14824 100644 --- a/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt +++ b/kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt @@ -18,10 +18,9 @@ class DataViewMemory(val view: DataView) : Memory { return DataViewMemory(DataView(view.buffer, view.byteOffset + offset, length)) } - override fun copy(): Memory = DataViewMemory(DataView(view.buffer.slice(0))) - private val reader = object : MemoryReader { + private val reader: MemoryReader = object : MemoryReader { override val memory: Memory get() = this@DataViewMemory override fun readDouble(offset: Int): Double = view.getFloat64(offset, false) @@ -44,7 +43,7 @@ class DataViewMemory(val view: DataView) : Memory { override fun reader(): MemoryReader = reader - private val writer = object : MemoryWriter { + private val writer: MemoryWriter = object : MemoryWriter { override val memory: Memory get() = this@DataViewMemory override fun writeDouble(offset: Int, value: Double) { @@ -75,7 +74,6 @@ class DataViewMemory(val view: DataView) : Memory { override fun release() { //does nothing on JS } - } override fun writer(): MemoryWriter = writer -- 2.34.1