#include <stdio.h>
#include <string.h>
#include <stdarg.h>
+#include <limits.h>
#include "jv_alloc.h"
#include "jv.h"
return j.u.number;
}
+int jv_is_integer(jv j){
+ if(jv_get_kind(j) != JV_KIND_NUMBER){
+ return 0;
+ }
+ double x = jv_number_value(j);
+ if(x != x || x > INT_MAX || x < INT_MIN){
+ return 0;
+ }
+
+ return x == (int)x;
+}
/*
* Arrays (internal helpers)
v = jv_null();
}
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_NUMBER) {
- // FIXME: don't do lookup for noninteger index
- v = jv_array_get(t, (int)jv_number_value(k));
- if (!jv_is_valid(v)) {
- jv_free(v);
+ if(jv_is_integer(k)){
+ v = jv_array_get(t, (int)jv_number_value(k));
+ if (!jv_is_valid(v)) {
+ jv_free(v);
+ v = jv_null();
+ }
+ } else {
+ jv_free(t);
+ jv_free(k);
v = jv_null();
}
} else if (jv_get_kind(t) == JV_KIND_ARRAY && jv_get_kind(k) == JV_KIND_OBJECT) {