int main(int argc, char *argv[]) { (void) argc; (void) argv; duk_context *ctx = duk_create_heap_default(); duk_eval_string(ctx, "print('Hello world!');"); duk_push_global_object(ctx); duk_push_c_function(ctx, adder, DUK_VARARGS); duk_put_prop_string(ctx, -2, "adder"); duk_pop(ctx); duk_eval_string(ctx, "print('2+3=' + adder(2, 3));"); duk_pop(ctx); duk_destroy_heap(ctx); return 0; }
int main(int argc, char *argv[]) { duk_context *ctx; int i; int exitcode = 0; ctx = duk_create_heap_default(); if (!ctx) { return 1; } duk_push_c_function(ctx, handle_print, 1); duk_put_global_string(ctx, "print"); duk_push_c_function(ctx, handle_assert, 2); duk_put_global_string(ctx, "assert"); duk_push_object(ctx); duk_push_c_function(ctx, cb_resolve_module, DUK_VARARGS); duk_put_prop_string(ctx, -2, "resolve"); duk_push_c_function(ctx, cb_load_module, DUK_VARARGS); duk_put_prop_string(ctx, -2, "load"); duk_module_node_init(ctx); printf("top after init: %ld\n", (long) duk_get_top(ctx)); for (i = 1; i < argc; i++) { printf("Evaling: %s\n", argv[i]); if (duk_peval_string(ctx, argv[i]) != 0) { if (duk_get_prop_string(ctx, -1, "stack")) { duk_replace(ctx, -2); } else { duk_pop(ctx); } exitcode = 1; } printf("--> %s\n", duk_safe_to_string(ctx, -1)); duk_pop(ctx); } printf("Done\n"); duk_destroy_heap(ctx); return exitcode; }
static duk_ret_t test_use_module(duk_context *ignored_ctx, void *udata) { duk_context *ctx; (void) udata; ctx = duk_create_heap_default(); if (!ctx) { printf("Failed to create heap\n"); return 0; } duk_push_c_function(ctx, dukopen_my_module, 0); duk_call(ctx, 0); duk_put_global_string(ctx, "my_module"); duk_eval_string_noresult(ctx, "my_module.func2()"); printf("final top: %ld\n", (long) duk_get_top(ctx)); duk_destroy_heap(ctx); return 0; }
int main(int argc, const char *argv[]) { duk_context *ctx = NULL; int code = 0; ctx = duk_create_heap_default(); if (!ctx) { printf("Failed to create a Duktape heap.\n"); exit(1); } duk_push_c_function(ctx, pmain, 2); duk_push_int(ctx, argc); duk_push_pointer(ctx, argv); if (duk_pcall(ctx, 2) != DUK_EXEC_SUCCESS) { printf("%s\n", duk_safe_to_string(ctx, -1)); code = 1; } duk_destroy_heap(ctx); return code; }
//returns 0 for success, -1 for error int __stdcall DukOp(int operation, duk_context *ctx, int arg1, char* arg2){ #pragma EXPORT //these do not require a context.. switch(operation){ case opd_LastString: return mLastString; case opd_ScriptTimeout: watchdogTimeout = arg1; return 0; } if(ctx == 0) return -1; switch(operation){ case opd_PushUndef: duk_push_undefined(ctx); return 0; case opd_PushNum: duk_push_number(ctx,arg1); return 0; case opd_PushStr: duk_push_string(ctx, arg2); return 0; case opd_GetInt: return duk_to_number(ctx, arg1); case opd_IsNullUndef: return (int)duk_is_null_or_undefined(ctx, arg1); case opd_GetString: return (int)duk_safe_to_string(ctx, arg1); case opd_Destroy: duk_destroy_heap(ctx); ctx = 0; return 0; case opd_dbgCoOp: duk_debugger_cooperate(ctx); return 0; case opd_PushBool: duk_push_boolean(ctx, (arg1==0 ? 0 : 1) ); return 0; //(0==false, 1==true, vbtrue = -1 ok) case opd_debugAttach: if(arg1==1){ if(vbDbgReadHandler==0 || vbStdOut==0 || vbDbgWriteHandler==0) return -1; duk_debugger_attach(ctx, DebugRead, DebugWrite, 0, 0,0, DebugDetached,0); } else duk_debugger_detach(ctx); return 0; /*case opd_dbgCurLine: return duk_debug_curr_line(ctx); */ case opd_dbgTriggerRead: duk__debug_process_message(ctx); return 0; } return -1; }
TEST_F(FooTest,test_has_prop) { duk_smart_ptr duk_instance(duk_create_heap_default(), [](duk_context* f) { duk_destroy_heap(f); }); test_function_vector t_vector = { test_1a, test_1b, test_1c, test_1d, test_1e, test_2a, test_2b, test_2c, test_3a, test_3b, test_3c}; for (auto f : t_vector) { auto pf = *(f.target<duk_safe_call_function>()); TEST_SAFE_CALL(duk_instance.get(), pf); } }
int main(int argc, char *argv[]) { duk_context *ctx = duk_create_heap_default(); if( argc > 1 ) { FILE *fp = fopen(argv[1], "rb"); if( fp ) { fseek(fp, 0L, SEEK_END); size_t size = ftell(fp); fseek(fp, 0L, SEEK_SET); char *buffer = (char *)malloc( size + 1 ); if( buffer ) { fread( buffer, size, 1, fp ); buffer[ size ] = '\0'; duk_eval_string(ctx, buffer); } free(buffer); fclose(fp); } } else { duk_eval_string(ctx, "print('Hello world!');"); } duk_destroy_heap(ctx); return 0; }
TEST_F(FooTest, test_to_fixed_dynamic_buffer) { duk_smart_ptr duk_instance(duk_create_heap_default(), [](duk_context* f) { duk_destroy_heap(f); }); test_function_vector t_vector = { test_1a, test_1b, test_2a, test_2b, test_3a, test_3b, test_4a, test_4b, test_5a, test_5b, test_6a, test_6b, test_6c}; for (auto f : t_vector) { auto pf = *(f.target<duk_safe_call_function>()); TEST_SAFE_CALL(duk_instance.get(), pf); } }
int main(int argc, char *argv[]) { duk_context *ctx; int i; const char *res; if (argc < 2) { usage_exit(); } ctx = duk_create_heap_default(); for (i = 1; i < argc; i++) { printf("=== eval: '%s' ===\n", argv[i]); duk_push_string(ctx, argv[i]); duk_safe_call(ctx, eval_raw, 1 /*nargs*/, 1 /*nrets*/); duk_safe_call(ctx, tostring_raw, 1 /*nargs*/, 1 /*nrets*/); res = duk_get_string(ctx, -1); printf("%s\n", res ? res : "null"); duk_pop(ctx); } duk_destroy_heap(ctx); return 0; }
int main(int argc, const char *argv[]) { duk_context *ctx; int i; int mode; void *hptr1; void *hptr2; void *hptr3; void *hptr4; void *hptr5; void *hptr6; void *hptr7; void *hptr8; void *hptr9; void *hptr10; if (argc < 2) { return 1; } mode = atoi(argv[1]); ctx = duk_create_heap_default(); duk_push_object(ctx); /* Push the strings we're dealing with, get their heapptrs, and keep * them reachable throughout the test. This is necessary for the * heapptr test to work. But we also want this so that the strings * won't be freed and reallocated repeatedly during the test. That * might happen in real application code too, but this test focuses * on the differences between the intern checks only, assuming the * string itself is already in the string table. */ duk_push_string(ctx, "foo1"); hptr1 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo2"); hptr2 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo3"); hptr3 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo4"); hptr4 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo5"); hptr5 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo6"); hptr6 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo7"); hptr7 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo8"); hptr8 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo9"); hptr9 = duk_get_heapptr(ctx, -1); duk_push_string(ctx, "foo10"); hptr10 = duk_get_heapptr(ctx, -1); for (i = 0; i < 10000000; i++) { switch (mode) { case 0: duk_push_string(ctx, "foo1"); duk_push_string(ctx, "foo2"); duk_push_string(ctx, "foo3"); duk_push_string(ctx, "foo4"); duk_push_string(ctx, "foo5"); duk_push_string(ctx, "foo6"); duk_push_string(ctx, "foo7"); duk_push_string(ctx, "foo8"); duk_push_string(ctx, "foo9"); duk_push_string(ctx, "foo10"); break; case 1: duk_push_lstring(ctx, "foo1", 4); duk_push_lstring(ctx, "foo2", 4); duk_push_lstring(ctx, "foo3", 4); duk_push_lstring(ctx, "foo4", 4); duk_push_lstring(ctx, "foo5", 4); duk_push_lstring(ctx, "foo6", 4); duk_push_lstring(ctx, "foo7", 4); duk_push_lstring(ctx, "foo8", 4); duk_push_lstring(ctx, "foo9", 4); duk_push_lstring(ctx, "foo10", 5); break; case 2: duk_push_literal(ctx, "foo1"); duk_push_literal(ctx, "foo2"); duk_push_literal(ctx, "foo3"); duk_push_literal(ctx, "foo4"); duk_push_literal(ctx, "foo5"); duk_push_literal(ctx, "foo6"); duk_push_literal(ctx, "foo7"); duk_push_literal(ctx, "foo8"); duk_push_literal(ctx, "foo9"); duk_push_literal(ctx, "foo10"); break; case 3: duk_push_heapptr(ctx, hptr1); duk_push_heapptr(ctx, hptr2); duk_push_heapptr(ctx, hptr3); duk_push_heapptr(ctx, hptr4); duk_push_heapptr(ctx, hptr5); duk_push_heapptr(ctx, hptr6); duk_push_heapptr(ctx, hptr7); duk_push_heapptr(ctx, hptr8); duk_push_heapptr(ctx, hptr9); duk_push_heapptr(ctx, hptr10); break; case 10: duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo1"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo2"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo3"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo4"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo5"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo6"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo7"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo8"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo9"); duk_push_uint(ctx, 123); duk_put_prop_string(ctx, 0, "foo10"); duk_push_uint(ctx, 123); duk_get_prop_string(ctx, 0, "foo1"); duk_get_prop_string(ctx, 0, "foo2"); duk_get_prop_string(ctx, 0, "foo3"); duk_get_prop_string(ctx, 0, "foo4"); duk_get_prop_string(ctx, 0, "foo5"); duk_get_prop_string(ctx, 0, "foo6"); duk_get_prop_string(ctx, 0, "foo7"); duk_get_prop_string(ctx, 0, "foo8"); duk_get_prop_string(ctx, 0, "foo9"); duk_get_prop_string(ctx, 0, "foo10"); break; case 11: duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo1", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo2", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo3", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo4", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo5", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo6", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo7", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo8", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo9", 4); duk_push_uint(ctx, 123); duk_put_prop_lstring(ctx, 0, "foo10", 5); duk_push_uint(ctx, 123); duk_get_prop_lstring(ctx, 0, "foo1", 4); duk_get_prop_lstring(ctx, 0, "foo2", 4); duk_get_prop_lstring(ctx, 0, "foo3", 4); duk_get_prop_lstring(ctx, 0, "foo4", 4); duk_get_prop_lstring(ctx, 0, "foo5", 4); duk_get_prop_lstring(ctx, 0, "foo6", 4); duk_get_prop_lstring(ctx, 0, "foo7", 4); duk_get_prop_lstring(ctx, 0, "foo8", 4); duk_get_prop_lstring(ctx, 0, "foo9", 4); duk_get_prop_lstring(ctx, 0, "foo10", 5); break; case 12: duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo1"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo2"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo3"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo4"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo5"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo6"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo7"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo8"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo9"); duk_push_uint(ctx, 123); duk_put_prop_literal(ctx, 0, "foo10"); duk_push_uint(ctx, 123); duk_get_prop_literal(ctx, 0, "foo1"); duk_get_prop_literal(ctx, 0, "foo2"); duk_get_prop_literal(ctx, 0, "foo3"); duk_get_prop_literal(ctx, 0, "foo4"); duk_get_prop_literal(ctx, 0, "foo5"); duk_get_prop_literal(ctx, 0, "foo6"); duk_get_prop_literal(ctx, 0, "foo7"); duk_get_prop_literal(ctx, 0, "foo8"); duk_get_prop_literal(ctx, 0, "foo9"); duk_get_prop_literal(ctx, 0, "foo10"); break; case 13: duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr1); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr2); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr3); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr4); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr5); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr6); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr7); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr8); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr9); duk_push_uint(ctx, 123); duk_put_prop_heapptr(ctx, 0, hptr10); duk_push_uint(ctx, 123); duk_get_prop_heapptr(ctx, 0, hptr1); duk_get_prop_heapptr(ctx, 0, hptr2); duk_get_prop_heapptr(ctx, 0, hptr3); duk_get_prop_heapptr(ctx, 0, hptr4); duk_get_prop_heapptr(ctx, 0, hptr5); duk_get_prop_heapptr(ctx, 0, hptr6); duk_get_prop_heapptr(ctx, 0, hptr7); duk_get_prop_heapptr(ctx, 0, hptr8); duk_get_prop_heapptr(ctx, 0, hptr9); duk_get_prop_heapptr(ctx, 0, hptr10); break; case 20: duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo1"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo2"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo3"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo4"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo5"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo6"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo7"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo8"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo9"); duk_push_uint(ctx, 123); duk_put_global_string(ctx, "foo10"); duk_push_uint(ctx, 123); duk_get_global_string(ctx, "foo1"); duk_get_global_string(ctx, "foo2"); duk_get_global_string(ctx, "foo3"); duk_get_global_string(ctx, "foo4"); duk_get_global_string(ctx, "foo5"); duk_get_global_string(ctx, "foo6"); duk_get_global_string(ctx, "foo7"); duk_get_global_string(ctx, "foo8"); duk_get_global_string(ctx, "foo9"); duk_get_global_string(ctx, "foo10"); break; case 21: duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo1", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo2", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo3", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo4", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo5", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo6", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo7", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo8", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo9", 4); duk_push_uint(ctx, 123); duk_put_global_lstring(ctx, "foo10", 5); duk_push_uint(ctx, 123); duk_get_global_lstring(ctx, "foo1", 4); duk_get_global_lstring(ctx, "foo2", 4); duk_get_global_lstring(ctx, "foo3", 4); duk_get_global_lstring(ctx, "foo4", 4); duk_get_global_lstring(ctx, "foo5", 4); duk_get_global_lstring(ctx, "foo6", 4); duk_get_global_lstring(ctx, "foo7", 4); duk_get_global_lstring(ctx, "foo8", 4); duk_get_global_lstring(ctx, "foo9", 4); duk_get_global_lstring(ctx, "foo10", 5); break; case 22: duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo1"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo2"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo3"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo4"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo5"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo6"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo7"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo8"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo9"); duk_push_uint(ctx, 123); duk_put_global_literal(ctx, "foo10"); duk_push_uint(ctx, 123); duk_get_global_literal(ctx, "foo1"); duk_get_global_literal(ctx, "foo2"); duk_get_global_literal(ctx, "foo3"); duk_get_global_literal(ctx, "foo4"); duk_get_global_literal(ctx, "foo5"); duk_get_global_literal(ctx, "foo6"); duk_get_global_literal(ctx, "foo7"); duk_get_global_literal(ctx, "foo8"); duk_get_global_literal(ctx, "foo9"); duk_get_global_literal(ctx, "foo10"); break; case 23: duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr1); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr2); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr3); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr4); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr5); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr6); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr7); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr8); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr9); duk_push_uint(ctx, 123); duk_put_global_heapptr(ctx, hptr10); duk_push_uint(ctx, 123); duk_get_global_heapptr(ctx, hptr1); duk_get_global_heapptr(ctx, hptr2); duk_get_global_heapptr(ctx, hptr3); duk_get_global_heapptr(ctx, hptr4); duk_get_global_heapptr(ctx, hptr5); duk_get_global_heapptr(ctx, hptr6); duk_get_global_heapptr(ctx, hptr7); duk_get_global_heapptr(ctx, hptr8); duk_get_global_heapptr(ctx, hptr9); duk_get_global_heapptr(ctx, hptr10); break; default: return 1; } /* Keep strings and test object. */ duk_set_top(ctx, 11); } 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 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[]) { duk_context *ctx = NULL; int retval = 0; const char *filename = NULL; int i; #ifndef NO_SIGNAL set_sigint_handler(); /* This is useful at the global level; libraries should avoid SIGPIPE though */ /*signal(SIGPIPE, SIG_IGN);*/ #endif for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!arg) { goto usage; } if (strcmp(arg, "-c") == 0) { c_evloop = 1; } else if (strlen(arg) > 1 && arg[0] == '-') { goto usage; } else { if (filename) { goto usage; } filename = arg; } } if (!filename) { goto usage; } ctx = duk_create_heap_default(); poll_register(ctx); ncurses_register(ctx); socket_register(ctx); fileio_register(ctx); if (c_evloop) { fprintf(stderr, "Using C based eventloop (omit -c to use Ecmascript based eventloop)\n"); fflush(stderr); eventloop_register(ctx); duk_eval_file(ctx, "c_eventloop.js"); } else { fprintf(stderr, "Using Ecmascript based eventloop (give -c to use C based eventloop)\n"); fflush(stderr); duk_eval_file(ctx, "ecma_eventloop.js"); } fprintf(stderr, "Executing code from: '%s'\n", filename); fflush(stderr); if (strcmp(filename, "-") == 0) { if (handle_stdin(ctx) != 0) { retval = 1; goto cleanup; } } else { if (handle_file(ctx, filename) != 0) { retval = 1; goto cleanup; } } cleanup: if (ctx) { duk_destroy_heap(ctx); } return retval; usage: fprintf(stderr, "Usage: evloop [-c] <filename>\n"); fprintf(stderr, "\n"); fprintf(stderr, "Uses an Ecmascript based eventloop (ecma_eventloop.js) by default.\n"); fprintf(stderr, "If -c option given, uses a C based eventloop (c_eventloop.{c,js}).\n"); fprintf(stderr, "If <filename> is '-', the entire STDIN executed.\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; }
Engine::~Engine() { duk_destroy_heap(m_ctx.handle()); }
//void duk_destroy_heap(duk_context *ctx); void aperl_duk_destroy_heap(duk_context *ctx) { duk_destroy_heap(ctx); }
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); }
static void _D(duk_context* ctx) { duk_destroy_heap(ctx); }
void js_destroy() { duk_destroy_heap(ctx); }
StyleContext::~StyleContext() { duk_destroy_heap(m_ctx); }
int main(int argc, char *argv[]) { duk_context *ctx = duk_create_heap_default(); duk_int_t rc; (void) argc; (void) argv; /* suppress warning */ printf("*** test1 - duk_pcall()\n"); duk_push_c_function(ctx, test1, 0); rc = duk_pcall(ctx, 0); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test1 - duk_safe_call()\n"); rc = duk_safe_call(ctx, test1, 0, 1); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test1 - ecmascript try-catch\n"); duk_push_c_function(ctx, test1, 0); duk_put_global_string(ctx, "test1"); duk_eval_string_noresult(ctx, "try {\n" " test1();\n" "} catch (e) {\n" " print(e.stack || e);\n" "}\n"); printf("\n"); printf("*** test2 - duk_pcall()\n"); duk_push_c_function(ctx, test2, 0); rc = duk_pcall(ctx, 0); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test2 - duk_safe_call()\n"); rc = duk_safe_call(ctx, test2, 0, 1); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test2 - ecmascript try-catch\n"); duk_push_c_function(ctx, test2, 0); duk_put_global_string(ctx, "test2"); duk_eval_string_noresult(ctx, "try {\n" " test2();\n" "} catch (e) {\n" " print(e.stack || e);\n" "}\n"); printf("\n"); printf("*** test3 - duk_pcall()\n"); duk_push_c_function(ctx, test3, 0); rc = duk_pcall(ctx, 0); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test3 - duk_safe_call()\n"); rc = duk_safe_call(ctx, test3, 0, 1); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test3 - ecmascript try-catch\n"); duk_push_c_function(ctx, test3, 0); duk_put_global_string(ctx, "test3"); duk_eval_string_noresult(ctx, "try {\n" " test3();\n" "} catch (e) {\n" " print(e.stack || e);\n" "}\n"); printf("\n"); printf("*** test4 - duk_pcall()\n"); duk_push_c_function(ctx, test4, 0); rc = duk_pcall(ctx, 0); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test4 - duk_safe_call()\n"); rc = duk_safe_call(ctx, test4, 0, 1); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test4 - ecmascript try-catch\n"); duk_push_c_function(ctx, test4, 0); duk_put_global_string(ctx, "test4"); duk_eval_string_noresult(ctx, "try {\n" " test4();\n" "} catch (e) {\n" " print(e.stack || e);\n" "}\n"); printf("\n"); printf("*** test5 - duk_pcall()\n"); duk_push_c_function(ctx, test5, 0); rc = duk_pcall(ctx, 0); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test5 - duk_safe_call()\n"); rc = duk_safe_call(ctx, test5, 0, 1); printf("--> rc=%ld (%s)\n", (long) rc, duk_safe_to_string(ctx, -1)); duk_pop(ctx); printf("\n"); printf("*** test5 - ecmascript try-catch\n"); duk_push_c_function(ctx, test5, 0); duk_put_global_string(ctx, "test5"); duk_eval_string_noresult(ctx, "try {\n" " test5();\n" "} catch (e) {\n" " print(e.stack || e);\n" "}\n"); printf("\n"); printf("*** done\n"); duk_destroy_heap(ctx); return 0; }
int _como_worker_thread (void *data){ comoWorker *worker = data; QUEUE *q; if (worker->ctx == NULL){ char *argv[3]; int argc = 3; const char *arg = ""; const char *prefix = "--childWorker"; argv[0] = (char *)arg; argv[1] = worker->file; argv[2] = (char *)prefix; duk_context *ctx = como_create_new_heap (argc, argv, NULL); worker->ctx = ctx; como_run(ctx); } while (1){ como_sleep(1); while ( !QUEUE_EMPTY(&worker->queueIn) ){ mtx_lock(&worker->mtx); q = QUEUE_HEAD(&(worker)->queueIn); QUEUE_REMOVE(q); comoQueue *queue = QUEUE_DATA(q, comoQueue, queue); mtx_unlock(&worker->mtx); if (worker->destroy != 0){ goto FREE; } como_push_worker_value(worker->ctx, queue); duk_push_pointer(worker->ctx, worker); duk_call(worker->ctx, 2); FREE : if (queue->data != NULL && queue->type != DUK_TYPE_POINTER){ free(queue->data); } free(queue); } //call this to run event loop only duk_call(worker->ctx, 0); if (worker->destroy == 1){ worker->destroy = 2; /* pass destruction to main thread */ duk_push_global_object(worker->ctx); duk_get_prop_string(worker->ctx, -1, "process"); duk_get_prop_string(worker->ctx, -1, "_emitExit"); // dump_stack(worker->ctx, "PP"); duk_call(worker->ctx, 0); break; } } duk_destroy_heap(worker->ctx); return 0; }
DuktapeContext::~DuktapeContext() { // Delete the proxies before destroying the heap. m_jsObjects.clear(); duk_destroy_heap(m_context); }
static void ctx_dealloc(void *ptr) { struct state *state = (struct state *)ptr; duk_destroy_heap(state->ctx); free(state); }
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; }
int main(int argc, char *argv[]) { duk_context *ctx = NULL; int retval = 0; int have_files = 0; int interactive = 0; int memlimit_high = 1; int i; /* * 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, "-r") == 0) { memlimit_high = 0; } else if (strcmp(arg, "-i") == 0) { interactive = 1; } else if (strlen(arg) >= 1 && arg[0] == '-') { goto usage; } else { have_files = 1; } } if (!have_files) { interactive = 1; } /* * Memory limit */ #ifndef NO_RLIMIT set_resource_limits(memlimit_high ? MEM_LIMIT_HIGH : MEM_LIMIT_NORMAL); #else (void) memlimit_high; /* suppress warning */ #endif /* * Create context and execute any argument file(s) */ 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]; if (!arg) { 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); } if (ctx) { duk_destroy_heap(ctx); } return retval; /* * Usage */ usage: fprintf(stderr, "Usage: duk [-i] [-r] [<filenames>]\n" "\n" " -i enter interactive mode after executing argument file(s)\n" " -r use lower memory limit (used by test runner)" #ifdef NO_RLIMIT " (disabled)\n" #else "\n" #endif "\n" "If <filename> is omitted, interactive mode is started automatically.\n"); fflush(stderr); exit(1); }