Ejemplo n.º 1
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.º 2
0
        public static IScriptable newObjectLiteral(object [] propertyIds, object [] propertyValues, Context cx, IScriptable scope)
        {
            IScriptable obj = cx.NewObject (scope);
            for (int i = 0, end = propertyIds.Length; i != end; ++i) {
                object id = propertyIds [i];
                object value = propertyValues [i];

                if (id is Node.GetterPropertyLiteral) {
                    BuiltinObject nativeObj = (BuiltinObject)obj;
                    InterpretedFunction fun = (InterpretedFunction)value;
                    nativeObj.DefineGetter ((string)((Node.GetterPropertyLiteral)id).Property, fun);
                }
                else if (id is Node.SetterPropertyLiteral) {
                    BuiltinObject nativeObj = (BuiltinObject)obj;
                    InterpretedFunction fun = (InterpretedFunction)value;
                    nativeObj.DefineSetter ((string)((Node.SetterPropertyLiteral)id).Property, fun);
                }
                else if (id is string) {
                    ScriptableObject.PutProperty (obj, (string)id, value);
                }
                else {
                    ScriptableObject.PutProperty (obj, (int)id, value);
                }
            }
            return obj;
        }
Ejemplo n.º 3
0
 public static IScriptable newArrayLiteral(object [] objects, int [] skipIndexces, Context cx, IScriptable scope)
 {
     int count = objects.Length;
     int skipCount = 0;
     if (skipIndexces != null) {
         skipCount = skipIndexces.Length;
     }
     int length = count + skipCount;
     int lengthObj = (int)length;
     IScriptable arrayObj;
     /*
     * If the version is 120, then new Array(4) means create a new
     * array with 4 as the first element.  In this case, we have to
     * set length property manually.
     */
     if (cx.Version == Context.Versions.JS1_2) {
         arrayObj = cx.NewObject (scope, "Array", ScriptRuntime.EmptyArgs);
         ScriptableObject.PutProperty (arrayObj, "length", (object)lengthObj);
     }
     else {
         arrayObj = cx.NewObject (scope, "Array", new object [] { lengthObj });
     }
     int skip = 0;
     for (int i = 0, j = 0; i != length; ++i) {
         if (skip != skipCount && skipIndexces [skip] == i) {
             ++skip;
             continue;
         }
         ScriptableObject.PutProperty (arrayObj, i, objects [j]);
         ++j;
     }
     return arrayObj;
 }