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 Pipe
17
18///|
19pub fn 
type Pipe
Pipe
::to_handle(self :
type Pipe
Pipe
) ->
type Handle
Handle
= "%identity"
20 21///| 22pub fn
type Pipe
Pipe
::to_stream(self :
type Pipe
Pipe
) ->
type Stream
Stream
= "%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 Pipe
Pipe
with
(self : Pipe) -> Handle
to_handle
(
Pipe
self
) {
26
Pipe
self
.
(self : Pipe) -> Handle
to_handle
()
27} 28 29///| 30pub 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 Pipe
Pipe
with of_handle(self) = "%identity"
31 32///| 33pub impl
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
for
type Pipe
Pipe
with
(self : Pipe) -> Stream
to_stream
(
Pipe
self
) {
34
Pipe
self
.
(self : Pipe) -> Stream
to_stream
()
35} 36 37///| 38pub impl
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
for
type Pipe
Pipe
with of_stream(self) = "%identity"
39 40///| 41extern "c" fn uv_pipe_make() ->
type Pipe
Pipe
= "moonbit_uv_pipe_make"
42 43///| 44extern "c" fn uv_pipe_init(uv :
type Loop Bytes
Loop
, pipe :
type Pipe
Pipe
, ipc :
Bool
Bool
) ->
Int
Int
= "moonbit_uv_pipe_init"
45 46///| 47extern "c" fn uv_pipe_bind(pipe :
type Pipe
Pipe
, name :
Bytes
Bytes
, flags :
UInt
UInt
) ->
Int
Int
= "moonbit_uv_pipe_bind"
48 49///| 50extern "c" fn uv_pipe( 51 fds :
type FixedArray[A]
FixedArray
[
Int
Int
],
52 read_flags :
Int
Int
,
53 write_flags :
Int
Int
54) ->
Int
Int
= "moonbit_uv_pipe"
55 56///| 57pub fn
type Pipe
Pipe
::
(self : Loop, ipc~ : Bool = ..) -> Pipe raise Errno
new
(
Loop
self
:
type Loop Bytes
Loop
,
Bool
ipc
~ :
Bool
Bool
= false) ->
type Pipe
Pipe
raise
type! Errno Status
Errno
{
58 let
Pipe
pipe
=
() -> Pipe
uv_pipe_make
()
59 let
Int
status
=
(uv : Loop, pipe : Pipe, ipc : Bool) -> Int
uv_pipe_init
(
Loop
self
,
Pipe
pipe
,
Bool
ipc
)
60 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
61 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
62 } 63 return
Pipe
pipe
64} 65 66///| 67type PipeBindFlags
UInt
UInt
68 69///| 70const
UInt
PIPE_NO_TRUNCATE
:
UInt
UInt
= 1U
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<<
0
71 72///| 73pub fn
type PipeBindFlags UInt
PipeBindFlags
::
(truncate~ : Bool = ..) -> PipeBindFlags
new
(
Bool
truncate
~ :
Bool
Bool
= true) ->
type PipeBindFlags UInt
PipeBindFlags
{
74 let mut
UInt
flags
= 0U
75 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
truncate
) {
76
UInt
flags
=
UInt
flags
(UInt, UInt) -> UInt
|
UInt
PIPE_NO_TRUNCATE
77 } 78 return
UInt
flags
79} 80 81///| 82pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Pipe
Pipe
::
(self : Pipe, name : Ntbs, flags : PipeBindFlags) -> Unit raise Errno
bind
(
83
Pipe
self
:
type Pipe
Pipe
,
84
Ntbs
name
:

type parameter Ntbs

Ntbs
,
85
PipeBindFlags
flags
:
type PipeBindFlags UInt
PipeBindFlags
86) ->
Unit
Unit
raise
type! Errno Status
Errno
{
87 let
Bytes
name
=
Ntbs
name
.
(Ntbs) -> Bytes
to_ntbs
()
88 let
Int
status
=
(pipe : Pipe, name : Bytes, flags : UInt) -> Int
uv_pipe_bind
(
Pipe
self
,
Bytes
name
,
PipeBindFlags
flags
.
(self : PipeBindFlags) -> UInt

Convert newtype to its underlying type, automatically derived.

inner
())
89 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
90 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
91 } 92} 93 94///| 95pub fn
(read_flags : Int, write_flags : Int) -> (File, File) raise Errno
pipe
(
Int
read_flags
:
Int
Int
,
Int
write_flags
:
Int
Int
) -> (
type File Int
File
,
type File Int
File
) raise
type! Errno Status
Errno
{
96 let
FixedArray[Int]
fds
:
type FixedArray[A]
FixedArray
[
Int
Int
] = [0, 0]
97 let
Int
status
=
(fds : FixedArray[Int], read_flags : Int, write_flags : Int) -> Int
uv_pipe
(
FixedArray[Int]
fds
,
Int
read_flags
,
Int
write_flags
)
98 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
99 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
100 } 101 return (
(Int) -> File
File
(
FixedArray[Int]
fds
[0]),
(Int) -> File
File
(
FixedArray[Int]
fds
[1]))
102} 103 104///| 105extern "c" fn uv_pipe_open(pipe :
type Pipe
Pipe
, file :
Int
Int
) ->
Int
Int
= "moonbit_uv_pipe_open"
106 107///| 108pub fn
type Pipe
Pipe
::
(self : Pipe, file : File) -> Unit raise Errno
open
(
Pipe
self
:
type Pipe
Pipe
,
File
file
:
type File Int
File
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
109 let
Int
status
=
(pipe : Pipe, file : Int) -> Int
uv_pipe_open
(
Pipe
self
,
File
file
.
(self : File) -> Int

Convert newtype to its underlying type, automatically derived.

inner
())
110 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
111 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
112 } 113} 114 115///| 116extern "c" fn uv_pipe_pending_count(pipe :
type Pipe
Pipe
) ->
UInt
UInt
= "moonbit_uv_pipe_pending_count"
117 118///| 119pub fn
type Pipe
Pipe
::
(self : Pipe) -> UInt
pending_count
(
Pipe
self
:
type Pipe
Pipe
) ->
UInt
UInt
{
120
(pipe : Pipe) -> UInt
uv_pipe_pending_count
(
Pipe
self
)
121} 122 123///| 124extern "c" fn uv_pipe_pending_type(pipe :
type Pipe
Pipe
) ->
enum HandleType {
  Unknown
  Async
  Check
  FsEvent
  FsPoll
  Handle
  Idle
  Pipe
  Poll
  Prepare
  Process
  Stream
  Tcp
  Timer
  Tty
  Udp
  Signal
  File
}
HandleType
= "moonbit_uv_pipe_pending_type"
125 126///| 127pub fn
type Pipe
Pipe
::
(self : Pipe) -> HandleType
pending_type
(
Pipe
self
:
type Pipe
Pipe
) ->
enum HandleType {
  Unknown
  Async
  Check
  FsEvent
  FsPoll
  Handle
  Idle
  Pipe
  Poll
  Prepare
  Process
  Stream
  Tcp
  Timer
  Tty
  Udp
  Signal
  File
}
HandleType
{
128 return
(pipe : Pipe) -> HandleType
uv_pipe_pending_type
(
Pipe
self
)
129} 130