@@ -32,7 +32,7 @@ <h2>Command line tool</h2>
3232< p > You can now run Ecmascript code interactively:</ p >
3333< pre >
3434$ ./duk
35- ((o) Duktape 0.11 .0
35+ ((o) Duktape 0.12 .0
3636duk> print('Hello world!')
3737Hello world!
3838= undefined
@@ -46,7 +46,6 @@ <h2>Command line tool</h2>
4646< pre >
4747$ ./duk fib.js
48480 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
49- Cleaning up...
5049</ pre >
5150
5251< h2 > Integrating Duktape into your program</ h2 >
@@ -124,14 +123,11 @@ <h2>Integrating Duktape into your program</h2>
124123
125124< li >
126125< pre class ="c-code ">
127- duk_eval_file(ctx, "process.js");
128- duk_pop(ctx); /* pop eval result */
126+ duk_eval_file_noresult(ctx, "process.js");
129127</ pre >
130- < p > The first call reads in < code > process.js</ code > and evaluates its contents.
131- The script registers the < code > processLine()</ code > function into the Ecmascript
132- global object. The result of the evaluation is pushed on top of the value
133- stack. Here we don't need the evaluation result, so we pop the value off
134- the stack.</ p >
128+ < p > The eval call reads in < code > process.js</ code > , then compiles and executes the
129+ script. The eval result is ignored. The script registers the < code > processLine()</ code >
130+ function into the Ecmascript global object.</ p >
135131</ li >
136132
137133< li >
@@ -174,11 +170,11 @@ <h2>Integrating Duktape into your program</h2>
174170
175171< li >
176172< pre class ="c-code ">
177- printf("%s\n", duk_to_string (ctx, -1));
173+ printf("%s\n", duk_safe_to_string (ctx, -1));
178174duk_pop(ctx);
179175</ pre >
180- < p > The < code > duk_to_string ()</ code > call requests Duktape to convert the value stack
181- element at index -1 (the topmost value on the stack, which is the processLine
176+ < p > The < code > duk_safe_to_string ()</ code > call requests Duktape to convert the value
177+ stack element at index -1 (the topmost value on the stack, which is the processLine
182178function call result here) to a string, returning a < code > const char *</ code > pointing
183179to the result. This return value is a read-only, NUL terminated UTF-8 value which
184180C code can use directly as long as the value resides on the value stack. Here we
@@ -203,7 +199,7 @@ <h2>Integrating Duktape into your program</h2>
203199$ gcc -std=c99 -o processlines -Isrc/ src/duktape.c processlines.c -lm
204200</ pre >
205201
206- < p > Test run:</ p >
202+ < p > Test run (ensure that < code > process.js </ code > is in the current directory) :</ p >
207203< pre >
208204$ echo "I like *Sam & Max*." | ./processlines
209205I like <b>Sam &<!-- avoiding double decode is tricky --> #38; Max</b>.
@@ -230,7 +226,7 @@ <h2>Calling C code from Ecmascript (Duktape/C bindings)</h2>
230226< i > value stack</ i > , manipulated with the Duktape API. We'll go deeper into
231227Duktape/C binding and the Duktape API later on. Example:</ p >
232228< pre class ="c-code ">
233- int my_native_func(duk_context *ctx) {
229+ duk_ret_t my_native_func(duk_context *ctx) {
234230 double arg = duk_require_number(ctx, 0 /*index*/);
235231 duk_push_number(ctx, arg * arg);
236232 return 1;
@@ -305,8 +301,12 @@ <h2>Calling C code from Ecmascript (Duktape/C bindings)</h2>
305301If they are numbers, their value is converted to an integer and assigned to
306302the < code > val</ code > and < code > lim</ code > locals. The index 0 refers to the first
307303function argument and index 1 to the second.</ p >
304+ < p >
305+ Technically < code > duk_require_int()</ code > returns a < code > duk_int_t</ code > ; this
306+ indirect type is always mapped to an < code > int</ code > except on obscure platforms
307+ where an < code > int</ code > is only 16 bits wide. In ordinary application code you
308+ don't need to worry about this, see < a href ="#ctypes "> C types</ a > for more discussion.</ p >
308309</ li >
309-
310310< li >
311311< pre class ="c-code ">
312312duk_push_false(ctx);
@@ -357,7 +357,7 @@ <h2>Calling C code from Ecmascript (Duktape/C bindings)</h2>
357357$ gcc -std=c99 -o primecheck -Isrc/ src/duktape.c primecheck.c -lm
358358</ pre >
359359
360- < p > Test run:</ p >
360+ < p > Test run (ensure that < code > prime.js </ code > is in the current directory) :</ p >
361361< pre >
362362$ time ./primecheck
363363Have native helper: true
@@ -388,4 +388,3 @@ <h2>Calling C code from Ecmascript (Duktape/C bindings)</h2>
388388user 0m23.573s
389389sys 0m0.000s
390390</ pre >
391-
0 commit comments