size_t duk_hbuffer_insert_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, size_t offset, const char *str) { size_t len; DUK_ASSERT(thr != NULL); DUK_ASSERT(buf != NULL); DUK_ASSERT(str != NULL); DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf)); len = DUK_STRLEN(str); duk_hbuffer_insert_bytes(thr, buf, offset, (duk_uint8_t *) str, len); return len; }
DUK_EXTERNAL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { duk__compile_raw_args comp_args_alloc; duk__compile_raw_args *comp_args = &comp_args_alloc; DUK_ASSERT_CTX_VALID(ctx); if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) { /* String length is computed here to avoid multiple evaluation * of a macro argument in the calling side. */ src_length = DUK_STRLEN(src_buffer); } comp_args->src_buffer = (const duk_uint8_t *) src_buffer; comp_args->src_length = src_length; comp_args->flags = flags; /* [ ... source? filename? ] (depends on flags) */ if (flags & DUK_COMPILE_SAFE) { duk_int_t rc; duk_int_t nargs; duk_int_t nrets = 1; /* Arguments can be: [ source? filename? &comp_args] so that * nargs is 1 to 3. Call site encodes the correct nargs count * directly into flags. */ nargs = flags & 0x07; DUK_ASSERT(nargs == ((flags & DUK_COMPILE_NOSOURCE) ? 0 : 1) + ((flags & DUK_COMPILE_NOFILENAME) ? 0 : 1)); rc = duk_safe_call(ctx, duk__do_compile, (void *) comp_args, nargs, nrets); /* [ ... closure ] */ return rc; } (void) duk__do_compile(ctx, (void *) comp_args); /* [ ... closure ] */ return DUK_EXEC_SUCCESS; }
DUK_EXTERNAL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) { duk__compile_raw_args comp_args_alloc; duk__compile_raw_args *comp_args = &comp_args_alloc; if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) { /* String length is computed here to avoid multiple evaluation * of a macro argument in the calling side. */ src_length = DUK_STRLEN(src_buffer); } comp_args->src_buffer = (const duk_uint8_t *) src_buffer; comp_args->src_length = src_length; comp_args->flags = flags; duk_push_pointer(ctx, (void *) comp_args); /* [ ... source? filename &comp_args ] (depends on flags) */ if (flags & DUK_COMPILE_SAFE) { duk_int_t rc; duk_int_t nargs; duk_int_t nrets = 1; /* Arguments are either: [ filename &comp_args ] or [ source filename &comp_args ] */ nargs = (flags & DUK_COMPILE_NOSOURCE) ? 2 : 3; rc = duk_safe_call(ctx, duk__do_compile, nargs, nrets); /* [ ... closure ] */ return rc; } (void) duk__do_compile(ctx); /* [ ... closure ] */ return DUK_EXEC_SUCCESS; }
duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val) { char buf[DUK_STRTAB_U32_MAX_STRLEN+1]; DUK_SNPRINTF(buf, sizeof(buf), "%u", (unsigned int) val); buf[sizeof(buf) - 1] = (char) 0; return duk_heap_string_intern(heap, (duk_uint8_t *) buf, DUK_STRLEN(buf)); }
DUK_INTERNAL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) { duk_fb_put_bytes(fb, (const duk_uint8_t *) x, (duk_size_t) DUK_STRLEN(x)); }
/* Log frontend shared helper, magic value indicates log level. Provides * frontend functions: trace(), debug(), info(), warn(), error(), fatal(). * This needs to have small footprint, reasonable performance, minimal * memory churn, etc. */ DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) { duk_hthread *thr = (duk_hthread *) ctx; duk_double_t now; duk_small_int_t entry_lev = duk_get_current_magic(ctx); duk_small_int_t logger_lev; duk_int_t nargs; duk_int_t i; duk_size_t tot_len; const duk_uint8_t *arg_str; duk_size_t arg_len; duk_uint8_t *buf, *p; const duk_uint8_t *q; duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE]; duk_size_t date_len; duk_small_int_t rc; DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5); /* XXX: sanitize to printable (and maybe ASCII) */ /* XXX: better multiline */ /* * Logger arguments are: * * magic: log level (0-5) * this: logger * stack: plain log args * * We want to minimize memory churn so a two-pass approach * is used: first pass formats arguments and computes final * string length, second pass copies strings either into a * pre-allocated and reused buffer (short messages) or into a * newly allocated fixed buffer. If the backend function plays * nice, it won't coerce the buffer to a string (and thus * intern it). */ nargs = duk_get_top(ctx); /* [ arg1 ... argN this ] */ /* * Log level check */ duk_push_this(ctx); duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L); logger_lev = (duk_small_int_t) duk_get_int(ctx, -1); if (entry_lev < logger_lev) { return 0; } /* log level could be popped but that's not necessary */ now = duk_bi_date_get_now(ctx); duk_bi_date_format_timeval(now, date_buf); date_len = DUK_STRLEN((const char *) date_buf); duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N); duk_to_string(ctx, -1); DUK_ASSERT(duk_is_string(ctx, -1)); /* [ arg1 ... argN this loggerLevel loggerName ] */ /* * Pass 1 */ /* Line format: <time> <entryLev> <loggerName>: <msg> */ tot_len = 0; tot_len += 3 + /* separators: space, space, colon */ 3 + /* level string */ date_len + /* time */ duk_get_length(ctx, -1); /* loggerName */ for (i = 0; i < nargs; i++) { /* When formatting an argument to a string, errors may happen from multiple * causes. In general we want to catch obvious errors like a toLogString() * throwing an error, but we don't currently try to catch every possible * error. In particular, internal errors (like out of memory or stack) are * not caught. Also, we expect Error toString() to not throw an error. */ if (duk_is_object(ctx, i)) { /* duk_pcall_prop() may itself throw an error, but we're content * in catching the obvious errors (like toLogString() throwing an * error). */ duk_push_hstring_stridx(ctx, DUK_STRIDX_FMT); duk_dup(ctx, i); /* [ arg1 ... argN this loggerLevel loggerName 'fmt' arg ] */ /* call: this.fmt(arg) */ rc = duk_pcall_prop(ctx, -5 /*obj_index*/, 1 /*nargs*/); if (rc) { /* Keep the error as the result (coercing it might fail below, * but we don't catch that now). */ ; } duk_replace(ctx, i); } (void) duk_to_lstring(ctx, i, &arg_len); tot_len++; /* sep (even before first one) */ tot_len += arg_len; } /* * Pass 2 */ if (tot_len <= DUK_BI_LOGGER_SHORT_MSG_LIMIT) { duk_hbuffer_dynamic *h_buf; DUK_DDD(DUK_DDDPRINT("reuse existing small log message buffer, tot_len %ld", (long) tot_len)); /* We can assert for all buffer properties because user code * never has access to heap->log_buffer. */ DUK_ASSERT(thr != NULL); DUK_ASSERT(thr->heap != NULL); h_buf = thr->heap->log_buffer; DUK_ASSERT(h_buf != NULL); DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) h_buf)); DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_ALLOC_SIZE(h_buf) == DUK_BI_LOGGER_SHORT_MSG_LIMIT); /* Set buffer 'visible size' to actual message length and * push it to the stack. */ DUK_HBUFFER_SET_SIZE((duk_hbuffer *) h_buf, tot_len); duk_push_hbuffer(ctx, (duk_hbuffer *) h_buf); buf = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf); } else { DUK_DDD(DUK_DDDPRINT("use a one-off large log message buffer, tot_len %ld", (long) tot_len)); buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, tot_len); } DUK_ASSERT(buf != NULL); p = buf; DUK_MEMCPY((void *) p, (void *) date_buf, date_len); p += date_len; *p++ = (duk_uint8_t) DUK_ASC_SPACE; q = duk__log_level_strings + (entry_lev * 3); DUK_MEMCPY((void *) p, (void *) q, (duk_size_t) 3); p += 3; *p++ = (duk_uint8_t) DUK_ASC_SPACE; arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, -2, &arg_len); DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len); p += arg_len; *p++ = (duk_uint8_t) DUK_ASC_COLON; for (i = 0; i < nargs; i++) { *p++ = (duk_uint8_t) DUK_ASC_SPACE; arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &arg_len); DUK_ASSERT(arg_str != NULL); DUK_MEMCPY((void *) p, (const void *) arg_str, arg_len); p += arg_len; } DUK_ASSERT(buf + tot_len == p); /* [ arg1 ... argN this loggerLevel loggerName buffer ] */ #if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_LOGGING) /* Do debugger forwarding before raw() because the raw() function * doesn't get the log level right now. */ if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) { const char *log_buf; duk_size_t sz_buf; log_buf = (const char *) duk_get_buffer(ctx, -1, &sz_buf); DUK_ASSERT(log_buf != NULL); duk_debug_write_notify(thr, DUK_DBG_CMD_LOG); duk_debug_write_int(thr, (duk_int32_t) entry_lev); duk_debug_write_string(thr, (const char *) log_buf, sz_buf); duk_debug_write_eom(thr); } #endif /* Call this.raw(msg); look up through the instance allows user to override * the raw() function in the instance or in the prototype for maximum * flexibility. */ duk_push_hstring_stridx(ctx, DUK_STRIDX_RAW); duk_dup(ctx, -2); /* [ arg1 ... argN this loggerLevel loggerName buffer 'raw' buffer ] */ duk_call_prop(ctx, -6, 1); /* this.raw(buffer) */ return 0; }
DUK_INTERNAL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap) { duk_fixedbuffer fb; const char *p = format; const char *p_end = p + DUK_STRLEN(format); duk_int_t retval; DUK_MEMZERO(&fb, sizeof(fb)); fb.buffer = (duk_uint8_t *) str; fb.length = size; fb.offset = 0; fb.truncated = 0; while (p < p_end) { char ch = *p++; const char *p_begfmt = NULL; duk_bool_t got_exclamation = 0; duk_bool_t got_long = 0; /* %lf, %ld etc */ duk__dprint_state st; if (ch != DUK_ASC_PERCENT) { duk_fb_put_byte(&fb, (duk_uint8_t) ch); continue; } /* * Format tag parsing. Since we don't understand all the * possible format tags allowed, we just scan for a terminating * specifier and keep track of relevant modifiers that we do * understand. See man 3 printf. */ DUK_MEMZERO(&st, sizeof(st)); st.fb = &fb; st.depth = 0; st.depth_limit = 1; st.loop_stack_index = 0; st.loop_stack_limit = DUK__LOOP_STACK_DEPTH; p_begfmt = p - 1; while (p < p_end) { ch = *p++; if (ch == DUK_ASC_STAR) { /* unsupported: would consume multiple args */ goto format_error; } else if (ch == DUK_ASC_PERCENT) { duk_fb_put_byte(&fb, (duk_uint8_t) DUK_ASC_PERCENT); break; } else if (ch == DUK_ASC_EXCLAMATION) { got_exclamation = 1; } else if (!got_exclamation && ch == DUK_ASC_LC_L) { got_long = 1; } else if (got_exclamation && ch == DUK_ASC_LC_D) { st.depth_limit = DUK__DEEP_DEPTH_LIMIT; } else if (got_exclamation && ch == DUK_ASC_LC_P) { st.follow_proto = 1; } else if (got_exclamation && ch == DUK_ASC_LC_I) { st.internal = 1; } else if (got_exclamation && ch == DUK_ASC_LC_X) { st.hexdump = 1; } else if (got_exclamation && ch == DUK_ASC_LC_H) { st.heavy = 1; } else if (got_exclamation && ch == DUK_ASC_ATSIGN) { st.pointer = 1; } else if (got_exclamation && ch == DUK_ASC_HASH) { st.binary = 1; } else if (got_exclamation && ch == DUK_ASC_UC_T) { duk_tval *t = va_arg(ap, duk_tval *); if (st.pointer && !st.heavy) { duk_fb_sprintf(&fb, "(%p)", (void *) t); } duk__print_tval(&st, t); break; } else if (got_exclamation && ch == DUK_ASC_UC_O) { duk_heaphdr *t = va_arg(ap, duk_heaphdr *); if (st.pointer && !st.heavy) { duk_fb_sprintf(&fb, "(%p)", (void *) t); } duk__print_heaphdr(&st, t); break; } else if (got_exclamation && ch == DUK_ASC_UC_I) {