/// <summary> /// Run in context. /// </summary> /// <typeparam name="TReturn">The type of the return.</typeparam> /// <param name="context">The context.</param> /// <param name="action">The action.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// context /// or /// action /// </exception> public static TReturn RunInContext <TReturn>(CefV8Context context, Func <TReturn> action) { if (context == null) { throw new ArgumentNullException("context"); } if (action == null) { throw new ArgumentNullException("action"); } var contextEntered = false; if (!CefV8Context.InContext || !CefV8Context.GetEnteredContext().IsSame(context)) { context.Enter(); contextEntered = true; } try { return(action()); } finally { if (contextEntered) { context.Exit(); contextEntered = false; } } }
public static JavaScriptValue ToJSValue(this CefV8Value source) { if (source == null || !source.IsValid) { throw new ArgumentNullException(); } JavaScriptValue target; if (source.IsFunction) { var context = CefV8Context.GetEnteredContext(); var info = new JavaScriptRenderSideFunction(context, source); JavaScriptObjectRepository.RenderSideFunctions.Add(info); target = JavaScriptValue.CreateFunction(info); } else if (source.IsArray) { target = JavaScriptValue.CreateArray(); for (int i = 0; i < source.GetArrayLength(); i++) { var item = source.GetValue(i); if (item != null && item.IsValid) { target.AddArrayValue(item.ToJSValue()); } } } else if (source.IsObject) { target = JavaScriptValue.CreateObject(); foreach (var key in source.GetKeys()) { var item = source.GetValue(key); if (item != null && item.IsValid) { target.SetValue(key, item.ToJSValue()); } } } else if (source.IsBool) { target = JavaScriptValue.CreateBool(source.GetBoolValue()); } else if (source.IsDate) { target = JavaScriptValue.CreateDateTime(source.GetDateValue()); } else if (source.IsDouble) { target = JavaScriptValue.CreateNumber(source.GetDoubleValue()); } else if (source.IsInt) { target = JavaScriptValue.CreateNumber(source.GetIntValue()); } else if (source.IsUInt) { target = JavaScriptValue.CreateNumber(source.GetUIntValue()); } else if (source.IsString) { target = JavaScriptValue.CreateString(source.GetStringValue()); } else { target = JavaScriptValue.CreateNull(); } return(target); }