Browse Source

some more indirection for ANSI C functions

pull/2/head
Sami Vaarala 11 years ago
parent
commit
2842b0c20d
  1. 20
      src/duk_api.c
  2. 14
      src/duk_bi_global.c
  3. 2
      src/duk_debug_fixedbuffer.c
  4. 80
      src/duk_debug_macros.c
  5. 2
      src/duk_debug_vsnprintf.c
  6. 16
      src/duk_error_macros.c
  7. 15
      src/duk_features.h
  8. 4
      src/duk_hbuffer_ops.c
  9. 4
      src/duk_heap_stringtable.c

20
src/duk_api.c

@ -2303,7 +2303,7 @@ const char *duk_push_string(duk_context *ctx, const char *str) {
DUK_ASSERT(ctx != NULL); DUK_ASSERT(ctx != NULL);
if (str) { if (str) {
return duk_push_lstring(ctx, str, strlen(str)); return duk_push_lstring(ctx, str, DUK_STRLEN(str));
} else { } else {
duk_push_null(ctx); duk_push_null(ctx);
return NULL; return NULL;
@ -2317,7 +2317,7 @@ const char *duk_push_string(duk_context *ctx, const char *str) {
*/ */
const char *duk_push_string_file(duk_context *ctx, const char *path) { const char *duk_push_string_file(duk_context *ctx, const char *path) {
duk_hthread *thr = (duk_hthread *) ctx; duk_hthread *thr = (duk_hthread *) ctx;
FILE *f = NULL; duk_file *f = NULL;
char *buf; char *buf;
long sz; long sz;
@ -2325,32 +2325,32 @@ const char *duk_push_string_file(duk_context *ctx, const char *path) {
if (!path) { if (!path) {
goto fail; goto fail;
} }
f = fopen(path, "rb"); f = DUK_FOPEN(path, "rb");
if (!f) { if (!f) {
goto fail; goto fail;
} }
if (fseek(f, 0, SEEK_END) < 0) { if (DUK_FSEEK(f, 0, SEEK_END) < 0) {
goto fail; goto fail;
} }
sz = ftell(f); sz = DUK_FTELL(f);
if (sz < 0) { if (sz < 0) {
goto fail; goto fail;
} }
if (fseek(f, 0, SEEK_SET) < 0) { if (DUK_FSEEK(f, 0, SEEK_SET) < 0) {
goto fail; goto fail;
} }
buf = (char *) duk_push_fixed_buffer(ctx, (size_t) sz); buf = (char *) duk_push_fixed_buffer(ctx, (size_t) sz);
DUK_ASSERT(buf != NULL); DUK_ASSERT(buf != NULL);
if (fread(buf, 1, sz, f) != (size_t) sz) { if (DUK_FREAD(buf, 1, sz, f) != (size_t) sz) {
goto fail; goto fail;
} }
(void) fclose(f); /* ignore fclose() error */ (void) DUK_FCLOSE(f); /* ignore fclose() error */
f = NULL; f = NULL;
return duk_to_string(ctx, -1); return duk_to_string(ctx, -1);
fail: fail:
if (f) { if (f) {
fclose(f); DUK_FCLOSE(f);
} }
DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "failed to read file"); DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "failed to read file");
return NULL; return NULL;
@ -2584,7 +2584,7 @@ const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) {
} }
/* initial estimate based on format string */ /* initial estimate based on format string */
sz = strlen(fmt) + 16; /* XXX: plus something to avoid just missing */ sz = DUK_STRLEN(fmt) + 16; /* XXX: plus something to avoid just missing */
if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) { if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) {
sz = DUK_PUSH_SPRINTF_INITIAL_SIZE; sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
} }

14
src/duk_bi_global.c

@ -650,7 +650,7 @@ int duk_bi_global_object_unescape(duk_context *ctx) {
#ifdef DUK_USE_BROWSER_LIKE #ifdef DUK_USE_BROWSER_LIKE
#ifdef DUK_USE_FILE_IO #ifdef DUK_USE_FILE_IO
static int duk__print_alert_helper(duk_context *ctx, FILE *f_out) { static int duk__print_alert_helper(duk_context *ctx, duk_file *f_out) {
int nargs; int nargs;
int i; int i;
const char *str; const char *str;
@ -668,7 +668,7 @@ static int duk__print_alert_helper(duk_context *ctx, FILE *f_out) {
size_t sz = 0; size_t sz = 0;
buf = (const char *) duk_get_buffer(ctx, 0, &sz); buf = (const char *) duk_get_buffer(ctx, 0, &sz);
if (buf && sz > 0) { if (buf && sz > 0) {
fwrite(buf, 1, sz, f_out); DUK_FWRITE(buf, 1, sz, f_out);
} }
goto flush; goto flush;
} }
@ -689,23 +689,23 @@ static int duk__print_alert_helper(duk_context *ctx, FILE *f_out) {
str = duk_get_lstring(ctx, -1, &len); str = duk_get_lstring(ctx, -1, &len);
if (str) { if (str) {
fwrite(str, 1, len, f_out); DUK_FWRITE(str, 1, len, f_out);
} }
} }
fwrite(&nl, 1, 1, f_out); DUK_FWRITE(&nl, 1, 1, f_out);
flush: flush:
fflush(f_out); DUK_FFLUSH(f_out);
return 0; return 0;
} }
int duk_bi_global_object_print(duk_context *ctx) { int duk_bi_global_object_print(duk_context *ctx) {
return duk__print_alert_helper(ctx, stdout); return duk__print_alert_helper(ctx, DUK_STDOUT);
} }
int duk_bi_global_object_alert(duk_context *ctx) { int duk_bi_global_object_alert(duk_context *ctx) {
return duk__print_alert_helper(ctx, stderr); return duk__print_alert_helper(ctx, DUK_STDERR);
} }
#else /* DUK_USE_FILE_IO */ #else /* DUK_USE_FILE_IO */
/* Supported but no file I/O -> silently ignore, no error */ /* Supported but no file I/O -> silently ignore, no error */

2
src/duk_debug_fixedbuffer.c

@ -26,7 +26,7 @@ void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x) {
} }
void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) { void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) {
duk_fb_put_bytes(fb, (duk_uint8_t *) x, (duk_uint32_t) strlen(x)); duk_fb_put_bytes(fb, (duk_uint8_t *) x, (duk_uint32_t) DUK_STRLEN(x));
} }
void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...) { void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...) {

80
src/duk_debug_macros.c

@ -91,28 +91,28 @@ void duk_debug_log(int level, const char *file, int line, const char *func, char
duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap); duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
#ifdef DUK_USE_DPRINT_RDTSC #ifdef DUK_USE_DPRINT_RDTSC
fprintf(stderr, "%s[%s] <%llu> %s:%d (%s):%s %s%s\n", DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%d (%s):%s %s%s\n",
duk__get_term_1(level), duk__get_term_1(level),
duk__get_level_string(level), duk__get_level_string(level),
duk_rdtsc(), duk_rdtsc(),
file, file,
line, line,
func, func,
duk__get_term_2(level), duk__get_term_2(level),
duk__debug_buf, duk__debug_buf,
duk__get_term_3(level)); duk__get_term_3(level));
#else #else
fprintf(stderr, "%s[%s] %s:%d (%s):%s %s%s\n", DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%d (%s):%s %s%s\n",
duk__get_term_1(level), duk__get_term_1(level),
duk__get_level_string(level), duk__get_level_string(level),
file, file,
line, line,
func, func,
duk__get_term_2(level), duk__get_term_2(level),
duk__debug_buf, duk__debug_buf,
duk__get_term_3(level)); duk__get_term_3(level));
#endif #endif
fflush(stderr); DUK_FFLUSH(DUK_STDERR);
va_end(ap); va_end(ap);
} }
@ -134,28 +134,28 @@ void duk_debug_log(char *fmt, ...) {
duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap); duk_debug_vsnprintf(duk__debug_buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
#ifdef DUK_USE_DPRINT_RDTSC #ifdef DUK_USE_DPRINT_RDTSC
fprintf(stderr, "%s[%s] <%llu> %s:%s (%s):%s %s%s\n", DUK_FPRINTF(DUK_STDERR, "%s[%s] <%llu> %s:%s (%s):%s %s%s\n",
duk__get_term_1(level), duk__get_term_1(level),
duk__get_level_string(duk_debug_level_stash), duk__get_level_string(duk_debug_level_stash),
duk_rdtsc(), duk_rdtsc(),
duk_debug_file_stash, duk_debug_file_stash,
duk_debug_line_stash, duk_debug_line_stash,
duk_debug_func_stash, duk_debug_func_stash,
duk__get_term_2(level), duk__get_term_2(level),
duk__debug_buf, duk__debug_buf,
duk__get_term_3(level)); duk__get_term_3(level));
#else #else
fprintf(stderr, "%s[%s] %s:%s (%s):%s %s%s\n", DUK_FPRINTF(DUK_STDERR, "%s[%s] %s:%s (%s):%s %s%s\n",
duk__get_term_1(level), duk__get_term_1(level),
duk__get_level_string(duk_debug_level_stash), duk__get_level_string(duk_debug_level_stash),
duk_debug_file_stash, duk_debug_file_stash,
duk_debug_line_stash, duk_debug_line_stash,
duk_debug_func_stash, duk_debug_func_stash,
duk__get_term_2(level), duk__get_term_2(level),
duk__debug_buf, duk__debug_buf,
duk__get_term_3(level)); duk__get_term_3(level));
#endif #endif
fflush(stderr); DUK_FFLUSH(DUK_STDERR);
va_end(ap); va_end(ap);
} }

2
src/duk_debug_vsnprintf.c

@ -764,7 +764,7 @@ static void duk__print_opcode(duk__dprint_state *st, int opcode) {
int duk_debug_vsnprintf(char *str, size_t size, const char *format, va_list ap) { int duk_debug_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
duk_fixedbuffer fb; duk_fixedbuffer fb;
const char *p = format; const char *p = format;
const char *p_end = p + strlen(format); const char *p_end = p + DUK_STRLEN(format);
int retval; int retval;
DUK_MEMZERO(&fb, sizeof(fb)); DUK_MEMZERO(&fb, sizeof(fb));

16
src/duk_error_macros.c

@ -72,8 +72,8 @@ void duk_err_handle_error_nonverbose2(const char *filename, int line, duk_hthrea
void duk_default_fatal_handler(duk_context *ctx, int code, const char *msg) { void duk_default_fatal_handler(duk_context *ctx, int code, const char *msg) {
DUK_UNREF(ctx); DUK_UNREF(ctx);
#ifdef DUK_USE_FILE_IO #ifdef DUK_USE_FILE_IO
fprintf(stderr, "FATAL %d: %s\n", code, msg ? msg : "null"); DUK_FPRINTF(DUK_STDERR, "FATAL %d: %s\n", code, msg ? msg : "null");
fflush(stderr); DUK_FFLUSH(DUK_STDERR);
#else #else
/* omit print */ /* omit print */
#endif #endif
@ -89,18 +89,18 @@ void duk_default_fatal_handler(duk_context *ctx, int code, const char *msg) {
#if !defined(DUK_USE_PANIC_HANDLER) #if !defined(DUK_USE_PANIC_HANDLER)
void duk_default_panic_handler(int code, const char *msg) { void duk_default_panic_handler(int code, const char *msg) {
#ifdef DUK_USE_FILE_IO #ifdef DUK_USE_FILE_IO
fprintf(stderr, "PANIC %d: %s (" DUK_FPRINTF(DUK_STDERR, "PANIC %d: %s ("
#if defined(DUK_USE_PANIC_ABORT) #if defined(DUK_USE_PANIC_ABORT)
"calling abort" "calling abort"
#elif defined(DUK_USE_PANIC_EXIT) #elif defined(DUK_USE_PANIC_EXIT)
"calling exit" "calling exit"
#elif defined(DUK_USE_PANIC_SEGFAULT) #elif defined(DUK_USE_PANIC_SEGFAULT)
"segfaulting on purpose" "segfaulting on purpose"
#else #else
#error no DUK_USE_PANIC_xxx macro defined #error no DUK_USE_PANIC_xxx macro defined
#endif #endif
")\n", code, msg ? msg : "null"); ")\n", code, msg ? msg : "null");
fflush(stderr); DUK_FFLUSH(DUK_STDERR);
#else #else
/* omit print */ /* omit print */
#endif #endif

15
src/duk_features.h

@ -1065,6 +1065,11 @@ extern double duk_computed_nan;
* and so on. Such broken platforms can be dealt with here. * and so on. Such broken platforms can be dealt with here.
*/ */
typedef FILE duk_file;
#define DUK_STDIN stdin
#define DUK_STDOUT stdout
#define DUK_STDERR stderr
/* Old uclibcs have a broken memcpy so use memmove instead (this is overly /* Old uclibcs have a broken memcpy so use memmove instead (this is overly
* wide now on purpose): * wide now on purpose):
* http://lists.uclibc.org/pipermail/uclibc-cvs/2008-October/025511.html * http://lists.uclibc.org/pipermail/uclibc-cvs/2008-October/025511.html
@ -1078,8 +1083,11 @@ extern double duk_computed_nan;
#define DUK_MEMMOVE memmove #define DUK_MEMMOVE memmove
#define DUK_MEMCMP memcmp #define DUK_MEMCMP memcmp
#define DUK_MEMSET memset #define DUK_MEMSET memset
#define DUK_STRLEN strlen
#define DUK_STRCMP strcmp #define DUK_STRCMP strcmp
#define DUK_STRNCMP strncmp #define DUK_STRNCMP strncmp
#define DUK_PRINTF printf
#define DUK_FPRINTF fprintf
#define DUK_SPRINTF sprintf #define DUK_SPRINTF sprintf
#if defined(DUK_F_MSVC) #if defined(DUK_F_MSVC)
/* _snprintf() does NOT NUL terminate on truncation, but Duktape code never /* _snprintf() does NOT NUL terminate on truncation, but Duktape code never
@ -1094,6 +1102,13 @@ extern double duk_computed_nan;
#define DUK_VSNPRINTF vsnprintf #define DUK_VSNPRINTF vsnprintf
#define DUK_SSCANF sscanf #define DUK_SSCANF sscanf
#define DUK_VSSCANF vsscanf #define DUK_VSSCANF vsscanf
#define DUK_FOPEN fopen
#define DUK_FCLOSE fclose
#define DUK_FREAD fread
#define DUK_FWRITE fwrite
#define DUK_FSEEK fseek
#define DUK_FTELL ftell
#define DUK_FFLUSH fflush
#define DUK_MEMZERO(p,n) DUK_MEMSET((p), 0, (n)) #define DUK_MEMZERO(p,n) DUK_MEMSET((p), 0, (n))

4
src/duk_hbuffer_ops.c

@ -169,7 +169,7 @@ size_t duk_hbuffer_insert_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, si
DUK_ASSERT(str != NULL); DUK_ASSERT(str != NULL);
DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf)); DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
len = strlen(str); len = DUK_STRLEN(str);
duk_hbuffer_insert_bytes(thr, buf, offset, (duk_uint8_t *) str, len); duk_hbuffer_insert_bytes(thr, buf, offset, (duk_uint8_t *) str, len);
return len; return len;
} }
@ -261,7 +261,7 @@ size_t duk_hbuffer_append_cstring(duk_hthread *thr, duk_hbuffer_dynamic *buf, co
DUK_ASSERT(str != NULL); DUK_ASSERT(str != NULL);
DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf)); DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
len = strlen(str); len = DUK_STRLEN(str);
duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), (duk_uint8_t *) str, len); duk_hbuffer_insert_bytes(thr, buf, DUK_HBUFFER_GET_SIZE(buf), (duk_uint8_t *) str, len);
return len; return len;
} }

4
src/duk_heap_stringtable.c

@ -391,14 +391,14 @@ duk_hstring *duk_heap_string_lookup_u32(duk_heap *heap, duk_uint32_t val) {
char buf[DUK_STRTAB_U32_MAX_STRLEN+1]; char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
DUK_SNPRINTF(buf, sizeof(buf), "%u", (unsigned int) val); DUK_SNPRINTF(buf, sizeof(buf), "%u", (unsigned int) val);
buf[sizeof(buf) - 1] = (char) 0; buf[sizeof(buf) - 1] = (char) 0;
return duk_heap_string_lookup(heap, (duk_uint8_t *) buf, strlen(buf)); return duk_heap_string_lookup(heap, (duk_uint8_t *) buf, DUK_STRLEN(buf));
} }
duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val) { duk_hstring *duk_heap_string_intern_u32(duk_heap *heap, duk_uint32_t val) {
char buf[DUK_STRTAB_U32_MAX_STRLEN+1]; char buf[DUK_STRTAB_U32_MAX_STRLEN+1];
DUK_SNPRINTF(buf, sizeof(buf), "%u", (unsigned int) val); DUK_SNPRINTF(buf, sizeof(buf), "%u", (unsigned int) val);
buf[sizeof(buf) - 1] = (char) 0; buf[sizeof(buf) - 1] = (char) 0;
return duk_heap_string_intern(heap, (duk_uint8_t *) buf, strlen(buf)); return duk_heap_string_intern(heap, (duk_uint8_t *) buf, DUK_STRLEN(buf));
} }
duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val) { duk_hstring *duk_heap_string_intern_u32_checked(duk_hthread *thr, duk_uint32_t val) {

Loading…
Cancel
Save