<pixelherodev>
Are Zig's varargs compatible with C's?
<pixelherodev>
e.g. if a C function calls a zig function and passes it variable args, and the Zig function specifies `args: ...` as the last argument, will it work, or is that one of those "why would you even try that?" things
<andrewrk>
we need bnoordhuis to come in here and re-implement libuv in the zig std lib with async/await
<andrewrk>
I wonder how successful it would be to run a fundraiser to try to hire him for a year
<lunamn>
i'm unsure what would be the process of getting the structs i made for zigdig into stdlib, so i'm looking from a distance
<andrewrk>
lunamn, I didn't realize that zigdig was a candidate for std lib. I'll look more closely now
<andrewrk>
I've been porting musl's implementation of getaddrinfo, but adjusting it to zig std lib
<lunamn>
it can do what we'd mostly want, which is resolving addresses, I haven't looked into musl's
<andrewrk>
do you use poll?
<lunamn>
no, all synchronous
<andrewrk>
lunamn, here's my little test program, with strace attached: https://clbin.com/jmraX
<andrewrk>
this is with -lc -target x86_64-linux-musl. you can see that it sends to multiple servers then uses poll() to block until any of them respond
<andrewrk>
this is a good case to try using async/await and event-based I/O
<andrewrk>
in blocking mode it should probably still do that poll() strategy
<lunamn>
I haven't touched anything regarding async/await yet, so I'm unsure of doing something with it yet
marijnfs_ has quit [Ping timeout: 245 seconds]
<andrewrk>
yeah that's fair
<andrewrk>
btw you were the one who initially did the terminal cli progress implementation stuff right?
<lunamn>
yes
<andrewrk>
I took some liberties when merging, I hope you like it
<lunamn>
yeah I noticed, it's allright
<andrewrk>
thanks for getting things started on that
marijnfs_ has joined #zig
<lunamn>
np! I'm also unsure about doing poll() as a dns client, it feels like doing more work than we're supposed to, as clients
<lunamn>
maybe select a random nameserver, but not send to all three at once
<andrewrk>
hmm, I would at least find out why musl does it this way, it's possible they have a good reason
<lunamn>
that'd be good
<daurnimator>
andrewrk: FWIW I've been playing with more net io abstractions...
<daurnimator>
after lunch I'll go grab out my laptop with that work on it
<lunamn>
oh I see musl does both v4 and v6 queries and poll()s to find whatever comes first, clever, don't know why to all nameservers though (was confused why there were 6 sendto() calls instead of 3)
<andrewrk>
lunamn, why not to all nameservers? makes sense to me to use whichever one is fastest
<daurnimator>
also note that happy eyeballs implies that you shouldn't call dns and then connect yourself
<daurnimator>
instead you should at least have a connectToHostName() function that does both v4 and v6 lookups; and then as soon as each one succeeds, attempt a connect()
<daurnimator>
only after a connect() succeeds do you abort the other protocol
<daurnimator>
additionally: because of TCP fast-open and early data, you can send data in the first packet! this means that you actually need a connectToHostNameAndMaybeSend(host, port, early_data)
<daurnimator>
and that's without bringing TLS into the situation
<daurnimator>
in practice, I prefer an api where you go: S=newConnection(.TCP); S.addConnectHost("foo.com", someport); S.send("somedata"); S.flush(); // only calling flush actually guarantees making all the syscalls
<lunamn>
andrewrk: was confusing udp behavior with tcp behavior where there'd be a bit more of an overhead for every send, which probably means I should sleep
<andrewrk>
daurnimator, that API looks reasonable to me
<daurnimator>
andrewrk: yeah I've been playing with it
<daurnimator>
andrewrk: one question that comes up is error reporting...
<daurnimator>
the other is lifetime of strings you pass to send()
<daurnimator>
the only practical answer I've come up with (unfortunatly) is that S.send *must* copy :(
<daurnimator>
one slight improvement would be enabling something like: `const buf = try S.getSendBuffer(somesize); mem.writeIntLittle(buf[0..4], 42);`
muffindrake has quit [Ping timeout: 250 seconds]
<daurnimator>
in practice I'm hoping that the copy won't matter much: at least for e.g. user-space TLS connections, you can encrypt immediately....
muffindrake has joined #zig
<andrewrk>
copying is really really fast
<andrewrk>
more important is avoiding annoying-but-rare possibilities of error.OutOfMemory
<daurnimator>
ooooo. absurd idea..... okay so I was thinking about sending large constant strings in the current binary..... could you open() yourself and use S.splice()
<daurnimator>
not that large strings embedded in the binary is a common situation
<andrewrk>
why do you need to open yourself? that's the same thing as const foo = "bar"; or @embedFile
<daurnimator>
andrewrk: because with the splice syscall (and eventual socket method) you don't need to read() the string and then send() it on the socket: it goes directly from file on disk to in-kernel socket buffers
<saskwach>
So would it be worth while for someone to flesh out the standard library's docs, or do you think it's still too unstable?
<pixelherodev>
Personally, I think it'd be worth it for large parts
<daurnimator>
saskwach: go for it! some parts are more stable than others
<pixelherodev>
With existing docs there, it'd be simple enough to modify them when they become outdated
<pixelherodev>
It's a lot easier for me personally to update docs written by others than to write them myself
<saskwach>
Well that's all I needed. I don't actually know what I'm doing, but I can at least write docs for the bits of it I use as I use them.
<andrewrk>
daurnimator, ah I see.
<daurnimator>
so a couple of ideas in my head: sometimes you need to do one operation after the other; e.g. sendmsg with an attached FD after some other amount of data.
<daurnimator>
this implies that we can either have a queue of things/writes to do.... or alternatively, that if you have something queued already, that sendmsg with an FD would fail with `error.needsFlush` or something
<daurnimator>
andrewrk: `error.needsFlush` would also be relevant to your "annoying-but-rare possibilities of error.OutOfMemory" comment before
presiden has joined #zig
presiden has left #zig [#zig]
lunamn has quit [Ping timeout: 264 seconds]
lunamn has joined #zig
chemist69 has quit [Ping timeout: 264 seconds]
chemist69 has joined #zig
<saskwach>
Does `Allocator.alloc()` return an uninitialized array? I'm too much of a newbie to be sure.
<pixelherodev>
You can check the source :) Give me a sec to check
<saskwach>
Yeah, I'm looking at the source.
<pixelherodev>
99% sure it's uninitialized
<saskwach>
I'm not sure what `[*]T` means, or what `self.reallocFn(self, ([*]u8)(undefined)[0..0], undefined, byte_count, a)` does.
<saskwach>
Okers, I'll assume it is.
<pixelherodev>
[*]T = pointer to unknown number of T
<pixelherodev>
reallocFn calls the allocator's realloc function
<pixelherodev>
Hint: for C, that's literally realloc
<saskwach>
I probably wouldn't have asked if not for calloc.
<saskwach>
So I guess its initialization state actually depends on the allocator.
<pixelherodev>
If realloc() is called with a null pointer, it's identical to malloc
<pixelherodev>
Unless it's initialized *in alignedAlloc* the answer is it's not initialized at all
<daurnimator>
saskwach: as a rule, zig doesn't zero initialise
<saskwach>
Alright, there's a PR with a little bit of documentation. If it looks sensible I'll do some more tomorrow.
<saskwach>
(3540)
jjido has joined #zig
mahmudov has quit [Ping timeout: 252 seconds]
<pixelherodev>
Hmm, apparently Zig still allows SSE in freestanding for `build-obj` with C source
<mq32>
it works when you have a field count divisible by 8
<mikdusan>
does linux mmap support any way to "clone" an existing mapping just for purposes of changing PROT_ flags? ie: one map is read-only, the other is read+write
<daurnimator>
mikdusan: huh?
<mikdusan>
let's say i have map0 which is PROT_READ. i'd like to have map1 which is the same memory, but PROT_READ|PROT_WRITE
<daurnimator>
mikdusan: I think you can open /proc/self/mem and map it again...
<mikdusan>
oh that is sneaky sneaky
<mq32>
daurnimator, thanks for the issue reference. seems like this is exactly my problem
<mq32>
luckily the workaround is trivial
<mq32>
today is not my day :D
<mq32>
"oh, you fixed that bug? here are another three!"
<mikdusan>
no good deed... :)
<mq32>
seems to
<mq32>
@bitCast fails, @ptrCast works
riba has joined #zig
jokoon has quit [Quit: jokoon]
<mq32>
hmm
<mq32>
can i somehow set the target CPU model?
<mq32>
it looks like i386 and u64 will fail on freestanding
<andrewrk>
oops meant to link to the Editing Source Code section on that last link
<erandria>
thank you
lunamn has quit [Ping timeout: 240 seconds]
<saskwach>
I'm trying to make a very simple library, but getting "unable to find 'foo'" on my `@import("foo");` line. The file is called foo and it's in the same directory as the file trying to import it. What am I missing?
<andrewrk>
saskwach, probably the file extension
lunamn has joined #zig
<saskwach>
Should it not be .zig?
<andrewrk>
.zig is good. but your example does not have .zig in the @import path
<saskwach>
Oh, it's supposed to be "@import("foo.zig")"?
<saskwach>
Huh, so it is.
<saskwach>
Is that the zig equivalent of `#import "file"`? Including the .zig in the module's name?
<andrewrk>
it's very different from C's #import
<saskwach>
er, include
<andrewrk>
oops yes we both meant #include
<andrewrk>
in zig every file is a struct
<saskwach>
So why @import("std") but @import("mything.zig")?
<andrewrk>
@import obtains a reference to that struct type
<andrewrk>
every package has a table of mapped packages with arbitrary names mapped to files
<andrewrk>
all packages have "std" mapped to the std lib
<saskwach>
So the standard library's special cased in the implementation?
<saskwach>
Also, is there a place I can read about this package concept? I saw the bit about @import returning structs in the documentation, but I'm having trouble finding more details. I'm guessing this will get improved when the package system stuff is done.
<andrewrk>
I'm not sure what you're asking, but there are a small set of ways the std lib is special cased. 1. it's shipped with the compiler. 2. the types in std.builtin are used for some of the language features
<andrewrk>
package stuff is one of the big topics of this release cycle
<saskwach>
That will be good stuff.
<saskwach>
I guess I'll go look at some existing libraries and how they're used.
erandria has quit [Remote host closed the connection]
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
waleee-cl has joined #zig
jjido has joined #zig
<tgschultz>
3. "std" package (and "builtin") are automatically available without --pkg-begin/end
<tgschultz>
..which I guess was already covered, so nevermind
erandria has joined #zig
<erandria>
andrewrk: I managed to reproduce the build bug I mentioned
<erandria>
I tried using the `--verbose-ir` option, but the segfault happens before the behaviour of that option "runs"
<erandria>
huh, disregard, it actually works
<mikdusan>
heads-up with `--verbose-ir`, it is not fully-vetted, there are cases (especially with large reproductions) that simply using --verbose-ir cases a segfault. it is highly recommended to get a minimal reduction, and use a few tricks to reduce what the compiler is doing (like define your own panic fn)
<erandria>
oh, I see
<mikdusan>
here is an issue with examples of defining your own panic() and using `export fn ...` to avoid even using main. note this means you need to "zig build-obj" or similar:
<erandria>
but if I build with `build-obj` it doesn't cause a segfault
<erandria>
huh... this is a runtime segfault... I don't know how I missed that
<mikdusan>
oh that's happened to me before too. assumption mother of all evil
<erandria>
I was running `zig build` before, which runs the tests
<erandria>
mikdusan: haha, agree
erandria has quit [Remote host closed the connection]
erandria has joined #zig
kllr_sbstn has joined #zig
erandria has quit [Remote host closed the connection]
<andrewrk>
lunamn, hmmm if we put a fs watch on /etc/resolv.conf, then we could detect when it changes while waiting for DNS results to come back. This would allow zig programs to seamlessly work when someone loses internet and reconnects to a different network mid-request
<lunamn>
andrewrk: that'd be cool
<lunamn>
I was experimenting with poll() earlier but hit a compiler segfault so I went to other things
<andrewrk>
damn, that's annoying
<andrewrk>
I'll take a quick peek
<andrewrk>
lunamn, you can do `zig build` instead of `zig build install --prefix ~/.local/ `
<andrewrk>
it defaults to zig-cache as the install folder
<lunamn>
sure, it became kind of muscle memory after being confused where the binaries were lol
<lunamn>
still segfaults, tho
<andrewrk>
lunamn, here's how you can add an assert to show how to work around the crash at least: https://clbin.com/mj7sZ
<andrewrk>
now it outputs: when analyzing /home/andy/Downloads/zigdig/src/packet.zig:407:48: assertion failed. This is a bug in the Zig compiler.
<andrewrk>
let me look into this though, it appears to be a bug with codegen of async functions
<lunamn>
I was gonna say that was my wild guess
<lunamn>
after commenting/uncommenting lines I got it related to io.Deserializer and being in an async fn
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
wootehfoot has joined #zig
porky11 has quit [Ping timeout: 250 seconds]
<andrewrk>
lunamn, this is a missing compile error for invalid async fn recursion
<lunamn>
oh
<andrewrk>
somehow deserializeName is async
<lunamn>
yeah the deserializer is recursive, that'd make sense
<andrewrk>
but it probably shouldn't be
<andrewrk>
unless it calls read
<andrewrk>
then it makes sense
<lunamn>
it shouldn't call read, I read it before deserialization, check packet.recvDNSPacket
<andrewrk>
I haven't enabled this compile error yet because it's going to be coupled with https://github.com/ziglang/zig/issues/1006 which means even non-async functions will get this error
<lunamn>
so I should heap allocate deserializeName?
<andrewrk>
let me work on the compile errors for this case, hopefully I can make it clear what should be done that way