private CfrV8Value CreateV8Value(object value) { if (value is bool) { return(CfrV8Value.CreateBool((bool)value)); } else if (value is DateTime) { return(CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)value))); } else if (value is double) { return(CfrV8Value.CreateDouble((double)value)); } else if (value is int) { return(CfrV8Value.CreateInt((int)value)); } else if (value is uint) { return(CfrV8Value.CreateUint((uint)value)); } else if (value is string) { return(CfrV8Value.CreateString((string)value)); } else if (value == null) { return(CfrV8Value.CreateUndefined()); } else { throw new ArgumentException($"Type \"{ value.GetType().Name }\" is not supported."); } }
private static CfrV8Value ClrToV8Value(object clrValue) { var type = clrValue.GetType(); var typeCode = Type.GetTypeCode(type); CfrV8Value value = null; switch (typeCode) { case TypeCode.Boolean: value = CfrV8Value.CreateBool((bool)clrValue); break; case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: value = CfrV8Value.CreateInt((int)clrValue); break; case TypeCode.SByte: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: value = CfrV8Value.CreateUint((uint)clrValue); break; case TypeCode.Single: case TypeCode.Double: value = CfrV8Value.CreateDouble((double)clrValue); break; case TypeCode.Decimal: value = CfrV8Value.CreateDouble((double)Convert.ChangeType(clrValue, TypeCode.Double)); break; case TypeCode.DateTime: value = CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)clrValue)); break; case TypeCode.Char: case TypeCode.String: value = CfrV8Value.CreateString((string)clrValue); break; } return(value); }
static ChromiumFxFactory() { Register <string>(CfrV8Value.CreateString); Register <Int64>((source) => CfrV8Value.CreateDouble((double)source)); Register <UInt64>((source) => CfrV8Value.CreateDouble((double)source)); Register <float>((source) => CfrV8Value.CreateDouble((double)source)); Register <Int32>(CfrV8Value.CreateInt); Register <Int16>((source) => CfrV8Value.CreateInt((int)source)); Register <UInt32>(CfrV8Value.CreateUint); Register <UInt16>((source) => CfrV8Value.CreateUint((UInt32)source)); Register <char>((source) => CfrV8Value.CreateString(new StringBuilder().Append(source).ToString())); Register <double>(CfrV8Value.CreateDouble); Register <decimal>((source) => CfrV8Value.CreateDouble((double)source)); Register <bool>(CfrV8Value.CreateBool); Register <DateTime>((source) => CfrV8Value.CreateDate(CfrTime.FromUniversalTime(source.ToUniversalTime()))); }
public static CfrV8Value V8Serialize(this object o, HashSet <object> refin = null) { if (Accessor == null) { Accessor = new CfrV8Accessor(); } var propattr = CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly; switch (o) { case null: return(CfrV8Value.CreateNull()); case bool onb: return(CfrV8Value.CreateBool(onb)); case string os: return(CfrV8Value.CreateString(os)); case Enum oenum: return(CfrV8Value.CreateString(oenum.ToString())); case int oni: return(CfrV8Value.CreateInt(oni)); case sbyte oni: return(CfrV8Value.CreateInt(oni)); case short oni: return(CfrV8Value.CreateInt(oni)); case long oni: return(CfrV8Value.CreateInt((int)oni)); case uint onui: return(CfrV8Value.CreateUint(onui)); case byte onui: return(CfrV8Value.CreateUint(onui)); case ushort onui: return(CfrV8Value.CreateUint(onui)); case double ond: return(CfrV8Value.CreateDouble(ond)); case float onf: return(CfrV8Value.CreateDouble(onf)); case ObjectBuilder oob: return(oob.Serialize(refin)); case RGBAColor ocv: var vcolres = CfrV8Value.CreateObject(Accessor); vcolres.SetValue("r", CfrV8Value.CreateInt(ocv.Color.R), propattr); vcolres.SetValue("g", CfrV8Value.CreateInt(ocv.Color.G), propattr); vcolres.SetValue("b", CfrV8Value.CreateInt(ocv.Color.B), propattr); vcolres.SetValue("a", CfrV8Value.CreateDouble(ocv.A), propattr); vcolres.SetValue("name", CfrV8Value.CreateString("#" + ocv.Color.Name.Remove(0, 2)), propattr); return(vcolres); case Color4 ocdx: var dxcolres = CfrV8Value.CreateObject(Accessor); dxcolres.SetValue("r", CfrV8Value.CreateDouble(ocdx.Red * 255), propattr); dxcolres.SetValue("g", CfrV8Value.CreateDouble(ocdx.Green * 255), propattr); dxcolres.SetValue("b", CfrV8Value.CreateDouble(ocdx.Blue * 255), propattr); dxcolres.SetValue("a", CfrV8Value.CreateDouble(ocdx.Alpha), propattr); dxcolres.SetValue("name", CfrV8Value.CreateString(ocdx.ToString()), propattr); return(dxcolres); case TimeSpan ots: return(CfrV8Value.CreateDate(new CfrTime() { Year = 0, Month = 0, DayOfMonth = ots.Days, DayOfWeek = ots.Days, Hour = ots.Hours, Minute = ots.Minutes, Second = ots.Seconds, Millisecond = ots.Milliseconds })); case DateTime odt: return(CfrV8Value.CreateDate(new CfrTime() { Year = odt.Year, Month = odt.Month, DayOfMonth = odt.Day, DayOfWeek = (int)odt.DayOfWeek, Hour = odt.Hour, Minute = odt.Minute, Second = odt.Second, Millisecond = odt.Millisecond })); case IEnumerable oenum: lock (oenum) { var reslist = (from object obj in oenum select obj.V8Serialize()).ToList(); var res = CfrV8Value.CreateArray(reslist.Count); for (int i = 0; i < reslist.Count; i++) { res.SetValue(i, reslist[i]); } return(res); } default: var referenced = refin; if (referenced != null && referenced.Contains(o)) { return(CfrV8Value.CreateNull()); } else if (referenced == null) { referenced = new HashSet <object> { o }; } else { referenced.Add(o); } var oT = o.GetType(); if (oT.IsClass || (oT.IsValueType && !oT.IsPrimitive)) { var reso = CfrV8Value.CreateObject(Accessor); foreach (var prop in oT.GetProperties().Where(p => p.CanRead && p.GetMethod.IsPublic && !p.GetMethod.IsStatic && !p.PropertyType.IsPointer) ) { var cobj = prop.GetValue(o).V8Serialize(referenced); reso.SetValue(prop.Name, cobj, propattr); } foreach (var field in oT.GetFields().Where(f => f.IsPublic && !f.IsStatic && !f.FieldType.IsPointer) ) { var cobj = field.GetValue(o).V8Serialize(referenced); reso.SetValue(field.Name, cobj, propattr); } return(reso); } else { return(CfrV8Value.CreateNull()); } } }
public CfrV8Value ConvertValue(object data) { if (data == null) { return(CfrV8Value.CreateNull()); } var typObject = data.GetType(); var retValue = CfrV8Value.CreateUndefined(); switch (typObject.Name) { case "Boolean": retValue = Convert.ToBoolean(data); break; case "ExpandoObject": IDictionary <string, object> keyValues = data as IDictionary <string, object>; var resultx = CfrV8Value.CreateObject(new CfrV8Accessor(), new CfrV8Interceptor()); foreach (var keypair in keyValues) { resultx.SetValue(keypair.Key, ConvertValue(keypair.Value), CfxV8PropertyAttribute.ReadOnly); } retValue = resultx; break; case "DBNull": retValue = CfrV8Value.CreateNull(); break; case "DateTime": var newValt = Convert.ToDateTime(data); var univerTime = newValt.ToUniversalTime(); retValue = CfrV8Value.CreateDate(CfrTime.FromUniversalTime(univerTime)); break; case "Decimal": case "Double": case "Single": retValue = Convert.ToDouble(data); break; case "Byte": case "UInt16": case "UInt32": case "UInt64": retValue = Convert.ToUInt64(data); break; case "SByte": case "Int16": case "Int32": case "Int64": retValue = Convert.ToInt64(data); break; case "String": retValue = (string)data; break; default: if (typObject.IsEnum) { retValue = Enum.GetName(typObject, data); } else if (typObject.IsArray) { var pos = 0; if (typObject.Name == "Byte[]") { var arr_byte = (byte[])data; var result = CfrV8Value.CreateArray(arr_byte.Length); foreach (var item in arr_byte) { result.SetValue(pos++, ConvertValue(item)); } retValue = result; } else { var arr = (object[])data; var result = CfrV8Value.CreateArray(arr.Length); foreach (var item in arr) { result.SetValue(pos++, ConvertValue(item)); } retValue = result; } } else if (typObject.Name.StartsWith("Dictionary")) { var dictionary = (IDictionary <string, object>)data; var result = CfrV8Value.CreateObject(new CfrV8Accessor(), new CfrV8Interceptor()); foreach (var keypair in dictionary) { result.SetValue(keypair.Key, ConvertValue(keypair.Value), CfxV8PropertyAttribute.ReadOnly); } retValue = result; } else if (typObject.IsClass) { var propertyInfoes = typObject.GetProperties(); var result = CfrV8Value.CreateObject(new CfrV8Accessor(), new CfrV8Interceptor()); foreach (var item in propertyInfoes) { result.SetValue(item.Name, ConvertValue(item.GetValue(data)), CfxV8PropertyAttribute.ReadOnly); } retValue = result; } break; } return(retValue); }
public IJavascriptObject CreateDateTime(DateTime value) { return(CfrV8Value.CreateDate(CfrTime.FromUniversalTime(value.ToUniversalTime())).ConvertBasic()); }
public static CfrV8Value GetCfrObject(this JSObject _, object item) { var nameDict = new Dictionary <string, string>(); var accessor = new CfrV8Accessor(); var o = CfrV8Value.CreateObject(accessor); var t = item.GetType(); foreach (var p in t.GetProperties()) { var name = p.Name.Substring(0, 1).ToLower() + p.Name.Substring(1); nameDict[name] = p.Name; o.SetValue(name, CfxV8AccessControl.Default, CfxV8PropertyAttribute.DontDelete); } accessor.Get += (s, e) => { var name = nameDict[e.Name]; var p = t.GetProperty(name); var value = p.GetValue(item, null); var valueType = value?.GetType(); if (value == null) { e.Retval = CfrV8Value.CreateNull(); } else if (valueType == typeof(string) || valueType == typeof(Guid)) { e.Retval = CfrV8Value.CreateString((string)value); } else if (valueType == typeof(int) || valueType == typeof(short) || valueType == typeof(long)) { e.Retval = CfrV8Value.CreateInt((int)value); } else if (valueType == typeof(decimal)) { e.Retval = CfrV8Value.CreateDouble(Convert.ToDouble(value)); } else if (valueType == typeof(float) || valueType == typeof(double)) { e.Retval = CfrV8Value.CreateDouble(Convert.ToDouble(value)); } else if (valueType == typeof(bool)) { e.Retval = CfrV8Value.CreateBool(Convert.ToBoolean(value)); } else if (valueType == typeof(DateTime)) { e.Retval = CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)value)); } else { e.Retval = CfrV8Value.CreateNull(); } e.SetReturnValue(true); }; return(o); }