Ponyc: Will Pony allow bare methods to use 'this'?

Created on 24 Nov 2018  路  5Comments  路  Source: ponylang/ponyc

Sorry, I'm new to Pony and maybe I got this completely wrong. Is using 'this' in bare methods no good idea because of memory safety reasons or is it rather not yet implemented?

If bare methods could pop the first argument away from the actual list that is provided in Pony (do they already?), and use it as 'this' pointer, then I think I would be able to write a Windows GUI app with Pony.

I made a shim function in C like one of the code examples does it. And the FFI chapter in the tutorial kind of suggests to pass 'this' to SQLite for example. And for a "method", 'this' goes into the this pointer, one would think.

For me, if the Pony GC has wiped an object and another API gives me the dangling pointer to it back, I'm okay when it crashes. I'd use it mostly for the WindowProc, so I'd get it stable after some time.

Currently, I'm not sure. When all I can access from a callback is static classes and actors I feel a bit lost. Though, I have to find out what I could do from there. But someone already stated in another issue, that a static actor cannot find non-static actors.

Maybe I should buffer the Windows messages in the C side and poll them from Pony, instead of using a callback in Pony.

This is a bare Windows app that just tries to do static Windows things in the static part (bare lambda) and non-static things in the main actor. That means in the main actor it looks into the Windows messages after they have been dispatched as good as possible, and then does the rest. It works but sometimes hangs. https://gist.github.com/ponylang-gist/4ee25805123c8c646ce5fc6a1db93975

Most helpful comment

Again, I know almost nothing about Windows APIs, so take this with a grain of salt, but the GWLP_USERDATA option in SetWindowLongPtrA seems promising. It looks like you can use it to attach an arbitrary pointer to the HWND, which is probably what you'd want here.

All 5 comments

From within the bare lambda you only have access to the explicit arguments in the function signature - there is no implicit first argument in that case. I'm not familiar with this Windows API in question, but many C APIs allow you to pass some sort of void* or similar that gets passed to the callback, which would allow you to put a Pony object reference there (though, depending on how the callback gets called by the library, there could potentially be issues with concurrency or with the GC, as you allude).

Again, I know almost nothing about Windows APIs, so take this with a grain of salt, but the GWLP_USERDATA option in SetWindowLongPtrA seems promising. It looks like you can use it to attach an arbitrary pointer to the HWND, which is probably what you'd want here.

Thanks a lot! Using a reference to another class indeed works. In some sense, the issue could be closed, because there's a way to get it done and that's enough. The Windows example would look like this: https://gist.github.com/cec7264c1daa5092adaabd335655554c

Glad to help!

Just for the record for anyone else finding this later, to explicitly answer the question "Will Pony allow bare methods to use 'this'?":

No, Pony won't allow this, by design. Bare lambdas have no value of this, and that's what's "bare" about them. Whenever you think you need this feature, you'll have to find some other way of accomplishing what you're doing, as in the example I helped @marsuk with above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

afritfr picture afritfr  路  7Comments

willemneal picture willemneal  路  7Comments

EpicEric picture EpicEric  路  9Comments

burjui picture burjui  路  9Comments

SeanTAllen picture SeanTAllen  路  9Comments