
Duktape is an embeddable JavaScript engine, with a focus on portability - tosh
https://duktape.org/
======
kowbell
I was taking a Game Engines class at my uni last semester and we had to add
scripting to our C++ engines using Duktape (or it's C++ counterpart, Dukglue.)
It was surprisingly easy to use Duktape once you figured out the initial
boilerplate, and it was especially exciting and fun to be able to write a demo
game using the engine in two languages (especially with one which didn't
require recompilation!)

------
jupp0r
Just keep in mind that performance is abysmal (order of magnitude of 1% vs
what you are used to in a browser for the same amount of cpu time spent),
which might be ok for your use case, just keep in mind that many libraries
assume a somewhat performant runtime. Another down point is that you are stuck
with ES5.

~~~
CharlesW
For anyone who needs a performant "JavaScript for embedded" runtime that also
fully supports ECMAScript 2018, Moddable's XS is a great choice.[1]

Moddable is the only embedded engine provider in the Ecma TC39 JavaScript
language committee, so they tend to be really aggressive in supporting new
JavaScript features.

[1]
[https://www.moddable.com/faq.php#comparison](https://www.moddable.com/faq.php#comparison)

~~~
amelius
How difficult would it be to port Firefox's javascript engine to embedded
platforms?

~~~
hajile
I've never tried, but Mozilla's example doesn't look that hard.

[https://github.com/mozilla-spidermonkey/spidermonkey-
embeddi...](https://github.com/mozilla-spidermonkey/spidermonkey-embedding-
examples)

The big issue (like with v8) is binary size. Double-digit megabytes before you
even start running code is unacceptable in a lot of environments.

~~~
X-Istence
It's a giant pain in the behind, if you can even get it to compile correctly.

------
SlowRobotAhead
I tried it on embedded. Wasn’t happening for me. Eventually moved to mJS a
similar JS engine for C. Eventually dumped that and moved to Lua.

Part of the issue is I needed a scripting engine, but I didn’t NEED JS. It was
also a pain that the code/script was in ASCII which had to go to base64 and
none of this was as good as compiled for efficiency.

I wasn’t going to let users run their own code, so that’s probably a big
reason people use this.

~~~
tyingq
TCL would have been another choice. Still used by quite a few commercial
entities as an embedded scripting language.

~~~
SlowRobotAhead
I need FFI (foreign function interface) to fall C functions from the scripting
language. My glance into TCL did not turn up a native FFI.

~~~
blacksqr
Ffidl ([https://prs-de.github.io/ffidl/](https://prs-de.github.io/ffidl/)) is
the standard library extension for Tcl to make use of FFI.

Tcl's philosophy is to keep the core lean and extend functionality via
loadable libraries.

~~~
SlowRobotAhead
I'll take a look, thanks.

EDIT: Not too bad actually. I'll take a look and compare to Lua. I'm not
::crazy::about:TCL's syntax, but it's not that bad.

------
snek
moddablexs and quickjs are probably better at this point, with current spec
compatibility and whatnot. still a lot of respect for duktape though, it is
not that bad to embed.

~~~
juancampa
QuickJS has a relevant comparison/benchmark:
[https://bellard.org/quickjs/bench.html](https://bellard.org/quickjs/bench.html)

~~~
hajile
600kb or 1.2mb are way larger than 330kb and makes them much less suitable for
smaller embedded applications which Duktape seems to target (it can get way
smaller than 330kb).

I'm surprised with v8 though. I assumed they'd have a separate binary
available so you could run jitless without having to carry all the binary
bloat.

~~~
zamadatix
Another thing to consider (and probably part of the size difference) is
QuickJS carries with it about 5 years newer JS standards support than Duktape.

~~~
hajile
I suspect these two things are related. ES2015 more than doubled the size of
the spec and each spec since then has continued adding loads of things that
take precious kb to implement.

That said, it seems like there are serious code savings to be had with some
things like destructuring, template strings, and arrow functions. Generators
are probably complex to add, but also don't transpile well (that is, debugging
the resulting code is a horrible experience).

------
peteretep
I have used this to steal JS libraries I wanted into other high-level
languages that had Duktape wrappings

------
ausjke
the legendary option has always been Lua: small, fast, light-weight, easy to
embed and get the job done.

------
petters
Can also recommend QuickJS which has 2020 features. Duktape is ES5.

------
swyx
i feel compelled to share Figma's experience trying out Duktape to build a
plugin system: [https://www.figma.com/blog/how-we-built-the-figma-plugin-
sys...](https://www.figma.com/blog/how-we-built-the-figma-plugin-system/) they
picked somehting else in the end but its a great use case

