Example #1
0
        /// <summary>
        /// Evaluates JavaScript code represented as a <see cref="String"/> in this V8 context.
        /// </summary>
        /// <param name="code">The JavaScript code.</param>
        /// <param name="scriptUrl">The URL where the script in question can be found, if any.</param>
        /// <param name="line">The base line number to use for error reporting.</param>
        /// <returns>The completion value of evaluating the given code.</returns>
        /// <exception cref="CefNetJSExcepton">
        /// Thrown when an exception is raised in the JavaScript engine.
        /// </exception>
        public CefV8Value Eval(string code, string scriptUrl, int line = 1)
        {
            if (line <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(line));
            }
            if (code is null)
                throw new ArgumentNullException(nameof(code));

            fixed(char *s0 = code)
            fixed(char *s1 = scriptUrl)
            {
                var cstr0 = new cef_string_t {
                    Str = s0, Length = code.Length
                };
                var cstr1 = new cef_string_t {
                    Str = s1, Length = scriptUrl != null ? scriptUrl.Length : 0
                };


                cef_v8value_t *     rv    = null;
                cef_v8value_t **    pRv   = &rv;
                cef_v8exception_t * jsex  = null;
                cef_v8exception_t **pJsex = &jsex;

                if (NativeInstance->Eval(&cstr0, &cstr1, line, pRv, pJsex) != 0)
                {
                    return(CefV8Value.Wrap(CefV8Value.Create, rv));
                }
                GC.KeepAlive(this);

                throw new CefNetJSExcepton(CefV8Exception.Wrap(CefV8Exception.Create, jsex));
            }
        }
Example #2
0
        /// <summary>
        /// Returns the value with the specified identifier on success. Returns NULL if
        /// this function is called incorrectly or an exception is thrown.
        /// </summary>
        public unsafe virtual CefV8Value GetValueByKey(string key)
        {
            fixed(char *s0 = key)
            {
                var cstr0 = new cef_string_t {
                    Str = s0, Length = key != null ? key.Length : 0
                };

                return(SafeCall(CefV8Value.Wrap(CefV8Value.Create, NativeInstance->GetValueByKey(&cstr0))));
            }
        }
Example #3
0
        /// <summary>
        /// Execute the function using the specified V8 context. |object| is the
        /// receiver (&apos;this&apos; object) of the function. If |object| is NULL the specified
        /// context&apos;s global object will be used. |arguments| is the list of arguments
        /// that will be passed to the function. Returns the function return value on
        /// success. Returns NULL if this function is called incorrectly or an
        /// exception is thrown.
        /// </summary>
        public unsafe virtual CefV8Value ExecuteFunctionWithContext(CefV8Context context, CefV8Value @object, CefV8Value[] arguments)
        {
            cef_v8value_t **arr3 = (cef_v8value_t **)Marshal.AllocHGlobal(sizeof(cef_v8value_t *) * arguments.Length);

            for (int i = 0; i < arguments.Length; i++)
            {
                var e3 = arguments[i];
                *(arr3 + i) = e3 != null?e3.GetNativeInstance() : null;
            }
            var rv = CefV8Value.Wrap(CefV8Value.Create, NativeInstance->ExecuteFunctionWithContext((context != null) ? context.GetNativeInstance() : null, (@object != null) ? @object.GetNativeInstance() : null, new UIntPtr((uint)arguments.Length), arr3));

            Marshal.FreeHGlobal((IntPtr)arr3);
            GC.KeepAlive(this);
            return(rv);
        }
Example #4
0
        /// <summary>
        /// Gets the value associated with the specified keys chain.
        /// </summary>
        /// <param name="names">Names of properties.</param>
        /// <returns>
        /// A value associated with the specified keys chain.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="names"/> is null.</exception>
        /// <exception cref="InvalidOperationException">
        /// Any parent property is null or undefined, or if this function is called incorrectly
        /// or an JavaScript-exception is thrown.
        /// </exception>
        public CefV8Value GetValue(ArraySegment <string> names)
        {
            if (names.Count <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(names));
            }

            cef_v8value_t *value = null;
            cef_v8value_t *self  = GetNativeInstance();

            foreach (string name in names)
            {
                try
                {
                    if (self->IsNull() != 0)
                    {
                        throw new InvalidOperationException($"Cannot read property '{name}' of null.");
                    }
                    if (self->IsUndefined() != 0)
                    {
                        throw new InvalidOperationException($"Cannot read property '{name}' of undefined.");
                    }
                    if (string.IsNullOrEmpty(name))
                        throw new ArgumentOutOfRangeException(nameof(names));

                    fixed(char *s = name)
                    {
                        var aName = new cef_string_t {
                            Str = s, Length = name.Length
                        };

                        value = self->GetValueByKey(&aName);
                    }
                }
                finally
                {
                    self->@base.Release();
                }

                if (value == null)
                {
                    throw new InvalidOperationException();
                }
                self = value;
            }
            GC.KeepAlive(this);
            return(CefV8Value.Wrap(CefV8Value.Create, value));
        }
Example #5
0
        /// <summary>
        /// Execute a string of JavaScript code in this V8 context. The |script_url|
        /// parameter is the URL where the script in question can be found, if any. The
        /// |start_line| parameter is the base line number to use for error reporting.
        /// On success |retval| will be set to the return value, if any, and the
        /// function will return true (1). On failure |exception| will be set to the
        /// exception, if any, and the function will return false (0).
        /// </summary>
        public unsafe virtual bool Eval(string code, string scriptUrl, int startLine, ref CefV8Value retval, ref CefV8Exception exception)
        {
            fixed(char *s0 = code)
            fixed(char *s1 = scriptUrl)
            {
                var cstr0 = new cef_string_t {
                    Str = s0, Length = code != null ? code.Length : 0
                };
                var cstr1 = new cef_string_t {
                    Str = s1, Length = scriptUrl != null ? scriptUrl.Length : 0
                };
                cef_v8value_t *     p3  = (retval != null) ? retval.GetNativeInstance() : null;
                cef_v8value_t **    pp3 = &p3;
                cef_v8exception_t * p4  = (exception != null) ? exception.GetNativeInstance() : null;
                cef_v8exception_t **pp4 = &p4;
                var rv = NativeInstance->Eval(&cstr0, &cstr1, startLine, pp3, pp4) != 0;

                retval    = CefV8Value.Wrap(CefV8Value.Create, p3);
                exception = CefV8Exception.Wrap(CefV8Exception.Create, p4);
                GC.KeepAlive(this);
                return(rv);
            }
        }
Example #6
0
 /// <summary>
 /// Execute the function without arguments using the current V8 context.<para/>
 /// This function should only be called from within the scope of a <see cref="CefV8Handler"/>
 /// or <see cref="CefV8Accessor"/> callback, or in combination with calling
 /// <see cref="CefV8Context.Enter"/> and <see cref="CefV8Context.Enter"/>
 /// on a stored <see cref="CefV8Context"/> reference.
 /// </summary>
 /// <param name="thisArg">
 /// The receiver (&apos;this&apos; object) of the function. If <paramref name="thisArg"/>
 /// is null the current context&apos;s global objectwill be used.
 /// </param>
 /// <returns>
 /// Returns the function return value on success, or null if this function
 /// is called incorrectly or an exception is thrown.
 /// </returns>
 public unsafe virtual CefV8Value ExecuteFunction(CefV8Value thisArg)
 {
     return(SafeCall(CefV8Value.Wrap(CefV8Value.Create, NativeInstance->ExecuteFunction(thisArg != null ? thisArg.GetNativeInstance() : null, new UIntPtr(), null))));
 }
Example #7
0
 /// <summary>
 /// Returns the global object for this context. The context must be entered
 /// before calling this function.
 /// </summary>
 public unsafe virtual CefV8Value GetGlobal()
 {
     return(SafeCall(CefV8Value.Wrap(CefV8Value.Create, NativeInstance->GetGlobal())));
 }
Example #8
0
 /// <summary>
 /// Returns the value with the specified identifier on success. Returns NULL if
 /// this function is called incorrectly or an exception is thrown.
 /// </summary>
 public unsafe virtual CefV8Value GetValueByIndex(int index)
 {
     return(SafeCall(CefV8Value.Wrap(CefV8Value.Create, NativeInstance->GetValueByIndex(index))));
 }