Skip to content

Commit 649c780

Browse files
committed
Example fixes for duk_safe_call() change
1 parent e00721c commit 649c780

7 files changed

Lines changed: 63 additions & 27 deletions

File tree

examples/cmdline/duk_cmdline.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ static void set_sigint_handler(void) {
141141
}
142142
#endif /* DUK_CMDLINE_SIGNAL */
143143

144-
static int get_stack_raw(duk_context *ctx) {
144+
static int get_stack_raw(duk_context *ctx, void *udata) {
145+
(void) udata;
146+
145147
if (!duk_is_object(ctx, -1)) {
146148
return 1;
147149
}
@@ -164,17 +166,19 @@ static void print_pop_error(duk_context *ctx, FILE *f) {
164166
* Note that getting the stack trace may throw an error
165167
* so this also needs to be safe call wrapped.
166168
*/
167-
(void) duk_safe_call(ctx, get_stack_raw, 1 /*nargs*/, 1 /*nrets*/);
169+
(void) duk_safe_call(ctx, get_stack_raw, NULL /*udata*/, 1 /*nargs*/, 1 /*nrets*/);
168170
fprintf(f, "%s\n", duk_safe_to_string(ctx, -1));
169171
fflush(f);
170172
duk_pop(ctx);
171173
}
172174

173-
static int wrapped_compile_execute(duk_context *ctx) {
175+
static int wrapped_compile_execute(duk_context *ctx, void *udata) {
174176
const char *src_data;
175177
duk_size_t src_len;
176178
int comp_flags;
177179

180+
(void) udata;
181+
178182
/* XXX: Here it'd be nice to get some stats for the compilation result
179183
* when a suitable command line is given (e.g. code size, constant
180184
* count, function count. These are available internally but not through
@@ -315,7 +319,7 @@ static int completion_digit(unsigned char c) {
315319
return (c >= (unsigned char) '0' && c <= (unsigned char) '9');
316320
}
317321

318-
static duk_ret_t linenoise_completion_lookup(duk_context *ctx) {
322+
static duk_ret_t linenoise_completion_lookup(duk_context *ctx, void *udata) {
319323
duk_size_t len;
320324
const char *orig;
321325
const unsigned char *p;
@@ -326,6 +330,8 @@ static duk_ret_t linenoise_completion_lookup(duk_context *ctx) {
326330
linenoiseCompletions *lc;
327331
duk_idx_t idx_obj;
328332

333+
(void) udata;
334+
329335
orig = duk_require_string(ctx, -3);
330336
p_curr = (const unsigned char *) duk_require_lstring(ctx, -2, &len);
331337
p_end = p_curr + len;
@@ -478,7 +484,7 @@ static void linenoise_completion(const char *buf, linenoiseCompletions *lc) {
478484
duk_push_lstring(ctx, (const char *) p, (duk_size_t) (p_end - p));
479485
duk_push_pointer(ctx, (void *) lc);
480486

481-
rc = duk_safe_call(ctx, linenoise_completion_lookup, 3 /*nargs*/, 1 /*nrets*/);
487+
rc = duk_safe_call(ctx, linenoise_completion_lookup, NULL /*udata*/, 3 /*nargs*/, 1 /*nrets*/);
482488
if (rc != DUK_EXEC_SUCCESS) {
483489
fprintf(stderr, "Completion handling failure: %s\n", duk_safe_to_string(ctx, -1));
484490
}
@@ -586,7 +592,7 @@ static int handle_fh(duk_context *ctx, FILE *f, const char *filename, const char
586592

587593
interactive_mode = 0; /* global */
588594

589-
rc = duk_safe_call(ctx, wrapped_compile_execute, 4 /*nargs*/, 1 /*nret*/);
595+
rc = duk_safe_call(ctx, wrapped_compile_execute, NULL /*udata*/, 4 /*nargs*/, 1 /*nret*/);
590596

591597
#if defined(DUK_CMDLINE_AJSHEAP)
592598
ajsheap_clear_exec_timeout();
@@ -664,7 +670,7 @@ static int handle_eval(duk_context *ctx, char *code) {
664670

665671
interactive_mode = 0; /* global */
666672

667-
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
673+
rc = duk_safe_call(ctx, wrapped_compile_execute, NULL /*udata*/, 3 /*nargs*/, 1 /*nret*/);
668674

669675
#if defined(DUK_CMDLINE_AJSHEAP)
670676
ajsheap_clear_exec_timeout();
@@ -724,7 +730,7 @@ static int handle_interactive(duk_context *ctx) {
724730

725731
interactive_mode = 1; /* global */
726732

727-
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
733+
rc = duk_safe_call(ctx, wrapped_compile_execute, NULL /*udata*/, 3 /*nargs*/, 1 /*nret*/);
728734

729735
#if defined(DUK_CMDLINE_AJSHEAP)
730736
ajsheap_clear_exec_timeout();
@@ -799,7 +805,7 @@ static int handle_interactive(duk_context *ctx) {
799805

800806
interactive_mode = 1; /* global */
801807

802-
rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/);
808+
rc = duk_safe_call(ctx, wrapped_compile_execute, NULL /*udata*/, 3 /*nargs*/, 1 /*nret*/);
803809

804810
#if defined(DUK_CMDLINE_AJSHEAP)
805811
ajsheap_clear_exec_timeout();

examples/cpp-exceptions/cpp_exceptions.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ duk_ret_t test1(duk_context *ctx) {
6565

6666
return 0;
6767
}
68+
duk_ret_t test1_safecall(duk_context *ctx, void *udata) {
69+
(void) udata;
70+
return test1(ctx);
71+
}
6872

6973
/*
7074
* You can use C++ exceptions inside Duktape/C functions for your own
@@ -82,6 +86,10 @@ duk_ret_t test2(duk_context *ctx) {
8286

8387
return 0;
8488
}
89+
duk_ret_t test2_safecall(duk_context *ctx, void *udata) {
90+
(void) udata;
91+
test2(ctx);
92+
}
8593

8694
/*
8795
* If you let your own C++ exceptions propagate out of a Duktape/C function
@@ -99,6 +107,10 @@ duk_ret_t test3(duk_context *ctx) {
99107

100108
return 0;
101109
}
110+
duk_ret_t test3_safecall(duk_context *ctx, void *udata) {
111+
(void) udata;
112+
return test3(ctx);
113+
}
102114

103115
/*
104116
* Same as above, but if the exception inherits from std::exception, it's
@@ -119,6 +131,10 @@ duk_ret_t test4(duk_context *ctx) {
119131

120132
return 0;
121133
}
134+
duk_ret_t test4_safecall(duk_context *ctx, void *udata) {
135+
(void) udata;
136+
return test4(ctx);
137+
}
122138

123139
/*
124140
* Same as above, but if the exception inherits from std::exception with
@@ -139,6 +155,10 @@ duk_ret_t test5(duk_context *ctx) {
139155

140156
return 0;
141157
}
158+
duk_ret_t test5_safecall(duk_context *ctx, void *udata) {
159+
(void) udata;
160+
return test5(ctx);
161+
}
142162

143163
int main(int argc, char *argv[]) {
144164
duk_context *ctx = duk_create_heap_default();
@@ -154,7 +174,7 @@ int main(int argc, char *argv[]) {
154174
printf("\n");
155175

156176
printf("*** test1 - duk_safe_call()\n");
157-
rc = duk_safe_call(ctx, test1, 0, 1);
177+
rc = duk_safe_call(ctx, test1_safecall, NULL, 0, 1);
158178
printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
159179
duk_pop(ctx);
160180
printf("\n");
@@ -178,7 +198,7 @@ int main(int argc, char *argv[]) {
178198
printf("\n");
179199

180200
printf("*** test2 - duk_safe_call()\n");
181-
rc = duk_safe_call(ctx, test2, 0, 1);
201+
rc = duk_safe_call(ctx, test2_safecall, NULL, 0, 1);
182202
printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
183203
duk_pop(ctx);
184204
printf("\n");
@@ -202,7 +222,7 @@ int main(int argc, char *argv[]) {
202222
printf("\n");
203223

204224
printf("*** test3 - duk_safe_call()\n");
205-
rc = duk_safe_call(ctx, test3, 0, 1);
225+
rc = duk_safe_call(ctx, test3_safecall, NULL, 0, 1);
206226
printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
207227
duk_pop(ctx);
208228
printf("\n");
@@ -226,7 +246,7 @@ int main(int argc, char *argv[]) {
226246
printf("\n");
227247

228248
printf("*** test4 - duk_safe_call()\n");
229-
rc = duk_safe_call(ctx, test4, 0, 1);
249+
rc = duk_safe_call(ctx, test4_safecall, NULL, 0, 1);
230250
printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
231251
duk_pop(ctx);
232252
printf("\n");
@@ -250,7 +270,7 @@ int main(int argc, char *argv[]) {
250270
printf("\n");
251271

252272
printf("*** test5 - duk_safe_call()\n");
253-
rc = duk_safe_call(ctx, test5, 0, 1);
273+
rc = duk_safe_call(ctx, test5_safecall, NULL, 0, 1);
254274
printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
255275
duk_pop(ctx);
256276
printf("\n");

examples/eval/eval.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
#include "duktape.h"
77
#include <stdio.h>
88

9-
static int eval_raw(duk_context *ctx) {
9+
static duk_ret_t eval_raw(duk_context *ctx, void *udata) {
10+
(void) udata;
1011
duk_eval(ctx);
1112
return 1;
1213
}
1314

14-
static int tostring_raw(duk_context *ctx) {
15+
static duk_ret_t tostring_raw(duk_context *ctx, void *udata) {
16+
(void) udata;
1517
duk_to_string(ctx, -1);
1618
return 1;
1719
}
@@ -35,8 +37,8 @@ int main(int argc, char *argv[]) {
3537
for (i = 1; i < argc; i++) {
3638
printf("=== eval: '%s' ===\n", argv[i]);
3739
duk_push_string(ctx, argv[i]);
38-
duk_safe_call(ctx, eval_raw, 1 /*nargs*/, 1 /*nrets*/);
39-
duk_safe_call(ctx, tostring_raw, 1 /*nargs*/, 1 /*nrets*/);
40+
duk_safe_call(ctx, eval_raw, NULL, 1 /*nargs*/, 1 /*nrets*/);
41+
duk_safe_call(ctx, tostring_raw, NULL, 1 /*nargs*/, 1 /*nrets*/);
4042
res = duk_get_string(ctx, -1);
4143
printf("%s\n", res ? res : "null");
4244
duk_pop(ctx);

examples/eventloop/c_eventloop.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static void compact_poll_list(void) {
254254
poll_count = j;
255255
}
256256

257-
int eventloop_run(duk_context *ctx) {
257+
duk_ret_t eventloop_run(duk_context *ctx, void *udata) {
258258
ev_timer *t;
259259
double now;
260260
double diff;
@@ -264,6 +264,8 @@ int eventloop_run(duk_context *ctx) {
264264
int idx_eventloop;
265265
int idx_fd_handler;
266266

267+
(void) udata;
268+
267269
/* The Ecmascript poll handler is passed through EventLoop.fdPollHandler
268270
* which c_eventloop.js sets before we come here.
269271
*/

examples/eventloop/main.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern void ncurses_register(duk_context *ctx);
1919
extern void socket_register(duk_context *ctx);
2020
extern void fileio_register(duk_context *ctx);
2121
extern void eventloop_register(duk_context *ctx);
22-
extern int eventloop_run(duk_context *ctx); /* Duktape/C function, safe called */
22+
extern duk_ret_t eventloop_run(duk_context *ctx, void *udata);
2323

2424
static int c_evloop = 0;
2525

@@ -54,10 +54,12 @@ static void print_error(duk_context *ctx, FILE *f) {
5454
duk_pop(ctx);
5555
}
5656

57-
int wrapped_compile_execute(duk_context *ctx) {
57+
duk_ret_t wrapped_compile_execute(duk_context *ctx, void *udata) {
5858
int comp_flags = 0;
5959
int rc;
6060

61+
(void) udata;
62+
6163
/* Compile input and place it into global _USERCODE */
6264
duk_compile(ctx, comp_flags);
6365
duk_push_global_object(ctx);
@@ -82,7 +84,7 @@ int wrapped_compile_execute(duk_context *ctx) {
8284
if (c_evloop) {
8385
fprintf(stderr, "calling eventloop_run()\n");
8486
fflush(stderr);
85-
rc = duk_safe_call(ctx, eventloop_run, 0 /*nargs*/, 1 /*nrets*/);
87+
rc = duk_safe_call(ctx, eventloop_run, NULL, 0 /*nargs*/, 1 /*nrets*/);
8688
if (rc != 0) {
8789
fprintf(stderr, "eventloop_run() failed: %s\n", duk_to_string(ctx, -1));
8890
fflush(stderr);
@@ -125,7 +127,7 @@ int handle_fh(duk_context *ctx, FILE *f, const char *filename) {
125127
free(buf);
126128
buf = NULL;
127129

128-
rc = duk_safe_call(ctx, wrapped_compile_execute, 2 /*nargs*/, 1 /*nret*/);
130+
rc = duk_safe_call(ctx, wrapped_compile_execute, NULL, 2 /*nargs*/, 1 /*nret*/);
129131
if (rc != DUK_EXEC_SUCCESS) {
130132
print_error(ctx, stderr);
131133
goto error;

examples/jxpretty/jxpretty.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
#include <stdlib.h>
77
#include "duktape.h"
88

9-
static duk_ret_t do_jxpretty(duk_context *ctx) {
9+
static duk_ret_t do_jxpretty(duk_context *ctx, void *udata) {
1010
FILE *f = stdin;
1111
char buf[4096];
1212
size_t ret;
1313

14+
(void) udata;
15+
1416
for (;;) {
1517
if (ferror(f)) {
1618
duk_error(ctx, DUK_ERR_ERROR, "ferror() on stdin");
@@ -51,7 +53,7 @@ int main(int argc, char *argv[]) {
5153

5254
ctx = duk_create_heap_default();
5355

54-
rc = duk_safe_call(ctx, do_jxpretty, 0 /*nargs*/, 1 /*nrets*/);
56+
rc = duk_safe_call(ctx, do_jxpretty, NULL, 0 /*nargs*/, 1 /*nrets*/);
5557
if (rc) {
5658
fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1));
5759
fflush(stderr);

examples/sandbox/sandbox.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,14 @@ static void sandbox_free(void *udata, void *ptr) {
144144
* Sandbox setup and test
145145
*/
146146

147-
static duk_ret_t do_sandbox_test(duk_context *ctx) {
147+
static duk_ret_t do_sandbox_test(duk_context *ctx, void *udata) {
148148
FILE *f;
149149
char buf[4096];
150150
size_t ret;
151151
const char *globobj;
152152

153+
(void) udata;
154+
153155
/*
154156
* Setup sandbox
155157
*/
@@ -236,7 +238,7 @@ int main(int argc, char *argv[]) {
236238
sandbox_fatal);
237239

238240
duk_push_string(ctx, argv[1]);
239-
rc = duk_safe_call(ctx, do_sandbox_test, 1 /*nargs*/, 1 /*nrets*/);
241+
rc = duk_safe_call(ctx, do_sandbox_test, NULL, 1 /*nargs*/, 1 /*nrets*/);
240242
if (rc) {
241243
fprintf(stderr, "ERROR: %s\n", duk_safe_to_string(ctx, -1));
242244
fflush(stderr);

0 commit comments

Comments
 (0)