Skip to content

Commit 565c34c

Browse files
authored
Merge pull request #253 from thatstoasty/25.3
[WIP] Update for 25.3
2 parents 0c7d999 + ef7ddd3 commit 565c34c

28 files changed

+456
-1098
lines changed

lightbug_http/_libc.mojo

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from utils import StaticTuple
2-
from sys.ffi import external_call
2+
from sys.ffi import external_call, c_char, c_int, c_size_t, c_ssize_t, c_uchar, c_ushort, c_uint
33
from sys.info import sizeof, os_is_windows, os_is_macos, os_is_linux
44
from memory import memcpy, UnsafePointer, stack_allocation
55
from lightbug_http.io.bytes import Bytes
@@ -13,30 +13,9 @@ alias EPROTONOSUPPORT = 93
1313
alias SUCCESS = 0
1414
alias GRND_NONBLOCK: UInt8 = 1
1515

16-
alias char_UnsafePointer = UnsafePointer[c_char]
17-
1816
# Adapted from https://github.com/crisadamo/mojo-Libc . Huge thanks to Cristian!
1917
# C types
2018
alias c_void = UInt8
21-
alias c_char = UInt8
22-
alias c_schar = Int8
23-
alias c_uchar = UInt8
24-
alias c_short = Int16
25-
alias c_ushort = UInt16
26-
alias c_int = Int32
27-
alias c_uint = UInt32
28-
alias c_long = Int64
29-
alias c_ulong = UInt64
30-
alias c_float = Float32
31-
alias c_double = Float64
32-
33-
# `Int` is known to be machine's width
34-
alias c_size_t = Int
35-
alias c_ssize_t = Int
36-
37-
alias ptrdiff_t = Int64
38-
alias intptr_t = Int64
39-
alias uintptr_t = UInt64
4019

4120

4221
# --- ( error.h Constants )-----------------------------------------------------
@@ -579,21 +558,14 @@ fn inet_ntop[address_family: AddressFamily, address_length: AddressLength](ip_ad
579558

580559
# TODO: For some reason, using a pointer instead of a String here leads to a "tried to free invalid ptr crash".
581560
# Ideally we should try not to modify private members of the String.
582-
var dst = String(capacity=address_length.value)
561+
var dst = UnsafePointer[c_char].alloc(address_length.value + 1)
583562
var result = _inet_ntop(
584563
address_family.value,
585-
UnsafePointer.address_of(ip_address).bitcast[c_void](),
586-
dst.unsafe_ptr(),
564+
UnsafePointer(to=ip_address).bitcast[c_void](),
565+
dst,
587566
address_length.value,
588567
)
589568

590-
var i = 0
591-
while i <= address_length.value:
592-
if result[i] == 0:
593-
break
594-
i += 1
595-
dst._buffer._len = i + 1
596-
597569
# `inet_ntop` returns NULL on error.
598570
if not result:
599571
var errno = get_errno()
@@ -607,8 +579,14 @@ fn inet_ntop[address_family: AddressFamily, address_length: AddressLength](ip_ad
607579
else:
608580
raise Error("inet_ntop Error: An error occurred while converting the address. Error code: " + String(errno))
609581

610-
# We want the string representation of the address, so it's ok to take ownership of the pointer here.
611-
return dst^
582+
var i = 0
583+
while i <= address_length.value:
584+
if result[i] == 0:
585+
break
586+
i += 1
587+
588+
# Copy the dst pointer's contents into a new String.
589+
return String(StringSlice(ptr=dst.bitcast[c_uchar](), length=i))
612590

613591

614592
fn _inet_pton(af: c_int, src: UnsafePointer[c_char, mut=False], dst: UnsafePointer[c_void]) -> c_int:
@@ -642,7 +620,7 @@ fn _inet_pton(af: c_int, src: UnsafePointer[c_char, mut=False], dst: UnsafePoint
642620
](af, src, dst)
643621

644622

645-
fn inet_pton[address_family: AddressFamily](src: UnsafePointer[c_char, mut=False]) raises -> c_uint:
623+
fn inet_pton[address_family: AddressFamily](owned src: String) raises -> c_uint:
646624
"""Libc POSIX `inet_pton` function. Converts a presentation format address (that is, printable form as held in a character string)
647625
to network format (usually a struct in_addr or some other internal binary representation, in network byte order).
648626
@@ -679,7 +657,7 @@ fn inet_pton[address_family: AddressFamily](src: UnsafePointer[c_char, mut=False
679657
else:
680658
ip_buffer = stack_allocation[4, c_void]()
681659

682-
var result = _inet_pton(address_family.value, src, ip_buffer)
660+
var result = _inet_pton(address_family.value, src.unsafe_cstr_ptr().origin_cast[mut=False](), ip_buffer)
683661
if result == 0:
684662
raise Error("inet_pton Error: The input is not a valid address.")
685663
elif result == -1:
@@ -847,7 +825,7 @@ fn setsockopt(
847825
#### Notes:
848826
* Reference: https://man7.org/linux/man-pages/man3/setsockopt.3p.html .
849827
"""
850-
var result = _setsockopt(socket, level, option_name, Pointer.address_of(option_value), sizeof[Int]())
828+
var result = _setsockopt(socket, level, option_name, Pointer(to=option_value), sizeof[Int]())
851829
if result == -1:
852830
var errno = get_errno()
853831
if errno == EBADF:
@@ -939,7 +917,7 @@ fn getsockopt(
939917
"""
940918
var option_value = stack_allocation[1, c_void]()
941919
var option_len: socklen_t = sizeof[Int]()
942-
var result = _getsockopt(socket, level, option_name, option_value, Pointer.address_of(option_len))
920+
var result = _getsockopt(socket, level, option_name, option_value, Pointer(to=option_len))
943921
if result == -1:
944922
var errno = get_errno()
945923
if errno == EBADF:
@@ -1089,7 +1067,7 @@ fn getpeername(file_descriptor: c_int) raises -> sockaddr_in:
10891067
* Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html .
10901068
"""
10911069
var remote_address = stack_allocation[1, sockaddr]()
1092-
var result = _getpeername(file_descriptor, remote_address, Pointer.address_of(socklen_t(sizeof[sockaddr]())))
1070+
var result = _getpeername(file_descriptor, remote_address, Pointer(to=socklen_t(sizeof[sockaddr]())))
10931071
if result == -1:
10941072
var errno = get_errno()
10951073
if errno == EBADF:
@@ -1173,7 +1151,7 @@ fn bind(socket: c_int, address: sockaddr_in) raises:
11731151
#### Notes:
11741152
* Reference: https://man7.org/linux/man-pages/man3/bind.3p.html .
11751153
"""
1176-
var result = _bind(socket, Pointer.address_of(address), sizeof[sockaddr_in]())
1154+
var result = _bind(socket, Pointer(to=address), sizeof[sockaddr_in]())
11771155
if result == -1:
11781156
var errno = get_errno()
11791157
if errno == EACCES:
@@ -1330,7 +1308,7 @@ fn accept(socket: c_int) raises -> c_int:
13301308
* Reference: https://man7.org/linux/man-pages/man3/accept.3p.html .
13311309
"""
13321310
var remote_address = sockaddr()
1333-
var result = _accept(socket, Pointer.address_of(remote_address), Pointer.address_of(socklen_t(sizeof[socklen_t]())))
1311+
var result = _accept(socket, Pointer(to=remote_address), Pointer(to=socklen_t(sizeof[socklen_t]())))
13341312
if result == -1:
13351313
var errno = get_errno()
13361314
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
@@ -1432,7 +1410,7 @@ fn connect(socket: c_int, address: sockaddr_in) raises:
14321410
#### Notes:
14331411
* Reference: https://man7.org/linux/man-pages/man3/connect.3p.html .
14341412
"""
1435-
var result = _connect(socket, Pointer.address_of(address), sizeof[sockaddr_in]())
1413+
var result = _connect(socket, Pointer(to=address), sizeof[sockaddr_in]())
14361414
if result == -1:
14371415
var errno = get_errno()
14381416
if errno == EACCES:
@@ -1659,7 +1637,7 @@ fn recvfrom(
16591637
* `MSG_WAITALL`: On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.
16601638
16611639
"""
1662-
var result = _recvfrom(socket, buffer, length, flags, address, Pointer[socklen_t].address_of(sizeof[sockaddr]()))
1640+
var result = _recvfrom(socket, buffer, length, flags, address, Pointer[socklen_t](to=sizeof[sockaddr]()))
16631641
if result == -1:
16641642
var errno = get_errno()
16651643
if Int(errno) in [EAGAIN, EWOULDBLOCK]:
@@ -1858,7 +1836,7 @@ fn _sendto(
18581836
"sendto",
18591837
c_ssize_t,
18601838
c_int,
1861-
UnsafePointer[c_char, mut=False],
1839+
UnsafePointer[c_void, mut=False],
18621840
c_size_t,
18631841
c_int,
18641842
UnsafePointer[sockaddr, mut=False],

lightbug_http/_logger.mojo

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from sys.param_env import env_get_string
2+
from sys import stdout, stderr
23

34

45
struct LogLevel:
@@ -47,17 +48,14 @@ mojo ... -D LB_LOG_LEVEL=DEBUG
4748

4849
@value
4950
struct Logger[level: Int]:
50-
alias STDOUT = 1
51-
alias STDERR = 2
52-
5351
fn _log_message[event_level: Int](self, message: String):
5452
@parameter
5553
if level >= event_level:
5654

5755
@parameter
5856
if event_level < LogLevel.WARN:
5957
# Write to stderr if FATAL or ERROR
60-
print(message, file=Self.STDERR)
58+
print(message, file=stderr)
6159
else:
6260
print(message)
6361

lightbug_http/_owning_list.mojo

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ from memory import Pointer, UnsafePointer, memcpy, Span
77
from collections import Optional
88

99

10-
trait EqualityComparableMovable(EqualityComparable, Movable):
11-
"""A trait for types that are both `EqualityComparable` and `Movable`."""
12-
13-
...
14-
15-
1610
# ===-----------------------------------------------------------------------===#
1711
# List
1812
# ===-----------------------------------------------------------------------===#
@@ -48,10 +42,10 @@ struct _OwningListIter[
4842
@parameter
4943
if forward:
5044
self.index += 1
51-
return Pointer.address_of(self.src[][self.index - 1])
45+
return Pointer(to=self.src[][self.index - 1])
5246
else:
5347
self.index -= 1
54-
return Pointer.address_of(self.src[][self.index])
48+
return Pointer(to=self.src[][self.index])
5549

5650
@always_inline
5751
fn __has_next__(self) -> Bool:
@@ -123,7 +117,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
123117
# Operator dunders
124118
# ===-------------------------------------------------------------------===#
125119

126-
fn __contains__[U: EqualityComparableMovable, //](self: OwningList[U, *_], value: U) -> Bool:
120+
fn __contains__[U: EqualityComparable & Movable, //](self: OwningList[U, *_], value: U) -> Bool:
127121
"""Verify if a given value is present in the list.
128122
129123
Parameters:
@@ -147,7 +141,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
147141
Returns:
148142
An iterator of immutable references to the list elements.
149143
"""
150-
return _OwningListIter(0, Pointer.address_of(self))
144+
return _OwningListIter(0, Pointer(to=self))
151145

152146
# ===-------------------------------------------------------------------===#
153147
# Trait implementations
@@ -170,7 +164,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
170164
return len(self) > 0
171165

172166
@no_inline
173-
fn __str__[U: RepresentableCollectionElement, //](self: OwningList[U, *_]) -> String:
167+
fn __str__[U: Representable & Movable, //](self: OwningList[U, *_]) -> String:
174168
"""Returns a string representation of a `List`.
175169
176170
When the compiler supports conditional methods, then a simple `String(my_list)` will
@@ -180,7 +174,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
180174
181175
Parameters:
182176
U: The type of the elements in the list. Must implement the
183-
traits `Representable` and `CollectionElement`.
177+
traits `Representable` and `Movable`.
184178
185179
Returns:
186180
A string representation of the list.
@@ -190,12 +184,12 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
190184
return output^
191185

192186
@no_inline
193-
fn write_to[W: Writer, U: RepresentableCollectionElement, //](self: OwningList[U, *_], mut writer: W):
187+
fn write_to[W: Writer, U: Representable & Movable, //](self: OwningList[U, *_], mut writer: W):
194188
"""Write `my_list.__str__()` to a `Writer`.
195189
196190
Parameters:
197191
W: A type conforming to the Writable trait.
198-
U: The type of the List elements. Must have the trait `RepresentableCollectionElement`.
192+
U: The type of the List elements. Must have the trait `Representable & Movable`.
199193
200194
Args:
201195
writer: The object to write to.
@@ -208,7 +202,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
208202
writer.write("]")
209203

210204
@no_inline
211-
fn __repr__[U: RepresentableCollectionElement, //](self: OwningList[U, *_]) -> String:
205+
fn __repr__[U: Representable & Movable, //](self: OwningList[U, *_]) -> String:
212206
"""Returns a string representation of a `List`.
213207
214208
Note that since we can't condition methods on a trait yet,
@@ -226,7 +220,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
226220
227221
Parameters:
228222
U: The type of the elements in the list. Must implement the
229-
traits `Representable` and `CollectionElement`.
223+
traits `Representable` and `Movable`.
230224
231225
Returns:
232226
A string representation of the list.
@@ -399,7 +393,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
399393

400394
# TODO: Remove explicit self type when issue 1876 is resolved.
401395
fn index[
402-
C: EqualityComparableMovable, //
396+
C: EqualityComparable & Movable, //
403397
](ref self: OwningList[C, *_], value: C, start: Int = 0, stop: Optional[Int] = None,) raises -> Int:
404398
"""
405399
Returns the index of the first occurrence of a value in a list
@@ -419,7 +413,7 @@ struct OwningList[T: Movable](Movable, Sized, Boolable):
419413
420414
Parameters:
421415
C: The type of the elements in the list. Must implement the
422-
`EqualityComparableMovable` trait.
416+
`EqualityComparable & Movable` trait.
423417
424418
Returns:
425419
The index of the first occurrence of the value in the list.

0 commit comments

Comments
 (0)