Added new API mono_runtime_set_main_args() and fixed CommandLine for embedders.
authorPaolo Molaro <lupus@oddwiz.org>
Tue, 25 Feb 2014 16:25:24 +0000 (17:25 +0100)
committerPaolo Molaro <lupus@oddwiz.org>
Tue, 25 Feb 2014 16:43:08 +0000 (17:43 +0100)
The new API allows to set the program arguments from embedders that don't call
mono_jit_exec() and related functions that set them automatically. This change
also fixes the NullRef exception that would happen if the arguments are not set.

mono/metadata/object.c
mono/metadata/object.h

index dd8da0b0a107465bd2f699d8ffeabe75c58478c3..5065ee6c06e53d57b006afa3f0b2db1cc5076d08 100644 (file)
@@ -3540,7 +3540,7 @@ mono_runtime_delegate_invoke (MonoObject *delegate, void **params, MonoObject **
 }
 
 static char **main_args = NULL;
-static int num_main_args;
+static int num_main_args = 0;
 
 /**
  * mono_runtime_get_main_args:
@@ -3554,9 +3554,6 @@ mono_runtime_get_main_args (void)
        int i;
        MonoDomain *domain = mono_domain_get ();
 
-       if (!main_args)
-               return NULL;
-
        res = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, num_main_args);
 
        for (i = 0; i < num_main_args; ++i)
@@ -3573,6 +3570,40 @@ free_main_args (void)
        for (i = 0; i < num_main_args; ++i)
                g_free (main_args [i]);
        g_free (main_args);
+       num_main_args = 0;
+       main_args = NULL;
+}
+
+/**
+ * mono_runtime_set_main_args:
+ * @argc: number of arguments from the command line
+ * @argv: array of strings from the command line
+ *
+ * Set the command line arguments from an embedding application that doesn't otherwise call
+ * mono_runtime_run_main ().
+ */
+int
+mono_runtime_set_main_args (int argc, char* argv[])
+{
+       int i;
+
+       free_main_args ();
+       main_args = g_new0 (char*, argc);
+       num_main_args = argc;
+
+       for (i = 0; i < argc; ++i) {
+               gchar *utf8_arg;
+
+               utf8_arg = mono_utf8_from_external (argv[i]);
+               if (utf8_arg == NULL) {
+                       g_print ("\nCannot determine the text encoding for argument %d (%s).\n", i, argv [i]);
+                       g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
+                       exit (-1);
+               }
+
+               main_args [i] = utf8_arg;
+       }
+
 }
 
 /**
index 3015159d7fb7668325dccb70fc7ee14aacdd018a..0f96cf7ecc39ce5120a10fe9ef5e9cedcc9b29a4 100644 (file)
@@ -245,6 +245,9 @@ MONO_API int
 mono_runtime_exec_main     (MonoMethod *method, MonoArray *args,
                             MonoObject **exc);
 
+MONO_API int
+mono_runtime_set_main_args  (int argc, char* argv[]);
+
 /* The following functions won't be available with mono was configured with remoting disabled. */
 /*#ifndef DISABLE_REMOTING */
 MONO_API void*