D guest bindings generator - #1561
Conversation
|
This PR is not yet finished. Just opening a draft early to help interested parties keep track. |
|
Thanks! One thing I'd also recommend as you're implementing things is to add Happy to help answer questions about anything in specific if you have them, and if you have any questions about Rust/idioms/etc feel free to leave a comment here and I can dig in. Otherwise I'll leave this be until you're ready, in which case feel free to ping me and I can take a closer look. |
|
Things are coming along nicely, but there's one question I have so far. The Problems arise with the testing of the export wrappers. The way I've implemented exports is using D templates, which are not and cannot be semantically analyzed at all until they have been instantiated. Until an implementation is provided, the wrappers can't be tested. Is this a problem? This gap in the testing? Or are the |
|
A good question! If I understand the problem right I believe you're looking for this. The |
|
Yeah! That's pretty much the answer I'm looking for. Something that can create a very basic stub (no impl; just declarations of functions and types) that I can pump into the exports wrapper soley for tests. Thanks! |
|
I'm still working on getting full test coverage, but I'm ready for this to start getting reviewed. Support for the new async stuff will be tackled in a future PR. Same with |
alexcrichton
left a comment
There was a problem hiding this comment.
Integration points all look good to me 👍. I can't speak much to D idioms and I'm not looking too closesly at crates/d/src/lib.rs, but that's what'd be in your purview
|
For the CI failure, can you publish a |
I'll do that closer to when this is all ready. I might find more small things needing tweaking as I flesh out the tests. No point making more releases than necessary. |
|
Perhaps you have some opinion or suggestions... (e.g. based on the choices made by other bindings)? Up to now I've been hammering this out trying to make the API as symmetric as possible, and deferring certain aspects of memory management to the user. Import parameters are "borrowing" (caller takes a constant reference; callee retains ownership of memory), and returns are owning, where the user has to free when they are done. D's Same applies to exports, where for parameters the API provides you a const "borrow" and frees the memory itself. However you have to move any returned memory to the C heap ( To keep signatures consistent for parameters vs. arguments, a thin But once resources come into play, this starts getting more difficult. Right now you have to drop own handles yourself when you are done, and there are no guardrails to prevent use-after-free. And I just realized that borrow handles provided to you via parameters must ALSO be dropped. I've been allowing quietly coercing owns into borrows, but now one has to keep track of where the So I'm considering maybe bringing in some RAII (and some other D features) to help make this easier to deal with. Specifically, I'm thinking of making resource handles drop themselves on destroy, and disabling copying, making moves explicit. But moving an own handle out of a parameter requires said parameter to be mutable. Lists, etc. also have to be aware of this. So I'm thinking maybe switch things up. Split the types used for parameters from the ones use for returns (which requires duplicating all the record types; making two variants). Parameters can be idiomatic D slices (since lifting lists of lists requires extra copies anyway because of And returns can be wrapped in their own RAII type that handles freeing all the memory and handles contained (if you don't explicitly move particular buffers or handles out). Switching it now would be setting this back, as I have to rework... most of it. Increases the convenience of the bindings, but increases the complexity of the generator. But better to do it before merging, rather than breaking things later? |
|
After some more thought, I think I'll just keep going with the current model, since I'm so close to having something working available. I might switch things up later. Need to get more feedback first. |
|
FWIW I personally find WIT bindings more-or-less unusable without automatic resource management (e.g. the C bindings are basically unusable). If it's possible to have a |
|
Yeah...fair point. Once you have the way to opt-in to RAII, I'm not sure there's a point in being able to opt-out (it just adds more complexity to the generator). There could always be escape-hatches created for those who really need custom control over the lifetimes. But good to know. I'm still just rounding out the rest of the tests and make small tweaks along the way. I think most of the hard ones are out of the way. Then after this is merged I'll probably work on the refactoring. Probably before introducing async. Though I notice I'm going through the tests more thoroughly than some other languages? I'm adding D to every possible test. The only other language that does this consistently is Rust. |
|
For test coverage there's no real reason other than someone hasn't gone through just yet. Don't feel obligated to fill everything out for D, but if you'd like to continue there's also of course no issue with that! |
|
Alright, this is just about finished for the initial implementation. However, I'm having trouble with the two I can't figure out why, but specifically for these two tests, the @wasmImport!("[export]test:resource-alias/e1", "[resource-new]x")
pragma(mangle, "baad")
static private extern(C) uint __import_makeNew(void*);I get strange values like This is on the |
|
Hm ok yeah that's quite fishy. My guess is that this is either a bug in the D toolchain or a bug in wasm-tools. Could you share the test.d compiled core module and/or component? And/or could you share a link of how I could install/run D and build locally on x64 linux? |
[skip ci]
[skip ci]
[skip ci]
|
Crate published with |
Will do that ASAP. So...closer to 4 PM (1 - 2 hours from now) |
Contains both the core module and component, as well as the generated D bindings.
You'll need to install LDC (the LLVM-based D compiler): https://github.com/ldc-developers/ldc You'll need 1.42 (the latest release). If I do: Let me know if you need more details. |
|
In that core wasm I see: (func $_D3wit4test14resource_alias2e17exports1X6__ctorMFNbNcNiNfkZSQCgQCfQCdQBqQBqQBl (;2;) (type 2) (param i32 i32) (result i32)
(local i32)
(local.set 2
(i32.sub
(global.get $__stack_pointer)
(i32.const 16)))
(i32.store offset=12
(local.get 2)
(local.get 1))
(i32.store
(local.get 0)
(i32.load offset=12
(local.get 2)))
(return
(local.get 0))
)which is called by the only caller of the imported resource constructor: (local.set 7
(call $__wit_import_:export:test:resource_alias__e1__:resource_new:x
(i32.load offset=16
(local.get 2))))
(drop
(call $_D3wit4test14resource_alias2e17exports1X6__ctorMFNbNcNiNfkZSQCgQCfQCdQBqQBqQBl
(i32.add
(local.get 2)
(i32.const 8))
(local.get 7)))this ctor function looks a bit suspect insofar as it decrements the stack pointer but never increments it. The calling function, |
|
Interesting... I don't think alleged stack imbalance is actually a problem. Said stack pointer is never written back out to the global |
|
Also, I can cut @wasmImport!("[export]test:resource-alias/e1", "[resource-new]x")
pragma(mangle, "baad")
static private extern(C) uint __import_makeNew(void*);
@witExport("test:resource-alias/e1", "x")
struct XImpl {
uint val;
@witExport("test:resource-alias/e1", "[constructor]x")
static X constructor(uint v) {
auto h = __import_makeNew(null);
return *cast(X*)&h;
//return X.makeNew((out typeof(this) self) {
// self.val = v;
//});
}
}Whatever I put in This gives (import "[export]test:resource-alias/e1" "[resource-new]x" (func $baad (;0;) (type 0)))
...
(func $__wit_export_test:resource_alias__e1:::constructor:x (;26;) (type 0) (param i32) (result i32)
(local i32 i32)
global.get $__stack_pointer
i32.const 16
i32.sub
local.set 1
local.get 1
global.set $__stack_pointer
local.get 1
local.get 0
i32.store offset=12
local.get 1
local.get 1
i32.load offset=12
call $_D4test5XImpl11constructorFkZS3witQBg14resource_alias2e17exports1X
i32.store offset=4
local.get 1
local.get 1
i32.load offset=4
i32.store offset=8
local.get 1
i32.load offset=8
local.set 2
local.get 1
i32.const 16
i32.add
global.set $__stack_pointer
local.get 2
return
)
(func $_D4test5XImpl11constructorFkZS3witQBg14resource_alias2e17exports1X (;27;) (type 0) (param i32) (result i32)
(local i32 i32)
global.get $__stack_pointer
i32.const 16
i32.sub
local.set 1
local.get 1
global.set $__stack_pointer
local.get 1
local.get 0
i32.store offset=12
local.get 1
i32.const 0
call $baad
i32.store offset=8
local.get 1
i32.load offset=8
local.set 2
local.get 1
i32.const 16
i32.add
global.set $__stack_pointer
local.get 2
return
) |
|
With -O3 the above optimizes directly to As simple as it gets. At -O0 I consistently see EDIT: At -Oz it is always 0??? This is really strange. |
Oh, right, yes of course! Ok so I double-checked everything related to wit-component and the component itself and everything checks out. I don't know enough D to understand the syntax/semantics of the generated code. I was curious and threw this at an LLM since I've had some success with debugging in the past with that, and I got this output. I don't understand it myself, but if you're ok wading through some LLM outupt it may be helpful? Otherwise though my guess is leaning towards a bindings generator issue, I just don't know what :( |
|
Yeah, sometimes LLMs be useful. Though it can take some coaxing to reach the actual solution.
Not quite. This is not the issue Furthermore, the lists aren't a consideration here. The problem isn't I've checked, and it is not a problem with some sort of corruption occuring between the components. I can return whatever value I want from the constructor and see This is simply, |
|
O_o The LLM was right. Using explicit |
|
So its nothing wrong with the bindings, I was just using the language wrong. -_- I guess passing a static array like makes a copy of the array THEN slices it. Thanks! |
|
Okay, it's a touch more complicated than that. I was right in my initial understanding: passing But I messed up my Long story short, it works now! And once CI passes, this should be ready to merge! :D So yeah. Thanks and sorry about that! |
alexcrichton
left a comment
There was a problem hiding this comment.
Heh no worries I agree LLM output should be always taken with a very large grain of salt :)
Regardless thanks again for this!
084407c
|
The published crate needs to be updated to account for the last minute changes to Or will this just happen for 0.61.0? |
|
Ah yeah that'll get fixed with 0.61, 0.60 won't actually get used anywhere by the CLI at least. I'll in theory be publishing an 0.61 in the not-too-distant future as well |
Nice. I would like to get #1602 finished before then though. |
Implements a
dsubcrate to support generating bindings for the D programming language.