js/src/tests/supporting/sta.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/supporting/sta.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,901 @@
     1.4 +/// Copyright (c) 2012 Ecma International.  All rights reserved. 
     1.5 +/// Ecma International makes this code available under the terms and conditions set
     1.6 +/// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the 
     1.7 +/// "Use Terms").   Any redistribution of this code must retain the above 
     1.8 +/// copyright and this notice and otherwise comply with the Use Terms.
     1.9 +
    1.10 +//-----------------------------------------------------------------------------
    1.11 +function compareArray(aExpected, aActual) {
    1.12 +    if (aActual.length != aExpected.length) {
    1.13 +        return false;
    1.14 +    }
    1.15 +
    1.16 +    aExpected.sort();
    1.17 +    aActual.sort();
    1.18 +
    1.19 +    var s;
    1.20 +    for (var i = 0; i < aExpected.length; i++) {
    1.21 +        if (aActual[i] !== aExpected[i]) {
    1.22 +            return false;
    1.23 +        }
    1.24 +    }
    1.25 +    return true;
    1.26 +}
    1.27 +
    1.28 +//-----------------------------------------------------------------------------
    1.29 +function arrayContains(arr, expected) {
    1.30 +    var found;
    1.31 +    for (var i = 0; i < expected.length; i++) {
    1.32 +        found = false;
    1.33 +        for (var j = 0; j < arr.length; j++) {
    1.34 +            if (expected[i] === arr[j]) {
    1.35 +                found = true;
    1.36 +                break;
    1.37 +            }
    1.38 +        }
    1.39 +        if (!found) {
    1.40 +            return false;
    1.41 +        }
    1.42 +    }
    1.43 +    return true;
    1.44 +}
    1.45 +
    1.46 +//-----------------------------------------------------------------------------
    1.47 +var supportsArrayIndexGettersOnArrays = undefined;
    1.48 +function fnSupportsArrayIndexGettersOnArrays() {
    1.49 +    if (typeof supportsArrayIndexGettersOnArrays !== "undefined") {
    1.50 +        return supportsArrayIndexGettersOnArrays;
    1.51 +    }
    1.52 +
    1.53 +    supportsArrayIndexGettersOnArrays = false;
    1.54 +
    1.55 +    if (fnExists(Object.defineProperty)) {
    1.56 +        var arr = [];
    1.57 +        Object.defineProperty(arr, "0", {
    1.58 +            get: function() {
    1.59 +                supportsArrayIndexGettersOnArrays = true;
    1.60 +                return 0;
    1.61 +            }
    1.62 +        });
    1.63 +        var res = arr[0];
    1.64 +    }
    1.65 +
    1.66 +    return supportsArrayIndexGettersOnArrays;
    1.67 +}
    1.68 +
    1.69 +//-----------------------------------------------------------------------------
    1.70 +var supportsArrayIndexGettersOnObjects = undefined;
    1.71 +function fnSupportsArrayIndexGettersOnObjects() {
    1.72 +    if (typeof supportsArrayIndexGettersOnObjects !== "undefined")
    1.73 +        return supportsArrayIndexGettersOnObjects;
    1.74 +
    1.75 +    supportsArrayIndexGettersOnObjects = false;
    1.76 +
    1.77 +    if (fnExists(Object.defineProperty)) {
    1.78 +        var obj = {};
    1.79 +        Object.defineProperty(obj, "0", {
    1.80 +            get: function() {
    1.81 +                supportsArrayIndexGettersOnObjects = true;
    1.82 +                return 0;
    1.83 +            }
    1.84 +        });
    1.85 +        var res = obj[0];
    1.86 +    }
    1.87 +
    1.88 +    return supportsArrayIndexGettersOnObjects;
    1.89 +}
    1.90 +
    1.91 +//-----------------------------------------------------------------------------
    1.92 +function ConvertToFileUrl(pathStr) {
    1.93 +    return "file:" + pathStr.replace(/\\/g, "/");
    1.94 +}
    1.95 +
    1.96 +//-----------------------------------------------------------------------------
    1.97 +function fnExists(/*arguments*/) {
    1.98 +    for (var i = 0; i < arguments.length; i++) {
    1.99 +        if (typeof (arguments[i]) !== "function") return false;
   1.100 +    }
   1.101 +    return true;
   1.102 +}
   1.103 +
   1.104 +//-----------------------------------------------------------------------------
   1.105 +var __globalObject = Function("return this;")();
   1.106 +function fnGlobalObject() {
   1.107 +     return __globalObject;
   1.108 +}
   1.109 +
   1.110 +//-----------------------------------------------------------------------------
   1.111 +function fnSupportsStrict() {
   1.112 +    "use strict";
   1.113 +    try {
   1.114 +        eval('with ({}) {}');
   1.115 +        return false;
   1.116 +    } catch (e) {
   1.117 +        return true;
   1.118 +    }
   1.119 +}
   1.120 +
   1.121 +//-----------------------------------------------------------------------------
   1.122 +//Verify all attributes specified data property of given object:
   1.123 +//value, writable, enumerable, configurable
   1.124 +//If all attribute values are expected, return true, otherwise, return false
   1.125 +function dataPropertyAttributesAreCorrect(obj,
   1.126 +                                          name,
   1.127 +                                          value,
   1.128 +                                          writable,
   1.129 +                                          enumerable,
   1.130 +                                          configurable) {
   1.131 +    var attributesCorrect = true;
   1.132 +
   1.133 +    if (obj[name] !== value) {
   1.134 +        if (typeof obj[name] === "number" &&
   1.135 +            isNaN(obj[name]) &&
   1.136 +            typeof value === "number" &&
   1.137 +            isNaN(value)) {
   1.138 +            // keep empty
   1.139 +        } else {
   1.140 +            attributesCorrect = false;
   1.141 +        }
   1.142 +    }
   1.143 +
   1.144 +    try {
   1.145 +        if (obj[name] === "oldValue") {
   1.146 +            obj[name] = "newValue";
   1.147 +        } else {
   1.148 +            obj[name] = "OldValue";
   1.149 +        }
   1.150 +    } catch (we) {
   1.151 +    }
   1.152 +
   1.153 +    var overwrited = false;
   1.154 +    if (obj[name] !== value) {
   1.155 +        if (typeof obj[name] === "number" &&
   1.156 +            isNaN(obj[name]) &&
   1.157 +            typeof value === "number" &&
   1.158 +            isNaN(value)) {
   1.159 +            // keep empty
   1.160 +        } else {
   1.161 +            overwrited = true;
   1.162 +        }
   1.163 +    }
   1.164 +    if (overwrited !== writable) {
   1.165 +        attributesCorrect = false;
   1.166 +    }
   1.167 +
   1.168 +    var enumerated = false;
   1.169 +    for (var prop in obj) {
   1.170 +        if (obj.hasOwnProperty(prop) && prop === name) {
   1.171 +            enumerated = true;
   1.172 +        }
   1.173 +    }
   1.174 +
   1.175 +    if (enumerated !== enumerable) {
   1.176 +        attributesCorrect = false;
   1.177 +    }
   1.178 +
   1.179 +
   1.180 +    var deleted = false;
   1.181 +
   1.182 +    try {
   1.183 +        delete obj[name];
   1.184 +    } catch (de) {
   1.185 +    }
   1.186 +    if (!obj.hasOwnProperty(name)) {
   1.187 +        deleted = true;
   1.188 +    }
   1.189 +    if (deleted !== configurable) {
   1.190 +        attributesCorrect = false;
   1.191 +    }
   1.192 +
   1.193 +    return attributesCorrect;
   1.194 +}
   1.195 +
   1.196 +//-----------------------------------------------------------------------------
   1.197 +//Verify all attributes specified accessor property of given object:
   1.198 +//get, set, enumerable, configurable
   1.199 +//If all attribute values are expected, return true, otherwise, return false
   1.200 +function accessorPropertyAttributesAreCorrect(obj,
   1.201 +                                              name,
   1.202 +                                              get,
   1.203 +                                              set,
   1.204 +                                              setVerifyHelpProp,
   1.205 +                                              enumerable,
   1.206 +                                              configurable) {
   1.207 +    var attributesCorrect = true;
   1.208 +
   1.209 +    if (get !== undefined) {
   1.210 +        if (obj[name] !== get()) {
   1.211 +            if (typeof obj[name] === "number" &&
   1.212 +                isNaN(obj[name]) &&
   1.213 +                typeof get() === "number" &&
   1.214 +                isNaN(get())) {
   1.215 +                // keep empty
   1.216 +            } else {
   1.217 +                attributesCorrect = false;
   1.218 +            }
   1.219 +        }
   1.220 +    } else {
   1.221 +        if (obj[name] !== undefined) {
   1.222 +            attributesCorrect = false;
   1.223 +        }
   1.224 +    }
   1.225 +
   1.226 +    try {
   1.227 +        var desc = Object.getOwnPropertyDescriptor(obj, name);
   1.228 +        if (typeof desc.set === "undefined") {
   1.229 +            if (typeof set !== "undefined") {
   1.230 +                attributesCorrect = false;
   1.231 +            }
   1.232 +        } else {
   1.233 +            obj[name] = "toBeSetValue";
   1.234 +            if (obj[setVerifyHelpProp] !== "toBeSetValue") {
   1.235 +                attributesCorrect = false;
   1.236 +            }
   1.237 +        }
   1.238 +    } catch (se) {
   1.239 +        throw se;
   1.240 +    }
   1.241 +
   1.242 +
   1.243 +    var enumerated = false;
   1.244 +    for (var prop in obj) {
   1.245 +        if (obj.hasOwnProperty(prop) && prop === name) {
   1.246 +            enumerated = true;
   1.247 +        }
   1.248 +    }
   1.249 +
   1.250 +    if (enumerated !== enumerable) {
   1.251 +        attributesCorrect = false;
   1.252 +    }
   1.253 +
   1.254 +
   1.255 +    var deleted = false;
   1.256 +    try {
   1.257 +        delete obj[name];
   1.258 +    } catch (de) {
   1.259 +        throw de;
   1.260 +    }
   1.261 +    if (!obj.hasOwnProperty(name)) {
   1.262 +        deleted = true;
   1.263 +    }
   1.264 +    if (deleted !== configurable) {
   1.265 +        attributesCorrect = false;
   1.266 +    }
   1.267 +
   1.268 +    return attributesCorrect;
   1.269 +}
   1.270 +
   1.271 +//-----------------------------------------------------------------------------
   1.272 +var NotEarlyErrorString = "NotEarlyError";
   1.273 +var EarlyErrorRePat = "^((?!" + NotEarlyErrorString + ").)*$";
   1.274 +var NotEarlyError = new Error(NotEarlyErrorString);
   1.275 +
   1.276 +//-----------------------------------------------------------------------------
   1.277 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.278 +// This code is governed by the BSD license found in the LICENSE file.
   1.279 +
   1.280 +function Test262Error(message) {
   1.281 +    if (message) this.message = message;
   1.282 +}
   1.283 +
   1.284 +Test262Error.prototype.toString = function () {
   1.285 +    return "Test262 Error: " + this.message;
   1.286 +};
   1.287 +
   1.288 +function testFailed(message) {
   1.289 +    throw new Test262Error(message);
   1.290 +}
   1.291 +
   1.292 +
   1.293 +function testPrint(message) {
   1.294 +
   1.295 +}
   1.296 +
   1.297 +
   1.298 +//adaptors for Test262 framework
   1.299 +function $PRINT(message) {
   1.300 +
   1.301 +}
   1.302 +
   1.303 +function $INCLUDE(message) { }
   1.304 +function $ERROR(message) {
   1.305 +    testFailed(message);
   1.306 +}
   1.307 +
   1.308 +function $FAIL(message) {
   1.309 +    testFailed(message);
   1.310 +}
   1.311 +
   1.312 +
   1.313 +
   1.314 +//Sputnik library definitions
   1.315 +//Ultimately these should be namespaced some how and only made
   1.316 +//available to tests that explicitly include them.
   1.317 +//For now, we just define the globally
   1.318 +
   1.319 +//math_precision.js
   1.320 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.321 +// This code is governed by the BSD license found in the LICENSE file.
   1.322 +
   1.323 +function getPrecision(num) {
   1.324 +    //TODO: Create a table of prec's,
   1.325 +    //      because using Math for testing Math isn't that correct.
   1.326 +
   1.327 +    var log2num = Math.log(Math.abs(num)) / Math.LN2;
   1.328 +    var pernum = Math.ceil(log2num);
   1.329 +    return (2 * Math.pow(2, -52 + pernum));
   1.330 +    //return(0);
   1.331 +}
   1.332 +
   1.333 +
   1.334 +//math_isequal.js
   1.335 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.336 +// This code is governed by the BSD license found in the LICENSE file.
   1.337 +
   1.338 +var prec;
   1.339 +function isEqual(num1, num2) {
   1.340 +    if ((num1 === Infinity) && (num2 === Infinity)) {
   1.341 +        return (true);
   1.342 +    }
   1.343 +    if ((num1 === -Infinity) && (num2 === -Infinity)) {
   1.344 +        return (true);
   1.345 +    }
   1.346 +    prec = getPrecision(Math.min(Math.abs(num1), Math.abs(num2)));
   1.347 +    return (Math.abs(num1 - num2) <= prec);
   1.348 +    //return(num1 === num2);
   1.349 +}
   1.350 +
   1.351 +//numeric_conversion.js
   1.352 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.353 +// This code is governed by the BSD license found in the LICENSE file.
   1.354 +
   1.355 +function ToInteger(p) {
   1.356 +    var x = Number(p);
   1.357 +
   1.358 +    if (isNaN(x)) {
   1.359 +        return +0;
   1.360 +    }
   1.361 +
   1.362 +    if ((x === +0)
   1.363 +  || (x === -0)
   1.364 +  || (x === Number.POSITIVE_INFINITY)
   1.365 +  || (x === Number.NEGATIVE_INFINITY)) {
   1.366 +        return x;
   1.367 +    }
   1.368 +
   1.369 +    var sign = (x < 0) ? -1 : 1;
   1.370 +
   1.371 +    return (sign * Math.floor(Math.abs(x)));
   1.372 +}
   1.373 +
   1.374 +//Date_constants.js
   1.375 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.376 +// This code is governed by the BSD license found in the LICENSE file.
   1.377 +
   1.378 +var HoursPerDay = 24;
   1.379 +var MinutesPerHour = 60;
   1.380 +var SecondsPerMinute = 60;
   1.381 +
   1.382 +var msPerDay = 86400000;
   1.383 +var msPerSecond = 1000;
   1.384 +var msPerMinute = 60000;
   1.385 +var msPerHour = 3600000;
   1.386 +
   1.387 +var date_1899_end = -2208988800001;
   1.388 +var date_1900_start = -2208988800000;
   1.389 +var date_1969_end = -1;
   1.390 +var date_1970_start = 0;
   1.391 +var date_1999_end = 946684799999;
   1.392 +var date_2000_start = 946684800000;
   1.393 +var date_2099_end = 4102444799999;
   1.394 +var date_2100_start = 4102444800000;
   1.395 +
   1.396 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.397 +// This code is governed by the BSD license found in the LICENSE file.
   1.398 +
   1.399 +//the following values are normally generated by the sputnik.py driver
   1.400 +var $LocalTZ,
   1.401 +    $DST_start_month,
   1.402 +    $DST_start_sunday,
   1.403 +    $DST_start_hour,
   1.404 +    $DST_start_minutes,
   1.405 +    $DST_end_month,
   1.406 +    $DST_end_sunday,
   1.407 +    $DST_end_hour,
   1.408 +    $DST_end_minutes;
   1.409 +
   1.410 +(function () {
   1.411 +    /**
   1.412 +      * Finds the first date, starting from |start|, where |predicate|
   1.413 +      * holds.
   1.414 +      */
   1.415 +    var findNearestDateBefore = function(start, predicate) {
   1.416 +        var current = start;
   1.417 +        var month = 1000 * 60 * 60 * 24 * 30;
   1.418 +        for (var step = month; step > 0; step = Math.floor(step / 3)) {
   1.419 +            if (!predicate(current)) {
   1.420 +                while (!predicate(current))
   1.421 +                    current = new Date(current.getTime() + step);
   1.422 +                    current = new Date(current.getTime() - step);
   1.423 +                }
   1.424 +        }
   1.425 +        while (!predicate(current)) {
   1.426 +            current = new Date(current.getTime() + 1);
   1.427 +        }
   1.428 +        return current;
   1.429 +    };
   1.430 +
   1.431 +    var juneDate = new Date(2000, 5, 20, 0, 0, 0, 0);
   1.432 +    var decemberDate = new Date(2000, 11, 20, 0, 0, 0, 0);
   1.433 +    var juneOffset = juneDate.getTimezoneOffset();
   1.434 +    var decemberOffset = decemberDate.getTimezoneOffset();
   1.435 +    var isSouthernHemisphere = (juneOffset > decemberOffset);
   1.436 +    var winterTime = isSouthernHemisphere ? juneDate : decemberDate;
   1.437 +    var summerTime = isSouthernHemisphere ? decemberDate : juneDate;
   1.438 +
   1.439 +    var dstStart = findNearestDateBefore(winterTime, function (date) {
   1.440 +        return date.getTimezoneOffset() == summerTime.getTimezoneOffset();
   1.441 +    });
   1.442 +    $DST_start_month = dstStart.getMonth();
   1.443 +    $DST_start_sunday = dstStart.getDate() > 15 ? '"last"' : '"first"';
   1.444 +    $DST_start_hour = dstStart.getHours();
   1.445 +    $DST_start_minutes = dstStart.getMinutes();
   1.446 +
   1.447 +    var dstEnd = findNearestDateBefore(summerTime, function (date) {
   1.448 +        return date.getTimezoneOffset() == winterTime.getTimezoneOffset();
   1.449 +    });
   1.450 +    $DST_end_month = dstEnd.getMonth();
   1.451 +    $DST_end_sunday = dstEnd.getDate() > 15 ? '"last"' : '"first"';
   1.452 +    $DST_end_hour = dstEnd.getHours();
   1.453 +    $DST_end_minutes = dstEnd.getMinutes();
   1.454 +
   1.455 +    return;
   1.456 +})();
   1.457 +
   1.458 +
   1.459 +//Date.library.js
   1.460 +// Copyright 2009 the Sputnik authors.  All rights reserved.
   1.461 +// This code is governed by the BSD license found in the LICENSE file.
   1.462 +
   1.463 +//15.9.1.2 Day Number and Time within Day
   1.464 +function Day(t) {
   1.465 +  return Math.floor(t/msPerDay);
   1.466 +}
   1.467 +
   1.468 +function TimeWithinDay(t) {
   1.469 +  return t%msPerDay;
   1.470 +}
   1.471 +
   1.472 +//15.9.1.3 Year Number
   1.473 +function DaysInYear(y){
   1.474 +  if(y%4 != 0) return 365;
   1.475 +  if(y%4 == 0 && y%100 != 0) return 366;
   1.476 +  if(y%100 == 0 && y%400 != 0) return 365;
   1.477 +  if(y%400 == 0) return 366;
   1.478 +}
   1.479 +
   1.480 +function DayFromYear(y) {
   1.481 +  return (365*(y-1970)
   1.482 +          + Math.floor((y-1969)/4)
   1.483 +          - Math.floor((y-1901)/100)
   1.484 +          + Math.floor((y-1601)/400));
   1.485 +}
   1.486 +
   1.487 +function TimeFromYear(y){
   1.488 +  return msPerDay*DayFromYear(y);
   1.489 +}
   1.490 +
   1.491 +function YearFromTime(t) {
   1.492 +  t = Number(t);
   1.493 +  var sign = ( t < 0 ) ? -1 : 1;
   1.494 +  var year = ( sign < 0 ) ? 1969 : 1970;
   1.495 +
   1.496 +  for(var time = 0;;year += sign){
   1.497 +    time = TimeFromYear(year);
   1.498 +
   1.499 +    if(sign > 0 && time > t){
   1.500 +      year -= sign;
   1.501 +      break;
   1.502 +    }
   1.503 +    else if(sign < 0 && time <= t){
   1.504 +      break;
   1.505 +    }
   1.506 +  };
   1.507 +  return year;
   1.508 +}
   1.509 +
   1.510 +function InLeapYear(t){
   1.511 +  if(DaysInYear(YearFromTime(t)) == 365)
   1.512 +    return 0;
   1.513 +
   1.514 +  if(DaysInYear(YearFromTime(t)) == 366)
   1.515 +    return 1;
   1.516 +}
   1.517 +
   1.518 +function DayWithinYear(t) {
   1.519 +  return Day(t)-DayFromYear(YearFromTime(t));
   1.520 +}
   1.521 +
   1.522 +//15.9.1.4 Month Number
   1.523 +function MonthFromTime(t){
   1.524 +  var day = DayWithinYear(t);
   1.525 +  var leap = InLeapYear(t);
   1.526 +
   1.527 +  if((0 <= day) && (day < 31)) return 0;
   1.528 +  if((31 <= day) && (day < (59+leap))) return 1;
   1.529 +  if(((59+leap) <= day) && (day < (90+leap))) return 2;
   1.530 +  if(((90+leap) <= day) && (day < (120+leap))) return 3;
   1.531 +  if(((120+leap) <= day) && (day < (151+leap))) return 4;
   1.532 +  if(((151+leap) <= day) && (day < (181+leap))) return 5;
   1.533 +  if(((181+leap) <= day) && (day < (212+leap))) return 6;
   1.534 +  if(((212+leap) <= day) && (day < (243+leap))) return 7;
   1.535 +  if(((243+leap) <= day) && (day < (273+leap))) return 8;
   1.536 +  if(((273+leap) <= day) && (day < (304+leap))) return 9;
   1.537 +  if(((304+leap) <= day) && (day < (334+leap))) return 10;
   1.538 +  if(((334+leap) <= day) && (day < (365+leap))) return 11;
   1.539 +}
   1.540 +
   1.541 +//15.9.1.5 Date Number
   1.542 +function DateFromTime(t) {
   1.543 +  var day = DayWithinYear(t);
   1.544 +  var month = MonthFromTime(t);
   1.545 +  var leap = InLeapYear(t);
   1.546 +
   1.547 +  if(month == 0) return day+1;
   1.548 +  if(month == 1) return day-30;
   1.549 +  if(month == 2) return day-58-leap;
   1.550 +  if(month == 3) return day-89-leap;
   1.551 +  if(month == 4) return day-119-leap;
   1.552 +  if(month == 5) return day-150-leap;
   1.553 +  if(month == 6) return day-180-leap;
   1.554 +  if(month == 7) return day-211-leap;
   1.555 +  if(month == 8) return day-242-leap;
   1.556 +  if(month == 9) return day-272-leap;
   1.557 +  if(month == 10) return day-303-leap;
   1.558 +  if(month == 11) return day-333-leap;
   1.559 +}
   1.560 +
   1.561 +//15.9.1.6 Week Day
   1.562 +function WeekDay(t) {
   1.563 +  var weekday = (Day(t)+4)%7;
   1.564 +  return (weekday < 0 ? 7+weekday : weekday);
   1.565 +}
   1.566 +
   1.567 +//15.9.1.9 Daylight Saving Time Adjustment
   1.568 +$LocalTZ = (new Date()).getTimezoneOffset() / -60;
   1.569 +if (DaylightSavingTA((new Date()).valueOf()) !== 0) {
   1.570 +   $LocalTZ -= 1;
   1.571 +}
   1.572 +var LocalTZA = $LocalTZ*msPerHour;
   1.573 +
   1.574 +function DaysInMonth(m, leap) {
   1.575 +  m = m%12;
   1.576 +
   1.577 +  //April, June, Sept, Nov
   1.578 +  if(m == 3 || m == 5 || m == 8 || m == 10 ) {
   1.579 +    return 30;
   1.580 +  }
   1.581 +
   1.582 +  //Jan, March, May, July, Aug, Oct, Dec
   1.583 +  if(m == 0 || m == 2 || m == 4 || m == 6 || m == 7 || m == 9 || m == 11){
   1.584 +    return 31;
   1.585 +  }
   1.586 +
   1.587 +  //Feb
   1.588 +  return 28+leap;
   1.589 +}
   1.590 +
   1.591 +function GetSundayInMonth(t, m, count){
   1.592 +    var year = YearFromTime(t);
   1.593 +    var tempDate;
   1.594 +
   1.595 +    if (count==='"first"') {
   1.596 +        for (var d=1; d <= DaysInMonth(m, InLeapYear(t)); d++) {
   1.597 +            tempDate = new Date(year, m, d);
   1.598 +            if (tempDate.getDay()===0) {
   1.599 +                return tempDate.valueOf();
   1.600 +            }
   1.601 +        }
   1.602 +    } else if(count==='"last"') {
   1.603 +        for (var d=DaysInMonth(m, InLeapYear(t)); d>0; d--) {
   1.604 +            tempDate = new Date(year, m, d);
   1.605 +            if (tempDate.getDay()===0) {
   1.606 +                return tempDate.valueOf();
   1.607 +            }
   1.608 +        }
   1.609 +    }
   1.610 +    throw new Error("Unsupported 'count' arg:" + count);
   1.611 +}
   1.612 +/*
   1.613 +function GetSundayInMonth(t, m, count){
   1.614 +  var year = YearFromTime(t);
   1.615 +  var leap = InLeapYear(t);
   1.616 +  var day = 0;
   1.617 +
   1.618 +  if(m >= 1) day += DaysInMonth(0, leap);
   1.619 +  if(m >= 2) day += DaysInMonth(1, leap);
   1.620 +  if(m >= 3) day += DaysInMonth(2, leap);
   1.621 +  if(m >= 4) day += DaysInMonth(3, leap);
   1.622 +  if(m >= 5) day += DaysInMonth(4, leap);
   1.623 +  if(m >= 6) day += DaysInMonth(5, leap);
   1.624 +  if(m >= 7) day += DaysInMonth(6, leap);
   1.625 +  if(m >= 8) day += DaysInMonth(7, leap);
   1.626 +  if(m >= 9) day += DaysInMonth(8, leap);
   1.627 +  if(m >= 10) day += DaysInMonth(9, leap);
   1.628 +  if(m >= 11) day += DaysInMonth(10, leap);
   1.629 +
   1.630 +  var month_start = TimeFromYear(year)+day*msPerDay;
   1.631 +  var sunday = 0;
   1.632 +
   1.633 +  if(count === "last"){
   1.634 +    for(var last_sunday = month_start+DaysInMonth(m, leap)*msPerDay;
   1.635 +      WeekDay(last_sunday)>0;
   1.636 +      last_sunday -= msPerDay
   1.637 +    ){};
   1.638 +    sunday = last_sunday;
   1.639 +  }
   1.640 +  else {
   1.641 +    for(var first_sunday = month_start;
   1.642 +      WeekDay(first_sunday)>0;
   1.643 +      first_sunday += msPerDay
   1.644 +    ){};
   1.645 +    sunday = first_sunday+7*msPerDay*(count-1);
   1.646 +  }
   1.647 +
   1.648 +  return sunday;
   1.649 +}*/
   1.650 +
   1.651 +function DaylightSavingTA(t) {
   1.652 +//  t = t-LocalTZA;
   1.653 +
   1.654 +  var DST_start = GetSundayInMonth(t, $DST_start_month, $DST_start_sunday) +
   1.655 +                  $DST_start_hour*msPerHour +
   1.656 +                  $DST_start_minutes*msPerMinute;
   1.657 +
   1.658 +  var k = new Date(DST_start);
   1.659 +
   1.660 +  var DST_end   = GetSundayInMonth(t, $DST_end_month, $DST_end_sunday) +
   1.661 +                  $DST_end_hour*msPerHour +
   1.662 +                  $DST_end_minutes*msPerMinute;
   1.663 +
   1.664 +  if ( t >= DST_start && t < DST_end ) {
   1.665 +    return msPerHour;
   1.666 +  } else {
   1.667 +    return 0;
   1.668 +  }
   1.669 +}
   1.670 +
   1.671 +//15.9.1.9 Local Time
   1.672 +function LocalTime(t){
   1.673 +  return t+LocalTZA+DaylightSavingTA(t);
   1.674 +}
   1.675 +
   1.676 +function UTC(t) {
   1.677 +  return t-LocalTZA-DaylightSavingTA(t-LocalTZA);
   1.678 +}
   1.679 +
   1.680 +//15.9.1.10 Hours, Minutes, Second, and Milliseconds
   1.681 +function HourFromTime(t){
   1.682 +  return Math.floor(t/msPerHour)%HoursPerDay;
   1.683 +}
   1.684 +
   1.685 +function MinFromTime(t){
   1.686 +  return Math.floor(t/msPerMinute)%MinutesPerHour;
   1.687 +}
   1.688 +
   1.689 +function SecFromTime(t){
   1.690 +  return Math.floor(t/msPerSecond)%SecondsPerMinute;
   1.691 +}
   1.692 +
   1.693 +function msFromTime(t){
   1.694 +  return t%msPerSecond;
   1.695 +}
   1.696 +
   1.697 +//15.9.1.11 MakeTime (hour, min, sec, ms)
   1.698 +function MakeTime(hour, min, sec, ms){
   1.699 +  if ( !isFinite(hour) || !isFinite(min) || !isFinite(sec) || !isFinite(ms)) {
   1.700 +    return Number.NaN;
   1.701 +  }
   1.702 +
   1.703 +  hour = ToInteger(hour);
   1.704 +  min  = ToInteger(min);
   1.705 +  sec  = ToInteger(sec);
   1.706 +  ms   = ToInteger(ms);
   1.707 +
   1.708 +  return ((hour*msPerHour) + (min*msPerMinute) + (sec*msPerSecond) + ms);
   1.709 +}
   1.710 +
   1.711 +//15.9.1.12 MakeDay (year, month, date)
   1.712 +function MakeDay(year, month, date) {
   1.713 +  if ( !isFinite(year) || !isFinite(month) || !isFinite(date)) {
   1.714 +    return Number.NaN;
   1.715 +  }
   1.716 +
   1.717 +  year = ToInteger(year);
   1.718 +  month = ToInteger(month);
   1.719 +  date = ToInteger(date );
   1.720 +
   1.721 +  var result5 = year + Math.floor(month/12);
   1.722 +  var result6 = month%12;
   1.723 +
   1.724 +  var sign = ( year < 1970 ) ? -1 : 1;
   1.725 +  var t =    ( year < 1970 ) ? 1 :  0;
   1.726 +  var y =    ( year < 1970 ) ? 1969 : 1970;
   1.727 +
   1.728 +  if( sign == -1 ){
   1.729 +    for ( y = 1969; y >= year; y += sign ) {
   1.730 +      t += sign * DaysInYear(y)*msPerDay;
   1.731 +    }
   1.732 +  } else {
   1.733 +    for ( y = 1970 ; y < year; y += sign ) {
   1.734 +      t += sign * DaysInYear(y)*msPerDay;
   1.735 +    }
   1.736 +  }
   1.737 +
   1.738 +  var leap = 0;
   1.739 +  for ( var m = 0; m < month; m++ ) {
   1.740 +    //if year is changed, than we need to recalculate leep
   1.741 +    leap = InLeapYear(t);
   1.742 +    t += DaysInMonth(m, leap)*msPerDay;
   1.743 +  }
   1.744 +
   1.745 +  if ( YearFromTime(t) != result5 ) {
   1.746 +    return Number.NaN;
   1.747 +  }
   1.748 +  if ( MonthFromTime(t) != result6 ) {
   1.749 +    return Number.NaN;
   1.750 +  }
   1.751 +  if ( DateFromTime(t) != 1 ) {
   1.752 +    return Number.NaN;
   1.753 +  }
   1.754 +
   1.755 +  return Day(t)+date-1;
   1.756 +}
   1.757 +
   1.758 +//15.9.1.13 MakeDate (day, time)
   1.759 +function MakeDate( day, time ) {
   1.760 +  if(!isFinite(day) || !isFinite(time)) {
   1.761 +    return Number.NaN;
   1.762 +  }
   1.763 +
   1.764 +  return day*msPerDay+time;
   1.765 +}
   1.766 +
   1.767 +//15.9.1.14 TimeClip (time)
   1.768 +function TimeClip(time) {
   1.769 +  if(!isFinite(time) || Math.abs(time) > 8.64e15){
   1.770 +    return Number.NaN;
   1.771 +  }
   1.772 +
   1.773 +  return ToInteger(time);
   1.774 +}
   1.775 +
   1.776 +//Test Functions
   1.777 +//ConstructDate is considered deprecated, and should not be used directly from
   1.778 +//test262 tests as it's incredibly sensitive to DST start/end dates that 
   1.779 +//vary with geographic location.
   1.780 +function ConstructDate(year, month, date, hours, minutes, seconds, ms){
   1.781 +  /*
   1.782 +   * 1. Call ToNumber(year)
   1.783 +   * 2. Call ToNumber(month)
   1.784 +   * 3. If date is supplied use ToNumber(date); else use 1
   1.785 +   * 4. If hours is supplied use ToNumber(hours); else use 0
   1.786 +   * 5. If minutes is supplied use ToNumber(minutes); else use 0
   1.787 +   * 6. If seconds is supplied use ToNumber(seconds); else use 0
   1.788 +   * 7. If ms is supplied use ToNumber(ms); else use 0
   1.789 +   * 8. If Result(1) is not NaN and 0 <= ToInteger(Result(1)) <= 99, Result(8) is
   1.790 +   * 1900+ToInteger(Result(1)); otherwise, Result(8) is Result(1)
   1.791 +   * 9. Compute MakeDay(Result(8), Result(2), Result(3))
   1.792 +   * 10. Compute MakeTime(Result(4), Result(5), Result(6), Result(7))
   1.793 +   * 11. Compute MakeDate(Result(9), Result(10))
   1.794 +   * 12. Set the [[Value]] property of the newly constructed object to TimeClip(UTC(Result(11)))
   1.795 +   */
   1.796 +  var r1 = Number(year);
   1.797 +  var r2 = Number(month);
   1.798 +  var r3 = ((date && arguments.length > 2) ? Number(date) : 1);
   1.799 +  var r4 = ((hours && arguments.length > 3) ? Number(hours) : 0);
   1.800 +  var r5 = ((minutes && arguments.length > 4) ? Number(minutes) : 0);
   1.801 +  var r6 = ((seconds && arguments.length > 5) ? Number(seconds) : 0);
   1.802 +  var r7 = ((ms && arguments.length > 6) ? Number(ms) : 0);
   1.803 +
   1.804 +  var r8 = r1;
   1.805 +
   1.806 +  if(!isNaN(r1) && (0 <= ToInteger(r1)) && (ToInteger(r1) <= 99))
   1.807 +    r8 = 1900+r1;
   1.808 +
   1.809 +  var r9 = MakeDay(r8, r2, r3);
   1.810 +  var r10 = MakeTime(r4, r5, r6, r7);
   1.811 +  var r11 = MakeDate(r9, r10);
   1.812 +
   1.813 +  var retVal = TimeClip(UTC(r11));
   1.814 +  return retVal;
   1.815 +}
   1.816 +
   1.817 +
   1.818 +
   1.819 +/**** Python code for initialize the above constants
   1.820 +// We may want to replicate the following in JavaScript.
   1.821 +// However, using JS date operations to generate parameters that are then used to
   1.822 +// test those some date operations seems unsound.  However, it isn't clear if there
   1.823 +//is a good interoperable alternative.
   1.824 +
   1.825 +# Copyright 2009 the Sputnik authors.  All rights reserved.
   1.826 +# This code is governed by the BSD license found in the LICENSE file.
   1.827 +
   1.828 +def GetDaylightSavingsTimes():
   1.829 +# Is the given floating-point time in DST?
   1.830 +def IsDst(t):
   1.831 +return time.localtime(t)[-1]
   1.832 +# Binary search to find an interval between the two times no greater than
   1.833 +# delta where DST switches, returning the midpoint.
   1.834 +def FindBetween(start, end, delta):
   1.835 +while end - start > delta:
   1.836 +middle = (end + start) / 2
   1.837 +if IsDst(middle) == IsDst(start):
   1.838 +start = middle
   1.839 +else:
   1.840 +end = middle
   1.841 +return (start + end) / 2
   1.842 +now = time.time()
   1.843 +one_month = (30 * 24 * 60 * 60)
   1.844 +# First find a date with different daylight savings.  To avoid corner cases
   1.845 +# we try four months before and after today.
   1.846 +after = now + 4 * one_month
   1.847 +before = now - 4 * one_month
   1.848 +if IsDst(now) == IsDst(before) and IsDst(now) == IsDst(after):
   1.849 +logger.warning("Was unable to determine DST info.")
   1.850 +return None
   1.851 +# Determine when the change occurs between now and the date we just found
   1.852 +# in a different DST.
   1.853 +if IsDst(now) != IsDst(before):
   1.854 +first = FindBetween(before, now, 1)
   1.855 +else:
   1.856 +first = FindBetween(now, after, 1)
   1.857 +# Determine when the change occurs between three and nine months from the
   1.858 +# first.
   1.859 +second = FindBetween(first + 3 * one_month, first + 9 * one_month, 1)
   1.860 +# Find out which switch is into and which if out of DST
   1.861 +if IsDst(first - 1) and not IsDst(first + 1):
   1.862 +start = second
   1.863 +end = first
   1.864 +else:
   1.865 +start = first
   1.866 +end = second
   1.867 +return (start, end)
   1.868 +
   1.869 +
   1.870 +def GetDaylightSavingsAttribs():
   1.871 +times = GetDaylightSavingsTimes()
   1.872 +if not times:
   1.873 +return None
   1.874 +(start, end) = times
   1.875 +def DstMonth(t):
   1.876 +return time.localtime(t)[1] - 1
   1.877 +def DstHour(t):
   1.878 +return time.localtime(t - 1)[3] + 1
   1.879 +def DstSunday(t):
   1.880 +if time.localtime(t)[2] > 15:
   1.881 +return "'last'"
   1.882 +else:
   1.883 +return "'first'"
   1.884 +def DstMinutes(t):
   1.885 +return (time.localtime(t - 1)[4] + 1) % 60
   1.886 +attribs = { }
   1.887 +attribs['start_month'] = DstMonth(start)
   1.888 +attribs['end_month'] = DstMonth(end)
   1.889 +attribs['start_sunday'] = DstSunday(start)
   1.890 +attribs['end_sunday'] = DstSunday(end)
   1.891 +attribs['start_hour'] = DstHour(start)
   1.892 +attribs['end_hour'] = DstHour(end)
   1.893 +attribs['start_minutes'] = DstMinutes(start)
   1.894 +attribs['end_minutes'] = DstMinutes(end)
   1.895 +return attribs
   1.896 +
   1.897 +*********/
   1.898 +
   1.899 +//--Test case registration-----------------------------------------------------
   1.900 +function runTestCase(testcase) {
   1.901 +    if (testcase() !== true) {
   1.902 +        $ERROR("Test case returned non-true value!");
   1.903 +    }
   1.904 +}

mercurial