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 Loop 
Bytes
Bytes
17 18///| 19extern "c" fn uv_loop_make() ->
type Loop Bytes
Loop
= "moonbit_uv_loop_make"
20 21///| 22extern "c" fn uv_loop_init(uv :
type Loop Bytes
Loop
) ->
Int
Int
= "moonbit_uv_loop_init"
23 24///| 25/// Creates a new event loop instance. 26/// 27/// Returns a new event loop that can be used for asynchronous I/O operations. 28/// 29/// Throws an error of type `Errno` if the loop initialization fails due to 30/// system resource constraints or other platform-specific issues. 31/// 32/// Example: 33/// 34/// ```moonbit 35/// let uv = @uv.Loop::new() 36/// uv.close() 37/// ``` 38pub fn
type Loop Bytes
Loop
::
() -> Loop raise Errno

Creates a new event loop instance.

Returns a new event loop that can be used for asynchronous I/O operations.

Throws an error of type Errno if the loop initialization fails due to system resource constraints or other platform-specific issues.

Example:

let uv = @uv.Loop::new()
uv.close()
new
() ->
type Loop Bytes
Loop
raise
type! Errno Status
Errno
{
39 let
Loop
uv
=
() -> Loop
uv_loop_make
()
40 let
Int
status
=
(uv : Loop) -> Int
uv_loop_init
(
Loop
uv
)
41 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
42 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
43 } 44 return
Loop
uv
45} 46 47///| 48extern "c" fn uv_loop_close(uv :
type Loop Bytes
Loop
) ->
Int
Int
= "moonbit_uv_loop_close"
49 50///| 51/// Closes the event loop and releases its associated resources. 52/// 53/// Parameters: 54/// 55/// * `self` : The event loop to close. 56/// 57/// Throws an error of type `Errno` if the loop cannot be closed properly, such 58/// as when there are still active handles or requests associated with the loop. 59/// 60/// Example: 61/// 62/// ```moonbit 63/// let uv = @uv.Loop::new() 64/// uv.close() 65/// ``` 66pub fn
type Loop Bytes
Loop
::
(self : Loop) -> Unit raise Errno

Closes the event loop and releases its associated resources.

Parameters:

  • self : The event loop to close.

Throws an error of type Errno if the loop cannot be closed properly, such as when there are still active handles or requests associated with the loop.

Example:

let uv = @uv.Loop::new()
uv.close()
close
(
Loop
self
:
type Loop Bytes
Loop
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
67 let
Int
status
=
(uv : Loop) -> Int
uv_loop_close
(
Loop
self
)
68 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
69 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
70 } 71} 72 73///| 74pub(all) enum RunMode { 75
RunMode
Default
= 0
76
RunMode
Once
= 1
77
RunMode
NoWait
= 2
78} 79 80///| 81extern "c" fn uv_run(uv :
type Loop Bytes
Loop
, mode :
enum RunMode {
  Default
  Once
  NoWait
}
RunMode
) ->
Int
Int
= "moonbit_uv_run"
82 83///| 84/// Runs the event loop with the specified mode. 85/// 86/// Parameters: 87/// 88/// * `self` : The event loop to run. 89/// * `mode` : The run mode that determines how the loop executes. Can be 90/// `Default` (runs until no more active handles), `Once` (runs once and 91/// returns), or `NoWait` (polls for I/O once but doesn't block). 92/// 93/// Throws an error of type `Errno` if the loop fails to run properly. 94/// 95/// Example: 96/// 97/// ```moonbit 98/// let uv = @uv.Loop::new() 99/// uv.run(Default) 100/// uv.close() 101/// ``` 102pub fn
type Loop Bytes
Loop
::
(self : Loop, mode : RunMode) -> Unit raise Errno

Runs the event loop with the specified mode.

Parameters:

  • self : The event loop to run.
  • mode : The run mode that determines how the loop executes. Can be Default (runs until no more active handles), Once (runs once and returns), or NoWait (polls for I/O once but doesn't block).

Throws an error of type Errno if the loop fails to run properly.

Example:

let uv = @uv.Loop::new()
uv.run(Default)
uv.close()
run
(
Loop
self
:
type Loop Bytes
Loop
,
RunMode
mode
:
enum RunMode {
  Default
  Once
  NoWait
}
RunMode
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
103 let
Int
status
=
(uv : Loop, mode : RunMode) -> Int
uv_run
(
Loop
self
,
RunMode
mode
)
104 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
105 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
106 } 107} 108 109///| 110extern "c" fn uv_stop(uv :
type Loop Bytes
Loop
) = "moonbit_uv_stop"
111 112///| 113pub fn
type Loop Bytes
Loop
::
(self : Loop) -> Unit
stop
(
Loop
self
:
type Loop Bytes
Loop
) ->
Unit
Unit
{
114
(uv : Loop) -> Unit
uv_stop
(
Loop
self
)
115} 116 117///| 118extern "c" fn uv_walk(uv :
type Loop Bytes
Loop
, walk_cb : (
type Handle
Handle
) ->
Unit
Unit
) = "moonbit_uv_walk"
119 120///| 121pub fn
type Loop Bytes
Loop
::
(self : Loop, walk_cb : (Handle) -> Unit) -> Unit
walk
(
Loop
self
:
type Loop Bytes
Loop
,
(Handle) -> Unit
walk_cb
: (
type Handle
Handle
) ->
Unit
Unit
) ->
Unit
Unit
{
122
(uv : Loop, walk_cb : (Handle) -> Unit) -> Unit
uv_walk
(
Loop
self
,
(Handle) -> Unit
walk_cb
)
123} 124 125///| 126extern "c" fn uv_loop_alive(uv :
type Loop Bytes
Loop
) ->
Int
Int
= "moonbit_uv_loop_alive"
127 128///| 129pub fn
type Loop Bytes
Loop
::
(uv : Loop) -> Bool
alive
(
Loop
uv
:
type Loop Bytes
Loop
) ->
Bool
Bool
{
130 return
(uv : Loop) -> Int
uv_loop_alive
(
Loop
uv
)
(x : Int, y : Int) -> Bool
!=
0
131} 132