/* * High precission time. If an optional array is given * it returns the difference. */ static duk_ret_t time_hrtime(duk_context* ctx) { uint64_t t = sjs_time_hrtime(); uint64_t sec; uint64_t nsec; if (duk_is_array(ctx, 0)) { uint64_t t0; uint64_t sec0; uint64_t nsec0; duk_get_prop_index(ctx, 0, 0); sec0 = duk_require_uint(ctx, -1); duk_pop(ctx); duk_get_prop_index(ctx, 0, 1); nsec0 = duk_require_uint(ctx, -1); duk_pop(ctx); t0 = sec0 * NANOS_PER_SEC + nsec0; t -= t0; } sec = t / NANOS_PER_SEC; nsec = t % NANOS_PER_SEC; duk_push_array(ctx); duk_push_uint(ctx, sec); duk_put_prop_index(ctx, -2, 0); duk_push_uint(ctx, nsec); duk_put_prop_index(ctx, -2, 1); return 1; }
static int NativeI2cTransfer(duk_context* ctx) { AJ_Status status; uint8_t addr = duk_require_int(ctx, 0); uint8_t* txBuf = NULL; uint8_t* rxBuf = NULL; duk_size_t txLen = 0; duk_size_t rxLen = 0; uint8_t rxBytes = 0; if (duk_is_undefined(ctx, 2)) { duk_push_undefined(ctx); } else { rxLen = duk_require_uint(ctx, 2); rxBuf = duk_push_dynamic_buffer(ctx, rxLen); } if (duk_is_null(ctx, 1)) { duk_push_undefined(ctx); } else { txBuf = SerializeToBuffer(ctx, 1, &txLen); } status = AJS_TargetIO_I2cTransfer(PinCtxPtr(ctx), addr, txBuf, txLen, rxBuf, rxLen, &rxBytes); if (status != AJ_OK) { duk_error(ctx, DUK_ERR_INTERNAL_ERROR, "I2C transfer failed %s\n", AJ_StatusText(status)); } duk_pop(ctx); if (rxLen) { duk_resize_buffer(ctx, -1, rxBytes); } return 1; }
static int sjs__compile_execute(duk_context *ctx) { const char *code; const char* filename; duk_size_t len; bool use_strict; int flags; /* [ ... use_strict code len filename ] */ use_strict = duk_require_boolean(ctx, -4); code = duk_require_pointer(ctx, -3); len = duk_require_uint(ctx, -2); filename = duk_require_string(ctx, -1); flags = 0; if (use_strict) { flags |= DUK_COMPILE_STRICT; } /* remove shebang if present */ if (strncmp(code, "#!", 2) == 0) { memcpy((void*) code, "//", 2); } duk_compile_lstring_filename(ctx, flags, code, len); /* [ ... use_strict code len function ] */ duk_push_global_object(ctx); /* 'this' binding */ duk_push_string(ctx, filename); duk_put_prop_string(ctx, -2, "__file__"); duk_call_method(ctx, 0); return 1; /* either the result or error are on the stack top */ }
static duk_ret_t raw_require_heapptr(duk_context *ctx) { duk_idx_t i; void *ptr; i = duk_require_uint(ctx, -1); duk_pop(ctx); ptr = duk_require_heapptr(ctx, i); printf("idx %ld: type %ld, duk_require_heapptr() -> %s\n", (long) i, (long) duk_get_type(ctx, i), (ptr ? "non-NULL" : "NULL")); return 0; }
/* * Serializes various data types into a buffer. Leaves the buffer on the top of the stack. */ static uint8_t* SerializeToBuffer(duk_context* ctx, duk_idx_t idx, duk_size_t* sz) { uint8_t* ptr; switch (duk_get_type(ctx, idx)) { case DUK_TYPE_BUFFER: duk_dup(ctx, idx); ptr = duk_get_buffer(ctx, -1, sz); break; case DUK_TYPE_BOOLEAN: ptr = duk_push_fixed_buffer(ctx, 1); ptr[0] = duk_get_boolean(ctx, idx); *sz = 1; break; case DUK_TYPE_NUMBER: ptr = duk_push_fixed_buffer(ctx, 1); ptr[0] = duk_get_int(ctx, idx); *sz = 1; break; case DUK_TYPE_STRING: duk_dup(ctx, idx); ptr = duk_to_fixed_buffer(ctx, -1, sz); break; case DUK_TYPE_OBJECT: if (duk_is_array(ctx, idx)) { duk_idx_t i; duk_idx_t len = duk_get_length(ctx, idx); ptr = duk_push_fixed_buffer(ctx, len); for (i = 0; i < len; ++i) { duk_get_prop_index(ctx, idx, i); ptr[i] = duk_require_uint(ctx, -1); duk_pop(ctx); } *sz = len; } else { duk_error(ctx, DUK_ERR_TYPE_ERROR, "Can only serialize arrays of numbers"); } break; default: duk_error(ctx, DUK_ERR_TYPE_ERROR, "Cannot serialize"); break; } return ptr; }
static int sjs__compile_execute(duk_context *ctx) { const char *code; duk_size_t len; /* [ ... code len filename ] */ code = duk_require_pointer(ctx, -3); len = duk_require_uint(ctx, -2); duk_compile_lstring_filename(ctx, DUK_COMPILE_STRICT, code, len); /* [ ... code len function ] */ duk_push_global_object(ctx); /* 'this' binding */ duk_call_method(ctx, 0); return 1; /* either the result or error are on the stack top */ }
static duk_ret_t js_RawFile_readString(duk_context* ctx) { size_t num_bytes = duk_require_uint(ctx, 0); FILE* file; void* read_buffer; duk_push_this(ctx); file = duk_require_sphere_obj(ctx, -1, "RawFile"); duk_pop(ctx); if (num_bytes <= 0 || num_bytes > INT_MAX) duk_error_ni(ctx, -1, DUK_ERR_RANGE_ERROR, "RawFile:read(): Read size out of range (%i)", num_bytes / 1048576); if (file == NULL) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): File has been closed"); if (!(read_buffer = malloc(num_bytes))) duk_error_ni(ctx, -1, DUK_ERR_ERROR, "RawFile:read(): Failed to allocate buffer for file read"); num_bytes = fread(read_buffer, 1, num_bytes, file); duk_push_lstring(ctx, read_buffer, num_bytes); return 1; }
static duk_ret_t js_ByteArray_setProp(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); set_byte(array, index, duk_require_uint(ctx, 2)); return 0; } else { duk_dup(ctx, 1); duk_dup(ctx, 2); duk_put_prop(ctx, 0); return 0; } }
/* * Returns non-zero if a key and/or value was enumerated, and: * * [enum] -> [key] (get_value == 0) * [enum] -> [key value] (get_value == 1) * * Returns zero without pushing anything on the stack otherwise. */ duk_bool_t duk_hobject_enumerator_next(duk_context *ctx, duk_bool_t get_value) { duk_hthread *thr = (duk_hthread *) ctx; duk_hobject *e; duk_hobject *enum_target; duk_hstring *res = NULL; duk_uint_fast32_t idx; duk_bool_t check_existence; DUK_ASSERT(ctx != NULL); /* [... enum] */ e = duk_require_hobject(ctx, -1); /* XXX use get tval ptr, more efficient */ duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_NEXT); idx = (duk_uint_fast32_t) duk_require_uint(ctx, -1); duk_pop(ctx); DUK_DDD(DUK_DDDPRINT("enumeration: index is: %ld", (long) idx)); /* Enumeration keys are checked against the enumeration target (to see * that they still exist). In the proxy enumeration case _target will * be the proxy, and checking key existence against the proxy is not * required (or sensible, as the keys may be fully virtual). */ duk_get_prop_stridx(ctx, -1, DUK_STRIDX_INT_TARGET); enum_target = duk_require_hobject(ctx, -1); DUK_ASSERT(enum_target != NULL); #if defined(DUK_USE_ES6_PROXY) check_existence = (!DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(enum_target)); #else check_existence = 1; #endif duk_pop(ctx); /* still reachable */ DUK_DDD(DUK_DDDPRINT("getting next enum value, enum_target=%!iO, enumerator=%!iT", (duk_heaphdr *) enum_target, (duk_tval *) duk_get_tval(ctx, -1))); /* no array part */ for (;;) { duk_hstring *k; if (idx >= e->e_next) { DUK_DDD(DUK_DDDPRINT("enumeration: ran out of elements")); break; } /* we know these because enum objects are internally created */ k = DUK_HOBJECT_E_GET_KEY(e, idx); DUK_ASSERT(k != NULL); DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(e, idx)); DUK_ASSERT(!DUK_TVAL_IS_UNDEFINED_UNUSED(&DUK_HOBJECT_E_GET_VALUE(e, idx).v)); idx++; /* recheck that the property still exists */ if (check_existence && !duk_hobject_hasprop_raw(thr, enum_target, k)) { DUK_DDD(DUK_DDDPRINT("property deleted during enumeration, skip")); continue; } DUK_DDD(DUK_DDDPRINT("enumeration: found element, key: %!O", (duk_heaphdr *) k)); res = k; break; } DUK_DDD(DUK_DDDPRINT("enumeration: updating next index to %ld", (long) idx)); duk_push_number(ctx, (double) idx); duk_put_prop_stridx(ctx, -2, DUK_STRIDX_INT_NEXT); /* [... enum] */ if (res) { duk_push_hstring(ctx, res); if (get_value) { duk_push_hobject(ctx, enum_target); duk_dup(ctx, -2); /* -> [... enum key enum_target key] */ duk_get_prop(ctx, -2); /* -> [... enum key enum_target val] */ duk_remove(ctx, -2); /* -> [... enum key val] */ duk_remove(ctx, -3); /* -> [... key val] */ } else { duk_remove(ctx, -2); /* -> [... key] */ } return 1; } else { duk_pop(ctx); /* -> [...] */ 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 int wrapped_compile_execute(duk_context *ctx) { const char *src_data; duk_size_t src_len; int comp_flags; /* XXX: Here it'd be nice to get some stats for the compilation result * when a suitable command line is given (e.g. code size, constant * count, function count. These are available internally but not through * the public API. */ /* Use duk_compile_lstring_filename() variant which avoids interning * the source code. This only really matters for low memory environments. */ /* [ ... src_data src_len filename ] */ comp_flags = 0; src_data = (const char *) duk_require_pointer(ctx, -3); src_len = (duk_size_t) duk_require_uint(ctx, -2); duk_compile_lstring_filename(ctx, comp_flags, src_data, src_len); /* [ ... src_data src_len function ] */ #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_start_exec_timeout(); #endif duk_push_global_object(ctx); /* 'this' binding */ duk_call_method(ctx, 0); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (interactive_mode) { /* * In interactive mode, write to stdout so output won't * interleave as easily. * * NOTE: the ToString() coercion may fail in some cases; * for instance, if you evaluate: * * ( {valueOf: function() {return {}}, * toString: function() {return {}}}); * * The error is: * * TypeError: failed to coerce with [[DefaultValue]] * duk_api.c:1420 * * These are handled now by the caller which also has stack * trace printing support. User code can print out errors * safely using duk_safe_to_string(). */ fprintf(stdout, "= %s\n", duk_to_string(ctx, -1)); fflush(stdout); } else { /* In non-interactive mode, success results are not written at all. * It is important that the result value is not string coerced, * as the string coercion may cause an error in some cases. */ } duk_pop(ctx); return 0; }
unsigned int Context::requireUInt(index_t i) { return duk_require_uint(m_handle, i); }
static int wrapped_compile_execute(duk_context *ctx) { const char *src_data; duk_size_t src_len; int comp_flags; /* XXX: Here it'd be nice to get some stats for the compilation result * when a suitable command line is given (e.g. code size, constant * count, function count. These are available internally but not through * the public API. */ /* Use duk_compile_lstring_filename() variant which avoids interning * the source code. This only really matters for low memory environments. */ /* [ ... bytecode_filename src_data src_len filename ] */ src_data = (const char *) duk_require_pointer(ctx, -3); src_len = (duk_size_t) duk_require_uint(ctx, -2); if (src_data != NULL && src_len >= 2 && src_data[0] == (char) 0xff) { /* Bytecode. */ duk_push_lstring(ctx, src_data, src_len); duk_to_buffer(ctx, -1, NULL); duk_load_function(ctx); } else { /* Source code. */ comp_flags = 0; duk_compile_lstring_filename(ctx, comp_flags, src_data, src_len); } /* [ ... bytecode_filename src_data src_len function ] */ /* Optional bytecode dump. */ if (duk_is_string(ctx, -4)) { FILE *f; void *bc_ptr; duk_size_t bc_len; size_t wrote; duk_dup_top(ctx); duk_dump_function(ctx); bc_ptr = duk_require_buffer(ctx, -1, &bc_len); f = fopen(duk_require_string(ctx, -5), "wb"); if (!f) { duk_error(ctx, DUK_ERR_ERROR, "failed to open bytecode output file"); } wrote = fwrite(bc_ptr, 1, (size_t) bc_len, f); /* XXX: handle partial writes */ (void) fclose(f); if (wrote != bc_len) { duk_error(ctx, DUK_ERR_ERROR, "failed to write all bytecode"); } return 0; /* duk_safe_call() cleans up */ } #if 0 /* Manual test for bytecode dump/load cycle: dump and load before * execution. Enable manually, then run "make qecmatest" for a * reasonably good coverage of different functions and programs. */ duk_dump_function(ctx); duk_load_function(ctx); #endif #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_start_exec_timeout(); #endif duk_push_global_object(ctx); /* 'this' binding */ duk_call_method(ctx, 0); #if defined(DUK_CMDLINE_AJSHEAP) ajsheap_clear_exec_timeout(); #endif if (interactive_mode) { /* * In interactive mode, write to stdout so output won't * interleave as easily. * * NOTE: the ToString() coercion may fail in some cases; * for instance, if you evaluate: * * ( {valueOf: function() {return {}}, * toString: function() {return {}}}); * * The error is: * * TypeError: failed to coerce with [[DefaultValue]] * duk_api.c:1420 * * These are handled now by the caller which also has stack * trace printing support. User code can print out errors * safely using duk_safe_to_string(). */ fprintf(stdout, "= %s\n", duk_to_string(ctx, -1)); fflush(stdout); } else { /* In non-interactive mode, success results are not written at all. * It is important that the result value is not string coerced, * as the string coercion may cause an error in some cases. */ } return 0; /* duk_safe_call() cleans up */ }
//duk_uint_t duk_require_uint(duk_context *ctx, duk_idx_t index); duk_uint_t aperl_duk_require_uint(duk_context *ctx, duk_idx_t index) { duk_uint_t ret = duk_require_uint(ctx, index); return ret; }