Example #1
0
        public Jint.Native.JsValue __GetJsValue(string property)
        {
            Jint.Native.Object.ObjectInstance o = this._jsValue.AsObject();
            var jsValue = o.Get(property);

            return(jsValue);
        }
        private JsRuntimeException ConvertJavaScriptExceptionToJsRuntimeException(
            OriginalJsException jsException)
        {
            string          category     = string.Empty;
            int             lineNumber   = jsException.LineNumber;
            int             columnNumber = jsException.Column + 1;
            string          message      = jsException.Message;
            OriginalJsValue errorValue   = jsException.Error;

            if (errorValue.IsObject())
            {
                OriginalObjectInstance errorObject           = errorValue.AsObject();
                OriginalJsValue        categoryPropertyValue = errorObject.Get("name");

                if (categoryPropertyValue.IsString())
                {
                    category = categoryPropertyValue.AsString();
                }

                message = GenerateErrorMessageWithLocation(category, message,
                                                           jsException.Location.Source, lineNumber, columnNumber);
            }

            var jsRuntimeException = new JsRuntimeException(message, EngineName, EngineVersion,
                                                            jsException)
            {
                Category     = category,
                LineNumber   = lineNumber,
                ColumnNumber = columnNumber
            };

            return(jsRuntimeException);
        }
Example #3
0
        private JsRuntimeException ConvertJavaScriptExceptionToJsRuntimeException(
            OriginalJsException jsException)
        {
            string          category   = string.Empty;
            OriginalJsValue errorValue = jsException.Error;

            if (errorValue.IsObject())
            {
                OriginalObjectInstance errorObject           = errorValue.AsObject();
                OriginalJsValue        categoryPropertyValue = errorObject.Get("name");

                if (categoryPropertyValue.IsString())
                {
                    category = categoryPropertyValue.AsString();
                }
            }

            var jsRuntimeException = new JsRuntimeException(jsException.Message, ENGINE_NAME, ENGINE_VERSION,
                                                            jsException)
            {
                Category     = category,
                LineNumber   = jsException.LineNumber,
                ColumnNumber = jsException.Column,
                Source       = jsException.Source,
                HelpLink     = jsException.HelpLink
            };

            return(jsRuntimeException);
        }
Example #4
0
        private object AsPOJO(Jint.Native.JsValue JsValue)
        {
            if (JsValue.Type == Jint.Runtime.Types.Boolean)
            {
                return(Jint.Runtime.TypeConverter.ToBoolean(JsValue));
            }

            else if (JsValue.Type == Jint.Runtime.Types.Number)
            {
                double AsDouble = Jint.Runtime.TypeConverter.ToNumber(JsValue);
                int    AsInt    = Jint.Runtime.TypeConverter.ToInt32(JsValue);
                var    diff     = Math.Abs(AsDouble - AsInt);
                if (diff < 0.00000001)
                {
                    return(AsInt);
                }
                else
                {
                    return(AsDouble);
                }
            }

            else if (JsValue.Type == Jint.Runtime.Types.String)
            {
                return(Jint.Runtime.TypeConverter.ToString(JsValue));
            }

            else // if (JsValue.Type == Jint.Runtime.Types.Object)
            {
                Jint.Native.Object.ObjectInstance AsObject =
                    JsValue.TryCast <Jint.Native.Object.ObjectInstance>();

                if (AsObject is Jint.Native.Array.ArrayInstance)
                {
                    int      length = Jint.Runtime.TypeConverter.ToInt32(AsObject.Get("length"));
                    object[] array  = new object[length];
                    for (int i = 0; i < length; i++)
                    {
                        array[i] = AsPOJO(AsObject.Get(i.ToString()));
                    }
                    return(array);
                }

                else
                {
                    Dictionary <string, object> AsDictionary = new Dictionary <string, object>();

                    foreach (string Property in AsObject.Properties.Keys)
                    {
                        AsDictionary.Add(Property, AsPOJO(AsObject.Get(Property)));
                    }

                    return(AsDictionary);
                }
            }
        }
        private static WrapperException WrapJavaScriptException(
            OriginalJavaScriptException originalJavaScriptException)
        {
            WrapperException wrapperException;
            string           message = originalJavaScriptException.Message;

            if (string.IsNullOrWhiteSpace(message))
            {
                message = "An unknown error occurred";
            }
            string description  = message;
            string type         = string.Empty;
            string documentName = originalJavaScriptException.Location.Source;
            int    lineNumber   = originalJavaScriptException.LineNumber;
            int    columnNumber = originalJavaScriptException.Column + 1;

            OriginalValue errorValue = originalJavaScriptException.Error;

            if (errorValue.IsObject())
            {
                OriginalObjectInstance errorObject = errorValue.AsObject();

                OriginalValue namePropertyValue = errorObject.Get("name");
                if (namePropertyValue.IsString())
                {
                    type = namePropertyValue.AsString();
                }
            }

            if (!string.IsNullOrEmpty(type))
            {
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
                                                                    columnNumber);

                var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                          originalJavaScriptException)
                {
                    Type         = type,
                    DocumentName = documentName,
                    LineNumber   = lineNumber,
                    ColumnNumber = columnNumber
                };

                wrapperException = wrapperRuntimeException;
            }
            else
            {
                wrapperException = new WrapperException(message, EngineName, EngineVersion,
                                                        originalJavaScriptException);
            }

            wrapperException.Description = description;

            return(wrapperException);
        }
Example #6
0
        private Dictionary <string, string> extractOptions(Jint.Native.Object.ObjectInstance args)
        {
            var defaults = new Dictionary <string, string>()
            {
                { "url", null },
                { "user", null },
                { "password", null },
                { "method", "POST" },
                { "mimeType", "" },
                { "encoding", "UTF-8" },
                { "responseType", "" },
                { "transport", "browser" }
            };

            var options = new Dictionary <string, string>();

            if (openParameters != null)
            {
                foreach (string key in openParameters.Keys)
                {
                    options[key] = openParameters[key] as string;
                }
            }
            if (args != null)
            {
                foreach (string key in defaults.Keys)
                {
                    options.Add(key, args.Get(key).AsString());
                }
            }
            string value;

            foreach (var item in defaults)
            {
                if (!options.TryGetValue(item.Key, out value))
                {
                    options.Add(item.Key, item.Value);
                }
            }

            // make sure this one is uppercase
            options["method"] = options["method"].ToUpper();

            return(options);
        }
        private WrapperRuntimeException WrapRuntimeException(OriginalRuntimeException originalRuntimeException)
        {
            WrapperRuntimeException wrapperRuntimeException;
            string message = originalRuntimeException.Message;

            if (string.IsNullOrWhiteSpace(message))
            {
                message = "An unknown error occurred";
            }
            string description  = message;
            string type         = string.Empty;
            string documentName = string.Empty;
            int    lineNumber   = 0;
            int    columnNumber = 0;
            string callStack    = string.Empty;

            if (originalRuntimeException is OriginalJavaScriptException)
            {
                var originalJavaScriptException = (OriginalJavaScriptException)originalRuntimeException;
                documentName = originalJavaScriptException.Location.Source;
                lineNumber   = originalJavaScriptException.LineNumber;
                columnNumber = originalJavaScriptException.Column + 1;

                OriginalValue errorValue = originalJavaScriptException.Error;
                if (errorValue.IsObject())
                {
                    OriginalObjectInstance errorObject = errorValue.AsObject();

                    OriginalValue namePropertyValue = errorObject.Get("name");
                    if (namePropertyValue.IsString())
                    {
                        type = namePropertyValue.AsString();
                    }
                }

                if (string.IsNullOrEmpty(type))
                {
                    type = JsErrorType.Common;
                }

                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
                                                                    columnNumber);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalJavaScriptException);
            }
            else if (originalRuntimeException is OriginalMemoryLimitExceededException)
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalRecursionDepthOverflowException)
            {
                var      originalRecursionException = (OriginalRecursionDepthOverflowException)originalRuntimeException;
                string[] callChainItems             = originalRecursionException.CallChain
                                                      .Split(new string[] { "->" }, StringSplitOptions.None)
                ;

                if (callChainItems.Length > 0)
                {
                    var           stringBuilderPool = StringBuilderPool.Shared;
                    StringBuilder stackBuilder      = stringBuilderPool.Rent();

                    for (int chainItemIndex = callChainItems.Length - 1; chainItemIndex >= 0; chainItemIndex--)
                    {
                        string chainItem = callChainItems[chainItemIndex];
                        if (chainItem == "anonymous function")
                        {
                            chainItem = "Anonymous function";
                        }

                        JsErrorHelpers.WriteErrorLocationLine(stackBuilder, chainItem, string.Empty, 0, 0);
                        if (chainItemIndex > 0)
                        {
                            stackBuilder.AppendLine();
                        }
                    }

                    callStack = stackBuilder.ToString();
                    stringBuilderPool.Return(stackBuilder);
                }

                type    = JsErrorType.Range;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRecursionException);
            }
            else if (originalRuntimeException is OriginalStatementsCountOverflowException)
            {
                type    = JsErrorType.Range;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalExecutionCanceledException)
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationConstraint.Reset(_cancellationTokenSource.Token);

                type        = JsErrorType.Common;
                message     = CoreStrings.Runtime_ScriptInterrupted;
                description = message;

                wrapperRuntimeException = new WrapperInterruptedException(message,
                                                                          EngineName, EngineVersion, originalRuntimeException);
            }
            else
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }

            wrapperRuntimeException.Description  = description;
            wrapperRuntimeException.Type         = type;
            wrapperRuntimeException.DocumentName = documentName;
            wrapperRuntimeException.LineNumber   = lineNumber;
            wrapperRuntimeException.ColumnNumber = columnNumber;
            wrapperRuntimeException.CallStack    = callStack;

            return(wrapperRuntimeException);
        }
Example #8
0
        private WrapperRuntimeException WrapRuntimeException(OriginalRuntimeException originalRuntimeException)
        {
            WrapperRuntimeException wrapperRuntimeException;
            string message = originalRuntimeException.Message;

            if (string.IsNullOrWhiteSpace(message))
            {
                message = "An unknown error occurred";
            }
            string description  = message;
            string type         = string.Empty;
            string documentName = string.Empty;
            int    lineNumber   = 0;
            int    columnNumber = 0;
            string callStack    = string.Empty;

            if (originalRuntimeException is OriginalJavaScriptException)
            {
                var originalJavaScriptException = (OriginalJavaScriptException)originalRuntimeException;
                documentName = originalJavaScriptException.Location.Source;
                lineNumber   = originalJavaScriptException.LineNumber;
                columnNumber = originalJavaScriptException.Column + 1;

                ErrorLocationItem[] callStackItems = JintJsErrorHelpers.ParseErrorLocation(
                    originalJavaScriptException.StackTrace);
                if (callStackItems.Length > 0)
                {
                    JintJsErrorHelpers.FixErrorLocationItems(callStackItems);
                    callStack = JsErrorHelpers.StringifyErrorLocationItems(callStackItems, true);
                }

                OriginalValue errorValue = originalJavaScriptException.Error;
                if (errorValue.IsObject())
                {
                    OriginalObjectInstance errorObject = errorValue.AsObject();

                    OriginalValue namePropertyValue = errorObject.Get("name");
                    if (namePropertyValue.IsString())
                    {
                        type = namePropertyValue.AsString();
                    }
                }

                if (string.IsNullOrEmpty(type))
                {
                    type = JsErrorType.Common;
                }

                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalJavaScriptException);
            }
            else if (originalRuntimeException is OriginalMemoryLimitExceededException)
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalRecursionDepthOverflowException)
            {
                var originalRecursionException = (OriginalRecursionDepthOverflowException)originalRuntimeException;
                callStack = JintJsErrorHelpers.ConvertCallChainToStack(originalRecursionException.CallChain);
                type      = JsErrorType.Range;
                message   = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRecursionException);
            }
            else if (originalRuntimeException is OriginalStatementsCountOverflowException)
            {
                type    = JsErrorType.Range;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalExecutionCanceledException)
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationConstraint.Reset(_cancellationTokenSource.Token);

                type        = JsErrorType.Common;
                message     = CoreStrings.Runtime_ScriptInterrupted;
                description = message;

                wrapperRuntimeException = new WrapperInterruptedException(message,
                                                                          EngineName, EngineVersion, originalRuntimeException);
            }
            else
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }

            wrapperRuntimeException.Description  = description;
            wrapperRuntimeException.Type         = type;
            wrapperRuntimeException.DocumentName = documentName;
            wrapperRuntimeException.LineNumber   = lineNumber;
            wrapperRuntimeException.ColumnNumber = columnNumber;
            wrapperRuntimeException.CallStack    = callStack;

            return(wrapperRuntimeException);
        }