add assertions to type cast functions

This commit is contained in:
Yuichi Nishiwaki 2017-04-02 00:12:24 +09:00
parent 130d226d65
commit eaea31ee19
3 changed files with 33 additions and 17 deletions

View File

@ -20,7 +20,7 @@ pic_blob_value(pic_state *pic, const unsigned char *buf, int len)
} }
unsigned char * unsigned char *
pic_blob(pic_state *PIC_UNUSED(pic), pic_value blob, int *len) pic_blob(pic_state *pic, pic_value blob, int *len)
{ {
if (len) { if (len) {
*len = pic_blob_ptr(pic, blob)->len; *len = pic_blob_ptr(pic, blob)->len;

View File

@ -41,6 +41,10 @@ enum {
PIC_TYPE_MAX = 63 PIC_TYPE_MAX = 63
}; };
PIC_STATIC_INLINE bool pic_int_p(pic_state *, pic_value);
PIC_STATIC_INLINE bool pic_float_p(pic_state *, pic_value);
PIC_STATIC_INLINE bool pic_char_p(pic_state *, pic_value);
#if !PIC_NAN_BOXING #if !PIC_NAN_BOXING
PIC_STATIC_INLINE pic_value PIC_STATIC_INLINE pic_value
@ -61,18 +65,21 @@ pic_type(pic_state *PIC_UNUSED(pic), pic_value v)
PIC_STATIC_INLINE int PIC_STATIC_INLINE int
pic_int(pic_state *PIC_UNUSED(pic), pic_value v) pic_int(pic_state *PIC_UNUSED(pic), pic_value v)
{ {
assert(pic_int_p(pic, v));
return v.u.i; return v.u.i;
} }
PIC_STATIC_INLINE double PIC_STATIC_INLINE double
pic_float(pic_state *PIC_UNUSED(pic), pic_value v) pic_float(pic_state *PIC_UNUSED(pic), pic_value v)
{ {
assert(pic_float_p(v));
return v.u.f; return v.u.f;
} }
PIC_STATIC_INLINE char PIC_STATIC_INLINE char
pic_char(pic_state *PIC_UNUSED(pic), pic_value v) pic_char(pic_state *PIC_UNUSED(pic), pic_value v)
{ {
assert(pic_char_p(v));
return v.u.c; return v.u.c;
} }
@ -128,6 +135,7 @@ PIC_STATIC_INLINE int
pic_int(pic_state *PIC_UNUSED(pic), pic_value v) pic_int(pic_state *PIC_UNUSED(pic), pic_value v)
{ {
union { int i; unsigned u; } u; union { int i; unsigned u; } u;
assert(pic_int_p(pic, v));
u.u = v.v & 0xfffffffful; u.u = v.v & 0xfffffffful;
return u.i; return u.i;
} }
@ -136,6 +144,7 @@ PIC_STATIC_INLINE double
pic_float(pic_state *PIC_UNUSED(pic), pic_value v) pic_float(pic_state *PIC_UNUSED(pic), pic_value v)
{ {
union { double f; uint64_t i; } u; union { double f; uint64_t i; } u;
assert(pic_float_p(pic, v));
u.i = v.v; u.i = v.v;
return u.f; return u.f;
} }
@ -143,6 +152,7 @@ pic_float(pic_state *PIC_UNUSED(pic), pic_value v)
PIC_STATIC_INLINE char PIC_STATIC_INLINE char
pic_char(pic_state *PIC_UNUSED(pic), pic_value v) pic_char(pic_state *PIC_UNUSED(pic), pic_value v)
{ {
assert(pic_char_p(pic, v));
return v.v & 0xfffffffful; return v.v & 0xfffffffful;
} }

View File

@ -251,25 +251,31 @@ obj_value(pic_state *PIC_UNUSED(pic), void *ptr)
#endif /* NAN_BOXING */ #endif /* NAN_BOXING */
#define DEFPTR(name,type) \ #define DEFPTR(name,type) \
PIC_STATIC_INLINE type *name(pic_state *PIC_UNUSED(pic), pic_value o) { \ PIC_STATIC_INLINE type * \
pic_##name##_ptr(pic_state *PIC_UNUSED(pic), pic_value o) { \
assert(pic_##name##_p(pic,o)); \
return (type *) obj_ptr(pic, o); \ return (type *) obj_ptr(pic, o); \
} }
DEFPTR(pic_id_ptr, struct identifier) #define pic_data_p(pic,o) (pic_data_p(pic,o,NULL))
DEFPTR(pic_sym_ptr, symbol) #define pic_port_p(pic,o) (pic_port_p(pic,o,NULL))
DEFPTR(pic_str_ptr, struct string) DEFPTR(id, struct identifier)
DEFPTR(pic_blob_ptr, struct blob) DEFPTR(sym, symbol)
DEFPTR(pic_pair_ptr, struct pair) DEFPTR(str, struct string)
DEFPTR(pic_vec_ptr, struct vector) DEFPTR(blob, struct blob)
DEFPTR(pic_dict_ptr, struct dict) DEFPTR(pair, struct pair)
DEFPTR(pic_weak_ptr, struct weak) DEFPTR(vec, struct vector)
DEFPTR(pic_data_ptr, struct data) DEFPTR(dict, struct dict)
DEFPTR(pic_proc_ptr, struct proc) DEFPTR(weak, struct weak)
DEFPTR(pic_env_ptr, struct env) DEFPTR(data, struct data)
DEFPTR(pic_port_ptr, struct port) DEFPTR(proc, struct proc)
DEFPTR(pic_error_ptr, struct error) DEFPTR(env, struct env)
DEFPTR(pic_rec_ptr, struct record) DEFPTR(port, struct port)
DEFPTR(pic_irep_ptr, struct irep) DEFPTR(error, struct error)
DEFPTR(rec, struct record)
DEFPTR(irep, struct irep)
#undef pic_data_p
#undef pic_port_p
struct object *pic_obj_alloc(pic_state *, size_t, int type); struct object *pic_obj_alloc(pic_state *, size_t, int type);