static void go_deep(int depth) { JD_VAR(a); jd_sprintf(a, "depth=%d", depth); if (depth == 0) jd_throw("Reached the bottom"); else go_deep(depth - 1); }
jd_var *jd_get_context(jd_var *root, jd_var *path, jd_path_context *ctx, int vivify) { jd_var *ptr = NULL; JD_SCOPE { JD_2VARS(part, elt); if (path->type == ARRAY) jd_clone(part, path, 0); else jd_split(part, path, jd_nsv(".")); if (!jd_shift(part, 1, elt) || jd_compare(elt, jd_nsv("$"))) jd_throw("Bad path"); for (ptr = root; ptr && jd_shift(part, 1, elt);) { if (ptr->type == VOID) { /* empty slot: type depends on key format */ if (is_positive_int(elt)) jd_set_array(ptr, 1); else jd_set_hash(ptr, 1); } if (ptr->type == ARRAY) { size_t ac = jd_count(ptr); jd_int ix = jd_get_int(elt); if (ix == ac && vivify) ptr = jd_push(ptr, 1); else if (ix < ac) ptr = jd_get_idx(ptr, ix); else { ptr = NULL; } } else if (ptr->type == HASH) { ptr = jd_get_key(ptr, elt, vivify); } else { jd_throw("Unexpected element in structure"); } } } return ptr; }
int main(void) { try { JD_2VARS(a, b); jd_set_string(a, "This is A"); jd_throw("Oops: a=%J", a); jd_set_bool(b, 1); } puts("cleanup"); catch (e) jd_rethrow(e);
static void test_throw_in_catch(void) { int volatile catch = 0, first = 0; try { JD_SV(a, "first"); jd_throw("Throw from %V block", a); } catch (e) { try { JD_SV(a, "catch"); jd_throw("Throw from %V block", a); } catch (e) { jdt_is_string(jd_rv(e, "$.message"), "Throw from catch block", "got throw catch"); catch ++; } jdt_is_string(jd_rv(e, "$.message"), "Throw from first block", "got first catch"); first++; }
static void nest_deep(int depth) { try { JD_VAR(a); jd_sprintf(a, "depth=%d", depth); if (depth == 0) jd_throw("Reached the bottom"); else nest_deep(depth - 1); } catch (e) { jd_rethrow(e); } }
static y4m2_output *filter__hook(jd_var *stash, const char *name, y4m2_output *out) { (void)stash; jd_var *fp = jd_get_ks(&filters, name, 0); if (!fp) jd_throw("Unknown filter \"%s\"", name); filter *filt = filter__clone(jd_ptr(fp)); filt->ctx = NULL; filt->out = out; jd_set_object(jd_unshift(stash, 1), filt, filter__free_cb); return y4m2_output_next(filter__callback, filt); }
static void test_simple_throw(void) { try { JD_2VARS(a, b); jd_set_string(a, "This is A"); jd_throw("Oops: a=%J", a); jd_set_bool(b, 1); } catch (e) { jdt_is_string(jd_rv(e, "$.message"), "Oops: a=\"This is A\"", "exception message matches"); jd_release(e); } }
static unsigned cook_idx(int idx, size_t count, size_t max) { if (idx < 0) idx += count; if (idx < 0 || idx >= max) jd_throw("Array index %d out of bounds (0..%lu)", idx, (unsigned long) max); return (unsigned) idx; }