3 ms·
What language did you find was easier to embed, and how does it handle the lifetime management that Lua's embedding API handles with its stack? The language it
by kragen 2y ago
What language did you find was easier to embed, and how does it handle the lifetime management that Lua's embedding API handles with its stack?
The language itself may be a matter of taste. I prefer languages which, "in the face of ambiguity, refuse the temptation to guess," but certainly I've spent enough time using Perl, bash, JS, and BASIC to be able to deal with Lua's relatively mild level of DWIMminess.
- le-mark 2y agoI would use quickjs today for embedding over lua. Everyone know js nowadays and quickjs is really nice. The advantage lua has is library support via Lua rocks. Maybe someone can comment on the quickjs ecosystem?
- kragen 2y agoIt sounds like you're saying quickjs is what you'd try if you wanted to embed a language, but you haven't tried embedding quickjs yet?
- garbagelang 2y ago[flagged]
- feznyng 2y agoI think there’s a fork called quickjs-ng that’s actively maintained on GH. Should be able to use it as a lib maybe with some externing. I’ve also had luck using it in a wasm runtime thanks to quickjs-emscripten. Good if you need sandboxing sans full process isolation.
- kragen 2y agoThank you for sharing your experience! Wasm provides full process isolation, doesn't it? Do you mean that QuickJS provides cheaper sandboxing than wasm?
- feznyng 2y agoI’m not sure if wasm would be considered full process isolation but it does provide software based sandboxing. I’m referring to running quickjs in wasm by compiling it with emscripten. https://github.com/justjake/quickjs-emscripten https://github.com/justjake/quickjs-emscripten
- kragen 2y agoI see, thanks!
- dchest 2y agoI have experience of embedding and modifying both QuickJS and Lua. QuickJS is not bad, but Lua is a lot easier and has fewer bugs. JavaScript is a very complicated language under the surface; I think very few people, if any, know and remember all its quirks.
- ga2mer 2y agoAmazfit's Zepp OS uses quickjs to run 3rd party watchfaces and apps in their smartwatch I was surprised to find out I'd been wearing javascript on my arm all this time
- xigoi 2y agoWhile QuickJS is nice, JavaScript as a language is much uglier and more complicated than Lua.
- int_19h 2y agoPython is arguably easier since you don't need to handle non-native stacks at all; you just have a straightforward mapping of Python operations to C functions that you call with regular arguments, as usual.
- kragen 2y agoWith Python's extension API, you have to carefully incref and decref Python values to ensure that you don't either leak memory or deallocate data that's still in use. That's what the Lua stack interface saves you. It's not a call stack, which seems to be what you're thinking of.
- wruza 2y agoLifetimes and references are things to consider in Lua integration code too. See luaL_ref() rationale and why you can’t lua_pop() a string that is in use. The fact that gc doesn’t happen instantly 99.99999% of the times doesn’t save you from thinking about it. Not that this was a hard topic in a language where lifetimes are the main concern and business as usual. In C/etc you can’t hold anything without a ref or some sort of a lifetime guarantee. What makes it special wrt Lua/Python?
- kragen 2y agoYeah, I didn't mean to say you didn't have to think about lifetimes at all when embedding Lua, just that the stack, weird as it is, seems to simplify that kind of thing rather than complicating it (even if you do occasionally have to luaL_ref). But if there's a clearly better alternative, I'd like to know about it. Constantly calling Py_INCREF and Py_DECREF isn't it. Delayed GC seems like it might add headaches for this kind of thing rather than removing them, because your bugs may take a while to show up in testing.
- wruza 2y agoLua stack basically works as an arena if you aren’t eager to clean it. I guess in C one could macro hack himself a similar arena to borrow objects from python runtime. In C++ it’s a matter of befriending a smart pointer with ref/unref calls. It was probably done hundreds of times in all sorts of python embedding wrappers.