Getting started

Downloading

Download the source distributable from the Download page.

Command line tool for testing

Unpack the distributable:

$ cd /tmp
$ tar xvfJ duktape-<version>.tar.xz

Compile the command line tool using the provided Makefile:

$ cd /tmp/duktape-<version>/
$ make -f Makefile.cmdline

The Makefile assumes you have gcc installed. If you don't, you can just edit the Makefile to match your compiler (the Makefile is quite simple).

Duktape doesn't provide built-in bindings for file or console I/O to avoid portability issues (for example, some platforms don't have I/O APIs at all). The command line utility provides print() and alert() bindings using extras/print-alert to make it easier to play with. There are useful "extras" in the distributable providing useful (optional) bindings such as: Throughout the guide examples will assume a print() binding for illustration.
The command line tool avoids platform dependencies by default. You can add line editing support via linenoise by editing the Makefile:

You can now run ECMAScript code interactively:

$ ./duk
((o) Duktape 2.6.0 (v2.6.0)
duk> print('Hello world!')
Hello world!
= undefined

You can also run ECMAScript code from a file which is useful for playing with features and algorithms. As an example, create fib.js:



Test the script from the command line:

$ ./duk fib.js
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Integrating Duktape into your program

The command line tool is simply an example of a program which embeds Duktape. Embedding Duktape into your program is very simple:

The Duktape distributable (duktape-N.N.N.tar.xz) src/ directory contains preconfigured header and source files for the Duktape default configuration which can usually be used as is. If needed, the configuration tool allows you to customize Duktape options, such as optimizing Duktape for low memory targets and enable/disable features. See Compiling and Configuring Duktape for build for more details and examples.

The distributable contains a very simple example program, hello.c, which illustrates this process. Compile the test program with the preconfigured Duktape header and source files e.g. as follows:

$ cd /tmp/duktape-<version>/
$ gcc -std=c99 -o hello -Isrc src/duktape.c examples/hello/hello.c -lm

To customize Duktape configuration use configure.py:

$ cd /tmp/duktape-<version>/
# Here we disable ECMAScript 6 Proxy object support
$ python2 tools/configure.py --output-directory duktape-src -UDUK_USE_ES6_PROXY
$ gcc -std=c99 -o hello -Iduktape-src duktape-src/duktape.c examples/hello/hello.c -lm

The test program creates a Duktape context and uses it to run some ECMAScript code:

$ ./hello
Hello world!
2+3=5

Because Duktape is an embeddable engine, you don't need to change the basic control flow of your program. The basic approach is:

More broadly, there are several approaches to how you can use Duktape with native code; for example:

See the following Wiki articles for detailed examples: