merge -r 61110:61111
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / ErrorConstructor.cs
index 0ea629119d4e6512464c2324484e46d56b50ce8a..ece203310f96f8d7b976fb721c106ff4c85a95b5 100644 (file)
@@ -28,6 +28,7 @@
 //
 
 using System;
+using System.Reflection;
 
 namespace Microsoft.JScript {
 
@@ -43,16 +44,89 @@ namespace Microsoft.JScript {
 
        public class ErrorConstructor : ScriptFunction {
 
+               ErrorType error_type;
+               internal static ErrorConstructor Ctr = new ErrorConstructor ();
+               internal static ErrorConstructor EvalErrorCtr = new ErrorConstructor (ErrorType.EvalError);
+               internal static ErrorConstructor RangeErrorCtr = new ErrorConstructor (ErrorType.RangeError);
+               internal static ErrorConstructor ReferenceErrorCtr = new ErrorConstructor (ErrorType.ReferenceError);
+               internal static ErrorConstructor SyntaxErrorCtr = new ErrorConstructor (ErrorType.SyntaxError);
+               internal static ErrorConstructor TypeErrorCtr = new ErrorConstructor (ErrorType.TypeError);
+               internal static ErrorConstructor URIErrorCtr = new ErrorConstructor (ErrorType.URIError);
+
+               static Type ErrorTypeToClass (ErrorType type)
+               {
+                       switch (type) {
+                       case ErrorType.EvalError:
+                               return typeof (EvalErrorObject);
+                       case ErrorType.RangeError:
+                               return typeof (RangeErrorObject);
+                       case ErrorType.ReferenceError:
+                               return typeof (ReferenceErrorObject);
+                       case ErrorType.SyntaxError:
+                               return typeof (SyntaxErrorObject);
+                       case ErrorType.TypeError:
+                               return typeof (TypeErrorObject);
+                       case ErrorType.URIError:
+                               return typeof (URIErrorObject);
+                       }
+
+                       Console.WriteLine ("ErrorTypeToClass: unknown type {0}", type);
+                       throw new NotImplementedException ();
+               }
+
+               static string ErrorTypeToName (ErrorType type)
+               {
+                       switch (type) {
+                       case ErrorType.EvalError:
+                               return "EvalError";
+                       case ErrorType.OtherError:
+                               return "Error"; // TODO: Is this correct?
+                       case ErrorType.RangeError:
+                               return "RangeError";
+                       case ErrorType.ReferenceError:
+                               return "ReferenceError";
+                       case ErrorType.SyntaxError:
+                               return "SyntaxError";
+                       case ErrorType.TypeError:
+                               return "TypeError";
+                       case ErrorType.URIError:
+                               return "URIError";
+                       }
+
+                       Console.WriteLine ("ErrorTypeToName: unknown type {0}", type);
+                       throw new NotImplementedException ();
+               }
+
+               internal ErrorConstructor ()
+               {
+                       name = "Error";
+                       _prototype = ErrorPrototype.Proto;
+                       _length = 1;
+               }
+
+               internal ErrorConstructor (ErrorType errorType)
+               {
+                       error_type = errorType;
+                       name = ErrorTypeToName (errorType);
+                       _prototype = ErrorPrototype.Proto;
+                       _length = 1;
+               }
+
                [JSFunctionAttribute (JSFunctionAttributeEnum.HasVarArgs)]
                public new ErrorObject CreateInstance (params Object [] args)
                {
-                       throw new NotImplementedException ();
+                       string message = "";
+                       if (args.Length >= 1)
+                               message = Convert.ToString (args[0]);
+
+                       Type type = ErrorTypeToClass (error_type);
+                       return (ErrorObject) Activator.CreateInstance (type, new object [] { message });
                }
 
                [JSFunctionAttribute(JSFunctionAttributeEnum.HasVarArgs)]
                public Object Invoke (params Object [] args)
                {
-                       throw new NotImplementedException ();
+                       return CreateInstance (args);
                }
        }
-}
\ No newline at end of file
+}