Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / core / Number.as
bloba87dc7244910c5de34c317ec5d2c96de5f483577
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 package
43 // No instancegc, value is primitive.
45 [native(cls="NumberClass", classgc="exact", instance="double", methods="auto", construct="override")]
46 public final class Number
48 // Number.length = 1 per ES3
49 // E262 {ReadOnly, DontDelete, DontEnum }
50 public static const length:int = 1
52 // E262 {DontEnum, DontDelete, ReadOnly}
53 public static const NaN :Number = 0/0
54 public static const NEGATIVE_INFINITY :Number = -1/0
55 public static const POSITIVE_INFINITY :Number = 1/0
56 public static const MIN_VALUE :Number = _minValue() // typically: 4.9e-324
57 public static const MAX_VALUE :Number = 1.7976931348623158e+308
59 // these must match the same constants in MathUtils
60 private static const DTOSTR_FIXED:int = 1
61 private static const DTOSTR_PRECISION:int = 2
62 private static const DTOSTR_EXPONENTIAL:int = 3
64 private static native function _numberToString(n:Number, radix:int):String
65 private static native function _convert(n:Number, precision:int, mode:int):String
66 private static native function _minValue():Number
68 AS3 function toString(radix=10):String {
69 return _numberToString(this,radix)
71 AS3 function valueOf():Number { return this }
73 prototype.toLocaleString =
74 prototype.toString = function (radix=10):String
76 if (this === prototype) return "0"
78 if (!(this is Number))
79 Error.throwError( TypeError, 1004 /*kInvokeOnIncompatibleObjectError*/, "Number.prototype.toString" );
81 return _numberToString(this, radix)
84 prototype.valueOf = function()
86 if (this === prototype) return 0
87 if (!(this is Number))
88 Error.throwError( TypeError, 1004 /*kInvokeOnIncompatibleObjectError*/, "Number.prototype.valueOf" );
89 return this;
92 AS3 function toExponential(p=0):String
94 return _convert(this, int(p), DTOSTR_EXPONENTIAL)
96 prototype.toExponential = function(p=0):String
98 return _convert(Number(this), int(p), DTOSTR_EXPONENTIAL)
101 AS3 function toPrecision(p=0):String
103 if (p == undefined)
104 return this.toString();
106 return _convert(this, int(p), DTOSTR_PRECISION)
108 prototype.toPrecision = function(p=0):String
110 if (p == undefined)
111 return this.toString();
113 return _convert(Number(this), int(p), DTOSTR_PRECISION)
116 AS3 function toFixed(p=0):String
118 return _convert(this, int(p), DTOSTR_FIXED)
120 prototype.toFixed = function(p=0):String
122 return _convert(Number(this), int(p), DTOSTR_FIXED)
125 // Dummy constructor function - This is neccessary so the compiler can do arg # checking for the ctor in strict mode
126 // The code for the actual ctor is in NumberClass::construct in the avmplus
127 public function Number(value = 0)
130 _dontEnumPrototype(prototype);
133 // No instancegc, value is primitive.
135 [native(cls="IntClass", classgc="exact", instance="int32_t", methods="auto", construct="override")]
136 public final class int
138 // based on Number: E262 {ReadOnly, DontDelete, DontEnum}
139 public static const MIN_VALUE:int = -0x80000000;
140 public static const MAX_VALUE:int = 0x7fffffff;
142 // Number.length = 1 per ES3
143 // E262 {ReadOnly, DontDelete, DontEnum }
144 public static const length:int = 1
146 AS3 function toString(radix=10):String {
147 return Number(this).AS3::toString(radix)
149 AS3 function valueOf():int { return this }
151 prototype.toLocaleString =
152 prototype.toString = function toString(radix=10):String
154 if (this === prototype) return "0"
155 if (!(this is int))
156 Error.throwError( TypeError, 1004 /*kInvokeOnIncompatibleObjectError*/, "int.prototype.toString" );
157 return Number(this).toString(radix)
160 prototype.valueOf = function()
162 if (this === prototype) return 0
163 if (!(this is int))
164 Error.throwError( TypeError, 1004 /*kInvokeOnIncompatibleObjectError*/, "int.prototype.valueOf" );
165 return this
168 AS3 function toExponential(p=0):String
170 return Number(this).AS3::toExponential(p)
172 prototype.toExponential = function(p=0):String
174 return Number(this).AS3::toExponential(p)
177 AS3 function toPrecision(p=0):String
179 return Number(this).AS3::toPrecision(p)
181 prototype.toPrecision = function(p=0):String
183 return Number(this).AS3::toPrecision(p)
186 AS3 function toFixed(p=0):String
188 return Number(this).AS3::toFixed(p)
190 prototype.toFixed = function(p=0):String
192 return Number(this).AS3::toFixed(p)
195 // Dummy constructor function - This is neccessary so the compiler can do arg # checking for the ctor in strict mode
196 // The code for the actual ctor is in IntClass::construct in the avmplus
197 public function int(value = 0)
200 _dontEnumPrototype(prototype);
203 // No instancegc, value is primitive.
205 [native(cls="UIntClass", classgc="exact", instance="uint32_t", methods="auto", construct="override")]
206 public final class uint
208 // based on Number: E262 {ReadOnly, DontDelete, DontEnum}
209 public static const MIN_VALUE:uint = 0;
210 public static const MAX_VALUE:uint = 0xffffffff;
212 // Number.length = 1 per ES3
213 // E262 {ReadOnly, DontDelete, DontEnum}
214 public static const length:int = 1
216 AS3 function toString(radix=10):String {
217 return Number(this).AS3::toString(radix)
219 AS3 function valueOf():uint { return this }
221 prototype.toLocaleString =
222 prototype.toString = function toString(radix=10):String
224 if (this === prototype) return "0"
225 if (!(this is Number))
226 Error.throwError( TypeError, 1004 /*kInvokeOnIncompatibleObjectError*/, "uint.prototype.toString" );
227 return Number(this).toString(radix)
230 prototype.valueOf = function()
232 if (this === prototype) return 0
233 if (!(this is uint))
234 Error.throwError( TypeError, 1004 /*kInvokeOnIncompatibleObjectError*/, "uint.prototype.valueOf" );
235 return this
238 AS3 function toExponential(p=0):String
240 return Number(this).AS3::toExponential(p)
242 prototype.toExponential = function(p=0):String
244 return Number(this).AS3::toExponential(p)
247 AS3 function toPrecision(p=0):String
249 return Number(this).AS3::toPrecision(p)
251 prototype.toPrecision = function(p=0):String
253 return Number(this).AS3::toPrecision(p)
256 AS3 function toFixed(p=0):String
258 return Number(this).AS3::toFixed(p)
260 prototype.toFixed = function(p=0):String
262 return Number(this).AS3::toFixed(p)
265 // Dummy constructor function - This is neccessary so the compiler can do arg # checking for the ctor in strict mode
266 // The code for the actual ctor is in UIntClass::construct in the avmplus
267 public function uint(value = 0)
270 _dontEnumPrototype(prototype);