Ejemplo n.º 1
0
 /// <summary> Convenient method to convert java value to its closest representation
 /// in JavaScript.
 /// <p>
 /// If value is an instance of String, Number, Boolean, Function or
 /// Scriptable, it is returned as it and will be treated as the corresponding
 /// JavaScript type of string, number, boolean, function and object.
 /// <p>
 /// Note that for Number instances during any arithmetic operation in
 /// JavaScript the engine will always use the result of
 /// <tt>Number.doubleValue()</tt> resulting in a precision loss if
 /// the number can not fit into double.
 /// <p>
 /// If value is an instance of Character, it will be converted to string of
 /// length 1 and its JavaScript type will be string.
 /// <p>
 /// The rest of values will be wrapped as LiveConnect objects
 /// by calling {@link WrapFactory#wrap(Context cx, Scriptable scope,
 /// Object obj, Class staticType)} as in:
 /// <pre>
 /// Context cx = Context.getCurrentContext();
 /// return cx.getWrapFactory().wrap(cx, scope, value, null);
 /// </pre>
 /// 
 /// </summary>
 /// <param name="value">any Java object
 /// </param>
 /// <param name="scope">top scope object
 /// </param>
 /// <returns> value suitable to pass to any API that takes JavaScript values.
 /// </returns>
 public static object CliToJS(Context cx, object value, IScriptable scope)
 {
     if (value is string || CliHelper.IsNumber (value) || value is bool || value is IScriptable) {
         return value;
     }
     else if (value is char) {
         return Convert.ToString (((char)value));
     }
     else {
         Type type = (value as Type);
         if (type !=  null) {
             return cx.Wrap (scope, value, (Type)value);
         } else {
             return cx.Wrap (scope, value, null);
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary> Convert the value to an object.
        /// 
        /// See ECMA 9.9.
        /// </summary>
        public static IScriptable ToObject (Context cx, IScriptable scope, object val)
        {
            if (val is IScriptable) {
                return (IScriptable)val;
            }
            if (val == null) {
                throw ScriptRuntime.TypeErrorById ("msg.null.to.object");
            }
            if (val == Undefined.Value) {
                throw ScriptRuntime.TypeErrorById ("msg.undef.to.object");
            }
            string className = val is string ? "String" : (CliHelper.IsNumber (val) ? "Number" : (val is bool ? "Boolean" : null));
            if (className != null) {
                object [] args = new object [] { val };
                scope = ScriptableObject.GetTopLevelScope (scope);
                return ScriptRuntime.NewObject (cx == null ? Context.CurrentContext : cx, scope, className, args);
            }

            // Extension: Wrap as a LiveConnect object.
            object wrapped = cx.Wrap (scope, val, null);
            if (wrapped is IScriptable)
                return (IScriptable)wrapped;
            throw ScriptRuntime.errorWithClassName ("msg.invalid.type", val);
        }
Ejemplo n.º 3
0
        public static IScriptable NewCatchScope(Exception t, IScriptable lastCatchScope, string exceptionName, Context cx, IScriptable scope)
        {
            object obj;
            bool cacheObj;

            if (t is EcmaScriptThrow) {
                cacheObj = false;
                obj = ((EcmaScriptThrow)t).Value;
            }
            else {
                cacheObj = true;

                // Create wrapper object unless it was associated with
                // the previous scope object

                if (lastCatchScope != null) {
                    BuiltinObject last = (BuiltinObject)lastCatchScope;
                    obj = last.GetAssociatedValue (t);
                    if (obj == null)
                        Context.CodeBug ();

                    goto getObj_brk;
                }

                EcmaScriptException re;
                string errorName;
                string errorMsg;

                Exception javaException = null;

                if (t is EcmaScriptError) {
                    EcmaScriptError ee = (EcmaScriptError)t;
                    re = ee;
                    errorName = ee.Name;
                    errorMsg = ee.ErrorMessage;
                }
                else if (t is EcmaScriptRuntimeException) {
                    re = (EcmaScriptRuntimeException)t;
                    if (t.InnerException != null) {
                        javaException = t.InnerException;
                        errorName = "JavaException";
                        errorMsg = javaException.GetType ().FullName + ": " + javaException.Message;
                    }
                    else {
                        errorName = "InternalError";
                        errorMsg = re.Message;
                    }
                }
                else {
                    // Script can catch only instances of JavaScriptException,
                    // EcmaError and EvaluatorException
                    throw Context.CodeBug ();
                }

                string sourceUri = re.SourceName;
                if (sourceUri == null) {
                    sourceUri = "";
                }
                int line = re.LineNumber;
                object [] args;
                if (line > 0) {
                    args = new object [] { errorMsg, sourceUri, (int)line };
                }
                else {
                    args = new object [] { errorMsg, sourceUri };
                }

                IScriptable errorObject = cx.NewObject (scope, errorName, args);
                ScriptableObject.PutProperty (errorObject, "name", errorName);

                if (javaException != null) {
                    object wrap = cx.Wrap (scope, javaException, null);
                    ScriptableObject.DefineProperty (errorObject, "javaException", wrap, ScriptableObject.PERMANENT | ScriptableObject.READONLY);
                }
                if (re != null) {
                    object wrap = cx.Wrap (scope, re, null);
                    ScriptableObject.DefineProperty (errorObject, "rhinoException", wrap, ScriptableObject.PERMANENT | ScriptableObject.READONLY);
                }

                obj = errorObject;
            }

            getObj_brk:
            ;

            BuiltinObject catchScopeObject = new BuiltinObject ();
            // See ECMA 12.4
            catchScopeObject.DefineProperty (exceptionName, obj, ScriptableObject.PERMANENT);
            if (cacheObj) {
                catchScopeObject.AssociateValue (t, obj);
            }
            return catchScopeObject;
        }
Ejemplo n.º 4
0
 public override object Call(Context cx, IScriptable scope, IScriptable thisObj, object [] args)
 {
     try {
         return cx.Wrap (scope, Activator.CreateInstance (
             m_Type, args), m_Type);
     }
     catch (Exception ex) {
         throw Context.ThrowAsScriptRuntimeEx (ex);
     }
 }