Ejemplo n.º 1
0
        public virtual object GetVariableValue(string variableName)
        {
            VerifyNotDisposed();

            if (variableName == null)
            {
                throw new ArgumentNullException(
                          nameof(variableName),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(variableName))
                          );
            }

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, nameof(variableName)),
                          nameof(variableName)
                          );
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Usage_InvalidVariableNameFormat, variableName),
                          nameof(variableName)
                          );
            }

            return(InnerGetVariableValue(variableName));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Embeds a host type to script code
        /// </summary>
        /// <param name="itemName">The name for the new global variable that will represent the type</param>
        /// <param name="type">The type to expose</param>
        /// <remarks>
        /// Host types are exposed to script code in the form of objects whose properties and
        /// methods are bound to the type's static members.
        /// </remarks>
        public void EmbedHostType(string itemName, Type type)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(itemName))
            {
                throw new ArgumentException(
                          string.Format(CommonStrings.Common_ArgumentIsEmpty, "itemName"), "itemName");
            }

            if (!ValidationHelpers.CheckNameFormat(itemName))
            {
                throw new FormatException(
                          string.Format(CommonStrings.Runtime_InvalidScriptItemNameFormat, itemName));
            }

            if (type != null)
            {
                if (ValidationHelpers.IsPrimitiveType(type) ||
                    type == typeof(Undefined))
                {
                    throw new NotSupportedTypeException(
                              string.Format(CommonStrings.Runtime_EmbeddedHostTypeNotSupported, type.FullName));
                }
            }
            else
            {
                throw new ArgumentNullException("type", string.Format(CommonStrings.Common_ArgumentIsNull, "type"));
            }

            _jsEngine.EmbedHostType(itemName, type);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets a value of variable
        /// </summary>
        /// <param name="variableName">Name of variable</param>
        /// <param name="value">Value of variable</param>
        /// <exception cref="System.ObjectDisposedException">Operation is performed on a disposed MSIE
        /// JavaScript engine.</exception>
        /// <exception cref="System.ArgumentException" />
        /// <exception cref="System.FormatException">The variable name has incorrect format.</exception>
        /// <exception cref="MsieJavaScriptEngine.NotSupportedTypeException">The type of variable value
        /// is not supported.</exception>
        /// <exception cref="MsieJavaScriptEngine.JsRuntimeException">JavaScript runtime error.</exception>
        public void SetVariableValue(string variableName, object value)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(CommonStrings.Common_ArgumentIsEmpty, "variableName"), "variableName");
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new FormatException(
                          string.Format(CommonStrings.Runtime_InvalidVariableNameFormat, variableName));
            }

            if (value != null)
            {
                Type variableType = value.GetType();

                if (!ValidationHelpers.IsSupportedType(variableType))
                {
                    throw new NotSupportedTypeException(
                              string.Format(CommonStrings.Runtime_VariableTypeNotSupported,
                                            variableName, variableType.FullName));
                }
            }

            _jsEngine.SetVariableValue(variableName, value);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets a value of variable
        /// </summary>
        /// <typeparam name="T">Type of variable</typeparam>
        /// <param name="variableName">Name of variable</param>
        /// <returns>Value of variable</returns>
        /// <exception cref="System.ObjectDisposedException">Operation is performed on a disposed MSIE
        /// JavaScript engine.</exception>
        /// <exception cref="System.ArgumentException" />
        /// <exception cref="System.FormatException">The variable name has incorrect format.</exception>
        /// <exception cref="MsieJavaScriptEngine.NotSupportedTypeException">The type of return value
        /// is not supported.</exception>
        /// <exception cref="MsieJavaScriptEngine.JsRuntimeException">JavaScript runtime error.</exception>
        public T GetVariableValue <T>(string variableName)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(CommonStrings.Common_ArgumentIsEmpty, "variableName"), "variableName");
            }

            Type returnValueType = typeof(T);

            if (!ValidationHelpers.IsSupportedType(returnValueType))
            {
                throw new NotSupportedTypeException(
                          string.Format(CommonStrings.Runtime_ReturnValueTypeNotSupported, returnValueType.FullName));
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new FormatException(
                          string.Format(CommonStrings.Runtime_InvalidVariableNameFormat, variableName));
            }

            object result = _jsEngine.GetVariableValue(variableName);

            return(TypeConverter.ConvertToType <T>(result));
        }
        public virtual void EmbedHostObject(string itemName, object value)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(itemName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, "itemName"), "itemName");
            }

            if (!ValidationHelpers.CheckNameFormat(itemName))
            {
                throw new FormatException(
                          string.Format(Strings.Runtime_InvalidScriptItemNameFormat, itemName));
            }

            if (value != null)
            {
                Type itemType = value.GetType();

                if (ValidationHelpers.IsPrimitiveType(itemType) ||
                    itemType == typeof(Undefined) ||
                    itemType == typeof(DateTime))
                {
                    throw new NotSupportedTypeException(
                              string.Format(Strings.Runtime_EmbeddedHostObjectTypeNotSupported, itemType.FullName));
                }
            }
            else
            {
                throw new ArgumentNullException("value", string.Format(Strings.Common_ArgumentIsNull, "value"));
            }

            InnerEmbedHostObject(itemName, value);
        }
        public virtual T GetVariableValue <T>(string variableName)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, "variableName"), "variableName");
            }

            Type returnValueType = typeof(T);

            if (!ValidationHelpers.IsSupportedType(returnValueType))
            {
                throw new NotSupportedTypeException(
                          string.Format(Strings.Runtime_ReturnValueTypeNotSupported, returnValueType.FullName));
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new FormatException(
                          string.Format(Strings.Runtime_InvalidVariableNameFormat, variableName));
            }

            return(InnerGetVariableValue <T>(variableName));
        }
Ejemplo n.º 7
0
        public virtual object CallFunction(string functionName, params object[] args)
        {
            VerifyNotDisposed();

            if (functionName == null)
            {
                throw new ArgumentNullException(
                          nameof(functionName),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(functionName))
                          );
            }

            if (string.IsNullOrWhiteSpace(functionName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, nameof(functionName)),
                          nameof(functionName)
                          );
            }

            if (!ValidationHelpers.CheckNameFormat(functionName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Usage_InvalidFunctionNameFormat, nameof(functionName)),
                          nameof(functionName)
                          );
            }

            int argumentCount = args.Length;

            if (argumentCount > 0)
            {
                for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
                {
                    object argument = args[argumentIndex];

                    if (argument != null)
                    {
                        Type argType = argument.GetType();

                        if (!ValidationHelpers.IsSupportedType(argType))
                        {
                            throw new ArgumentException(
                                      string.Format(Strings.Usage_FunctionParameterTypeNotSupported,
                                                    functionName, argType.FullName),
                                      nameof(args)
                                      );
                        }
                    }
                }
            }

            return(InnerCallFunction(functionName, args));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Calls a function
        /// </summary>
        /// <typeparam name="T">Type of function result</typeparam>
        /// <param name="functionName">Function name</param>
        /// <param name="args">Function arguments</param>
        /// <returns>Result of the function execution</returns>
        /// <exception cref="System.ObjectDisposedException">Operation is performed on a disposed MSIE
        /// JavaScript engine.</exception>
        /// <exception cref="System.ArgumentException" />
        /// <exception cref="System.FormatException">The function name has incorrect format.</exception>
        /// <exception cref="MsieJavaScriptEngine.NotSupportedTypeException">The type of return value or
        /// one function parameter is not supported.</exception>
        /// <exception cref="MsieJavaScriptEngine.JsRuntimeException">JavaScript runtime error.</exception>
        public T CallFunction <T>(string functionName, params object[] args)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(functionName))
            {
                throw new ArgumentException(
                          string.Format(CommonStrings.Common_ArgumentIsEmpty, "functionName"), "functionName");
            }

            Type returnValueType = typeof(T);

            if (!ValidationHelpers.IsSupportedType(returnValueType))
            {
                throw new NotSupportedTypeException(
                          string.Format(CommonStrings.Runtime_ReturnValueTypeNotSupported, returnValueType.FullName));
            }

            if (!ValidationHelpers.CheckNameFormat(functionName))
            {
                throw new FormatException(
                          string.Format(CommonStrings.Runtime_InvalidFunctionNameFormat, functionName));
            }

            int argumentCount = args.Length;

            if (argumentCount > 0)
            {
                for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
                {
                    object argument = args[argumentIndex];

                    if (argument != null)
                    {
                        Type argType = argument.GetType();

                        if (!ValidationHelpers.IsSupportedType(argType))
                        {
                            throw new NotSupportedTypeException(
                                      string.Format(CommonStrings.Runtime_FunctionParameterTypeNotSupported,
                                                    functionName, argType.FullName));
                        }
                    }
                }
            }

            object result = _jsEngine.CallFunction(functionName, args);

            return(TypeConverter.ConvertToType <T>(result));
        }
Ejemplo n.º 9
0
        public virtual void EmbedHostType(string itemName, Type type)
        {
            VerifyNotDisposed();

            if (itemName == null)
            {
                throw new ArgumentNullException(
                          nameof(itemName),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(itemName))
                          );
            }

            if (type == null)
            {
                throw new ArgumentNullException(
                          nameof(type),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(type))
                          );
            }

            if (string.IsNullOrWhiteSpace(itemName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, nameof(itemName)),
                          nameof(itemName)
                          );
            }

            if (!ValidationHelpers.CheckNameFormat(itemName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Usage_InvalidScriptItemNameFormat, itemName),
                          nameof(itemName)
                          );
            }

            if (ValidationHelpers.IsPrimitiveType(type) || type == typeof(Undefined))
            {
                throw new ArgumentException(
                          string.Format(Strings.Usage_EmbeddedHostTypeNotSupported, type.FullName),
                          nameof(type)
                          );
            }

            InnerEmbedHostType(itemName, type);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Removes a variable
        /// </summary>
        /// <param name="variableName">Name of variable</param>
        /// <exception cref="System.ObjectDisposedException">Operation is performed on a disposed MSIE
        /// JavaScript engine.</exception>
        /// <exception cref="System.ArgumentException" />
        /// <exception cref="System.FormatException">The variable name has incorrect format.</exception>
        /// <exception cref="MsieJavaScriptEngine.JsRuntimeException">JavaScript runtime error.</exception>
        public void RemoveVariable(string variableName)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(CommonStrings.Common_ArgumentIsEmpty, "variableName"), "variableName");
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new FormatException(
                          string.Format(CommonStrings.Runtime_InvalidVariableNameFormat, variableName));
            }

            _jsEngine.RemoveVariable(variableName);
        }
Ejemplo n.º 11
0
        public virtual object GetVariableValue(string variableName)
        {
            VerifyNotDisposed();

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, "variableName"), "variableName");
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new FormatException(
                          string.Format(Strings.Runtime_InvalidVariableNameFormat, variableName));
            }

            return(InnerGetVariableValue(variableName));
        }
Ejemplo n.º 12
0
        public void NameFormatIsWrong()
        {
            // Arrange

            // Act
            bool name1FormatIsWrong = ValidationHelpers.CheckNameFormat("good-parts");
            bool name2FormatIsWrong = ValidationHelpers.CheckNameFormat("1sale");
            bool name3FormatIsWrong = ValidationHelpers.CheckNameFormat("Foo Bar");
            bool name4FormatIsWrong = ValidationHelpers.CheckNameFormat("@grid");
            bool name5FormatIsWrong = ValidationHelpers.CheckNameFormat("2");

            // Assert
            Assert.False(name1FormatIsWrong);
            Assert.False(name2FormatIsWrong);
            Assert.False(name3FormatIsWrong);
            Assert.False(name4FormatIsWrong);
            Assert.False(name5FormatIsWrong);
        }
        public void NameFormatIsCorrect()
        {
            // Arrange

            // Act
            bool name1FormatIsCorrect = ValidationHelpers.CheckNameFormat("good_parts");
            bool name2FormatIsCorrect = ValidationHelpers.CheckNameFormat("i18n");
            bool name3FormatIsCorrect = ValidationHelpers.CheckNameFormat("fooBar");
            bool name4FormatIsCorrect = ValidationHelpers.CheckNameFormat("$grid");
            bool name5FormatIsCorrect = ValidationHelpers.CheckNameFormat("a");

            // Assert
            Assert.IsTrue(name1FormatIsCorrect);
            Assert.IsTrue(name2FormatIsCorrect);
            Assert.IsTrue(name3FormatIsCorrect);
            Assert.IsTrue(name4FormatIsCorrect);
            Assert.IsTrue(name5FormatIsCorrect);
        }
Ejemplo n.º 14
0
        public virtual void SetVariableValue(string variableName, object value)
        {
            VerifyNotDisposed();

            if (variableName == null)
            {
                throw new ArgumentNullException(
                          nameof(variableName),
                          string.Format(Strings.Common_ArgumentIsNull, nameof(variableName))
                          );
            }

            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Common_ArgumentIsEmpty, nameof(variableName)),
                          nameof(variableName)
                          );
            }

            if (!ValidationHelpers.CheckNameFormat(variableName))
            {
                throw new ArgumentException(
                          string.Format(Strings.Usage_InvalidVariableNameFormat, variableName),
                          nameof(variableName)
                          );
            }

            if (value != null)
            {
                Type variableType = value.GetType();

                if (!ValidationHelpers.IsSupportedType(variableType))
                {
                    throw new ArgumentException(
                              string.Format(Strings.Usage_VariableTypeNotSupported,
                                            variableName, variableType.FullName),
                              nameof(value)
                              );
                }
            }

            InnerSetVariableValue(variableName, value);
        }