public static CefV8Value ToCefV8Value(this JavaScriptValue source) { switch (source.ValueType) { case JavaScriptValueType.Null: return(CefV8Value.CreateNull()); case JavaScriptValueType.Bool: return(CefV8Value.CreateBool(source.GetBool())); case JavaScriptValueType.Int: return(CefV8Value.CreateInt(source.GetInt())); case JavaScriptValueType.Double: return(CefV8Value.CreateDouble(source.GetDouble())); case JavaScriptValueType.String: return(CefV8Value.CreateString(source.GetString())); case JavaScriptValueType.DateTime: return(CefV8Value.CreateDate(source.GetDateTime())); case JavaScriptValueType.Property: break; case JavaScriptValueType.Function: break; case JavaScriptValueType.Object: var obj = CefV8Value.CreateObject(); foreach (var key in source.Keys) { var retval = source.GetValue(key)?.ToCefV8Value(); if (retval != null) { obj.SetValue(key, retval); } } return(obj); case JavaScriptValueType.Array: var result = new List <CefV8Value>(); for (int i = 0; i < source.ArrayLength; i++) { var retval = source.GetValue(i)?.ToCefV8Value(); if (retval != null) { result.Add(retval); } } var array = CefV8Value.CreateArray(result.Count); for (int i = 0; i < result.Count; i++) { array.SetValue(i, result[i]); } return(array); } return(null); }
public void Test_GetSimpleValue_Date() { Test(() => { object res = null; var date = new DateTime(1974, 02, 26, 01, 02, 03, DateTimeKind.Utc); bool ok = _CefV8_Converter.GetSimpleValue(CefV8Value.CreateDate(date).Convert(), out res); ok.Should().BeTrue(); res.Should().Be(date); }); }
public static CefV8Value ConvertToV8Value(object o) { if (o == null) { return(CefV8Value.CreateUndefined()); } if (o is bool) { return(CefV8Value.CreateBool((bool)o)); } if (o is DateTime) { return(CefV8Value.CreateDate((DateTime)o)); } if (o is double) { return(CefV8Value.CreateDouble((double)o)); } if (o is int) { return(CefV8Value.CreateInt((int)o)); } if (o is string) { return(CefV8Value.CreateString((string)o)); } if (o is uint) { return(CefV8Value.CreateUInt((uint)o)); } if (o is Array) { var a = (Array)o; var rtn = CefV8Value.CreateArray(a.Length); for (int i = 0; i < a.Length; i++) { rtn.SetValue(i, ConvertToV8Value(a.GetValue(i))); } return(rtn); } if (o is System.Collections.IList) { var a = (System.Collections.IList)o; var rtn = CefV8Value.CreateArray(a.Count); for (int i = 0; i < a.Count; i++) { rtn.SetValue(i, ConvertToV8Value(a[i])); } return(rtn); } throw new NotSupportedException("??"); }
public static CefV8Value ConvertToV8Value(dynamic result) { if (result != null) { if (result is bool) { return(CefV8Value.CreateBool(result)); } else if (result is DateTime) { return(CefV8Value.CreateDate(result)); } else if (result is double) { return(CefV8Value.CreateDouble(result)); } else if (result is int) { return(CefV8Value.CreateInt(result)); } else if (result is string) { return(CefV8Value.CreateString(result)); } else if (result is uint) { return(CefV8Value.CreateUInt(result)); } else if (result is Array) { var source = (Array)result; var destination = CefV8Value.CreateArray(source.Length); for (int x = 0; x < source.Length; x++) { destination.SetValue(x, ConvertToV8Value(source.GetValue(x))); } return(destination); } else if (result is System.Collections.IList) { var source = (System.Collections.IList)result; var destination = CefV8Value.CreateArray(source.Count); for (int x = 0; x < source.Count; x++) { destination.SetValue(x, ConvertToV8Value(source[x])); } return(destination); } //else throw new NotSupportedException("V8 Value"); } return(CefV8Value.CreateNull()); }
private CefV8Value InvokeMethod(MethodInfo method, List <object> args, Type type) { if (method != null) { if (type == typeof(bool)) { return(CefV8Value.CreateBool((bool)method.Invoke(this, args.ToArray()))); } else if (type == typeof(DateTime)) { return(CefV8Value.CreateDate((DateTime)method.Invoke(this, args.ToArray()))); } else if (type == typeof(double)) { return(CefV8Value.CreateDouble((double)method.Invoke(this, args.ToArray()))); } else if (type == typeof(int)) { return(CefV8Value.CreateInt((int)method.Invoke(this, args.ToArray()))); } else if (type == typeof(string)) { return(CefV8Value.CreateString((string)method.Invoke(this, args.ToArray()))); } else if (type == typeof(uint)) { return(CefV8Value.CreateUInt((uint)method.Invoke(this, args.ToArray()))); } else if (type == typeof(string[]) || type == typeof(int[]) || type == typeof(double[]) || type == typeof(DateTime[]) || type == typeof(bool[]) || type == typeof(uint[]) || type == typeof(Enum[])) { return(Extension.ConvertToV8Value(method.Invoke(this, args.ToArray()))); } } return(null); }
private CefV8Value InvokeContructor(ConstructorInfo constructor, List <object> args, Type type) { if (constructor != null) { if (type == typeof(bool)) { return(CefV8Value.CreateBool((bool)constructor.Invoke(args.ToArray()))); } else if (type == typeof(DateTime)) { return(CefV8Value.CreateDate((DateTime)constructor.Invoke(args.ToArray()))); } else if (type == typeof(double)) { return(CefV8Value.CreateDouble((double)constructor.Invoke(args.ToArray()))); } else if (type == typeof(int)) { return(CefV8Value.CreateInt((int)constructor.Invoke(args.ToArray()))); } else if (type == typeof(string)) { return(CefV8Value.CreateString((string)constructor.Invoke(args.ToArray()))); } else if (type == typeof(uint)) { return(CefV8Value.CreateUInt((uint)constructor.Invoke(args.ToArray()))); } else if (type == typeof(string[]) || type == typeof(int[]) || type == typeof(double[]) || type == typeof(DateTime[]) || type == typeof(bool[]) || type == typeof(uint[]) || type == typeof(Enum[])) { return(Extension.ConvertToV8Value(constructor.Invoke(args.ToArray()))); } } return(null); }
static CefV8_Factory() { Register <string>((source) => CefV8Value.CreateString(source)); Register <Int64>((source) => CefV8Value.CreateDouble((double)source)); Register <UInt64>((source) => CefV8Value.CreateDouble((double)source)); Register <float>((source) => CefV8Value.CreateDouble((double)source)); Register <Int32>((source) => CefV8Value.CreateInt(source)); Register <Int16>((source) => CefV8Value.CreateInt((int)source)); Register <UInt32>((source) => CefV8Value.CreateUInt(source)); Register <UInt16>((source) => CefV8Value.CreateUInt((UInt32)source)); //check two way and convertion back Register <char>((source) => CefV8Value.CreateString(new StringBuilder().Append(source).ToString())); Register <double>((source) => CefV8Value.CreateDouble(source)); Register <decimal>((source) => CefV8Value.CreateDouble((double)source)); Register <bool>((source) => CefV8Value.CreateBool(source)); Register <DateTime>((source) => CefV8Value.CreateDate(source)); }
public IJavascriptObject CreateDateTime(DateTime value) { return(new CefV8_JavascriptObject(CefV8Value.CreateDate(value))); }
public object Deserialize(CefValue value, Type targetType, ObjectSerializer objectSerializer) { if (!CanHandle(value, targetType)) { throw new InvalidOperationException(); } var valueType = value.GetValueType(); if (valueType == CefValueType.String) { return(CefV8Value.CreateString(value.GetString())); } if (valueType == CefValueType.Int) { return(CefV8Value.CreateInt(value.GetInt())); } if (valueType == CefValueType.Double) { return(CefV8Value.CreateDouble(value.GetDouble())); } if (value.IsType(CefTypes.Int64)) { return(CefV8Value.CreateDouble(value.GetInt64())); } if (value.IsType(CefTypes.Time)) { return(CefV8Value.CreateDate(value.GetTime())); } if (valueType == CefValueType.Bool) { return(CefV8Value.CreateBool(value.GetBool())); } if (valueType == CefValueType.List) { using (var list = value.GetList()) { if (list.Count > 0) { var array = CefV8Value.CreateArray(list.Count); for (var i = 0; i < list.Count; i++) { using (var cefValue = list.GetValue(i)) { array.SetValue(i, (CefV8Value)objectSerializer.Deserialize(cefValue, typeof(CefV8Value))); } } return(array); } } } if (valueType == CefValueType.Dictionary) { using (var dictionary = value.GetDictionary()) using (var valDict = dictionary.GetDictionary(ObjectSerializer.ValuePropertyName)) { var typeId = dictionary.GetString(ObjectSerializer.TypeIdPropertyName); if (typeId == ObjectSerializer.DictionaryTypeId) { var obj = CefV8Value.CreateObject(); foreach (var key in valDict.GetKeys()) { obj.SetValue(key, (CefV8Value)objectSerializer.Deserialize(valDict.GetValue(key), typeof(CefV8Value))); } return(obj); } else { var deserialized = objectSerializer.Deserialize(value, typeof(object)); if (deserialized is ObjectDescriptor descriptor) { return(new ObjectBinder(descriptor, objectSerializer, functionCallPromiseRegistry).BindToNew()); } } } } return(CefV8Value.CreateNull()); }
private static ICefV8Value CreateDateValue(DateTime dateTime) { return(new CefV8ValueImpl(CefV8Value.CreateDate(dateTime))); }
public CefV8Value AsV8Value() { CefV8Value result = null; if (this.Value is IList) { var v = (IList)this.Value; result = CefV8Value.CreateArray(v.Count); for (var i = 0; i < v.Count; i++) { result.SetValue(i, new JSValue(v[i]).AsV8Value()); } } else if (this.Value is Boolean) { result = CefV8Value.CreateBool((Boolean)this.Value); } else if (this.Value is DateTime) { result = CefV8Value.CreateDate((DateTime)this.Value); } else if (this.Value is Single || this.Value is Double || this.Value is Decimal || this.Value is UInt64 || this.Value is Int64) { result = CefV8Value.CreateDouble((Double)this.Value); } else if (this.Value is CefV8Handler) { result = CefV8Value.CreateFunction(null, (CefV8Handler)this.Value); } else if (this.Value is SByte || this.Value is Int16 || this.Value is Int32) { result = CefV8Value.CreateInt((Int32)this.Value); } else if (this.Value == null) { result = CefV8Value.CreateNull(); } else if (this.Value is IDictionary) { var v = (IDictionary)this.Value; Debug.Assert(v.Keys.Count == v.Values.Count); var vKeys = new String[v.Keys.Count]; var vValues = new CefV8Value[v.Values.Count]; result = CefV8Value.CreateObject(null); for (var i = 0; i < vKeys.Length; i++) { result.SetValue(vKeys[i], new JSValue(vValues[i]).AsV8Value(), CefV8PropertyAttribute.None); } } else if (this.Value is String) { result = CefV8Value.CreateString((String)this.Value); } else if (this.Value is Byte || this.Value is UInt16 || this.Value is UInt32) { result = CefV8Value.CreateUInt((UInt32)this.Value); } if (result == null) { result = CefV8Value.CreateUndefined(); } return(result); }
public static CefV8Value ToCef(JToken jsonToken) { switch (jsonToken.Type) { case JTokenType.Object: { var jObject = (JObject)jsonToken; var plainObject = CefV8Value.CreateObject(null); foreach (var jsonProperty in jObject.Properties()) { plainObject.SetValue(jsonProperty.Name, ToCef(jsonProperty.Value), CefV8PropertyAttribute.None); } return(plainObject); } case JTokenType.Array: { var jArray = (JArray)jsonToken; var arrayValue = CefV8Value.CreateArray(jArray.Count); for (var i = 0; i < jArray.Count; i++) { arrayValue.SetValue(i, ToCef(jArray[i])); } return(arrayValue); } case JTokenType.Integer: { var jsonValue = (JValue)jsonToken; if (jsonValue.Value is long) { return(CefV8Value.CreateDouble((double)jsonToken)); } return(CefV8Value.CreateInt((int)jsonToken)); } case JTokenType.Float: return(CefV8Value.CreateDouble((double)jsonToken)); case JTokenType.Guid: case JTokenType.Uri: case JTokenType.String: return(CefV8Value.CreateString((string)jsonToken)); case JTokenType.Boolean: return(CefV8Value.CreateBool((bool)jsonToken)); case JTokenType.Null: return(CefV8Value.CreateNull()); case JTokenType.Undefined: return(CefV8Value.CreateUndefined()); case JTokenType.Date: return(CefV8Value.CreateDate((DateTime)jsonToken)); case JTokenType.Raw: case JTokenType.Bytes: case JTokenType.TimeSpan: case JTokenType.Property: case JTokenType.Comment: case JTokenType.Constructor: case JTokenType.None: throw new NotSupportedException(); default: throw new ArgumentOutOfRangeException(); } }
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception) { try { if (name == "Log") { var message = arguments[0].GetStringValue(); #if DIAGNOSTICS Cef.Logger.Info(LogTarget.Default, message); #endif returnValue = null; } else if (name == "ReturnVoid") { returnValue = null; } else if (name == "ReturnVoidAndDisposeThis") { returnValue = null; if (obj != null) { obj.Dispose(); obj = null; } } else if (name == "ReturnUndefined") { returnValue = CefV8Value.CreateUndefined(); } else if (name == "ReturnNull") { returnValue = CefV8Value.CreateNull(); } else if (name == "ReturnBool") { returnValue = CefV8Value.CreateBool(true); } else if (name == "ReturnInt") { returnValue = CefV8Value.CreateInt(12345678); } else if (name == "ReturnDouble") { returnValue = CefV8Value.CreateDouble(1234.5678); } else if (name == "ReturnDate") { returnValue = CefV8Value.CreateDate(DateTime.UtcNow); } else if (name == "ReturnString") { returnValue = CefV8Value.CreateString("Some string, passed to CEF!"); } else if (name == "ReturnArray") { var array = CefV8Value.CreateArray(3); array.SetValue(0, CefV8Value.CreateInt(123)); array.SetValue(1, CefV8Value.CreateString("hello!")); array.SetValue(2, CefV8Value.CreateBool(false)); returnValue = array; } else if (name == "ReturnObject") { var obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); returnValue = obj1; } else if (name == "ReturnComplexArray") { var obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); var array = CefV8Value.CreateArray(5); array.SetValue(0, CefV8Value.CreateInt(123)); array.SetValue(1, CefV8Value.CreateString("hello!")); array.SetValue(2, CefV8Value.CreateBool(false)); array.SetValue(3, obj1); array.SetValue(4, CefV8Value.CreateString("hello2!")); obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); var obj2 = CefV8Value.CreateObject(); obj2.SetValue("i'm still", CefV8Value.CreateString("alive")); obj1.SetValue("inner", obj2); array.SetValue(5, obj1); returnValue = array; } else if (name == "ReturnComplexObject") { var obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); var obj2 = CefV8Value.CreateObject(); obj2.SetValue("i'm still", CefV8Value.CreateString("alive")); obj1.SetValue("inner", obj2); obj2.Dispose(); // force to dispose object wrapper and underlying v8 persistent handle. // note, that obj2 will passed in obj before, but it anyway safe to destroy obj2 handle, // 'cause v8 api internally always open handles. returnValue = obj1; } else if (name == "SubtractIntImplicit") { var a = arguments[0].GetIntValue(); var b = arguments[1].GetIntValue(); returnValue = CefV8Value.CreateInt(a - b); } else if (name == "SubtractIntExplicit") { if (!arguments[0].IsInt) { throw new ArgumentException("arg0"); } var a = arguments[0].GetIntValue(); if (!arguments[1].IsInt) { throw new ArgumentException("arg1"); } var b = arguments[1].GetIntValue(); returnValue = CefV8Value.CreateInt(a - b); } else if (name == "Dump") { returnValue = CefV8Value.CreateString(Dump(arguments)); } else if (name == "get_PrivateWorkingSet") { var result = Process.GetCurrentProcess().PrivateMemorySize64 / (1024.0 * 1024.0); returnValue = CefV8Value.CreateDouble(result); } else if (name == "leakTestV8Func") { var handler = new TestV8Handler(); for (var i = 0; i < 100000; i++) { var x = CefV8Value.CreateFunction("LeakTest", handler); x.Dispose(); } returnValue = CefV8Value.CreateBool(true); } else { returnValue = null; exception = null; return(false); } exception = null; return(true); } catch (Exception ex) { returnValue = null; exception = ex.ToString(); return(true); } }