// ECMA 262 - 15.9.1.2
function Day(time) {
- return FLOOR(time/msPerDay);
+ return FLOOR(time / msPerDay);
}
%SetCode($Date, function(year, month, date, hours, minutes, seconds, ms) {
- if (%_IsConstructCall()) {
- // ECMA 262 - 15.9.3
- var argc = %_ArgumentsLength();
- if (argc == 0) {
- %_SetValueOf(this, %DateCurrentTime());
- return;
- }
- if (argc == 1) {
+ if (!%_IsConstructCall()) {
+ // ECMA 262 - 15.9.2
+ return (new $Date()).toString();
+ }
+
+ // ECMA 262 - 15.9.3
+ var argc = %_ArgumentsLength();
+ var value;
+ if (argc == 0) {
+ value = %DateCurrentTime();
+
+ } else if (argc == 1) {
+ if (IS_NUMBER(year)) {
+ value = TimeClip(year);
+ } else {
// According to ECMA 262, no hint should be given for this
- // conversion. However, ToPrimitive defaults to String Hint
- // for Date objects which will lose precision when the Date
+ // conversion. However, ToPrimitive defaults to STRING_HINT for
+ // Date objects which will lose precision when the Date
// constructor is called with another Date object as its
- // argument. We therefore use Number Hint for the conversion
- // (which is the default for everything else than Date
- // objects). This makes us behave like KJS and SpiderMonkey.
+ // argument. We therefore use NUMBER_HINT for the conversion,
+ // which is the default for everything else than Date objects.
+ // This makes us behave like KJS and SpiderMonkey.
var time = ToPrimitive(year, NUMBER_HINT);
- if (IS_STRING(time)) {
- %_SetValueOf(this, DateParse(time));
- } else {
- %_SetValueOf(this, TimeClip(ToNumber(time)));
- }
- return;
+ value = IS_STRING(time) ? DateParse(time) : TimeClip(ToNumber(time));
}
+
+ } else {
year = ToNumber(year);
month = ToNumber(month);
date = argc > 2 ? ToNumber(date) : 1;
? 1900 + TO_INTEGER(year) : year;
var day = MakeDay(year, month, date);
var time = MakeTime(hours, minutes, seconds, ms);
- %_SetValueOf(this, TimeClip(UTC(MakeDate(day, time))));
- } else {
- // ECMA 262 - 15.9.2
- return (new $Date()).toString();
+ value = TimeClip(UTC(MakeDate(day, time)));
}
+ %_SetValueOf(this, value);
});
// ECMA 262 - 15.8.2.9
function MathFloor(x) {
if (!IS_NUMBER(x)) x = ToNumber(x);
- if (0 < x && x <= 0x7FFFFFFF) {
+ // It's more common to call this with a positive number that's out
+ // of range than negative numbers; check the upper bound first.
+ if (x <= 0x7FFFFFFF && x > 0) {
// Numbers in the range [0, 2^31) can be floored by converting
// them to an unsigned 32-bit value using the shift operator.
// We avoid doing so for -0, because the result of Math.floor(-0)
// Left operand (this) is already a string.
function STRING_ADD_LEFT(y) {
- if (!IS_STRING(y)) y = %ToString(%ToPrimitive(y, NO_HINT));
+ if (!IS_STRING(y)) {
+ if (IS_STRING_WRAPPER(y)) {
+ y = %_ValueOf(y);
+ } else {
+ y = IS_NUMBER(y)
+ ? %NumberToString(y)
+ : %ToString(%ToPrimitive(y, NO_HINT));
+ }
+ }
return %StringAdd(this, y);
}
// Right operand (y) is already a string.
function STRING_ADD_RIGHT(y) {
- var x = IS_STRING(this) ? this : %ToString(%ToPrimitive(this, NO_HINT));
+ var x = this;
+ if (!IS_STRING(x)) {
+ if (IS_STRING_WRAPPER(x)) {
+ x = %_ValueOf(x);
+ } else {
+ x = IS_NUMBER(x)
+ ? %NumberToString(x)
+ : %ToString(%ToPrimitive(x, NO_HINT));
+ }
+ }
return %StringAdd(x, y);
}