Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
cpp_exceptions.cpp File Reference
#include <stdio.h>
#include <exception>
#include "duktape.h"

Go to the source code of this file.

Data Structures

class  my_class
 
class  my_exception
 
class  my_exception2
 

Macros

#define NOEXCEPT   throw()
 

Functions

duk_ret_t test1 (duk_context *ctx)
 
duk_ret_t test2 (duk_context *ctx)
 
duk_ret_t test3 (duk_context *ctx)
 
duk_ret_t test4 (duk_context *ctx)
 
duk_ret_t test5 (duk_context *ctx)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ NOEXCEPT

#define NOEXCEPT   throw()

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 143 of file duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp.

143 {
145 duk_int_t rc;
146
147 (void) argc; (void) argv; /* suppress warning */
148
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));
153 duk_pop(ctx);
154 printf("\n");
155
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));
159 duk_pop(ctx);
160 printf("\n");
161
162 printf("*** test1 - ecmascript try-catch\n");
163 duk_push_c_function(ctx, test1, 0);
164 duk_put_global_string(ctx, "test1");
166 "try {\n"
167 " test1();\n"
168 "} catch (e) {\n"
169 " print(e.stack || e);\n"
170 "}\n");
171 printf("\n");
172
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));
177 duk_pop(ctx);
178 printf("\n");
179
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));
183 duk_pop(ctx);
184 printf("\n");
185
186 printf("*** test2 - ecmascript try-catch\n");
187 duk_push_c_function(ctx, test2, 0);
188 duk_put_global_string(ctx, "test2");
190 "try {\n"
191 " test2();\n"
192 "} catch (e) {\n"
193 " print(e.stack || e);\n"
194 "}\n");
195 printf("\n");
196
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));
201 duk_pop(ctx);
202 printf("\n");
203
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));
207 duk_pop(ctx);
208 printf("\n");
209
210 printf("*** test3 - ecmascript try-catch\n");
211 duk_push_c_function(ctx, test3, 0);
212 duk_put_global_string(ctx, "test3");
214 "try {\n"
215 " test3();\n"
216 "} catch (e) {\n"
217 " print(e.stack || e);\n"
218 "}\n");
219 printf("\n");
220
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));
225 duk_pop(ctx);
226 printf("\n");
227
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));
231 duk_pop(ctx);
232 printf("\n");
233
234 printf("*** test4 - ecmascript try-catch\n");
235 duk_push_c_function(ctx, test4, 0);
236 duk_put_global_string(ctx, "test4");
238 "try {\n"
239 " test4();\n"
240 "} catch (e) {\n"
241 " print(e.stack || e);\n"
242 "}\n");
243 printf("\n");
244
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));
249 duk_pop(ctx);
250 printf("\n");
251
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));
255 duk_pop(ctx);
256 printf("\n");
257
258 printf("*** test5 - ecmascript try-catch\n");
259 duk_push_c_function(ctx, test5, 0);
260 duk_put_global_string(ctx, "test5");
262 "try {\n"
263 " test5();\n"
264 "} catch (e) {\n"
265 " print(e.stack || e);\n"
266 "}\n");
267 printf("\n");
268
269 printf("*** done\n");
270
271 duk_destroy_heap(ctx);
272
273 return 0;
274}
duk_ret_t test2(duk_context *ctx)
duk_ret_t test3(duk_context *ctx)
duk_ret_t test1(duk_context *ctx)
duk_ret_t test4(duk_context *ctx)
duk_ret_t test5(duk_context *ctx)
duk_int_fast32_t duk_int_t
DUK_EXTERNAL duk_bool_t duk_put_global_string(duk_context *ctx, const char *key)
DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_context *ctx, duk_c_function func, duk_int_t nargs)
DUK_EXTERNAL duk_int_t duk_safe_call(duk_context *ctx, duk_safe_call_function func, duk_idx_t nargs, duk_idx_t nrets)
DUK_EXTERNAL void duk_destroy_heap(duk_context *ctx)
DUK_EXTERNAL void duk_pop(duk_context *ctx)
DUK_EXTERNAL duk_int_t duk_pcall(duk_context *ctx, duk_idx_t nargs)
#define duk_safe_to_string(ctx, index)
#define duk_create_heap_default()
#define duk_eval_string_noresult(ctx, src)
#define printf

References duk_create_heap_default, duk_destroy_heap(), duk_eval_string_noresult, duk_pcall(), duk_pop(), duk_push_c_function(), duk_put_global_string(), duk_safe_call(), duk_safe_to_string, printf, test1(), test2(), test3(), test4(), and test5().

◆ test1()

duk_ret_t test1 ( duk_context * ctx)

Definition at line 61 of file duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp.

61 {
62 my_class myclass;
63
64 duk_eval_string(ctx, "aiee=");
65
66 return 0;
67}
#define duk_eval_string(ctx, src)

References duk_eval_string.

Referenced by main().

◆ test2()

duk_ret_t test2 ( duk_context * ctx)

Definition at line 74 of file duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp.

74 {
75 my_class myclass;
76
77 try {
78 throw 123;
79 } catch (int myvalue) {
80 printf("Caught: %d\n", myvalue);
81 }
82
83 return 0;
84}

References printf.

Referenced by main().

◆ test3()

duk_ret_t test3 ( duk_context * ctx)

Definition at line 95 of file duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp.

95 {
96 my_class myclass;
97
98 throw 123; /* ERROR: exception propagated to Duktape */
99
100 return 0;
101}

Referenced by main().

◆ test4()

duk_ret_t test4 ( duk_context * ctx)

Definition at line 114 of file duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp.

114 {
115 my_class myclass;
116 my_exception myexc;
117
118 throw myexc; /* ERROR: exception propagated to Duktape */
119
120 return 0;
121}

Referenced by main().

◆ test5()

duk_ret_t test5 ( duk_context * ctx)

Definition at line 134 of file duktape-1.5.2/examples/cpp-exceptions/cpp_exceptions.cpp.

134 {
135 my_class myclass;
136 my_exception2 myexc;
137
138 throw myexc; /* ERROR: exception propagated to Duktape */
139
140 return 0;
141}

Referenced by main().