private string JSValueToString(JavaScriptValue val) { switch (val.ValueType) { case JavaScriptValueType.Boolean: return(val.ToBoolean() ? "true" : "false"); case JavaScriptValueType.Number: try { return(val.ToInt32().ToString()); } catch (JavaScriptException) { return(val.ToDouble().ToString(CultureInfo.InvariantCulture)); } case JavaScriptValueType.Error: var keys = val.GetOwnPropertyNames(); throw new Exception("Error in js"); case JavaScriptValueType.String: return(val.ToString()); case JavaScriptValueType.Array: // int length = val.GetProperty(JavaScriptPropertyId.FromString("length")).ToInt32(); // return "Array"; case JavaScriptValueType.Object: return(val.ToJsonString()); default: return(val.ValueType.ToString()); } }
private object Visit(JavaScriptValue value, IShibaContext context) { switch (value.ValueType) { case JavaScriptValueType.Error: case JavaScriptValueType.Object: return(VisitJavascriptObject(value)); case JavaScriptValueType.Array: return(VisitJavascriptArray(value)); case JavaScriptValueType.Boolean: return(value.ToBoolean()); case JavaScriptValueType.Null: return(null); case JavaScriptValueType.Number: return(value.ToDouble()); case JavaScriptValueType.String: return(value.ToString()); case JavaScriptValueType.Undefined: case JavaScriptValueType.Function: default: return(value); } }
/// <summary> /// Converts a <see cref="JavaScriptValue"/> with a bool type to a host value. /// </summary> public object ToHostBoolean(JavaScriptValue arg, Type toType) { if (!toType.IsAssignableFrom(typeof(bool))) { throw new Exception($"Cannot convert javascript boolean to type: {toType}"); } return(arg.ToBoolean()); }
/// <summary> /// Converts a JavaScript value into a c# object (this does not affect private variable /// only public properties / getters and setters). /// </summary> /// <param name="value">The value to convert.</param> /// <param name="type">The type of object you are expecting.</param> /// <returns>The converted object.</returns> public object FromJavaScriptValue(JavaScriptValue value, Type type) { var jsonObject = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("JSON")); var stringify = jsonObject.GetProperty(JavaScriptPropertyId.FromString("stringify")); switch (value.ValueType) { case JavaScriptValueType.Undefined: case JavaScriptValueType.Null: return(null); case JavaScriptValueType.Number: { var number = stringify.CallFunction(JavaScriptValue.GlobalObject, value.ConvertToObject()); return(JsonConvert.DeserializeObject(number.ToString(), type)); } case JavaScriptValueType.String: return(value.ToString()); case JavaScriptValueType.Boolean: return(value.ToBoolean()); case JavaScriptValueType.Object: { var number = stringify.CallFunction(JavaScriptValue.GlobalObject, value.ConvertToObject()); return(JsonConvert.DeserializeObject(number.ToString(), type)); } case JavaScriptValueType.Function: // Somehow create a func to return back to c# that calls JS?? return(null); case JavaScriptValueType.Array: { var number = stringify.CallFunction(JavaScriptValue.GlobalObject, value.ConvertToObject()); return(JsonConvert.DeserializeObject(number.ToString(), type)); } default: Console.WriteLine($"Type {value.ValueType} is not supported!"); return(null); } }
private IEnumerable <Span> GetValueSpans(JavaScriptValue value, int level = 0, bool inArray = false) { switch (value.ValueType) { case JavaScriptValueType.Null: return(new Span[] { new Span("null") }); case JavaScriptValueType.Undefined: return(new Span[] { new Span("undefined") }); case JavaScriptValueType.String: return(new Span[] { $"\"{value.ToString().Replace("\"", "\\\"")}\"".DarkGreen() }); case JavaScriptValueType.Boolean: return(new Span[] { new Span(value.ToBoolean() ? "true" : "false") }); case JavaScriptValueType.Number: try { return(new Span[] { value.ToInt32().ToString().Red() }); } catch (JavaScriptException) { return(new Span[] { value.ToDouble().ToString(CultureInfo.InvariantCulture).Red() }); } case JavaScriptValueType.Function: return(new Span[] { GetFuncRep(value).Magenta() }); case JavaScriptValueType.Error: return(new Span[] { value.ToJsonString().DarkRed() }); case JavaScriptValueType.Object: return(GetObjectSpans(value, level, inArray)); case JavaScriptValueType.Array: return(GetArraySpans(value, level)); default: return(new Span[] { new Span(value.ValueType.ToString()), }); } }
private JToken VisitBoolean(JavaScriptValue value) { return(value.ToBoolean() ? s_true : s_false); }
private static object ConvertJavaScriptValue(JavaScriptValue value) { JavaScriptValueType t = value.ValueType; if (t == JavaScriptValueType.Boolean) { return(value.ToBoolean()); } else if (t == JavaScriptValueType.Number) { return(value.ToDouble()); } else if (t == JavaScriptValueType.String) { return(value.ToString()); } else if (t == JavaScriptValueType.Null) { return(null); } else if (t == JavaScriptValueType.Undefined) { return(null); } else if (t == JavaScriptValueType.Object) { Dictionary <string, object> dictionary = new Dictionary <string, object>(); JavaScriptValue propNames = value.GetOwnPropertyNames(); int i = 0; JavaScriptValue index; while (propNames.HasIndexedProperty(index = JavaScriptValue.FromInt32(i))) { JavaScriptValue propName = propNames.GetIndexedProperty(index); JavaScriptPropertyId propId = JavaScriptPropertyId.FromString(propName.ToString()); JavaScriptValue propValue = value.GetProperty(propId); object value2 = ConvertJavaScriptValue(propValue); dictionary.Add(propName.ToString(), value2); //Debug.WriteLine(propName.ToString() + ": " + propValue.ConvertToString().ToString()); i += 1; } return(dictionary); } else if (t == JavaScriptValueType.Array) { JavaScriptPropertyId lengthPropId = JavaScriptPropertyId.FromString("length"); JavaScriptValue arrayLength = value.GetProperty(lengthPropId); int length = (int)arrayLength.ToDouble(); object[] array = new object[length]; int i; JavaScriptValue index; for (i = 0; i < length; i++) { if (value.HasIndexedProperty(index = JavaScriptValue.FromInt32(i))) { JavaScriptValue prop = value.GetIndexedProperty(index); array[i] = ConvertJavaScriptValue(prop); } } return(array); } return(null); }
private JToken VisitBoolean(JavaScriptValue value) { return(value.ToBoolean() ? TrueValue : FalseValue); }
private JToken VisitBoolean(JavaScriptValue value) { return value.ToBoolean() ? s_true : s_false; }