Skip to content

Commit

Permalink
Free strings used for exception formats
Browse files Browse the repository at this point in the history
  • Loading branch information
hoelzro committed Jul 9, 2015
1 parent f090305 commit ac517ce
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 109 deletions.
25 changes: 17 additions & 8 deletions src/6model/6model.c
Expand Up @@ -48,9 +48,11 @@ static void die_over_missing_method(MVMThreadContext *tc, MVMObject *obj, MVMStr
return;
}
else {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Cannot find method '%s'",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
}
static void late_bound_find_method_return(MVMThreadContext *tc, void *sr_data) {
Expand All @@ -74,10 +76,13 @@ void MVM_6model_find_method(MVMThreadContext *tc, MVMObject *obj, MVMString *nam
MVMObject *cache, *HOW, *find_method, *code;
MVMCallsite *findmeth_callsite;

if (MVM_is_null(tc, obj))
MVM_exception_throw_adhoc(tc,
if (MVM_is_null(tc, obj)) {
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Cannot call method '%s' on a null object",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}

/* First try to find it in the cache. If we find it, we have a result.
* If we don't find it, but the cache is authoritative, then error. */
Expand All @@ -99,10 +104,13 @@ void MVM_6model_find_method(MVMThreadContext *tc, MVMObject *obj, MVMString *nam
HOW = MVM_6model_get_how(tc, STABLE(obj));
find_method = MVM_6model_find_method_cache_only(tc, HOW,
tc->instance->str_consts.find_method);
if (MVM_is_null(tc, find_method))
MVM_exception_throw_adhoc(tc,
if (MVM_is_null(tc, find_method)) {
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Cannot find method '%s': no method cache and no .^find_method",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}

/* Set up the call, using the result register as the target. */
code = MVM_frame_find_invokee(tc, find_method, NULL);
Expand Down Expand Up @@ -164,6 +172,7 @@ MVMint64 MVM_6model_can_method_cache_only(MVMThreadContext *tc, MVMObject *obj,
MVMObject *cache;

if (MVM_is_null(tc, obj))
// XXX here
MVM_exception_throw_adhoc(tc,
"Cannot look for method '%s' on a null object",
MVM_string_utf8_encode_C_string(tc, name));
Expand Down
9 changes: 6 additions & 3 deletions src/6model/reprs.c
Expand Up @@ -240,9 +240,12 @@ static MVMReprRegistry * find_repr_by_name(MVMThreadContext *tc,
MVM_string_flatten(tc, name);
MVM_HASH_GET(tc, tc->instance->repr_hash, name, entry)

if (entry == NULL)
MVM_exception_throw_adhoc(tc, "Lookup by name of unknown REPR: %s",
MVM_string_ascii_encode_any(tc, name));
if (entry == NULL) {
char *c_name = MVM_string_ascii_encode_any(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "Lookup by name of unknown REPR: %s",
c_name);
}

return entry;
}
Expand Down
6 changes: 4 additions & 2 deletions src/6model/reprs/CStruct.c
Expand Up @@ -344,8 +344,10 @@ static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *d
/* Helper for complaining about attribute access errors. */
MVM_NO_RETURN static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) MVM_NO_RETURN_GCC;
static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) {
MVM_exception_throw_adhoc(tc, "Can not %s non-existent attribute '%s'",
action, MVM_string_utf8_encode_C_string(tc, name));
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "Can not %s non-existent attribute '%s'",
action, c_name);
}

/* Helper to die because this type doesn't support attributes. */
Expand Down
6 changes: 4 additions & 2 deletions src/6model/reprs/CUnion.c
Expand Up @@ -357,8 +357,10 @@ static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *d
/* Helper for complaining about attribute access errors. */
MVM_NO_RETURN static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) MVM_NO_RETURN_GCC;
static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) {
MVM_exception_throw_adhoc(tc, "Can not %s non-existent attribute '%s'",
action, MVM_string_utf8_encode_C_string(tc, name));
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "Can not %s non-existent attribute '%s'",
action, c_name);
}

/* Helper to die because this type doesn't support attributes. */
Expand Down
36 changes: 24 additions & 12 deletions src/6model/reprs/MVMContext.c
Expand Up @@ -42,21 +42,27 @@ static void at_key(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *d
MVMFrame *frame = body->context;
MVMLexicalRegistry *lexical_names = frame->static_info->body.lexical_names, *entry;
if (!lexical_names) {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' does not exist in this frame",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
MVM_string_flatten(tc, name);
MVM_HASH_GET(tc, lexical_names, name, entry);
if (!entry) {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' does not exist in this frame",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
if (frame->static_info->body.lexical_types[entry->value] != kind) {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' has a different type in this frame",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
*result = frame->env[entry->value];
if (kind == MVM_reg_obj && !result->o)
Expand All @@ -69,21 +75,27 @@ static void bind_key(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void
MVMFrame *frame = body->context;
MVMLexicalRegistry *lexical_names = frame->static_info->body.lexical_names, *entry;
if (!lexical_names) {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' does not exist in this frame",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
MVM_string_flatten(tc, name);
MVM_HASH_GET(tc, lexical_names, name, entry);
if (!entry) {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' does not exist in this frame",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
if (frame->static_info->body.lexical_types[entry->value] != kind) {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' has a different type in this frame",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
frame->env[entry->value] = value;
}
Expand Down
12 changes: 8 additions & 4 deletions src/6model/reprs/NativeRef.c
Expand Up @@ -281,16 +281,20 @@ static MVMObject * lexref_by_name(MVMThreadContext *tc, MVMObject *type, MVMStri
return reg_or_lex_ref(tc, type, cur_frame, &cur_frame->env[entry->value], kind);
}
else {
MVM_exception_throw_adhoc(tc,
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Lexical with name '%s' has wrong type",
MVM_string_utf8_encode_C_string(tc, name));
c_name);
}
}
}
cur_frame = cur_frame->outer;
}
MVM_exception_throw_adhoc(tc, "No lexical found with name '%s'",
MVM_string_utf8_encode_C_string(tc, name));
char *c_name = MVM_string_utf8_encode_C_string(tc, name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "No lexical found with name '%s'",
c_name);
}
MVMObject * MVM_nativeref_lex_name_i(MVMThreadContext *tc, MVMString *name) {
MVMObject *ref_type = MVM_hll_current(tc)->int_lex_ref;
Expand Down
4 changes: 3 additions & 1 deletion src/6model/reprs/P6opaque.c
Expand Up @@ -201,7 +201,9 @@ static void gc_free_repr_data(MVMThreadContext *tc, MVMSTable *st) {
MVM_NO_RETURN
static void no_such_attribute(MVMThreadContext *tc, const char *action, MVMObject *class_handle, MVMString *name) {
MVMuint64 output_size;
MVM_exception_throw_adhoc(tc, "P6opaque: no such attribute '%s'", MVM_string_ascii_encode(tc, name, &output_size));
char *c_name = MVM_string_ascii_encode(tc, name, &output_size);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "P6opaque: no such attribute '%s'", c_name);
}

/* Gets the current value for an attribute. */
Expand Down
38 changes: 26 additions & 12 deletions src/6model/sc.c
Expand Up @@ -126,11 +126,18 @@ MVMint64 MVM_sc_find_code_idx(MVMThreadContext *tc, MVMSerializationContext *sc,
if (test == obj)
return i;
}
MVM_exception_throw_adhoc(tc,
"Code ref '%s' does not exist in serialization context",
REPR(obj)->ID == MVM_REPR_ID_MVMCode
? MVM_string_utf8_encode_C_string(tc, ((MVMCode *)obj)->body.name)
: "<NOT A CODE OBJECT>");

if (REPR(obj)->ID == MVM_REPR_ID_MVMCode) {
char *c_name = MVM_string_utf8_encode_C_string(tc, ((MVMCode *)obj)->body.name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Code ref '%s' does not exist in serialization context",
c_name);
}
else {
MVM_exception_throw_adhoc(tc,
"Code ref '<NOT A CODE OBJECT>' does not exist in serialization context");
}
}

/* Given a compilation unit and dependency index, returns that SC. */
Expand Down Expand Up @@ -163,10 +170,13 @@ MVMObject * MVM_sc_get_object(MVMThreadContext *tc, MVMSerializationContext *sc,
return roots[idx] && !sc_working(sc)
? roots[idx]
: MVM_serialization_demand_object(tc, sc, idx);
else
MVM_exception_throw_adhoc(tc,
else {
char *c_description = MVM_string_utf8_encode_C_string(tc, sc->body->description);
char *waste[] = { c_description, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Probable version skew in pre-compiled '%s' (cause: no object at index %"PRId64")",
MVM_string_utf8_encode_C_string(tc, sc->body->description), idx);
c_description, idx);
}
}

MVMObject * MVM_sc_get_sc_object(MVMThreadContext *tc, MVMCompUnit *cu,
Expand Down Expand Up @@ -224,9 +234,11 @@ MVMSTable * MVM_sc_get_stable(MVMThreadContext *tc, MVMSerializationContext *sc,
return got && !sc_working(sc) ? got : MVM_serialization_demand_stable(tc, sc, idx);
}
else {
MVM_exception_throw_adhoc(tc,
char *c_description = MVM_string_utf8_encode_C_string(tc, sc->body->description);
char *waste[] = { c_description, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Probable version skew in pre-compiled '%s' (cause: no STable at index %"PRId64")",
MVM_string_utf8_encode_C_string(tc, sc->body->description), idx);
c_description, idx);
}
}

Expand Down Expand Up @@ -288,9 +300,11 @@ MVMObject * MVM_sc_get_code(MVMThreadContext *tc, MVMSerializationContext *sc, M
: found;
}
else {
MVM_exception_throw_adhoc(tc,
char *c_description = MVM_string_utf8_encode_C_string(tc, sc->body->description);
char *waste[] = { c_description, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Probable version skew in pre-compiled '%s' (cause: no code ref at index %"PRId64")",
MVM_string_utf8_encode_C_string(tc, sc->body->description), idx);
c_description, idx);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/6model/serialization.c
Expand Up @@ -503,11 +503,14 @@ static MVMObject * closure_to_static_code_ref(MVMThreadContext *tc, MVMObject *c
MVMObject *scr = (MVMObject *)(((MVMCode *)closure)->body.sf)->body.static_code;

if (scr == NULL || MVM_sc_get_obj_sc(tc, scr) == NULL) {
if (fatal)
MVM_exception_throw_adhoc(tc,
if (fatal) {
char *c_name = MVM_string_utf8_encode_C_string(tc,
(((MVMCode *)closure)->body.sf)->body.name);
char *waste[] = { c_name, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Serialization Error: missing static code ref for closure '%s'",
MVM_string_utf8_encode_C_string(tc,
(((MVMCode *)closure)->body.sf)->body.name));
c_name);
}
return NULL;
}
return scr;
Expand Down
23 changes: 15 additions & 8 deletions src/core/args.c
Expand Up @@ -348,7 +348,9 @@ MVMArgInfo MVM_args_get_pos_str(MVMThreadContext *tc, MVMArgProcContext *ctx, MV
for (flag_pos = arg_pos = ctx->num_pos; arg_pos < ctx->arg_count; flag_pos++, arg_pos += 2) { \
if (MVM_string_equal(tc, ctx->args[arg_pos].s, name)) { \
if (ctx->named_used[(arg_pos - ctx->num_pos)/2]) { \
MVM_exception_throw_adhoc(tc, "Named argument '%s' already used", MVM_string_utf8_encode_C_string(tc, name)); \
char *c_name = MVM_string_utf8_encode_C_string(tc, name); \
char *waste[] = { c_name, NULL }; \
MVM_exception_throw_adhoc_free(tc, waste, "Named argument '%s' already used", c_name); \
} \
result.arg = ctx->args[arg_pos + 1]; \
result.flags = (ctx->arg_flags ? ctx->arg_flags : ctx->callsite->arg_flags)[flag_pos]; \
Expand All @@ -357,9 +359,11 @@ MVMArgInfo MVM_args_get_pos_str(MVMThreadContext *tc, MVMArgProcContext *ctx, MV
break; \
} \
} \
if (!result.exists && required) \
MVM_exception_throw_adhoc(tc, "Required named parameter '%s' not passed", MVM_string_utf8_encode_C_string(tc, name)); \
\
if (!result.exists && required) { \
char *c_name = MVM_string_utf8_encode_C_string(tc, name); \
char *waste[] = { c_name, NULL }; \
MVM_exception_throw_adhoc_free(tc, waste, "Required named parameter '%s' not passed", c_name); \
} \
} while (0)

MVMArgInfo MVM_args_get_named_obj(MVMThreadContext *tc, MVMArgProcContext *ctx, MVMString *name, MVMuint8 required) {
Expand Down Expand Up @@ -398,11 +402,14 @@ void MVM_args_assert_nameds_used(MVMThreadContext *tc, MVMArgProcContext *ctx) {
MVMuint16 size = (ctx->arg_count - ctx->num_pos) / 2;
MVMuint16 i;
for (i = 0; i < size; i++)
if (!ctx->named_used[i])
MVM_exception_throw_adhoc(tc,
if (!ctx->named_used[i]) {
char *c_param = MVM_string_utf8_encode_C_string(tc,
ctx->args[ctx->num_pos + 2 * i].s);
char *waste[] = { c_param, NULL };
MVM_exception_throw_adhoc_free(tc, waste,
"Unexpected named parameter '%s' passed",
MVM_string_utf8_encode_C_string(tc,
ctx->args[ctx->num_pos + 2 * i].s));
c_param);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/dll.c
Expand Up @@ -28,7 +28,8 @@ int MVM_dll_load(MVMThreadContext *tc, MVMString *name, MVMString *path) {

if (!lib) {
uv_mutex_unlock(&tc->instance->mutex_dll_registry);
MVM_exception_throw_adhoc(tc, "failed to load library '%s'", cpath);
char *waste[] = { cpath, NULL };
MVM_exception_throw_adhoc_free(tc, waste, "failed to load library '%s'", cpath);
}

MVM_free(cpath);
Expand Down

0 comments on commit ac517ce

Please sign in to comment.