Which Javascript engine would you embed in your application? [closed]
Asked Answered
E

10

67

I want to embed Javascript in a hobby game engine of mine. Now that we have the 5th generation of Javascript engines out (all blazing fast) I'm curious what engine would you choose to embed in a C++ framework (that includes actual ease of embeding it)?

Note: Just to make it clear, I'm not interested in DOM scripting or writing Javascript in a browser.

Here's a compilation of links so far and some tips from the thread

Just for the record, I love Lua and have already embedded it in game engines about 5 times at work.

However now this is a hobby project, and I think that Javascript being known by most web developers and because its ECMA, Flash and Flex developers, a game engine that uses Javascript and XML for scripting would be more user-friendly and cater to a larger user base (and one that so far has not had a chance to use their skills for games) than one with Lua (and there are plenty of those around!).

Also for the record I'll go with V8 on this one, mostly because I like it's C++ style.

Esoteric answered 18/9, 2008 at 15:40 Comment(6)
I know this is about 3 years too late, but if you're going to embed Javascript, using JSON instead of XML seems a better choice, mostly because the syntax is rather close to JS, and even C++.Quetzal
"JSON ... syntax is rather close to JS". Odd how Javascript Object Notation (JSON) would have similar syntax to Javascript (JS).Maestas
Also, JavaScript has always had a perfect JSON parser. exec()Figge
This is nonsense, JSON is JS.Kotta
@lukasz1985: Wrong: timelessrepo.com/json-isnt-a-javascript-subsetKalasky
Javascript objects do not need quotes such as "somekey":"some value" where as valid JSON requires quotes.Uund
H
21

Mozilla's SpiderMonkey is fairly easy and well-documented. It's a C API, but it's straightforward to wrap it in C++. It can be compiled to be thread-safe, which is useful for games since you'd likely want to have your main logic in one thread and user interface logic in a second thread.

Google's V8 might be a good choice, since you're using C++, but I have no experience with it yet. According to the documentation (thanks to Daniel James), V8 is not thread-safe, although this may change in the future.

There's also WebKit's SquirrelFish, but I couldn't find a standalone version of that when I was looking earlier.

Handlebar answered 18/9, 2008 at 15:41 Comment(3)
I added some information about v8's thread safety in my post (too long for a comment). Basically, it isn't really thread safe.Cargian
Does anyone know if V8 has become threadsafe? It has been 4 years since these comments after all. Thx. :)Ohaus
@sidewinderguy: It seems there is explicit support for multi-threaded applications. V8 supports the concept of Isolate objects allowing you to run separate interpreters in different threads. However, interpreters are not thread-safe and using the same interpreter across multiple threads still requires locking of all interactions with the interpreter (including accessing object handles).Haggai
I
20

I've tried both SpiderMonkey and V8. With SpiderMonkey, I couldn't get anything to work. I couldn't even get the examples on mozilla.org to compile.

V8 worked out-of-the-box and I got some basic C++ <-> Javascript interaction going pretty quickly. There are some google lists for people using V8, and I found most of my questions answered there already.

Intelligible answered 10/10, 2008 at 4:17 Comment(3)
Thanks for the "answer/comment" I totally agree with you on this I got V8 up and running, and could begin using it within an hour. It's really very easy to grasp and well modularizedEsoteric
I'd love to find out whether V8 truly is slower than SquirrelFish Extreme in real world game programming examples. Any chance you could try them both Mr Gould?Allanson
As an update, I re-picked up the project I was working on several months ago with v8. I grabbed the latest version of v8, and my program compiles and runs just like it did before. It's a very stable API. I'm duly impressed.Intelligible
H
8

Is Java Script really the right language for your game? Many of games out there are using the Lua programming language for scripting. It's easy to integrate, it's very small, it compiles on almost every platform and it's easy to learn.

This somewhat off topic, but thinking outside the box can be important to get things right .

Heteronym answered 18/9, 2008 at 17:26 Comment(1)
The advantage of javascript comes when you already know the language (or any of the other ecmascript derivatives), plus it's a lot similar to C++ or Java than Lua is. However, I do agree Lua is pretty awesome, tiny footprint and super easy to embed.Mason
C
8

I believe that v8 only works on x86, x64 and arm processors at the moment. Which might be a disadvantage.

With regards to thread safety, from include/v8.h:

 * Multiple threads in V8 are allowed, but only one thread at a time
 * is allowed to use V8.  The definition of 'using V8' includes
 * accessing handles or holding onto object pointers obtained from V8
 * handles.  It is up to the user of V8 to ensure (perhaps with
 * locking) that this constraint is not violated.

You can read more in the source file (it looks like doxygen documentation, but they don't seem to have put it up anywhere).

Update: That comment has been removed, probably some time ago. It looks like v8 now has an Isolate object which represents an instance of the engine. A single Isolate instance can only be used in a single thread at a time, but other Isolate instances can be used in other threads at the same time.

Cargian answered 18/9, 2008 at 21:29 Comment(2)
Compiles natively to x64 now, too.Intelligible
Thanks, I've updated it. But feel free to edit it if you can.Cargian
A
7

When speaking of a scripting engine and c++ you could also consider chaiscript. It is close to ecma script (~Javascript) and very easy to embed in c++.

Seller from the webpage:

... ChaiScript, on the other hand, was designed from the ground up with integration with C++ in mind. ... ChaiScript has no meta-compiler, no library dependencies, no build system requirements and no legacy baggage of any kind. At can work seamlessly with any C++ functions you expose to it. It does not have to be told explicitly about any type, it is function centric.

With ChaiScript you can literally begin scripting your application by adding three lines of code to your program and not modifying your build steps at all.

Althorn answered 30/11, 2011 at 5:21 Comment(0)
S
6

The benchmark that came out when V8 first hit the scene that showed V8 being 1000% (or whatever) faster than other engines was heavily weighted towards favoring engines that were good at recursion. If your code uses a lot of recursion, then V8 might give you a significant advantage, speed-wise. For "real world" (currently, at least) web stuff, SquirrelFish Extreme seems to be the hands down winner at the moment (see my blog post on the topic for the results of my own, informal testing).

As others have pointed out, ease of integration and quality of documentation might prevail over pure speed. It don't mean jack if you don't ship!

Sevigny answered 19/9, 2008 at 2:30 Comment(1)
+1 It don't mean jack if you don't ship!Contestant
A
3

I'd wait for TraceMonkey, the next evolution of SpiderMonkey to come out. Faster and better designed. ( Uses code donated from Adobe Flash ).

Tracemonkey prides itself in making repetitious actions much faster by aggressively optimizing the structure at runtime based on actual usage, which aught to be handy for game-augmentation.

Accrete answered 18/9, 2008 at 15:52 Comment(1)
TraceMonkey's designed to be backwards-compatible with the existing SpiderMonkey API, so designing your application around SpiderMonkey means that you'd get TraceMonkey "for free" when it's ready.Handlebar
B
2

Try Javascript .NET:

http://javascriptdotnet.codeplex.com/

It implements Google V8. You can compile and run Javascript directly from .NET code with it, and supply CLI objects to be used by the Javascript code as well. And V8 is probably the best engine ever created in terms of performance, it generates native code from Javascript.

Bazemore answered 8/7, 2010 at 12:2 Comment(0)
T
1

You may also want to look at V8 from Google. It's pretty new, though.

Torrent answered 18/9, 2008 at 15:57 Comment(0)
C
1

I would keep an eye on v8 as it is screaming fast javascript engine, and i'm sure it will develop cross-platform support as it grows to maturity.

Cl answered 18/9, 2008 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.