Skip to content

Commit fc7bda6

Browse files
jaykrelllambdageek
authored andcommitted
[coop] Convert some mono_exception_* functions
Reduce managed exposure in utils a little. [coop] install object-forward.h
1 parent e2874db commit fc7bda6

File tree

14 files changed

+227
-9
lines changed

14 files changed

+227
-9
lines changed

mono/metadata/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ libmonoruntimeinclude_HEADERS = \
347347
mono-debug.h \
348348
mono-gc.h \
349349
object.h \
350+
object-forward.h \
350351
opcodes.h \
351352
profiler.h \
352353
profiler-events.h \

mono/metadata/exception.c

Lines changed: 129 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@
3434
static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
3535
static gpointer unhandled_exception_hook_data = NULL;
3636

37+
static MonoExceptionHandle
38+
mono_exception_new_by_name_domain (MonoDomain *domain, MonoImage *image,
39+
const char* name_space, const char *name, MonoError *error);
40+
41+
/**
42+
* mono_exception_new_by_name:
43+
* \param image the Mono image where to look for the class
44+
* \param name_space the namespace for the class
45+
* \param name class name
46+
*
47+
* Creates an exception of the given namespace/name class in the
48+
* current domain.
49+
*
50+
* \returns the initialized exception instance.
51+
*/
52+
static MonoExceptionHandle
53+
mono_exception_new_by_name (MonoImage *image, const char *name_space, const char *name, MonoError *error)
54+
{
55+
return mono_exception_new_by_name_domain (mono_domain_get (), image, name_space, name, error);
56+
}
57+
3758
/**
3859
* mono_exception_from_name:
3960
* \param image the Mono image where to look for the class
@@ -52,6 +73,48 @@ mono_exception_from_name (MonoImage *image, const char *name_space,
5273
return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
5374
}
5475

76+
/**
77+
* mono_exception_new_by_name_domain:
78+
* \param domain Domain where the return object will be created.
79+
* \param image the Mono image where to look for the class
80+
* \param name_space the namespace for the class
81+
* \param name class name
82+
*
83+
* Creates an exception object of the given namespace/name class on
84+
* the given domain.
85+
*
86+
* \returns the initialized exception instance.
87+
*/
88+
static MonoExceptionHandle
89+
mono_exception_new_by_name_domain (MonoDomain *domain, MonoImage *image,
90+
const char* name_space, const char *name, MonoError *error)
91+
{
92+
HANDLE_FUNCTION_ENTER ()
93+
94+
MonoDomain * const caller_domain = mono_domain_get ();
95+
96+
MonoClass * const klass = mono_class_load_from_name (image, name_space, name);
97+
98+
MonoObjectHandle o = MONO_HANDLE_NEW (MonoObject, mono_object_new_checked (domain, klass, error));
99+
goto_if_nok (error, return_null);
100+
101+
if (domain != caller_domain)
102+
mono_domain_set_internal (domain);
103+
104+
mono_runtime_object_init_handle (o, error);
105+
mono_error_assert_ok (error);
106+
107+
// Restore domain in success and error path.
108+
if (domain != caller_domain)
109+
mono_domain_set_internal (caller_domain);
110+
111+
goto_if_ok (error, exit);
112+
return_null:
113+
MONO_HANDLE_ASSIGN (o, NULL);
114+
exit:
115+
HANDLE_FUNCTION_RETURN_REF (MonoException, o);
116+
}
117+
55118
/**
56119
* mono_exception_from_name_domain:
57120
* \param domain Domain where the return object will be created.
@@ -215,6 +278,40 @@ mono_exception_from_name_two_strings_checked (MonoImage *image, const char *name
215278
return create_exception_two_strings (klass, a1, a2, error);
216279
}
217280

281+
/**
282+
* mono_exception_new_by_name_msg:
283+
* \param image the Mono image where to look for the class
284+
* \param name_space the namespace for the class
285+
* \param name class name
286+
* \param msg the message to embed inside the exception
287+
*
288+
* Creates an exception and initializes its message field.
289+
*
290+
* \returns the initialized exception instance.
291+
*/
292+
MonoExceptionHandle
293+
mono_exception_new_by_name_msg (MonoImage *image, const char *name_space,
294+
const char *name, const char *msg, MonoError *error)
295+
{
296+
HANDLE_FUNCTION_ENTER ()
297+
298+
MonoExceptionHandle ex = mono_exception_new_by_name (image, name_space, name, error);
299+
goto_if_nok (error, return_null);
300+
301+
if (msg) {
302+
MonoStringHandle msg_str = mono_string_new_handle (MONO_HANDLE_DOMAIN (ex), msg, error);
303+
// FIXME? Maybe just ignore this error, the exception is close to correct.
304+
goto_if_nok (error, return_null);
305+
// ex->message = msg_str;
306+
MONO_HANDLE_SET (ex, message, msg_str);
307+
}
308+
goto exit;
309+
return_null:
310+
MONO_HANDLE_ASSIGN (ex, NULL);
311+
exit:
312+
HANDLE_FUNCTION_RETURN_REF (MonoException, ex)
313+
}
314+
218315
/**
219316
* mono_exception_from_name_msg:
220317
* \param image the Mono image where to look for the class
@@ -304,6 +401,16 @@ mono_get_exception_security ()
304401
"SecurityException");
305402
}
306403

404+
/**
405+
* mono_exception_new_thread_abort:
406+
* \returns a new instance of the \c System.Threading.ThreadAbortException
407+
*/
408+
MonoExceptionHandle
409+
mono_exception_new_thread_abort (MonoError *error)
410+
{
411+
return mono_exception_new_by_name (mono_get_corlib (), "System.Threading", "ThreadAbortException", error);
412+
}
413+
307414
/**
308415
* mono_get_exception_thread_abort:
309416
* \returns a new instance of the \c System.Threading.ThreadAbortException
@@ -315,6 +422,16 @@ mono_get_exception_thread_abort ()
315422
"ThreadAbortException");
316423
}
317424

425+
/**
426+
* mono_exception_new_thread_interrupted:
427+
* \returns a new instance of the \c System.Threading.ThreadInterruptedException
428+
*/
429+
MonoExceptionHandle
430+
mono_exception_new_thread_interrupted (MonoError *error)
431+
{
432+
return mono_exception_new_by_name (mono_get_corlib (), "System.Threading", "ThreadInterruptedException", error);
433+
}
434+
318435
/**
319436
* mono_get_exception_thread_interrupted:
320437
* \returns a new instance of the \c System.Threading.ThreadInterruptedException
@@ -403,6 +520,12 @@ mono_get_exception_invalid_operation (const char *msg)
403520
"InvalidOperationException", msg);
404521
}
405522

523+
MonoExceptionHandle
524+
mono_exception_new_invalid_operation (const char *msg, MonoError *error)
525+
{
526+
return mono_exception_new_by_name_msg (mono_get_corlib (), "System", "InvalidOperationException", msg, error);
527+
}
528+
406529
/**
407530
* mono_get_exception_index_out_of_range:
408531
* \returns a new instance of the \c System.IndexOutOfRangeException
@@ -589,6 +712,12 @@ mono_get_exception_argument_out_of_range (const char *arg)
589712
* \param msg the message to present to the user
590713
* \returns a new instance of the \c System.Threading.ThreadStateException
591714
*/
715+
MonoExceptionHandle
716+
mono_exception_new_thread_state (const char *msg, MonoError *error)
717+
{
718+
return mono_exception_new_by_name_msg (mono_get_corlib (), "System.Threading", "ThreadStateException", msg, error);
719+
}
720+
592721
MonoException *
593722
mono_get_exception_thread_state (const char *msg)
594723
{
@@ -850,10 +979,7 @@ mono_get_exception_reflection_type_load (MonoArray *types_raw, MonoArray *except
850979
if (is_ok (error)) {
851980
mono_error_cleanup (error);
852981
ret = MONO_HANDLE_CAST (MonoException, NULL_HANDLE);
853-
goto leave;
854982
}
855-
856-
leave:
857983
HANDLE_FUNCTION_RETURN_OBJ (ret);
858984

859985
}

mono/metadata/exception.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef _MONO_METADATA_EXCEPTION_H_
66
#define _MONO_METADATA_EXCEPTION_H_
77

8+
#include <mono/metadata/object-forward.h>
89
#include <mono/metadata/object.h>
910
#include <mono/metadata/image.h>
1011

mono/metadata/handle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <mono/utils/mono-error-internals.h>
2323
#include <mono/utils/mono-threads.h>
2424
#include <mono/utils/checked-build.h>
25+
#include <mono/metadata/class-internals.h>
2526

2627
G_BEGIN_DECLS
2728

@@ -208,18 +209,21 @@ Icall macros
208209
CLEAR_ICALL_FRAME; \
209210
} while (0)
210211

212+
// Return a non-pointer or non-managed pointer, e.g. gboolean.
211213
#define HANDLE_FUNCTION_RETURN_VAL(VAL) \
212214
CLEAR_ICALL_FRAME; \
213215
return (VAL); \
214216
} while (0)
215217

218+
// Return a raw pointer from coop handle.
216219
#define HANDLE_FUNCTION_RETURN_OBJ(HANDLE) \
217220
do { \
218221
void* __result = MONO_HANDLE_RAW (HANDLE); \
219222
CLEAR_ICALL_FRAME; \
220223
return __result; \
221224
} while (0); } while (0);
222225

226+
// Return a coop handle from coop handle.
223227
#define HANDLE_FUNCTION_RETURN_REF(TYPE, HANDLE) \
224228
do { \
225229
MonoRawHandle __result; \

mono/metadata/image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include <stdio.h>
99
#include <mono/utils/mono-publib.h>
1010
#include <mono/utils/mono-error.h>
11+
#include <mono/metadata/object-forward.h>
1112

1213
MONO_BEGIN_DECLS
1314

14-
typedef struct _MonoImage MonoImage;
1515
typedef struct _MonoAssembly MonoAssembly;
1616
typedef struct _MonoAssemblyName MonoAssemblyName;
1717
typedef struct _MonoTableInfo MonoTableInfo;

mono/metadata/metadata.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <mono/metadata/blob.h>
1111
#include <mono/metadata/row-indexes.h>
1212
#include <mono/metadata/image.h>
13+
#include <mono/metadata/object-forward.h>
1314

1415
MONO_BEGIN_DECLS
1516

@@ -22,9 +23,7 @@ MONO_BEGIN_DECLS
2223

2324
#define MONO_CLASS_IS_IMPORT(c) ((mono_class_get_flags (c) & TYPE_ATTRIBUTE_IMPORT))
2425

25-
typedef struct _MonoClass MonoClass;
2626
typedef struct _MonoDomain MonoDomain;
27-
typedef struct _MonoMethod MonoMethod;
2827

2928
typedef enum {
3029
MONO_EXCEPTION_CLAUSE_NONE,

mono/metadata/object-forward.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
#ifndef __MONO_OBJECT_FORWARD_H__
99
#define __MONO_OBJECT_FORWARD_H__
1010

11+
#include <mono/utils/mono-publib.h>
12+
1113
typedef struct _MonoReflectionTypeBuilder MonoReflectionTypeBuilder;
14+
typedef struct _MonoException MONO_RT_MANAGED_ATTR MonoException;
15+
typedef struct _MonoClass MonoClass;
16+
typedef struct _MonoImage MonoImage;
17+
typedef struct _MonoMethod MonoMethod;
18+
1219

1320
#endif /* __MONO_OBJECT_FORWARD_H__ */

mono/metadata/object-internals.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,4 +1959,7 @@ ves_icall_ModuleBuilder_set_wrappers_type (MonoReflectionModuleBuilderHandle mod
19591959
MonoAssembly*
19601960
mono_try_assembly_resolve_handle (MonoDomain *domain, MonoStringHandle fname, MonoAssembly *requesting, gboolean refonly, MonoError *error);
19611961

1962+
gboolean
1963+
mono_runtime_object_init_handle (MonoObjectHandle this_obj, MonoError *error);
1964+
19621965
#endif /* __MONO_OBJECT_INTERNALS_H__ */

mono/metadata/object.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,42 @@ mono_runtime_object_init (MonoObject *this_obj)
102102
mono_error_assert_ok (error);
103103
}
104104

105+
/**
106+
* mono_runtime_object_init_checked:
107+
* \param this_obj the object to initialize
108+
* \param error set on error.
109+
* This function calls the zero-argument constructor (which must
110+
* exist) for the given object and returns TRUE on success, or FALSE
111+
* on error and sets \p error.
112+
*/
113+
gboolean
114+
mono_runtime_object_init_handle (MonoObjectHandle this_obj, MonoError *error)
115+
{
116+
MONO_REQ_GC_UNSAFE_MODE;
117+
118+
error_init (error);
119+
120+
MonoClass * const klass = MONO_HANDLE_GETVAL (this_obj, vtable)->klass;
121+
MonoMethod * const method = mono_class_get_method_from_name (klass, ".ctor", 0);
122+
g_assertf (method, "Could not lookup zero argument constructor for class %s", mono_type_get_full_name (klass));
123+
124+
guint gchandle = 0;
125+
gpointer raw;
126+
127+
if (method->klass->valuetype) {
128+
raw = mono_object_handle_pin_unbox (this_obj, &gchandle);
129+
} else {
130+
gchandle = mono_gchandle_from_handle (this_obj, TRUE);
131+
raw = MONO_HANDLE_RAW (this_obj);
132+
}
133+
134+
mono_runtime_invoke_checked (method, raw, NULL, error);
135+
136+
mono_gchandle_free (gchandle);
137+
138+
return is_ok (error);
139+
}
140+
105141
/**
106142
* mono_runtime_object_init_checked:
107143
* \param this_obj the object to initialize

mono/metadata/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef _MONO_CLI_OBJECT_H_
66
#define _MONO_CLI_OBJECT_H_
77

8+
#include <mono/metadata/object-forward.h>
89
#include <mono/metadata/class.h>
910
#include <mono/utils/mono-error.h>
1011

@@ -22,7 +23,6 @@ typedef struct _MonoReflectionProperty MONO_RT_MANAGED_ATTR MonoReflectionProper
2223
typedef struct _MonoReflectionEvent MONO_RT_MANAGED_ATTR MonoReflectionEvent;
2324
typedef struct _MonoReflectionType MONO_RT_MANAGED_ATTR MonoReflectionType;
2425
typedef struct _MonoDelegate MONO_RT_MANAGED_ATTR MonoDelegate;
25-
typedef struct _MonoException MONO_RT_MANAGED_ATTR MonoException;
2626
typedef struct _MonoThreadsSync MonoThreadsSync;
2727
typedef struct _MonoThread MONO_RT_MANAGED_ATTR MonoThread;
2828
typedef struct _MonoDynamicAssembly MonoDynamicAssembly;

0 commit comments

Comments
 (0)