Harmony: implement Math.trunc.
BUG=v8:2938 R=dslomov@chromium.org Review URL: https://codereview.chromium.org/28793002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17286 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
e14c323819
commit
2d6dab1f2e
@ -29,7 +29,7 @@
|
||||
|
||||
// ES6 draft 09-27-13, section 20.2.2.28.
|
||||
function MathSign(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
x = TO_NUMBER_INLINE(x);
|
||||
if (x > 0) return 1;
|
||||
if (x < 0) return -1;
|
||||
if (x === 0) return x;
|
||||
@ -37,12 +37,23 @@ function MathSign(x) {
|
||||
}
|
||||
|
||||
|
||||
// ES6 draft 09-27-13, section 20.2.2.34.
|
||||
function MathTrunc(x) {
|
||||
x = TO_NUMBER_INLINE(x);
|
||||
if (x > 0) return MathFloor(x);
|
||||
if (x < 0) return MathCeil(x);
|
||||
if (x === 0) return x;
|
||||
return NAN;
|
||||
}
|
||||
|
||||
|
||||
function ExtendMath() {
|
||||
%CheckIsBootstrapping();
|
||||
|
||||
// Set up the non-enumerable functions on the Math object.
|
||||
InstallFunctions($Math, DONT_ENUM, $Array(
|
||||
"sign", MathSign
|
||||
"sign", MathSign,
|
||||
"trunc", MathTrunc
|
||||
));
|
||||
}
|
||||
|
||||
|
57
src/math.js
57
src/math.js
@ -45,59 +45,51 @@ var $Math = new MathConstructor();
|
||||
// ECMA 262 - 15.8.2.1
|
||||
function MathAbs(x) {
|
||||
if (%_IsSmi(x)) return x >= 0 ? x : -x;
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
x = TO_NUMBER_INLINE(x);
|
||||
if (x === 0) return 0; // To handle -0.
|
||||
return x > 0 ? x : -x;
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.2
|
||||
function MathAcos(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %Math_acos(x);
|
||||
return %Math_acos(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.3
|
||||
function MathAsin(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %Math_asin(x);
|
||||
return %Math_asin(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.4
|
||||
function MathAtan(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %Math_atan(x);
|
||||
return %Math_atan(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.5
|
||||
// The naming of y and x matches the spec, as does the order in which
|
||||
// ToNumber (valueOf) is called.
|
||||
function MathAtan2(y, x) {
|
||||
if (!IS_NUMBER(y)) y = NonNumberToNumber(y);
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %Math_atan2(y, x);
|
||||
return %Math_atan2(TO_NUMBER_INLINE(y), TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.6
|
||||
function MathCeil(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %Math_ceil(x);
|
||||
return %Math_ceil(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.7
|
||||
function MathCos(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %_MathCos(x);
|
||||
return %_MathCos(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.8
|
||||
function MathExp(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %Math_exp(x);
|
||||
return %Math_exp(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.9
|
||||
function MathFloor(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
x = TO_NUMBER_INLINE(x);
|
||||
// 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 < 0x80000000 && x > 0) {
|
||||
@ -113,16 +105,15 @@ function MathFloor(x) {
|
||||
|
||||
// ECMA 262 - 15.8.2.10
|
||||
function MathLog(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %_MathLog(x);
|
||||
return %_MathLog(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.11
|
||||
function MathMax(arg1, arg2) { // length == 2
|
||||
var length = %_ArgumentsLength();
|
||||
if (length == 2) {
|
||||
if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1);
|
||||
if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2);
|
||||
arg1 = TO_NUMBER_INLINE(arg1);
|
||||
arg2 = TO_NUMBER_INLINE(arg2);
|
||||
if (arg2 > arg1) return arg2;
|
||||
if (arg1 > arg2) return arg1;
|
||||
if (arg1 == arg2) {
|
||||
@ -151,8 +142,8 @@ function MathMax(arg1, arg2) { // length == 2
|
||||
function MathMin(arg1, arg2) { // length == 2
|
||||
var length = %_ArgumentsLength();
|
||||
if (length == 2) {
|
||||
if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1);
|
||||
if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2);
|
||||
arg1 = TO_NUMBER_INLINE(arg1);
|
||||
arg2 = TO_NUMBER_INLINE(arg2);
|
||||
if (arg2 > arg1) return arg1;
|
||||
if (arg1 > arg2) return arg2;
|
||||
if (arg1 == arg2) {
|
||||
@ -179,9 +170,7 @@ function MathMin(arg1, arg2) { // length == 2
|
||||
|
||||
// ECMA 262 - 15.8.2.13
|
||||
function MathPow(x, y) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
if (!IS_NUMBER(y)) y = NonNumberToNumber(y);
|
||||
return %_MathPow(x, y);
|
||||
return %_MathPow(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.14
|
||||
@ -191,33 +180,27 @@ function MathRandom() {
|
||||
|
||||
// ECMA 262 - 15.8.2.15
|
||||
function MathRound(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %RoundNumber(x);
|
||||
return %RoundNumber(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.16
|
||||
function MathSin(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %_MathSin(x);
|
||||
return %_MathSin(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.17
|
||||
function MathSqrt(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %_MathSqrt(x);
|
||||
return %_MathSqrt(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// ECMA 262 - 15.8.2.18
|
||||
function MathTan(x) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
return %_MathTan(x);
|
||||
return %_MathTan(TO_NUMBER_INLINE(x));
|
||||
}
|
||||
|
||||
// Non-standard extension.
|
||||
function MathImul(x, y) {
|
||||
if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
|
||||
if (!IS_NUMBER(y)) y = NonNumberToNumber(y);
|
||||
return %NumberImul(x, y);
|
||||
return %NumberImul(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y));
|
||||
}
|
||||
|
||||
|
||||
|
51
test/mjsunit/harmony/math-trunc.js
Normal file
51
test/mjsunit/harmony/math-trunc.js
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2013 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following
|
||||
// disclaimer in the documentation and/or other materials provided
|
||||
// with the distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Flags: --harmony-maths
|
||||
|
||||
assertEquals("Infinity", String(1/Math.trunc(0)));
|
||||
assertEquals("-Infinity", String(1/Math.trunc(-0)));
|
||||
assertEquals("Infinity", String(1/Math.trunc(Math.PI/4)));
|
||||
assertEquals("-Infinity", String(1/Math.trunc(-Math.sqrt(2)/2)));
|
||||
assertEquals(100, Math.trunc(100));
|
||||
assertEquals(-199, Math.trunc(-199));
|
||||
assertEquals(100, Math.trunc(100.1));
|
||||
assertTrue(isNaN(Math.trunc("abc")));
|
||||
assertTrue(isNaN(Math.trunc({})));
|
||||
assertEquals(0, Math.trunc([]));
|
||||
assertEquals(1, Math.trunc([1]));
|
||||
assertEquals(-100, Math.trunc([-100.1]));
|
||||
assertTrue(isNaN(Math.trunc([1, 1])));
|
||||
assertEquals(-100, Math.trunc({ toString: function() { return "-100.3"; } }));
|
||||
assertEquals(10, Math.trunc({ toString: function() { return 10.1; } }));
|
||||
assertEquals(-1, Math.trunc({ valueOf: function() { return -1.1; } }));
|
||||
assertEquals("-Infinity",
|
||||
String(1/Math.trunc({ valueOf: function() { return "-0.1"; } })));
|
||||
assertEquals("-Infinity", String(Math.trunc(-Infinity)));
|
||||
assertEquals("Infinity", String(Math.trunc(Infinity)));
|
||||
assertEquals("-Infinity", String(Math.trunc("-Infinity")));
|
||||
assertEquals("Infinity", String(Math.trunc("Infinity")));
|
Loading…
Reference in New Issue
Block a user