
Duktape: an embeddable JavaScript engine - brassybadger
http://duktape.org/index.html
======
chubot
This looks really cool. I always wanted a small embeddable JS engine with a C
interface. And it looks like you modelled the C API after the Lua C API? That
is what I wanted as well.

Here are some related projects I saved links to. I think there were a few
others.

[http://code.google.com/p/tiny-js/](http://code.google.com/p/tiny-js/)

[http://adaptive-enterprises.com/~d/software/see/](http://adaptive-
enterprises.com/~d/software/see/)

[http://sourceforge.net/projects/njs/](http://sourceforge.net/projects/njs/)

v8 is an obvious open source implementation, because it's packaged separately
(and used in node.js), but its API is C++ and relatively complex. For a lot of
applications, it's overkill.

~~~
saurik
JavaScriptCore (the engine from WebKit) also has a very simple C interface
(and I've seen it embedded by people as a static library without serious
difficulty).

~~~
yoklov
It's LGPL though, which is too restrictive for some projects (especially as a
static library).

~~~
saurik
FWIW, the only times I've seen it as a static library (and the only real place
you need a static library) is on iOS (where it was actually extra-dubious and
I'm surprised more people didn't point out the problems; specifically this was
as part of one of those JavaScript app frameworks like Titanium, although I
don't quite remember if it was specifically Titanium), and JavaScriptCore is
now provided on the system by Apple as a public framework as of iOS 7. On any
other platform you should have no trouble shipping it as a dynamically-linked
library due to its simple C API.

------
deckiedan
Awesome. This is exactly what I wanted. Hopefully now I can make a standalone
statically linked version jslint, lessjs, etc, so I can use them as part of my
workflow without needing the whole flipping Node stack installed...

~~~
cmicali
This would be amazing.. svgo and less are the only two things between us and a
node-free environment - would love to not have to install/deal with npm.

------
PythonicAlpha
The idea is good, I remember that other projects like Avidemux integrate
QtScript or Spidermonkey for scripting.

Has anybody experience, how the integration overhead is? Looks as this could
be integrated very simply. The problem is, that it is in alpha stage and how
stable and complete is it currently?

~~~
brassybadger
Integration is very easy (check out the examples in their git repo), but this
is still alpha quality software - there are known limitations, e.g. the one I
ran into is that a large JS expression involving function calls might make
Duktape run out of bytecode registers.

------
bichiliad
What is the advantage of something like this? (Not trying to be cynical, I
just have no idea).

~~~
Pitarou
_Developer Productivity_

C/C++ is great for low-level and performance sensitive code, but at the cost
of ease of use and a potentially slow compile-test-debug cycle. For instance,
in a game engine, the Physics and graphics rendering are usually done in C++.
But for the rest, you get more bang-for-your-buck with a higher-level
language.

 _Mindshare_

Other high-level languages can do the same thing. In fact, _Lua_ and _Guile_
were designed for this exact purpose. But Javascript works well and Javascript
programmers are much easier to find. And if, say, you want your users to start
writing plug-ins for your application, you'll have more success with a
language they're more likely to know.

 _Performance_

Thanks to the great Javascript JIT compilers, you'll probably take a smaller
performance hit than with other high-level languages you might choose.

------
sitkack
[http://www.reddit.com/r/programming/comments/1uv5n0/duktape_...](http://www.reddit.com/r/programming/comments/1uv5n0/duktape_an_embeddable_javascript_engine/)

------
skrebbel
Is size the main selling point? 4 years ago or so, I integrated SpiderMonkey
into a C++ program with relative ease; it has an easy to understand C API and
no dependencies.

~~~
brassybadger
I'd say the combination of being small, easily embeddable and having a liberal
license make it attractive.

~~~
skrebbel
SpiderMonkey is easily embeddable like I said. The Mozilla Public License is
less liberal than BSD/MIT, but for most practical purposes it's the same.

It's not tiny though (for an underbelly feeling, I found an old binary Windows
build online of about 800kb).

Looking at the current SpiderMonkey docs, things look like they've only
improved. Back in the day, I just took SpiderMonkey from the "js" subdirectory
of the Firefox repo. They split it off and documented build steps since then.

I'm not saying there's no space for contenders here, but don't underestimate
the quality of what's out there already. SpiderMonkey really is remarkably
easy to interface with.

~~~
brassybadger
After a quick glance it also seems that newer versions of SpiderMonkey only
offer a C++ API, so you can't embed it anymore into C apps without some glue
code. I checked out jsapi.h in SpiderMonkey24 - maybe some trick is needed to
use SM from C?

~~~
pcwalton
New versions of SpiderMonkey only expose a C++ API (because of exact rooting),
but we'll need to fix that at some point as part of Servo since this is a
hazard for Rust. Currently we use an old version of SpiderMonkey for this
reason.

------
igl
Great... If every aspect of lua wasn't better than js.

~~~
Turing_Machine
Lua is nice, no doubt about it, but it isn't better in every aspect.

Libraries count. Developer mindshare counts.

It's the same rationale as having so many modern languages that compile to
Java. Java itself may not be that great, but man, there sure are a lot of
useful libraries for it. Same with Javascript.

------
angersock
Bloody wonderful!

I'm quite pleased to see a compact and reasonable C interface for a JS engine
--v8 can go eat a bag of rocks.

~~~
eropple
Agreed. I really like the engineering behind V8, but the interface is _such a
pain_ to work with. I actually switched my game from C++ to the JVM so I could
use Rhino.

~~~
_pmf_
> I actually switched my game from C++ to the JVM so I could use Rhino.

Would you mind elaborating a bit what makes v8 tedious to work with?

~~~
angersock
[https://developers.google.com/v8/embed](https://developers.google.com/v8/embed)

EDIT:

V8 is quite powerful, don't get me wrong. But, it requires a bit more
knowledge of C++ to use and use well, and sometimes that can be a bit much.
Also, if you're exporting functionality via a static library, you really don't
always want to use anything other than vanilla C.

------
icefox
Why does the readme say "liberal license" when the license file is in fact
"MIT license" Why hide the fact that it is under the MIT license? Why is there
a folder called licenses and why does it contain an unfilled in license
(Copyright (c) <year> <copyright holders>)?

~~~
drgath
MIT is considered by most to be a "liberal license", and phrasing it that way
makes for better marketing than stating the exact type of license in a
"Features" section.

The additional license is for another project (murmurhash2).

------
bsilvereagle
If this is embedded in C++ I feel like you would be manipulating objects in
the JavaScript scripts. JavaScript has OOP bolted on and it is not nearly as
nice as Lua or similar alternatives. Why would a developer embed a JavaScript
engine compared to something already existing?

~~~
yetfeo
I wouldn't describe JavaScript's OOP as bolted on. It's just a different kind
of OOP - prototype based rather than class based. You could argue that Lua is
more "bolted on OOP" since there are quite a few different libraries that
build OOP for it.

~~~
anonymoushn
That would be a silly argument. Lua's metatables are strictly more expressive
than js's prototypes, and few libraries assume the presence of any sort of OO.

------
PaulFreund
It always amazes me how often topics I currently work on come up here. I am
currently thinking about using
[http://www.chaiscript.com/](http://www.chaiscript.com/)

------
NAFV_P
Is the name "Duktape" a reference to Perl [0]?

[0]
[http://www.salon.com/1998/10/13/feature_269/](http://www.salon.com/1998/10/13/feature_269/)

------
edwinyzh
Sounds great! I wish there will be a DLL version for Windows, so that people
can make wrappers in other languages such as Pascal, Python, etc.

------
Doctor_Fegg
Any benchmarks for how this typically performs against Lua?

------
dottrap
Very interesting. How large is the binary size?

~~~
userbinator
The source is around 60kLOC and 2MB, so it's somewhere between the big ones
like V8 and the smaller JS interpreters I know of:

[http://code.google.com/p/tiny-js/](http://code.google.com/p/tiny-js/)
(~2kLOC)

[http://code.google.com/p/quad-wheel/](http://code.google.com/p/quad-wheel/)

