mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
6 years ago
committed by
GitHub
11 changed files with 212 additions and 39 deletions
@ -0,0 +1,44 @@ |
|||
/*===
|
|||
*** test_1 (duk_safe_call) |
|||
duk_get_top[0] = '0' |
|||
duk_safe_to_stacktrace[0] = 'Error |
|||
at err (eval:1) |
|||
at eval (eval:1) preventsyield' |
|||
duk_get_top[1] = '0' |
|||
duk_safe_to_stacktrace[1] = '4' |
|||
duk_get_top[2] = '0' |
|||
duk_safe_to_stacktrace[2] = 'Error |
|||
at [anon] (eval:1) preventsyield' |
|||
duk_get_top[3] = '0' |
|||
==> rc=0, result='undefined' |
|||
===*/ |
|||
|
|||
static duk_ret_t test_1(duk_context *ctx, void *udata) { |
|||
(void) udata; |
|||
|
|||
printf("duk_get_top[0] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
duk_peval_string(ctx, "function err() { throw new Error(); }; err();"); |
|||
printf("duk_safe_to_stacktrace[0] = '%s'\n", duk_safe_to_stacktrace(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
printf("duk_get_top[1] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
duk_peval_string(ctx, "2+2"); |
|||
printf("duk_safe_to_stacktrace[1] = '%s'\n", duk_safe_to_stacktrace(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
printf("duk_get_top[2] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
duk_peval_string(ctx, "var err = new Error(); Object.defineProperty(err, 'stack', {get: function() { throw new Error(); }}); throw err;"); |
|||
printf("duk_safe_to_stacktrace[2] = '%s'\n", duk_safe_to_stacktrace(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
printf("duk_get_top[3] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void test(duk_context *ctx) { |
|||
TEST_SAFE_CALL(test_1); |
|||
} |
@ -0,0 +1,35 @@ |
|||
/*===
|
|||
*** test_1 (duk_safe_call) |
|||
duk_get_top[0] = '0' |
|||
duk_to_stacktrace[0] = 'Error |
|||
at err (eval:1) |
|||
at eval (eval:1) preventsyield' |
|||
duk_get_top[1] = '0' |
|||
duk_to_stacktrace[1] = '4' |
|||
duk_get_top[2] = '0' |
|||
==> rc=0, result='undefined' |
|||
===*/ |
|||
|
|||
static duk_ret_t test_1(duk_context *ctx, void *udata) { |
|||
(void) udata; |
|||
|
|||
printf("duk_get_top[0] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
duk_peval_string(ctx, "function err() { throw new Error(); }; err();"); |
|||
printf("duk_to_stacktrace[0] = '%s'\n", duk_to_stacktrace(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
printf("duk_get_top[1] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
duk_peval_string(ctx, "2+2"); |
|||
printf("duk_to_stacktrace[1] = '%s'\n", duk_to_stacktrace(ctx, -1)); |
|||
duk_pop(ctx); |
|||
|
|||
printf("duk_get_top[2] = '%ld'\n", (long) duk_get_top(ctx)); |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void test(duk_context *ctx) { |
|||
TEST_SAFE_CALL(test_1); |
|||
} |
@ -0,0 +1,49 @@ |
|||
name: duk_safe_to_stacktrace |
|||
|
|||
proto: | |
|||
const char *duk_safe_to_stacktrace(duk_context *ctx, duk_idx_t idx); |
|||
|
|||
stack: | |
|||
[ ... val! ... ] -> [ ... val.stack! ... ] |
|||
|
|||
summary: | |
|||
<p>Like <code><a href="#duk_safe_to_string">duk_safe_to_string()</a></code> but |
|||
instead of converting a value to a string, it converts an error to a stacktrace. |
|||
If the object is not an error, it will fallback to duk_safe_to_string(). If there |
|||
is an error when reading .stack, it will return the stacktrace of that error.</p> |
|||
|
|||
example: | |
|||
/* Throw Error and print stacktrace */ |
|||
duk_eval_string(ctx, "function err() { throw new Error(); }; err();"); |
|||
printf("stacktrace: %s\n", duk_safe_to_stacktrace(ctx, -1)); /* -> "Error at err (eval:1)" */ |
|||
duk_pop(ctx); |
|||
|
|||
/* Equivalent ECMAScript */ |
|||
try { |
|||
function err() { |
|||
throw new Error(); |
|||
} |
|||
err(); |
|||
} catch (e) { |
|||
if (e instanceof Error) { |
|||
var stack = e.stack; |
|||
if (stack) { |
|||
console.log(stack); |
|||
} else { |
|||
console.log(e.toString()); |
|||
} |
|||
} else { |
|||
console.log(e.toString()); |
|||
} |
|||
} |
|||
|
|||
tags: |
|||
- stack |
|||
- string |
|||
- protected |
|||
|
|||
seealso: |
|||
- duk_safe_to_string |
|||
- duk_to_stacktrace |
|||
|
|||
introduced: 2.4.0 |
@ -0,0 +1,28 @@ |
|||
name: duk_to_stacktrace |
|||
|
|||
proto: | |
|||
const char *duk_to_stacktrace(duk_context *ctx, duk_idx_t idx); |
|||
|
|||
stack: | |
|||
[ ... val! ... ] -> [ ... val.stack! ... ] |
|||
|
|||
summary: | |
|||
<p>Like <code><a href="#duk_to_string">duk_to_string()</a></code> but |
|||
instead of converting a value to a string, it converts an error to a stacktrace. |
|||
If the object is not an error, it will fallback to duk_to_string().</p> |
|||
|
|||
example: | |
|||
/* Throw Error and print stacktrace */ |
|||
duk_eval_string(ctx, "function err() { throw new Error(); }; err();"); |
|||
printf("stacktrace: %s\n", duk_to_stacktrace(ctx, -1)); /* -> "Error at err (eval:1)" */ |
|||
duk_pop(ctx); |
|||
|
|||
tags: |
|||
- stack |
|||
- string |
|||
|
|||
seealso: |
|||
- duk_to_string |
|||
- duk_safe_to_stacktrace |
|||
|
|||
introduced: 2.4.0 |
Loading…
Reference in new issue