aurelian
MModular
•Created by aurelian on 10/22/2024 in #questions
modifying runtime value from @parameter scope crashes the compiler
This is probably not an allowed operation, should there be a different error?
fn cmd_unchecked(args: Tuple) raises -> UnixSocket:
alias args_len = __type_of(args).__len__()
...
slices = InlineArray[StringSlice[__origin_of(args)], args_len]()
@parameter
for i in range(args_len):
slices[i] = args.get[i, StringSlice[__origin_of(args)]]()
...
fn cmd_unchecked(args: Tuple) raises -> UnixSocket:
alias args_len = __type_of(args).__len__()
...
slices = InlineArray[StringSlice[__origin_of(args)], args_len]()
@parameter
for i in range(args_len):
slices[i] = args.get[i, StringSlice[__origin_of(args)]]()
...
/Users/ec2-user/actions-runner/_work/modular/modular/open-source/mojo/stdlib/stdlib/builtin/_startup.mojo:113:4: error: call expansion failed
/Users/ec2-user/actions-runner/_work/modular/modular/open-source/mojo/stdlib/stdlib/builtin/_startup.mojo:68:4: note: function instantiation failed
/Users/ec2-user/actions-runner/_work/modular/modular/open-source/mojo/stdlib/stdlib/builtin/_startup.mojo:113:4: error: call expansion failed
/Users/ec2-user/actions-runner/_work/modular/modular/open-source/mojo/stdlib/stdlib/builtin/_startup.mojo:68:4: note: function instantiation failed
1 replies
MModular
•Created by aurelian on 10/21/2024 in #questions
Segfault when `Tuple.get()` rebinds
fn cmd_unchecked(args: Tuple) raises -> UnixSocket:
buf = List[Byte](capacity=128)
buf.size = 4
@parameter
for i in range(__type_of(args).__len__()):
arg = args.get[i, StringSlice[__origin_of(args)]]() # segfault here
...
fn cmd_unchecked(args: Tuple) raises -> UnixSocket:
buf = List[Byte](capacity=128)
buf.size = 4
@parameter
for i in range(__type_of(args).__len__()):
arg = args.get[i, StringSlice[__origin_of(args)]]() # segfault here
...
1 replies
MModular
•Created by aurelian on 10/20/2024 in #questions
why does this String init work without len?
buf = InlineArray[UInt8, 640](0)
...
s = String(buf.unsafe_ptr())
buf = InlineArray[UInt8, 640](0)
...
s = String(buf.unsafe_ptr())
fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int):
fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int):
52 replies
MModular
•Created by aurelian on 10/19/2024 in #questions
why is this return a copy?
# @value
struct UnixSocket:
var address: Optional[UnixAddress]
var fd: Int32
@staticmethod
fn connect(path: StringSlice) raises -> UnixSocket:
socket = UnixSocket()
address = UnixAddress(path)
connect(socket.fd, address, sizeof[UnixAddress]())
socket.address = address^ # this would also be a copy, easy to forget the ^
return socket # 'UnixSocket' is not copyable because it has no '__copyinit__'
fn accept(self) raises -> UnixSocket:
if not self.address:
raise Error("socket address unintialized")
conn = accept(self.fd, self.address.value(), sizeof[UnixAddress]())
return UnixSocket(fd=conn) # this one is fine, not sure why
# @value
struct UnixSocket:
var address: Optional[UnixAddress]
var fd: Int32
@staticmethod
fn connect(path: StringSlice) raises -> UnixSocket:
socket = UnixSocket()
address = UnixAddress(path)
connect(socket.fd, address, sizeof[UnixAddress]())
socket.address = address^ # this would also be a copy, easy to forget the ^
return socket # 'UnixSocket' is not copyable because it has no '__copyinit__'
fn accept(self) raises -> UnixSocket:
if not self.address:
raise Error("socket address unintialized")
conn = accept(self.fd, self.address.value(), sizeof[UnixAddress]())
return UnixSocket(fd=conn) # this one is fine, not sure why
39 replies
MModular
•Created by aurelian on 10/19/2024 in #questions
Not sure why these strings are missing the last char
buf = InlineArray[UInt8, 32](0)
while True:
client = server.accept()
read = client.read_into(buf)
if read == 0:
print("missing first argument")
continue
var delim: Int
for i in range(read):
if buf[i] == 32:
delim = i
break
else:
print("missing second argument")
continue
span1 = Span(buf)[:delim]
for v in span1:
print(chr(int(v[])), sep="", end="")
print()
span2 = Span(buf)[delim + 1 : read - 1]
for v in span2:
print(chr(int(v[])), sep="", end="")
print()
arg1 = String(Span(buf)[:delim])
arg2 = String(Span(buf)[delim + 1 : read - 1])
print("arg1:", arg1)
print("arg2:", arg2)
buf = InlineArray[UInt8, 32](0)
while True:
client = server.accept()
read = client.read_into(buf)
if read == 0:
print("missing first argument")
continue
var delim: Int
for i in range(read):
if buf[i] == 32:
delim = i
break
else:
print("missing second argument")
continue
span1 = Span(buf)[:delim]
for v in span1:
print(chr(int(v[])), sep="", end="")
print()
span2 = Span(buf)[delim + 1 : read - 1]
for v in span2:
print(chr(int(v[])), sep="", end="")
print()
arg1 = String(Span(buf)[:delim])
arg2 = String(Span(buf)[delim + 1 : read - 1])
print("arg1:", arg1)
print("arg2:", arg2)
move
south
arg1: mov
arg2: sout
move
south
arg1: mov
arg2: sout
16 replies