You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
2.4 KiB

<div id="front-blurp">
<div class="main-title"><strong>Duktape</strong></div>
<p>Duktape is an <b>embeddable Javascript</b> engine,
with a focus on <b>portability</b> and compact <b>footprint</b>.</p>
<p>Duktape is easy to integrate into a C/C++ project: add <code>duktape.c</code>
and <code>duktape.h</code> to your build, and use the Duktape API to call Ecmascript
functions from C code and vice versa.</p>
<p>Main features:</p>
<ul>
<li>Embeddable, portable, compact</li>
<li>Ecmascript E5/E5.1 compliant</li>
<li>Built-in regular expression engine</li>
<li>Built-in Unicode support</li>
<li>Minimal platform dependencies</li>
<li>Combined reference counting and mark-and-sweep garbage collection
with finalization</li>
<li>Liberal license</li>
</ul>
<p>Current status:</p>
<ul>
<li>Alpha</li>
</ul>
</div> <!-- front-blurp -->
<div id="front-steps">
(See <a href="guide.html#gettingstarted">Getting started</a> for a more
detailed introduction.)
<h1><span class="step">1</span> Add to build</h1>
<p>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:</p>
<pre>
$ gcc -o test test.c duktape.c -lm
$ ./test
Hello world!
</pre>
<h1><span class="step">2</span> Initialize a context</h1>
<p>Initialize and use Duktape somewhere in your program:</p>
<pre class="c-code">
/* test.c */
#include &quot;duktape.h&quot;
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;
}
</pre>
<h1><span class="step">3</span> Add C function bindings</h1>
<p>To call a C function from Ecmascript code, first declare your
C function:</p>
<pre class="c-code">
int adder(duk_context *ctx) {
int i;
int n = duk_get_top(ctx); /* #args */
double res = 0.0;
for (i = 0; i &lt; n; i++) {
res += duk_to_number(ctx, i);
}
duk_push_number(ctx, res);
return 1; /* one return value */
}
</pre>
<p>Register your function e.g. into the global object:</p>
<pre class="c-code">
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 */
</pre>
<p>You can then call your function from Ecmascript code:</p>
<pre class="c-code">
duk_eval_string(ctx, "print('2+3=' + adder(2, 3));");
duk_pop(ctx); /* pop eval result */
</pre>
</div> <!-- front-steps -->