Skip to content

Commit

Permalink
Report more details on argument conversion failure
Browse files Browse the repository at this point in the history
Will hopefully help users and developers to debug conversion problems
more easily. For now prints .toString() of the JS object and the
GITypeTag of the expected data.
In the future the exception could output the GType information.
  • Loading branch information
jonnor committed Aug 28, 2012
1 parent 764c00b commit c9722a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,31 @@ v8::Handle<v8::Value> Func::CallAndGetPtr(GObject *obj, GIFunctionInfo *info, co
* o.__call__("func_name", args) VS o.func_name(args) */
const int real_arg_idx = ignore_function_name ? i : i + offset_;
GIArgInfo *arg = g_callable_info_get_arg(info, i);
GITypeInfo *arg_type_info = g_arg_info_get_type(arg);
GIDirection dir = g_arg_info_get_direction(arg);

if(dir == GI_DIRECTION_IN || dir == GI_DIRECTION_INOUT) {
if(!Args::ToGType(args[real_arg_idx], &in_args[in_c], arg, NULL, FALSE)) {
return BAD_ARGS("IN arguments conversion failed");
gchar *instance_desc = Util::utf8StringFromValue(args[real_arg_idx]);
gchar *exc_msg = g_strdup_printf("Failed to convert argument %d \"%s\" to GI Type tag \"%s\"",
in_c, instance_desc, g_type_tag_to_string(g_type_info_get_tag(arg_type_info)));
g_free(instance_desc);
return BAD_ARGS(exc_msg);
}
//printf("IN ARG (%d) '%s' \n", in_c, in_args[in_c].v_string);
in_c++;
}
if(dir == GI_DIRECTION_OUT || dir == GI_DIRECTION_INOUT) {
if(!Args::ToGType(args[real_arg_idx], &out_args[out_c], arg, NULL, TRUE)) {
return BAD_ARGS("OUT arguments conversion failed");
gchar *instance_desc = Util::utf8StringFromValue(args[real_arg_idx]);
gchar *exc_msg = g_strdup_printf("Failed to convert output %d \"%s\" to GI Type tag \"%s\"",
out_c, instance_desc, g_type_tag_to_string(g_type_info_get_tag(arg_type_info)));
g_free(instance_desc);
return BAD_ARGS(exc_msg);
}
out_c++;
}
g_base_info_unref(arg);
g_base_info_unref(arg_type_info);
}

GError *error = NULL;
Expand Down
15 changes: 14 additions & 1 deletion src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <glib.h>

static char *_format_message(const char *fmt, va_list args)
{
Expand All @@ -28,3 +27,17 @@ extern "C" void debug_printf(const char *fmt, ...)
printf("%s", msg);
g_free(msg);
}

namespace gir {
namespace Util {

gchar *utf8StringFromValue(v8::Handle<v8::Value> value)
{
v8::Local<v8::String> string = value->ToString();
gchar *buffer = g_new(gchar, string->Utf8Length());
string->WriteUtf8(buffer);
return buffer;
}

}
}
13 changes: 12 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#ifndef GIR_UTIL_H
#define GIR_UTIL_H

#include <glib.h>
#include <v8.h>

#define GIR_DEFINE_CONSTANT(target, name, constant) \
(target)->Set(v8::String::NewSymbol(name), \
v8::Integer::New(constant), \
static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete))

#define BAD_ARGS(_msg) \
ThrowException(Exception::Error(String::New(_msg)));
ThrowException(Exception::TypeError(String::New(_msg)));

#define NO_UNDERLYING_OBJECT() \
ThrowException(Exception::Error(String::New("no underlying object found")));
Expand All @@ -17,4 +20,12 @@

extern "C" void debug_printf(const char *fmt, ...);

namespace gir {

namespace Util {
gchar *utf8StringFromValue(v8::Handle<v8::Value> value);
}

}

#endif

0 comments on commit c9722a5

Please sign in to comment.