static void destroy_duktape_heap(duk_context *ctx, int alloc_provider) { (void) alloc_provider; #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump before duk_destroy_heap(), before forced gc\n"); ajsheap_dump(); duk_gc(ctx, 0); fprintf(stdout, "Pool dump before duk_destroy_heap(), after forced gc\n"); ajsheap_dump(); } #endif if (ctx) { duk_destroy_heap(ctx); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump after duk_destroy_heap() (should have zero allocs)\n"); ajsheap_dump(); } #endif }
static duk_ret_t test_finalizer_loop(duk_context *ctx, void *udata) { (void) udata; /* Setup a finalizer loop: the finalizer of a finalizer is the * finalizer itself. The finalizer won't be called recursively. */ duk_eval_string(ctx, "(function (obj) { print('finalizer called'); })"); duk_dup(ctx, -1); duk_set_finalizer(ctx, -2); printf("before pop\n"); duk_pop(ctx); printf("after pop\n"); /* The finalizer participates in two circular references so it won't * be collected until mark-and-sweep happens. The first circular * reference is the function<->prototype loop. The second circular * reference is the finalizer reference which points to the object * itself. */ printf("before forced gc\n"); duk_gc(ctx, 0); printf("after forced gc\n"); return 0; }
static duk_ret_t test_gc(duk_context *ctx, void *udata) { (void) udata; prep(ctx); /* Both objects are now in a prototype loop. Force garbage * collection to ensure nothing breaks. */ printf("first gc\n"); fflush(stdout); duk_gc(ctx, 0); /* Make the objects unreachable and re-run GC. This triggers * e.g. finalizer checks. */ printf("make unreachable\n"); fflush(stdout); duk_set_top(ctx, 0); printf("second gc\n"); fflush(stdout); duk_gc(ctx, 0); return 0; }
static duk_ret_t test_func(duk_context *ctx, void *udata) { (void) udata; if (ctx) { printf("dummy - return here\n"); fflush(stdout); return 0; } /* Up-to-date for Duktape 1.3.0, alphabetical order: * $ cd website/api; ls *.yaml */ (void) duk_alloc_raw(ctx, 0); (void) duk_alloc(ctx, 0); (void) duk_base64_decode(ctx, 0); (void) duk_base64_encode(ctx, 0); (void) duk_buffer_to_string(ctx, 0); (void) duk_call_method(ctx, 0); (void) duk_call_prop(ctx, 0, 0); (void) duk_call(ctx, 0); (void) duk_char_code_at(ctx, 0, 0); (void) duk_check_stack_top(ctx, 0); (void) duk_check_stack(ctx, 0); (void) duk_check_type_mask(ctx, 0, 0); (void) duk_check_type(ctx, 0, 0); (void) duk_compact(ctx, 0); (void) duk_compile_lstring_filename(ctx, 0, "dummy", 0); (void) duk_compile_lstring(ctx, 0, "dummy", 0); (void) duk_compile_string_filename(ctx, 0, "dummy"); (void) duk_compile_string(ctx, 0, "dummy"); (void) duk_compile(ctx, 0); (void) duk_concat(ctx, 0); (void) duk_config_buffer(ctx, 0, NULL, 0); (void) duk_copy(ctx, 0, 0); (void) duk_create_heap_default(); (void) duk_create_heap(NULL, NULL, NULL, NULL, NULL); (void) duk_debugger_attach(ctx, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); (void) duk_debugger_cooperate(ctx); (void) duk_debugger_detach(ctx); (void) duk_debugger_notify(ctx, 0); (void) duk_debugger_pause(ctx); (void) duk_decode_string(ctx, 0, NULL, NULL); (void) duk_def_prop(ctx, 0, 0); (void) duk_del_prop_index(ctx, 0, 0); (void) duk_del_prop_string(ctx, 0, "dummy"); (void) duk_del_prop(ctx, 0); (void) duk_destroy_heap(ctx); (void) duk_dump_function(ctx); (void) duk_dup_top(ctx); (void) duk_dup(ctx, 0); (void) duk_enum(ctx, 0, 0); (void) duk_equals(ctx, 0, 0); duk_error_va(ctx, 0, NULL, NULL); duk_error(ctx, 0, "dummy"); /* (void) cast won't work without variadic macros */ (void) duk_eval_lstring_noresult(ctx, "dummy", 0); (void) duk_eval_lstring(ctx, "dummy", 0); (void) duk_eval_noresult(ctx); (void) duk_eval_string_noresult(ctx, "dummy"); (void) duk_eval_string(ctx, "dummy"); (void) duk_eval(ctx); (void) duk_fatal(ctx, "dummy"); (void) duk_free_raw(ctx, NULL); (void) duk_free(ctx, NULL); (void) duk_gc(ctx, 0); (void) duk_get_boolean(ctx, 0); (void) duk_get_buffer_data(ctx, 0, NULL); (void) duk_get_buffer(ctx, 0, NULL); (void) duk_get_c_function(ctx, 0); (void) duk_get_context(ctx, 0); (void) duk_get_current_magic(ctx); (void) duk_get_error_code(ctx, 0); (void) duk_get_finalizer(ctx, 0); (void) duk_get_global_string(ctx, 0); (void) duk_get_heapptr(ctx, 0); (void) duk_get_int(ctx, 0); (void) duk_get_length(ctx, 0); (void) duk_get_lstring(ctx, 0, NULL); (void) duk_get_magic(ctx, 0); (void) duk_get_memory_functions(ctx, NULL); (void) duk_get_number(ctx, 0); (void) duk_get_pointer(ctx, 0); (void) duk_get_prop_index(ctx, 0, 0); (void) duk_get_prop_string(ctx, 0, "dummy"); (void) duk_get_prop(ctx, 0); (void) duk_get_prototype(ctx, 0); (void) duk_get_string(ctx, 0); (void) duk_get_top_index(ctx); (void) duk_get_top(ctx); (void) duk_get_type_mask(ctx, 0); (void) duk_get_type(ctx, 0); (void) duk_get_uint(ctx, 0); (void) duk_has_prop_index(ctx, 0, 0); (void) duk_has_prop_string(ctx, 0, "dummy"); (void) duk_has_prop(ctx, 0); (void) duk_hex_decode(ctx, 0); (void) duk_hex_encode(ctx, 0); (void) duk_insert(ctx, 0); (void) duk_instanceof(ctx, 0, 0); (void) duk_is_array(ctx, 0); (void) duk_is_boolean(ctx, 0); (void) duk_is_bound_function(ctx, 0); (void) duk_is_buffer(ctx, 0); (void) duk_is_callable(ctx, 0); (void) duk_is_c_function(ctx, 0); (void) duk_is_constructor_call(ctx); (void) duk_is_dynamic_buffer(ctx, 0); (void) duk_is_ecmascript_function(ctx, 0); (void) duk_is_error(ctx, 0); (void) duk_is_eval_error(ctx, 0); (void) duk_is_fixed_buffer(ctx, 0); (void) duk_is_function(ctx, 0); (void) duk_is_lightfunc(ctx, 0); (void) duk_is_nan(ctx, 0); (void) duk_is_null_or_undefined(ctx, 0); (void) duk_is_null(ctx, 0); (void) duk_is_number(ctx, 0); (void) duk_is_object_coercible(ctx, 0); (void) duk_is_object(ctx, 0); (void) duk_is_pointer(ctx, 0); (void) duk_is_primitive(ctx, 0); (void) duk_is_range_error(ctx, 0); (void) duk_is_reference_error(ctx, 0); (void) duk_is_strict_call(ctx); (void) duk_is_string(ctx, 0); (void) duk_is_syntax_error(ctx, 0); (void) duk_is_thread(ctx, 0); (void) duk_is_type_error(ctx, 0); (void) duk_is_undefined(ctx, 0); (void) duk_is_uri_error(ctx, 0); (void) duk_is_valid_index(ctx, 0); (void) duk_join(ctx, 0); (void) duk_json_decode(ctx, 0); (void) duk_json_encode(ctx, 0); (void) duk_load_function(ctx); (void) duk_map_string(ctx, 0, NULL, NULL); (void) duk_new(ctx, 0); (void) duk_next(ctx, 0, 0); (void) duk_normalize_index(ctx, 0); (void) duk_pcall_method(ctx, 0); (void) duk_pcall_prop(ctx, 0, 0); (void) duk_pcall(ctx, 0); (void) duk_pcompile_lstring_filename(ctx, 0, "dummy", 0); (void) duk_pcompile_lstring(ctx, 0, "dummy", 0); (void) duk_pcompile_string_filename(ctx, 0, "dummy"); (void) duk_pcompile_string(ctx, 0, "dummy"); (void) duk_pcompile(ctx, 0); (void) duk_peval_lstring_noresult(ctx, "dummy", 0); (void) duk_peval_lstring(ctx, "dummy", 0); (void) duk_peval_noresult(ctx); (void) duk_peval_string_noresult(ctx, "dummy"); (void) duk_peval_string(ctx, "dummy"); (void) duk_peval(ctx); (void) duk_pnew(ctx, 0); (void) duk_pop_2(ctx); (void) duk_pop_3(ctx); (void) duk_pop_n(ctx, 0); (void) duk_pop(ctx); (void) duk_push_array(ctx); (void) duk_push_boolean(ctx, 0); (void) duk_push_buffer_object(ctx, 0, 0, 0, 0); (void) duk_push_buffer(ctx, 0, 0); (void) duk_push_c_function(ctx, NULL, 0); (void) duk_push_c_lightfunc(ctx, NULL, 0, 0, 0); (void) duk_push_context_dump(ctx); (void) duk_push_current_function(ctx); (void) duk_push_current_thread(ctx); (void) duk_push_dynamic_buffer(ctx, 0); (void) duk_push_error_object_va(ctx, 0, NULL, NULL); (void) duk_push_error_object(ctx, 0, "dummy"); (void) duk_push_external_buffer(ctx); (void) duk_push_false(ctx); (void) duk_push_fixed_buffer(ctx, 0); (void) duk_push_global_object(ctx); (void) duk_push_global_stash(ctx); (void) duk_push_heap_stash(ctx); (void) duk_push_heapptr(ctx, NULL); (void) duk_push_int(ctx, 0); (void) duk_push_lstring(ctx, "dummy", 0); (void) duk_push_nan(ctx); (void) duk_push_null(ctx); (void) duk_push_number(ctx, 0.0); (void) duk_push_object(ctx); (void) duk_push_pointer(ctx, NULL); (void) duk_push_sprintf(ctx, "dummy"); (void) duk_push_string(ctx, "dummy"); (void) duk_push_this(ctx); (void) duk_push_thread_new_globalenv(ctx); (void) duk_push_thread_stash(ctx, NULL); (void) duk_push_thread(ctx); (void) duk_push_true(ctx); (void) duk_push_uint(ctx, 0); (void) duk_push_undefined(ctx); (void) duk_push_vsprintf(ctx, "dummy", NULL); (void) duk_put_function_list(ctx, 0, NULL); (void) duk_put_global_string(ctx, NULL); (void) duk_put_number_list(ctx, 0, NULL); (void) duk_put_prop_index(ctx, 0, 0); (void) duk_put_prop_string(ctx, 0, "dummy"); (void) duk_put_prop(ctx, 0); (void) duk_realloc_raw(ctx, NULL, 0); (void) duk_realloc(ctx, NULL, 0); (void) duk_remove(ctx, 0); (void) duk_replace(ctx, 0); (void) duk_require_boolean(ctx, 0); (void) duk_require_buffer_data(ctx, 0, NULL); (void) duk_require_buffer(ctx, 0, NULL); (void) duk_require_c_function(ctx, 0); (void) duk_require_callable(ctx, 0); (void) duk_require_context(ctx, 0); (void) duk_require_function(ctx, 0); (void) duk_require_heapptr(ctx, 0); (void) duk_require_int(ctx, 0); (void) duk_require_lstring(ctx, 0, NULL); (void) duk_require_normalize_index(ctx, 0); (void) duk_require_null(ctx, 0); (void) duk_require_number(ctx, 0); (void) duk_require_object_coercible(ctx, 0); (void) duk_require_pointer(ctx, 0); (void) duk_require_stack_top(ctx, 0); (void) duk_require_stack(ctx, 0); (void) duk_require_string(ctx, 0); (void) duk_require_top_index(ctx); (void) duk_require_type_mask(ctx, 0, 0); (void) duk_require_uint(ctx, 0); (void) duk_require_undefined(ctx, 0); (void) duk_require_valid_index(ctx, 0); (void) duk_resize_buffer(ctx, 0, 0); (void) duk_safe_call(ctx, NULL, NULL, 0, 0); (void) duk_safe_to_lstring(ctx, 0, NULL); (void) duk_safe_to_string(ctx, 0); (void) duk_set_finalizer(ctx, 0); (void) duk_set_global_object(ctx); (void) duk_set_magic(ctx, 0, 0); (void) duk_set_prototype(ctx, 0); (void) duk_set_top(ctx, 0); (void) duk_steal_buffer(ctx, 0, NULL); (void) duk_strict_equals(ctx, 0, 0); (void) duk_substring(ctx, 0, 0, 0); (void) duk_swap_top(ctx, 0); (void) duk_swap(ctx, 0, 0); (void) duk_throw(ctx); (void) duk_to_boolean(ctx, 0); (void) duk_to_buffer(ctx, 0, NULL); (void) duk_to_defaultvalue(ctx, 0, 0); (void) duk_to_dynamic_buffer(ctx, 0, NULL); (void) duk_to_fixed_buffer(ctx, 0, NULL); (void) duk_to_int32(ctx, 0); (void) duk_to_int(ctx, 0); (void) duk_to_lstring(ctx, 0, NULL); (void) duk_to_null(ctx, 0); (void) duk_to_number(ctx, 0); (void) duk_to_object(ctx, 0); (void) duk_to_pointer(ctx, 0); (void) duk_to_primitive(ctx, 0, 0); (void) duk_to_string(ctx, 0); (void) duk_to_uint16(ctx, 0); (void) duk_to_uint32(ctx, 0); (void) duk_to_uint(ctx, 0); (void) duk_to_undefined(ctx, 0); (void) duk_trim(ctx, 0); (void) duk_xcopy_top(ctx, NULL, 0); (void) duk_xmove_top(ctx, NULL, 0); printf("never here\n"); fflush(stdout); return 0; }
static duk_ret_t test_basic(duk_context *ctx) { duk_idx_t i, n; void *ptr; void *p1, *p2, *p3; duk_int_t ret; duk_push_undefined(ctx); duk_push_null(ctx); duk_push_boolean(ctx, 1); duk_push_number(ctx, 123.0); duk_push_string(ctx, "test string"); duk_eval_string(ctx, "({ foo: 'bar' })"); duk_eval_string(ctx, "Duktape.dec('hex', 'deadbeef');"); /* buffer */ /* Print pointer NULL/non-NULL */ n = duk_get_top(ctx); for (i = 0; i < n + 1; i++) { /* Note: access i == n on purpose (invalid index) */ ptr = duk_get_heapptr(ctx, i); printf("top: %ld\n", (long) duk_get_top(ctx)); printf("idx %ld: type %ld, duk_get_heapptr() -> %s\n", (long) i, (long) duk_get_type(ctx, i), (ptr ? "non-NULL" : "NULL")); duk_push_uint(ctx, (duk_uint_t) i); ret = duk_safe_call(ctx, raw_require_heapptr, 1 /*nargs*/, 1 /*nrets*/); if (ret == DUK_EXEC_SUCCESS) { ; } else { printf("idx %ld: type %ld, duk_require_heapptr() -> %s\n", (long) i, (long) duk_get_type(ctx, i), duk_safe_to_string(ctx, -1)); } duk_pop(ctx); } /* Write the values to the global stash to simulate whatever * reachability mechanisms user code uses. */ n = duk_get_top(ctx); duk_push_global_stash(ctx); for (i = 0; i < n; i++) { duk_dup(ctx, i); duk_put_prop_index(ctx, -2, (duk_uarridx_t) i); } /* Get borrowed references */ p1 = duk_get_heapptr(ctx, 4); p2 = duk_get_heapptr(ctx, 5); p3 = duk_get_heapptr(ctx, 6); /* Erase value stack, simulating user code moving on and relying on * stashed values keeping the target values reachable. Force a GC * for good measure. */ duk_set_top(ctx, 0); duk_gc(ctx, 0); /* Push the values back one by one and test that they're intact. */ duk_eval_string(ctx, "(function (v) { print(Duktape.enc('jx', v)); })"); duk_dup_top(ctx); duk_push_heapptr(ctx, p1); duk_call(ctx, 1); duk_pop(ctx); duk_dup_top(ctx); duk_push_heapptr(ctx, p2); duk_call(ctx, 1); duk_pop(ctx); duk_dup_top(ctx); duk_push_heapptr(ctx, p3); duk_call(ctx, 1); duk_pop(ctx); duk_dup_top(ctx); duk_push_heapptr(ctx, NULL); duk_call(ctx, 1); duk_pop(ctx); duk_pop(ctx); /* Done. */ printf("final top: %ld\n", (long) duk_get_top(ctx)); return 0; }
int main(int argc, char *argv[]) { duk_context *ctx = NULL; int retval = 0; int have_files = 0; int have_eval = 0; int interactive = 0; int memlimit_high = 1; int alloc_provider = ALLOC_DEFAULT; int i; #ifdef DUK_CMDLINE_AJSHEAP alloc_provider = ALLOC_AJSHEAP; #endif /* * Signal handling setup */ #ifndef NO_SIGNAL set_sigint_handler(); /* This is useful at the global level; libraries should avoid SIGPIPE though */ /*signal(SIGPIPE, SIG_IGN);*/ #endif /* * Parse options */ for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!arg) { goto usage; } if (strcmp(arg, "--restrict-memory") == 0) { memlimit_high = 0; } else if (strcmp(arg, "-i") == 0) { interactive = 1; } else if (strcmp(arg, "-e") == 0) { have_eval = 1; if (i == argc - 1) { goto usage; } i++; /* skip code */ } else if (strcmp(arg, "--alloc-default") == 0) { alloc_provider = ALLOC_DEFAULT; } else if (strcmp(arg, "--alloc-logging") == 0) { alloc_provider = ALLOC_LOGGING; } else if (strcmp(arg, "--alloc-torture") == 0) { alloc_provider = ALLOC_TORTURE; } else if (strcmp(arg, "--alloc-hybrid") == 0) { alloc_provider = ALLOC_HYBRID; } else if (strcmp(arg, "--alloc-ajsheap") == 0) { alloc_provider = ALLOC_AJSHEAP; } else if (strlen(arg) >= 1 && arg[0] == '-') { goto usage; } else { have_files = 1; } } if (!have_files && !have_eval) { interactive = 1; } /* * Memory limit */ #ifndef NO_RLIMIT set_resource_limits(memlimit_high ? MEM_LIMIT_HIGH : MEM_LIMIT_NORMAL); #else if (memlimit_high == 0) { fprintf(stderr, "Warning: option --restrict-memory ignored, no rlimit support\n"); fflush(stderr); } #endif /* * Create context */ ctx = NULL; if (!ctx && alloc_provider == ALLOC_LOGGING) { #ifdef DUK_CMDLINE_ALLOC_LOGGING ctx = duk_create_heap(duk_alloc_logging, duk_realloc_logging, duk_free_logging, NULL, NULL); #else fprintf(stderr, "Warning: option --alloc-logging ignored, no logging allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_TORTURE) { #ifdef DUK_CMDLINE_ALLOC_TORTURE ctx = duk_create_heap(duk_alloc_torture, duk_realloc_torture, duk_free_torture, NULL, NULL); #else fprintf(stderr, "Warning: option --alloc-torture ignored, no torture allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_HYBRID) { #ifdef DUK_CMDLINE_ALLOC_HYBRID void *udata = duk_alloc_hybrid_init(); if (!udata) { fprintf(stderr, "Failed to init hybrid allocator\n"); fflush(stderr); } else { ctx = duk_create_heap(duk_alloc_hybrid, duk_realloc_hybrid, duk_free_hybrid, udata, NULL); } #else fprintf(stderr, "Warning: option --alloc-hybrid ignored, no hybrid allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_AJSHEAP) { #ifdef DUK_CMDLINE_AJSHEAP ajsheap_init(); ctx = duk_create_heap(AJS_Alloc, AJS_Realloc, AJS_Free, (void *) &ctx, /* alloc_udata */ NULL); /* fatal_handler */ #else fprintf(stderr, "Warning: option --alloc-ajsheap ignored, no ajsheap allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_DEFAULT) { ctx = duk_create_heap_default(); } if (!ctx) { fprintf(stderr, "Failed to create Duktape heap\n"); fflush(stderr); exit(-1); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump after heap creation\n"); fflush(stdout); AJS_HeapDump(); fflush(stdout); } #endif #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { ajsheap_register(ctx); } #endif /* * Execute any argument file(s) */ for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!arg) { continue; } else if (strlen(arg) == 2 && strcmp(arg, "-e") == 0) { /* Here we know the eval arg exists but check anyway */ if (i == argc - 1) { retval = 1; goto cleanup; } if (handle_eval(ctx, argv[i + 1]) != 0) { retval = 1; goto cleanup; } i++; /* skip code */ continue; } else if (strlen(arg) >= 1 && arg[0] == '-') { continue; } if (handle_file(ctx, arg) != 0) { retval = 1; goto cleanup; } } /* * Enter interactive mode if options indicate it */ if (interactive) { if (handle_interactive(ctx) != 0) { retval = 1; goto cleanup; } } /* * Cleanup and exit */ cleanup: if (interactive) { fprintf(stderr, "Cleaning up...\n"); fflush(stderr); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump before duk_destroy_heap(), before forced gc\n"); fflush(stdout); AJS_HeapDump(); fflush(stdout); duk_gc(ctx, 0); fprintf(stdout, "Pool dump before duk_destroy_heap(), after forced gc\n"); fflush(stdout); AJS_HeapDump(); fflush(stdout); } #endif if (ctx) { duk_destroy_heap(ctx); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump after duk_destroy_heap() (should have zero allocs)\n"); fflush(stdout); AJS_HeapDump(); fflush(stdout); } #endif return retval; /* * Usage */ usage: fprintf(stderr, "Usage: duk [options] [<filenames>]\n" "\n" " -i enter interactive mode after executing argument file(s) / eval code\n" " -e CODE evaluate code\n" " --restrict-memory use lower memory limit (used by test runner)\n" " --alloc-default use Duktape default allocator\n" #ifdef DUK_CMDLINE_ALLOC_LOGGING " --alloc-logging use logging allocator (writes to /tmp)\n" #endif #ifdef DUK_CMDLINE_ALLOC_TORTURE " --alloc-torture use torture allocator\n" #endif #ifdef DUK_CMDLINE_ALLOC_HYBRID " --alloc-hybrid use hybrid allocator\n" #endif #ifdef DUK_CMDLINE_AJSHEAP " --alloc-ajsheap use ajsheap allocator (enabled by default with 'ajduk')\n" #endif "\n" "If <filename> is omitted, interactive mode is started automatically.\n"); fflush(stderr); exit(1); }
int main(int argc, char *argv[]) { /* initialize js environment */ duk_context *ctx = duk_create_heap_default(); /* add native functions to js environment */ duk_push_global_object(ctx); /* 'read' */ duk_push_c_function(ctx, js_read_file, 1); duk_put_prop_string(ctx, -2, "read"); #ifdef USE_PROMPT /* 'prompt' */ duk_push_c_function(ctx, js_prompt, 1); duk_put_prop_string(ctx, -2, "prompt"); #endif /* 'get' */ duk_push_c_function(ctx, js_level_get, 2); duk_put_prop_string(ctx, -2, "get"); /* 'set' */ duk_push_c_function(ctx, js_level_set, 3); duk_put_prop_string(ctx, -2, "set"); /* 'register' */ duk_push_c_function(ctx, js_register_block, 3); duk_put_prop_string(ctx, -2, "register"); /* 'load_file' */ duk_push_c_function(ctx, js_load_file, 1); duk_put_prop_string(ctx, -2, "load_file"); /* 'fill' */ duk_push_c_function(ctx, js_algo_fill, 1); duk_put_prop_string(ctx, -2, "fill"); /* 'findall' */ duk_push_c_function(ctx, js_algo_findall, 1); duk_put_prop_string(ctx, -2, "findall"); /* 'neighbors_of' */ duk_push_c_function(ctx, js_algo_neighbors_of, 3); duk_put_prop_string(ctx, -2, "neighbors_of"); /* 'distance' */ duk_push_c_function(ctx, js_algo_raycast, 4); duk_put_prop_string(ctx, -2, "raycast"); /* pop global object */ duk_pop(ctx); /* try to load some basic functionality */ if (duk_peval_file(ctx, "base/base.js") != 0) { //printf("Failed to load 'base/base.js': %s\n", duk_safe_to_string(ctx, -1)); /* When failing to load base/base.js, se simple, default module loader. */ duk_eval_string_noresult(ctx, "Duktape.modSearch = function(id) { return read(id); };"); } duk_pop(ctx); if (argc > 1) { /* run garbage collector (twice as told by the docs) */ duk_gc(ctx, 0); duk_gc(ctx, 0); /* where to save the png */ char *export_file = "generated_map"; int generate_num = 1; int save_png_disabled = 0; /* parse command line flags */ for (int i = 0; i < argc; ++i) { if (!strcmp(argv[i], "--width") || !strcmp(argv[i], "-w")) { if (i + 1 < argc) js_level_width = atoi(argv[i + 1]); } else if (!strcmp(argv[i], "--height") || !strcmp(argv[i], "-h")) { if (i + 1 < argc) js_level_height = atoi(argv[i + 1]); } else if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output")) { if (i + 1 < argc) export_file = argv[i + 1]; } else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--count")) { if (i + 1 < argc) generate_num = atoi(argv[i + 1]); } else if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--no-export")) { save_png_disabled = 1; } } /* reserve enough memory for level */ js_level = malloc(sizeof(int) * js_level_width * js_level_height); blocks = malloc(sizeof(struct color) * blocks_max_len); /* set all zeroes, maybe change this use to calloc */ memset(js_level, 0, sizeof(int) * js_level_width * js_level_height); /* declare & assign global variable Width & Height */ duk_push_int(ctx, js_level_width); duk_put_global_string(ctx, "Width"); duk_push_int(ctx, js_level_height); duk_put_global_string(ctx, "Height"); /* can generate multiple maps */ for (int i = 0; i < generate_num; ++i) { if (duk_peval_file(ctx, argv[1]) != 0) { printf("('%s' #%d) %s\n", argv[1], i, duk_safe_to_string(ctx, -1)); } duk_pop(ctx); /* export the level with a special name */ char export_file_num[256]; if (generate_num > 1) { sprintf(export_file_num, "%s_%d.png", export_file, i); } else { sprintf(export_file_num, "%s.png", export_file); } /* export the level as png */ if (!save_png_disabled) export_level(export_file_num); } /* free all memory */ free(js_level); free(blocks); } else { /* tell the user how to use this */ print_usage(argv[0]); } /* clean up */ duk_destroy_heap(ctx); return 0; }
int main(int argc, char *argv[]) { duk_context *ctx = NULL; int retval = 0; int have_files = 0; int have_eval = 0; int interactive = 0; int memlimit_high = 1; int alloc_provider = ALLOC_DEFAULT; int ajsheap_log = 0; int debugger = 0; const char *compile_filename = NULL; int i; #ifdef DUK_CMDLINE_AJSHEAP alloc_provider = ALLOC_AJSHEAP; #endif (void) ajsheap_log; /* * Signal handling setup */ #ifndef NO_SIGNAL set_sigint_handler(); /* This is useful at the global level; libraries should avoid SIGPIPE though */ /*signal(SIGPIPE, SIG_IGN);*/ #endif /* * Parse options */ for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!arg) { goto usage; } if (strcmp(arg, "--restrict-memory") == 0) { memlimit_high = 0; } else if (strcmp(arg, "-i") == 0) { interactive = 1; } else if (strcmp(arg, "-c") == 0) { if (i == argc - 1) { goto usage; } i++; compile_filename = argv[i]; } else if (strcmp(arg, "-e") == 0) { have_eval = 1; if (i == argc - 1) { goto usage; } i++; /* skip code */ } else if (strcmp(arg, "--alloc-default") == 0) { alloc_provider = ALLOC_DEFAULT; } else if (strcmp(arg, "--alloc-logging") == 0) { alloc_provider = ALLOC_LOGGING; } else if (strcmp(arg, "--alloc-torture") == 0) { alloc_provider = ALLOC_TORTURE; } else if (strcmp(arg, "--alloc-hybrid") == 0) { alloc_provider = ALLOC_HYBRID; } else if (strcmp(arg, "--alloc-ajsheap") == 0) { alloc_provider = ALLOC_AJSHEAP; } else if (strcmp(arg, "--ajsheap-log") == 0) { ajsheap_log = 1; } else if (strcmp(arg, "--debugger") == 0) { debugger = 1; } else if (strlen(arg) >= 1 && arg[0] == '-') { goto usage; } else { have_files = 1; } } if (!have_files && !have_eval) { interactive = 1; } /* * Memory limit */ #ifndef NO_RLIMIT set_resource_limits(memlimit_high ? MEM_LIMIT_HIGH : MEM_LIMIT_NORMAL); #else if (memlimit_high == 0) { fprintf(stderr, "Warning: option --restrict-memory ignored, no rlimit support\n"); fflush(stderr); } #endif /* * Create context */ ctx = NULL; if (!ctx && alloc_provider == ALLOC_LOGGING) { #ifdef DUK_CMDLINE_ALLOC_LOGGING ctx = duk_create_heap(duk_alloc_logging, duk_realloc_logging, duk_free_logging, (void *) 0xdeadbeef, NULL); #else fprintf(stderr, "Warning: option --alloc-logging ignored, no logging allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_TORTURE) { #ifdef DUK_CMDLINE_ALLOC_TORTURE ctx = duk_create_heap(duk_alloc_torture, duk_realloc_torture, duk_free_torture, (void *) 0xdeadbeef, NULL); #else fprintf(stderr, "Warning: option --alloc-torture ignored, no torture allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_HYBRID) { #ifdef DUK_CMDLINE_ALLOC_HYBRID void *udata = duk_alloc_hybrid_init(); if (!udata) { fprintf(stderr, "Failed to init hybrid allocator\n"); fflush(stderr); } else { ctx = duk_create_heap(duk_alloc_hybrid, duk_realloc_hybrid, duk_free_hybrid, udata, NULL); } #else fprintf(stderr, "Warning: option --alloc-hybrid ignored, no hybrid allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_AJSHEAP) { #ifdef DUK_CMDLINE_AJSHEAP ajsheap_init(); ctx = duk_create_heap( ajsheap_log ? ajsheap_alloc_wrapped : AJS_Alloc, ajsheap_log ? ajsheap_realloc_wrapped : AJS_Realloc, ajsheap_log ? ajsheap_free_wrapped : AJS_Free, (void *) 0xdeadbeef, /* heap_udata: ignored by AjsHeap, use as marker */ NULL ); /* fatal_handler */ #else fprintf(stderr, "Warning: option --alloc-ajsheap ignored, no ajsheap allocator support\n"); fflush(stderr); #endif } if (!ctx && alloc_provider == ALLOC_DEFAULT) { ctx = duk_create_heap_default(); } if (!ctx) { fprintf(stderr, "Failed to create Duktape heap\n"); fflush(stderr); exit(-1); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump after heap creation\n"); ajsheap_dump(); } #endif #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { ajsheap_register(ctx); } #endif if (debugger) { #ifdef DUK_CMDLINE_DEBUGGER_SUPPORT fprintf(stderr, "Debugger enabled, create socket and wait for connection\n"); fflush(stderr); duk_trans_socket_init(); duk_trans_socket_waitconn(); fprintf(stderr, "Debugger connected, call duk_debugger_attach() and then execute requested file(s)/eval\n"); fflush(stderr); duk_debugger_attach(ctx, duk_trans_socket_read_cb, duk_trans_socket_write_cb, duk_trans_socket_peek_cb, duk_trans_socket_read_flush_cb, duk_trans_socket_write_flush_cb, debugger_detached, (void *) 0xbeef1234); #else fprintf(stderr, "Warning: option --debugger ignored, no debugger support\n"); fflush(stderr); #endif } #if 0 /* Manual test for duk_debugger_cooperate() */ { for (i = 0; i < 60; i++) { printf("cooperate: %d\n", i); usleep(1000000); duk_debugger_cooperate(ctx); } } #endif /* * Execute any argument file(s) */ for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!arg) { continue; } else if (strlen(arg) == 2 && strcmp(arg, "-e") == 0) { /* Here we know the eval arg exists but check anyway */ if (i == argc - 1) { retval = 1; goto cleanup; } if (handle_eval(ctx, argv[i + 1]) != 0) { retval = 1; goto cleanup; } i++; /* skip code */ continue; } else if (strlen(arg) == 2 && strcmp(arg, "-c") == 0) { i++; /* skip filename */ continue; } else if (strlen(arg) >= 1 && arg[0] == '-') { continue; } if (handle_file(ctx, arg, compile_filename) != 0) { retval = 1; goto cleanup; } } /* * Enter interactive mode if options indicate it */ if (interactive) { if (handle_interactive(ctx) != 0) { retval = 1; goto cleanup; } } /* * Cleanup and exit */ cleanup: if (interactive) { fprintf(stderr, "Cleaning up...\n"); fflush(stderr); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump before duk_destroy_heap(), before forced gc\n"); ajsheap_dump(); duk_gc(ctx, 0); fprintf(stdout, "Pool dump before duk_destroy_heap(), after forced gc\n"); ajsheap_dump(); } #endif if (ctx) { duk_destroy_heap(ctx); } #ifdef DUK_CMDLINE_AJSHEAP if (alloc_provider == ALLOC_AJSHEAP) { fprintf(stdout, "Pool dump after duk_destroy_heap() (should have zero allocs)\n"); ajsheap_dump(); } #endif return retval; /* * Usage */ usage: fprintf(stderr, "Usage: duk [options] [<filenames>]\n" "\n" " -i enter interactive mode after executing argument file(s) / eval code\n" " -e CODE evaluate code\n" " -c FILE compile into bytecode (use with only one file argument)\n" " --restrict-memory use lower memory limit (used by test runner)\n" " --alloc-default use Duktape default allocator\n" #ifdef DUK_CMDLINE_ALLOC_LOGGING " --alloc-logging use logging allocator (writes to /tmp)\n" #endif #ifdef DUK_CMDLINE_ALLOC_TORTURE " --alloc-torture use torture allocator\n" #endif #ifdef DUK_CMDLINE_ALLOC_HYBRID " --alloc-hybrid use hybrid allocator\n" #endif #ifdef DUK_CMDLINE_AJSHEAP " --alloc-ajsheap use ajsheap allocator (enabled by default with 'ajduk')\n" " --ajsheap-log write alloc log to /tmp/ajduk-alloc-log.txt\n" #endif #ifdef DUK_CMDLINE_DEBUGGER_SUPPORT " --debugger start example debugger\n" #endif "\n" "If <filename> is omitted, interactive mode is started automatically.\n"); fflush(stderr); exit(1); }
//void duk_gc(duk_context *ctx, duk_uint_t flags); void aperl_duk_gc(duk_context *ctx, duk_uint_t flags) { duk_gc(ctx, flags); }
static duk_ret_t test_recursive_finalizer(duk_context *ctx, void *udata) { (void) udata; /* Object to be finalized */ duk_push_object(ctx); duk_push_int(ctx, 123); duk_put_prop_string(ctx, -2, "foo"); /* Ecmascript finalizer */ duk_eval_string(ctx, "(function (obj) { print('finalizing obj, obj.foo:', obj.foo); })"); duk_push_int(ctx, 321); duk_put_prop_string(ctx, -2, "bar"); /* [ target finalizer ] */ /* Break the function <-> prototype reference loop so that the * Ecmascript finalizer is not in a reference loop and gets * collected by refcounting. * * (Note that 'prototype' is not configurable so we can't * delete it.) */ duk_push_string(ctx, "dummy"); duk_put_prop_string(ctx, -2, "prototype"); /* Add a Duktape/C finalizer for the Ecmascript finalizer to * exercise both Duktape/C finalizers and recursive finalization */ duk_push_c_function(ctx, c_finalizer, 1); duk_push_int(ctx, 234); duk_put_prop_string(ctx, -2, "quux"); duk_set_finalizer(ctx, -2); /* [ target(foo:123) finalizer(bar:321) ] */ /* Set Ecmascript finalizer to original object */ duk_set_finalizer(ctx, -2); /* [ target(foo:123) ] */ printf("top: %ld\n", (long) duk_get_top(ctx)); /* Read back the finalizer and the finalizer's finalizer */ duk_get_finalizer(ctx, -1); /* target's finalizer */ duk_get_finalizer(ctx, -1); /* finalizer's finalizer */ /* [ target(foo:123) finalizer(bar:321) c_finalizer(quux:234) ] */ duk_get_prop_string(ctx, -2, "bar"); printf("finalizer.bar=%s\n", duk_safe_to_string(ctx, -1)); duk_pop(ctx); duk_get_prop_string(ctx, -1, "quux"); printf("c_finalizer.quux=%s\n", duk_safe_to_string(ctx, -1)); duk_pop(ctx); /* [ target(foo:123) finalizer(bar:321) c_finalizer(quux:234) ] */ printf("before set top 0\n"); duk_set_top(ctx, 0); /* causes finalization */ printf("after set top 0\n"); /* [ ] */ /* Explicit GC (just in case e.g. a reference loop prevented collection) */ printf("before explicit gc\n"); duk_gc(ctx, 0); printf("after explicit gc\n"); return 0; }