Пример #1
0
 private void makeRegex(Arguments args)
 {
     var ptrn = args[0];
     if (ptrn.valueType == JSObjectType.Object && ptrn.oValue is RegExp)
     {
         if (args.GetMember("length").iValue > 1 && args[1].valueType > JSObjectType.Undefined)
             throw new JSException(new TypeError("Cannot supply flags when constructing one RegExp from another"));
         oValue = ptrn.oValue;
         regEx = (oValue as RegExp).regEx;
         _global = (oValue as RegExp).global;
         _source = (oValue as RegExp)._source;
         return;
     }
     var pattern = ptrn.valueType > JSObjectType.Undefined ? ptrn.ToString() : "";
     var flags = args.GetMember("length").iValue > 1 && args[1].valueType > JSObjectType.Undefined ? args[1].ToString() : "";
     makeRegex(pattern, flags);
 }
Пример #2
0
 public static JSObject toString(JSObject self, Arguments radix)
 {
     var ovt = self.valueType;
     if (self.valueType > JSObjectType.Double && self.GetType() == typeof(Number))
         self.valueType = self.dValue == 0.0 ? JSObjectType.Int : JSObjectType.Double;
     try
     {
         if (self.valueType != JSObjectType.Int && self.valueType != JSObjectType.Double)
             throw new JSException((new TypeError("Try to call Number.toString on not Number object")));
         int r = 10;
         if (radix != null && radix.GetMember("length").iValue > 0)
         {
             var ar = radix[0];
             if (ar.valueType == JSObjectType.Object && ar.oValue == null)
                 throw new JSException((new Error("Radix can't be null.")));
             switch (ar.valueType)
             {
                 case JSObjectType.Int:
                 case JSObjectType.Bool:
                     {
                         r = ar.iValue;
                         break;
                     }
                 case JSObjectType.Double:
                     {
                         r = (int)ar.dValue;
                         break;
                     }
                 case JSObjectType.NotExistsInObject:
                 case JSObjectType.Undefined:
                     {
                         r = 10;
                         break;
                     }
                 default:
                     {
                         r = Tools.JSObjectToInt32(ar);
                         break;
                     }
             }
         }
         if (r < 2 || r > 36)
             throw new JSException((new TypeError("Radix must be between 2 and 36.")));
         if (r == 10)
             return self.ToString();
         else
         {
             long res = self.iValue;
             var sres = new StringBuilder();
             bool neg;
             if (self.valueType == JSObjectType.Double)
             {
                 if (double.IsNaN(self.dValue))
                     return "NaN";
                 if (double.IsPositiveInfinity(self.dValue))
                     return "Infinity";
                 if (double.IsNegativeInfinity(self.dValue))
                     return "-Infinity";
                 res = (long)self.dValue;
                 if (res != self.dValue) // your bunny wrote
                 {
                     double dtemp = self.dValue;
                     neg = dtemp < 0;
                     if (neg)
                         dtemp = -dtemp;
                     sres.Append(Tools.NumChars[(int)(dtemp % r)]);
                     res /= r;
                     while (dtemp >= 1.0)
                     {
                         sres.Append(Tools.NumChars[(int)(dtemp % r)]);
                         dtemp /= r;
                     }
                     if (neg)
                         sres.Append('-');
                     for (int i = sres.Length - 1, j = 0; i > j; j++, i--)
                     {
                         sres[i] ^= sres[j];
                         sres[j] ^= sres[i];
                         sres[i] ^= sres[j];
                         sres[i] += (char)((sres[i] / 'A') * ('a' - 'A'));
                         sres[j] += (char)((sres[j] / 'A') * ('a' - 'A'));
                     }
                     return sres.ToString();
                 }
             }
             neg = res < 0;
             if (neg)
                 res = -res;
             if (res < 0)
                 throw new JSException(new Error("Internal error"));
             sres.Append(Tools.NumChars[res % r]);
             res /= r;
             while (res != 0)
             {
                 sres.Append(Tools.NumChars[res % r]);
                 res /= r;
             }
             if (neg)
                 sres.Append('-');
             for (int i = sres.Length - 1, j = 0; i > j; j++, i--)
             {
                 sres[i] ^= sres[j];
                 sres[j] ^= sres[i];
                 sres[i] ^= sres[j];
                 sres[i] += (char)((sres[i] / 'A') * ('a' - 'A'));
                 sres[j] += (char)((sres[j] / 'A') * ('a' - 'A'));
             }
             return sres.ToString();
         }
     }
     finally
     {
         self.valueType = ovt;
     }
 }
Пример #3
0
 public ArrayBuffer slice(Arguments args)
 {
     if (args == null)
         throw new ArgumentNullException("args");
     var l = Tools.JSObjectToInt32(args.GetMember("length"));
     if (l == 0)
         return this;
     if (l == 1)
         return slice(Tools.JSObjectToInt32(args[0]), Data.Length - 1);
     else
         return slice(Tools.JSObjectToInt32(args[0]), Tools.JSObjectToInt32(args[1]));
 }