Esempio n. 1
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && !IsDisposed)
            {
                BaristaExecutionScope scope = null;
                if (!HasCurrentScope)
                {
                    scope = Scope();
                }

                try
                {
                    m_valueFactory.Dispose();
                    m_moduleRecordFactory.Dispose();
                }
                finally
                {
                    if (scope != null)
                    {
                        scope.Dispose();
                    }
                }
            }

            if (!IsDisposed)
            {
                //Unset the before collect callback.
                Engine.JsSetObjectBeforeCollectCallback(Handle, IntPtr.Zero, null);
                m_beforeCollectCallbackDelegateHandle.Free();
            }

            base.Dispose(disposing);
        }
Esempio n. 2
0
        /// <summary>
        /// Converts the value to a number and returns the integer representation.
        /// </summary>
        /// <returns></returns>
        public virtual int ToInt32()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(JsValue));
            }

            //Create a scope if we're not currently in one.
            BaristaExecutionScope scope = null;

            if (!Context.HasCurrentScope)
            {
                scope = Context.Scope();
            }

            try
            {
                using (var numberValueHandle = Engine.JsConvertValueToNumber(Handle))
                {
                    return(Engine.JsNumberToInt(numberValueHandle));
                }
            }
            finally
            {
                if (scope != null)
                {
                    scope.Dispose();
                }
            }
        }
Esempio n. 3
0
        private void ReleaseScope()
        {
            Engine.JsSetCurrentContext(JavaScriptContextSafeHandle.Invalid);
            m_currentExecutionScope = null;

            //Release the lock
            Interlocked.Exchange(ref m_withinScope, 0);
        }
Esempio n. 4
0
 /// <summary>
 /// Returns a new JavaScript Execution Scope to perform work in.
 /// </summary>
 /// <returns></returns>
 public BaristaExecutionScope Scope()
 {
     if (0 == Interlocked.Exchange(ref m_withinScope, 1))
     {
         Engine.JsSetCurrentContext(Handle);
         m_currentExecutionScope = new BaristaExecutionScope(this, m_promiseTaskQueue, ReleaseScope);
         return(m_currentExecutionScope);
     }
     else
     {
         throw new InvalidOperationException("This context already has an active execution scope.");
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Converts the value to a string (using standard JavaScript semantics) and returns the result
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(JsValue));
            }

            //Create a scope if we're not currently in one.
            BaristaExecutionScope scope = null;

            if (!Context.HasCurrentScope)
            {
                scope = Context.Scope();
            }

            try
            {
                using (var stringValueHandle = Engine.JsConvertValueToString(Handle))
                {
                    //Get the size
                    var size = Engine.JsCopyString(stringValueHandle, null, 0);
                    if ((int)size > int.MaxValue)
                    {
                        throw new OutOfMemoryException("Exceeded maximum string length.");
                    }

                    byte[] result  = new byte[(int)size];
                    var    written = Engine.JsCopyString(stringValueHandle, result, (ulong)result.Length);
                    return(Encoding.UTF8.GetString(result, 0, result.Length));
                }
            }
            finally
            {
                if (scope != null)
                {
                    scope.Dispose();
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Evaluates the specified script as a module, the default export will be the returned value.
        /// </summary>
        /// <param name="script">Script to evaluate.</param>
        /// <returns></returns>
        public JsValue EvaluateModule(string script, IBaristaModuleLoader moduleLoader = null)
        {
            //Create a scope if we're not currently in one.
            BaristaExecutionScope scope = null;

            if (!HasCurrentScope)
            {
                scope = Scope();
            }

            try
            {
                var globalName = EvaluateModuleInternal(script, moduleLoader);
                return(GlobalObject.GetProperty(globalName));
            }
            finally
            {
                if (scope != null)
                {
                    scope.Dispose();
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Evaluates the specified script as a module, the default export will be the returned value.
        /// </summary>
        /// <param name="script">Script to evaluate.</param>
        /// <returns></returns>
        public T EvaluateModule <T>(string script, IBaristaModuleLoader moduleLoader = null)
            where T : JsValue
        {
            //Create a scope if we're not currently in one.
            BaristaExecutionScope scope = null;

            if (!HasCurrentScope)
            {
                scope = Scope();
            }

            try
            {
                var moduleNamespace = EvaluateModuleInternal(script, moduleLoader);
                return(moduleNamespace.GetProperty <T>("default"));
            }
            finally
            {
                if (scope != null)
                {
                    scope.Dispose();
                }
            }
        }