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); }
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); }
protected override object InnerCallFunction(string functionName, params object[] args) { OriginalJsValue functionValue; try { functionValue = _jsEngine.GetValue(functionName); } catch (OriginalJsException e) { throw ConvertJavaScriptExceptionToJsRuntimeException(e); } var callable = functionValue.TryCast <IOriginalCallable>(); if (callable == null) { throw new JsRuntimeException( string.Format(CoreStrings.Runtime_FunctionNotExist, functionName)); } int argumentCount = args.Length; var processedArgs = new OriginalJsValue[argumentCount]; if (argumentCount > 0) { for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) { processedArgs[argumentIndex] = MapToJintType(args[argumentIndex]); } } OriginalJsValue resultValue; try { resultValue = callable.Call(functionValue, processedArgs); } catch (OriginalJsException e) { throw ConvertJavaScriptExceptionToJsRuntimeException(e); } catch (OriginalRecursionDepthOverflowException e) { throw ConvertRecursionDepthOverflowExceptionToJsRuntimeException(e); } catch (OriginalStatementsCountOverflowException e) { throw ConvertStatementsCountOverflowExceptionToJsRuntimeException(e); } catch (TimeoutException e) { throw ConvertTimeoutExceptionToJsRuntimeException(e); } object result = MapToHostType(resultValue); return(result); }
/// <summary> /// Makes a mapping of value from the host type to a script type /// </summary> /// <param name="value">The source value</param> /// <returns>The mapped value</returns> private OriginalValue MapToScriptType(object value) { if (value is Undefined) { return(OriginalValue.Undefined); } return(OriginalValue.FromObject(_jsEngine, value)); }
/// <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(OriginalValue value) { if (value.IsUndefined()) { return(Undefined.Value); } return(value.ToObject()); }
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); }
public bool TryConvert(OriginalEngine engine, object value, out OriginalValue result) { if (value is Undefined) { result = OriginalValue.Undefined; return(true); } result = OriginalValue.Null; return(false); }
protected override void InnerEmbedHostObject(string itemName, object value) { OriginalJsValue processedValue = MapToJintType(value); try { _jsEngine.SetValue(itemName, processedValue); } catch (OriginalJsException e) { throw ConvertJavaScriptExceptionToJsRuntimeException(e); } }
protected override bool InnerHasVariable(string variableName) { bool result; try { OriginalJsValue variableValue = _jsEngine.GetValue(variableName); result = !variableValue.IsUndefined(); } catch (OriginalJsException) { result = false; } return(result); }
protected override void InnerEmbedHostObject(string itemName, object value) { lock (_executionSynchronizer) { OriginalValue processedValue = MapToScriptType(value); try { _jsEngine.SetValue(itemName, processedValue); } catch (OriginalJavaScriptException e) { throw WrapJavaScriptException(e); } } }
protected override void InnerSetVariableValue(string variableName, object value) { lock (_executionSynchronizer) { OriginalValue processedValue = MapToScriptType(value); try { _jsEngine.SetValue(variableName, processedValue); } catch (OriginalRuntimeException e) { throw WrapRuntimeException(e); } } }
protected override bool InnerHasVariable(string variableName) { bool result; lock (_executionSynchronizer) { try { OriginalValue variableValue = _jsEngine.GetValue(variableName); result = !variableValue.IsUndefined(); } catch (OriginalJavaScriptException) { result = false; } } return(result); }
/// <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(OriginalValue value) { switch (value.Type) { case OriginalTypes.Undefined: return(Undefined.Value); case OriginalTypes.Object: if (!(value is IOriginalPrimitiveInstance)) { return(value); } else { break; } } return(value.ToObject()); }
/// <summary> /// Führt einen HTTP-POST mit Argumenten aus und lädt dann die Antwort herunter. /// </summary> /// <param name="url">die URL</param> /// <param name="value">Ein JavaScript Objekt mit Werten.</param> /// <returns>Die Antwort von der URL</returns> public virtual string WebPost(string url, Jint.Native.JsValue value) { if (!value.IsObject()) { return(WebGet(url)); } var obj = value.AsObject(); using (var wc = new System.Net.WebClient()) { var nvc = new System.Collections.Specialized.NameValueCollection(); foreach (var param in obj.GetOwnProperties()) { nvc.Add(param.Key, param.Value.Value.ToString()); } var uri = new Uri(new Uri("http://localhost:" + Server.Settings.Port.ToString() + "/"), url); var result = wc.UploadValues(uri, "POST", nvc); return(wc.Encoding.GetString(result)); } }
/// <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(OriginalValue value) { switch (value.Type) { case OriginalTypes.Undefined: return(Undefined.Value); case OriginalTypes.Object: var objInstance = value.As <OriginalObjectInstance>(); if (objInstance != null && !_primitiveClassNames.Contains(objInstance.Class)) { return(objInstance); } else { break; } } return(value.ToObject()); }
public static JScriptAliasCommand CreateSilent(string AliasName, string Cost, int CooldownSeconds, string PermissionNeeded, Jint.Native.JsValue func) { return(new JScriptAliasCommand() { CommandAlias = AliasName, CommandsToExecute = null, CooldownSeconds = CooldownSeconds, Permissions = PermissionNeeded, func = func, Silent = true }); }
public void AddHandler(Jint.Native.JsValue Function) { Shared.Scripting.EventHandler EventHandler = new Shared.Scripting.EventHandler(); EventHandler.Function = Function; Event.AddHandler(EventHandler); }
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); }
/// <summary> /// Führt einen HTTP-POST mit Argumenten aus und lädt dann die Antwort herunter. /// </summary> /// <param name="url">die URL</param> /// <param name="value">Ein JavaScript Objekt mit Werten.</param> /// <returns>Die Antwort von der URL</returns> public string wpost(string url, Jint.Native.JsValue value) { return(WebPost(url, value)); }
/// <summary> /// Makes a mapping of value from the host type to a script type /// </summary> /// <param name="value">The source value</param> /// <returns>The mapped value</returns> private OriginalValue MapToScriptType(object value) { return(OriginalValue.FromObject(_jsEngine, value)); }
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); }