Beispiel #1
0
        /// <summary>
        /// Adds the specified code to the scripting engine under the specified namespace.
        /// The code that is added will be available only under the namespace specified
        /// instead of in the global scope of the script. The script name will be used
        /// to provide more useful error information if an error occurs during the execution
        /// of the code that was added.
        /// </summary>
        /// <param name="code">The code to be added.</param>
        /// <param name="namespaceName">The name of the namespace to add the code to.</param>
        /// <param name="scriptName">The script name that the code came from.</param>
        /// <exception cref="ArgumentNullException">If code is null.</exception>
        /// <exception cref="ArgumentException">If code is blank.</exception>
        public void AddCode(string code, string namespaceName, string scriptName)
        {
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }

            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentException("code parameter must contain code", "code");
            }

            if (namespaceName != null)
            {
                activeScript.AddNamedItem(namespaceName, ScriptItemFlags.CodeOnly | ScriptItemFlags.IsVisible);
            }

            try
            {
                /*
                 * In the event that the passed in script is not valid syntax
                 * an error will be thrown by the script engine. This will be
                 * handled by the OnScriptError event before the exception
                 * is thrown here so we need to set this variable to use
                 * in the OnScriptError block to figure out the script name
                 * since it won't have been added to the script list.
                 */
                scriptToParse = scriptName;

                EXCEPINFO exceptionInfo = new EXCEPINFO();

                ulong cookie = (ulong)scripts.Count;

                parser.ParseScriptText(
                    code: code,
                    itemName: namespaceName,
                    context: null,
                    delimiter: null,
                    sourceContext: cookie,
                    startingLineNumber: 1u,
                    flags: ScriptTextFlags.IsVisible,
                    pVarResult: IntPtr.Zero,
                    excepInfo: out exceptionInfo);

                ScriptInfo si = new ScriptInfo()
                {
                    Code       = code,
                    ScriptName = scriptName
                };

                scripts.Add(cookie, si);
            }
            finally
            {
                scriptToParse = null;
            }
        }
Beispiel #2
0
        public static object CreateScriptObject(ScriptSiteBase scriptSite, string scriptText)
        {
            IActiveScript      engine = (IActiveScript)engineCache[scriptSite];
            IActiveScriptParse parser = (IActiveScriptParse)engine;

            if (engine == null)
            {
                engine = (IActiveScript) new JScriptEngine();
                engine.SetScriptSite(scriptSite);
                foreach (string name in scriptSite.GetNamedItems())
                {
                    engine.AddNamedItem(name, ScriptItem.IsVisible);
                }
                parser = (IActiveScriptParse)engine;
                parser.InitNew();
                engineCache.Add(scriptSite, engine);
            }

            EXCEPINFO ei;
            object    result;
            IScript   scriptObject;

            parser.ParseScriptText(scriptText, null, null, null, IntPtr.Zero, 1, ScriptText.None, out result, out ei);
            engine.GetScriptDispatch(null, out scriptObject);

            return(scriptObject);
        }
Beispiel #3
0
        private void EmbedHostItem(string itemName, object value)
        {
            InvokeScript(() =>
            {
                object oldValue = null;
                if (_hostItems.ContainsKey(itemName))
                {
                    oldValue = _hostItems[itemName];
                }
                _hostItems[itemName] = value;

                try
                {
                    _activeScript.AddNamedItem(itemName, ScriptItemFlags.IsVisible | ScriptItemFlags.GlobalMembers);
                }
                catch (Exception)
                {
                    if (oldValue != null)
                    {
                        _hostItems[itemName] = oldValue;
                    }
                    else
                    {
                        _hostItems.Remove(itemName);
                    }

                    throw;
                }
            });
        }
 /// <summary>
 /// Adds the name of a root-level item to the scripting engine's name space.
 /// </summary>
 /// <param name="name">The name. May not be null.</param>
 /// <param name="value">The value. It must be a ComVisible object.</param>
 public void SetNamedItem(string name, object value)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     _engine.AddNamedItem(name, ScriptItem.IsVisible | ScriptItem.IsSource);
     Site.NamedItems[name] = value;
 }
 public void AddGlobalValue(string name, object globalData)
 {
     globals[name] = globalData;
     engine.AddNamedItem(name, ScriptItemFlags.IsVisible);
 }
Beispiel #6
0
 public override void AddNamedItem(string name, ScriptItemFlags flags)
 {
     activeScript.AddNamedItem(name, flags);
 }
Beispiel #7
0
 /// <summary>
 /// Adds the name of a root-level item to the scripting engine's name space. A root-level item
 /// is an object with properties and methods, an event source, or all three.
 /// </summary>
 /// <param name="name">The name of the item as viewed from the script. The name must be unique
 /// and persistable</param>
 /// <param name="flags">Flags associated with an item</param>
 public void AddNamedItem(string name, ScriptItemFlags flags)
 {
     _activeScript.AddNamedItem(name, flags);
 }