12

I'm looking into scripting languages to embed into an application.

I've always assumed Lua was the best choice, but I've read some recent news about embedding V8 and was considering using it instead.

My question is two fold:

Does anyone with experience embedding v8 (or any javascript engine) recommend it?

How does it compare with embedding Lua?

I like that v8 has a c++ embedding API. However the Lua API is has had lots of time to be refined (newer isn't always better and all that).

Note: I'm not concerned with which language/library is better or which has better performance. I'm only asking about ease of embedding.

10
  • 3
    gamedev cross post. Commented Jun 13, 2011 at 18:33
  • 1
    You can also embed Lisp, Python, Java and tons of other interpreted languages. Commented Jun 13, 2011 at 18:34
  • @Vlad: Yes I could but I've already eliminated most of those. Lisp is supposed to be insanely easy to embed (and write from scratch). However, Lisp's syntax disqualified it. Python can be embedded, but the general feeling it get from those with experience is it's much more difficult to embed than it should be. Java ... are you trolling me? Maybe not. I didn't even consider compiled langages for use as the scripting language. Various other interpreted langauages where, too small, too niche, or carried too much baggage (eg Ruby's or Python's standard library). Commented Jun 13, 2011 at 18:52
  • 1
    Go with Lua :) I mean, really, V8 is not what you want to embed unless you are embedding it into Chrome browser. Yannick provided a nice URL. Commented Jun 13, 2011 at 18:57
  • FWIW, Python is relatively easy to embed and you can expose functionality very easily using Cython. Commented Jun 13, 2011 at 20:12

8 Answers 8

18
+200

v8 is just ok. I tried to use it as a script interpreter for a video game some time ago with mixed results. On the one hand, it is very fast and the API is simple; but on the other hand it doesn't really do a good job of encapsulating the interpreter's state. Because the code base is littered with global variables, you are basically shit out of luck if you need to reset v8 in the middle of an application, or run it in parallel from multiple threads. These design decisions are understandable from the perspective of Chrome's one-process-per-VM model, but make it somewhat awkward to integrate into something like a game where you might want to run multiple VMs at once (for example in a game server back end), or have some way to quickly serialize/reset the state of the whole interpreter.

For these reasons, I would actually recommend you try giving Lua a second chance. As a language it tends to be much better suited for game programming tasks, plus it has a few nifty features which make game scripting way more convenient (for example, coroutines).

Sign up to request clarification or add additional context in comments.

2 Comments

What is debugging like? That's technically part of embedding, as you need to be able to debug an embedded language from within the language it is embedded with. Lua has lots of nice hooks that allow you to examine state, implement breakpoints, and all kinds of stuff. Do you get that with V8?
Here is a great example of using Node-inspector and chrome as the debugger for embedded v8. It sounds like it's not too difficult to setup, though I haven't try it myself.
4

There was a recent post on HackerNews about the author of Nginx discussing the (non-) suitability of V8 as an embedded scripting language: http://news.ycombinator.net/item?id=2519674

Lua is definitely more geared towards general embedding purposes, while V8 probably can be made to work somehow, if you prefer the familiarity of Javascript.

2 Comments

Very informational link, but to be fair the post was about using v8 in a network server process. To summarize the article, not multithreaded, inflexible out or memory handling, 2ms context creation too long, only supports common architectures (arm, i386, amd64).
news.ycombinator.com/item?id=2519674 fixed link now .com and https
4

Lua is trivial to embed, but the extension API is lower level than V8's. It's a stack based API and you have a handful of primitives to work with. It's no less powerful, it's very robust and doesn't limit you in any way, and if you just want to export global functions into the language, it's a no-brainer. However, exporting C++ objects into Lua requires that you understand Lua's metatables and you may find it very confusing at first. V8 probably makes that more straightforward.

If you want a Lua embedding API that does more work for you, there are libraries like Luabind or ToLua++. Lua doesn't make you pay for what you don't use.

I personaly would not Javascript over Lua. Javascript is a remarkably good language, given that one engineer wrote it in a few weeks, but Lua had a lot more time and thought put into it. It's a CS gem, making the most of a small set of carefully chosen concepts. It does everything Javascript does, but better. It has proper lexical scoping, tail recursion, a very powerful metaprogramming facility which can emulate Javascript's prototype-based inheritence (among other things), coroutines, etc. It's just a cleaner, better language.

One reason I might choose Javascript over it is if I knew my audience already knew Javascript, but I did that once with TCL and lived to regret it (though JS is nowhere near as bad as TCL; you can't go that wrong here).

4 Comments

I don't know Lua, but I do feel the need to point out that JavaScript is now the product of a great deal more than one engineer and a few weeks.
This is true, Eric, but because of the nature of web distribution, certain flaws are unlikely to ever be addressed. With Lua, the authors could make breaking changes to fix language flaws, and often did during Lua's evolution, because if users didn't want to update, they didn't have to. With how the web works, if JavaScript were to make a breaking change, it would break millions of web pages.
JS evolves every time a new browser is introduced. While admittedly this is more often through addition of new features rather than tweaking or removal of old stuff, features do actually get deprecated and eventually pulled or modified. When the time comes to fundamentally alter the core, browsers will likely tack on entirely new interpreters and JITs for that purpose while holding on to the old ones for backwards compatibility. Every language has debatable design compromises but I can't think of anything in JS I would call broken.
I can't think of any I would call broken, either.
2

My personal experience of embedding Lua was that it sucked quite horrifically. The Lua API is only designed for C, and it shows. You could get various wrapping libraries, but they have their own problems.

I haven't tried V8, but the brief overview seems to think that it has useful things like RAII and templates, so I'd vote for that.

1 Comment

+1 for some actual experience. I've had some experience embedding Lua. Nothing large, but more than just trying it out. I didn't find it to be terrible. I think the API is elegant, but unfortunately very C, which makes it very verbose and error prone.
2

Unfortunately I have no experience embedding V8, so I can't directly answer your question, but I've found embedding Lua to be super easy. The C api is verbose, but also very simple and easy to get to grips with, and manages the transitions between Lua and C very effectively.

If C++ is your preferred language, I believe Lua also compiles cleanly as C++ and there are C++ wrappers available for it as well.

Comments

2

v8 can manipulate JSON. and purely lua can't. However, lua has many libraries that is enough to customize your application. But v8 have no libraries. v8 provide only javascript engine. For example, we can't write a file with only v8. For writing a file with v8, you should add API that can access from javascript. If you hope powerful embeding easily, like network access, automation, etc, you hava better to use lua. Or if you hope beautiful, use v8. :)

Sorry for my poor english.

Comments

0

I would suggest v8. Google tends to make nice API's, and it looks pretty easy to embed.

I won't say anything about which is a "better" language, since that is obviously subjective, and you said you don't want to hear that advice. But I can say that Javascript sure has a lot of people who know how to use it.

Comments

0

To reflect your point: just because a scripting language is old, does not make it more refined. Otherwise, bring on Cobol/Fortran/Assembly over C++.

I'd choose v8 over Lua.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.