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 *
pic_blob(pic_state *PIC_UNUSED(pic), pic_value blob, int *len)
pic_blob(pic_state *pic, pic_value blob, int *len)
{
if (len) {
*len = pic_blob_ptr(pic, blob)->len;

View File

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

View File

@ -251,25 +251,31 @@ obj_value(pic_state *PIC_UNUSED(pic), void *ptr)
#endif /* NAN_BOXING */
#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); \
}
DEFPTR(pic_id_ptr, struct identifier)
DEFPTR(pic_sym_ptr, symbol)
DEFPTR(pic_str_ptr, struct string)
DEFPTR(pic_blob_ptr, struct blob)
DEFPTR(pic_pair_ptr, struct pair)
DEFPTR(pic_vec_ptr, struct vector)
DEFPTR(pic_dict_ptr, struct dict)
DEFPTR(pic_weak_ptr, struct weak)
DEFPTR(pic_data_ptr, struct data)
DEFPTR(pic_proc_ptr, struct proc)
DEFPTR(pic_env_ptr, struct env)
DEFPTR(pic_port_ptr, struct port)
DEFPTR(pic_error_ptr, struct error)
DEFPTR(pic_rec_ptr, struct record)
DEFPTR(pic_irep_ptr, struct irep)
#define pic_data_p(pic,o) (pic_data_p(pic,o,NULL))
#define pic_port_p(pic,o) (pic_port_p(pic,o,NULL))
DEFPTR(id, struct identifier)
DEFPTR(sym, symbol)
DEFPTR(str, struct string)
DEFPTR(blob, struct blob)
DEFPTR(pair, struct pair)
DEFPTR(vec, struct vector)
DEFPTR(dict, struct dict)
DEFPTR(weak, struct weak)
DEFPTR(data, struct data)
DEFPTR(proc, struct proc)
DEFPTR(env, struct env)
DEFPTR(port, struct port)
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);