Embedded language: Lua vs Common Lisp (ECL) [closed]
Asked Answered
L

2

26

Does anybody here have a experience with Common Lisp as a embedded language (using ECL)? If so, how good is ECL compared to Lua?

Litigant answered 11/7, 2010 at 15:35 Comment(0)
C
26

I haven't embedded CL before, but I've done it with both Lua and two particular Scheme implementations (Gambit-C and GNU Guile).

Scheme makes a great embedded language in my opinion, because it is flexible and not too bloated. Gambit-C is particularly awesome for this because it allows you to both run interpreted scripts, and also compile your code down to C. In my tests, Gambit-C's generated C code was only a little slower than handwritten C (for example, a particular test that ran 0.030s in C was 0.040 in Gambit!). Gambit also has a really nice FFI (foreign function interface), which is essentially just Scheme with special syntax for writing bindings to C libraries (ObjC and C++ are directly supported also). Gambit also has a very nice repl with some debugging capabilities.

Guile is also pretty nice, and it actually runs faster than Lua (fastest interpreted language that I currently know of -- Guile has made great progress in recent years). But since Gambit-C can compile to really fast code, I generally don't use Guile as much unless I intend to use interpreted code in the final version.

Lua has closures, but you won't get continuations like in Scheme, and you also won't get the macros. It's still possible to do a reasonable amount of functional stuff though. It won't have a fully featured object system (like CLOS in CL), but it does have tables and they can be used to implement both class-based inheritance and prototype-based inheritance quite easily. Also, Lua has an excellent C API that is really a pleasure to work with. It's stack-based, and designed in a way that you don't have to worry about the Lua side of memory management at all. The API is very clear and well organized, and there is a lot of great documentation and example code out there. Lua can't compile down, but it does use byte-code (always -- when you send code to the Lua VM, it always compiles that code down the byte-code first and then runs it).

Now, as for Common Lisp, I think that it would probably not make a very good language for embedding. The reason for this is just that CL is huge. Generally, it's desirable to embed a lightweight language because it's going to be using the platform/libs that you provide to it, and not so much external stuff.

So, I think you can't go wrong with either Gambit-C, Guile or Lua. They'll all really nice. CL is powerful, but I just think it's too big for embedding.

Clavicorn answered 13/7, 2010 at 12:9 Comment(6)
No- this is wrong. The Lua API is only great if you've only ever done basic things with it. The stack-based interface is simple, but you have to manually remember what's on every place on the stack and compute the indicies yourself. In addition, the OO is terrible- implementation of inheritance is poor, no constructors/destructors, et cetera. Registering UDTs is also pretty damn poor. I hope you never wanted anything but a POD. I've never used any of the alternatives and it may well be the best out there. But my personal experience was that the language is not good or easy to use.Poorhouse
I wrote a binding to Cairo before and it wasn't hard at all. Just use the userdata type, set __gc for destruction, and call the appropriate luaL_check* function to verify argument types (have to write it for your own types ofc). But I'll agree that it is somewhat more basic and lower level, and if someone wants more than that they'll just need to extend Lua. Regardless, I feel it's a really nice language with a lot of interesting features and is much easier to use than most others.Clavicorn
@DeadMG: I can't comment on the API, since I haven't used it. I velieve that Lua's OO isn't "terrible"; it's just not provided by default, since that's not the language's goal. But there are lots of good libraries that provide an OO environment if you need it. I recommend ObjectLua for that: github.com/sroccaserra/object-luaLazor
@DeadMG: You are wrong, it is possible to have seamless inheritance with userdata with constructors and destructors. You are clearly misinformed as Lua is one of the easiest languages to implement, if not THE easiest (and I've written a massive function consisting almost entirely of Lua C API calls, so I know just how hairy keeping track of the stack can get.)First
Yeah, @DeadMG, you're pretty offbase calling the answer "wrong" because you happen to have a different opinion. Maybe you don't like the Lua API, but it's justifiably very well regarded by many people, as is Lua as a language.Pes
"Lua has closures, but you won't get continuations like in Scheme" Hmmm and what about Lua coroutines?Helicon
S
0

I can only agree that Lua is terrible. It works well when you have a pure imperative functional programming style but not if you try OO with large hierarchies, for example NEVER try to wrap a typical GUI toolkit like GTK in a Lua hierarchy, the performance will be just terrible.

I still use Lua because it's so lightweight that you can have dozens of interpreters running at the same time and end users understand to write code snippets with it while Lisp/Scheme has an expert only (lack of) syntax.

I would now add that mruby 3.0 is out and a great language to embed. Unfortunately in the meantime everyone went Javascript and Javascript only.

Sestertium answered 25/2, 2021 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.