ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 28 | // This file relies on the fact that the following declarations have been made |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 29 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | // in runtime.js: |
| 31 | // const $Object = global.Object; |
| 32 | // const $Boolean = global.Boolean; |
| 33 | // const $Number = global.Number; |
| 34 | // const $Function = global.Function; |
| 35 | // const $Array = global.Array; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 36 | // const $NaN = 0/0; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 37 | // |
| 38 | // in math.js: |
| 39 | // const $floor = MathFloor |
| 40 | |
| 41 | const $isNaN = GlobalIsNaN; |
| 42 | const $isFinite = GlobalIsFinite; |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | |
| 46 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 47 | // Helper function used to install functions on objects. |
| 48 | function InstallFunctions(object, attributes, functions) { |
| 49 | for (var i = 0; i < functions.length; i += 2) { |
| 50 | var key = functions[i]; |
| 51 | var f = functions[i + 1]; |
| 52 | %FunctionSetName(f, key); |
| 53 | %SetProperty(object, key, f, attributes); |
| 54 | } |
| 55 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 57 | // Emulates JSC by installing functions on a hidden prototype that |
| 58 | // lies above the current object/prototype. This lets you override |
| 59 | // functions on String.prototype etc. and then restore the old function |
| 60 | // with delete. See http://code.google.com/p/chromium/issues/detail?id=1717 |
| 61 | function InstallFunctionsOnHiddenPrototype(object, attributes, functions) { |
| 62 | var hidden_prototype = new $Object(); |
| 63 | %SetHiddenPrototype(object, hidden_prototype); |
| 64 | InstallFunctions(hidden_prototype, attributes, functions); |
| 65 | } |
| 66 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 68 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | // ECMA 262 - 15.1.4 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 72 | function GlobalIsNaN(number) { |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 73 | var n = ToNumber(number); |
| 74 | return NUMBER_IS_NAN(n); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 75 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | // ECMA 262 - 15.1.5 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 79 | function GlobalIsFinite(number) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | return %NumberIsFinite(ToNumber(number)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 81 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | // ECMA-262 - 15.1.2.2 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 85 | function GlobalParseInt(string, radix) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 86 | if (radix === void 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | // Some people use parseInt instead of Math.floor. This |
| 88 | // optimization makes parseInt on a Smi 12 times faster (60ns |
| 89 | // vs 800ns). The following optimization makes parseInt on a |
| 90 | // non-Smi number 9 times faster (230ns vs 2070ns). Together |
| 91 | // they make parseInt on a string 1.4% slower (274ns vs 270ns). |
| 92 | if (%_IsSmi(string)) return string; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 93 | if (IS_NUMBER(string) && |
| 94 | ((string < -0.01 && -1e9 < string) || |
| 95 | (0.01 < string && string < 1e9))) { |
| 96 | // Truncate number. |
| 97 | return string | 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 99 | radix = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | } else { |
| 101 | radix = TO_INT32(radix); |
| 102 | if (!(radix == 0 || (2 <= radix && radix <= 36))) |
| 103 | return $NaN; |
| 104 | } |
| 105 | return %StringParseInt(ToString(string), radix); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 106 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | // ECMA-262 - 15.1.2.3 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 110 | function GlobalParseFloat(string) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | return %StringParseFloat(ToString(string)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | function GlobalEval(x) { |
| 116 | if (!IS_STRING(x)) return x; |
| 117 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 118 | var global_receiver = %GlobalReceiver(global); |
| 119 | var this_is_global_receiver = (this === global_receiver); |
| 120 | var global_is_detached = (global === global_receiver); |
| 121 | |
| 122 | if (!this_is_global_receiver || global_is_detached) { |
| 123 | throw new $EvalError('The "this" object passed to eval must ' + |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 124 | 'be the global object from which eval originated'); |
| 125 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 126 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 127 | var f = %CompileString(x, false); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 128 | if (!IS_FUNCTION(f)) return f; |
| 129 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 130 | return f.call(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
| 134 | // execScript for IE compatibility. |
| 135 | function GlobalExecScript(expr, lang) { |
| 136 | // NOTE: We don't care about the character casing. |
| 137 | if (!lang || /javascript/i.test(lang)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 138 | var f = %CompileString(ToString(expr), false); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 139 | f.call(%GlobalReceiver(global)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 140 | } |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | // ---------------------------------------------------------------------------- |
| 146 | |
| 147 | |
| 148 | function SetupGlobal() { |
| 149 | // ECMA 262 - 15.1.1.1. |
| 150 | %SetProperty(global, "NaN", $NaN, DONT_ENUM | DONT_DELETE); |
| 151 | |
| 152 | // ECMA-262 - 15.1.1.2. |
| 153 | %SetProperty(global, "Infinity", 1/0, DONT_ENUM | DONT_DELETE); |
| 154 | |
| 155 | // ECMA-262 - 15.1.1.3. |
| 156 | %SetProperty(global, "undefined", void 0, DONT_ENUM | DONT_DELETE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 157 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 158 | // Setup non-enumerable function on the global object. |
| 159 | InstallFunctions(global, DONT_ENUM, $Array( |
| 160 | "isNaN", GlobalIsNaN, |
| 161 | "isFinite", GlobalIsFinite, |
| 162 | "parseInt", GlobalParseInt, |
| 163 | "parseFloat", GlobalParseFloat, |
| 164 | "eval", GlobalEval, |
| 165 | "execScript", GlobalExecScript |
| 166 | )); |
| 167 | } |
| 168 | |
| 169 | SetupGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | // ---------------------------------------------------------------------------- |
| 173 | // Boolean (first part of definition) |
| 174 | |
| 175 | |
| 176 | %SetCode($Boolean, function(x) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 177 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | %_SetValueOf(this, ToBoolean(x)); |
| 179 | } else { |
| 180 | return ToBoolean(x); |
| 181 | } |
| 182 | }); |
| 183 | |
| 184 | %FunctionSetPrototype($Boolean, new $Boolean(false)); |
| 185 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 186 | %SetProperty($Boolean.prototype, "constructor", $Boolean, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | |
| 188 | // ---------------------------------------------------------------------------- |
| 189 | // Object |
| 190 | |
| 191 | $Object.prototype.constructor = $Object; |
| 192 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 193 | // ECMA-262 - 15.2.4.2 |
| 194 | function ObjectToString() { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 195 | var c = %_ClassOf(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | // Hide Arguments from the outside. |
| 197 | if (c === 'Arguments') c = 'Object'; |
| 198 | return "[object " + c + "]"; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 199 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | |
| 201 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 202 | // ECMA-262 - 15.2.4.3 |
| 203 | function ObjectToLocaleString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | return this.toString(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 205 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | |
| 207 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 208 | // ECMA-262 - 15.2.4.4 |
| 209 | function ObjectValueOf() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 210 | return this; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 211 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | |
| 213 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 214 | // ECMA-262 - 15.2.4.5 |
| 215 | function ObjectHasOwnProperty(V) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | return %HasLocalProperty(ToObject(this), ToString(V)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 217 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | |
| 219 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 220 | // ECMA-262 - 15.2.4.6 |
| 221 | function ObjectIsPrototypeOf(V) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | if (!IS_OBJECT(V) && !IS_FUNCTION(V)) return false; |
| 223 | return %IsInPrototypeChain(this, V); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 224 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | |
| 226 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 227 | // ECMA-262 - 15.2.4.6 |
| 228 | function ObjectPropertyIsEnumerable(V) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | if (this == null) return false; |
| 230 | if (!IS_OBJECT(this) && !IS_FUNCTION(this)) return false; |
| 231 | return %IsPropertyEnumerable(this, ToString(V)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 232 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | // Extensions for providing property getters and setters. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 236 | function ObjectDefineGetter(name, fun) { |
| 237 | if (this == null) { |
| 238 | throw new $TypeError('Object.prototype.__defineGetter__: this is Null'); |
| 239 | } |
| 240 | if (!IS_FUNCTION(fun)) { |
| 241 | throw new $TypeError('Object.prototype.__defineGetter__: Expecting function'); |
| 242 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | return %DefineAccessor(ToObject(this), ToString(name), GETTER, fun); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 244 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
| 246 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 247 | function ObjectLookupGetter(name) { |
| 248 | if (this == null) { |
| 249 | throw new $TypeError('Object.prototype.__lookupGetter__: this is Null'); |
| 250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | return %LookupAccessor(ToObject(this), ToString(name), GETTER); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 252 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | |
| 254 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 255 | function ObjectDefineSetter(name, fun) { |
| 256 | if (this == null) { |
| 257 | throw new $TypeError('Object.prototype.__defineSetter__: this is Null'); |
| 258 | } |
| 259 | if (!IS_FUNCTION(fun)) { |
| 260 | throw new $TypeError( |
| 261 | 'Object.prototype.__defineSetter__: Expecting function'); |
| 262 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 263 | return %DefineAccessor(ToObject(this), ToString(name), SETTER, fun); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 264 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | |
| 266 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 267 | function ObjectLookupSetter(name) { |
| 268 | if (this == null) { |
| 269 | throw new $TypeError('Object.prototype.__lookupSetter__: this is Null'); |
| 270 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | return %LookupAccessor(ToObject(this), ToString(name), SETTER); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 272 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | %SetCode($Object, function(x) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 276 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | if (x == null) return this; |
| 278 | return ToObject(x); |
| 279 | } else { |
| 280 | if (x == null) return { }; |
| 281 | return ToObject(x); |
| 282 | } |
| 283 | }); |
| 284 | |
| 285 | |
| 286 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | |
| 288 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 289 | function SetupObject() { |
| 290 | // Setup non-enumerable functions on the Object.prototype object. |
| 291 | InstallFunctions($Object.prototype, DONT_ENUM, $Array( |
| 292 | "toString", ObjectToString, |
| 293 | "toLocaleString", ObjectToLocaleString, |
| 294 | "valueOf", ObjectValueOf, |
| 295 | "hasOwnProperty", ObjectHasOwnProperty, |
| 296 | "isPrototypeOf", ObjectIsPrototypeOf, |
| 297 | "propertyIsEnumerable", ObjectPropertyIsEnumerable, |
| 298 | "__defineGetter__", ObjectDefineGetter, |
| 299 | "__lookupGetter__", ObjectLookupGetter, |
| 300 | "__defineSetter__", ObjectDefineSetter, |
| 301 | "__lookupSetter__", ObjectLookupSetter |
| 302 | )); |
| 303 | } |
| 304 | |
| 305 | SetupObject(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | // ---------------------------------------------------------------------------- |
| 309 | // Boolean |
| 310 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 311 | function BooleanToString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 312 | // NOTE: Both Boolean objects and values can enter here as |
| 313 | // 'this'. This is not as dictated by ECMA-262. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 314 | if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 315 | throw new $TypeError('Boolean.prototype.toString is not generic'); |
| 316 | return ToString(%_ValueOf(this)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | |
| 319 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 320 | function BooleanValueOf() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 321 | // NOTE: Both Boolean objects and values can enter here as |
| 322 | // 'this'. This is not as dictated by ECMA-262. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 323 | if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | throw new $TypeError('Boolean.prototype.valueOf is not generic'); |
| 325 | return %_ValueOf(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | |
| 328 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 329 | function BooleanToJSON(key) { |
| 330 | return CheckJSONPrimitive(this.valueOf()); |
| 331 | } |
| 332 | |
| 333 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 334 | // ---------------------------------------------------------------------------- |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 335 | |
| 336 | |
| 337 | function SetupBoolean() { |
| 338 | InstallFunctions($Boolean.prototype, DONT_ENUM, $Array( |
| 339 | "toString", BooleanToString, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 340 | "valueOf", BooleanValueOf, |
| 341 | "toJSON", BooleanToJSON |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 342 | )); |
| 343 | } |
| 344 | |
| 345 | SetupBoolean(); |
| 346 | |
| 347 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | // Number |
| 349 | |
| 350 | // Set the Number function and constructor. |
| 351 | %SetCode($Number, function(x) { |
| 352 | var value = %_ArgumentsLength() == 0 ? 0 : ToNumber(x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 353 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | %_SetValueOf(this, value); |
| 355 | } else { |
| 356 | return value; |
| 357 | } |
| 358 | }); |
| 359 | |
| 360 | %FunctionSetPrototype($Number, new $Number(0)); |
| 361 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 362 | // ECMA-262 section 15.7.4.2. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 363 | function NumberToString(radix) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | // NOTE: Both Number objects and values can enter here as |
| 365 | // 'this'. This is not as dictated by ECMA-262. |
| 366 | var number = this; |
| 367 | if (!IS_NUMBER(this)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 368 | if (!IS_NUMBER_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | throw new $TypeError('Number.prototype.toString is not generic'); |
| 370 | // Get the value of this number in case it's an object. |
| 371 | number = %_ValueOf(this); |
| 372 | } |
| 373 | // Fast case: Convert number in radix 10. |
| 374 | if (IS_UNDEFINED(radix) || radix === 10) { |
| 375 | return ToString(number); |
| 376 | } |
| 377 | |
| 378 | // Convert the radix to an integer and check the range. |
| 379 | radix = TO_INTEGER(radix); |
| 380 | if (radix < 2 || radix > 36) { |
| 381 | throw new $RangeError('toString() radix argument must be between 2 and 36'); |
| 382 | } |
| 383 | // Convert the number to a string in the given radix. |
| 384 | return %NumberToRadixString(number, radix); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 385 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | |
| 387 | |
| 388 | // ECMA-262 section 15.7.4.3 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 389 | function NumberToLocaleString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 390 | return this.toString(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 391 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 392 | |
| 393 | |
| 394 | // ECMA-262 section 15.7.4.4 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 395 | function NumberValueOf() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | // NOTE: Both Number objects and values can enter here as |
| 397 | // 'this'. This is not as dictated by ECMA-262. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 398 | if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | throw new $TypeError('Number.prototype.valueOf is not generic'); |
| 400 | return %_ValueOf(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 401 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | |
| 403 | |
| 404 | // ECMA-262 section 15.7.4.5 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 405 | function NumberToFixed(fractionDigits) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 406 | var f = TO_INTEGER(fractionDigits); |
| 407 | if (f < 0 || f > 20) { |
| 408 | throw new $RangeError("toFixed() digits argument must be between 0 and 20"); |
| 409 | } |
| 410 | var x = ToNumber(this); |
| 411 | return %NumberToFixed(x, f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 412 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | |
| 414 | |
| 415 | // ECMA-262 section 15.7.4.6 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 416 | function NumberToExponential(fractionDigits) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 417 | var f = -1; |
| 418 | if (!IS_UNDEFINED(fractionDigits)) { |
| 419 | f = TO_INTEGER(fractionDigits); |
| 420 | if (f < 0 || f > 20) { |
| 421 | throw new $RangeError("toExponential() argument must be between 0 and 20"); |
| 422 | } |
| 423 | } |
| 424 | var x = ToNumber(this); |
| 425 | return %NumberToExponential(x, f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 426 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 427 | |
| 428 | |
| 429 | // ECMA-262 section 15.7.4.7 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 430 | function NumberToPrecision(precision) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 431 | if (IS_UNDEFINED(precision)) return ToString(%_ValueOf(this)); |
| 432 | var p = TO_INTEGER(precision); |
| 433 | if (p < 1 || p > 21) { |
| 434 | throw new $RangeError("toPrecision() argument must be between 1 and 21"); |
| 435 | } |
| 436 | var x = ToNumber(this); |
| 437 | return %NumberToPrecision(x, p); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 441 | function CheckJSONPrimitive(val) { |
| 442 | if (!IsPrimitive(val)) |
| 443 | throw MakeTypeError('result_not_primitive', ['toJSON', val]); |
| 444 | return val; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | function NumberToJSON(key) { |
| 449 | return CheckJSONPrimitive(this.valueOf()); |
| 450 | } |
| 451 | |
| 452 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 453 | // ---------------------------------------------------------------------------- |
| 454 | |
| 455 | function SetupNumber() { |
| 456 | // Setup the constructor property on the Number prototype object. |
| 457 | %SetProperty($Number.prototype, "constructor", $Number, DONT_ENUM); |
| 458 | |
| 459 | // ECMA-262 section 15.7.3.1. |
| 460 | %SetProperty($Number, |
| 461 | "MAX_VALUE", |
| 462 | 1.7976931348623157e+308, |
| 463 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 464 | |
| 465 | // ECMA-262 section 15.7.3.2. |
| 466 | %SetProperty($Number, "MIN_VALUE", 5e-324, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 467 | |
| 468 | // ECMA-262 section 15.7.3.3. |
| 469 | %SetProperty($Number, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 470 | |
| 471 | // ECMA-262 section 15.7.3.4. |
| 472 | %SetProperty($Number, |
| 473 | "NEGATIVE_INFINITY", |
| 474 | -1/0, |
| 475 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 476 | |
| 477 | // ECMA-262 section 15.7.3.5. |
| 478 | %SetProperty($Number, |
| 479 | "POSITIVE_INFINITY", |
| 480 | 1/0, |
| 481 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 482 | |
| 483 | // Setup non-enumerable functions on the Number prototype object. |
| 484 | InstallFunctions($Number.prototype, DONT_ENUM, $Array( |
| 485 | "toString", NumberToString, |
| 486 | "toLocaleString", NumberToLocaleString, |
| 487 | "valueOf", NumberValueOf, |
| 488 | "toFixed", NumberToFixed, |
| 489 | "toExponential", NumberToExponential, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 490 | "toPrecision", NumberToPrecision, |
| 491 | "toJSON", NumberToJSON |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 492 | )); |
| 493 | } |
| 494 | |
| 495 | SetupNumber(); |
| 496 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | |
| 498 | |
| 499 | // ---------------------------------------------------------------------------- |
| 500 | // Function |
| 501 | |
| 502 | $Function.prototype.constructor = $Function; |
| 503 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | function FunctionSourceString(func) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 505 | if (!IS_FUNCTION(func)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | throw new $TypeError('Function.prototype.toString is not generic'); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame^] | 507 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 508 | |
| 509 | var source = %FunctionGetSourceCode(func); |
| 510 | if (!IS_STRING(source)) { |
| 511 | var name = %FunctionGetName(func); |
| 512 | if (name) { |
| 513 | // Mimic what KJS does. |
| 514 | return 'function ' + name + '() { [native code] }'; |
| 515 | } else { |
| 516 | return 'function () { [native code] }'; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Censor occurrences of internal calls. We do that for all |
| 521 | // functions and don't cache under the assumption that people rarly |
| 522 | // convert functions to strings. Note that we (apparently) can't |
| 523 | // use regular expression literals in natives files. |
| 524 | var regexp = ORIGINAL_REGEXP("%(\\w+\\()", "gm"); |
| 525 | if (source.match(regexp)) source = source.replace(regexp, "$1"); |
| 526 | var name = %FunctionGetName(func); |
| 527 | return 'function ' + name + source; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 528 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 529 | |
| 530 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 531 | function FunctionToString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | return FunctionSourceString(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 533 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 534 | |
| 535 | |
| 536 | function NewFunction(arg1) { // length == 1 |
| 537 | var n = %_ArgumentsLength(); |
| 538 | var p = ''; |
| 539 | if (n > 1) { |
| 540 | p = new $Array(n - 1); |
| 541 | // Explicitly convert all parameters to strings. |
| 542 | // Array.prototype.join replaces null with empty strings which is |
| 543 | // not appropriate. |
| 544 | for (var i = 0; i < n - 1; i++) p[i] = ToString(%_Arguments(i)); |
| 545 | p = p.join(','); |
| 546 | // If the formal parameters string include ) - an illegal |
| 547 | // character - it may make the combined function expression |
| 548 | // compile. We avoid this problem by checking for this early on. |
| 549 | if (p.indexOf(')') != -1) throw MakeSyntaxError('unable_to_parse',[]); |
| 550 | } |
| 551 | var body = (n > 0) ? ToString(%_Arguments(n - 1)) : ''; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 552 | var source = '(function(' + p + ') {\n' + body + '\n})'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | |
| 554 | // The call to SetNewFunctionAttributes will ensure the prototype |
| 555 | // property of the resulting function is enumerable (ECMA262, 15.3.5.2). |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 556 | var f = %CompileString(source, false)(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 557 | %FunctionSetName(f, "anonymous"); |
| 558 | return %SetNewFunctionAttributes(f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 559 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | |
| 561 | %SetCode($Function, NewFunction); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 562 | |
| 563 | // ---------------------------------------------------------------------------- |
| 564 | |
| 565 | function SetupFunction() { |
| 566 | InstallFunctions($Function.prototype, DONT_ENUM, $Array( |
| 567 | "toString", FunctionToString |
| 568 | )); |
| 569 | } |
| 570 | |
| 571 | SetupFunction(); |