Skip to content

Commit 63d01ff

Browse files
committed
Guide gettingstarted.html cleanups
1 parent 85d3e8b commit 63d01ff

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

examples/guide/primecheck.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string.h>
55
#include "duktape.h"
66

7-
static int native_prime_check(duk_context *ctx) {
7+
static duk_ret_t native_prime_check(duk_context *ctx) {
88
int val = duk_require_int(ctx, 0);
99
int lim = duk_require_int(ctx, 1);
1010
int i;
@@ -30,8 +30,7 @@ int main(int argc, const char *argv[]) {
3030
duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/);
3131
duk_put_prop_string(ctx, -2, "primeCheckNative");
3232

33-
duk_eval_file(ctx, "prime.js");
34-
duk_pop(ctx); /* pop eval result */
33+
duk_eval_file_noresult(ctx, "prime.js");
3534

3635
duk_get_prop_string(ctx, -1, "primeTest");
3736
duk_call(ctx, 0);

examples/guide/processlines.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ int main(int argc, const char *argv[]) {
1313
ctx = duk_create_heap_default();
1414
if (!ctx) { exit(1); }
1515

16-
duk_eval_file(ctx, "process.js");
17-
duk_pop(ctx); /* pop eval result */
16+
duk_eval_file_noresult(ctx, "process.js");
1817

1918
memset(line, 0, sizeof(line));
2019
idx = 0;
@@ -29,7 +28,7 @@ int main(int argc, const char *argv[]) {
2928
duk_get_prop_string(ctx, -1 /*index*/, "processLine");
3029
duk_push_string(ctx, line);
3130
duk_call(ctx, 1 /*nargs*/);
32-
printf("%s\n", duk_to_string(ctx, -1));
31+
printf("%s\n", duk_safe_to_string(ctx, -1));
3332
duk_pop(ctx);
3433

3534
idx = 0;

website/guide/gettingstarted.html

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3636
duk&gt; print('Hello world!')
3737
Hello world!
3838
= undefined
@@ -46,7 +46,6 @@ <h2>Command line tool</h2>
4646
<pre>
4747
$ ./duk fib.js
4848
0 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));
178174
duk_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
182178
function call result here) to a string, returning a <code>const char *</code> pointing
183179
to the result. This return value is a read-only, NUL terminated UTF-8 value which
184180
C 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 &amp; Max*." | ./processlines
209205
I like &lt;b&gt;Sam &amp;<!-- avoiding double decode is tricky -->&#35;38; Max&lt;/b&gt;.
@@ -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
231227
Duktape/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>
305301
If they are numbers, their value is converted to an integer and assigned to
306302
the <code>val</code> and <code>lim</code> locals. The index 0 refers to the first
307303
function 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">
312312
duk_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
363363
Have native helper: true
@@ -388,4 +388,3 @@ <h2>Calling C code from Ecmascript (Duktape/C bindings)</h2>
388388
user 0m23.573s
389389
sys 0m0.000s
390390
</pre>
391-

0 commit comments

Comments
 (0)