ConvertToNumber() public method

Converts a value to Number using regular JavaScript semantics
Requires an active script context.
public ConvertToNumber ( ) : IeJsValue
return IeJsValue
Example #1
0
        /// <summary>
        /// Makes a mapping of value from the script type to a host type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        private object MapToHostType(IeJsValue value)
        {
            JsValueType valueType = value.ValueType;
            IeJsValue   processedValue;
            object      result;

            switch (valueType)
            {
            case JsValueType.Null:
                result = null;
                break;

            case JsValueType.Undefined:
                result = Undefined.Value;
                break;

            case JsValueType.Boolean:
                processedValue = value.ConvertToBoolean();
                result         = processedValue.ToBoolean();
                break;

            case JsValueType.Number:
                processedValue = value.ConvertToNumber();
                result         = NumericHelpers.CastDoubleValueToCorrectType(processedValue.ToDouble());
                break;

            case JsValueType.String:
                processedValue = value.ConvertToString();
                result         = processedValue.ToString();
                break;

            case JsValueType.Object:
            case JsValueType.Function:
            case JsValueType.Error:
            case JsValueType.Array:
#if NETSTANDARD1_3
                result = ToObject(value);
#else
                processedValue = value.ConvertToObject();
                object obj = processedValue.ToObject();

                if (!TypeConverter.IsPrimitiveType(obj.GetType()))
                {
                    var hostObj = obj as HostObject;
                    result = hostObj != null ? hostObj.Target : obj;
                }
                else
                {
                    result = obj;
                }
#endif
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(result);
        }
Example #2
0
        private JsRuntimeException ConvertJsExceptionToJsRuntimeException(
            JsException jsException)
        {
            string message        = jsException.Message;
            string category       = string.Empty;
            int    lineNumber     = 0;
            int    columnNumber   = 0;
            string sourceFragment = string.Empty;

            var jsScriptException = jsException as IeJsScriptException;

            if (jsScriptException != null)
            {
                category = "Script error";
                IeJsValue errorValue = jsScriptException.Error;

                IeJsPropertyId messagePropertyId    = IeJsPropertyId.FromString("message");
                IeJsValue      messagePropertyValue = errorValue.GetProperty(messagePropertyId);
                string         scriptMessage        = messagePropertyValue.ConvertToString().ToString();
                if (!string.IsNullOrWhiteSpace(scriptMessage))
                {
                    message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage);
                }

                IeJsPropertyId linePropertyId = IeJsPropertyId.FromString("line");
                if (errorValue.HasProperty(linePropertyId))
                {
                    IeJsValue linePropertyValue = errorValue.GetProperty(linePropertyId);
                    lineNumber = (int)linePropertyValue.ConvertToNumber().ToDouble() + 1;
                }

                IeJsPropertyId columnPropertyId = IeJsPropertyId.FromString("column");
                if (errorValue.HasProperty(columnPropertyId))
                {
                    IeJsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId);
                    columnNumber = (int)columnPropertyValue.ConvertToNumber().ToDouble() + 1;
                }

                IeJsPropertyId sourcePropertyId = IeJsPropertyId.FromString("source");
                if (errorValue.HasProperty(sourcePropertyId))
                {
                    IeJsValue sourcePropertyValue = errorValue.GetProperty(sourcePropertyId);
                    sourceFragment = sourcePropertyValue.ConvertToString().ToString();
                }
            }
            else if (jsException is JsUsageException)
            {
                category = "Usage error";
            }
            else if (jsException is JsEngineException)
            {
                category = "Engine error";
            }
            else if (jsException is JsFatalException)
            {
                category = "Fatal error";
            }

            var jsEngineException = new JsRuntimeException(message, _engineModeName)
            {
                ErrorCode      = ((uint)jsException.ErrorCode).ToString(CultureInfo.InvariantCulture),
                Category       = category,
                LineNumber     = lineNumber,
                ColumnNumber   = columnNumber,
                SourceFragment = sourceFragment
            };

            return(jsEngineException);
        }