2016-09-01 01:08:37 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-11-06 21:25:09 +03:00
|
|
|
#include "mono_embeddinator.h"
|
2016-08-18 04:40:16 +03:00
|
|
|
#include "glib.h"
|
|
|
|
|
2016-09-01 01:08:37 +03:00
|
|
|
#include <stdbool.h>
|
2016-08-18 04:40:16 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <errno.h>
|
|
|
|
#include <libproc.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2016-09-30 19:03:54 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-23 00:33:04 +03:00
|
|
|
#include <mono/jit/jit.h>
|
|
|
|
#include <mono/metadata/mono-config.h>
|
2016-09-29 20:18:40 +03:00
|
|
|
#include <mono/metadata/assembly.h>
|
|
|
|
|
2016-09-28 18:26:31 +03:00
|
|
|
mono_m2n_context_t* _current_context;
|
|
|
|
|
|
|
|
mono_m2n_context_t* mono_m2n_get_context()
|
|
|
|
{
|
|
|
|
return _current_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mono_m2n_set_context(mono_m2n_context_t* ctx)
|
|
|
|
{
|
|
|
|
_current_context = ctx;
|
|
|
|
}
|
|
|
|
|
2016-08-23 00:33:04 +03:00
|
|
|
int mono_m2n_init(mono_m2n_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");
|
|
|
|
|
2016-09-28 18:26:31 +03:00
|
|
|
mono_m2n_set_context(ctx);
|
|
|
|
|
2016-08-23 00:33:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-01 01:08:37 +03:00
|
|
|
int mono_m2n_destroy(mono_m2n_context_t* ctx)
|
|
|
|
{
|
|
|
|
if (ctx == 0 || ctx->domain != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mono_jit_cleanup (ctx->domain);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-18 04:40:16 +03:00
|
|
|
static GString* get_current_executable_path()
|
|
|
|
{
|
2016-09-30 19:03:54 +03:00
|
|
|
#if defined(__APPLE__)
|
2016-08-18 04:40:16 +03:00
|
|
|
int ret;
|
|
|
|
pid_t pid;
|
|
|
|
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
|
|
|
|
|
|
|
|
pid = getpid();
|
|
|
|
ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
|
|
|
|
|
|
|
|
return (ret > 0) ? g_string_new(pathbuf) : 0;
|
2016-09-30 19:03:54 +03:00
|
|
|
#elif defined(_WIN32)
|
2016-10-19 15:24:06 +03:00
|
|
|
HMODULE hModule = GetModuleHandleW(0);
|
|
|
|
CHAR pathbuf[MAX_PATH];
|
2016-09-30 19:03:54 +03:00
|
|
|
DWORD ret = GetModuleFileNameA(hModule, pathbuf, MAX_PATH);
|
|
|
|
|
|
|
|
return (ret > 0) ? g_string_new(pathbuf) : 0;
|
2016-08-18 04:40:16 +03:00
|
|
|
#else
|
2016-09-30 19:03:54 +03:00
|
|
|
g_assert_not_reached();
|
2016-08-18 04:40:16 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-18 05:18:26 +03:00
|
|
|
static gchar*
|
|
|
|
strrchr_seperator (const gchar* filename)
|
2016-08-18 04:40:16 +03:00
|
|
|
{
|
2016-08-18 05:18:26 +03:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-18 21:42:01 +03:00
|
|
|
char *p2;
|
2016-08-18 05:18:26 +03:00
|
|
|
#endif
|
2016-08-18 21:42:01 +03:00
|
|
|
char *p;
|
2016-08-18 05:18:26 +03:00
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
p = strrchr (filename, G_DIR_SEPARATOR);
|
2016-08-18 05:18:26 +03:00
|
|
|
#ifdef G_OS_WIN32
|
2016-08-18 21:42:01 +03:00
|
|
|
p2 = strrchr (filename, '/');
|
|
|
|
if (p2 > p)
|
|
|
|
p = p2;
|
2016-08-18 05:18:26 +03:00
|
|
|
#endif
|
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
return p;
|
2016-08-18 05:18:26 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
char* mono_m2n_search_assembly(const char* assembly)
|
2016-08-18 05:18:26 +03:00
|
|
|
{
|
2016-08-18 21:42:01 +03:00
|
|
|
GString* path = get_current_executable_path();
|
2016-08-18 05:18:26 +03:00
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
gchar* sep = strrchr_seperator(path->str);
|
|
|
|
g_string_truncate(path, sep - path->str);
|
2016-08-18 05:18:26 +03:00
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
g_string_append(path, G_DIR_SEPARATOR_S);
|
|
|
|
g_string_append(path, assembly);
|
2016-08-18 05:18:26 +03:00
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
char* data = path->str;
|
|
|
|
g_string_free(path, /*free_segment=*/ FALSE);
|
2016-08-18 04:40:16 +03:00
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:58:54 +03:00
|
|
|
MonoImage* mono_m2n_load_assembly(mono_m2n_context_t* ctx, const char* assembly)
|
|
|
|
{
|
|
|
|
const char* path = mono_m2n_search_assembly(assembly);
|
|
|
|
MonoAssembly* mono_assembly = mono_domain_assembly_open(ctx->domain, path);
|
|
|
|
if (mono_assembly == 0)
|
|
|
|
{
|
|
|
|
mono_m2n_error_t __error;
|
|
|
|
__error.type = MONO_M2N_ASSEMBLY_OPEN_FAILED;
|
|
|
|
__error.string = path;
|
|
|
|
mono_m2n_error(__error);
|
2016-11-07 16:50:18 +03:00
|
|
|
return 0;
|
2016-10-21 17:58:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mono_assembly_get_image(mono_assembly);
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
static mono_m2n_assembly_search_hook_t g_assembly_search_hook = 0;
|
|
|
|
|
|
|
|
void* mono_m2n_install_assembly_search_hook(mono_m2n_assembly_search_hook_t hook)
|
|
|
|
{
|
|
|
|
mono_m2n_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;
|
2016-08-18 21:42:01 +03:00
|
|
|
}
|
|
|
|
|
2016-09-28 21:45:25 +03:00
|
|
|
MonoClass* mono_m2n_search_class(const char* assembly, const char* _namespace,
|
|
|
|
const char* name)
|
|
|
|
{
|
|
|
|
mono_m2n_context_t* ctx = mono_m2n_get_context();
|
|
|
|
|
|
|
|
const char* path = mono_m2n_search_assembly(assembly);
|
2016-09-29 20:18:40 +03:00
|
|
|
MonoAssembly* mono_assembly = mono_domain_assembly_open(ctx->domain, path);
|
2016-09-28 21:45:25 +03:00
|
|
|
|
2016-09-29 20:18:40 +03:00
|
|
|
if (mono_assembly == 0)
|
2016-09-28 21:45:25 +03:00
|
|
|
{
|
|
|
|
mono_m2n_error_t error;
|
2016-09-29 20:18:40 +03:00
|
|
|
error.type = MONO_M2N_ASSEMBLY_OPEN_FAILED;
|
|
|
|
error.string = path;
|
2016-09-28 21:45:25 +03:00
|
|
|
mono_m2n_error(error);
|
|
|
|
}
|
|
|
|
|
2016-09-29 20:18:40 +03:00
|
|
|
MonoImage* image = mono_assembly_get_image(mono_assembly);
|
2016-09-28 21:45:25 +03:00
|
|
|
MonoClass* klass = mono_class_from_name(image, _namespace, name);
|
|
|
|
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
static mono_m2n_error_report_hook_t g_error_report_hook = 0;
|
|
|
|
|
|
|
|
void* mono_m2n_install_error_report_hook(mono_m2n_error_report_hook_t hook)
|
|
|
|
{
|
|
|
|
mono_m2n_error_report_hook_t prev = g_error_report_hook;
|
|
|
|
g_error_report_hook = hook;
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mono_m2n_error(mono_m2n_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_m2n_create_object(MonoObject* instance)
|
|
|
|
{
|
|
|
|
MonoEmbedObject* object = g_new(MonoEmbedObject, 1);
|
|
|
|
object->_class = mono_object_get_class(instance);
|
|
|
|
object->_handle = mono_gchandle_new(instance, /*pinned=*/false);
|
|
|
|
return object;
|
2016-08-18 04:40:16 +03:00
|
|
|
}
|