Skip to content

Commit 854365f

Browse files
committed
refactor(randomInt): remove unnecessary overload, clean up comments and test.
1 parent debdd00 commit 854365f

File tree

3 files changed

+6
-43
lines changed

3 files changed

+6
-43
lines changed

packages/graalvm/src/main/kotlin/elide/runtime/intrinsics/js/node/CryptoAPI.kt

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,28 @@ import elide.vm.annotations.Polyglot
5959

6060
/**
6161
* ## Crypto: randomInt
62+
* Public overload of [randomInt].
6263
* Generates a cryptographically secure random integer between the specified `min` (inclusive) and `max` (exclusive) values.
63-
*
64-
* See also: [Node Crypto API: `randomInt`](https://nodejs.org/api/crypto.html#cryptorandomintmin-max-callback)
65-
*
6664
* @param min Lower bound (inclusive)
6765
* @param max Upper bound (exclusive)
6866
* @param callback Callback to receive the generated safe integer or an error.
69-
* @return Unit
7067
*/
7168
@Polyglot public fun randomInt(min: Value, max: Value, callback: Value)
7269

7370
/**
7471
* ## Crypto: randomInt
72+
* Public overload of [randomInt].
7573
* Generates a cryptographically secure random integer between the specified `min` (inclusive) and `max` (exclusive) values.
76-
*
77-
* See also: [Node Crypto API: `randomInt`](https://nodejs.org/api/crypto.html#cryptorandomintmin-max-callback)
78-
*
79-
* @param max
80-
* @param callback
81-
* @return A randomly generated safe integer between `0` (inclusive) and `max` (exclusive) or nothing if a callback was provided.
82-
*/
83-
@Polyglot public fun randomInt(max: Value, callback: Value?)
84-
85-
/**
86-
* ## Crypto: randomInt
87-
* Generates a cryptographically secure random integer between the specified `min` (inclusive) and `max` (exclusive) values.
88-
*
89-
* See also: [Node Crypto API: `randomInt`](https://nodejs.org/api/crypto.html#cryptorandomintmin-max-callback)
90-
*
9174
* @param min Lower bound (inclusive)
9275
* @param max Upper bound (exclusive)
93-
* @return A randomly generated safe integer between `min` (inclusive) and `max` (exclusive).
9476
*/
9577
@Polyglot public fun randomInt(min: Value, max: Value): Long
9678

9779
/**
9880
* ## Crypto: randomInt
99-
* Generates a cryptographically secure random integer between `0` (inclusive) and the specified `max` (exclusive) value.
100-
*
101-
* See also: [Node Crypto API: `randomInt`](https://nodejs.org/api/crypto.html#cryptorandomintmin-max-callback)
102-
*
81+
* Public overload of [randomInt].
82+
* Generates a cryptographically secure random integer between the specified `min` (inclusive) and `max` (exclusive) values.
10383
* @param max Upper bound (exclusive)
104-
* @return A randomly generated safe integer between `0` (inclusive) and `max` (exclusive).
10584
*/
10685
@Polyglot public fun randomInt(max: Value): Long
10786
}

packages/graalvm/src/main/kotlin/elide/runtime/node/crypto/NodeCrypto.kt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,6 @@ internal class NodeCrypto private constructor () : ReadOnlyProxyObject, CryptoAP
174174
return randomInt(safeMin, safeMax)
175175
}
176176

177-
@Polyglot override fun randomInt(max: Value, callback: Value?) {
178-
val (safeMin, safeMax) = genSafeRange(Value.asValue(0), max)
179-
val safeCallback: RandomIntCallback? = callback?.let { cb ->
180-
{ err: Throwable?, value: Long? ->
181-
cb.execute(
182-
err?.let { Value.asValue(it) },
183-
value?.let { Value.asValue(it) }
184-
)
185-
}
186-
}
187-
188-
if (safeCallback != null) {
189-
randomInt(safeMin, safeMax, safeCallback)
190-
}
191-
}
192-
193177
@Polyglot override fun randomInt(max: Value): Long {
194178
val (safeMin, safeMax) = genSafeRange(Value.asValue(0), max)
195179
return randomInt(safeMin, safeMax)
@@ -219,7 +203,7 @@ internal class NodeCrypto private constructor () : ReadOnlyProxyObject, CryptoAP
219203
2 -> {
220204
if (lastIsCb) {
221205
// randomInt(max, callback)
222-
this.randomInt(args[0], args.last())
206+
this.randomInt(Value.asValue(0), args[0], args.last())
223207
} else {
224208
// randomInt(min, max)
225209
this.randomInt(args[0], args[1])

packages/graalvm/src/test/kotlin/elide/runtime/node/NodeCryptoTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ import elide.testing.annotations.TestCase
230230
@Test fun `randomInt should throw a RangeError when min is greater than or equal to max`() = conforms {
231231

232232
assertFailsWith<RangeError> { crypto.provide().randomInt(Value.asValue(10L), Value.asValue(10L)) }
233-
assertFailsWith<RangeError> { crypto.provide().randomInt(Value.asValue(10L),Value.asValue( 5L)) }
233+
assertFailsWith<RangeError> { crypto.provide().randomInt(Value.asValue(10L), Value.asValue( 5L)) }
234234
}.guest {
235235
//language=javascript
236236
"""

0 commit comments

Comments
 (0)