From 575438518c7b5f035be2f26d00a92de405be32d6 Mon Sep 17 00:00:00 2001 From: "yangguo@chromium.org" Date: Mon, 21 Oct 2013 09:16:31 +0000 Subject: [PATCH] Harmony: implement Math.sign. R=dslomov@chromium.org BUG=v8:2938 Review URL: https://codereview.chromium.org/28723002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@17279 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/bootstrapper.cc | 5 ++++ src/flag-definitions.h | 2 ++ src/harmony-math.js | 49 +++++++++++++++++++++++++++++++++++++++ test/mjsunit/harmony/math-sign.js | 48 ++++++++++++++++++++++++++++++++++++++ tools/gyp/v8.gyp | 1 + 5 files changed, 105 insertions(+) create mode 100644 src/harmony-math.js create mode 100644 test/mjsunit/harmony/math-sign.js diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc index c48385c..234a211 100644 --- a/src/bootstrapper.cc +++ b/src/bootstrapper.cc @@ -2067,6 +2067,11 @@ bool Genesis::InstallExperimentalNatives() { "native harmony-array.js") == 0) { if (!CompileExperimentalBuiltin(isolate(), i)) return false; } + if (FLAG_harmony_maths && + strcmp(ExperimentalNatives::GetScriptName(i).start(), + "native harmony-math.js") == 0) { + if (!CompileExperimentalBuiltin(isolate(), i)) return false; + } } InstallExperimentalNativeFunctions(); diff --git a/src/flag-definitions.h b/src/flag-definitions.h index e7180a0..45f0c72 100644 --- a/src/flag-definitions.h +++ b/src/flag-definitions.h @@ -182,6 +182,7 @@ DEFINE_bool(harmony_numeric_literals, false, "enable harmony numeric literals (0o77, 0b11)") DEFINE_bool(harmony_strings, false, "enable harmony string") DEFINE_bool(harmony_arrays, false, "enable harmony arrays") +DEFINE_bool(harmony_maths, false, "enable harmony math functions") DEFINE_bool(harmony, false, "enable all harmony features (except typeof)") DEFINE_implication(harmony, harmony_scoping) DEFINE_implication(harmony, harmony_modules) @@ -194,6 +195,7 @@ DEFINE_implication(harmony, harmony_iteration) DEFINE_implication(harmony, harmony_numeric_literals) DEFINE_implication(harmony, harmony_strings) DEFINE_implication(harmony, harmony_arrays) +DEFINE_implication(harmony, harmony_maths) DEFINE_implication(harmony_modules, harmony_scoping) DEFINE_implication(harmony_observation, harmony_collections) diff --git a/src/harmony-math.js b/src/harmony-math.js new file mode 100644 index 0000000..2e9b42a --- /dev/null +++ b/src/harmony-math.js @@ -0,0 +1,49 @@ +// 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. + +'use strict'; + +// ES6 draft 09-27-13, section 20.2.2.28. +function MathSign(x) { + if (!IS_NUMBER(x)) x = NonNumberToNumber(x); + if (x > 0) return 1; + if (x < 0) return -1; + 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 + )); +} + +ExtendMath(); diff --git a/test/mjsunit/harmony/math-sign.js b/test/mjsunit/harmony/math-sign.js new file mode 100644 index 0000000..8a89d62 --- /dev/null +++ b/test/mjsunit/harmony/math-sign.js @@ -0,0 +1,48 @@ +// 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.sign(0))); +assertEquals("-Infinity", String(1/Math.sign(-0))); +assertEquals(1, Math.sign(100)); +assertEquals(-1, Math.sign(-199)); +assertEquals(1, Math.sign(100.1)); +assertTrue(isNaN(Math.sign("abc"))); +assertTrue(isNaN(Math.sign({}))); +assertEquals(0, Math.sign([])); +assertEquals(1, Math.sign([1])); +assertEquals(-1, Math.sign([-100.1])); +assertTrue(isNaN(Math.sign([1, 1]))); +assertEquals(1, Math.sign({ toString: function() { return "100"; } })); +assertEquals(1, Math.sign({ toString: function() { return 100; } })); +assertEquals(-1, Math.sign({ valueOf: function() { return -1.1; } })); +assertEquals(-1, Math.sign({ valueOf: function() { return "-1.1"; } })); +assertEquals(-1, Math.sign(-Infinity)); +assertEquals(1, Math.sign(Infinity)); +assertEquals(-1, Math.sign("-Infinity")); +assertEquals(1, Math.sign("Infinity")); diff --git a/tools/gyp/v8.gyp b/tools/gyp/v8.gyp index 94b9bc0..22fcf94 100644 --- a/tools/gyp/v8.gyp +++ b/tools/gyp/v8.gyp @@ -936,6 +936,7 @@ '../../src/array-iterator.js', '../../src/harmony-string.js', '../../src/harmony-array.js', + '../../src/harmony-math.js' ], }, 'actions': [ -- 2.7.4