forked from loongly/PureScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.c
More file actions
276 lines (218 loc) · 6.31 KB
/
Copy pathruntime.c
File metadata and controls
276 lines (218 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "runtime.h"
#if !RUNTIME_IOS
#include <windows.h>
#include <direct.h>
#endif
#include "mono/jit/jit.h"
#include "mono/metadata/environment.h"
#include "mono/utils/mono-publib.h"
#include "mono/utils/mono-logger.h"
#include "mono/metadata/assembly.h"
#include "mono/metadata/mono-debug.h"
#include "mono/metadata/exception.h"
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
typedef char bool;
#define false 0
#define true 1
extern char* mono_runtime_bundle_path;
void* g_manageFuncPtr;
MonoDomain *g_domain;
bool
file_exists(const char *path)
{
struct stat buffer;
return stat(path, &buffer) == 0;
}
char *
strdup_printf(const char *msg, ...)
{
va_list args;
char *formatted = NULL;
va_start(args, msg);
printf(formatted, msg, args);
va_end(args);
return formatted;
}
const char *
get_bundle_path (void);
const char *
get_documents_path (void);
const char *
runtime_bundle_path(void)
{
if(mono_runtime_bundle_path != NULL)
return mono_runtime_bundle_path;
return get_bundle_path();
}
MonoAssembly*
load_assembly(const char *name, const char *culture)
{
const char *bundle = runtime_bundle_path();
char path[1024];
int res;
const char *post = name + strlen(name) -4;
printf("load_assembly: %s %s %s\n", name, culture, bundle);
if (culture && strcmp(culture, ""))
res = snprintf(path, sizeof(path) - 1, "%s/Managed/%s/%s", bundle, culture, name);
else if(strcmp(post, ".dll") == 0 || strcmp(post, ".exe") == 0)
res = snprintf(path, sizeof(path) - 1, "%s/Managed/%s", bundle, name);
else
res = snprintf(path, sizeof(path) - 1, "%s/Managed/%s.dll", bundle, name);
assert(res > 0);
/*if (!file_exists(path))
{
const char *documents = get_documents_path();
res = snprintf(path, sizeof(path) - 1, "%s/Managed/%s", documents, name);
assert(res > 0);
}*/
printf("load_assembly load path: %s \n", path);
if (file_exists(path)) {
MonoAssembly *assembly = mono_assembly_open(path, NULL);
assert(assembly);
return assembly;
}
return NULL;
}
static void main_function (MonoDomain *domain, const char *file, int argc, char** argv)
{
//assembly = mono_domain_assembly_open (domain, file);
load_assembly("AdapterWrapper.dll", NULL);
MonoAssembly *assembly = load_assembly(file, NULL);
if (!assembly)
exit (2);
/*
* mono_jit_exec() will run the Main() method in the assembly.
* The return value needs to be looked up from
* System.Environment.ExitCode.
*/
mono_jit_exec (domain, assembly, argc, argv);
}
static MonoAssembly*
assembly_preload_hook(MonoAssemblyName *aname, char **assemblies_path, void* user_data)
{
const char *name = mono_assembly_name_get_name(aname);
const char *culture = mono_assembly_name_get_culture(aname);
return load_assembly(name, culture);
}
void
log_callback_default(const char *log_domain, const char *log_level, const char *message, mono_bool fatal, void *user_data)
{
printf("(%s %s) %s", log_domain, log_level, message);
if (fatal) {
printf("Exit code: %d.", 1);
exit(1);
}
}
MonoObject *
mono_exception_property(MonoObject *obj, const char *name, char is_virtual)
{
MonoMethod *get = NULL;
MonoMethod *get_virt = NULL;
MonoObject *exc = NULL;
get = mono_class_get_method_from_name(mono_get_exception_class(), name, 0);
if (get) {
if (is_virtual) {
get_virt = mono_object_get_virtual_method(obj, get);
if (get_virt)
get = get_virt;
}
return (MonoObject *)mono_runtime_invoke(get, obj, NULL, &exc);
}
else {
printf("Could not find the property System.Exception.%s", name);
}
return NULL;
}
static char *
fetch_exception_property_string(MonoObject *obj, const char *name, bool is_virtual)
{
MonoString *str = (MonoString *)mono_exception_property(obj, name, is_virtual);
return str ? mono_string_to_utf8(str) : NULL;
}
void
unhandled_exception_handler(MonoObject *exc, void *user_data)
{
//NSMutableString *msg = [[NSMutableString alloc] init];
MonoClass *type = mono_object_get_class(exc);
const char* type_name = mono_class_get_name(type);
//char *type_name = strdup_printf("%s.%s", mono_class_get_namespace(type), mono_class_get_name(type));
char *trace = fetch_exception_property_string(exc, "get_StackTrace", true);
char *message = fetch_exception_property_string(exc, "get_Message", true);
printf("Unhandled managed exception:\n");
printf("%s (%s)\n%s\n", message, type_name, trace ? trace : "");
if (trace != NULL)
mono_free(trace);
if (message != NULL)
mono_free(message);
//os_log_info (OS_LOG_DEFAULT, "%@", msg);
printf("Exit code: %d.", 1);
exit(1);
}
/*
static int malloc_count = 0;
static void* custom_malloc(size_t bytes)
{
++malloc_count;
return malloc(bytes);
}*/
/* Implemented by generated code */
void mono_register_icall(void);
#if RUNTIME_IOS
void mono_ios_runtime_init(void);
#endif
void mono_debug() {
mono_debug_init(MONO_DEBUG_FORMAT_MONO);
static const char* options[] = {
"--soft-breakpoints",
"--debugger-agent=transport=dt_socket,address=127.0.0.1:10001,embedding=1,server=y,suspend=n"
};
mono_jit_parse_options(sizeof(options) / sizeof(char*), (char**)options);
}
int
mono_setup(char* bundleDir, const char* file) {
mono_runtime_bundle_path = NULL;
//mono_runtime_bundle_path = strdup(bundleDir);
int retval = 0;
//MonoAllocatorVTable mem_vtable = {custom_malloc};
//mono_set_allocator_vtable (&mem_vtable);
mono_install_assembly_preload_hook(assembly_preload_hook, NULL);
mono_install_unhandled_exception_hook(unhandled_exception_handler, NULL);
mono_trace_set_log_handler(log_callback_default, NULL);
mono_set_signal_chaining(true);
mono_set_crash_chaining(true);
mono_debug();
/*
* Load the default Mono configuration file, this is needed
* if you are planning on using the dllmaps defined on the
* system configuration
*/
//mono_config_parse (NULL);
/*
* mono_jit_init() creates a domain: each assembly is
* loaded and run in a MonoDomain.
*/
#if RUNTIME_IOS
mono_ios_runtime_init();
#endif
g_domain = mono_jit_init (file);
/*
* We add our special internal call, so that C# code
* can call us back.
*/
mono_register_icall();
char *managed_argv[2];
managed_argv[0] = file;
managed_argv[1] = file;
main_function (g_domain, file, 2, managed_argv);
return retval;
}
int mono_exit()
{
int retval = mono_environment_exitcode_get ();
mono_jit_cleanup (mono_domain_get());
return retval;
}