static int wrapped_compile_execute(duk_context *ctx) { const char *src_data; duk_size_t src_len; int comp_flags; /* XXX: Here it'd be nice to get some stats for the compilation result * when a suitable command line is given (e.g. code size, constant * count, function count. These are available internally but not through * the public API. */ /* Use duk_compile_lstring_filename() variant which avoids interning * the source code. This only really matters for low memory environments. */ /* [ ... src_data src_len filename ] */ comp_flags = 0; src_data = (const char *) duk_require_pointer(ctx, -3); src_len = (duk_size_t) duk_require_uint(ctx, -2); duk_compile_lstring_filename(ctx, comp_flags, src_data, src_len); /* [ ... src_data src_len function ] */ #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_start_exec_timeout(); #endif duk_push_global_object(ctx); /* 'this' binding */ duk_call_method(ctx, 0); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (interactive_mode) { /* * 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 * * These are handled now by the caller which also has stack * trace printing support. User code can print out errors * safely using duk_safe_to_string(). */ fprintf(stdout, "= %s\n", duk_to_string(ctx, -1)); fflush(stdout); } else { /* 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); return 0; }
static int wrapped_compile_execute(duk_context *ctx) { const char *src_data; duk_size_t src_len; int comp_flags; /* XXX: Here it'd be nice to get some stats for the compilation result * when a suitable command line is given (e.g. code size, constant * count, function count. These are available internally but not through * the public API. */ /* Use duk_compile_lstring_filename() variant which avoids interning * the source code. This only really matters for low memory environments. */ /* [ ... bytecode_filename src_data src_len filename ] */ src_data = (const char *) duk_require_pointer(ctx, -3); src_len = (duk_size_t) duk_require_uint(ctx, -2); if (src_data != NULL && src_len >= 2 && src_data[0] == (char) 0xff) { /* Bytecode. */ duk_push_lstring(ctx, src_data, src_len); duk_to_buffer(ctx, -1, NULL); duk_load_function(ctx); } else { /* Source code. */ comp_flags = 0; duk_compile_lstring_filename(ctx, comp_flags, src_data, src_len); } /* [ ... bytecode_filename src_data src_len function ] */ /* Optional bytecode dump. */ if (duk_is_string(ctx, -4)) { FILE *f; void *bc_ptr; duk_size_t bc_len; size_t wrote; duk_dup_top(ctx); duk_dump_function(ctx); bc_ptr = duk_require_buffer(ctx, -1, &bc_len); f = fopen(duk_require_string(ctx, -5), "wb"); if (!f) { duk_error(ctx, DUK_ERR_ERROR, "failed to open bytecode output file"); } wrote = fwrite(bc_ptr, 1, (size_t) bc_len, f); /* XXX: handle partial writes */ (void) fclose(f); if (wrote != bc_len) { duk_error(ctx, DUK_ERR_ERROR, "failed to write all bytecode"); } return 0; /* duk_safe_call() cleans up */ } #if 0 /* Manual test for bytecode dump/load cycle: dump and load before * execution. Enable manually, then run "make qecmatest" for a * reasonably good coverage of different functions and programs. */ duk_dump_function(ctx); duk_load_function(ctx); #endif #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_start_exec_timeout(); #endif duk_push_global_object(ctx); /* 'this' binding */ duk_call_method(ctx, 0); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (interactive_mode) { /* * 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 * * These are handled now by the caller which also has stack * trace printing support. User code can print out errors * safely using duk_safe_to_string(). */ fprintf(stdout, "= %s\n", duk_to_string(ctx, -1)); fflush(stdout); } else { /* 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. */ } return 0; /* duk_safe_call() cleans up */ }