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///|
16pub extern "c" fn disable_stdio_inheritance() = "moonbit_uv_disable_stdio_inheritance"
17
18///|
19type Process
20
21///|
22pub impl 
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
  loop_(Self) -> Loop
}
ToHandle
for
type Process
Process
with to_handle(self :
type Process
Process
) ->
type Handle
Handle
= "%identity"
23 24///| 25pub impl
trait ToHandle {
  to_handle(Self) -> Handle
  of_handle(Handle) -> Self
  close(Self, (Self) -> Unit) -> Unit
  is_closing(Self) -> Bool
  loop_(Self) -> Loop
}
ToHandle
for
type Process
Process
with of_handle(handle) = "%identity"
26 27///| 28extern "c" fn uv_process_get_pid(process :
type Process
Process
) ->
Int
Int
= "moonbit_uv_process_get_pid"
29 30///| 31pub fn
type Process
Process
::
(self : Process) -> Int
pid
(
Process
self
:
type Process
Process
) ->
Int
Int
{
32 return
(process : Process) -> Int
uv_process_get_pid
(
Process
self
)
33} 34 35///| 36extern "c" fn uv_process_make() ->
type Process
Process
= "moonbit_uv_process_make"
37 38///| 39type ProcessOptions 40 41///| 42priv enum ProcessFlag { 43
ProcessFlag
SetUID
= 1
44
ProcessFlag
SetGID
= 2
45
ProcessFlag
WindowsVerbatimArguments
= 4
46
ProcessFlag
Detached
= 8
47
ProcessFlag
WindowsHide
= 16
48
ProcessFlag
WindowsHideConsole
= 32
49
ProcessFlag
WindowsHideGui
= 64
50
ProcessFlag
WindowsFilePathExactName
= 128
51} 52 53///| 54fn
enum ProcessFlag {
  SetUID
  SetGID
  WindowsVerbatimArguments
  Detached
  WindowsHide
  WindowsHideConsole
  WindowsHideGui
  WindowsFilePathExactName
}
ProcessFlag
::to_int(self :
enum ProcessFlag {
  SetUID
  SetGID
  WindowsVerbatimArguments
  Detached
  WindowsHide
  WindowsHideConsole
  WindowsHideGui
  WindowsFilePathExactName
}
ProcessFlag
) ->
UInt
UInt
= "%identity"
55 56///| 57extern "c" fn uv_process_options_make() ->
type ProcessOptions
ProcessOptions
= "moonbit_uv_process_options_make"
58 59///| 60extern "c" fn uv_process_options_set_exit_cb( 61 process_options :
type ProcessOptions
ProcessOptions
,
62 exit_cb : (
type Process
Process
,
Int64
Int64
,
Int
Int
) ->
Unit
Unit
63) = "moonbit_uv_process_options_set_exit_cb" 64 65///| 66extern "c" fn uv_process_options_set_file( 67 process_options :
type ProcessOptions
ProcessOptions
,
68 file :
Bytes
Bytes
69) = "moonbit_uv_process_options_set_file" 70 71///| 72extern "c" fn uv_process_options_set_args( 73 process_options :
type ProcessOptions
ProcessOptions
,
74 args :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
?]
75) = "moonbit_uv_process_options_set_args" 76 77///| 78extern "c" fn uv_process_options_set_env( 79 process_options :
type ProcessOptions
ProcessOptions
,
80 env :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
?]
81) = "moonbit_uv_process_options_set_env" 82 83///| 84extern "c" fn uv_process_options_set_cwd( 85 process_options :
type ProcessOptions
ProcessOptions
,
86 cwd :
Bytes
Bytes
87) = "moonbit_uv_process_options_set_cwd" 88 89///| 90extern "c" fn uv_process_options_set_flags( 91 process_options :
type ProcessOptions
ProcessOptions
,
92 flags :
UInt
UInt
93) = "moonbit_uv_process_options_set_flags" 94 95///| 96extern "c" fn uv_process_options_set_stdio( 97 process_options :
type ProcessOptions
ProcessOptions
,
98 stdio :
type FixedArray[A]
FixedArray
[
type StdioContainer Bytes
StdioContainer
]
99) = "moonbit_uv_process_options_set_stdio" 100 101///| 102extern "c" fn uv_process_options_set_uid( 103 process_options :
type ProcessOptions
ProcessOptions
,
104 uid :
type Uid Int
Uid
105) = "moonbit_uv_process_options_set_uid" 106 107///| 108extern "c" fn uv_process_options_set_gid( 109 process_options :
type ProcessOptions
ProcessOptions
,
110 gid :
type Gid Int
Gid
111) = "moonbit_uv_process_options_set_gid" 112 113///| 114pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type ProcessOptions
ProcessOptions
::
(file : Ntbs, args : Array[@string.StringView], env? : Array[@string.StringView], cwd? : @string.StringView, stdio? : Array[StdioContainer], uid? : Uid, gid? : Gid, exit_cb : (Process, Int64, Int) -> Unit) -> ProcessOptions
new
(
115
Ntbs
file
:

type parameter Ntbs

Ntbs
,
116
Array[@string.StringView]
args
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
type @string.StringView

A @string.View represents a view of a String that maintains proper Unicode character boundaries. It allows safe access to a substring while handling multi-byte characters correctly.

@string.View
],
117
Array[@string.StringView]?
env
? :
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
type @string.StringView

A @string.View represents a view of a String that maintains proper Unicode character boundaries. It allows safe access to a substring while handling multi-byte characters correctly.

@string.View
],
118
@string.StringView?
cwd
? :
type @string.StringView

A @string.View represents a view of a String that maintains proper Unicode character boundaries. It allows safe access to a substring while handling multi-byte characters correctly.

@string.View
,
119
Array[StdioContainer]?
stdio
? :
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
type StdioContainer Bytes
StdioContainer
],
120
Uid?
uid
? :
type Uid Int
Uid
,
121
Gid?
gid
? :
type Gid Int
Gid
,
122
(Process, Int64, Int) -> Unit
exit_cb
: (
type Process
Process
,
Int64
Int64
,
Int
Int
) ->
Unit
Unit
123) ->
type ProcessOptions
ProcessOptions
{
124 let mut
UInt
flags
= 0U
125 let
ProcessOptions
options
=
() -> ProcessOptions
uv_process_options_make
()
126
(process_options : ProcessOptions, exit_cb : (Process, Int64, Int) -> Unit) -> Unit
uv_process_options_set_exit_cb
(
ProcessOptions
options
,
(Process, Int64, Int) -> Unit
exit_cb
)
127 let
Bytes
file
=
Ntbs
file
.
(Ntbs) -> Bytes
to_ntbs
()
128
(process_options : ProcessOptions, file : Bytes) -> Unit
uv_process_options_set_file
(
ProcessOptions
options
,
Bytes
file
)
129 let
FixedArray[Bytes?]
args
=
type FixedArray[A]
FixedArray
::
(length : Int, value : (Int) -> Bytes?) -> FixedArray[Bytes?]

Creates a new fixed-size array of the specified length, where each element is initialized using a function that maps indices to values.

Parameters:

  • length : The length of the array to create. If length is less than or equal to 0, returns an empty array.
  • initializer : A function that takes an index (from 0 to length - 1) and returns a value of type T for that position.

Returns a new fixed array containing the values produced by applying the initializer function to each index.

Example:

  let arr = FixedArray::makei(3, fn(i) { i * 2 })
  inspect(arr, content="[0, 2, 4]")
makei
(
Array[@string.StringView]
args
.
(self : Array[@string.StringView]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(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
+
1, fn(
Int
i
) {
130 if
Int
i
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Array[@string.StringView]
args
.
(self : Array[@string.StringView]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
131 return
Bytes?
None
132 } else { 133 return
(Bytes) -> Bytes?
Some
(
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
(self : @string.StringView) -> Bytes
::to_ntbs
(
Array[@string.StringView]
args
(Array[@string.StringView], Int) -> @string.StringView

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]))
134 } 135 }) 136
(process_options : ProcessOptions, args : FixedArray[Bytes?]) -> Unit
uv_process_options_set_args
(
ProcessOptions
options
,
FixedArray[Bytes?]
args
)
137 if
Array[@string.StringView]?
env
is
(Array[@string.StringView]) -> Array[@string.StringView]?
Some
(
Array[@string.StringView]
env
) {
138 let
FixedArray[Bytes?]
env
=
type FixedArray[A]
FixedArray
::
(length : Int, value : (Int) -> Bytes?) -> FixedArray[Bytes?]

Creates a new fixed-size array of the specified length, where each element is initialized using a function that maps indices to values.

Parameters:

  • length : The length of the array to create. If length is less than or equal to 0, returns an empty array.
  • initializer : A function that takes an index (from 0 to length - 1) and returns a value of type T for that position.

Returns a new fixed array containing the values produced by applying the initializer function to each index.

Example:

  let arr = FixedArray::makei(3, fn(i) { i * 2 })
  inspect(arr, content="[0, 2, 4]")
makei
(
Array[@string.StringView]
env
.
(self : Array[@string.StringView]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(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
+
1, fn(
Int
i
) {
139 if
Int
i
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Array[@string.StringView]
env
.
(self : Array[@string.StringView]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
140 return
Bytes?
None
141 } else { 142 return
(Bytes) -> Bytes?
Some
(
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
(self : @string.StringView) -> Bytes
::to_ntbs
(
Array[@string.StringView]
env
(Array[@string.StringView], Int) -> @string.StringView

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]))
143 } 144 }) 145
(process_options : ProcessOptions, env : FixedArray[Bytes?]) -> Unit
uv_process_options_set_env
(
ProcessOptions
options
,
FixedArray[Bytes?]
env
)
146 } 147 if
@string.StringView?
cwd
is
(@string.StringView) -> @string.StringView?
Some
(
@string.StringView
cwd
) {
148 let
Bytes
cwd
=
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
(self : @string.StringView) -> Bytes
::to_ntbs
(
@string.StringView
cwd
)
149
(process_options : ProcessOptions, cwd : Bytes) -> Unit
uv_process_options_set_cwd
(
ProcessOptions
options
,
Bytes
cwd
)
150 } 151 if
Array[StdioContainer]?
stdio
is
(Array[StdioContainer]) -> Array[StdioContainer]?
Some
(
Array[StdioContainer]
stdio
) {
152 let
FixedArray[StdioContainer]
stdio
=
type FixedArray[A]
FixedArray
::
(length : Int, value : (Int) -> StdioContainer) -> FixedArray[StdioContainer]

Creates a new fixed-size array of the specified length, where each element is initialized using a function that maps indices to values.

Parameters:

  • length : The length of the array to create. If length is less than or equal to 0, returns an empty array.
  • initializer : A function that takes an index (from 0 to length - 1) and returns a value of type T for that position.

Returns a new fixed array containing the values produced by applying the initializer function to each index.

Example:

  let arr = FixedArray::makei(3, fn(i) { i * 2 })
  inspect(arr, content="[0, 2, 4]")
makei
(
Array[StdioContainer]
stdio
.
(self : Array[StdioContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
(), fn(
Int
i
) { return
Array[StdioContainer]
stdio
(Array[StdioContainer], Int) -> StdioContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i] })
153
(process_options : ProcessOptions, stdio : FixedArray[StdioContainer]) -> Unit
uv_process_options_set_stdio
(
ProcessOptions
options
,
FixedArray[StdioContainer]
stdio
)
154 } 155 if
Uid?
uid
is
(Uid) -> Uid?
Some
(
Uid
uid
) {
156
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
ProcessFlag
SetUID
.
(self : ProcessFlag) -> UInt
to_int
()
157
(process_options : ProcessOptions, uid : Uid) -> Unit
uv_process_options_set_uid
(
ProcessOptions
options
,
Uid
uid
)
158 } 159 if
Gid?
gid
is
(Gid) -> Gid?
Some
(
Gid
gid
) {
160
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
ProcessFlag
SetGID
.
(self : ProcessFlag) -> UInt
to_int
()
161
(process_options : ProcessOptions, gid : Gid) -> Unit
uv_process_options_set_gid
(
ProcessOptions
options
,
Gid
gid
)
162 } 163
(process_options : ProcessOptions, flags : UInt) -> Unit
uv_process_options_set_flags
(
ProcessOptions
options
,
UInt
flags
)
164 return
ProcessOptions
options
165} 166 167///| 168type StdioContainer
Bytes
Bytes
169 170///| 171pub extern "c" fn
type StdioContainer Bytes
StdioContainer
::ignore() ->
type StdioContainer Bytes
StdioContainer
= "moonbit_uv_stdio_container_ignore"
172 173///| 174extern "c" fn uv_stdio_container_create_pipe( 175 stream :
type Stream
Stream
,
176 readable~ :
Bool
Bool
= false,
177 writable~ :
Bool
Bool
= false,
178 non_block~ :
Bool
Bool
= false
179) ->
type StdioContainer Bytes
StdioContainer
= "moonbit_uv_stdio_container_create_pipe"
180 181///| 182pub fn[Stream :
trait ToStream {
  to_stream(Self) -> Stream
  of_stream(Stream) -> Self
  read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  read_stop(Self) -> Unit raise Errno
  write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Unit raise Errno
  listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
  is_readable(Self) -> Bool
  is_writable(Self) -> Bool
  shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream
]
type StdioContainer Bytes
StdioContainer
::
(stream : Stream, readable~ : Bool = .., writable~ : Bool = .., non_block~ : Bool = ..) -> StdioContainer
create_pipe
(
183
Stream
stream
:

type parameter Stream

Stream
,
184
Bool
readable
~ :
Bool
Bool
= false,
185
Bool
writable
~ :
Bool
Bool
= false,
186
Bool
non_block
~ :
Bool
Bool
= false
187) ->
type StdioContainer Bytes
StdioContainer
{
188 return
(stream : Stream, readable~ : Bool, writable~ : Bool, non_block~ : Bool) -> StdioContainer
uv_stdio_container_create_pipe
(
189
Stream
stream
.
(Stream) -> Stream
to_stream
(),
190
Bool
readable
~,
191
Bool
writable
~,
192
Bool
non_block
~,
193 ) 194} 195 196///| 197extern "c" fn uv_stdio_container_inherit_fd(fd :
Int
Int
) ->
type StdioContainer Bytes
StdioContainer
= "moonbit_uv_stdio_container_inherit_fd"
198 199///| 200pub fn
type StdioContainer Bytes
StdioContainer
::
(file : File) -> StdioContainer
inherit_file
(
File
file
:
type File Int
File
) ->
type StdioContainer Bytes
StdioContainer
{
201 return
(fd : Int) -> StdioContainer
uv_stdio_container_inherit_fd
(
File
file
.
(self : File) -> Int

Convert newtype to its underlying type, automatically derived.

inner
())
202} 203 204///| 205pub extern "c" fn
type StdioContainer Bytes
StdioContainer
::inherit_stream(
206 stream :
type Stream
Stream
207) ->
type StdioContainer Bytes
StdioContainer
= "moonbit_uv_stdio_container_inherit_stream"
208 209///| 210type Uid
Int
Int
211 212///| 213type Gid
Int
Int
214 215///| 216extern "c" fn uv_spawn( 217 uv :
type Loop Bytes
Loop
,
218 process :
type Process
Process
,
219 options :
type ProcessOptions
ProcessOptions
220) ->
Int
Int
= "moonbit_uv_spawn"
221 222///| 223pub fn
type Loop Bytes
Loop
::
(self : Loop, options : ProcessOptions) -> Process raise Errno
spawn
(
224
Loop
self
:
type Loop Bytes
Loop
,
225
ProcessOptions
options
:
type ProcessOptions
ProcessOptions
226) ->
type Process
Process
raise
type! Errno Status
Errno
{
227 let
Process
process
=
() -> Process
uv_process_make
()
228 let
Int
status
=
(uv : Loop, process : Process, options : ProcessOptions) -> Int
uv_spawn
(
Loop
self
,
Process
process
,
ProcessOptions
options
)
229 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
230 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
231 } 232 return
Process
process
233} 234 235///| 236extern "c" fn uv_process_kill(process :
type Process
Process
, signum :
Int
Int
) ->
Int
Int
= "moonbit_uv_process_kill"
237 238///| 239pub fn
type Process
Process
::
(self : Process, signum : Signum) -> Unit raise Errno
kill
(
Process
self
:
type Process
Process
,
Signum
signum
:
type Signum Int
Signum
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
240 let
Int
status
=
(process : Process, signum : Int) -> Int
uv_process_kill
(
Process
self
,
Signum
signum
.
(self : Signum) -> Int

Convert newtype to its underlying type, automatically derived.

inner
())
241 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
242 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
243 } 244} 245