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 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 44 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 45 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
| 47 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 48 | // Helper function used to install functions on objects. |
| 49 | function InstallFunctions(object, attributes, functions) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 50 | if (functions.length >= 8) { |
| 51 | %OptimizeObjectForAddingMultipleProperties(object, functions.length >> 1); |
| 52 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 53 | for (var i = 0; i < functions.length; i += 2) { |
| 54 | var key = functions[i]; |
| 55 | var f = functions[i + 1]; |
| 56 | %FunctionSetName(f, key); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 57 | %FunctionRemovePrototype(f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 58 | %SetProperty(object, key, f, attributes); |
| 59 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 60 | %ToFastProperties(object); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 61 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 63 | // Emulates JSC by installing functions on a hidden prototype that |
| 64 | // lies above the current object/prototype. This lets you override |
| 65 | // functions on String.prototype etc. and then restore the old function |
| 66 | // with delete. See http://code.google.com/p/chromium/issues/detail?id=1717 |
| 67 | function InstallFunctionsOnHiddenPrototype(object, attributes, functions) { |
| 68 | var hidden_prototype = new $Object(); |
| 69 | %SetHiddenPrototype(object, hidden_prototype); |
| 70 | InstallFunctions(hidden_prototype, attributes, functions); |
| 71 | } |
| 72 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 74 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | // ECMA 262 - 15.1.4 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 78 | function GlobalIsNaN(number) { |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 79 | var n = ToNumber(number); |
| 80 | return NUMBER_IS_NAN(n); |
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.5 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 85 | function GlobalIsFinite(number) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 86 | if (!IS_NUMBER(number)) number = ToNumber(number); |
| 87 | |
| 88 | // NaN - NaN == NaN, Infinity - Infinity == NaN, -Infinity - -Infinity == NaN. |
| 89 | return %_IsSmi(number) || number - number == 0; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 90 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | // ECMA-262 - 15.1.2.2 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 94 | function GlobalParseInt(string, radix) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 95 | if (IS_UNDEFINED(radix)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | // Some people use parseInt instead of Math.floor. This |
| 97 | // optimization makes parseInt on a Smi 12 times faster (60ns |
| 98 | // vs 800ns). The following optimization makes parseInt on a |
| 99 | // non-Smi number 9 times faster (230ns vs 2070ns). Together |
| 100 | // they make parseInt on a string 1.4% slower (274ns vs 270ns). |
| 101 | if (%_IsSmi(string)) return string; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 102 | if (IS_NUMBER(string) && |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 103 | ((0.01 < string && string < 1e9) || |
| 104 | (-1e9 < string && string < -0.01))) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 105 | // Truncate number. |
| 106 | return string | 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 108 | radix = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | } else { |
| 110 | radix = TO_INT32(radix); |
| 111 | if (!(radix == 0 || (2 <= radix && radix <= 36))) |
| 112 | return $NaN; |
| 113 | } |
| 114 | return %StringParseInt(ToString(string), radix); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 115 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | // ECMA-262 - 15.1.2.3 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 119 | function GlobalParseFloat(string) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | return %StringParseFloat(ToString(string)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | function GlobalEval(x) { |
| 125 | if (!IS_STRING(x)) return x; |
| 126 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 127 | var global_receiver = %GlobalReceiver(global); |
| 128 | var this_is_global_receiver = (this === global_receiver); |
| 129 | var global_is_detached = (global === global_receiver); |
| 130 | |
| 131 | if (!this_is_global_receiver || global_is_detached) { |
| 132 | throw new $EvalError('The "this" object passed to eval must ' + |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 133 | 'be the global object from which eval originated'); |
| 134 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 135 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 136 | var f = %CompileString(x, false); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 137 | if (!IS_FUNCTION(f)) return f; |
| 138 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 139 | return f.call(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | // execScript for IE compatibility. |
| 144 | function GlobalExecScript(expr, lang) { |
| 145 | // NOTE: We don't care about the character casing. |
| 146 | if (!lang || /javascript/i.test(lang)) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 147 | var f = %CompileString(ToString(expr), false); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 148 | f.call(%GlobalReceiver(global)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 149 | } |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | // ---------------------------------------------------------------------------- |
| 155 | |
| 156 | |
| 157 | function SetupGlobal() { |
| 158 | // ECMA 262 - 15.1.1.1. |
| 159 | %SetProperty(global, "NaN", $NaN, DONT_ENUM | DONT_DELETE); |
| 160 | |
| 161 | // ECMA-262 - 15.1.1.2. |
| 162 | %SetProperty(global, "Infinity", 1/0, DONT_ENUM | DONT_DELETE); |
| 163 | |
| 164 | // ECMA-262 - 15.1.1.3. |
| 165 | %SetProperty(global, "undefined", void 0, DONT_ENUM | DONT_DELETE); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 166 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 167 | // Setup non-enumerable function on the global object. |
| 168 | InstallFunctions(global, DONT_ENUM, $Array( |
| 169 | "isNaN", GlobalIsNaN, |
| 170 | "isFinite", GlobalIsFinite, |
| 171 | "parseInt", GlobalParseInt, |
| 172 | "parseFloat", GlobalParseFloat, |
| 173 | "eval", GlobalEval, |
| 174 | "execScript", GlobalExecScript |
| 175 | )); |
| 176 | } |
| 177 | |
| 178 | SetupGlobal(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | // ---------------------------------------------------------------------------- |
| 182 | // Boolean (first part of definition) |
| 183 | |
| 184 | |
| 185 | %SetCode($Boolean, function(x) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 186 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | %_SetValueOf(this, ToBoolean(x)); |
| 188 | } else { |
| 189 | return ToBoolean(x); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | %FunctionSetPrototype($Boolean, new $Boolean(false)); |
| 194 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 195 | %SetProperty($Boolean.prototype, "constructor", $Boolean, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | |
| 197 | // ---------------------------------------------------------------------------- |
| 198 | // Object |
| 199 | |
| 200 | $Object.prototype.constructor = $Object; |
| 201 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 202 | // ECMA-262 - 15.2.4.2 |
| 203 | function ObjectToString() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 204 | return "[object " + %_ClassOf(ToObject(this)) + "]"; |
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.3 |
| 209 | function ObjectToLocaleString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 210 | return this.toString(); |
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.4 |
| 215 | function ObjectValueOf() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 216 | return ToObject(this); |
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.5 |
| 221 | function ObjectHasOwnProperty(V) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | return %HasLocalProperty(ToObject(this), ToString(V)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 223 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | |
| 225 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 226 | // ECMA-262 - 15.2.4.6 |
| 227 | function ObjectIsPrototypeOf(V) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 228 | if (!IS_SPEC_OBJECT_OR_NULL(V) && !IS_UNDETECTABLE(V)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | return %IsInPrototypeChain(this, V); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 230 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 231 | |
| 232 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 233 | // ECMA-262 - 15.2.4.6 |
| 234 | function ObjectPropertyIsEnumerable(V) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | if (this == null) return false; |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 236 | if (!IS_SPEC_OBJECT_OR_NULL(this)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | return %IsPropertyEnumerable(this, ToString(V)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 238 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | // Extensions for providing property getters and setters. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 242 | function ObjectDefineGetter(name, fun) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 243 | if (this == null && !IS_UNDETECTABLE(this)) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 244 | throw new $TypeError('Object.prototype.__defineGetter__: this is Null'); |
| 245 | } |
| 246 | if (!IS_FUNCTION(fun)) { |
| 247 | throw new $TypeError('Object.prototype.__defineGetter__: Expecting function'); |
| 248 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | return %DefineAccessor(ToObject(this), ToString(name), GETTER, fun); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | |
| 252 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 253 | function ObjectLookupGetter(name) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 254 | if (this == null && !IS_UNDETECTABLE(this)) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 255 | throw new $TypeError('Object.prototype.__lookupGetter__: this is Null'); |
| 256 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | return %LookupAccessor(ToObject(this), ToString(name), GETTER); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 258 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 259 | |
| 260 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 261 | function ObjectDefineSetter(name, fun) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 262 | if (this == null && !IS_UNDETECTABLE(this)) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 263 | throw new $TypeError('Object.prototype.__defineSetter__: this is Null'); |
| 264 | } |
| 265 | if (!IS_FUNCTION(fun)) { |
| 266 | throw new $TypeError( |
| 267 | 'Object.prototype.__defineSetter__: Expecting function'); |
| 268 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | return %DefineAccessor(ToObject(this), ToString(name), SETTER, fun); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 270 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | |
| 272 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 273 | function ObjectLookupSetter(name) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 274 | if (this == null && !IS_UNDETECTABLE(this)) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 275 | throw new $TypeError('Object.prototype.__lookupSetter__: this is Null'); |
| 276 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | return %LookupAccessor(ToObject(this), ToString(name), SETTER); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | |
| 280 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 281 | function ObjectKeys(obj) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 282 | if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) && |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 283 | !IS_UNDETECTABLE(obj)) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 284 | throw MakeTypeError("obj_ctor_property_non_object", ["keys"]); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 285 | return %LocalKeys(obj); |
| 286 | } |
| 287 | |
| 288 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 289 | // ES5 8.10.1. |
| 290 | function IsAccessorDescriptor(desc) { |
| 291 | if (IS_UNDEFINED(desc)) return false; |
| 292 | return desc.hasGetter_ || desc.hasSetter_; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | // ES5 8.10.2. |
| 297 | function IsDataDescriptor(desc) { |
| 298 | if (IS_UNDEFINED(desc)) return false; |
| 299 | return desc.hasValue_ || desc.hasWritable_; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | // ES5 8.10.3. |
| 304 | function IsGenericDescriptor(desc) { |
| 305 | return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc)); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | function IsInconsistentDescriptor(desc) { |
| 310 | return IsAccessorDescriptor(desc) && IsDataDescriptor(desc); |
| 311 | } |
| 312 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 313 | // ES5 8.10.4 |
| 314 | function FromPropertyDescriptor(desc) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 315 | if (IS_UNDEFINED(desc)) return desc; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 316 | var obj = new $Object(); |
| 317 | if (IsDataDescriptor(desc)) { |
| 318 | obj.value = desc.getValue(); |
| 319 | obj.writable = desc.isWritable(); |
| 320 | } |
| 321 | if (IsAccessorDescriptor(desc)) { |
| 322 | obj.get = desc.getGet(); |
| 323 | obj.set = desc.getSet(); |
| 324 | } |
| 325 | obj.enumerable = desc.isEnumerable(); |
| 326 | obj.configurable = desc.isConfigurable(); |
| 327 | return obj; |
| 328 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 329 | |
| 330 | // ES5 8.10.5. |
| 331 | function ToPropertyDescriptor(obj) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 332 | if (!IS_SPEC_OBJECT_OR_NULL(obj)) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 333 | throw MakeTypeError("property_desc_object", [obj]); |
| 334 | } |
| 335 | var desc = new PropertyDescriptor(); |
| 336 | |
| 337 | if ("enumerable" in obj) { |
| 338 | desc.setEnumerable(ToBoolean(obj.enumerable)); |
| 339 | } |
| 340 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 341 | if ("configurable" in obj) { |
| 342 | desc.setConfigurable(ToBoolean(obj.configurable)); |
| 343 | } |
| 344 | |
| 345 | if ("value" in obj) { |
| 346 | desc.setValue(obj.value); |
| 347 | } |
| 348 | |
| 349 | if ("writable" in obj) { |
| 350 | desc.setWritable(ToBoolean(obj.writable)); |
| 351 | } |
| 352 | |
| 353 | if ("get" in obj) { |
| 354 | var get = obj.get; |
| 355 | if (!IS_UNDEFINED(get) && !IS_FUNCTION(get)) { |
| 356 | throw MakeTypeError("getter_must_be_callable", [get]); |
| 357 | } |
| 358 | desc.setGet(get); |
| 359 | } |
| 360 | |
| 361 | if ("set" in obj) { |
| 362 | var set = obj.set; |
| 363 | if (!IS_UNDEFINED(set) && !IS_FUNCTION(set)) { |
| 364 | throw MakeTypeError("setter_must_be_callable", [set]); |
| 365 | } |
| 366 | desc.setSet(set); |
| 367 | } |
| 368 | |
| 369 | if (IsInconsistentDescriptor(desc)) { |
| 370 | throw MakeTypeError("value_and_accessor", [obj]); |
| 371 | } |
| 372 | return desc; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | function PropertyDescriptor() { |
| 377 | // Initialize here so they are all in-object and have the same map. |
| 378 | // Default values from ES5 8.6.1. |
| 379 | this.value_ = void 0; |
| 380 | this.hasValue_ = false; |
| 381 | this.writable_ = false; |
| 382 | this.hasWritable_ = false; |
| 383 | this.enumerable_ = false; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 384 | this.hasEnumerable_ = false; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 385 | this.configurable_ = false; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 386 | this.hasConfigurable_ = false; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 387 | this.get_ = void 0; |
| 388 | this.hasGetter_ = false; |
| 389 | this.set_ = void 0; |
| 390 | this.hasSetter_ = false; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | PropertyDescriptor.prototype.setValue = function(value) { |
| 395 | this.value_ = value; |
| 396 | this.hasValue_ = true; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | PropertyDescriptor.prototype.getValue = function() { |
| 401 | return this.value_; |
| 402 | } |
| 403 | |
| 404 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 405 | PropertyDescriptor.prototype.hasValue = function() { |
| 406 | return this.hasValue_; |
| 407 | } |
| 408 | |
| 409 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 410 | PropertyDescriptor.prototype.setEnumerable = function(enumerable) { |
| 411 | this.enumerable_ = enumerable; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 412 | this.hasEnumerable_ = true; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
| 416 | PropertyDescriptor.prototype.isEnumerable = function () { |
| 417 | return this.enumerable_; |
| 418 | } |
| 419 | |
| 420 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 421 | PropertyDescriptor.prototype.hasEnumerable = function() { |
| 422 | return this.hasEnumerable_; |
| 423 | } |
| 424 | |
| 425 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 426 | PropertyDescriptor.prototype.setWritable = function(writable) { |
| 427 | this.writable_ = writable; |
| 428 | this.hasWritable_ = true; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | PropertyDescriptor.prototype.isWritable = function() { |
| 433 | return this.writable_; |
| 434 | } |
| 435 | |
| 436 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 437 | PropertyDescriptor.prototype.hasWritable = function() { |
| 438 | return this.hasWritable_; |
| 439 | } |
| 440 | |
| 441 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 442 | PropertyDescriptor.prototype.setConfigurable = function(configurable) { |
| 443 | this.configurable_ = configurable; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 444 | this.hasConfigurable_ = true; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | PropertyDescriptor.prototype.hasConfigurable = function() { |
| 449 | return this.hasConfigurable_; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | |
| 453 | PropertyDescriptor.prototype.isConfigurable = function() { |
| 454 | return this.configurable_; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | PropertyDescriptor.prototype.setGet = function(get) { |
| 459 | this.get_ = get; |
| 460 | this.hasGetter_ = true; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | PropertyDescriptor.prototype.getGet = function() { |
| 465 | return this.get_; |
| 466 | } |
| 467 | |
| 468 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 469 | PropertyDescriptor.prototype.hasGetter = function() { |
| 470 | return this.hasGetter_; |
| 471 | } |
| 472 | |
| 473 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 474 | PropertyDescriptor.prototype.setSet = function(set) { |
| 475 | this.set_ = set; |
| 476 | this.hasSetter_ = true; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | PropertyDescriptor.prototype.getSet = function() { |
| 481 | return this.set_; |
| 482 | } |
| 483 | |
| 484 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 485 | PropertyDescriptor.prototype.hasSetter = function() { |
| 486 | return this.hasSetter_; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 491 | // ES5 section 8.12.1. |
| 492 | function GetOwnProperty(obj, p) { |
| 493 | var desc = new PropertyDescriptor(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 494 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame^] | 495 | // GetOwnProperty returns an array indexed by the constants |
| 496 | // defined in macros.py. |
| 497 | // If p is not a property on obj undefined is returned. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 498 | var props = %GetOwnProperty(ToObject(obj), ToString(p)); |
| 499 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 500 | if (IS_UNDEFINED(props)) return void 0; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 501 | |
| 502 | // This is an accessor |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame^] | 503 | if (props[IS_ACCESSOR_INDEX]) { |
| 504 | desc.setGet(props[GETTER_INDEX]); |
| 505 | desc.setSet(props[SETTER_INDEX]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 506 | } else { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame^] | 507 | desc.setValue(props[VALUE_INDEX]); |
| 508 | desc.setWritable(props[WRITABLE_INDEX]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 509 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame^] | 510 | desc.setEnumerable(props[ENUMERABLE_INDEX]); |
| 511 | desc.setConfigurable(props[CONFIGURABLE_INDEX]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 512 | |
| 513 | return desc; |
| 514 | } |
| 515 | |
| 516 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 517 | // ES5 section 8.12.2. |
| 518 | function GetProperty(obj, p) { |
| 519 | var prop = GetOwnProperty(obj); |
| 520 | if (!IS_UNDEFINED(prop)) return prop; |
| 521 | var proto = obj.__proto__; |
| 522 | if (IS_NULL(proto)) return void 0; |
| 523 | return GetProperty(proto, p); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | // ES5 section 8.12.6 |
| 528 | function HasProperty(obj, p) { |
| 529 | var desc = GetProperty(obj, p); |
| 530 | return IS_UNDEFINED(desc) ? false : true; |
| 531 | } |
| 532 | |
| 533 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 534 | // ES5 8.12.9. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 535 | function DefineOwnProperty(obj, p, desc, should_throw) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 536 | var current = GetOwnProperty(obj, p); |
| 537 | var extensible = %IsExtensible(ToObject(obj)); |
| 538 | |
| 539 | // Error handling according to spec. |
| 540 | // Step 3 |
| 541 | if (IS_UNDEFINED(current) && !extensible) |
| 542 | throw MakeTypeError("define_disallowed", ["defineProperty"]); |
| 543 | |
| 544 | if (!IS_UNDEFINED(current) && !current.isConfigurable()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 545 | // Step 5 and 6 |
| 546 | if ((!desc.hasEnumerable() || |
| 547 | SameValue(desc.isEnumerable() && current.isEnumerable())) && |
| 548 | (!desc.hasConfigurable() || |
| 549 | SameValue(desc.isConfigurable(), current.isConfigurable())) && |
| 550 | (!desc.hasWritable() || |
| 551 | SameValue(desc.isWritable(), current.isWritable())) && |
| 552 | (!desc.hasValue() || |
| 553 | SameValue(desc.getValue(), current.getValue())) && |
| 554 | (!desc.hasGetter() || |
| 555 | SameValue(desc.getGet(), current.getGet())) && |
| 556 | (!desc.hasSetter() || |
| 557 | SameValue(desc.getSet(), current.getSet()))) { |
| 558 | return true; |
| 559 | } |
| 560 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 561 | // Step 7 |
| 562 | if (desc.isConfigurable() || desc.isEnumerable() != current.isEnumerable()) |
| 563 | throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 564 | // Step 9 |
| 565 | if (IsDataDescriptor(current) != IsDataDescriptor(desc)) |
| 566 | throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 567 | // Step 10 |
| 568 | if (IsDataDescriptor(current) && IsDataDescriptor(desc)) { |
| 569 | if (!current.isWritable() && desc.isWritable()) |
| 570 | throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 571 | if (!current.isWritable() && desc.hasValue() && |
| 572 | !SameValue(desc.getValue(), current.getValue())) { |
| 573 | throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 574 | } |
| 575 | } |
| 576 | // Step 11 |
| 577 | if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) { |
| 578 | if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())){ |
| 579 | throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 580 | } |
| 581 | if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet())) |
| 582 | throw MakeTypeError("redefine_disallowed", ["defineProperty"]); |
| 583 | } |
| 584 | } |
| 585 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 586 | // Send flags - enumerable and configurable are common - writable is |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 587 | // only send to the data descriptor. |
| 588 | // Take special care if enumerable and configurable is not defined on |
| 589 | // desc (we need to preserve the existing values from current). |
| 590 | var flag = NONE; |
| 591 | if (desc.hasEnumerable()) { |
| 592 | flag |= desc.isEnumerable() ? 0 : DONT_ENUM; |
| 593 | } else if (!IS_UNDEFINED(current)) { |
| 594 | flag |= current.isEnumerable() ? 0 : DONT_ENUM; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 595 | } else { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 596 | flag |= DONT_ENUM; |
| 597 | } |
| 598 | |
| 599 | if (desc.hasConfigurable()) { |
| 600 | flag |= desc.isConfigurable() ? 0 : DONT_DELETE; |
| 601 | } else if (!IS_UNDEFINED(current)) { |
| 602 | flag |= current.isConfigurable() ? 0 : DONT_DELETE; |
| 603 | } else |
| 604 | flag |= DONT_DELETE; |
| 605 | |
| 606 | if (IsDataDescriptor(desc) || IsGenericDescriptor(desc)) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 607 | if (desc.hasWritable()) { |
| 608 | flag |= desc.isWritable() ? 0 : READ_ONLY; |
| 609 | } else if (!IS_UNDEFINED(current)) { |
| 610 | flag |= current.isWritable() ? 0 : READ_ONLY; |
| 611 | } else { |
| 612 | flag |= READ_ONLY; |
| 613 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 614 | %DefineOrRedefineDataProperty(obj, p, desc.getValue(), flag); |
| 615 | } else { |
| 616 | if (desc.hasGetter() && IS_FUNCTION(desc.getGet())) { |
| 617 | %DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag); |
| 618 | } |
| 619 | if (desc.hasSetter() && IS_FUNCTION(desc.getSet())) { |
| 620 | %DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag); |
| 621 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 622 | } |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 627 | // ES5 section 15.2.3.2. |
| 628 | function ObjectGetPrototypeOf(obj) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 629 | if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) && |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 630 | !IS_UNDETECTABLE(obj)) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 631 | throw MakeTypeError("obj_ctor_property_non_object", ["getPrototypeOf"]); |
| 632 | return obj.__proto__; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 636 | // ES5 section 15.2.3.3 |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 637 | function ObjectGetOwnPropertyDescriptor(obj, p) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 638 | if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) && |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 639 | !IS_UNDETECTABLE(obj)) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 640 | throw MakeTypeError("obj_ctor_property_non_object", ["getOwnPropertyDescriptor"]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 641 | var desc = GetOwnProperty(obj, p); |
| 642 | return FromPropertyDescriptor(desc); |
| 643 | } |
| 644 | |
| 645 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 646 | // ES5 section 15.2.3.4. |
| 647 | function ObjectGetOwnPropertyNames(obj) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 648 | if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) && |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 649 | !IS_UNDETECTABLE(obj)) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 650 | throw MakeTypeError("obj_ctor_property_non_object", ["getOwnPropertyNames"]); |
| 651 | |
| 652 | // Find all the indexed properties. |
| 653 | |
| 654 | // Get the local element names. |
| 655 | var propertyNames = %GetLocalElementNames(obj); |
| 656 | |
| 657 | // Get names for indexed interceptor properties. |
| 658 | if (%GetInterceptorInfo(obj) & 1) { |
| 659 | var indexedInterceptorNames = |
| 660 | %GetIndexedInterceptorElementNames(obj); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 661 | if (indexedInterceptorNames) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 662 | propertyNames = propertyNames.concat(indexedInterceptorNames); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // Find all the named properties. |
| 666 | |
| 667 | // Get the local property names. |
| 668 | propertyNames = propertyNames.concat(%GetLocalPropertyNames(obj)); |
| 669 | |
| 670 | // Get names for named interceptor properties if any. |
| 671 | |
| 672 | if (%GetInterceptorInfo(obj) & 2) { |
| 673 | var namedInterceptorNames = |
| 674 | %GetNamedInterceptorPropertyNames(obj); |
| 675 | if (namedInterceptorNames) { |
| 676 | propertyNames = propertyNames.concat(namedInterceptorNames); |
| 677 | } |
| 678 | } |
| 679 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 680 | // Property names are expected to be strings. |
| 681 | for (var i = 0; i < propertyNames.length; ++i) |
| 682 | propertyNames[i] = ToString(propertyNames[i]); |
| 683 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 684 | return propertyNames; |
| 685 | } |
| 686 | |
| 687 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 688 | // ES5 section 15.2.3.5. |
| 689 | function ObjectCreate(proto, properties) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 690 | if (!IS_SPEC_OBJECT_OR_NULL(proto)) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 691 | throw MakeTypeError("proto_object_or_null", [proto]); |
| 692 | } |
| 693 | var obj = new $Object(); |
| 694 | obj.__proto__ = proto; |
| 695 | if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties); |
| 696 | return obj; |
| 697 | } |
| 698 | |
| 699 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 700 | // ES5 section 15.2.3.6. |
| 701 | function ObjectDefineProperty(obj, p, attributes) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 702 | if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) && |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 703 | !IS_UNDETECTABLE(obj)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 704 | throw MakeTypeError("obj_ctor_property_non_object", ["defineProperty"]); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 705 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 706 | var name = ToString(p); |
| 707 | var desc = ToPropertyDescriptor(attributes); |
| 708 | DefineOwnProperty(obj, name, desc, true); |
| 709 | return obj; |
| 710 | } |
| 711 | |
| 712 | |
| 713 | // ES5 section 15.2.3.7. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 714 | function ObjectDefineProperties(obj, properties) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 715 | if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) && |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 716 | !IS_UNDETECTABLE(obj)) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 717 | throw MakeTypeError("obj_ctor_property_non_object", ["defineProperties"]); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 718 | var props = ToObject(properties); |
| 719 | var key_values = []; |
| 720 | for (var key in props) { |
| 721 | if (%HasLocalProperty(props, key)) { |
| 722 | key_values.push(key); |
| 723 | var value = props[key]; |
| 724 | var desc = ToPropertyDescriptor(value); |
| 725 | key_values.push(desc); |
| 726 | } |
| 727 | } |
| 728 | for (var i = 0; i < key_values.length; i += 2) { |
| 729 | var key = key_values[i]; |
| 730 | var desc = key_values[i + 1]; |
| 731 | DefineOwnProperty(obj, key, desc, true); |
| 732 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 733 | return obj; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | %SetCode($Object, function(x) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 738 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 739 | if (x == null) return this; |
| 740 | return ToObject(x); |
| 741 | } else { |
| 742 | if (x == null) return { }; |
| 743 | return ToObject(x); |
| 744 | } |
| 745 | }); |
| 746 | |
| 747 | |
| 748 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 749 | |
| 750 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 751 | function SetupObject() { |
| 752 | // Setup non-enumerable functions on the Object.prototype object. |
| 753 | InstallFunctions($Object.prototype, DONT_ENUM, $Array( |
| 754 | "toString", ObjectToString, |
| 755 | "toLocaleString", ObjectToLocaleString, |
| 756 | "valueOf", ObjectValueOf, |
| 757 | "hasOwnProperty", ObjectHasOwnProperty, |
| 758 | "isPrototypeOf", ObjectIsPrototypeOf, |
| 759 | "propertyIsEnumerable", ObjectPropertyIsEnumerable, |
| 760 | "__defineGetter__", ObjectDefineGetter, |
| 761 | "__lookupGetter__", ObjectLookupGetter, |
| 762 | "__defineSetter__", ObjectDefineSetter, |
| 763 | "__lookupSetter__", ObjectLookupSetter |
| 764 | )); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 765 | InstallFunctions($Object, DONT_ENUM, $Array( |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 766 | "keys", ObjectKeys, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 767 | "create", ObjectCreate, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 768 | "defineProperty", ObjectDefineProperty, |
| 769 | "defineProperties", ObjectDefineProperties, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 770 | "getPrototypeOf", ObjectGetPrototypeOf, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 771 | "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor, |
| 772 | "getOwnPropertyNames", ObjectGetOwnPropertyNames |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 773 | )); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | SetupObject(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 777 | |
| 778 | |
| 779 | // ---------------------------------------------------------------------------- |
| 780 | // Boolean |
| 781 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 782 | function BooleanToString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 783 | // NOTE: Both Boolean objects and values can enter here as |
| 784 | // 'this'. This is not as dictated by ECMA-262. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 785 | if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 786 | throw new $TypeError('Boolean.prototype.toString is not generic'); |
| 787 | return ToString(%_ValueOf(this)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 788 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 789 | |
| 790 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 791 | function BooleanValueOf() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 792 | // NOTE: Both Boolean objects and values can enter here as |
| 793 | // 'this'. This is not as dictated by ECMA-262. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 794 | if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 795 | throw new $TypeError('Boolean.prototype.valueOf is not generic'); |
| 796 | return %_ValueOf(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 797 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 798 | |
| 799 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 800 | function BooleanToJSON(key) { |
| 801 | return CheckJSONPrimitive(this.valueOf()); |
| 802 | } |
| 803 | |
| 804 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | // ---------------------------------------------------------------------------- |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 806 | |
| 807 | |
| 808 | function SetupBoolean() { |
| 809 | InstallFunctions($Boolean.prototype, DONT_ENUM, $Array( |
| 810 | "toString", BooleanToString, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 811 | "valueOf", BooleanValueOf, |
| 812 | "toJSON", BooleanToJSON |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 813 | )); |
| 814 | } |
| 815 | |
| 816 | SetupBoolean(); |
| 817 | |
| 818 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 819 | // Number |
| 820 | |
| 821 | // Set the Number function and constructor. |
| 822 | %SetCode($Number, function(x) { |
| 823 | var value = %_ArgumentsLength() == 0 ? 0 : ToNumber(x); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 824 | if (%_IsConstructCall()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 825 | %_SetValueOf(this, value); |
| 826 | } else { |
| 827 | return value; |
| 828 | } |
| 829 | }); |
| 830 | |
| 831 | %FunctionSetPrototype($Number, new $Number(0)); |
| 832 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 833 | // ECMA-262 section 15.7.4.2. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 834 | function NumberToString(radix) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 835 | // NOTE: Both Number objects and values can enter here as |
| 836 | // 'this'. This is not as dictated by ECMA-262. |
| 837 | var number = this; |
| 838 | if (!IS_NUMBER(this)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 839 | if (!IS_NUMBER_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | throw new $TypeError('Number.prototype.toString is not generic'); |
| 841 | // Get the value of this number in case it's an object. |
| 842 | number = %_ValueOf(this); |
| 843 | } |
| 844 | // Fast case: Convert number in radix 10. |
| 845 | if (IS_UNDEFINED(radix) || radix === 10) { |
| 846 | return ToString(number); |
| 847 | } |
| 848 | |
| 849 | // Convert the radix to an integer and check the range. |
| 850 | radix = TO_INTEGER(radix); |
| 851 | if (radix < 2 || radix > 36) { |
| 852 | throw new $RangeError('toString() radix argument must be between 2 and 36'); |
| 853 | } |
| 854 | // Convert the number to a string in the given radix. |
| 855 | return %NumberToRadixString(number, radix); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 856 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | |
| 858 | |
| 859 | // ECMA-262 section 15.7.4.3 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 860 | function NumberToLocaleString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 861 | return this.toString(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 862 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 863 | |
| 864 | |
| 865 | // ECMA-262 section 15.7.4.4 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 866 | function NumberValueOf() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 867 | // NOTE: Both Number objects and values can enter here as |
| 868 | // 'this'. This is not as dictated by ECMA-262. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 869 | if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 870 | throw new $TypeError('Number.prototype.valueOf is not generic'); |
| 871 | return %_ValueOf(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 872 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 873 | |
| 874 | |
| 875 | // ECMA-262 section 15.7.4.5 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 876 | function NumberToFixed(fractionDigits) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 877 | var f = TO_INTEGER(fractionDigits); |
| 878 | if (f < 0 || f > 20) { |
| 879 | throw new $RangeError("toFixed() digits argument must be between 0 and 20"); |
| 880 | } |
| 881 | var x = ToNumber(this); |
| 882 | return %NumberToFixed(x, f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 883 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | |
| 885 | |
| 886 | // ECMA-262 section 15.7.4.6 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 887 | function NumberToExponential(fractionDigits) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | var f = -1; |
| 889 | if (!IS_UNDEFINED(fractionDigits)) { |
| 890 | f = TO_INTEGER(fractionDigits); |
| 891 | if (f < 0 || f > 20) { |
| 892 | throw new $RangeError("toExponential() argument must be between 0 and 20"); |
| 893 | } |
| 894 | } |
| 895 | var x = ToNumber(this); |
| 896 | return %NumberToExponential(x, f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 897 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 898 | |
| 899 | |
| 900 | // ECMA-262 section 15.7.4.7 |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 901 | function NumberToPrecision(precision) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | if (IS_UNDEFINED(precision)) return ToString(%_ValueOf(this)); |
| 903 | var p = TO_INTEGER(precision); |
| 904 | if (p < 1 || p > 21) { |
| 905 | throw new $RangeError("toPrecision() argument must be between 1 and 21"); |
| 906 | } |
| 907 | var x = ToNumber(this); |
| 908 | return %NumberToPrecision(x, p); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 912 | function CheckJSONPrimitive(val) { |
| 913 | if (!IsPrimitive(val)) |
| 914 | throw MakeTypeError('result_not_primitive', ['toJSON', val]); |
| 915 | return val; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | function NumberToJSON(key) { |
| 920 | return CheckJSONPrimitive(this.valueOf()); |
| 921 | } |
| 922 | |
| 923 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 924 | // ---------------------------------------------------------------------------- |
| 925 | |
| 926 | function SetupNumber() { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 927 | %OptimizeObjectForAddingMultipleProperties($Number.prototype, 8); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 928 | // Setup the constructor property on the Number prototype object. |
| 929 | %SetProperty($Number.prototype, "constructor", $Number, DONT_ENUM); |
| 930 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 931 | %OptimizeObjectForAddingMultipleProperties($Number, 5); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 932 | // ECMA-262 section 15.7.3.1. |
| 933 | %SetProperty($Number, |
| 934 | "MAX_VALUE", |
| 935 | 1.7976931348623157e+308, |
| 936 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 937 | |
| 938 | // ECMA-262 section 15.7.3.2. |
| 939 | %SetProperty($Number, "MIN_VALUE", 5e-324, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 940 | |
| 941 | // ECMA-262 section 15.7.3.3. |
| 942 | %SetProperty($Number, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 943 | |
| 944 | // ECMA-262 section 15.7.3.4. |
| 945 | %SetProperty($Number, |
| 946 | "NEGATIVE_INFINITY", |
| 947 | -1/0, |
| 948 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 949 | |
| 950 | // ECMA-262 section 15.7.3.5. |
| 951 | %SetProperty($Number, |
| 952 | "POSITIVE_INFINITY", |
| 953 | 1/0, |
| 954 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 955 | %ToFastProperties($Number); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 956 | |
| 957 | // Setup non-enumerable functions on the Number prototype object. |
| 958 | InstallFunctions($Number.prototype, DONT_ENUM, $Array( |
| 959 | "toString", NumberToString, |
| 960 | "toLocaleString", NumberToLocaleString, |
| 961 | "valueOf", NumberValueOf, |
| 962 | "toFixed", NumberToFixed, |
| 963 | "toExponential", NumberToExponential, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 964 | "toPrecision", NumberToPrecision, |
| 965 | "toJSON", NumberToJSON |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 966 | )); |
| 967 | } |
| 968 | |
| 969 | SetupNumber(); |
| 970 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 971 | |
| 972 | |
| 973 | // ---------------------------------------------------------------------------- |
| 974 | // Function |
| 975 | |
| 976 | $Function.prototype.constructor = $Function; |
| 977 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 978 | function FunctionSourceString(func) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 979 | if (!IS_FUNCTION(func)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 980 | throw new $TypeError('Function.prototype.toString is not generic'); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 981 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 982 | |
| 983 | var source = %FunctionGetSourceCode(func); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 984 | if (!IS_STRING(source) || %FunctionIsBuiltin(func)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | var name = %FunctionGetName(func); |
| 986 | if (name) { |
| 987 | // Mimic what KJS does. |
| 988 | return 'function ' + name + '() { [native code] }'; |
| 989 | } else { |
| 990 | return 'function () { [native code] }'; |
| 991 | } |
| 992 | } |
| 993 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 994 | var name = %FunctionGetName(func); |
| 995 | return 'function ' + name + source; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 996 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | |
| 998 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 999 | function FunctionToString() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1000 | return FunctionSourceString(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1001 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1002 | |
| 1003 | |
| 1004 | function NewFunction(arg1) { // length == 1 |
| 1005 | var n = %_ArgumentsLength(); |
| 1006 | var p = ''; |
| 1007 | if (n > 1) { |
| 1008 | p = new $Array(n - 1); |
| 1009 | // Explicitly convert all parameters to strings. |
| 1010 | // Array.prototype.join replaces null with empty strings which is |
| 1011 | // not appropriate. |
| 1012 | for (var i = 0; i < n - 1; i++) p[i] = ToString(%_Arguments(i)); |
| 1013 | p = p.join(','); |
| 1014 | // If the formal parameters string include ) - an illegal |
| 1015 | // character - it may make the combined function expression |
| 1016 | // compile. We avoid this problem by checking for this early on. |
| 1017 | if (p.indexOf(')') != -1) throw MakeSyntaxError('unable_to_parse',[]); |
| 1018 | } |
| 1019 | var body = (n > 0) ? ToString(%_Arguments(n - 1)) : ''; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1020 | var source = '(function(' + p + ') {\n' + body + '\n})'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1021 | |
| 1022 | // The call to SetNewFunctionAttributes will ensure the prototype |
| 1023 | // 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] | 1024 | var f = %CompileString(source, false)(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1025 | %FunctionSetName(f, "anonymous"); |
| 1026 | return %SetNewFunctionAttributes(f); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1027 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1028 | |
| 1029 | %SetCode($Function, NewFunction); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1030 | |
| 1031 | // ---------------------------------------------------------------------------- |
| 1032 | |
| 1033 | function SetupFunction() { |
| 1034 | InstallFunctions($Function.prototype, DONT_ENUM, $Array( |
| 1035 | "toString", FunctionToString |
| 1036 | )); |
| 1037 | } |
| 1038 | |
| 1039 | SetupFunction(); |