|
|
@ -40,14 +40,7 @@ |
|
|
|
#define MEM_LIMIT_HIGH (2047*1024*1024) /* ~2 GB */ |
|
|
|
#define LINEBUF_SIZE 65536 |
|
|
|
|
|
|
|
/* FIXME: additional modules should probably only be in some separate tool? */ |
|
|
|
#if 0 |
|
|
|
extern void duk_ncurses_register(duk_context *ctx); |
|
|
|
extern void duk_socket_register(duk_context *ctx); |
|
|
|
extern void duk_fileio_register(duk_context *ctx); |
|
|
|
#endif |
|
|
|
|
|
|
|
int interactive_mode = 0; |
|
|
|
static int interactive_mode = 0; |
|
|
|
|
|
|
|
#ifndef NO_RLIMIT |
|
|
|
static void set_resource_limits(rlim_t mem_limit_value) { |
|
|
@ -119,52 +112,40 @@ static void print_error(duk_context *ctx, FILE *f) { |
|
|
|
duk_pop(ctx); |
|
|
|
} |
|
|
|
|
|
|
|
int wrapped_compile_execute(duk_context *ctx) { |
|
|
|
static int wrapped_compile_execute(duk_context *ctx) { |
|
|
|
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. |
|
|
|
*/ |
|
|
|
|
|
|
|
comp_flags = 0; |
|
|
|
duk_compile(ctx, comp_flags); |
|
|
|
|
|
|
|
#if 0 |
|
|
|
/* FIXME: something similar with public API */ |
|
|
|
if (interactive_mode) { |
|
|
|
duk_hcompiledfunction *f = (duk_hcompiledfunction *) duk_get_hobject(ctx, -1); |
|
|
|
|
|
|
|
if (f && DUK_HOBJECT_IS_COMPILEDFUNCTION((duk_hobject *) f)) { |
|
|
|
fprintf(stdout, "[bytecode length %d opcodes, registers %d, constants %d, inner functions %d]\n", |
|
|
|
(int) DUK_HCOMPILEDFUNCTION_GET_CODE_COUNT(f), |
|
|
|
(int) f->nregs, |
|
|
|
(int) DUK_HCOMPILEDFUNCTION_GET_CONSTS_COUNT(f), |
|
|
|
(int) DUK_HCOMPILEDFUNCTION_GET_FUNCS_COUNT(f)); |
|
|
|
fflush(stdout); |
|
|
|
} else { |
|
|
|
fprintf(stdout, "[invalid compile result]\n"); |
|
|
|
fflush(stdout); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
duk_push_global_object(ctx); /* 'this' binding */ |
|
|
|
duk_call_method(ctx, 0); |
|
|
|
|
|
|
|
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: |
|
|
|
* NOTE: the ToString() coercion may fail in some cases; |
|
|
|
* for instance, if you evaluate: |
|
|
|
* |
|
|
|
* ( {valueOf: function() {return {}}, toString: function() {return {}}}); |
|
|
|
* ( {valueOf: function() {return {}}, |
|
|
|
* toString: function() {return {}}}); |
|
|
|
* |
|
|
|
* The error is: |
|
|
|
* |
|
|
|
* TypeError: failed to coerce with [[DefaultValue]] |
|
|
|
* duk_api.c:1420 |
|
|
|
* |
|
|
|
* These errors are caught and printed out as errors although |
|
|
|
* the errors are not generated by user code as such. Changing |
|
|
|
* duk_to_string() to duk_safe_to_string() would avoid these |
|
|
|
* errors. |
|
|
|
* 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)); |
|
|
@ -180,7 +161,7 @@ int wrapped_compile_execute(duk_context *ctx) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int handle_fh(duk_context *ctx, FILE *f, const char *filename) { |
|
|
|
static int handle_fh(duk_context *ctx, FILE *f, const char *filename) { |
|
|
|
char *buf = NULL; |
|
|
|
int len; |
|
|
|
int got; |
|
|
@ -231,7 +212,7 @@ int handle_fh(duk_context *ctx, FILE *f, const char *filename) { |
|
|
|
goto cleanup; |
|
|
|
} |
|
|
|
|
|
|
|
int handle_file(duk_context *ctx, const char *filename) { |
|
|
|
static int handle_file(duk_context *ctx, const char *filename) { |
|
|
|
FILE *f = NULL; |
|
|
|
int retval; |
|
|
|
|
|
|
@ -252,7 +233,7 @@ int handle_file(duk_context *ctx, const char *filename) { |
|
|
|
} |
|
|
|
|
|
|
|
#ifdef NO_READLINE |
|
|
|
int handle_interactive(duk_context *ctx) { |
|
|
|
static int handle_interactive(duk_context *ctx) { |
|
|
|
const char *prompt = "duk> "; |
|
|
|
char *buffer = NULL; |
|
|
|
int retval = 0; |
|
|
@ -317,7 +298,7 @@ int handle_interactive(duk_context *ctx) { |
|
|
|
return retval; |
|
|
|
} |
|
|
|
#else /* NO_READLINE */ |
|
|
|
int handle_interactive(duk_context *ctx) { |
|
|
|
static int handle_interactive(duk_context *ctx) { |
|
|
|
const char *prompt = "duk> "; |
|
|
|
char *buffer = NULL; |
|
|
|
int retval = 0; |
|
|
@ -435,11 +416,6 @@ int main(int argc, char *argv[]) { |
|
|
|
*/ |
|
|
|
|
|
|
|
ctx = duk_create_heap_default(); |
|
|
|
#if 0 |
|
|
|
duk_ncurses_register(ctx); |
|
|
|
duk_socket_register(ctx); |
|
|
|
duk_fileio_register(ctx); |
|
|
|
#endif |
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) { |
|
|
|
char *arg = argv[i]; |
|
|
|