Ejemplo n.º 1
0
        /// <summary>
        /// Executes the program stored in the <code>CompiledScript</code> object using
        /// the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the
        /// associated <code>ScriptEngine</code> during script execution.  If bindings is null,
        /// then the effect of calling this method is same as that of eval(getEngine().getContext()).
        /// <para>.
        /// The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>
        /// associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.
        ///
        /// </para>
        /// </summary>
        /// <param name="bindings"> The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
        /// </param>
        /// <returns> The return value from the script execution
        /// </returns>
        /// <exception cref="ScriptException"> if an error occurs. </exception>
        public virtual object Eval(IBindings bindings)
        {
            try
            {
                IScriptContext ctxt = GetEngine().GetContext();

                if (bindings != null)
                {
                    SimpleScriptContext tempctxt = new SimpleScriptContext();
                    tempctxt.SetBindings(bindings, ScriptContext_Fields.ENGINE_SCOPE);
                    tempctxt.SetBindings(ctxt.GetBindings(ScriptContext_Fields.GLOBAL_SCOPE), ScriptContext_Fields.GLOBAL_SCOPE);
                    tempctxt.SetWriter(ctxt.GetWriter());
                    tempctxt.SetReader(ctxt.GetReader());
                    tempctxt.SetErrorWriter(ctxt.GetErrorWriter());
                    ctxt = tempctxt;
                }

                return(Eval(ctxt));
            }
            catch (ScriptException ex)
            {
                throw ex;
            }
            catch (System.Exception ex)
            {
                throw new ScriptException(ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a <code>SimpleScriptContext</code>.  The <code>SimpleScriptContext</code>:
        /// <br><br>
        /// <ul>
        /// <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
        /// </li>
        /// <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
        /// method as its <code>GLOBAL_SCOPE</code>
        /// </li>
        /// <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
        /// <code>ScriptEngine</code>
        /// </li>
        /// </ul>
        /// <br><br>
        /// A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
        /// using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
        /// versions.
        /// </summary>
        /// <param name="nn"> Bindings to use for the <code>ENGINE_SCOPE</code> </param>
        /// <returns> The <code>SimpleScriptContext</code> </returns>
        protected internal virtual IScriptContext GetScriptContext(IBindings nn)
        {
            SimpleScriptContext ctxt = new SimpleScriptContext();
            IBindings           gs   = GetBindings(ScriptContext_Fields.GLOBAL_SCOPE);

            if (gs != null)
            {
                ctxt.SetBindings(gs, ScriptContext_Fields.GLOBAL_SCOPE);
            }

            if (nn != null)
            {
                ctxt.SetBindings(nn, ScriptContext_Fields.ENGINE_SCOPE);
            }
            else
            {
                throw new System.NullReferenceException("Engine scope Bindings may not be null.");
            }

            ctxt.SetReader(context.GetReader());
            ctxt.SetWriter(context.GetWriter());
            ctxt.SetErrorWriter(context.GetErrorWriter());

            return(ctxt);
        }