Skip to content

Commit 2184bc4

Browse files
committed
chore(randomInt): update method and related tests.
1 parent 8792bbc commit 2184bc4

File tree

4 files changed

+41
-69
lines changed

4 files changed

+41
-69
lines changed

packages/cli/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,6 @@ val initializeAtRuntime: List<String> = listOfNotNull(
12131213
"io.netty.incubator.codec.quic.BoringSSL",
12141214
"io.netty.incubator.codec.quic.BoringSSLPrivateKeyMethod",
12151215
$$"io.netty.util.NetUtilSubstitutions$NetUtilLocalhost6LazyHolder",
1216-
12171216
// --- Netty: Native Crypto -----
12181217

12191218
"io.netty.internal.tcnative.Buffer",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import elide.vm.annotations.Polyglot
5757
* @param callback
5858
* @return A randomly generated integer between `min` (inclusive) and `max` (exclusive) or nothing if a callback was provided.
5959
*/
60-
@Polyglot public fun randomInt(min: Value?, max: Value, callback: Value?): Any
60+
@Polyglot public fun randomInt(min: Value, max: Value, callback: Value?): Any
6161

6262
/**
6363
* ## Crypto: randomInt

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,26 @@ internal class NodeCrypto private constructor () : ReadOnlyProxyObject, CryptoAP
7979
var error: AbstractJsException? = null
8080

8181
if (min >= max) {
82-
error = RangeError.create("The value of \"max\" is out of range. It must be greater than the value of \"min\" ($min). Received $max")
82+
throw RangeError.create("The value of \"max\" is out of range. It must be greater than the value of \"min\" ($min). Received $max")
8383
}
8484

8585
val randomInt = cryptoRandomGenerator.nextInt(min, max)
8686

87-
if (callback != null) {
88-
callback.invoke(error, randomInt)
89-
90-
return Unit
91-
}
87+
callback?.invoke(error, randomInt)
9288

9389
return randomInt
9490
}
9591

96-
@Polyglot override fun randomInt(min: Value?, max: Value, callback: Value?): Int {
92+
@Polyglot override fun randomInt(min: Value, max: Value, callback: Value?): Any {
9793
return randomInt(min, max, callback)
9894
}
9995

100-
@Polyglot override fun randomInt(max: Value, callback: Value?): Int {
101-
return randomInt(max, callback)
96+
@Polyglot override fun randomInt(max: Value, callback: Value?): Any {
97+
return randomInt(0, max.asInt(), callback as? RandomIntCallback)
10298
}
10399

104-
@Polyglot override fun randomInt(max: Value): Int {
105-
return randomInt(max)
100+
@Polyglot override fun randomInt(max: Value): Any {
101+
return randomInt(0, max = max.asInt())
106102
}
107103

108104
// ProxyObject implementation
@@ -117,17 +113,17 @@ internal class NodeCrypto private constructor () : ReadOnlyProxyObject, CryptoAP
117113
val options = args.getOrNull(0)
118114
randomUUID(options)
119115
}
120-
F_RANDOM_INT -> ProxyExecutable {
116+
F_RANDOM_INT -> ProxyExecutable { args ->
121117
// Node.js signature: randomInt(max) OR randomInt(max, [callback]) OR randomInt(min, max) OR randomInt(min, max, [callback])
122-
when (it.size) {
123-
1 -> randomInt(it.first())
124-
2 -> if (it[1].fitsInInt()) {
125-
randomInt(min = it.first().asInt(), max = it[1].asInt())
118+
when (args.size) {
119+
1 -> randomInt(0, args.first().asInt())
120+
2 -> if (args[1].fitsInInt()) {
121+
randomInt(min = args.first().asInt(), max = args[1].asInt())
126122
} else {
127-
randomInt(max = it.first(), callback = it[1])
123+
randomInt(max = args.first(), callback = args[1])
128124
}
129-
3 -> randomInt(it.first(), it[1], it.getOrNull(2))
130-
else -> throw JsError.typeError("Invalid number of arguments for `crypto.randomInt`")
125+
3 -> randomInt(args.first().asInt(), args[1].asInt(), args.getOrNull(2) as? RandomIntCallback)
126+
else -> throw JsError.typeError("Invalid number of arguments for `crypto.randomInt`, ${args.size} arguments were provided.")
131127
}
132128
}
133129
else -> null

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

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package elide.runtime.node
1414

15+
import org.graalvm.polyglot.Value
16+
import org.junit.jupiter.api.assertThrows
1517
import kotlin.test.Test
1618
import kotlin.test.assertEquals
1719
import kotlin.test.assertIs
@@ -20,7 +22,9 @@ import kotlin.test.assertTrue
2022
import kotlin.test.assertNotNull
2123
import kotlin.test.assertNull
2224
import elide.annotations.Inject
25+
import elide.runtime.intrinsics.js.err.AbstractJsException
2326
import elide.runtime.intrinsics.js.err.RangeError
27+
import elide.runtime.intrinsics.js.err.ValueError
2428
import elide.runtime.node.crypto.NodeCryptoModule
2529
import elide.testing.annotations.TestCase
2630

@@ -211,76 +215,49 @@ import elide.testing.annotations.TestCase
211215
const crypto = require("crypto")
212216
const assert = require("assert")
213217
214-
const int = crypto.randomInt();
215-
assert.equal(typeof int, "Number");
218+
const int = crypto.randomInt(5, 10);
219+
assert.equal(typeof int, "number");
216220
"""
217221
}
218222

219-
@Test fun `randomInt should invoke callback with error when min is greater than or equal to max`() = conforms {
220-
var callbackInvoked = false
221-
crypto.provide().randomInt(10, 5) { err, value ->
222-
callbackInvoked = true
223-
assertNotNull(err, "Error should be provided when min >= max")
224-
assertIs<RangeError>(err, "Error should be of type RangeError")
225-
}
226-
assertTrue(callbackInvoked, "Callback should have been invoked")
223+
@Test fun `randomInt should throw a RangeError when min is greater than or equal to max`() = conforms {
224+
assertThrows<RangeError> { crypto.provide().randomInt(10, 5) }
225+
assertThrows<RangeError> { crypto.provide().randomInt(10, 10) }
227226
}.guest {
228227
//language=javascript
229228
"""
230229
const crypto = require("crypto")
231230
const assert = require("assert")
232231
233-
let callbackInvoked = false;
234-
crypto.randomInt(10, 5, (err, value) => {
235-
callbackInvoked = true;
236-
assert.ok(err, "Error should be provided when min >= max");
237-
assert.equal(err.name, "RangeError", "Error should be of type RangeError");
232+
assert.throws(() => {
233+
crypto.randomInt(10, 10);
234+
}, (err) => {
235+
assert.ok(err instanceof RangeError);
236+
return true;
238237
});
239-
240-
assert.ok(callbackInvoked, "Callback should have been invoked");
241-
"""
242-
}
243-
244-
@Test fun `randomInt should return Unit when a callback is provided`() = conforms {
245-
var callbackInvoked = false
246-
val result = crypto.provide().randomInt(5, 10) { err, value ->
247-
callbackInvoked = true
248-
assertNull(err, "Error should be null for valid range")
249-
assertTrue(value in 5 until 10, "Value should be within the specified range")
250-
}
251-
assertTrue(callbackInvoked, "Callback should have been invoked")
252-
assertEquals(Unit, result, "randomInt should return Unit when a callback is provided")
253-
}.guest {
254-
//language=javascript
255-
"""
256-
const crypto = require("crypto")
257-
const assert = require("assert")
258-
259-
let callbackInvoked = false;
260-
const result = crypto.randomInt(5, 10, (err, value) => {
261-
callbackInvoked = true;
262-
assert.ifError(err);
263-
assert.ok(value >= 5 && value < 10, "Value should be within the specified range");
238+
239+
assert.throws(() => {
240+
crypto.randomInt(10, 5);
241+
}, (err) => {
242+
assert.ok(err instanceof RangeError);
243+
return true;
264244
});
265-
266-
assert.ok(callbackInvoked, "Callback should have been invoked");
267-
assert.equal(result, undefined, "randomInt should return undefined when a callback is provided");
268245
"""
269246
}
270247

271248
@Test fun `randomInt should default min to 0 when only max is provided`() = conforms {
272-
val randomInt = crypto.provide().randomInt(max = 5)
249+
val randomInt = crypto.provide().randomInt(max = 1)
273250
assertIs<Int>(randomInt, "randomInt should return an Int")
274-
assertTrue(randomInt in 0 until 5, "randomInt should be within the range 0 to max")
251+
assertEquals(0, randomInt, "randomInt should be 0 when max is 1")
275252
}.guest {
276253
//language=javascript
277254
"""
278255
const crypto = require("crypto")
279256
const assert = require("assert")
280257
281-
const int = crypto.randomInt(5);
282-
assert.equal(typeof int, "Number");
283-
assert.ok(int >= 0 && int < 5, "randomInt should be within the range 0 to max");
258+
const int = crypto.randomInt(1);
259+
assert.equal(typeof int, "number");
260+
assert.ok(int >= 0 && int < 1, "randomInt should be within the range 0 to max");
284261
"""
285262
}
286263
}

0 commit comments

Comments
 (0)