Skip to content

Commit

Permalink
2010-07-02 Michael Hutchinson <[email protected]>
Browse files Browse the repository at this point in the history
	* object-internals.h, object.h: Make mono_string_to_utf_8_checked 
	public, as mono_string_to_utf_8 is deprecated.

	* object.(c|h): Added mono_object_to_string, a public helper function 
	that calls ToString on a MonoObject and returns a MonoString.

	* object.c (mono_print_unhandled_exception): Use mono_object_to_string.

svn path=/trunk/mono/; revision=159843
  • Loading branch information
mhutch committed Jul 2, 2010
1 parent de12b15 commit aa958b7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
10 changes: 10 additions & 0 deletions mono/metadata/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2010-07-02 Michael Hutchinson <[email protected]>

* object-internals.h, object.h: Make mono_string_to_utf_8_checked
public, as mono_string_to_utf_8 is deprecated.

* object.(c|h): Added mono_object_to_string, a public helper function
that calls ToString on a MonoObject and returns a MonoString.

* object.c (mono_print_unhandled_exception): Use mono_object_to_string.

2010-07-01 Zoltan Varga <[email protected]>

* filewatcher.h filewatcher.h decimal.c threads-types.h: Applied patch
Expand Down
3 changes: 0 additions & 3 deletions mono/metadata/object-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1500,9 +1500,6 @@ mono_class_compute_gc_descriptor (MonoClass *class) MONO_INTERNAL;
MonoObject*
mono_object_xdomain_representation (MonoObject *obj, MonoDomain *target_domain, MonoObject **exc) MONO_INTERNAL;

char *
mono_string_to_utf8_checked (MonoString *s, MonoError *error) MONO_INTERNAL;

gboolean
mono_class_is_reflection_method_or_constructor (MonoClass *class) MONO_INTERNAL;

Expand Down
74 changes: 46 additions & 28 deletions mono/metadata/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -5227,8 +5227,8 @@ mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig)
* mono_string_to_utf8:
* @s: a System.String
*
* Return the UTF8 representation for @s.
* the resulting buffer nedds to be freed with g_free().
* Returns the UTF8 representation for @s.
* The resulting buffer needs to be freed with mono_free().
*
* @deprecated Use mono_string_to_utf8_checked to avoid having an exception arbritraly raised.
*/
Expand All @@ -5243,6 +5243,15 @@ mono_string_to_utf8 (MonoString *s)
return result;
}

/**
* mono_string_to_utf8_checked:
* @s: a System.String
* @error: a MonoError.
*
* Converts a MonoString to its UTF8 representation. May fail; check
* @error to determine whether the conversion was successful.
* The resulting buffer should be freed with mono_free().
*/
char *
mono_string_to_utf8_checked (MonoString *s, MonoError *error)
{
Expand Down Expand Up @@ -5697,6 +5706,29 @@ mono_message_invoke (MonoObject *target, MonoMethodMessage *msg,
return ret;
}

/**
* mono_object_to_string:
* @obj: The object
* @exc: Any exception thrown by ToString (). May be NULL.
*
* Returns: the result of calling ToString () on an object.
*/
MonoString *
mono_object_to_string (MonoObject *obj, MonoObject **exc)
{
static MonoMethod *to_string = NULL;
MonoMethod *method;

g_assert (obj);

if (!to_string)
to_string = mono_class_get_method_from_name_flags (mono_get_object_class (), "ToString", 0, METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_PUBLIC);

method = mono_object_get_virtual_method (obj, to_string);

return (MonoString *) mono_runtime_invoke (method, obj, NULL, exc);
}

/**
* mono_print_unhandled_exception:
* @exc: The exception
Expand All @@ -5706,35 +5738,21 @@ mono_message_invoke (MonoObject *target, MonoMethodMessage *msg,
void
mono_print_unhandled_exception (MonoObject *exc)
{
MonoError error;
char *message = (char *) "";
MonoString *str;
MonoMethod *method;
MonoClass *klass;
MonoString * str;
char *message = "";
gboolean free_message = FALSE;
MonoError error;

if (mono_object_isinst (exc, mono_defaults.exception_class)) {
klass = exc->vtable->klass;
method = NULL;
while (klass && method == NULL) {
method = mono_class_get_method_from_name_flags (klass, "ToString", 0, METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_PUBLIC);
if (method == NULL)
klass = klass->parent;
}

g_assert (method);

str = (MonoString *) mono_runtime_invoke (method, exc, NULL, NULL);
if (str) {
message = mono_string_to_utf8_checked (str, &error);
if (!mono_error_ok (&error)) {
mono_error_cleanup (&error);
message = (char *)"";
} else {
free_message = TRUE;
}
str = mono_object_to_string (exc, NULL);
if (str) {
message = mono_string_to_utf8_checked (str, &error);
if (!mono_error_ok (&error)) {
mono_error_cleanup (&error);
message = (char *) "";
} else {
free_message = TRUE;
}
}
}

/*
* g_printerr ("\nUnhandled Exception: %s.%s: %s\n", exc->vtable->klass->name_space,
Expand Down
7 changes: 7 additions & 0 deletions mono/metadata/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _MONO_CLI_OBJECT_H_

#include <mono/metadata/class.h>
#include <mono/utils/mono-error.h>

MONO_BEGIN_DECLS

Expand Down Expand Up @@ -129,6 +130,9 @@ mono_string_new_len (MonoDomain *domain, const char *text, unsigned int leng
char *
mono_string_to_utf8 (MonoString *string_obj);

char *
mono_string_to_utf8_checked (MonoString *string_obj, MonoError *error);

mono_unichar2 *
mono_string_to_utf16 (MonoString *string_obj);

Expand All @@ -144,6 +148,9 @@ mono_string_hash (MonoString *s);
int
mono_object_hash (MonoObject* obj);

MonoString *
mono_object_to_string (MonoObject *obj, MonoObject **exc);

MonoObject *
mono_value_box (MonoDomain *domain, MonoClass *klass, void* val);

Expand Down

0 comments on commit aa958b7

Please sign in to comment.