]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp
2 * Example of how to use DUK_USE_CPP_EXCEPTIONS to support automatic
3 * variables (e.g. destructor calls) in Duktape/C functions.
5 * Compile with -DDUK_OPT_CPP_EXCEPTIONS:
7 * $ g++ -otest -DDUK_OPT_CPP_EXCEPTIONS -I<duktape_dist>/src/ \
8 * <duktape_dist>/src/duktape.c cpp_exceptions.cpp -lm
10 * or ensure duk_config.h has DUK_USE_CPP_EXCEPTIONS enabled using
11 * genconfig. When executed you should see something like:
14 * my_class instance created
15 * my_class instance destroyed <== destructor gets called
16 * --> rc=1 (SyntaxError: parse error (line 1))
19 * Duktape uses a custom exception class (duk_internal_exception) which
20 * doesn't inherit from any base class, so that catching any base classes
21 * in user code won't accidentally catch exceptions thrown by Duktape.
24 #if !defined(__cplusplus)
25 #error compile using a c++ compiler
32 #if defined(__cplusplus) && (__cplusplus >= 201103L)
33 #define NOEXCEPT noexcept
35 #define NOEXCEPT throw()
39 * Example class with a destructor
48 my_class::my_class() {
49 printf("my_class instance created\n");
52 my_class::~my_class() {
53 printf("my_class instance destroyed\n");
57 * SyntaxError caused by eval exits Duktape/C function but destructors
61 duk_ret_t
test1(duk_context
*ctx
) {
64 duk_eval_string(ctx
, "aiee=");
70 * You can use C++ exceptions inside Duktape/C functions for your own
71 * purposes but you should catch them before they propagate to Duktape.
74 duk_ret_t
test2(duk_context
*ctx
) {
79 } catch (int myvalue
) {
80 printf("Caught: %d\n", myvalue
);
87 * If you let your own C++ exceptions propagate out of a Duktape/C function
88 * it will be caught by Duktape and considered a programming error. Duktape
89 * will catch the exception and convert it to a Duktape error.
91 * This may be allowed in a later version once all the implications have been
95 duk_ret_t
test3(duk_context
*ctx
) {
98 throw 123; /* ERROR: exception propagated to Duktape */
104 * Same as above, but if the exception inherits from std::exception, it's
105 * "what()" will be included in the error message.
108 class my_exception
: public std::exception
{
109 virtual const char *what() const NOEXCEPT
{
110 return "my_exception";
114 duk_ret_t
test4(duk_context
*ctx
) {
118 throw myexc
; /* ERROR: exception propagated to Duktape */
124 * Same as above, but if the exception inherits from std::exception with
125 * a NULL what(). Duktape will describe the error as 'unknown' if so.
128 class my_exception2
: public std::exception
{
129 virtual const char *what() const NOEXCEPT
{
134 duk_ret_t
test5(duk_context
*ctx
) {
138 throw myexc
; /* ERROR: exception propagated to Duktape */
143 int main(int argc
, char *argv
[]) {
144 duk_context
*ctx
= duk_create_heap_default();
147 (void) argc
; (void) argv
; /* suppress warning */
149 printf("*** test1 - duk_pcall()\n");
150 duk_push_c_function(ctx
, test1
, 0);
151 rc
= duk_pcall(ctx
, 0);
152 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
156 printf("*** test1 - duk_safe_call()\n");
157 rc
= duk_safe_call(ctx
, test1
, 0, 1);
158 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
162 printf("*** test1 - ecmascript try-catch\n");
163 duk_push_c_function(ctx
, test1
, 0);
164 duk_put_global_string(ctx
, "test1");
165 duk_eval_string_noresult(ctx
,
169 " print(e.stack || e);\n"
173 printf("*** test2 - duk_pcall()\n");
174 duk_push_c_function(ctx
, test2
, 0);
175 rc
= duk_pcall(ctx
, 0);
176 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
180 printf("*** test2 - duk_safe_call()\n");
181 rc
= duk_safe_call(ctx
, test2
, 0, 1);
182 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
186 printf("*** test2 - ecmascript try-catch\n");
187 duk_push_c_function(ctx
, test2
, 0);
188 duk_put_global_string(ctx
, "test2");
189 duk_eval_string_noresult(ctx
,
193 " print(e.stack || e);\n"
197 printf("*** test3 - duk_pcall()\n");
198 duk_push_c_function(ctx
, test3
, 0);
199 rc
= duk_pcall(ctx
, 0);
200 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
204 printf("*** test3 - duk_safe_call()\n");
205 rc
= duk_safe_call(ctx
, test3
, 0, 1);
206 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
210 printf("*** test3 - ecmascript try-catch\n");
211 duk_push_c_function(ctx
, test3
, 0);
212 duk_put_global_string(ctx
, "test3");
213 duk_eval_string_noresult(ctx
,
217 " print(e.stack || e);\n"
221 printf("*** test4 - duk_pcall()\n");
222 duk_push_c_function(ctx
, test4
, 0);
223 rc
= duk_pcall(ctx
, 0);
224 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
228 printf("*** test4 - duk_safe_call()\n");
229 rc
= duk_safe_call(ctx
, test4
, 0, 1);
230 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
234 printf("*** test4 - ecmascript try-catch\n");
235 duk_push_c_function(ctx
, test4
, 0);
236 duk_put_global_string(ctx
, "test4");
237 duk_eval_string_noresult(ctx
,
241 " print(e.stack || e);\n"
245 printf("*** test5 - duk_pcall()\n");
246 duk_push_c_function(ctx
, test5
, 0);
247 rc
= duk_pcall(ctx
, 0);
248 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
252 printf("*** test5 - duk_safe_call()\n");
253 rc
= duk_safe_call(ctx
, test5
, 0, 1);
254 printf("--> rc=%ld (%s)\n", (long) rc
, duk_safe_to_string(ctx
, -1));
258 printf("*** test5 - ecmascript try-catch\n");
259 duk_push_c_function(ctx
, test5
, 0);
260 duk_put_global_string(ctx
, "test5");
261 duk_eval_string_noresult(ctx
,
265 " print(e.stack || e);\n"
269 printf("*** done\n");
271 duk_destroy_heap(ctx
);