Replies: 4 comments 8 replies
-
So, with some basic exploration done, the question becomes, do we want to push this into a state where it's actually usable and an official part of LuaSkin? Thoughts @asmagill / @latenitefilms ? |
Beta Was this translation helpful? Give feedback.
-
If anyone wants to play with this, I've pushed up cmsj@d1622d6 with the above extension fully integrated as |
Beta Was this translation helpful? Give feedback.
-
@asmagill @latenitefilms I keep thinking about this and while I think it's a good idea, I keep getting stuck on one thing that's been bugging me for ages - specifically, that our "do useful stuff" code and our "expose the useful stuff to Lua" code are inseparable. Basically I think I would like to create a HammerKit framework that abstracts away all the useful stuff we do, and then call that from the extensions. However, what I'm essentially suggesting here is that we rewrite all of Hammerspoon. We could do it in chunks rather than having to do it all at once, but it's a heck of a lot of work and some of the hairier modules are things I don't really understand because I didn't write them (e.g. I know none of us are overburdened with free time though, so I figured I'd start with the discussion. I think I might have mentioned it a few months ago, when I did a quick experiment to see what it would be like to abstract the useful parts of |
Beta Was this translation helpful? Give feedback.
-
I think this all sounds good, and I too am trying to familiarize myself with Swift, but I'm a little unclear on what you mean by "do useful stuff" code vs "expose useful stuff to lua" code..... do you have an example? |
Beta Was this translation helpful? Give feedback.
-
I think we're probably long past the point where Hammerspoon should get friendlier with Swift. We've built up a lot of great mechanisms for making ObjC<->Lua easy, but as @asmagill noted ~4 years ago in #928 - we are now in a world where most interesting discussions about Apple APIs are happening in Swift.
@heptal previously did some work to create Hammerspoon extensions in Swift, by manually mangling symbol names to match what was the Swift calling convention at the time. I have no idea if that convention is now fixed permanently, but either way it seems like a pretty fragile way of doing things.
I've been looking at a couple of projects which mix Lua and Swift and have come up with the following, as the simplest way I can find, of writing an extension in Swift. It doesn't require any special xcode build settings (other than
SWIFT_VERSION
being set inProject-Base.xcconfig
because for whatever reason it currently resolves tounset
) and can be loaded with one of our normalinit.lua
patterns.It's worth noting though that there are some limitations (which are unchanged since @heptal noted them in 2017):
...
) is not available in Swift, we would need to provide alternatives in LuaSkin that acceptva_list
arguments#define
(ie macros, which a lot of Lua's API is made of) are not available in Swift, but they're relatively easy to manually recreateluaopen_hs_
function (ie the extension's entrypoint after Luadlopen()
s the dylib) needs to be presented with a C calling convention. Swift has had an undocumented, unofficial@_cdecl()
compiler hint for many years and there are efforts underway to legitimise it, so this seems like a low risk to take.So, without further ado, here is the Swift example:
Beta Was this translation helpful? Give feedback.
All reactions