Embeddinator-4000/support/mono_embeddinator.c

266 строки
6.9 KiB
C
Исходник Обычный вид История

/*
* Mono managed-to-native support code.
*
* Author:
* Joao Matos (joao.matos@xamarin.com)
*
* (C) 2016 Microsoft, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "mono_embeddinator.h"
#include "glib.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
#if defined(__OBJC__)
#include <objc/runtime.h>
#endif
#ifdef _WIN32
#include <Windows.h>
#endif
#include <mono/jit/jit.h>
#include <mono/metadata/mono-config.h>
2016-09-29 20:18:40 +03:00
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
mono_embeddinator_context_t* _current_context;
mono_embeddinator_context_t* mono_embeddinator_get_context()
{
return _current_context;
}
void mono_embeddinator_set_context(mono_embeddinator_context_t* ctx)
{
_current_context = ctx;
}
int mono_embeddinator_init(mono_embeddinator_context_t* ctx, const char* domain)
{
if (ctx == 0 || ctx->domain != 0)
return false;
mono_config_parse(NULL);
ctx->domain = mono_jit_init_version(domain, "v4.0.30319");
mono_embeddinator_set_context(ctx);
return true;
}
int mono_embeddinator_destroy(mono_embeddinator_context_t* ctx)
{
if (ctx == 0 || ctx->domain != 0)
return false;
mono_jit_cleanup (ctx->domain);
return true;
}
static GString* path_override = NULL;
void mono_embeddinator_set_assembly_path (const char *path)
{
path_override = g_string_new (path);
}
static GString* get_current_executable_path()
{
if (path_override)
return path_override;
#if defined(__APPLE__)
char pathbuf [1024];
uint32_t bufsize = sizeof (pathbuf);
int ret = _NSGetExecutablePath (pathbuf, &bufsize);
return ret == 0 ? g_string_new (pathbuf) : 0;
#elif defined(_WIN32)
2016-10-19 15:24:06 +03:00
HMODULE hModule = GetModuleHandleW(0);
CHAR pathbuf[MAX_PATH];
DWORD ret = GetModuleFileNameA(hModule, pathbuf, MAX_PATH);
return (ret > 0) ? g_string_new(pathbuf) : 0;
#else
g_assert_not_reached();
#endif
}
static gchar* strrchr_seperator (const gchar* filename)
{
#ifdef G_OS_WIN32
char *p2;
#endif
char *p;
p = strrchr (filename, G_DIR_SEPARATOR);
#ifdef G_OS_WIN32
p2 = strrchr (filename, '/');
if (p2 > p)
p = p2;
#endif
return p;
}
char* mono_embeddinator_search_assembly(const char* assembly)
{
GString* path = get_current_executable_path();
gchar* sep = strrchr_seperator(path->str);
g_string_truncate(path, sep - path->str);
g_string_append(path, G_DIR_SEPARATOR_S);
g_string_append(path, assembly);
char* data = path->str;
g_string_free(path, /*free_segment=*/ FALSE);
return data;
}
MonoImage* mono_embeddinator_load_assembly(mono_embeddinator_context_t* ctx, const char* assembly)
{
char* path = mono_embeddinator_search_assembly(assembly);
MonoAssembly* mono_assembly = mono_domain_assembly_open(ctx->domain, path);
g_free (path);
if (!mono_assembly)
{
mono_embeddinator_error_t error;
error.type = MONO_EMBEDDINATOR_ASSEMBLY_OPEN_FAILED;
error.string = path;
mono_embeddinator_error(error);
return 0;
}
return mono_assembly_get_image(mono_assembly);
}
static mono_embeddinator_assembly_search_hook_t g_assembly_search_hook = 0;
void* mono_embeddinator_install_assembly_search_hook(mono_embeddinator_assembly_search_hook_t hook)
{
mono_embeddinator_assembly_search_hook_t prev = g_assembly_search_hook;
g_assembly_search_hook = hook;
2016-09-29 20:18:40 +03:00
return (void*)prev;
}
MonoClass* mono_embeddinator_search_class(const char* assembly, const char* _namespace,
const char* name)
{
mono_embeddinator_context_t* ctx = mono_embeddinator_get_context();
char* path = mono_embeddinator_search_assembly(assembly);
2016-09-29 20:18:40 +03:00
MonoAssembly* mono_assembly = mono_domain_assembly_open(ctx->domain, path);
g_free (path);
2016-09-29 20:18:40 +03:00
if (mono_assembly == 0)
{
mono_embeddinator_error_t error;
error.type = MONO_EMBEDDINATOR_ASSEMBLY_OPEN_FAILED;
2016-09-29 20:18:40 +03:00
error.string = path;
mono_embeddinator_error(error);
}
2016-09-29 20:18:40 +03:00
MonoImage* image = mono_assembly_get_image(mono_assembly);
MonoClass* klass = mono_class_from_name(image, _namespace, name);
return klass;
}
MonoMethod* mono_embeddinator_lookup_method(const char* method_name, MonoClass *klass)
{
MonoMethodDesc* desc = mono_method_desc_new(method_name, /*include_namespace=*/true);
MonoMethod* method = mono_method_desc_search_in_class(desc, klass);
mono_method_desc_free(desc);
if (!method)
{
mono_embeddinator_error_t error;
error.type = MONO_EMBEDDINATOR_METHOD_LOOKUP_FAILED;
error.string = method_name;
mono_embeddinator_error(error);
}
return method;
}
void mono_embeddinator_throw_exception(MonoObject *exception)
{
mono_embeddinator_error_t error;
error.type = MONO_EMBEDDINATOR_EXCEPTION_THROWN;
error.exception = (MonoException*) exception;
mono_embeddinator_error(error);
}
static mono_embeddinator_error_report_hook_t g_error_report_hook = 0;
void* mono_embeddinator_install_error_report_hook(mono_embeddinator_error_report_hook_t hook)
{
mono_embeddinator_error_report_hook_t prev = g_error_report_hook;
g_error_report_hook = hook;
return prev;
}
void mono_embeddinator_error(mono_embeddinator_error_t error)
{
if (g_error_report_hook == 0)
return;
g_error_report_hook(error);
2016-10-19 15:24:06 +03:00
}
void* mono_embeddinator_create_object(MonoObject* instance)
2016-10-19 15:24:06 +03:00
{
MonoEmbedObject* object = g_new(MonoEmbedObject, 1);
mono_embeddinator_init_object(object, instance);
return object;
}
void mono_embeddinator_init_object(MonoEmbedObject* object, MonoObject* instance)
{
2016-10-19 15:24:06 +03:00
object->_class = mono_object_get_class(instance);
object->_handle = mono_gchandle_new(instance, /*pinned=*/false);
[support] Fix creation of the NSAutoreleasePool Current code crash like this: 2017-03-24 15:52:02.732 a.out[92593:125062863] -[NSAutoreleasePool new]: unrecognized selector sent to instance 0x7fa2d6512c50 2017-03-24 15:52:02.733 a.out[92593:125062863] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAutoreleasePool new]: unrecognized selector sent to instance 0x7fa2d6512c50' *** First throw call stack: ( 0 CoreFoundation 0x00007fffd1e10e7b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00007fffe6a0bcad objc_exception_throw + 48 2 CoreFoundation 0x00007fffd1e92cb4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x00007fffd1d82fb5 ___forwarding___ + 1061 4 CoreFoundation 0x00007fffd1d82b08 _CF_forwarding_prep_0 + 120 5 libproperties.dylib 0x0000000106b6f5eb allocAndInitAutoreleasePool + 75 6 libproperties.dylib 0x0000000106b6f6b7 mono_m2n_init + 119 7 a.out 0x0000000106b64eb0 main + 64 8 libdyld.dylib 0x0000000107169255 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException Stacktrace: Native stacktrace: 0 libmonosgen-2.0.1.dylib 0x0000000106c47d6a mono_handle_native_sigsegv + 282 1 libsystem_platform.dylib 0x00007fffe74fcbba _sigtramp + 26 2 ??? 0x00000003f011f240 0x0 + 16912609856 3 libsystem_c.dylib 0x00007fffe7383420 abort + 129 4 libc++abi.dylib 0x00007fffe5ede85a __cxa_bad_cast + 0 5 libc++abi.dylib 0x00007fffe5f03c4f _ZL26default_unexpected_handlerv + 0 6 libobjc.A.dylib 0x00007fffe6a0df1e _ZL15_objc_terminatev + 103 7 libc++abi.dylib 0x00007fffe5f00d69 _ZSt11__terminatePFvvE + 8 8 libc++abi.dylib 0x00007fffe5f007de _ZN10__cxxabiv1L22exception_cleanup_funcE19_Unwind_Reason_CodeP17_Unwind_Exception + 0 9 libobjc.A.dylib 0x00007fffe6a0bdd6 objc_exception_throw + 345 10 CoreFoundation 0x00007fffd1e92cb4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 11 CoreFoundation 0x00007fffd1d82fb5 ___forwarding___ + 1061 12 CoreFoundation 0x00007fffd1d82b08 _CF_forwarding_prep_0 + 120 13 libproperties.dylib 0x0000000106b6f5eb allocAndInitAutoreleasePool + 75 14 libproperties.dylib 0x0000000106b6f6b7 mono_m2n_init + 119 15 a.out 0x0000000106b64eb0 main + 64 16 libdyld.dylib 0x0000000107169255 start + 1 `new` is a static method, but the class is already [alloc]ated by `class_createInstance`. Simply calling "init" is enough.
2017-03-25 00:05:52 +03:00
}
void mono_embeddinator_destroy_object(MonoEmbedObject* object)
{
mono_gchandle_free (object->_handle);
g_free (object);
}