1+
2+ #include " ../runtime.h"
3+ #if RUNTIME_IOS
4+ #import < Foundation/Foundation.h>
5+ #import < os/log.h>
6+
7+ #include < sys/stat.h>
8+ #include < sys/mman.h>
9+
10+
11+ #define PRINT (...) do { printf (__VA_ARGS__); } while (0 );
12+
13+ static print_log print_log_callback;
14+
15+ /* These are not in public headers */
16+ typedef unsigned char * (*MonoLoadAotDataFunc) (MonoAssembly *assembly, int size, void *user_data, void **out_handle);
17+ typedef void (*MonoFreeAotDataFunc) (MonoAssembly *assembly, int size, void *user_data, void *handle);
18+ void mono_install_load_aot_data_hook (MonoLoadAotDataFunc load_func, MonoFreeAotDataFunc free_func, void *user_data);
19+
20+ typedef enum {
21+ MONO_AOT_MODE_NONE,
22+ MONO_AOT_MODE_NORMAL,
23+ MONO_AOT_MODE_HYBRID,
24+ MONO_AOT_MODE_FULL,
25+ MONO_AOT_MODE_LLVMONLY,
26+ MONO_AOT_MODE_INTERP,
27+ MONO_AOT_MODE_INTERP_LLVMONLY
28+ } MonoAotMode;
29+
30+ void mono_jit_set_aot_mode (MonoAotMode mode);
31+
32+ extern void mono_ee_interp_init (const char *);
33+ extern void mono_icall_table_init (void );
34+ extern void mono_marshal_ilgen_init (void );
35+ extern void mono_method_builder_ilgen_init (void );
36+ extern void mono_sgen_mono_ilgen_init (void );
37+
38+ const char * runtime_bundle_path (void );
39+
40+ void mono_ios_setup_execution_mode (void )
41+ {
42+ mono_icall_table_init ();
43+ mono_marshal_ilgen_init ();
44+ mono_method_builder_ilgen_init ();
45+ mono_sgen_mono_ilgen_init ();
46+ mono_ee_interp_init (0 );
47+ mono_jit_set_aot_mode (MONO_AOT_MODE_INTERP);
48+ }
49+
50+ void set_log_callback (print_log callback)
51+ {
52+ print_log_callback = callback;
53+ }
54+
55+ static unsigned char *
56+ load_aot_data (MonoAssembly *assembly, int size, void *user_data, void **out_handle)
57+ {
58+ *out_handle = NULL ;
59+
60+ char path [1024 ];
61+ int res;
62+
63+ MonoAssemblyName *assembly_name = mono_assembly_get_name (assembly);
64+ const char *aname = mono_assembly_name_get_name (assembly_name);
65+ const char *bundle = runtime_bundle_path ();
66+
67+ // LOG (PRODUCT ": Looking for aot data for assembly '%s'.", name);
68+ res = snprintf (path, sizeof (path) - 1 , " %s /%s .aotdata" , bundle, aname);
69+ assert (res > 0 );
70+
71+ int fd = open (path, O_RDONLY);
72+ if (fd < 0 ) {
73+ // LOG (PRODUCT ": Could not load the aot data for %s from %s: %s\n", aname, path, strerror (errno));
74+ return NULL ;
75+ }
76+
77+ void *ptr = mmap (NULL , size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0 );
78+ if (ptr == MAP_FAILED) {
79+ // LOG (PRODUCT ": Could not map the aot file for %s: %s\n", aname, strerror (errno));
80+ close (fd);
81+ return NULL ;
82+ }
83+
84+ close (fd);
85+
86+ // LOG (PRODUCT ": Loaded aot data for %s.\n", name);
87+
88+ *out_handle = ptr;
89+
90+ return (unsigned char *) ptr;
91+ }
92+
93+ static void
94+ free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle)
95+ {
96+ munmap (handle, size);
97+ }
98+
99+ void
100+ log_callback (const char *log_domain, const char *log_level, const char *message, mono_bool fatal, void *user_data)
101+ {
102+ NSLog (@" (%s %s ) %s " , log_domain, log_level, message);
103+ if (fatal) {
104+ exit (1 );
105+ }
106+ }
107+
108+ static void
109+ register_dllmap (void )
110+ {
111+ mono_dllmap_insert (NULL , " System.Native" , NULL , " __Internal" , NULL );
112+ mono_dllmap_insert (NULL , " System.Security.Cryptography.Native.Apple" , NULL , " __Internal" , NULL );
113+ }
114+
115+ /* Implemented by generated code */
116+ void mono_ios_register_modules (void );
117+
118+ void
119+ mono_ios_runtime_init (void )
120+ {
121+ register_dllmap ();
122+
123+ #if TARGET_IPHONE_SIMULATOR// 模拟器
124+
125+ #elif TARGET_OS_IPHONE// 真机
126+ mono_ios_register_modules ();
127+ mono_ios_setup_execution_mode ();
128+
129+ #endif
130+
131+ mono_install_load_aot_data_hook (load_aot_data, free_aot_data, NULL );
132+ mono_trace_set_log_handler (log_callback, NULL );
133+
134+ // setenv ("MONO_LOG_LEVEL", "debug", TRUE);
135+
136+ // mono_jit_init_version ("Mono.ios", "mobile");
137+ }
138+
139+ char * doc_path = NULL ;
140+ const char *
141+ get_documents_path (void )
142+ {
143+ if (doc_path)
144+ return doc_path;
145+
146+ #if RUNTIME_IOS
147+ NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
148+ NSString *path = [paths objectAtIndex : 0 ];
149+ doc_path = strdup ([path UTF8String ]);
150+ #else
151+ if ((doc_path = _getcwd (NULL , 0 )) == NULL )
152+ {
153+ perror (" getcwd error" );
154+ }
155+ else
156+ {
157+ printf (" doc_path=%s \n " , doc_path);
158+ }
159+ #endif
160+ return doc_path;
161+ }
162+
163+ // #endif
0 commit comments