Esempio n. 1
0
        /// <summary>
        /// Parse a script
        /// </summary>
        /// <param name="container">Script container</param>
        /// <returns>An array of errors, if no errors then empty</returns>
        public ScriptError[] Parse(ScriptContainer container)
        {
            ScriptErrorListener listener = new ScriptErrorListener();

            try
            {
                foreach (AssemblyName name in container.ReferencedNames)
                {
                    _engine.Runtime.LoadAssembly(Assembly.Load(name));
                }

                if (container.EnableDebug)
                {
                    string newFile = Path.GetTempFileName();

                    ScriptUtils.AddTempFile(newFile);
                    File.WriteAllText(newFile, container.Script);
                    _source = _engine.CreateScriptSourceFromFile(newFile);
                }
                else
                {
                    _source = _engine.CreateScriptSourceFromString(container.Script);
                }

                _code = _source.Compile(listener);

                if (listener.Errors.Count == 0)
                {
                    // Just create the global scope, don't execute it yet
                    _scope = _engine.CreateScope();
                    _scope.SetVariable("CANAPEScriptContainer", new ScriptContainer(container));
                }
            }
            catch (Exception ex)
            {
                listener.Errors.Add(new ScriptError(ex.Message, "FatalError", 0, 0));
            }

            return(listener.Errors.ToArray());
        }
Esempio n. 2
0
        /// <summary>
        /// Parse a script
        /// </summary>
        /// <param name="container">Script container</param>
        /// <returns>An array of errors, if no errors then empty</returns>
        public ScriptError[] Parse(ScriptContainer container)
        {
            ScriptErrorListener listener = new ScriptErrorListener();

            try
            {
                foreach (AssemblyName name in container.ReferencedNames)
                {
                    _engine.Runtime.LoadAssembly(Assembly.Load(name));
                }

                if (container.EnableDebug)
                {
                    string newFile = Path.GetTempFileName();

                    ScriptUtils.AddTempFile(newFile);
                    File.WriteAllText(newFile, container.Script);
                    _source = _engine.CreateScriptSourceFromFile(newFile);
                }
                else
                {
                    _source = _engine.CreateScriptSourceFromString(container.Script);
                }

                _code = _source.Compile(listener);

                if (listener.Errors.Count == 0)
                {
                    // Just create the global scope, don't execute it yet
                    _scope = _engine.CreateScope();
                    _scope.SetVariable("CANAPEScriptContainer", new ScriptContainer(container));
                }
            }
            catch (Exception ex)
            {
                listener.Errors.Add(new ScriptError(ex.Message, "FatalError", 0, 0));
            }

            return listener.Errors.ToArray();
        }