Skip to content

Commit

Permalink
fix(graalvm): fix and suppress detekt issues where applicable.
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Valdespino <[email protected]>
  • Loading branch information
darvld authored and sgammon committed Jul 21, 2024
1 parent 5129fd6 commit a2a6537
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ import elide.runtime.core.PolyglotValue
operator fun set(index: Int, value: Byte) {
this.value.writeBufferByte(index.toLong(), value)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MagicNumber")

package elide.runtime.gvm.internals.node.buffer

import org.graalvm.polyglot.Value
Expand Down Expand Up @@ -114,11 +116,6 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
}

override fun from(source: PolyglotValue, offset: Int?, length: Int?, encoding: String?): BufferInstance {
if (source.isString) {
val bytes = NodeBufferEncoding.encode(source.asString(), encoding)
return NodeHostBuffer.allocate(bytes.size).apply { fillWith(bytes, 0, bytes.size) }
}

source.asBufferOrNull()?.let { sourceBuffer ->
val buffer = NodeHostBuffer.allocate(sourceBuffer.length)

Expand All @@ -135,21 +132,26 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
return buffer
}

if (source.hasBufferElements()) {
val buffer = NodeHostBuffer.allocate(length ?: (source.bufferSize.toInt() - (offset ?: 0)))
buffer.fillWith(GuestBytes(source), 0, buffer.length, offset ?: 0)

return buffer
}

if (source.hasArrayElements()) {
val buffer = NodeHostBuffer.allocate(source.arraySize.toInt())
for (i in 0 until buffer.length) buffer.byteBuffer.put(i, source.getArrayElement(i.toLong()).asLong().toByte())

return buffer
return when {
source.isString -> {
val bytes = NodeBufferEncoding.encode(source.asString(), encoding)
NodeHostBuffer.allocate(bytes.size).apply { fillWith(bytes, 0, bytes.size) }
}

source.hasBufferElements() -> {
val buffer = NodeHostBuffer.allocate(length ?: (source.bufferSize.toInt() - (offset ?: 0)))
buffer.fillWith(GuestBytes(source), 0, buffer.length, offset ?: 0)
buffer
}

source.hasArrayElements() -> {
val buffer = NodeHostBuffer.allocate(source.arraySize.toInt())
for (i in 0 until buffer.length) buffer.byteBuffer.put(i, source.getArrayElement(i.toLong()).asLong().toByte())
buffer
}

else -> throw typeError("Unexpected source type: expected string, int[], Buffer, UInt8Array, or ArrayBuffer")
}

throw typeError("Unexpected source type: expected string, int[], Buffer, UInt8Array, or ArrayBuffer")
}

override fun isBuffer(obj: PolyglotValue): Boolean {
Expand All @@ -173,6 +175,7 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
return staticMembers.binarySearch(key) >= 0
}

@Suppress("CyclomaticComplexMethod")
override fun getMember(key: String?): Any = when (key) {
"alloc" -> ProxyExecutable { args ->
if (args.isEmpty()) error("Buffer.alloc takes 1 to 3 arguments, but received none")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@file:Suppress("MagicNumber")

package elide.runtime.gvm.internals.node.buffer

import org.graalvm.polyglot.Value
import org.graalvm.polyglot.proxy.ProxyArray
import org.graalvm.polyglot.proxy.ProxyExecutable
import org.graalvm.polyglot.proxy.ProxyIterable
import org.graalvm.polyglot.proxy.ProxyObject
import kotlin.experimental.and
import elide.runtime.core.DelicateElideApi
import elide.runtime.core.PolyglotValue
import elide.runtime.gvm.internals.intrinsics.js.JsError
Expand All @@ -16,7 +17,9 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
* A base implementation for the Node.js [BufferInstance] API, serving as a template for Buffer instances without
* specifying the backing `ArrayBuffer` value.
*/
@DelicateElideApi internal sealed class NodeBufferInstance : BufferInstance, ProxyObject {
@DelicateElideApi
@Suppress("TooManyFunctions")
internal sealed class NodeBufferInstance : BufferInstance, ProxyObject {
/** Attempts to interpret this value as a [NodeBufferInstance] and unwrap it, returning `null` on failure. */
protected fun PolyglotValue.asBufferInstance(): NodeBufferInstance? {
return if (isProxyObject) runCatching { asProxyObject<NodeBufferInstance>() }.getOrNull()
Expand Down Expand Up @@ -291,19 +294,25 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance

/* ------- IndexOf ------- */

override fun indexOf(value: PolyglotValue, byteOffset: Int?, encoding: String?): Int {
if (value.isNumber) {
override fun indexOf(value: PolyglotValue, byteOffset: Int?, encoding: String?): Int = when {
value.isNumber -> {
val byteValue = value.asLong().toByte()
for (i in (byteOffset ?: 0) until length) if (getByte(i) == byteValue) return i
return -1
var index = -1

for (i in (byteOffset ?: 0) until length) if (getByte(i) == byteValue) {
index = i
break
}

index
}

if (value.isString) {
value.isString -> {
val bytes = NodeBufferEncoding.encode(value.asString(), encoding)
return search(byteOffset ?: 0, bytes.size) { bytes[it] }
search(byteOffset ?: 0, bytes.size) { bytes[it] }
}

return value.whenBufferOrView(
else -> value.whenBufferOrView(
onBuffer = { other ->
search(byteOffset ?: 0, other.length) { other.getByte(it) }
},
Expand All @@ -316,19 +325,25 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
)
}

override fun lastIndexOf(value: PolyglotValue, byteOffset: Int?, encoding: String?): Int {
if (value.isNumber) {
override fun lastIndexOf(value: PolyglotValue, byteOffset: Int?, encoding: String?): Int = when {
value.isNumber -> {
val byteValue = value.asLong().toByte()
for (i in (byteOffset ?: (length - 1)) downTo 0) if (getByte(i) == byteValue) return i
return -1
var index = -1

for (i in (byteOffset ?: (length - 1)) downTo 0) if (getByte(i) == byteValue) {
index = i
break
}

index
}

if (value.isString) {
value.isString -> {
val bytes = NodeBufferEncoding.encode(value.asString(), encoding)
return invertedSearch(byteOffset ?: (length - 1), bytes.size) { bytes[it] }
invertedSearch(byteOffset ?: (length - 1), bytes.size) { bytes[it] }
}

return value.whenBufferOrView(
else -> value.whenBufferOrView(
onBuffer = { other ->
invertedSearch(byteOffset ?: (length - 1), other.length) { other.getByte(it) }
},
Expand Down Expand Up @@ -497,6 +512,7 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
return instanceMembers.binarySearch(key) >= 0
}

@Suppress("LongMethod", "CyclomaticComplexMethod")
override fun getMember(key: String?): Any = when (key) {
"buffer" -> this.buffer
"byteOffset" -> this.byteOffset
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MagicNumber", "TooManyFunctions")

package elide.runtime.gvm.internals.node.buffer

import com.oracle.truffle.js.runtime.objects.Undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MagicNumber")

package elide.runtime.gvm.internals.node.buffer

import com.oracle.truffle.js.runtime.objects.Undefined
Expand All @@ -16,7 +18,9 @@ import elide.runtime.intrinsics.js.node.buffer.BufferInstance
* The backing buffer's position and limit are assumed to remain constant and should therefore not be modified in any
* case; doing so will cause unspecified behavior.
*/
@DelicateElideApi internal class NodeHostBuffer private constructor(
@DelicateElideApi
@Suppress("TooManyFunctions")
internal class NodeHostBuffer private constructor(
internal val byteBuffer: ByteBuffer
) : NodeBufferInstance() {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import elide.runtime.core.PolyglotValue
* All Buffer operations are performed using the backing array buffer, however it is up to the implementation to define
* the nature of the backing buffer itself (e.g. it could be a NIO `ByteBuffer`).
*/
@DelicateElideApi public interface BufferInstance : ProxyArray, ProxyObject {
@DelicateElideApi
@Suppress("TooManyFunctions")
public interface BufferInstance : ProxyArray, ProxyObject {
/**
* Returns the backing `ArrayBuffer` for this buffer instance. The [byteOffset] indicates the index of this backing
* value at which the view represented by this Buffer begins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import elide.testing.annotations.TestCase

/** Tests for [Buffer]. */
@DelicateElideApi
@TestCase internal class NodeBufferTest : NodeModuleConformanceTest<NodeBufferModule>() {
@TestCase
@Suppress("LargeClass")
internal class NodeBufferTest : NodeModuleConformanceTest<NodeBufferModule>() {
@Inject internal lateinit var buffer: BufferAPI
@Inject internal lateinit var module: NodeBufferModule

Expand Down Expand Up @@ -454,7 +456,9 @@ import elide.testing.annotations.TestCase
"""
}

@Test fun `Buffer (instance members)`() = conforms {
@Test
@Suppress("LongMethod")
fun `Buffer (instance members)`() = conforms {

}.guest {
"""
Expand Down

0 comments on commit a2a6537

Please sign in to comment.