1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type Random
17
18///|
19extern "c" fn uv_random_make() -> type Random
Random = "moonbit_uv_random_make"
20
21///|
22extern "c" fn uv_random(
23 uv : type Loop Bytes
Loop,
24 req : type Random
Random,
25 buffer : Bytes
Bytes,
26 buffer_offset : Int
Int,
27 buffer_length : Int
Int,
28 flags : Int
Int,
29 random_cb : (type Random
Random, Int
Int, Bytes
Bytes, Int
Int, Int
Int) -> Unit
Unit
30) -> Int
Int = "moonbit_uv_random"
31
32///|
33pub fn type Loop Bytes
Loop::(self : Loop, buffer : @bytes.View, flags : Int, random_cb : (Random, @bytes.View) -> Unit, error_cb : (Random, Errno) -> Unit) -> Unit raise Errno
random(
34 Loop
self : type Loop Bytes
Loop,
35 @bytes.View
buffer : type @bytes.View
A @bytes.View represents a view into a section of a Bytes without copying the data.
Example
let bs = b"\x00\x01\x02\x03\x04\x05"
let bv = bs[1:4]
assert_eq(bv.length(), 3)
assert_eq(bv[0], b'\x01')
assert_eq(bv[1], b'\x02')
assert_eq(bv[2], b'\x03')
@bytes.View,
36 Int
flags : Int
Int,
37 (Random, @bytes.View) -> Unit
random_cb : (type Random
Random, type @bytes.View
A @bytes.View represents a view into a section of a Bytes without copying the data.
Example
let bs = b"\x00\x01\x02\x03\x04\x05"
let bv = bs[1:4]
assert_eq(bv.length(), 3)
assert_eq(bv[0], b'\x01')
assert_eq(bv[1], b'\x02')
assert_eq(bv[2], b'\x03')
@bytes.View) -> Unit
Unit,
38 (Random, Errno) -> Unit
error_cb : (type Random
Random, type! Errno Status
Errno) -> Unit
Unit
39) -> Unit
Unit raise type! Errno Status
Errno {
40 fn (Random, Int, Bytes, Int, Int) -> Unit
uv_random_cb(Random
random, Int
status, Bytes
buffer : Bytes
Bytes, Int
offset, Int
length) {
41 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
42 (Random, Errno) -> Unit
error_cb(Random
random, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
43 } else {
44 (Random, @bytes.View) -> Unit
random_cb(Random
random, Bytes
buffer[Int
offset:Int
offset (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
inspect(42 + 1, content="43")
inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+ Int
length])
45 }
46 }
47
48 let Random
req = () -> Random
uv_random_make()
49 let Int
buffer_offset = @bytes.View
buffer.(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
50 let Int
buffer_length = @bytes.View
buffer.(self : @bytes.View) -> Int
Returns the number of bytes in the view.
Parameters:
bytes_view : The view of a byte sequence.
Returns an integer representing the length of the view.
Example:
let bytes = b"\x00\x01\x02\x03\x04"
let view = bytes[2:4]
inspect(view.length(), content="2")
length()
51 let Bytes
buffer = @bytes.View
buffer.(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
52 let Int
status = (uv : Loop, req : Random, buffer : Bytes, buffer_offset : Int, buffer_length : Int, flags : Int, random_cb : (Random, Int, Bytes, Int, Int) -> Unit) -> Int
uv_random(
53 Loop
self, Random
req, Bytes
buffer, Int
buffer_offset, Int
buffer_length, Int
flags, (Random, Int, Bytes, Int, Int) -> Unit
uv_random_cb,
54 )
55 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
56 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
57 }
58}
59