Download the source distributable from the Download page.
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).
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.
-DDUK_CMDLINE_FANCY
-Ipath/to/linenoise
for the linenoise.h
headerpath/to/linenoise.c
to the source listYou can now run ECMAScript code interactively:
$ ./duk ((o) Duktape 2.2.0 (v2.2.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
The command line tool is simply an example of a program which embeds Duktape. Embedding Duktape into your program is very simple:
duktape-N.N.N/tools/configure.py
to configure Duktape
for build. The result is a directory containing duktape.c
,
duktape.h
, and duk_config.h
.duktape.c
, duktape.h
, and duk_config.h
to your build, and call the Duktape API from elsewhere in your program.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: