Skip to content

Commit

Permalink
fix a bunch of minor issues for c++ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
svaarala committed Oct 25, 2013
1 parent 91ed73c commit bb91343
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 43 deletions.
8 changes: 4 additions & 4 deletions src/duk_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ static int resize_valstack(duk_context *ctx, size_t new_size) {
*/

new_alloc_size = sizeof(duk_tval) * new_size;
new_valstack = DUK_REALLOC_INDIRECT(thr->heap, (void **) &thr->valstack, new_alloc_size);
new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, (void **) &thr->valstack, new_alloc_size);
if (!new_valstack) {
DUK_DPRINT("failed to resize valstack to %d entries (%d bytes)",
new_size, new_alloc_size);
Expand Down Expand Up @@ -445,7 +445,7 @@ static int check_valstack_resize_helper(duk_context *ctx,
DUK_DDPRINT("valstack resize failed");

if (throw_flag) {
DUK_ERROR(ctx, DUK_ERR_ALLOC_ERROR, "failed to extend valstack");
DUK_ERROR(thr, DUK_ERR_ALLOC_ERROR, "failed to extend valstack");
} else {
return 0;
}
Expand Down Expand Up @@ -2320,7 +2320,7 @@ const char *duk_push_string_file(duk_context *ctx, const char *path) {
if (fseek(f, 0, SEEK_SET) < 0) {
goto fail;
}
buf = duk_push_fixed_buffer(ctx, sz);
buf = (char *) duk_push_fixed_buffer(ctx, sz);
DUK_ASSERT(buf != NULL);
if (fread(buf, 1, sz, f) != sz) {
goto fail;
Expand Down Expand Up @@ -2567,7 +2567,7 @@ const char *duk_push_vsprintf(duk_context *ctx, const char *fmt, va_list ap) {
}

/* FIXME: buffer to string */
res = duk_push_lstring(ctx, buf, len); /* [buf res] */
res = duk_push_lstring(ctx, (const char *) buf, (size_t) len); /* [buf res] */
duk_remove(ctx, -2);
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/duk_api_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void duk_to_fixed_buffer(duk_context *ctx, int index) {
}

size = DUK_HBUFFER_GET_SIZE(h_src);
data = duk_push_fixed_buffer(ctx, size);
data = (char *) duk_push_fixed_buffer(ctx, size);
if (size > 0) {
DUK_ASSERT(data != NULL);
DUK_MEMCPY(data, DUK_HBUFFER_DYNAMIC_GET_CURR_DATA_PTR(h_src), size);
Expand Down
6 changes: 3 additions & 3 deletions src/duk_api_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void duk_call(duk_context *ctx, int nargs) {
idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */
if (idx_func < 0 || nargs < 0) {
/* note that we can't reliably pop anything here */
DUK_ERROR(ctx, DUK_ERR_API_ERROR, "invalid call args");
DUK_ERROR(thr, DUK_ERR_API_ERROR, "invalid call args");
}

/* awkward; we assume there is space for this */
Expand Down Expand Up @@ -126,7 +126,7 @@ void duk_call_method(duk_context *ctx, int nargs) {
idx_func = duk_get_top(ctx) - nargs - 2; /* must work for nargs <= 0 */
if (idx_func < 0 || nargs < 0) {
/* note that we can't reliably pop anything here */
DUK_ERROR(ctx, DUK_ERR_API_ERROR, "invalid call args");
DUK_ERROR(thr, DUK_ERR_API_ERROR, "invalid call args");
}

errhandler = thr->heap->lj.errhandler; /* use existing one (if any) */
Expand Down Expand Up @@ -168,7 +168,7 @@ int duk_pcall(duk_context *ctx, int nargs, int errhandler_index) {
idx_func = duk_get_top(ctx) - nargs - 1; /* must work for nargs <= 0 */
if (idx_func < 0 || nargs < 0) {
/* note that we can't reliably pop anything here */
DUK_ERROR(ctx, DUK_ERR_API_ERROR, "invalid call args");
DUK_ERROR(thr, DUK_ERR_API_ERROR, "invalid call args");
/* FIXME: actually terminate thread? */
return DUK_ERR_EXEC_TERM;
}
Expand Down
6 changes: 3 additions & 3 deletions src/duk_api_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const char *duk_base64_encode(duk_context *ctx, int index) {
goto type_error;
}
dstlen = (srclen + 2) / 3 * 4;
dst = duk_push_fixed_buffer(ctx, dstlen);
dst = (unsigned char *) duk_push_fixed_buffer(ctx, dstlen);

base64_encode_helper((const unsigned char *) src, (const unsigned char *) (src + srclen),
(unsigned char *) dst, (unsigned char *) (dst + dstlen));
Expand Down Expand Up @@ -223,7 +223,7 @@ void duk_base64_decode(duk_context *ctx, int index) {
goto type_error;
}
dstlen = (srclen + 3) / 4 * 3; /* upper limit */
dst = duk_push_dynamic_buffer(ctx, dstlen);
dst = (unsigned char *) duk_push_dynamic_buffer(ctx, dstlen);
/* Note: for dstlen=0, dst may be NULL */

retval = base64_decode_helper((unsigned char *) src, (unsigned char *) (src + srclen),
Expand Down Expand Up @@ -252,7 +252,7 @@ const char *duk_hex_encode(duk_context *ctx, int index) {
/* FIXME: special case for input string, no need to coerce to buffer */

index = duk_require_normalize_index(ctx, index);
data = duk_to_buffer(ctx, index, &len);
data = (unsigned char *) duk_to_buffer(ctx, index, &len);
DUK_ASSERT(data != NULL);

buf = (unsigned char *) duk_push_fixed_buffer(ctx, len * 2);
Expand Down
2 changes: 1 addition & 1 deletion src/duk_api_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void concat_and_join_helper(duk_context *ctx, int count, int is_join) {
DUK_DDDPRINT("join/concat %d strings, total length %d bytes", count, len);

/* use stack allocated buffer to ensure reachability in errors (e.g. intern error) */
buf = duk_push_fixed_buffer(ctx, len);
buf = (duk_u8 *) duk_push_fixed_buffer(ctx, len);
DUK_ASSERT(buf != NULL);

/* [... (sep) str1 str2 ... strN buf] */
Expand Down
10 changes: 5 additions & 5 deletions src/duk_builtin_boolean.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ static int tostring_valueof_helper(duk_context *ctx, int coerce_tostring) {
}

int duk_builtin_boolean_constructor(duk_context *ctx) {
duk_hobject *this;
duk_hobject *h_this;

duk_to_boolean(ctx, 0);

if (duk_is_constructor_call(ctx)) {
/* FIXME: helper; rely on Boolean.prototype as being non-writable, non-configurable */
duk_push_this(ctx);
this = duk_get_hobject(ctx, -1);
DUK_ASSERT(this != NULL);
DUK_ASSERT(this->prototype == ((duk_hthread *) ctx)->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]);
h_this = duk_get_hobject(ctx, -1);
DUK_ASSERT(h_this != NULL);
DUK_ASSERT(h_this->prototype == ((duk_hthread *) ctx)->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]);

DUK_HOBJECT_SET_CLASS_NUMBER(this, DUK_HOBJECT_CLASS_BOOLEAN);
DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN);

duk_dup(ctx, 0); /* -> [ val obj val ] */
duk_def_prop_stridx(ctx, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* FIXME: proper flags? */
Expand Down
2 changes: 1 addition & 1 deletion src/duk_builtin_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ static int print_alert_helper(duk_context *ctx, FILE *f_out) {
if (nargs == 1 && duk_is_buffer(ctx, 0)) {
const char *buf = NULL;
size_t sz = 0;
buf = duk_get_buffer(ctx, 0, &sz);
buf = (const char *) duk_get_buffer(ctx, 0, &sz);
if (buf && sz > 0) {
fwrite(buf, 1, sz, f_out);
}
Expand Down
4 changes: 2 additions & 2 deletions src/duk_builtin_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void json_dec_reviver_walk(duk_json_dec_ctx *js_ctx);

static void json_emit_1(duk_json_enc_ctx *js_ctx, char ch);
static void json_emit_2(duk_json_enc_ctx *js_ctx, int chars);
static void json_emit_esc(duk_json_enc_ctx *js_ctx, duk_u32 cp, char *esc_str, int digits);
static void json_emit_esc(duk_json_enc_ctx *js_ctx, duk_u32 cp, const char *esc_str, int digits);
static void json_emit_esc16(duk_json_enc_ctx *js_ctx, duk_u32 cp);
static void json_emit_esc32(duk_json_enc_ctx *js_ctx, duk_u32 cp);
static void json_emit_xutf8(duk_json_enc_ctx *js_ctx, duk_u32 cp);
Expand Down Expand Up @@ -630,7 +630,7 @@ static void json_emit_2(duk_json_enc_ctx *js_ctx, int chars) {
duk_hbuffer_append_bytes(js_ctx->thr, js_ctx->h_buf, (duk_u8 *) buf, 2);
}

static void json_emit_esc(duk_json_enc_ctx *js_ctx, duk_u32 cp, char *esc_str, int digits) {
static void json_emit_esc(duk_json_enc_ctx *js_ctx, duk_u32 cp, const char *esc_str, int digits) {
int dig;

duk_hbuffer_append_cstring(js_ctx->thr, js_ctx->h_buf, esc_str);
Expand Down
2 changes: 1 addition & 1 deletion src/duk_builtin_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ int duk_builtin_string_prototype_split(duk_context *ctx) {
if (is_regexp) {
duk_dup(ctx, 0);
duk_dup(ctx, 2);
duk_regexp_match_force_global(ctx); /* [ ... regexp input ] -> [ res_obj ] */
duk_regexp_match_force_global(thr); /* [ ... regexp input ] -> [ res_obj ] */
if (!duk_is_object(ctx, -1)) {
duk_pop(ctx);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/duk_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ extern void duk_debug_log(char *fmt, ...);

void duk_fb_put_bytes(duk_fixedbuffer *fb, duk_u8 *buffer, duk_u32 length);
void duk_fb_put_byte(duk_fixedbuffer *fb, duk_u8 x);
void duk_fb_put_cstring(duk_fixedbuffer *fb, char *x);
void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x);
void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...);
int duk_fb_is_full(duk_fixedbuffer *fb);

Expand Down
2 changes: 1 addition & 1 deletion src/duk_debug_fixedbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void duk_fb_put_byte(duk_fixedbuffer *fb, duk_u8 x) {
duk_fb_put_bytes(fb, &x, 1);
}

void duk_fb_put_cstring(duk_fixedbuffer *fb, char *x) {
void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) {
duk_fb_put_bytes(fb, (duk_u8 *) x, (duk_u32) strlen(x));
}

Expand Down
1 change: 1 addition & 0 deletions src/duk_debug_macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int duk_debug_level_stash;

void duk_debug_log(char *fmt, ...) {
va_list ap;
int level = duk_debug_level_stash;

va_start(ap, fmt);

Expand Down
2 changes: 1 addition & 1 deletion src/duk_debug_vsnprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ static void print_hbuffer(duk_dprint_state *st, duk_hbuffer *h) {
if (st->hexdump) {
duk_fb_sprintf(fb, "=[");
n = DUK_HBUFFER_GET_SIZE(h);
p = DUK_HBUFFER_GET_DATA_PTR(h);
p = (duk_u8 *) DUK_HBUFFER_GET_DATA_PTR(h);
for (i = 0; i < n; i++) {
duk_fb_sprintf(fb, "%02x", (int) p[i]);
}
Expand Down
12 changes: 10 additions & 2 deletions src/duk_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@
/* XXX: more accurate detection of what gcc versions work; more inline
* asm versions for other compilers.
*/
#if defined(__GNUC__) && defined(__i386__)
#if defined(__GNUC__) && defined(__i386__) && \
!defined(__cplusplus) /* unsigned long long not standard */
static __inline__ unsigned long long duk_rdtsc(void) {
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#define DUK_RDTSC_AVAILABLE 1
#elif defined(__GNUC__) && defined(__x86_64__)
#elif defined(__GNUC__) && defined(__x86_64__) && \
!defined(__cplusplus) /* unsigned long long not standard */
static __inline__ unsigned long long duk_rdtsc(void) {
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
Expand Down Expand Up @@ -122,10 +124,16 @@ static __inline__ unsigned long long duk_rdtsc(void) {
*/

#if defined(__linux)
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* e.g. getdate_r */
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE /* e.g. strptime */
#endif
#endif

#if defined(__APPLE__)
/* Apple OSX */
Expand Down
2 changes: 1 addition & 1 deletion src/duk_hbuffer_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, size_t size, int dynamic) {
alloc_size = sizeof(duk_hbuffer_fixed) + size + 1; /* +1 for a safety nul term */
}

res = DUK_ALLOC(heap, alloc_size);
res = (duk_hbuffer *) DUK_ALLOC(heap, alloc_size);
if (!res) {
goto error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/duk_heap_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
#endif

/* use a raw call, all macros expect the heap to be initialized */
res = alloc_func(alloc_udata, sizeof(duk_heap));
res = (duk_heap *) alloc_func(alloc_udata, sizeof(duk_heap));
if (!res) {
goto error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/duk_heap_stringtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static duk_hstring *alloc_init_hstring(duk_heap *heap,
/* NUL terminate for convenient C access */

alloc_size = sizeof(duk_hstring) + blen + 1;
res = DUK_ALLOC(heap, alloc_size);
res = (duk_hstring *) DUK_ALLOC(heap, alloc_size);
if (!res) {
goto error;
}
Expand Down
8 changes: 4 additions & 4 deletions src/duk_hobject_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ duk_hobject *duk_hobject_alloc(duk_heap *heap, int hobject_flags) {
DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_NATIVEFUNCTION) == 0);
DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_THREAD) == 0);

res = DUK_ALLOC(heap, sizeof(duk_hobject));
res = (duk_hobject *) DUK_ALLOC(heap, sizeof(duk_hobject));
if (!res) {
return NULL;
}
Expand All @@ -62,7 +62,7 @@ duk_hobject *duk_hobject_alloc(duk_heap *heap, int hobject_flags) {
duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, int hobject_flags) {
duk_hcompiledfunction *res;

res = DUK_ALLOC(heap, sizeof(duk_hcompiledfunction));
res = (duk_hcompiledfunction *) DUK_ALLOC(heap, sizeof(duk_hcompiledfunction));
if (!res) {
return NULL;
}
Expand All @@ -82,7 +82,7 @@ duk_hcompiledfunction *duk_hcompiledfunction_alloc(duk_heap *heap, int hobject_f
duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, int hobject_flags) {
duk_hnativefunction *res;

res = DUK_ALLOC(heap, sizeof(duk_hnativefunction));
res = (duk_hnativefunction *) DUK_ALLOC(heap, sizeof(duk_hnativefunction));
if (!res) {
return NULL;
}
Expand All @@ -108,7 +108,7 @@ duk_hnativefunction *duk_hnativefunction_alloc(duk_heap *heap, int hobject_flags
duk_hthread *duk_hthread_alloc(duk_heap *heap, int hobject_flags) {
duk_hthread *res;

res = DUK_ALLOC(heap, sizeof(duk_hthread));
res = (duk_hthread *) DUK_ALLOC(heap, sizeof(duk_hthread));
if (!res) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/duk_hobject_props.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ static void realloc_props(duk_hthread *thr,
* allocation later.
*/

new_p = duk_push_dynamic_buffer(ctx, new_alloc_size); /* errors out if out of memory */
new_p = (duk_u8 *) duk_push_dynamic_buffer(ctx, new_alloc_size); /* errors out if out of memory */
DUK_ASSERT(new_p != NULL); /* since new_alloc_size > 0 */
}

Expand Down
6 changes: 3 additions & 3 deletions src/duk_hthread_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr) {

/* valstack */
alloc_size = sizeof(duk_tval) * DUK_VALSTACK_INITIAL_SIZE;
thr->valstack = DUK_ALLOC(heap, alloc_size);
thr->valstack = (duk_tval *) DUK_ALLOC(heap, alloc_size);
if (!thr->valstack) {
goto fail;
}
Expand All @@ -40,7 +40,7 @@ int duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr) {

/* callstack */
alloc_size = sizeof(duk_activation) * DUK_CALLSTACK_INITIAL_SIZE;
thr->callstack = DUK_ALLOC(heap, alloc_size);
thr->callstack = (duk_activation *) DUK_ALLOC(heap, alloc_size);
if (!thr->callstack) {
goto fail;
}
Expand All @@ -50,7 +50,7 @@ int duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr) {

/* catchstack */
alloc_size = sizeof(duk_catcher) * DUK_CATCHSTACK_INITIAL_SIZE;
thr->catchstack = DUK_ALLOC(heap, alloc_size);
thr->catchstack = (duk_catcher *) DUK_ALLOC(heap, alloc_size);
if (!thr->catchstack) {
goto fail;
}
Expand Down
4 changes: 2 additions & 2 deletions src/duk_hthread_stacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void duk_hthread_callstack_grow(duk_hthread *thr) {
* pointer may be changed by mark-and-sweep.
*/

thr->callstack = DUK_REALLOC_INDIRECT_CHECKED(thr, (void **) &thr->callstack, sizeof(duk_activation) * new_size);
thr->callstack = (duk_activation *) DUK_REALLOC_INDIRECT_CHECKED(thr, (void **) &thr->callstack, sizeof(duk_activation) * new_size);
thr->callstack_size = new_size;

/* note: any entries above the callstack top are garbage and not zeroed */
Expand Down Expand Up @@ -269,7 +269,7 @@ void duk_hthread_catchstack_grow(duk_hthread *thr) {
* pointer may be changed by mark-and-sweep.
*/

thr->catchstack = DUK_REALLOC_INDIRECT_CHECKED(thr, (void **) &thr->catchstack, sizeof(duk_catcher) * new_size);
thr->catchstack = (duk_catcher *) DUK_REALLOC_INDIRECT_CHECKED(thr, (void **) &thr->catchstack, sizeof(duk_catcher) * new_size);
thr->catchstack_size = new_size;

/* note: any entries above the catchstack top are garbage and not zeroed */
Expand Down
4 changes: 2 additions & 2 deletions src/duk_js_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ int duk_handle_call(duk_hthread *thr,
*/

if (!duk_is_callable(thr, idx_func)) {
DUK_ERROR(ctx, DUK_ERR_TYPE_ERROR, "call target not callable");
DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "call target not callable");
}
func = duk_get_hobject(thr, idx_func);
DUK_ASSERT(func != NULL);
Expand Down Expand Up @@ -1732,7 +1732,7 @@ void duk_handle_ecma_call_setup(duk_hthread *thr,
*/

if (!duk_is_callable(thr, idx_func)) {
DUK_ERROR(ctx, DUK_ERR_TYPE_ERROR, "call target not callable");
DUK_ERROR(thr, DUK_ERR_TYPE_ERROR, "call target not callable");
}
func = duk_get_hobject(thr, idx_func);
DUK_ASSERT(func != NULL);
Expand Down
4 changes: 2 additions & 2 deletions src/duk_js_executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ static int handle_longjmp(duk_hthread *thr,
return retval;

convert_to_internal_error:
DUK_ERROR((duk_context *) thr, DUK_ERR_INTERNAL_ERROR, "internal error in bytecode executor longjmp handler");
DUK_ERROR(thr, DUK_ERR_INTERNAL_ERROR, "internal error in bytecode executor longjmp handler");
#if 0
/* FIXME: could also handle internally */
thr->heap->lj.type = DUK_LJ_TYPE_THROW;
Expand Down Expand Up @@ -1769,7 +1769,7 @@ void duk_js_execute_bytecode(duk_hthread *entry_thread) {

duk_push_tval(ctx, REGCONSTP(c));
duk_push_tval(ctx, REGCONSTP(b)); /* -> [ ... escaped_source bytecode ] */
duk_regexp_create_instance(ctx); /* -> [ ... regexp_instance ] */
duk_regexp_create_instance(thr); /* -> [ ... regexp_instance ] */
DUK_DDDPRINT("regexp instance: %!iT", duk_get_tval(ctx, -1));
duk_replace(ctx, a);
#else
Expand Down

0 comments on commit bb91343

Please sign in to comment.