alaviss / nim-sys Goto Github PK
View Code? Open in Web Editor NEWAbstractions for common operating system interfaces
Home Page: https://alaviss.github.io/nim-sys
License: MIT License
Abstractions for common operating system interfaces
Home Page: https://alaviss.github.io/nim-sys
License: MIT License
The current version in features/exec-prototype
branch is the API we eventually want, but due to the sheer size it's hard to write all the tests in one go.
So we are gonna pick small parts out and start implementing and testing those instead. Current checklist:
findExe()
program
args
launch()
wait()
The Windows version will be implemented as part of this.
std/streams
is a blocking API, and asyncstreams
doesn't work for byte buffers. We would need to engineer a brand new interface that can support async operations.
It would be much easier if CPS worked...
Current progress:
Listener
listen()
interface
Conn
connect()
interface
At the moment, the Nim compiler remains a big obstacle to the completion of this implementation.
Line 101 in 9459b9e
Hey, great work on this library! Very clean. I hope it's in Nim 2.0!
I'm currently making a PR for a setNonBlocking
proc in Nim. I ran into an issue with FreeRTOS though. Its fcntl
impl seems (I don't know) to be dependent on the hardware and other extensions it is built with. But so far, I don't think anything other than setting socket FDs to non-blocking works.
If the handle types were all distinct in current Nim, then I could do something like this to cover FreeRTOS:
proc setNonBlocking(fd: cint, nonBlocking = true)
when defined freertos:
proc setNonBlocking(fd: FileHandle, nonBlocking = true)
{.error: "Not available for FreeRTOS".}
else:
proc setNonBlocking(fd: FileHandle, nonBlocking = true) =
setNonBlocking(cint(fd), nonBlocking)
proc setNonBlocking(fd: SocketHandle, nonBlocking = true) =
setNonBlocking(cint(fd), nonBlocking)
How would you handle this scenario with your API?
Maybe something like this, where it can be overriden if the user is using a FreeRTOS arch that fully implements fcntl
:
# handles_posix.nim
const hasFileNonBlock {.booldefine.} = defined(posix) and not defined(freertos)
template setBlockingImpl() {.dirty.} =
when hasFileNonBlock and not (fd is SocketFD):
{.error: "Platform doesn't support setting non-block property of file handles.".}
Also, aren't FDs blocking by default? If so, then why have your API be setBlocking
instead of setNonBlocking
? Wouldn't your API require the blocking
param to be set to false
for the most comon use case?
stdin.setBlocking false
stdin.setBlocking true
# vs
stdin.setNonBlocking
stdin.setNonBlocking false
# I don't know, now that I see them both, maybe your way is cleaner.
One more thing: You differentiate sockets from other handles. Why not have a handle for other types like anonymous pipes, etc? This would allow handling of incomplete platform impls like the FreeRTOS example above.
TODO: Write this thing into an actual document
Thanks for taking the time to contribute :)
The project currently enforces these rules:
Commit rule:
<component>(<subcomponent>): <summary>
<detailed description of the change>
For <component>
, use the Nim import path syntax, eg. sys/exec
, sys/pipes
or sys/[exec, pipes]
for changes to both at the same time. Changes affecting the entire project should use sys(treewide)
.
<subcomponent>
usually denotes the specific implementation of a module, for example: sys/exec(posix)
for changes to the POSIX implementation, sys/pipes(windows)
for changes to the Windows implementation. Changes affecting all implementations don't require this part.
<summary>
should provide a short description of what is changed and/or what is the effect of the commit.
The first line should not exceed 72 characters, and must not exceed 80 characters.
The detailed description should explain at least the rationale behind the change, and the change itself in details. The rationale is always required unless covered by the summary, but the details can be omitted if it's trivial or is covered by documentation within the implementation (ie. changing one line due to a typo). Detailed descriptions should be hard wrapped to 72 characters.
All commits staged for merging must follow this rule. No exceptions.
Some examples: 827afaf, a51a859, 6649f5c, 77a199d
Keep commit small if possible, and they need not to compile/pass tests perfectly, though it is preferred that they do.
Feel free to adjust the commit history within your own branch as needed (eg. you used temporary commits and now you want to squash them into one commit, or if you want to rebase your work on top of the current master). However, never change the history if collaboration is being done in the branch.
PR rules:
master
must be accompanied by tests in the same PR.Merge rules for maintainers:
I'm a bit stingy regarding commits, but these strict requirements will help collaboration and people who read the code a lot, trust me :)
Goals:
sys/fs
will be the extension of sys/path
for that purpose).Problems:
On Windows the native encoding is UTF-16. This poses a challenge where typical Nim strings has to converted to UTF-16 before they can be processed.
Right now, the conversion is done using WideCharToMultiByte and its counterpart, which silently drops invalid characters by normalizing them into � (U+FFFD -- Unicode Replacement Character). Furthermore, Windows itself doesn't deal in strict UTF-16, but rather WTF-16 (strings of arbitrary 16-bit codepoints) as a historical artifact.
All of this means that right now, sys
operations on Windows have unexpected behaviors when the input string is not well-formed UTF-8 or when the output is not well-formed UTF-16.
To handle this, add a new string type called strings.Native
(name subjected to change) that is in OS encoding and provide platform-specific conversion into their native code points.
An open question is to figure out what to do about strings.Without[T]
: Whether to have it dealt in strings.Native
or to create a new strings.WithoutNative[T]
.
i think it is possible and not hard to add new ioqueue and procs on common structures which will run syscalls through io_uring
Now that sys
hosts one of the only OS-agnostic async implementation based on CPS, there is a lot of demand for a socket library.
An unordered list of things to do:
Socket
type that works with all sockets.
read()
, write()
interface as with files
. This should allow concepts and friends to match sockets as well as files.TCPSocket
or even TCPClient
types to specialize sockets into different kinds, then implement specialized operations on top.Open questions:
loony
, but it doesn't work with ORC.A lot more research has to be done as the topic is researched further
Target: Vista+
Problems:
PATHEXT
can be run directly (need confirmation on whether CreateProcess
can launch .bat
directly or cmd.exe
has to be used).inherits
is racy on Windows since the inheritable state is global, and we need to restore it afterwards. Probably a lock is the best way atm.Currently files
only provide low-level read/write primitives. These are not buffered, so convenience like readLine
are not available.
Goals:
Stream
can be plugged into BufferedStream
and receive all the perks.Goals:
NullessString
in sys/exec
prototype.sys/exec
's POSIX impl).Problems:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.