wybe
wybe copied to clipboard
[WIP] Added foreign proc def
Closes #366
This is unfinished. I'd like to make it such that the order of which resources are specified is the order they're passed to the foreign proc. I think I can also make the parsing a little cleaner.
If we want this to be fairly general (ie, able to interface to pretty much any C function), then it's not just the order of resources that needs to be flexible. It should also be possible to interleave the resources among the positional parameters in any order. Also, we should probably support pass by reference for outputs. Perhaps a Wybe in/out parameter should to translated to a single pass-by-reference C parameter.
But maybe it's better to handle the simple cases first, and extend it in later PRs as time and inspiration dictate.
Interleaving can just be done in the verbose way, like:
def foreign c interleave(x, y) use foo {
foreign c interleave(x, foo, y)
}
I don't think there's a very clean way of doing this in the shorthand definition form.
Now that I think about it, we should probably require parameters have their types specified because there's no way of inferring them :)
Yes, types must definitely be specified for all parameters.
Offhand, we could support resources interleaved with normal arguments by forbidding a use clause in the definition, and instead allowing the use keyword to appear in front of a parameter spec, in which case the parameter name is instead interpreted as a resource name (and omitting the type).
Supporting return by reference, while continuing to support normal C return, seems like a trickier issue.
As you say, none of this is really necessary, since you can do without this feature by using explicit foreign
calls. But it would definitely be a convenience for people interfacing to C code.
Converting to draft while I havent made the relevant changes required.
I can do the doc updates.
On coalescing resources, I think this step might not be done yet. It should go into the normalisation phase of resources, which happens just before type checking. I'll double check and fix it up if not.
I did go through the library to see where I could make use of this syntactic sugar. I didn't find any, but that's primarily because the names of the foreign function differs from the Wybe proc.