private static XrayHandle GetGlobalInternal(CefFrame frame)
        {
            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => GetGlobalInternal(frame)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return((XrayHandle)callTask.GetResult());
                }
            }

            CefV8Context context = frame.V8Context;

            if (!context.Enter())
            {
                throw new InvalidOperationException();
            }
            try
            {
                CefV8Value globalObj = context.GetGlobal();
                return((XrayHandle)CastCefV8ValueToDotnetType(context, globalObj, out bool isxray));
            }
            finally
            {
                context.Exit();
            }
        }
        private object GetPropertyInternal(XrayHandle self, string name)
        {
            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => GetPropertyInternal(self, name)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return(callTask.GetResult());
                }
            }


            XrayObject target = self.GetTarget(this.Frame);

            if (!target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            object retval;

            try
            {
                CefV8Value value = target.Value.GetValueByKey(name);
                retval = CastCefV8ValueToDotnetType(target.Context, value, out bool isxray);
                if (!isxray)
                {
                    value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(retval);
        }
        private bool SetPropertyInternal(XrayHandle self, string name, object value)
        {
            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => SetPropertyInternal(self, name, value)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return((bool)callTask.GetResult());
                }
            }

            XrayObject target = self.GetTarget(this.Frame);

            if (target is null || !target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            bool result;

            try
            {
                CefV8Value v8value = CastDotnetTypeToCefV8Value(target.Context, value, out bool isNotXray);
                result = target.Value.SetValueByKey(name, v8value, CefV8PropertyAttribute.None);
                if (isNotXray)
                {
                    v8value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(result);
        }
        private object InvokeInternal(XrayHandle self, object[] args)
        {
            if (args is null || args.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(args));
            }

            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => InvokeInternal(self, args)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return((bool)callTask.GetResult());
                }
            }

            XrayObject target = self.GetTarget(this.Frame);

            if (target is null || !target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            object retval;

            try
            {
                CefV8Value func    = target.Value;
                CefV8Value thisArg = CastDotnetTypeToCefV8Value(target.Context, args[0], out bool isNewThisArg);
                CefV8Value value;

                int size     = args.Length - 1;
                var xraylist = new List <int>(size);
                var fnArgs   = new CefV8Value[size];
                try
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        int index = (i + 1);
                        fnArgs[i] = CastDotnetTypeToCefV8Value(target.Context, args[index], out bool isNew);
                        if (!isNew)
                        {
                            xraylist.Add(index);
                        }
                    }
                    value = func.ExecuteFunction(thisArg, fnArgs);
                }
                finally
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        if (!xraylist.Contains(i))
                        {
                            fnArgs[i].Dispose();
                        }
                    }
                }
                retval = CastCefV8ValueToDotnetType(target.Context, value, out bool isxray);
                if (!isxray)
                {
                    value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(retval);
        }
        public object InvokeMemberInternal(XrayHandle self, string name, object[] args)
        {
            if (args is null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => InvokeMemberInternal(self, name, args)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return((bool)callTask.GetResult());
                }
            }

            XrayObject target = self.GetTarget(this.Frame);

            if (target is null || !target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            object retval;

            try
            {
                CefV8Value thisArg = target.Value;
                CefV8Value func    = thisArg.GetValueByKey(name);
                if (!func.IsFunction)
                {
                    func.Dispose();
                    throw new MissingMethodException(string.Format("'{0} is not a function.'", name));
                }

                CefV8Value value;
                var        xraylist = new List <int>(args.Length);
                var        fnArgs   = new CefV8Value[args.Length];
                try
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        fnArgs[i] = CastDotnetTypeToCefV8Value(target.Context, args[i], out bool isNew);
                        if (!isNew)
                        {
                            xraylist.Add(i);
                        }
                    }
                    value = func.ExecuteFunction(thisArg, fnArgs);
                }
                finally
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        if (!xraylist.Contains(i))
                        {
                            fnArgs[i].Dispose();
                        }
                    }
                }
                retval = CastCefV8ValueToDotnetType(target.Context, value, out bool isxray);
                if (!isxray)
                {
                    value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(retval);
        }