#include <stdint.h>
#include <stddef.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include "jv_alloc.h"
#include "jv.h"
#include "jv_unicode.h"
#include "util.h"
/*
* Internal refcounting helpers
*/
typedef struct jv_refcnt {
int count;
} jv_refcnt;
static const jv_refcnt JV_REFCNT_INIT = {1};
static void jvp_refcnt_inc(jv_refcnt* c) {
c->count++;
}
static int jvp_refcnt_dec(jv_refcnt* c) {
c->count--;
return c->count == 0;
}
static int jvp_refcnt_unshared(jv_refcnt* c) {
assert(c->count > 0);
return c->count == 1;
}
/*
* Simple values (true, false, null)
*/
#define KIND_MASK 0xf
jv_kind jv_get_kind(jv x) {
return x.kind_flags & KIND_MASK;
}
const char* jv_kind_name(jv_kind k) {
switch (k) {
case JV_KIND_INVALID: return "<invalid>";
case JV_KIND_NULL: return "null";
case JV_KIND_FALSE: return "boolean";
case JV_KIND_TRUE: return "boolean";
case JV_KIND_NUMBER: return "number";
case JV_KIND_STRING: return "string";
case JV_KIND_ARRAY: return "array";
case JV_KIND_OBJECT: return "object";
}
assert(0 && "invalid kind");
return "<unknown>";
}
static const jv JV_NULL = {JV_KIND_NULL, 0, 0, 0, {0}};
static const jv JV_INVALID = {JV_KIND_INVALID, 0, 0, 0, {0}};
static const jv JV_FALSE = {JV_KIND_FALSE, 0, 0, 0, {0}};
static const jv JV_TRUE = {JV_KIND_TRUE, 0, 0, 0, {0}};
jv jv_true() {
return JV_TRUE;
}
jv jv_false() {
return JV_FALSE;
}
jv