From 1afae37c7f846c07a88997c290b2561238bca4a1 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 10 Apr 2017 06:48:35 +0200 Subject: [PATCH] [runtime] Add support for initializing in an embedder mode. --- runtime/launcher.m | 22 +++++++++++++++++----- runtime/monotouch-main.m | 17 +++++++++++++---- runtime/runtime.m | 12 ++++++++++++ runtime/xamarin/runtime.h | 9 ++++++++- tools/mtouch/mtouch.cs | 4 ++-- tools/mtouch/simlauncher.m | 2 +- 6 files changed, 53 insertions(+), 13 deletions(-) diff --git a/runtime/launcher.m b/runtime/launcher.m index d0f2a9cc4b..6d230bb9c6 100644 --- a/runtime/launcher.m +++ b/runtime/launcher.m @@ -530,10 +530,12 @@ app_initialize (xamarin_initialize_data *data, bool is_extension) #define __XAMARIN_MAC_RELAUNCH_APP__ "__XAMARIN_MAC_RELAUNCH_APP__" -int xamarin_main (int argc, char **argv, bool is_extension) +int xamarin_main (int argc, char **argv, enum XamarinLaunchMode launch_mode) { xamarin_initialize_data data = { 0 }; + bool is_extension = launch_mode == XamarinLaunchModeExtension; + if (getcwd (original_working_directory_path, sizeof (original_working_directory_path)) == NULL) original_working_directory_path [0] = '\0'; @@ -596,7 +598,8 @@ int xamarin_main (int argc, char **argv, bool is_extension) *ptr++ = argv [i]; *ptr = NULL; - if (is_extension) { + switch (launch_mode) { + case XamarinLaunchModeExtension: { void * libExtensionHandle = dlopen ("/usr/lib/libextension.dylib", RTLD_LAZY); if (libExtensionHandle == nil) exit_with_message ("Unable to load libextension.dylib", data.basename, false); @@ -610,8 +613,17 @@ int xamarin_main (int argc, char **argv, bool is_extension) rv = (*extensionMain) (new_argc, new_argv); dlclose (libExtensionHandle); - } else { + break; + } + case XamarinLaunchModeApp: rv = mono_main (new_argc, new_argv); + break; + case XamarinLaunchModeEmbedded: + // do nothing + break; + default: + xamarin_assertion_message ("Invalid launch mode: %i.", launch_mode); + break; } free (new_argv); @@ -622,10 +634,10 @@ int xamarin_main (int argc, char **argv, bool is_extension) int main (int argc, char **argv) { - return xamarin_main (argc, argv, false); + return xamarin_main (argc, argv, XamarinLaunchModeApp); } int xamarin_mac_extension_main (int argc, char **argv) { - return xamarin_main (argc, argv, true); + return xamarin_main (argc, argv, XamarinLaunchModeEmbedded); } diff --git a/runtime/monotouch-main.m b/runtime/monotouch-main.m index 24013ecb53..279adff88a 100644 --- a/runtime/monotouch-main.m +++ b/runtime/monotouch-main.m @@ -220,7 +220,7 @@ extern void mono_gc_init_finalizer_thread (void); */ int -xamarin_main (int argc, char *argv[], bool is_extension) +xamarin_main (int argc, char *argv[], enum XamarinLaunchMode launch_mode) { // COOP: ? // + 1 for the initial "monotouch" +1 for the final NULL = +2. @@ -234,7 +234,7 @@ xamarin_main (int argc, char *argv[], bool is_extension) patch_sigaction (); #endif - xamarin_is_extension = is_extension; + xamarin_is_extension = launch_mode == XamarinLaunchModeExtension; memset (managed_argv, 0, sizeof (char*) * (argc + 2)); managed_argv [0] = "monotouch"; @@ -464,7 +464,8 @@ xamarin_main (int argc, char *argv[], bool is_extension) DEBUG_LAUNCH_TIME_PRINT ("Total initialization time"); int rv = 0; - if (is_extension) { + switch (launch_mode) { + case XamarinLaunchModeExtension: char base_dir [1024]; char config_file_name [1024]; @@ -476,8 +477,16 @@ xamarin_main (int argc, char *argv[], bool is_extension) MONO_ENTER_GC_SAFE; rv = xamarin_extension_main (argc, argv); MONO_EXIT_GC_SAFE; - } else { + break; + case XamarinLaunchModeApp: mono_jit_exec (mono_domain_get (), assembly, managed_argc, managed_argv); + break; + case XamarinLaunchModeEmbedded: + // do nothing + break; + default: + xamarin_assertion_message ("Invalid launch mode: %i.", launch_mode); + break; } return rv; diff --git a/runtime/runtime.m b/runtime/runtime.m index 6a9d1d0f53..9d4a636205 100644 --- a/runtime/runtime.m +++ b/runtime/runtime.m @@ -1099,6 +1099,18 @@ print_callback (const char *string, mono_bool is_stdout) PRINT ("%s", string); } +void +xamarin_initialize_embedded () +{ + static bool initialized = false; + if (initialized) + return; + initialized = true; + + char *argv[] = { (char *) "embedded" }; + xamarin_main (1, argv, XamarinLaunchModeEmbedded); +} + void xamarin_initialize () { diff --git a/runtime/xamarin/runtime.h b/runtime/xamarin/runtime.h index 3957e6862f..c12d50437e 100644 --- a/runtime/xamarin/runtime.h +++ b/runtime/xamarin/runtime.h @@ -98,6 +98,12 @@ enum NSObjectFlags { NSObjectFlagsHasManagedRef = 32, }; +enum XamarinLaunchMode { + XamarinLaunchModeApp = 0, + XamarinLaunchModeExtension = 1, + XamarinLaunchModeEmbedded = 2, +}; + struct AssemblyLocation { const char *assembly_name; // base name (without extension) of the assembly const char *location; // the directory where the assembly is @@ -109,6 +115,7 @@ struct AssemblyLocations { }; void xamarin_initialize (); +void xamarin_initialize_embedded (); /* STABLE */ void xamarin_assertion_message (const char *msg, ...) __attribute__((__noreturn__)); const char * xamarin_get_bundle_path (); /* Public API */ @@ -159,7 +166,7 @@ void xamarin_create_managed_ref (id self, void * managed_object, bool retain); void xamarin_release_managed_ref (id self, MonoObject *managed_obj); void xamarin_notify_dealloc (id self, int gchandle); -int xamarin_main (int argc, char *argv[], bool is_extension); +int xamarin_main (int argc, char *argv[], enum XamarinLaunchMode launch_mode); char * xamarin_type_get_full_name (MonoType *type, guint32 *exception_gchandle); // return value must be freed with 'mono_free' char * xamarin_class_get_full_name (MonoClass *klass, guint32 *exception_gchandle); // return value must be freed with 'mono_free' diff --git a/tools/mtouch/mtouch.cs b/tools/mtouch/mtouch.cs index 2627bbbacf..8d5264d74f 100644 --- a/tools/mtouch/mtouch.cs +++ b/tools/mtouch/mtouch.cs @@ -680,9 +680,9 @@ namespace Xamarin.Bundler // the name of the executable must be the bundle id (reverse dns notation) // but we do not want to impose that (ugly) restriction to the managed .exe / project name / ... sw.WriteLine ("\targv [0] = (char *) \"{0}\";", Path.GetFileNameWithoutExtension (app.RootAssemblies [0])); - sw.WriteLine ("\tint rv = xamarin_main (argc, argv, true);"); + sw.WriteLine ("\tint rv = xamarin_main (argc, argv, XamarinLaunchModeApp);"); } else { - sw.WriteLine ("\tint rv = xamarin_main (argc, argv, false);"); + sw.WriteLine ("\tint rv = xamarin_main (argc, argv, XamarinLaunchModeExtension);"); } sw.WriteLine ("\t[pool drain];"); sw.WriteLine ("\treturn rv;"); diff --git a/tools/mtouch/simlauncher.m b/tools/mtouch/simlauncher.m index cb2067b3e4..229f8099e9 100644 --- a/tools/mtouch/simlauncher.m +++ b/tools/mtouch/simlauncher.m @@ -28,7 +28,7 @@ void xamarin_setup_impl () int main (int argc, char** argv) { - @autoreleasepool { return xamarin_main (argc, argv, false); } + @autoreleasepool { return xamarin_main (argc, argv, XamarinLaunchModeApp); } }