static int handle_eval(duk_context *ctx, const char *code) { int rc; int retval = -1; duk_push_pointer(ctx, (void *) code); duk_push_uint(ctx, (duk_uint_t) strlen(code)); duk_push_string(ctx, "eval"); interactive_mode = 0; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (rc != DUK_EXEC_SUCCESS) { print_pop_error(ctx, stderr); } else { duk_pop(ctx); retval = 0; } return retval; }
static int handle_fh(duk_context *ctx, FILE *f, const char *filename, const char *bytecode_filename) { char *buf = NULL; int len; size_t got; int rc; int retval = -1; if (fseek(f, 0, SEEK_END) < 0) { goto error; } len = (int) ftell(f); if (fseek(f, 0, SEEK_SET) < 0) { goto error; } buf = (char *) malloc(len); if (!buf) { goto error; } got = fread((void *) buf, (size_t) 1, (size_t) len, f); duk_push_string(ctx, bytecode_filename); duk_push_pointer(ctx, (void *) buf); duk_push_uint(ctx, (duk_uint_t) got); duk_push_string(ctx, filename); interactive_mode = 0; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 4 /*nargs*/, 1 /*nret*/); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif free(buf); buf = NULL; if (rc != DUK_EXEC_SUCCESS) { print_pop_error(ctx, stderr); goto error; } else { duk_pop(ctx); retval = 0; } /* fall thru */ cleanup: if (buf) { free(buf); } return retval; error: fprintf(stderr, "error in executing file %s\n", filename); fflush(stderr); goto cleanup; }
/* in interactive mode, write to stdout */ print_pop_error(ctx, stdout); retval = -1; /* an error 'taints' the execution */ } else { duk_pop(ctx); } } done: if (buffer) { free(buffer); buffer = NULL; } return retval; } #else /* NO_READLINE */ static int handle_interactive(duk_context *ctx) { const char *prompt = "duk> "; char *buffer = NULL; int retval = 0; int rc; duk_eval_string(ctx, GREET_CODE("")); duk_pop(ctx); /* * Note: using readline leads to valgrind-reported leaks inside * readline itself. Execute code from an input file (and not * through stdin) for clean valgrind runs. */ rl_initialize(); for (;;) { if (buffer) { free(buffer); buffer = NULL; } buffer = readline(prompt); if (!buffer) { break; } if (buffer && buffer[0] != (char) 0) { add_history(buffer); } duk_push_pointer(ctx, (void *) buffer); duk_push_uint(ctx, (duk_uint_t) strlen(buffer)); duk_push_string(ctx, "input"); interactive_mode = 1; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (buffer) { free(buffer); buffer = NULL; } if (rc != DUK_EXEC_SUCCESS) { /* in interactive mode, write to stdout */ print_pop_error(ctx, stdout); retval = -1; /* an error 'taints' the execution */ } else { duk_pop(ctx); } } if (buffer) { free(buffer); buffer = NULL; } return retval; }
static int handle_interactive(duk_context *ctx) { const char *prompt = "duk> "; char *buffer = NULL; int retval = 0; int rc; int got_eof = 0; duk_eval_string(ctx, GREET_CODE(" [no readline]")); duk_pop(ctx); buffer = (char *) malloc(LINEBUF_SIZE); if (!buffer) { fprintf(stderr, "failed to allocated a line buffer\n"); fflush(stderr); retval = -1; goto done; } while (!got_eof) { size_t idx = 0; fwrite(prompt, 1, strlen(prompt), stdout); fflush(stdout); for (;;) { int c = fgetc(stdin); if (c == EOF) { got_eof = 1; break; } else if (c == '\n') { break; } else if (idx >= LINEBUF_SIZE) { fprintf(stderr, "line too long\n"); fflush(stderr); retval = -1; goto done; } else { buffer[idx++] = (char) c; } } duk_push_pointer(ctx, (void *) buffer); duk_push_uint(ctx, (duk_uint_t) idx); duk_push_string(ctx, "input"); interactive_mode = 1; /* global */ rc = duk_safe_call(ctx, wrapped_compile_execute, 3 /*nargs*/, 1 /*nret*/); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (rc != DUK_EXEC_SUCCESS) { /* in interactive mode, write to stdout */ print_pop_error(ctx, stdout); retval = -1; /* an error 'taints' the execution */ } else { duk_pop(ctx); } } done: if (buffer) { free(buffer); buffer = NULL; } return retval; }
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 */ }