Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////

        #region Private Constructors
        //
        // NOTE: For use by the associated InterpreterHelper.Create method
        //       overload ONLY.
        //
        private InterpreterHelper(
            InterpreterSettings interpreterSettings,
            bool strict,
            ref Result result
            )
        {
            interpreter = Interpreter.Create(
                interpreterSettings, strict, ref result);
        }
Ejemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////

        #region Internal Static Methods
        internal static ReturnCode UseStartupDefaults(
            InterpreterSettings interpreterSettings,
            CreateFlags createFlags,
            ref Result error
            )
        {
            if (interpreterSettings == null)
            {
                error = "invalid interpreter settings";
                return(ReturnCode.Error);
            }

            //
            // NOTE: Use the creation flags specified by the caller,
            //       ignoring the creation flags in the interpreter
            //       settings.  If there are existing policies and/or
            //       traces, make sure the creation flags are modified
            //       to skip adding the default policies and/or traces
            //       during interpreter creation.
            //
            if (interpreterSettings.Policies != null)
            {
                createFlags |= CreateFlags.NoCorePolicies;
            }

            if (interpreterSettings.Traces != null)
            {
                createFlags |= CreateFlags.NoCoreTraces;
            }

            interpreterSettings.CreateFlags = createFlags;

            //
            // NOTE: The interpreter host may be disposed now -OR- may
            //       end up being disposed later, so avoid copying it.
            //
            interpreterSettings.Host = null;

            //
            // NOTE: Nulling these out is not necessary when the creation
            //       flags are modified to skip adding default policies
            //       and traces (above).
            //
            // interpreterSettings.Policies = null;
            // interpreterSettings.Traces = null;

            //
            // NOTE: These startup settings are reset by this method to
            //       avoid having their values used when the command line
            //       arguments have been "locked" by the interpreter host.
            //
            interpreterSettings.Text        = null;
            interpreterSettings.LibraryPath = null;

            return(ReturnCode.Ok);
        }
Ejemplo n.º 3
0
        ///////////////////////////////////////////////////////////////////////

        #region Static "Factory" Methods
        public static InterpreterHelper Create(
            AppDomain appDomain,
            InterpreterSettings interpreterSettings,
            bool strict,
            ref Result result
            )
        {
            if (appDomain == null)
            {
                result = "invalid application domain";
                return(null);
            }

            if (assemblyName == null)
            {
                result = "invalid assembly name";
                return(null);
            }

            if (typeName == null)
            {
                result = "invalid type name";
                return(null);
            }

            try
            {
                object[] ctorArgs = { interpreterSettings, strict, result };

                InterpreterHelper interpreterHelper =
                    (InterpreterHelper)appDomain.CreateInstanceAndUnwrap(
                        assemblyName.ToString(), typeName, false,
                        MarshalOps.PrivateCreateInstanceBindingFlags,
                        null, ctorArgs, null, null, null);

                //
                // NOTE: Grab the result as it may have been modified.
                //
                result = ctorArgs[ctorArgs.Length - 1] as Result;

                return(interpreterHelper);
            }
            catch (Exception e)
            {
                result = e;
            }

            return(null);
        }
Ejemplo n.º 4
0
        ///////////////////////////////////////////////////////////////////////

#if XML && SERIALIZATION
        public static InterpreterSettings CreateFrom(
            string fileName,
            bool expand,
            ref Result error
            )
        {
            InterpreterSettings interpreterSettings = null;

            if (LoadFrom(fileName, expand, ref interpreterSettings,
                         ref error) == ReturnCode.Ok)
            {
                return(interpreterSettings);
            }

            return(null);
        }
Ejemplo n.º 5
0
        ///////////////////////////////////////////////////////////////////////

        #region Public Static Methods
        public static ReturnCode LoadFrom(
            Interpreter interpreter,
            bool expand,
            ref InterpreterSettings interpreterSettings,
            ref Result error
            )
        {
            if (interpreter == null)
            {
                error = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (interpreterSettings != null)
            {
                error = "cannot overwrite valid interpreter settings";
                return(ReturnCode.Error);
            }

            try
            {
                InterpreterSettings localInterpreterSettings = Create();

                if (FromInterpreter(
                        interpreter, localInterpreterSettings,
                        ref error) == ReturnCode.Ok)
                {
                    if (expand)
                    {
                        Expand(localInterpreterSettings);
                    }

                    interpreterSettings = localInterpreterSettings;
                    return(ReturnCode.Ok);
                }
            }
            catch (Exception e)
            {
                error = e;
            }

            return(ReturnCode.Error);
        }
Ejemplo n.º 6
0
        ///////////////////////////////////////////////////////////////////////

        private static void Expand(
            InterpreterSettings interpreterSettings
            )
        {
            if (interpreterSettings != null)
            {
                IEnumerable <string> args = interpreterSettings.Args;

                if (args != null)
                {
                    StringList newArgs = new StringList();

                    foreach (string arg in args)
                    {
                        newArgs.Add(Expand(arg));
                    }

                    interpreterSettings.Args = newArgs;
                }

                interpreterSettings.Culture = Expand(
                    interpreterSettings.Culture);

                interpreterSettings.Profile = Expand(
                    interpreterSettings.Profile);

                interpreterSettings.Text = Expand(interpreterSettings.Text);

                interpreterSettings.LibraryPath = Expand(
                    interpreterSettings.LibraryPath);

                StringList autoPathList = interpreterSettings.AutoPathList;

                if (autoPathList != null)
                {
                    for (int index = 0; index < autoPathList.Count; index++)
                    {
                        autoPathList[index] = Expand(autoPathList[index]);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        ///////////////////////////////////////////////////////////////////////

        private static ReturnCode FromInterpreter(
            Interpreter interpreter,
            InterpreterSettings interpreterSettings,
            ref Result error
            )
        {
            if (interpreter == null)
            {
                error = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (interpreterSettings == null)
            {
                error = "invalid interpreter settings";
                return(ReturnCode.Error);
            }

            lock (interpreter.InternalSyncRoot) /* TRANSACTIONAL */
            {
                if (interpreter.PopulateInterpreterSettings(
                        ref interpreterSettings, ref error) != ReturnCode.Ok)
                {
                    return(ReturnCode.Error);
                }

                interpreterSettings.CreateFlags       = interpreter.CreateFlags;
                interpreterSettings.InitializeFlags   = interpreter.InitializeFlags;
                interpreterSettings.ScriptFlags       = interpreter.ScriptFlags;
                interpreterSettings.InterpreterFlags  = interpreter.InterpreterFlags;
                interpreterSettings.Owner             = interpreter.Owner;
                interpreterSettings.ApplicationObject = interpreter.ApplicationObject;
                interpreterSettings.PolicyObject      = interpreter.PolicyObject;
                interpreterSettings.ResolverObject    = interpreter.ResolverObject;
                interpreterSettings.UserObject        = interpreter.UserObject;
                interpreterSettings.LibraryPath       = interpreter.LibraryPath;
            }

            return(ReturnCode.Ok);
        }
Ejemplo n.º 8
0
        ///////////////////////////////////////////////////////////////////////

        public static ReturnCode SaveTo(
            string fileName,
            bool expand,
            InterpreterSettings interpreterSettings,
            ref Result error
            )
        {
            if (String.IsNullOrEmpty(fileName))
            {
                error = "invalid file name";
                return(ReturnCode.Error);
            }

            if (File.Exists(fileName))
            {
                error = String.Format(
                    "cannot write \"{0}\": file already exists",
                    fileName);

                return(ReturnCode.Error);
            }

            if (interpreterSettings == null)
            {
                error = "invalid interpreter settings";
                return(ReturnCode.Error);
            }

            try
            {
                using (Stream stream = new FileStream(fileName,
                                                      FileMode.CreateNew, FileAccess.Write)) /* EXEMPT */
                {
                    using (MemoryStream stream2 = new MemoryStream())
                    {
                        using (XmlTextWriter writer = new XmlTextWriter(
                                   stream2, null))
                        {
                            if (expand)
                            {
                                Expand(interpreterSettings);
                            }

                            if (XmlOps.Serialize(
                                    interpreterSettings,
                                    typeof(InterpreterSettings), writer,
                                    null, ref error) != ReturnCode.Ok)
                            {
                                return(ReturnCode.Error);
                            }

                            writer.Flush();

                            XmlDocument document;

                            using (MemoryStream stream3 = new MemoryStream(
                                       stream2.ToArray(), false))
                            {
                                writer.Close();

                                document = new XmlDocument();
                                document.Load(stream3);
                            }

                            if (ToDocument(
                                    document, interpreterSettings,
                                    ref error) != ReturnCode.Ok)
                            {
                                return(ReturnCode.Error);
                            }

                            XmlWriterSettings writerSettings =
                                new XmlWriterSettings();

                            writerSettings.Indent = true;

                            using (XmlWriter writer2 = XmlWriter.Create(
                                       stream, writerSettings))
                            {
                                document.WriteTo(writer2);
                            }

                            return(ReturnCode.Ok);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                error = e;
            }

            return(ReturnCode.Error);
        }
Ejemplo n.º 9
0
        ///////////////////////////////////////////////////////////////////////

#if XML && SERIALIZATION
        public static ReturnCode LoadFrom(
            string fileName,
            bool expand,
            ref InterpreterSettings interpreterSettings,
            ref Result error
            )
        {
            if (String.IsNullOrEmpty(fileName))
            {
                error = "invalid file name";
                return(ReturnCode.Error);
            }

            if (!File.Exists(fileName))
            {
                error = String.Format(
                    "cannot read \"{0}\": no such file",
                    fileName);

                return(ReturnCode.Error);
            }

            if (interpreterSettings != null)
            {
                error = "cannot overwrite valid interpreter settings";
                return(ReturnCode.Error);
            }

            try
            {
                XmlDocument document = new XmlDocument();
                document.Load(fileName);

                using (XmlNodeReader reader = new XmlNodeReader(document))
                {
                    object @object = null;

                    if (XmlOps.Deserialize(
                            typeof(InterpreterSettings), reader,
                            ref @object, ref error) == ReturnCode.Ok)
                    {
                        InterpreterSettings localInterpreterSettings =
                            @object as InterpreterSettings;

                        if (FromDocument(document, localInterpreterSettings,
                                         ref error) == ReturnCode.Ok)
                        {
                            if (expand)
                            {
                                Expand(localInterpreterSettings);
                            }

                            interpreterSettings = localInterpreterSettings;
                            return(ReturnCode.Ok);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                error = e;
            }

            return(ReturnCode.Error);
        }
Ejemplo n.º 10
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        private static ReturnCode ToDocument(
            XmlDocument document,
            InterpreterSettings interpreterSettings,
            ref Result error
            )
        {
            if (document == null)
            {
                error = "invalid xml document";
                return(ReturnCode.Error);
            }

            if (interpreterSettings == null)
            {
                error = "invalid interpreter settings";
                return(ReturnCode.Error);
            }

            XmlElement documentElement = document.DocumentElement;

            if (documentElement == null)
            {
                error = "invalid xml document element";
                return(ReturnCode.Error);
            }

            XmlNode node;

            if (interpreterSettings.Args != null)
            {
                node = document.CreateElement("Args");

                node.InnerText = new StringList(
                    interpreterSettings.Args).ToString();

                documentElement.AppendChild(node);
            }

            node           = document.CreateElement("CreateFlags");
            node.InnerText = interpreterSettings.CreateFlags.ToString();
            documentElement.AppendChild(node);

            node           = document.CreateElement("InitializeFlags");
            node.InnerText = interpreterSettings.InitializeFlags.ToString();
            documentElement.AppendChild(node);

            node           = document.CreateElement("ScriptFlags");
            node.InnerText = interpreterSettings.ScriptFlags.ToString();
            documentElement.AppendChild(node);

            node           = document.CreateElement("InterpreterFlags");
            node.InnerText = interpreterSettings.InterpreterFlags.ToString();
            documentElement.AppendChild(node);

            if (interpreterSettings.AutoPathList != null)
            {
                node           = document.CreateElement("AutoPathList");
                node.InnerText = interpreterSettings.AutoPathList.ToString();
                documentElement.AppendChild(node);
            }

            return(ReturnCode.Ok);
        }
Ejemplo n.º 11
0
        ///////////////////////////////////////////////////////////////////////

#if XML
        private static ReturnCode FromDocument(
            XmlDocument document,
            InterpreterSettings interpreterSettings,
            ref Result error
            )
        {
            if (document == null)
            {
                error = "invalid xml document";
                return(ReturnCode.Error);
            }

            if (interpreterSettings == null)
            {
                error = "invalid interpreter settings";
                return(ReturnCode.Error);
            }

            XmlElement documentElement = document.DocumentElement;

            if (documentElement == null)
            {
                error = "invalid xml document element";
                return(ReturnCode.Error);
            }

            XmlNode node;

            node = documentElement.SelectSingleNode("Args");

            if ((node != null) && !String.IsNullOrEmpty(node.InnerText))
            {
                StringList list = null;

                if (Parser.SplitList(
                        null, node.InnerText, 0, Length.Invalid, false,
                        ref list, ref error) == ReturnCode.Ok)
                {
                    interpreterSettings.Args = list;
                }
                else
                {
                    return(ReturnCode.Error);
                }
            }

            node = documentElement.SelectSingleNode("CreateFlags");

            if ((node != null) && !String.IsNullOrEmpty(node.InnerText))
            {
                object enumValue = EnumOps.TryParseEnum(
                    typeof(CreateFlags), node.InnerText,
                    true, true, ref error);

                if (enumValue is CreateFlags)
                {
                    interpreterSettings.CreateFlags = (CreateFlags)enumValue;
                }
                else
                {
                    return(ReturnCode.Error);
                }
            }

            node = documentElement.SelectSingleNode("InitializeFlags");

            if ((node != null) && !String.IsNullOrEmpty(node.InnerText))
            {
                object enumValue = EnumOps.TryParseEnum(
                    typeof(InitializeFlags), node.InnerText,
                    true, true, ref error);

                if (enumValue is InitializeFlags)
                {
                    interpreterSettings.InitializeFlags = (InitializeFlags)enumValue;
                }
                else
                {
                    return(ReturnCode.Error);
                }
            }

            node = documentElement.SelectSingleNode("ScriptFlags");

            if ((node != null) && !String.IsNullOrEmpty(node.InnerText))
            {
                object enumValue = EnumOps.TryParseEnum(
                    typeof(ScriptFlags), node.InnerText,
                    true, true, ref error);

                if (enumValue is ScriptFlags)
                {
                    interpreterSettings.ScriptFlags = (ScriptFlags)enumValue;
                }
                else
                {
                    return(ReturnCode.Error);
                }
            }

            node = documentElement.SelectSingleNode("InterpreterFlags");

            if ((node != null) && !String.IsNullOrEmpty(node.InnerText))
            {
                object enumValue = EnumOps.TryParseEnum(
                    typeof(InterpreterFlags), node.InnerText,
                    true, true, ref error);

                if (enumValue is InterpreterFlags)
                {
                    interpreterSettings.InterpreterFlags = (InterpreterFlags)enumValue;
                }
                else
                {
                    return(ReturnCode.Error);
                }
            }

            node = documentElement.SelectSingleNode("AutoPathList");

            if ((node != null) && !String.IsNullOrEmpty(node.InnerText))
            {
                StringList list = null;

                if (Parser.SplitList(
                        null, node.InnerText, 0, Length.Invalid, false,
                        ref list, ref error) == ReturnCode.Ok)
                {
                    interpreterSettings.AutoPathList = list;
                }
                else
                {
                    return(ReturnCode.Error);
                }
            }

            return(ReturnCode.Ok);
        }