File: cpp_exceptions.cpp

package info (click to toggle)
duktape 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 21,160 kB
  • sloc: ansic: 215,359; python: 5,961; javascript: 4,555; makefile: 477; cpp: 205
file content (311 lines) | stat: -rw-r--r-- 7,530 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *  Example of how to use DUK_USE_CPP_EXCEPTIONS to support automatic
 *  variables (e.g. destructor calls) in Duktape/C functions.
 *
 *  Configure and compile with -DDUK_USE_CPP_EXCEPTIONS:
 *
 *    $ python2 tools/configure.py \
 *          --source-directory src-input \
 *          --output-directory /tmp/output \
 *          --config-metadata config \
 *          -DDUK_USE_CPP_EXCEPTIONS
 *
 *    $ g++ -otest -I/tmp/output \
 *          /tmp/output/duktape.c cpp_exceptions.cpp -lm
 *
 *  When executed you should see something like:
 *
 *    $ ./test
 *    my_class instance created
 *    my_class instance destroyed      <== destructor gets called
 *    --> rc=1 (SyntaxError: parse error (line 1))
 *    [...]
 *
 *  Duktape uses a custom exception class (duk_internal_exception) which
 *  doesn't inherit from any base class, so that catching any base classes
 *  in user code won't accidentally catch exceptions thrown by Duktape.
 */

#if !defined(__cplusplus)
#error compile using a c++ compiler
#endif

#include <stdio.h>
#include <exception>
#include "duktape.h"

#if defined(__cplusplus) && (__cplusplus >= 201103L)
#define NOEXCEPT noexcept
#else
#define NOEXCEPT throw()
#endif

/*
 *  Example class with a destructor
 */

class my_class {
 public:
	my_class();
	~my_class();
};

my_class::my_class() {
	printf("my_class instance created\n");
}

my_class::~my_class() {
	printf("my_class instance destroyed\n");
}

/*
 *  SyntaxError caused by eval exits Duktape/C function but destructors
 *  are executed.
 */

duk_ret_t test1(duk_context *ctx) {
	my_class myclass;

	duk_eval_string(ctx, "aiee=");

	return 0;
}
duk_ret_t test1_safecall(duk_context *ctx, void *udata) {
	(void) udata;
	return test1(ctx);
}

/*
 *  You can use C++ exceptions inside Duktape/C functions for your own
 *  purposes but you should catch them before they propagate to Duktape.
 */

duk_ret_t test2(duk_context *ctx) {
	my_class myclass;

	try {
		throw 123;
	} catch (int myvalue) {
		printf("Caught: %d\n", myvalue);
	}

	return 0;
}
duk_ret_t test2_safecall(duk_context *ctx, void *udata) {
	(void) udata;
	return test2(ctx);
}

/*
 *  If you let your own C++ exceptions propagate out of a Duktape/C function
 *  it will be caught by Duktape and considered a programming error.  Duktape
 *  will catch the exception and convert it to a Duktape error.
 *
 *  This may be allowed in a later version once all the implications have been
 *  worked out.
 */

duk_ret_t test3(duk_context *ctx) {
	my_class myclass;

	throw 123;  /* ERROR: exception propagated to Duktape */

	return 0;
}
duk_ret_t test3_safecall(duk_context *ctx, void *udata) {
	(void) udata;
	return test3(ctx);
}

/*
 *  Same as above, but if the exception inherits from std::exception, it's
 *  "what()" will be included in the error message.
 */

class my_exception : public std::exception {
	virtual const char *what() const NOEXCEPT {
		return "my_exception";
	}
};

duk_ret_t test4(duk_context *ctx) {
	my_class myclass;
	my_exception myexc;

	throw myexc;  /* ERROR: exception propagated to Duktape */

	return 0;
}
duk_ret_t test4_safecall(duk_context *ctx, void *udata) {
	(void) udata;
	return test4(ctx);
}

/*
 *  Same as above, but if the exception inherits from std::exception with
 *  a NULL what().  Duktape will describe the error as 'unknown' if so.
 */

class my_exception2 : public std::exception {
	virtual const char *what() const NOEXCEPT {
		return NULL;
	}
};

duk_ret_t test5(duk_context *ctx) {
	my_class myclass;
	my_exception2 myexc;

	throw myexc;  /* ERROR: exception propagated to Duktape */

	return 0;
}
duk_ret_t test5_safecall(duk_context *ctx, void *udata) {
	(void) udata;
	return test5(ctx);
}

static duk_ret_t duk__print(duk_context *ctx) {
	duk_push_string(ctx, " ");
	duk_insert(ctx, 0);
	duk_join(ctx, duk_get_top(ctx) - 1);
	printf("%s\n", duk_safe_to_string(ctx, -1));
	return 0;
}

int main(int argc, char *argv[]) {
	duk_context *ctx = duk_create_heap_default();
	duk_int_t rc;

	(void) argc; (void) argv;  /* suppress warning */

	/* Minimal print() provider. */
	duk_push_c_function(ctx, duk__print, DUK_VARARGS);
	duk_put_global_string(ctx, "print");

	printf("*** test1 - duk_pcall()\n");
	duk_push_c_function(ctx, test1, 0);
	rc = duk_pcall(ctx, 0);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test1 - duk_safe_call()\n");
	rc = duk_safe_call(ctx, test1_safecall, NULL, 0, 1);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test1 - ecmascript try-catch\n");
	duk_push_c_function(ctx, test1, 0);
	duk_put_global_string(ctx, "test1");
	duk_eval_string_noresult(ctx,
		"try {\n"
		"    test1();\n"
		"} catch (e) {\n"
		"    print(e.stack || e);\n"
		"}\n");
	printf("\n");

	printf("*** test2 - duk_pcall()\n");
	duk_push_c_function(ctx, test2, 0);
	rc = duk_pcall(ctx, 0);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test2 - duk_safe_call()\n");
	rc = duk_safe_call(ctx, test2_safecall, NULL, 0, 1);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test2 - ecmascript try-catch\n");
	duk_push_c_function(ctx, test2, 0);
	duk_put_global_string(ctx, "test2");
	duk_eval_string_noresult(ctx,
		"try {\n"
		"    test2();\n"
		"} catch (e) {\n"
		"    print(e.stack || e);\n"
		"}\n");
	printf("\n");

	printf("*** test3 - duk_pcall()\n");
	duk_push_c_function(ctx, test3, 0);
	rc = duk_pcall(ctx, 0);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test3 - duk_safe_call()\n");
	rc = duk_safe_call(ctx, test3_safecall, NULL, 0, 1);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test3 - ecmascript try-catch\n");
	duk_push_c_function(ctx, test3, 0);
	duk_put_global_string(ctx, "test3");
	duk_eval_string_noresult(ctx,
		"try {\n"
		"    test3();\n"
		"} catch (e) {\n"
		"    print(e.stack || e);\n"
		"}\n");
	printf("\n");

	printf("*** test4 - duk_pcall()\n");
	duk_push_c_function(ctx, test4, 0);
	rc = duk_pcall(ctx, 0);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test4 - duk_safe_call()\n");
	rc = duk_safe_call(ctx, test4_safecall, NULL, 0, 1);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test4 - ecmascript try-catch\n");
	duk_push_c_function(ctx, test4, 0);
	duk_put_global_string(ctx, "test4");
	duk_eval_string_noresult(ctx,
		"try {\n"
		"    test4();\n"
		"} catch (e) {\n"
		"    print(e.stack || e);\n"
		"}\n");
	printf("\n");

	printf("*** test5 - duk_pcall()\n");
	duk_push_c_function(ctx, test5, 0);
	rc = duk_pcall(ctx, 0);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test5 - duk_safe_call()\n");
	rc = duk_safe_call(ctx, test5_safecall, NULL, 0, 1);
	printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1));
	duk_pop(ctx);
	printf("\n");

	printf("*** test5 - ecmascript try-catch\n");
	duk_push_c_function(ctx, test5, 0);
	duk_put_global_string(ctx, "test5");
	duk_eval_string_noresult(ctx,
		"try {\n"
		"    test5();\n"
		"} catch (e) {\n"
		"    print(e.stack || e);\n"
		"}\n");
	printf("\n");

	printf("*** done\n");

	duk_destroy_heap(ctx);

	return 0;
}