/// <summary> /// Makes a mapping of value from the script type to a .net type /// added by chuan.yin in 2017/4/27 /// </summary> /// <param name="value">The source value</param> /// <returns>The mapped value</returns> private object ConvertJsObjectToNetObject(JsValue value) { JsValueType valueType = value.ValueType; JsValue 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: result = MapToNetObject(value); break; case JsValueType.Function: case JsValueType.Error: case JsValueType.Array: result = ToArray(value); break; case JsValueType.Symbol: case JsValueType.ArrayBuffer: case JsValueType.TypedArray: case JsValueType.DataView: result = ToObject(value); break; default: throw new ArgumentOutOfRangeException(); } return(result); }
private static WrapperException WrapJsException(OriginalException originalException, string defaultDocumentName = null) { WrapperException wrapperException; JsErrorCode errorCode = originalException.ErrorCode; string description = originalException.Message; string message = description; string type = string.Empty; string documentName = defaultDocumentName ?? string.Empty; int lineNumber = 0; int columnNumber = 0; string callStack = string.Empty; string sourceFragment = string.Empty; var originalScriptException = originalException as OriginalScriptException; if (originalScriptException != null) { JsValue metadataValue = originalScriptException.Metadata; if (metadataValue.IsValid) { JsValue errorValue = metadataValue.GetProperty("exception"); JsValueType errorValueType = errorValue.ValueType; if (errorValueType == JsValueType.Error || errorValueType == JsValueType.Object) { JsValue messagePropertyValue = errorValue.GetProperty("message"); description = messagePropertyValue.ConvertToString().ToString(); JsValue namePropertyValue = errorValue.GetProperty("name"); type = namePropertyValue.ValueType == JsValueType.String ? namePropertyValue.ConvertToString().ToString() : string.Empty; JsPropertyId urlPropertyId = JsPropertyId.FromString("url"); if (metadataValue.HasProperty(urlPropertyId)) { JsValue urlPropertyValue = metadataValue.GetProperty(urlPropertyId); string url = urlPropertyValue.ConvertToString().ToString(); if (url != "undefined") { documentName = url; } } JsPropertyId linePropertyId = JsPropertyId.FromString("line"); if (metadataValue.HasProperty(linePropertyId)) { JsValue linePropertyValue = metadataValue.GetProperty(linePropertyId); lineNumber = linePropertyValue.ConvertToNumber().ToInt32() + 1; } JsPropertyId columnPropertyId = JsPropertyId.FromString("column"); if (metadataValue.HasProperty(columnPropertyId)) { JsValue columnPropertyValue = metadataValue.GetProperty(columnPropertyId); columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1; } string sourceLine = string.Empty; JsPropertyId sourcePropertyId = JsPropertyId.FromString("source"); if (metadataValue.HasProperty(sourcePropertyId)) { JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId); sourceLine = sourcePropertyValue.ConvertToString().ToString(); sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber); } JsPropertyId stackPropertyId = JsPropertyId.FromString("stack"); if (errorValue.HasProperty(stackPropertyId)) { JsPropertyId descriptionPropertyId = JsPropertyId.FromString("description"); if (errorValue.HasProperty(descriptionPropertyId)) { JsValue descriptionPropertyValue = errorValue.GetProperty(descriptionPropertyId); if (descriptionPropertyValue.ValueType == JsValueType.String || descriptionPropertyValue.StringLength > 0) { description = descriptionPropertyValue.ConvertToString().ToString(); } } JsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId); string messageWithTypeAndCallStack = stackPropertyValue.ValueType == JsValueType.String ? stackPropertyValue.ConvertToString().ToString() : string.Empty; string messageWithType = errorValue.ConvertToString().ToString(); string rawCallStack = messageWithTypeAndCallStack .TrimStart(messageWithType) .TrimStart("Error") .TrimStart(new char[] { '\n', '\r' }) ; string callStackWithSourceFragment = string.Empty; ErrorLocationItem[] callStackItems = CoreErrorHelpers.ParseErrorLocation(rawCallStack); if (callStackItems.Length > 0) { ErrorLocationItem firstCallStackItem = callStackItems[0]; firstCallStackItem.SourceFragment = sourceFragment; documentName = firstCallStackItem.DocumentName; lineNumber = firstCallStackItem.LineNumber; columnNumber = firstCallStackItem.ColumnNumber; callStack = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems, true); callStackWithSourceFragment = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems); } message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description, callStackWithSourceFragment); } else { message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber, columnNumber, sourceFragment); } } else { message = errorValue.ConvertToString().ToString(); description = message; } } WrapperScriptException wrapperScriptException; if (errorCode == JsErrorCode.ScriptCompile) { wrapperScriptException = new WrapperCompilationException(message, EngineName, EngineVersion, originalScriptException); } else if (errorCode == JsErrorCode.ScriptTerminated) { message = CoreStrings.Runtime_ScriptInterrupted; description = message; wrapperScriptException = new WrapperInterruptedException(message, EngineName, EngineVersion, originalScriptException) { CallStack = callStack }; } else { wrapperScriptException = new WrapperRuntimeException(message, EngineName, EngineVersion, originalScriptException) { CallStack = callStack }; } wrapperScriptException.Type = type; wrapperScriptException.DocumentName = documentName; wrapperScriptException.LineNumber = lineNumber; wrapperScriptException.ColumnNumber = columnNumber; wrapperScriptException.SourceFragment = sourceFragment; wrapperException = wrapperScriptException; } else { if (originalException is OriginalUsageException) { wrapperException = new WrapperUsageException(message, EngineName, EngineVersion, originalException); } else if (originalException is OriginalEngineException) { wrapperException = new WrapperEngineException(message, EngineName, EngineVersion, originalException); } else if (originalException is OriginalFatalException) { wrapperException = new WrapperFatalException(message, EngineName, EngineVersion, originalException); } else { wrapperException = new WrapperException(message, EngineName, EngineVersion, originalException); } } wrapperException.Description = description; return(wrapperException); }
private static JsRuntimeException ConvertJsExceptionToJsRuntimeException( OriginalJsException jsException) { string message = jsException.Message; string category = string.Empty; int lineNumber = 0; int columnNumber = 0; string sourceFragment = string.Empty; var jsScriptException = jsException as JsScriptException; if (jsScriptException != null) { category = "Script error"; JsValue errorValue = jsScriptException.Error; JsPropertyId stackPropertyId = JsPropertyId.FromString("stack"); if (errorValue.HasProperty(stackPropertyId)) { JsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId); message = stackPropertyValue.ConvertToString().ToString(); } else { JsValue messagePropertyValue = errorValue.GetProperty("message"); string scriptMessage = messagePropertyValue.ConvertToString().ToString(); if (!string.IsNullOrWhiteSpace(scriptMessage)) { message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage); } } JsPropertyId linePropertyId = JsPropertyId.FromString("line"); if (errorValue.HasProperty(linePropertyId)) { JsValue linePropertyValue = errorValue.GetProperty(linePropertyId); lineNumber = linePropertyValue.ConvertToNumber().ToInt32() + 1; } JsPropertyId columnPropertyId = JsPropertyId.FromString("column"); if (errorValue.HasProperty(columnPropertyId)) { JsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId); columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1; } if (lineNumber <= 0 && columnNumber <= 0) { Match errorStringMatch = _errorStringRegex.Match(message); if (errorStringMatch.Success) { GroupCollection errorStringGroups = errorStringMatch.Groups; lineNumber = int.Parse(errorStringGroups["lineNumber"].Value); columnNumber = int.Parse(errorStringGroups["columnNumber"].Value); } } JsPropertyId sourcePropertyId = JsPropertyId.FromString("source"); if (errorValue.HasProperty(sourcePropertyId)) { JsValue 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, EngineName, EngineVersion) { ErrorCode = ((uint)jsException.ErrorCode).ToString(CultureInfo.InvariantCulture), Category = category, LineNumber = lineNumber, ColumnNumber = columnNumber, SourceFragment = sourceFragment }; return(jsEngineException); }