2017-04-15 01:18:00 +03:00
|
|
|
|
/*
|
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>
|
2017-04-15 01:18:00 +03:00
|
|
|
|
#include <unistd.h>
|
2016-08-18 04:40:16 +03:00
|
|
|
|
|
2017-03-27 00:10:27 +03:00
|
|
|
|
#if defined(__APPLE__)
|
2017-04-11 15:51:15 +03:00
|
|
|
|
#include <mach-o/dyld.h>
|
2017-03-27 00:10:27 +03:00
|
|
|
|
#endif
|
2017-03-22 22:35:49 +03:00
|
|
|
|
|
2017-03-27 00:10:27 +03:00
|
|
|
|
#if defined(__OBJC__)
|
2017-03-22 22:35:49 +03:00
|
|
|
|
#include <objc/runtime.h>
|
2016-08-18 04:40:16 +03:00
|
|
|
|
#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>
|
2017-03-28 19:23:01 +03:00
|
|
|
|
#include <mono/metadata/debug-helpers.h>
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_context_t* _current_context;
|
2016-09-28 18:26:31 +03:00
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_context_t* mono_embeddinator_get_context()
|
2016-09-28 18:26:31 +03:00
|
|
|
|
{
|
|
|
|
|
return _current_context;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
void mono_embeddinator_set_context(mono_embeddinator_context_t* ctx)
|
2016-09-28 18:26:31 +03:00
|
|
|
|
{
|
|
|
|
|
_current_context = ctx;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
int mono_embeddinator_init(mono_embeddinator_context_t* ctx, const char* domain)
|
2016-08-23 00:33:04 +03:00
|
|
|
|
{
|
|
|
|
|
if (ctx == 0 || ctx->domain != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
mono_config_parse(NULL);
|
|
|
|
|
ctx->domain = mono_jit_init_version(domain, "v4.0.30319");
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_set_context(ctx);
|
2016-09-28 18:26:31 +03:00
|
|
|
|
|
2017-04-15 01:18:00 +03:00
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
|
getcwd(cwd, PATH_MAX);
|
|
|
|
|
mono_domain_set_config(ctx->domain, cwd, "app.config");
|
|
|
|
|
|
2016-08-23 00:33:04 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
int mono_embeddinator_destroy(mono_embeddinator_context_t* ctx)
|
2016-09-01 01:08:37 +03:00
|
|
|
|
{
|
|
|
|
|
if (ctx == 0 || ctx->domain != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
mono_jit_cleanup (ctx->domain);
|
2017-03-22 22:35:49 +03:00
|
|
|
|
|
2016-09-01 01:08:37 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 15:33:05 +03:00
|
|
|
|
static GString* path_override = NULL;
|
|
|
|
|
|
|
|
|
|
void mono_embeddinator_set_assembly_path (const char *path)
|
|
|
|
|
{
|
|
|
|
|
path_override = g_string_new (path);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 04:40:16 +03:00
|
|
|
|
static GString* get_current_executable_path()
|
|
|
|
|
{
|
2017-04-03 15:33:05 +03:00
|
|
|
|
if (path_override)
|
|
|
|
|
return path_override;
|
2016-09-30 19:03:54 +03:00
|
|
|
|
#if defined(__APPLE__)
|
2017-04-11 15:51:15 +03:00
|
|
|
|
char pathbuf [1024];
|
|
|
|
|
uint32_t bufsize = sizeof (pathbuf);
|
|
|
|
|
int ret = _NSGetExecutablePath (pathbuf, &bufsize);
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 21:37:16 +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
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
char* mono_embeddinator_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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
MonoImage* mono_embeddinator_load_assembly(mono_embeddinator_context_t* ctx, const char* assembly)
|
2016-10-21 17:58:54 +03:00
|
|
|
|
{
|
2017-04-06 16:50:43 +03:00
|
|
|
|
char* path = mono_embeddinator_search_assembly(assembly);
|
2017-04-02 21:37:16 +03:00
|
|
|
|
|
2016-10-21 17:58:54 +03:00
|
|
|
|
MonoAssembly* mono_assembly = mono_domain_assembly_open(ctx->domain, path);
|
2017-04-02 21:37:16 +03:00
|
|
|
|
|
|
|
|
|
if (!mono_assembly)
|
2016-10-21 17:58:54 +03:00
|
|
|
|
{
|
2017-04-02 21:37:16 +03:00
|
|
|
|
mono_embeddinator_error_t error;
|
|
|
|
|
error.type = MONO_EMBEDDINATOR_ASSEMBLY_OPEN_FAILED;
|
|
|
|
|
error.string = path;
|
|
|
|
|
mono_embeddinator_error(error);
|
|
|
|
|
|
2017-04-11 18:30:42 +03:00
|
|
|
|
g_free (path);
|
|
|
|
|
|
2016-11-07 16:50:18 +03:00
|
|
|
|
return 0;
|
2016-10-21 17:58:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 18:30:42 +03:00
|
|
|
|
g_free (path);
|
|
|
|
|
|
2016-10-21 17:58:54 +03:00
|
|
|
|
return mono_assembly_get_image(mono_assembly);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
static mono_embeddinator_assembly_search_hook_t g_assembly_search_hook = 0;
|
2016-08-18 21:42:01 +03:00
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
void* mono_embeddinator_install_assembly_search_hook(mono_embeddinator_assembly_search_hook_t hook)
|
2016-08-18 21:42:01 +03:00
|
|
|
|
{
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_assembly_search_hook_t prev = g_assembly_search_hook;
|
2016-08-18 21:42:01 +03:00
|
|
|
|
g_assembly_search_hook = hook;
|
2017-04-02 21:37:16 +03:00
|
|
|
|
|
2016-09-29 20:18:40 +03:00
|
|
|
|
return (void*)prev;
|
2016-08-18 21:42:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
MonoClass* mono_embeddinator_search_class(const char* assembly, const char* _namespace,
|
2016-09-28 21:45:25 +03:00
|
|
|
|
const char* name)
|
|
|
|
|
{
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_context_t* ctx = mono_embeddinator_get_context();
|
2016-09-28 21:45:25 +03:00
|
|
|
|
|
2017-04-06 16:50:43 +03:00
|
|
|
|
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);
|
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
|
|
|
|
{
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_error_t error;
|
|
|
|
|
error.type = MONO_EMBEDDINATOR_ASSEMBLY_OPEN_FAILED;
|
2016-09-29 20:18:40 +03:00
|
|
|
|
error.string = path;
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_error(error);
|
2016-09-28 21:45:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 18:30:42 +03:00
|
|
|
|
g_free (path);
|
|
|
|
|
|
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);
|
|
|
|
|
|
2017-04-07 03:27:14 +03:00
|
|
|
|
if (klass == 0)
|
|
|
|
|
{
|
|
|
|
|
mono_embeddinator_error_t error;
|
|
|
|
|
error.type = MONO_EMBEDDINATOR_CLASS_LOOKUP_FAILED;
|
|
|
|
|
error.string = path;
|
|
|
|
|
mono_embeddinator_error(error);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 21:45:25 +03:00
|
|
|
|
return klass;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 19:23:01 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 19:37:25 +03:00
|
|
|
|
void mono_embeddinator_throw_exception(MonoObject *exception)
|
|
|
|
|
{
|
|
|
|
|
mono_embeddinator_error_t error;
|
|
|
|
|
error.type = MONO_EMBEDDINATOR_EXCEPTION_THROWN;
|
|
|
|
|
error.exception = (MonoException*) exception;
|
2017-04-02 21:37:16 +03:00
|
|
|
|
|
2017-03-28 19:37:25 +03:00
|
|
|
|
mono_embeddinator_error(error);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 03:26:33 +03:00
|
|
|
|
char* mono_embeddinator_error_to_string(mono_embeddinator_error_t error)
|
|
|
|
|
{
|
|
|
|
|
switch(error.type)
|
|
|
|
|
{
|
|
|
|
|
case MONO_EMBEDDINATOR_OK:
|
|
|
|
|
return "No error";
|
|
|
|
|
case MONO_EMBEDDINATOR_EXCEPTION_THROWN:
|
|
|
|
|
return "Mono threw a managed exception";
|
|
|
|
|
case MONO_EMBEDDINATOR_ASSEMBLY_OPEN_FAILED:
|
|
|
|
|
return "Mono failed to load assembly";
|
|
|
|
|
case MONO_EMBEDDINATOR_CLASS_LOOKUP_FAILED:
|
|
|
|
|
return "Mono failed to lookup class";
|
|
|
|
|
case MONO_EMBEDDINATOR_METHOD_LOOKUP_FAILED:
|
|
|
|
|
return "Mono failed to lookup method";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert_not_reached();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mono_embeddinator_report_error_and_abort(mono_embeddinator_error_t error)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Embeddinator error: %s.\n", mono_embeddinator_error_to_string(error));
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static mono_embeddinator_error_report_hook_t g_error_report_hook = mono_embeddinator_report_error_and_abort;
|
2016-08-18 21:42:01 +03:00
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
void* mono_embeddinator_install_error_report_hook(mono_embeddinator_error_report_hook_t hook)
|
2016-08-18 21:42:01 +03:00
|
|
|
|
{
|
2017-03-27 03:18:01 +03:00
|
|
|
|
mono_embeddinator_error_report_hook_t prev = g_error_report_hook;
|
2016-08-18 21:42:01 +03:00
|
|
|
|
g_error_report_hook = hook;
|
2017-04-02 21:37:16 +03:00
|
|
|
|
|
2016-08-18 21:42:01 +03:00
|
|
|
|
return prev;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
void mono_embeddinator_error(mono_embeddinator_error_t error)
|
2016-08-18 21:42:01 +03:00
|
|
|
|
{
|
|
|
|
|
if (g_error_report_hook == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_error_report_hook(error);
|
2016-10-19 15:24:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 03:18:01 +03:00
|
|
|
|
void* mono_embeddinator_create_object(MonoObject* instance)
|
2016-10-19 15:24:06 +03:00
|
|
|
|
{
|
|
|
|
|
MonoEmbedObject* object = g_new(MonoEmbedObject, 1);
|
2017-03-28 18:14:30 +03:00
|
|
|
|
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);
|
2017-03-25 00:05:52 +03:00
|
|
|
|
}
|
2017-04-06 16:50:43 +03:00
|
|
|
|
|
|
|
|
|
void mono_embeddinator_destroy_object(MonoEmbedObject* object)
|
|
|
|
|
{
|
|
|
|
|
mono_gchandle_free (object->_handle);
|
|
|
|
|
g_free (object);
|
|
|
|
|
}
|