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///|
16extern "c" fn uv_os_getenv(
17 name : Bytes
Bytes,
18 bytes : Bytes
Bytes,
19 size : type FixedArray[A]
FixedArray[Int
Int]
20) -> Int
Int = "moonbit_uv_os_getenv"
21
22///|
23pub fn[ToNtbs : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
Convertible to null-terminated byte strings (NTBS).
ToNtbs] (name : ToNtbs) -> String? raise Errno
os_getenv(ToNtbs
name : type parameter ToNtbs
ToNtbs) -> String
String? raise type! Errno Status
Errno {
24 let Bytes
name = ToNtbs
name.(ToNtbs) -> Bytes
to_ntbs()
25 let mut Bytes
buffer = (Int, Byte) -> Bytes
Bytes::(len : Int, init : Byte) -> Bytes
Creates a new byte sequence of the specified length, where each byte is
initialized to the given value. Returns an empty byte sequence if the
length is negative.
Parameters:
length : The length of the byte sequence to create. Must be non-negative.
initial_value : The byte value used to initialize each position in the
sequence.
Example:
let bytes = Bytes::make(3, b'\xFF')
inspect(bytes, content=
#|b"\xff\xff\xff"
)
let empty = Bytes::make(0, b'\x00')
inspect(empty, content="b\"\"")
make(256, 0)
26 let FixedArray[Int]
length : type FixedArray[A]
FixedArray[Int
Int] = [Bytes
buffer.(self : Bytes) -> Int
Returns the number of bytes in a byte sequence.
Parameters:
bytes : The byte sequence whose length is to be determined.
Returns an integer representing the length (number of bytes) of the sequence.
Example:
let bytes = b"\x01\x02\x03"
inspect(bytes.length(), content="3")
let empty = b""
inspect(empty.length(), content="0")
length()]
27 let mut Int
status = (name : Bytes, bytes : Bytes, size : FixedArray[Int]) -> Int
uv_os_getenv(Bytes
name, Bytes
buffer, FixedArray[Int]
length)
28 if Int
status (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")
== Int
_ENOENT {
29 return String?
None
30 }
31 if Int
status (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")
== Int
_ENOBUFS {
32 Bytes
buffer = (Int, Byte) -> Bytes
Bytes::(len : Int, init : Byte) -> Bytes
Creates a new byte sequence of the specified length, where each byte is
initialized to the given value. Returns an empty byte sequence if the
length is negative.
Parameters:
length : The length of the byte sequence to create. Must be non-negative.
initial_value : The byte value used to initialize each position in the
sequence.
Example:
let bytes = Bytes::make(3, b'\xFF')
inspect(bytes, content=
#|b"\xff\xff\xff"
)
let empty = Bytes::make(0, b'\x00')
inspect(empty, content="b\"\"")
make(FixedArray[Int]
length[0], 0)
33 Int
status = (name : Bytes, bytes : Bytes, size : FixedArray[Int]) -> Int
uv_os_getenv(Bytes
name, Bytes
buffer, FixedArray[Int]
length)
34 }
35 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
36 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
37 }
38 (String) -> String?
Some((encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, Bytes
buffer[:FixedArray[Int]
length[0]]))
39}
40
41///|
42extern "c" fn uv_os_setenv(name : Bytes
Bytes, value : Bytes
Bytes) -> Int
Int = "moonbit_uv_os_setenv"
43
44///|
45pub fn[Name : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
Convertible to null-terminated byte strings (NTBS).
ToNtbs, Value : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
Convertible to null-terminated byte strings (NTBS).
ToNtbs] (name : Name, value : Value) -> Unit raise Errno
os_setenv(
46 Name
name : type parameter Name
Name,
47 Value
value : type parameter Value
Value
48) -> Unit
Unit raise type! Errno Status
Errno {
49 let Int
status = (name : Bytes, value : Bytes) -> Int
uv_os_setenv(Name
name.(Name) -> Bytes
to_ntbs(), Value
value.(Value) -> Bytes
to_ntbs())
50 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
51 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
52 }
53}
54
55///|
56extern "c" fn uv_os_unsetenv(name : Bytes
Bytes) -> Int
Int = "moonbit_uv_os_unsetenv"
57
58///|
59pub fn[Name : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
Convertible to null-terminated byte strings (NTBS).
ToNtbs] (name : Name) -> Unit raise Errno
os_unsetenv(Name
name : type parameter Name
Name) -> Unit
Unit raise type! Errno Status
Errno {
60 let Int
status = (name : Bytes) -> Int
uv_os_unsetenv(Name
name.(Name) -> Bytes
to_ntbs())
61 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
62 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
63 }
64}
65
66///|
67type Environ
68
69///|
70extern "c" fn uv_environ_make() -> type Environ
Environ = "moonbit_uv_environ_make"
71
72///|
73extern "c" fn uv_os_environ(environ : type Environ
Environ) -> Int
Int = "moonbit_uv_os_environ"
74
75///|
76pub fn () -> Environ raise Errno
os_environ() -> type Environ
Environ raise type! Errno Status
Errno {
77 let Environ
environ = () -> Environ
uv_environ_make()
78 let Int
status = (environ : Environ) -> Int
uv_os_environ(Environ
environ)
79 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
80 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
81 }
82 Environ
environ
83}
84
85///|
86#borrow(environ)
87extern "c" fn uv_environ_count(environ : type Environ
Environ) -> Int
Int = "moonbit_uv_environ_count"
88
89///|
90#borrow(environ)
91extern "c" fn uv_environ_get_name(
92 environ : type Environ
Environ,
93 index : Int
Int
94) -> type @c.Pointer[_]
@c.Pointer[Byte
Byte] = "moonbit_uv_environ_get_name"
95
96///|
97#borrow(environ)
98extern "c" fn uv_environ_get_value(
99 environ : type Environ
Environ,
100 index : Int
Int
101) -> type @c.Pointer[_]
@c.Pointer[Byte
Byte] = "moonbit_uv_environ_get_value"
102
103///|
104pub fn type Environ
Environ::(self : Environ) -> Iter2[String, String]
iter2(Environ
self : type Environ
Environ) -> type Iter2[A, B]
Iter2[String
String, String
String] {
105 fn (@c.Pointer[Byte]) -> String
read_c_string(@c.Pointer[Byte]
ptr : type @c.Pointer[_]
@c.Pointer[Byte
Byte]) -> String
String {
106 let @buffer.T
buf = (size_hint~ : Int = ..) -> @buffer.T
Creates a new extensible buffer with specified initial capacity. If the
initial capacity is less than 1, the buffer will be initialized with capacity
1.
Parameters:
size_hint : Initial capacity of the buffer in bytes. Defaults to 0.
Returns a new buffer of type T.
Example:
let buf = @buffer.new(size_hint=10)
inspect(buf.length(), content="0")
buf.write_string("test")
inspect(buf.length(), content="8")
@buffer.new()
107 for Int
i = 0; @c.Pointer[Byte]
ptr(@c.Pointer[Byte], Int) -> Byte
[i] (x : Byte, y : Byte) -> Bool
!= 0; Int
i = Int
i (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 {
108 @buffer.T
buf.(self : @buffer.T, value : Byte) -> Unit
Writes a single byte to the end of the buffer. The buffer will automatically
grow if necessary to accommodate the new byte.
Parameters:
buffer : The buffer to write to.
byte : The byte value to be written.
Example:
let buf = @buffer.new()
buf.write_byte(b'\x41')
inspect(buf.contents(), content="b\"\\x41\"")
write_byte(@c.Pointer[Byte]
ptr(@c.Pointer[Byte], Int) -> Byte
[i])
109 }
110 let Bytes
bytes = @buffer.T
buf.(self : @buffer.T) -> Bytes
Returns a copy of the buffer's content as a sequence of bytes.
Parameters:
buffer : The buffer to read from.
Returns a Bytes object containing all bytes written to the buffer.
Example:
let buf = @buffer.new()
buf.write_string("Test")
inspect(
buf.contents(),
content=
#|b"\x54\x00\x65\x00\x73\x00\x74\x00"
,
)
contents()
111 (encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, Bytes
bytes)
112 }
113
114 let Int
count = (environ : Environ) -> Int
uv_environ_count(Environ
self)
115 type Iter2[A, B]
Iter2::(((String, String) -> IterResult) -> IterResult) -> Iter2[String, String]
new(fn((String, String) -> IterResult
yield_) {
116 for Int
i in Int
0..<Int
count {
117 let String
name = (@c.Pointer[Byte]) -> String
read_c_string((environ : Environ, index : Int) -> @c.Pointer[Byte]
uv_environ_get_name(Environ
self, Int
i))
118 let String
value = (@c.Pointer[Byte]) -> String
read_c_string((environ : Environ, index : Int) -> @c.Pointer[Byte]
uv_environ_get_value(Environ
self, Int
i))
119 match (String, String) -> IterResult
yield_(String
name, String
value) {
120 IterResult
IterEnd => break IterResult
IterEnd
121 IterResult
IterContinue => ()
122 }
123 } else {
124 IterResult
IterContinue
125 }
126 })
127}
128