开发者

Ease of embedding javascript

开发者 https://www.devze.com 2023-03-12 21:38 出处:网络
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 inste

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.

开发者_Python百科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.


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).


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.


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).


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消