Browse Source

avoid ToString() coercion of final Program result in non-interactive mode, as it may itself throw a TypeError (e.g. failed to coerce with [[DefaultValue]])

pull/1/head
Sami Vaarala 11 years ago
parent
commit
dc00f8812e
  1. 25
      examples/cmdline/duk_cmdline.c

25
examples/cmdline/duk_cmdline.c

@ -146,14 +146,31 @@ int wrapped_compile_execute(duk_context *ctx) {
duk_push_global_object(ctx); /* 'this' binding */
duk_call_method(ctx, 0);
duk_to_string(ctx, -1);
if (interactive_mode) {
/* In interactive mode, write to stdout so output won't interleave as easily. */
/*
* In interactive mode, write to stdout so output won't interleave as easily.
*
* NOTE: the ToString() coercion may fail in some cases; for instance,
* if you evaluate:
*
* ( {valueOf: function() {return {}}, toString: function() {return {}}});
*
* The error is:
*
* TypeError: failed to coerce with [[DefaultValue]]
* duk_api.c:1420
*
* This should be fixed at some point.
*/
duk_to_string(ctx, -1);
fprintf(stdout, "= %s\n", duk_get_string(ctx, -1));
fflush(stdout);
} else {
/* In non-interactive mode, success results are not written at all. */
/* In non-interactive mode, success results are not written at all.
* It is important that the result value is not string coerced,
* as the string coercion may cause an error in some cases.
*/
}
duk_pop(ctx);

Loading…
Cancel
Save