Extract Mono initialization to support API.

This commit is contained in:
Joao Matos 2016-08-22 22:33:04 +01:00
Родитель faf4045538
Коммит 05b6618f08
4 изменённых файлов: 40 добавлений и 18 удалений

Просмотреть файл

@ -105,9 +105,9 @@ namespace MonoManagedToNative.Generators
if (type == typeof(string))
{
var argId = CGenerator.GenId(Context.ArgName);
var domainId = CGenerator.GenId("mono_domain");
Context.SupportBefore.WriteLine("MonoString* {0} = mono_string_new({1}, {2});",
argId, domainId, Context.ArgName);
var contextId = CGenerator.GenId("mono_context");
Context.SupportBefore.WriteLine("MonoString* {0} = mono_string_new({1}.domain, {2});",
argId, contextId, Context.ArgName);
Context.Return.Write("{0}", argId);
return true;
}

Просмотреть файл

@ -39,9 +39,7 @@ namespace MonoManagedToNative.Generators
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock();
WriteLine("MonoDomain* {0};", GeneratedIdentifier("mono_domain"));
WriteLine("bool {0};", GeneratedIdentifier("mono_initialized"));
WriteLine("mono_m2n_context_t {0};", GeneratedIdentifier("mono_context"));
WriteLine("MonoAssembly* {0}_assembly;", AssemblyId);
WriteLine("MonoImage* {0}_image;", AssemblyId);
PopBlock(NewLineKind.BeforeNextBlock);
@ -87,17 +85,12 @@ namespace MonoManagedToNative.Generators
WriteLine("static void {0}()", GeneratedIdentifier("initialize_mono"));
WriteStartBraceIndent();
var initializedId = GeneratedIdentifier("mono_initialized");
WriteLine("if ({0})", initializedId);
var contextId = GeneratedIdentifier("mono_context");
WriteLine("if ({0}.domain)", contextId);
WriteLineIndent("return;");
WriteLine("mono_config_parse(NULL);");
var domainName = "mono_managed_to_native_binding";
var version = "v4.0.30319";
WriteLine("{0} = mono_jit_init_version(\"{1}\", \"{2}\");",
GeneratedIdentifier("mono_domain"), domainName, version);
WriteLine("{0} = true;", initializedId);
WriteLine("mono_m2n_init(&{0}, \"{1}\");", contextId, domainName);
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
@ -121,8 +114,8 @@ namespace MonoManagedToNative.Generators
WriteLine("const char* {0} = mono_m2n_search_assembly(\"{1}.dll\");",
assemblyPathId, assemblyName);
WriteLine("{0} = mono_domain_assembly_open({1}, {2});",
monoAssemblyName, GeneratedIdentifier("mono_domain"), assemblyPathId);
WriteLine("{0} = mono_domain_assembly_open({1}.domain, {2});",
monoAssemblyName, GeneratedIdentifier("mono_context"), assemblyPathId);
WriteLine("if ({0} == 0)", monoAssemblyName);
WriteStartBraceIndent();
@ -212,8 +205,8 @@ namespace MonoManagedToNative.Generators
if (method.IsConstructor)
{
WriteLine("{0}* object = ({0}*) calloc(1, sizeof({0}));", @class.Name);
WriteLine("MonoObject* {0} = mono_object_new({1}, {2});",
instanceId, GeneratedIdentifier("mono_domain"), classId);
WriteLine("MonoObject* {0} = mono_object_new({1}.domain, {2});",
instanceId, GeneratedIdentifier("mono_context"), classId);
WriteLine("object->_handle = mono_gchandle_new({0}, /*pinned=*/false);",
instanceId);
}

Просмотреть файл

@ -11,6 +11,20 @@
#include <unistd.h>
#endif
#include <mono/jit/jit.h>
#include <mono/metadata/mono-config.h>
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");
return true;
}
static GString* get_current_executable_path()
{
#ifdef __APPLE__

Просмотреть файл

@ -50,6 +50,21 @@
MONO_M2N_BEGIN_DECLS
/**
* Represents a managed-to-native binding context.
*/
typedef struct _MonoDomain MonoDomain;
typedef struct
{
MonoDomain* domain;
} mono_m2n_context_t;
/**
* Initializes a managed-to-native binding context.
* Returns a boolean indicating success or failure.
*/
int mono_m2n_init(mono_m2n_context_t* ctx, const char* domain);
/**
* Searches and returns the path to the given managed assembly.
*/