Duktape
Duktape is an embeddable Javascript engine,
with a focus on portability and compact footprint.
Duktape is easy to integrate into a C/C++ project: add duktape.c
and duktape.h
to your build, and use the Duktape API to call Ecmascript
functions from C code and vice versa.
Main features
- Embeddable, portable, compact:
- 200kB code
- 46kB startup RAM (x86, default options)
- 22kB startup RAM (x86, lowmem options)
- 45kLoC source (excluding comments etc)
- Can run on platforms with 256kB flash and 96kB system RAM
- Ecmascript E5/E5.1
compliant, some features borrowed from
E6 draft
- Built-in debugger
- Built-in regular expression engine
- Built-in Unicode support
- Minimal platform dependencies
- Combined reference counting and mark-and-sweep garbage collection
with finalization
- Custom features like coroutines, built-in logging framework, and
built-in CommonJS-based module loading framework
- Property virtualization using a subset of Ecmascript E6 Proxy object
- Liberal license (MIT)
Current status
Support
- User community Q&A: Stack Overflow duktape tag
- Bugs and feature requests: GitHub issues
- General discussion: IRC
#duktape
on chat.freenode.net
(webchat)
Some projects using Duktape (alphabetical)
- AllJoyn.js, using Duktape for embedded applications, git repo
- Atomic Game Engine, using Duktape for scripting
- Contraption Maker, using Duktape for
mods
- DukLuv.io, libuv bindings for Duktape
- duktape-node, running scripts in a separate isolated context from Node.js
- duktape.rb, Ruby binding for Duktape
- go-duktape, Go binding for Duktape
- libpac, using Duktape to parse proxy PAC files
- Megatools, using Duktape for main program logic
- radare, using Duktape for reverse engineering scripts
- RareWire application studio
- Showtime media center, using Duktape for plugins
1 Add to build
(See
Getting started for a more
detailed introduction.)
Add Duktape C source and header to your build. Any build system can
be used. The distributable contains an example Makefile for reference.
In the simplest case:
$ gcc -std=c99 -o test test.c duktape.c -lm
$ ./test
Hello world!
2 Initialize a context
Initialize and use Duktape somewhere in your program:
/* test.c */
#include "duktape.h"
int main(int argc, char *argv[]) {
duk_context *ctx = duk_create_heap_default();
duk_eval_string(ctx, "print('Hello world!');");
duk_destroy_heap(ctx);
return 0;
}
3 Add C function bindings
To call a C function from Ecmascript code, first declare your
C function:
int adder(duk_context *ctx) {
int i;
int n = duk_get_top(ctx); /* #args */
double res = 0.0;
for (i = 0; i < n; i++) {
res += duk_to_number(ctx, i);
}
duk_push_number(ctx, res);
return 1; /* one return value */
}
Register your function e.g. into the global object:
duk_push_global_object(ctx);
duk_push_c_function(ctx, adder, DUK_VARARGS);
duk_put_prop_string(ctx, -2 /*idx:global*/, "adder");
duk_pop(ctx); /* pop global */
You can then call your function from Ecmascript code:
duk_eval_string(ctx, "print('2+3=' + adder(2, 3));");
duk_pop(ctx); /* pop eval result */