static T* get_pointer(duk_context* ctx) { duk_push_this(ctx); T* res = 0; if (duk_get_prop_string(ctx, -1, get_field_name<T>().c_str())) { res = static_cast<T*>(duk_get_pointer(ctx, -1)); duk_pop(ctx); } duk_pop(ctx); return res; }
gpointer _gum_duk_load_module_data (duk_context * ctx, const gchar * module_id) { gpointer result; guint8 key[32]; key[0] = 0xff; g_strlcpy ((gchar *) (key + 1), module_id, sizeof (key) - 1); duk_get_global_string (ctx, (const gchar *) key); result = duk_get_pointer (ctx, -1); duk_pop (ctx); return result; }
static duk_ret_t js_Font_drawText(duk_context* ctx) { int x = duk_require_int(ctx, 0); int y = duk_require_int(ctx, 1); const char* text = duk_to_string(ctx, 2); font_t* font; color_t mask; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); font = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_get_prop_string(ctx, -1, "\xFF" "color_mask"); mask = duk_require_sphere_color(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (!is_skipped_frame()) draw_text(font, mask, x, y, TEXT_ALIGN_LEFT, text); return 0; }
/* * [0] - dukf_ssl_context_t - pointer * [1] - data to read - buffer */ static duk_ret_t js_ssl_read(duk_context *ctx) { char errortext[256]; LOGD(">> js_ssl_read"); dukf_ssl_context_t *dukf_ssl_context = duk_get_pointer(ctx, -2); size_t len; uint8_t *buf = duk_get_buffer_data(ctx, -1, &len); int rc = mbedtls_ssl_read(&dukf_ssl_context->ssl, buf, len); if (rc < 0) { mbedtls_strerror(rc, errortext, sizeof(errortext)); LOGE("error from mbedtls_ssl_read: %d - %x - %s", rc, rc, errortext); duk_push_nan(ctx); } else { duk_push_int(ctx, rc); } LOGD("<< js_ssl_read: rc=%d", rc); return 1; } // js_ssl_read
font_t* duk_require_sphere_font(duk_context* ctx, duk_idx_t index) { font_t* font; const char* type; index = duk_require_normalize_index(ctx, index); duk_require_object_coercible(ctx, index); if (!duk_get_prop_string(ctx, index, "\xFF" "sphere_type")) goto on_error; type = duk_get_string(ctx, -1); duk_pop(ctx); if (strcmp(type, "font") != 0) goto on_error; duk_get_prop_string(ctx, index, "\xFF" "ptr"); font = duk_get_pointer(ctx, -1); duk_pop(ctx); return font; on_error: duk_error_ni(ctx, -1, DUK_ERR_TYPE_ERROR, "Object is not a Sphere font"); }
bytearray_t* duk_require_sphere_bytearray(duk_context* ctx, duk_idx_t index) { bytearray_t* array; const char* type; index = duk_require_normalize_index(ctx, index); duk_require_object_coercible(ctx, index); if (!duk_get_prop_string(ctx, index, "\xFF" "sphere_type")) goto on_error; type = duk_get_string(ctx, -1); duk_pop(ctx); if (strcmp(type, "bytearray") != 0) goto on_error; duk_get_prop_string(ctx, index, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx); return array; on_error: duk_error_ni(ctx, -1, DUK_ERR_TYPE_ERROR, "Not a Sphere byte array"); }
/*----------------------------------------------------------------------------*/ static duk_ret_t kbjs_PinId_str(duk_context *context) { /* STACK: [this] */ duk_push_this(context); /* STACK: [this, void*] */ duk_get_prop_string(context, (duk_idx_t)-1, KBJS_INSTANCE_PTR); kbjs_PinId *pin_id = duk_get_pointer(context, (duk_idx_t)-1); /* STACK: [] */ duk_pop_2(context); /* STACK: ["PIN*"] */ duk_push_string(context, pin_id->str); /* Return string */ return (duk_ret_t)1; }
static duk_ret_t js_ByteArray_concat(duk_context* ctx) { bytearray_t* array2 = duk_require_sphere_bytearray(ctx, 0); bytearray_t* array; bytearray_t* new_array; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (array->size + array2->size > INT_MAX) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "ByteArray:concat(): Unable to concatenate, final size would exceed 2 GB (size1: %u, size2: %u)", array->size, array2->size); if (!(new_array = concat_bytearrays(array, array2))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "ByteArray:concat(): Failed to create concatenated byte array (internal error)"); duk_push_sphere_bytearray(ctx, new_array); return 1; }
static duk_int_t myload_code(duk_context *ctx, const char *code) { static int reqid = 0; char buf[1024]; uv_fs_t open_req1; sprintf(buf, "file%d.js", reqid++); unlink(buf); duk_push_thread_stash(ctx, ctx); duk_bool_t rc = duk_get_prop_string(ctx, -1, "__duk_loop"); assert(rc); uv_loop_t *dukLoop = (uv_loop_t *)duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); int r; r = uv_fs_open(dukLoop, &open_req1, buf, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR, NULL); assert(r >= 0); assert(open_req1.result >= 0); uv_fs_req_cleanup(&open_req1); uv_buf_t iov = uv_buf_init((char *)code, strlen(code)); uv_fs_t write_req; r = uv_fs_write(NULL, &write_req, open_req1.result, &iov, 1, -1, NULL); assert(r >= 0); assert(write_req.result >= 0); uv_fs_req_cleanup(&write_req); uv_fs_t close_req; uv_fs_close(dukLoop, &close_req, open_req1.result, NULL); duk_push_c_function(ctx, duv_main, 1); duk_push_string(ctx, buf); if (duk_pcall(ctx, 1)) { duv_dump_error(ctx, -1); //uv_loop_close(dukLoop); duk_destroy_heap(ctx); return 1; } return 0; }
static duk_ret_t js_Sound_play(duk_context* ctx) { int n_args = duk_get_top(ctx); bool is_looping; sound_t* sound; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); sound = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (n_args >= 1) { reload_sound(sound); is_looping = duk_require_boolean(ctx, 0); set_sound_looping(sound, is_looping); } play_sound(sound); return 0; }
void test(duk_context *ctx) { int i, n; duk_push_undefined(ctx); duk_push_null(ctx); duk_push_true(ctx); duk_push_false(ctx); duk_push_string(ctx, "foo"); duk_push_int(ctx, 123); duk_push_object(ctx); duk_push_pointer(ctx, (void *) NULL); duk_push_pointer(ctx, (void *) 0xdeadbeef); n = duk_get_top(ctx); printf("top: %d\n", n); for (i = 0; i < n; i++) { void *ptr = duk_get_pointer(ctx, i); printf("index %d, pointer %p\n", i, ptr); } }
comoQueue *como_duk_val_to_queue (duk_context *ctx, duk_idx_t index) { const char *string = NULL; comoQueue *queue = malloc(sizeof(*queue)); memset(queue, 0, sizeof(*queue)); QUEUE_INIT(&queue->queue); queue->type = duk_get_type(ctx, index); queue->data = NULL; switch(queue->type){ case DUK_TYPE_UNDEFINED : case DUK_TYPE_NULL : break; case DUK_TYPE_NUMBER : COMO_FATAL_ERROR("NUMBERS SUPPORT IN PROGRESS"); break; case DUK_TYPE_OBJECT : string = duk_json_encode(ctx, index); break; case DUK_TYPE_POINTER : queue->data = duk_get_pointer(ctx, index); break; case DUK_TYPE_STRING : string = duk_get_string(ctx, index); break; } if (string != NULL){ queue->data = malloc( strlen(string) + 1); strcpy(queue->data, (char *)string); } return queue; }
static duk_ret_t js_ByteArray_getProp(duk_context* ctx) { bytearray_t* array; int index; int size; duk_get_prop_string(ctx, 0, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx); if (duk_is_number(ctx, 1)) { index = duk_to_int(ctx, 1); size = get_bytearray_size(array); if (index < 0 || index >= size) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "ByteArray[]: Index is out of bounds (%i - size: %i)", index, size); duk_push_uint(ctx, get_byte(array, index)); return 1; } else { duk_dup(ctx, 1); duk_get_prop(ctx, 0); return 1; } }
static duk_ret_t dukky_html_embed_element_getSVGDocument(duk_context *ctx) { /* ensure the parameters are present */ duk_idx_t dukky_argc = duk_get_top(ctx); if (dukky_argc > 0) { /* remove extraneous parameters */ duk_set_top(ctx, 0); } /* check types of passed arguments are correct */ /* Get private data for method */ html_embed_element_private_t *priv = NULL; duk_push_this(ctx); duk_get_prop_string(ctx, -1, dukky_magic_string_private); priv = duk_get_pointer(ctx, -1); duk_pop_2(ctx); if (priv == NULL) { return 0; /* can do? No can do. */ } return 0; }
static duk_ret_t js_ByteArray_slice(duk_context* ctx) { int n_args = duk_get_top(ctx); int start = duk_require_int(ctx, 0); int end = (n_args >= 2) ? duk_require_int(ctx, 1) : INT_MAX; bytearray_t* array; int end_norm; bytearray_t* new_array; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); array = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); end_norm = fmin(end >= 0 ? end : array->size + end, array->size); if (end_norm < start || end_norm > array->size) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "ByteArray:slice(): Start and/or end values out of bounds (start: %i, end: %i - size: %i)", start, end_norm, array->size); if (!(new_array = slice_bytearray(array, start, end_norm - start))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "ByteArray:slice(): Failed to create sliced byte array (internal error)"); duk_push_sphere_bytearray(ctx, new_array); return 1; }
static duk_ret_t js_Font_drawTextBox(duk_context* ctx) { int x = duk_require_int(ctx, 0); int y = duk_require_int(ctx, 1); int w = duk_require_int(ctx, 2); int h = duk_require_int(ctx, 3); int offset = duk_require_int(ctx, 4); const char* text = duk_to_string(ctx, 5); font_t* font; int line_height; const char* line_text; color_t mask; int num_lines; int i; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); font = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_get_prop_string(ctx, -1, "\xFF" "color_mask"); mask = duk_require_sphere_color(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (!is_skipped_frame()) { duk_push_c_function(ctx, js_Font_wordWrapString, DUK_VARARGS); duk_push_this(ctx); duk_push_string(ctx, text); duk_push_int(ctx, w); duk_call_method(ctx, 2); duk_get_prop_string(ctx, -1, "length"); num_lines = duk_get_int(ctx, -1); duk_pop(ctx); line_height = get_font_line_height(font); for (i = 0; i < num_lines; ++i) { duk_get_prop_index(ctx, -1, i); line_text = duk_get_string(ctx, -1); duk_pop(ctx); draw_text(font, mask, x + offset, y, TEXT_ALIGN_LEFT, line_text); y += line_height; } duk_pop(ctx); } return 0; }
static duk_ret_t js_Font_getStringHeight(duk_context* ctx) { const char* text = duk_to_string(ctx, 0); int width = duk_require_int(ctx, 1); font_t* font; int num_lines; duk_push_this(ctx); duk_get_prop_string(ctx, -1, "\xFF" "ptr"); font = duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); duk_push_c_function(ctx, js_Font_wordWrapString, DUK_VARARGS); duk_push_this(ctx); duk_push_string(ctx, text); duk_push_int(ctx, width); duk_call_method(ctx, 2); duk_get_prop_string(ctx, -1, "length"); num_lines = duk_get_int(ctx, -1); duk_pop(ctx); duk_pop(ctx); duk_push_int(ctx, get_font_line_height(font) * num_lines); return 1; }
JavaScriptObject::~JavaScriptObject() { if (!m_instance) { // Instance has already been cleaned up. return; } // The instance still exists - detach from it. duk_push_global_object(m_context); duk_push_heapptr(m_context, m_instance); // Remove this pointer from the JS object's property. if (duk_get_prop_string(m_context, -1, WRAPPER_THIS_PROP_NAME)) { const duk_size_t length = duk_get_length(m_context, -1); for (duk_uarridx_t i = 0; i < length; ++i) { duk_get_prop_index(m_context, -1, i); const void* ptr = duk_get_pointer(m_context, -1); duk_pop(m_context); if (this == ptr) { // Remove this object from the array. duk_del_prop_index(m_context, -1, i); break; } } } // Pop the array (or undefined if there was none). duk_pop(m_context); if (m_nextFinalizer) { // Reset to the object's previous finalizer. duk_push_c_function(m_context, m_nextFinalizer, 1); duk_set_finalizer(m_context, -2); } // Pop the instance & global object. duk_pop_2(m_context); }
static duk_ret_t dukky_html_table_cell_element_bgColor_setter(duk_context *ctx) { /* Get private data for method */ html_table_cell_element_private_t *priv = NULL; duk_push_this(ctx); duk_get_prop_string(ctx, -1, dukky_magic_string_private); priv = duk_get_pointer(ctx, -1); duk_pop_2(ctx); if (priv == NULL) { return 0; /* can do? No can do. */ } #line 20 "HTMLTableCellElement.bnd" dom_exception exc; dom_string *str; duk_size_t slen; const char *s; if (duk_is_null(ctx, 0)) { s = ""; slen = 0; } else { s = duk_safe_to_lstring(ctx, 0, &slen); } exc = dom_string_create((const uint8_t *)s, slen, &str); if (exc != DOM_NO_ERR) { return 0; } exc = dom_html_table_cell_element_set_bg_color((struct dom_html_table_cell_element *)((node_private_t*)priv)->node, str); dom_string_unref(str); if (exc != DOM_NO_ERR) { return 0; } return 0; }
void queueJavaExceptionForDuktapeError(JNIEnv *env, duk_context *ctx) { jclass exceptionClass = env->FindClass("com/squareup/duktape/DuktapeException"); // If it's a Duktape error object, try to pull out the full stacktrace. if (duk_is_error(ctx, -1) && duk_has_prop_string(ctx, -1, "stack")) { duk_get_prop_string(ctx, -1, "stack"); const char* stack = duk_safe_to_string(ctx, -1); // Is there an exception thrown from a Java method? if (duk_has_prop_string(ctx, -2, JAVA_EXCEPTION_PROP_NAME)) { duk_get_prop_string(ctx, -2, JAVA_EXCEPTION_PROP_NAME); jthrowable ex = static_cast<jthrowable>(duk_get_pointer(ctx, -1)); // add the Duktape JavaScript stack to this exception. const jmethodID addDuktapeStack = env->GetStaticMethodID(exceptionClass, "addDuktapeStack", "(Ljava/lang/Throwable;Ljava/lang/String;)V"); env->CallStaticVoidMethod(exceptionClass, addDuktapeStack, ex, env->NewStringUTF(stack)); // Rethrow the Java exception. env->Throw(ex); // Pop the Java throwable. duk_pop(ctx); } else { env->ThrowNew(exceptionClass, stack); } // Pop the stack text. duk_pop(ctx); } else { // Not an error or no stacktrace, just convert to a string. env->ThrowNew(exceptionClass, duk_safe_to_string(ctx, -1)); } duk_pop(ctx); }
void* Context::getPointer(index_t i) { return duk_get_pointer(m_handle, i); }
//void *duk_get_pointer(duk_context *ctx, duk_idx_t index); void *aperl_duk_get_pointer(duk_context *ctx, duk_idx_t index) { void *ret = duk_get_pointer(ctx, index); return ret; }
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; }
// Sync readfile using libuv APIs as an API function. static duk_ret_t duv_loadfile(duk_context *ctx) { const char* path = duk_require_string(ctx, 0); uv_fs_t req; int fd = 0; uint64_t size; char* chunk; uv_buf_t buf; duk_push_thread_stash(ctx, ctx); duk_bool_t rc = duk_get_prop_string(ctx, -1, "__duk_loop"); assert(rc); uv_loop_t *dukLoop = (uv_loop_t *)duk_get_pointer(ctx, -1); duk_pop(ctx); duk_pop(ctx); if (uv_fs_open(dukLoop, &req, path, O_RDONLY, 0644, NULL) < 0) goto fail; uv_fs_req_cleanup(&req); fd = req.result; if (uv_fs_fstat(dukLoop, &req, fd, NULL) < 0) goto fail; uv_fs_req_cleanup(&req); size = req.statbuf.st_size; chunk = (char*)duk_alloc(ctx, static_cast<duk_size_t>(size)); buf = uv_buf_init(chunk, static_cast<unsigned int>(size)); if (uv_fs_read(dukLoop, &req, fd, &buf, 1, 0, NULL) < 0) { duk_free(ctx, chunk); goto fail; } uv_fs_req_cleanup(&req); duk_push_lstring(ctx, chunk, static_cast<duk_size_t>(size)); duk_free(ctx, chunk); uv_fs_close(dukLoop, &req, fd, NULL); uv_fs_req_cleanup(&req); return 1; fail: uv_fs_req_cleanup(&req); if (fd) uv_fs_close(dukLoop, &req, fd, NULL); uv_fs_req_cleanup(&req); // TODO hacking in a fallback to look for modules in a directory called duk_modules // TODO what about windows... will this work? fd = 0; std::string path2 = "duk_modules/"; path2 += path; if (uv_fs_open(dukLoop, &req, path2.c_str(), O_RDONLY, 0644, NULL) < 0) goto fail; uv_fs_req_cleanup(&req); fd = req.result; if (uv_fs_fstat(dukLoop, &req, fd, NULL) < 0) goto fail; uv_fs_req_cleanup(&req); size = req.statbuf.st_size; chunk = (char*)duk_alloc(ctx, static_cast<duk_size_t>(size)); buf = uv_buf_init(chunk, static_cast<unsigned int>(size)); if (uv_fs_read(dukLoop, &req, fd, &buf, 1, 0, NULL) < 0) { duk_free(ctx, chunk); goto fail2; } uv_fs_req_cleanup(&req); duk_push_lstring(ctx, chunk, static_cast<duk_size_t>(size)); duk_free(ctx, chunk); uv_fs_close(dukLoop, &req, fd, NULL); uv_fs_req_cleanup(&req); return 1; fail2: uv_fs_req_cleanup(&req); if (fd) uv_fs_close(dukLoop, &req, fd, NULL); uv_fs_req_cleanup(&req); duk_error(ctx, DUK_ERR_ERROR, "%s: %s: %s", uv_err_name(req.result), uv_strerror(req.result), path2.c_str()); }