Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
| 28 | // This file relies on the fact that the following declarations have been made |
| 29 | // |
| 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; |
| 36 | // const $NaN = 0/0; |
| 37 | // |
| 38 | // in math.js: |
| 39 | // const $floor = MathFloor |
| 40 | |
| 41 | const $isNaN = GlobalIsNaN; |
| 42 | const $isFinite = GlobalIsFinite; |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | |
| 47 | // Helper function used to install functions on objects. |
| 48 | function InstallFunctions(object, attributes, functions) { |
| 49 | if (functions.length >= 8) { |
| 50 | %OptimizeObjectForAddingMultipleProperties(object, functions.length >> 1); |
| 51 | } |
| 52 | for (var i = 0; i < functions.length; i += 2) { |
| 53 | var key = functions[i]; |
| 54 | var f = functions[i + 1]; |
| 55 | %FunctionSetName(f, key); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 56 | %FunctionRemovePrototype(f); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | %SetProperty(object, key, f, attributes); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 58 | %SetNativeFlag(f); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 60 | %ToFastProperties(object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 63 | // Prevents changes to the prototype of a built-infunction. |
| 64 | // The "prototype" property of the function object is made non-configurable, |
| 65 | // and the prototype object is made non-extensible. The latter prevents |
| 66 | // changing the __proto__ property. |
| 67 | function SetUpLockedPrototype(constructor, fields, methods) { |
| 68 | %CheckIsBootstrapping(); |
| 69 | var prototype = constructor.prototype; |
| 70 | // Install functions first, because this function is used to initialize |
| 71 | // PropertyDescriptor itself. |
| 72 | var property_count = (methods.length >> 1) + (fields ? fields.length : 0); |
| 73 | if (property_count >= 4) { |
| 74 | %OptimizeObjectForAddingMultipleProperties(prototype, property_count); |
| 75 | } |
| 76 | if (fields) { |
| 77 | for (var i = 0; i < fields.length; i++) { |
| 78 | %SetProperty(prototype, fields[i], void 0, DONT_ENUM | DONT_DELETE); |
| 79 | } |
| 80 | } |
| 81 | for (var i = 0; i < methods.length; i += 2) { |
| 82 | var key = methods[i]; |
| 83 | var f = methods[i + 1]; |
| 84 | %SetProperty(prototype, key, f, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 85 | %SetNativeFlag(f); |
| 86 | } |
| 87 | prototype.__proto__ = null; |
| 88 | %ToFastProperties(prototype); |
| 89 | } |
| 90 | |
| 91 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | // ---------------------------------------------------------------------------- |
| 93 | |
| 94 | |
| 95 | // ECMA 262 - 15.1.4 |
| 96 | function GlobalIsNaN(number) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 97 | if (!IS_NUMBER(number)) number = NonNumberToNumber(number); |
| 98 | return NUMBER_IS_NAN(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | // ECMA 262 - 15.1.5 |
| 103 | function GlobalIsFinite(number) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 104 | if (!IS_NUMBER(number)) number = NonNumberToNumber(number); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 105 | return NUMBER_IS_FINITE(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | // ECMA-262 - 15.1.2.2 |
| 110 | function GlobalParseInt(string, radix) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 111 | if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | // Some people use parseInt instead of Math.floor. This |
| 113 | // optimization makes parseInt on a Smi 12 times faster (60ns |
| 114 | // vs 800ns). The following optimization makes parseInt on a |
| 115 | // non-Smi number 9 times faster (230ns vs 2070ns). Together |
| 116 | // they make parseInt on a string 1.4% slower (274ns vs 270ns). |
| 117 | if (%_IsSmi(string)) return string; |
| 118 | if (IS_NUMBER(string) && |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 119 | ((0.01 < string && string < 1e9) || |
| 120 | (-1e9 < string && string < -0.01))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | // Truncate number. |
| 122 | return string | 0; |
| 123 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 124 | string = TO_STRING_INLINE(string); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 125 | radix = radix | 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 126 | } else { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 127 | // The spec says ToString should be evaluated before ToInt32. |
| 128 | string = TO_STRING_INLINE(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | radix = TO_INT32(radix); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 130 | if (!(radix == 0 || (2 <= radix && radix <= 36))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 131 | return $NaN; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 132 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 134 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 135 | if (%_HasCachedArrayIndex(string) && |
| 136 | (radix == 0 || radix == 10)) { |
| 137 | return %_GetCachedArrayIndex(string); |
| 138 | } |
| 139 | return %StringParseInt(string, radix); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | // ECMA-262 - 15.1.2.3 |
| 144 | function GlobalParseFloat(string) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 145 | string = TO_STRING_INLINE(string); |
| 146 | if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string); |
| 147 | return %StringParseFloat(string); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | function GlobalEval(x) { |
| 152 | if (!IS_STRING(x)) return x; |
| 153 | |
| 154 | var global_receiver = %GlobalReceiver(global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | var global_is_detached = (global === global_receiver); |
| 156 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 157 | // For consistency with JSC we require the global object passed to |
| 158 | // eval to be the global object from which 'eval' originated. This |
| 159 | // is not mandated by the spec. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 160 | // We only throw if the global has been detached, since we need the |
| 161 | // receiver as this-value for the call. |
| 162 | if (global_is_detached) { |
| 163 | throw new $EvalError('The "this" value passed to eval must ' + |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | 'be the global object from which eval originated'); |
| 165 | } |
| 166 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 167 | var f = %CompileString(x); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | if (!IS_FUNCTION(f)) return f; |
| 169 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 170 | return %_CallFunction(global_receiver, f); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | // ---------------------------------------------------------------------------- |
| 175 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 176 | // Set up global object. |
| 177 | function SetUpGlobal() { |
| 178 | %CheckIsBootstrapping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | // ECMA 262 - 15.1.1.1. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 180 | %SetProperty(global, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | |
| 182 | // ECMA-262 - 15.1.1.2. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 183 | %SetProperty(global, "Infinity", 1/0, DONT_ENUM | DONT_DELETE | READ_ONLY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | |
| 185 | // ECMA-262 - 15.1.1.3. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 186 | %SetProperty(global, "undefined", void 0, |
| 187 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 189 | // Set up non-enumerable function on the global object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | InstallFunctions(global, DONT_ENUM, $Array( |
| 191 | "isNaN", GlobalIsNaN, |
| 192 | "isFinite", GlobalIsFinite, |
| 193 | "parseInt", GlobalParseInt, |
| 194 | "parseFloat", GlobalParseFloat, |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 195 | "eval", GlobalEval |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | )); |
| 197 | } |
| 198 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 199 | SetUpGlobal(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | |
| 201 | // ---------------------------------------------------------------------------- |
| 202 | // Boolean (first part of definition) |
| 203 | |
| 204 | |
| 205 | %SetCode($Boolean, function(x) { |
| 206 | if (%_IsConstructCall()) { |
| 207 | %_SetValueOf(this, ToBoolean(x)); |
| 208 | } else { |
| 209 | return ToBoolean(x); |
| 210 | } |
| 211 | }); |
| 212 | |
| 213 | %FunctionSetPrototype($Boolean, new $Boolean(false)); |
| 214 | |
| 215 | %SetProperty($Boolean.prototype, "constructor", $Boolean, DONT_ENUM); |
| 216 | |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | // Object |
| 219 | |
| 220 | $Object.prototype.constructor = $Object; |
| 221 | |
| 222 | // ECMA-262 - 15.2.4.2 |
| 223 | function ObjectToString() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 224 | if (IS_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 225 | return '[object Undefined]'; |
| 226 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 227 | if (IS_NULL(this)) return '[object Null]'; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 228 | return "[object " + %_ClassOf(ToObject(this)) + "]"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | // ECMA-262 - 15.2.4.3 |
| 233 | function ObjectToLocaleString() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 234 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 235 | throw MakeTypeError("called_on_null_or_undefined", |
| 236 | ["Object.prototype.toLocaleString"]); |
| 237 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | return this.toString(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | // ECMA-262 - 15.2.4.4 |
| 243 | function ObjectValueOf() { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 244 | return ToObject(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
| 248 | // ECMA-262 - 15.2.4.5 |
| 249 | function ObjectHasOwnProperty(V) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 250 | if (%IsJSProxy(this)) { |
| 251 | var handler = %GetHandler(this); |
| 252 | return CallTrap1(handler, "hasOwn", DerivedHasOwnTrap, TO_STRING_INLINE(V)); |
| 253 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 254 | return %HasLocalProperty(TO_OBJECT_INLINE(this), TO_STRING_INLINE(V)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | // ECMA-262 - 15.2.4.6 |
| 259 | function ObjectIsPrototypeOf(V) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 260 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 261 | throw MakeTypeError("called_on_null_or_undefined", |
| 262 | ["Object.prototype.isPrototypeOf"]); |
| 263 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 264 | if (!IS_SPEC_OBJECT(V)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | return %IsInPrototypeChain(this, V); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | // ECMA-262 - 15.2.4.6 |
| 270 | function ObjectPropertyIsEnumerable(V) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 271 | var P = ToString(V); |
| 272 | if (%IsJSProxy(this)) { |
| 273 | var desc = GetOwnProperty(this, P); |
| 274 | return IS_UNDEFINED(desc) ? false : desc.isEnumerable(); |
| 275 | } |
| 276 | return %IsPropertyEnumerable(ToObject(this), P); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
| 280 | // Extensions for providing property getters and setters. |
| 281 | function ObjectDefineGetter(name, fun) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 282 | var receiver = this; |
| 283 | if (receiver == null && !IS_UNDETECTABLE(receiver)) { |
| 284 | receiver = %GlobalReceiver(global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 286 | if (!IS_SPEC_FUNCTION(fun)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 287 | throw new $TypeError( |
| 288 | 'Object.prototype.__defineGetter__: Expecting function'); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 290 | var desc = new PropertyDescriptor(); |
| 291 | desc.setGet(fun); |
| 292 | desc.setEnumerable(true); |
| 293 | desc.setConfigurable(true); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 294 | DefineOwnProperty(ToObject(receiver), ToString(name), desc, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
| 298 | function ObjectLookupGetter(name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 299 | var receiver = this; |
| 300 | if (receiver == null && !IS_UNDETECTABLE(receiver)) { |
| 301 | receiver = %GlobalReceiver(global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 303 | return %LookupAccessor(ToObject(receiver), ToString(name), GETTER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | function ObjectDefineSetter(name, fun) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 308 | var receiver = this; |
| 309 | if (receiver == null && !IS_UNDETECTABLE(receiver)) { |
| 310 | receiver = %GlobalReceiver(global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 312 | if (!IS_SPEC_FUNCTION(fun)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | throw new $TypeError( |
| 314 | 'Object.prototype.__defineSetter__: Expecting function'); |
| 315 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 316 | var desc = new PropertyDescriptor(); |
| 317 | desc.setSet(fun); |
| 318 | desc.setEnumerable(true); |
| 319 | desc.setConfigurable(true); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 320 | DefineOwnProperty(ToObject(receiver), ToString(name), desc, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | |
| 324 | function ObjectLookupSetter(name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 325 | var receiver = this; |
| 326 | if (receiver == null && !IS_UNDETECTABLE(receiver)) { |
| 327 | receiver = %GlobalReceiver(global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 329 | return %LookupAccessor(ToObject(receiver), ToString(name), SETTER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | function ObjectKeys(obj) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 334 | if (!IS_SPEC_OBJECT(obj)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 335 | throw MakeTypeError("obj_ctor_property_non_object", ["keys"]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 336 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 337 | if (%IsJSProxy(obj)) { |
| 338 | var handler = %GetHandler(obj); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 339 | var names = CallTrap0(handler, "keys", DerivedKeysTrap); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 340 | return ToStringArray(names); |
| 341 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | return %LocalKeys(obj); |
| 343 | } |
| 344 | |
| 345 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 346 | // ES5 8.10.1. |
| 347 | function IsAccessorDescriptor(desc) { |
| 348 | if (IS_UNDEFINED(desc)) return false; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 349 | return desc.hasGetter() || desc.hasSetter(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | // ES5 8.10.2. |
| 354 | function IsDataDescriptor(desc) { |
| 355 | if (IS_UNDEFINED(desc)) return false; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 356 | return desc.hasValue() || desc.hasWritable(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
| 360 | // ES5 8.10.3. |
| 361 | function IsGenericDescriptor(desc) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 362 | if (IS_UNDEFINED(desc)) return false; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 363 | return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc)); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | function IsInconsistentDescriptor(desc) { |
| 368 | return IsAccessorDescriptor(desc) && IsDataDescriptor(desc); |
| 369 | } |
| 370 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 371 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 372 | // ES5 8.10.4 |
| 373 | function FromPropertyDescriptor(desc) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 374 | if (IS_UNDEFINED(desc)) return desc; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 375 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 376 | if (IsDataDescriptor(desc)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 377 | return { value: desc.getValue(), |
| 378 | writable: desc.isWritable(), |
| 379 | enumerable: desc.isEnumerable(), |
| 380 | configurable: desc.isConfigurable() }; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 381 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 382 | // Must be an AccessorDescriptor then. We never return a generic descriptor. |
| 383 | return { get: desc.getGet(), |
| 384 | set: desc.getSet(), |
| 385 | enumerable: desc.isEnumerable(), |
| 386 | configurable: desc.isConfigurable() }; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 389 | |
| 390 | // Harmony Proxies |
| 391 | function FromGenericPropertyDescriptor(desc) { |
| 392 | if (IS_UNDEFINED(desc)) return desc; |
| 393 | var obj = new $Object(); |
| 394 | |
| 395 | if (desc.hasValue()) { |
| 396 | %IgnoreAttributesAndSetProperty(obj, "value", desc.getValue(), NONE); |
| 397 | } |
| 398 | if (desc.hasWritable()) { |
| 399 | %IgnoreAttributesAndSetProperty(obj, "writable", desc.isWritable(), NONE); |
| 400 | } |
| 401 | if (desc.hasGetter()) { |
| 402 | %IgnoreAttributesAndSetProperty(obj, "get", desc.getGet(), NONE); |
| 403 | } |
| 404 | if (desc.hasSetter()) { |
| 405 | %IgnoreAttributesAndSetProperty(obj, "set", desc.getSet(), NONE); |
| 406 | } |
| 407 | if (desc.hasEnumerable()) { |
| 408 | %IgnoreAttributesAndSetProperty(obj, "enumerable", |
| 409 | desc.isEnumerable(), NONE); |
| 410 | } |
| 411 | if (desc.hasConfigurable()) { |
| 412 | %IgnoreAttributesAndSetProperty(obj, "configurable", |
| 413 | desc.isConfigurable(), NONE); |
| 414 | } |
| 415 | return obj; |
| 416 | } |
| 417 | |
| 418 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 419 | // ES5 8.10.5. |
| 420 | function ToPropertyDescriptor(obj) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 421 | if (!IS_SPEC_OBJECT(obj)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 422 | throw MakeTypeError("property_desc_object", [obj]); |
| 423 | } |
| 424 | var desc = new PropertyDescriptor(); |
| 425 | |
| 426 | if ("enumerable" in obj) { |
| 427 | desc.setEnumerable(ToBoolean(obj.enumerable)); |
| 428 | } |
| 429 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 430 | if ("configurable" in obj) { |
| 431 | desc.setConfigurable(ToBoolean(obj.configurable)); |
| 432 | } |
| 433 | |
| 434 | if ("value" in obj) { |
| 435 | desc.setValue(obj.value); |
| 436 | } |
| 437 | |
| 438 | if ("writable" in obj) { |
| 439 | desc.setWritable(ToBoolean(obj.writable)); |
| 440 | } |
| 441 | |
| 442 | if ("get" in obj) { |
| 443 | var get = obj.get; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 444 | if (!IS_UNDEFINED(get) && !IS_SPEC_FUNCTION(get)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 445 | throw MakeTypeError("getter_must_be_callable", [get]); |
| 446 | } |
| 447 | desc.setGet(get); |
| 448 | } |
| 449 | |
| 450 | if ("set" in obj) { |
| 451 | var set = obj.set; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 452 | if (!IS_UNDEFINED(set) && !IS_SPEC_FUNCTION(set)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 453 | throw MakeTypeError("setter_must_be_callable", [set]); |
| 454 | } |
| 455 | desc.setSet(set); |
| 456 | } |
| 457 | |
| 458 | if (IsInconsistentDescriptor(desc)) { |
| 459 | throw MakeTypeError("value_and_accessor", [obj]); |
| 460 | } |
| 461 | return desc; |
| 462 | } |
| 463 | |
| 464 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 465 | // For Harmony proxies. |
| 466 | function ToCompletePropertyDescriptor(obj) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 467 | var desc = ToPropertyDescriptor(obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 468 | if (IsGenericDescriptor(desc) || IsDataDescriptor(desc)) { |
| 469 | if (!desc.hasValue()) desc.setValue(void 0); |
| 470 | if (!desc.hasWritable()) desc.setWritable(false); |
| 471 | } else { |
| 472 | // Is accessor descriptor. |
| 473 | if (!desc.hasGetter()) desc.setGet(void 0); |
| 474 | if (!desc.hasSetter()) desc.setSet(void 0); |
| 475 | } |
| 476 | if (!desc.hasEnumerable()) desc.setEnumerable(false); |
| 477 | if (!desc.hasConfigurable()) desc.setConfigurable(false); |
| 478 | return desc; |
| 479 | } |
| 480 | |
| 481 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 482 | function PropertyDescriptor() { |
| 483 | // Initialize here so they are all in-object and have the same map. |
| 484 | // Default values from ES5 8.6.1. |
| 485 | this.value_ = void 0; |
| 486 | this.hasValue_ = false; |
| 487 | this.writable_ = false; |
| 488 | this.hasWritable_ = false; |
| 489 | this.enumerable_ = false; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 490 | this.hasEnumerable_ = false; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 491 | this.configurable_ = false; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 492 | this.hasConfigurable_ = false; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 493 | this.get_ = void 0; |
| 494 | this.hasGetter_ = false; |
| 495 | this.set_ = void 0; |
| 496 | this.hasSetter_ = false; |
| 497 | } |
| 498 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 499 | SetUpLockedPrototype(PropertyDescriptor, $Array( |
| 500 | "value_", |
| 501 | "hasValue_", |
| 502 | "writable_", |
| 503 | "hasWritable_", |
| 504 | "enumerable_", |
| 505 | "hasEnumerable_", |
| 506 | "configurable_", |
| 507 | "hasConfigurable_", |
| 508 | "get_", |
| 509 | "hasGetter_", |
| 510 | "set_", |
| 511 | "hasSetter_" |
| 512 | ), $Array( |
| 513 | "toString", function() { |
| 514 | return "[object PropertyDescriptor]"; |
| 515 | }, |
| 516 | "setValue", function(value) { |
| 517 | this.value_ = value; |
| 518 | this.hasValue_ = true; |
| 519 | }, |
| 520 | "getValue", function() { |
| 521 | return this.value_; |
| 522 | }, |
| 523 | "hasValue", function() { |
| 524 | return this.hasValue_; |
| 525 | }, |
| 526 | "setEnumerable", function(enumerable) { |
| 527 | this.enumerable_ = enumerable; |
| 528 | this.hasEnumerable_ = true; |
| 529 | }, |
| 530 | "isEnumerable", function () { |
| 531 | return this.enumerable_; |
| 532 | }, |
| 533 | "hasEnumerable", function() { |
| 534 | return this.hasEnumerable_; |
| 535 | }, |
| 536 | "setWritable", function(writable) { |
| 537 | this.writable_ = writable; |
| 538 | this.hasWritable_ = true; |
| 539 | }, |
| 540 | "isWritable", function() { |
| 541 | return this.writable_; |
| 542 | }, |
| 543 | "hasWritable", function() { |
| 544 | return this.hasWritable_; |
| 545 | }, |
| 546 | "setConfigurable", function(configurable) { |
| 547 | this.configurable_ = configurable; |
| 548 | this.hasConfigurable_ = true; |
| 549 | }, |
| 550 | "hasConfigurable", function() { |
| 551 | return this.hasConfigurable_; |
| 552 | }, |
| 553 | "isConfigurable", function() { |
| 554 | return this.configurable_; |
| 555 | }, |
| 556 | "setGet", function(get) { |
| 557 | this.get_ = get; |
| 558 | this.hasGetter_ = true; |
| 559 | }, |
| 560 | "getGet", function() { |
| 561 | return this.get_; |
| 562 | }, |
| 563 | "hasGetter", function() { |
| 564 | return this.hasGetter_; |
| 565 | }, |
| 566 | "setSet", function(set) { |
| 567 | this.set_ = set; |
| 568 | this.hasSetter_ = true; |
| 569 | }, |
| 570 | "getSet", function() { |
| 571 | return this.set_; |
| 572 | }, |
| 573 | "hasSetter", function() { |
| 574 | return this.hasSetter_; |
| 575 | })); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 576 | |
| 577 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 578 | // Converts an array returned from Runtime_GetOwnProperty to an actual |
| 579 | // property descriptor. For a description of the array layout please |
| 580 | // see the runtime.cc file. |
| 581 | function ConvertDescriptorArrayToDescriptor(desc_array) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 582 | if (desc_array === false) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 583 | throw 'Internal error: invalid desc_array'; |
Steve Block | 103cc40 | 2011-02-16 13:27:44 +0000 | [diff] [blame] | 584 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 585 | |
| 586 | if (IS_UNDEFINED(desc_array)) { |
| 587 | return void 0; |
| 588 | } |
| 589 | |
| 590 | var desc = new PropertyDescriptor(); |
| 591 | // This is an accessor. |
| 592 | if (desc_array[IS_ACCESSOR_INDEX]) { |
| 593 | desc.setGet(desc_array[GETTER_INDEX]); |
| 594 | desc.setSet(desc_array[SETTER_INDEX]); |
| 595 | } else { |
| 596 | desc.setValue(desc_array[VALUE_INDEX]); |
| 597 | desc.setWritable(desc_array[WRITABLE_INDEX]); |
| 598 | } |
| 599 | desc.setEnumerable(desc_array[ENUMERABLE_INDEX]); |
| 600 | desc.setConfigurable(desc_array[CONFIGURABLE_INDEX]); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 601 | |
| 602 | return desc; |
| 603 | } |
| 604 | |
| 605 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 606 | // For Harmony proxies. |
| 607 | function GetTrap(handler, name, defaultTrap) { |
| 608 | var trap = handler[name]; |
| 609 | if (IS_UNDEFINED(trap)) { |
| 610 | if (IS_UNDEFINED(defaultTrap)) { |
| 611 | throw MakeTypeError("handler_trap_missing", [handler, name]); |
| 612 | } |
| 613 | trap = defaultTrap; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 614 | } else if (!IS_SPEC_FUNCTION(trap)) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 615 | throw MakeTypeError("handler_trap_must_be_callable", [handler, name]); |
| 616 | } |
| 617 | return trap; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | function CallTrap0(handler, name, defaultTrap) { |
| 622 | return %_CallFunction(handler, GetTrap(handler, name, defaultTrap)); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | function CallTrap1(handler, name, defaultTrap, x) { |
| 627 | return %_CallFunction(handler, x, GetTrap(handler, name, defaultTrap)); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | function CallTrap2(handler, name, defaultTrap, x, y) { |
| 632 | return %_CallFunction(handler, x, y, GetTrap(handler, name, defaultTrap)); |
| 633 | } |
| 634 | |
| 635 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 636 | // ES5 section 8.12.1. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 637 | function GetOwnProperty(obj, v) { |
| 638 | var p = ToString(v); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 639 | if (%IsJSProxy(obj)) { |
| 640 | var handler = %GetHandler(obj); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 641 | var descriptor = CallTrap1(handler, "getOwnPropertyDescriptor", void 0, p); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 642 | if (IS_UNDEFINED(descriptor)) return descriptor; |
| 643 | var desc = ToCompletePropertyDescriptor(descriptor); |
| 644 | if (!desc.isConfigurable()) { |
| 645 | throw MakeTypeError("proxy_prop_not_configurable", |
| 646 | [handler, "getOwnPropertyDescriptor", p, descriptor]); |
| 647 | } |
| 648 | return desc; |
| 649 | } |
| 650 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 651 | // GetOwnProperty returns an array indexed by the constants |
| 652 | // defined in macros.py. |
| 653 | // If p is not a property on obj undefined is returned. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 654 | var props = %GetOwnProperty(ToObject(obj), ToString(v)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 655 | |
| 656 | // A false value here means that access checks failed. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 657 | if (props === false) return void 0; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 658 | |
| 659 | return ConvertDescriptorArrayToDescriptor(props); |
| 660 | } |
| 661 | |
| 662 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 663 | // Harmony proxies. |
| 664 | function DefineProxyProperty(obj, p, attributes, should_throw) { |
| 665 | var handler = %GetHandler(obj); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 666 | var result = CallTrap2(handler, "defineProperty", void 0, p, attributes); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 667 | if (!ToBoolean(result)) { |
| 668 | if (should_throw) { |
| 669 | throw MakeTypeError("handler_returned_false", |
| 670 | [handler, "defineProperty"]); |
| 671 | } else { |
| 672 | return false; |
| 673 | } |
| 674 | } |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 679 | // ES5 8.12.9. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 680 | function DefineOwnProperty(obj, p, desc, should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 681 | if (%IsJSProxy(obj)) { |
| 682 | var attributes = FromGenericPropertyDescriptor(desc); |
| 683 | return DefineProxyProperty(obj, p, attributes, should_throw); |
| 684 | } |
| 685 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 686 | var current_or_access = %GetOwnProperty(ToObject(obj), ToString(p)); |
| 687 | // A false value here means that access checks failed. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 688 | if (current_or_access === false) return void 0; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 689 | |
| 690 | var current = ConvertDescriptorArrayToDescriptor(current_or_access); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 691 | var extensible = %IsExtensible(ToObject(obj)); |
| 692 | |
| 693 | // Error handling according to spec. |
| 694 | // Step 3 |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 695 | if (IS_UNDEFINED(current) && !extensible) { |
| 696 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 697 | throw MakeTypeError("define_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 698 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 699 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 700 | } |
| 701 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 702 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 703 | if (!IS_UNDEFINED(current)) { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 704 | // Step 5 and 6 |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 705 | if ((IsGenericDescriptor(desc) || |
| 706 | IsDataDescriptor(desc) == IsDataDescriptor(current)) && |
| 707 | (!desc.hasEnumerable() || |
| 708 | SameValue(desc.isEnumerable(), current.isEnumerable())) && |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 709 | (!desc.hasConfigurable() || |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 710 | SameValue(desc.isConfigurable(), current.isConfigurable())) && |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 711 | (!desc.hasWritable() || |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 712 | SameValue(desc.isWritable(), current.isWritable())) && |
| 713 | (!desc.hasValue() || |
| 714 | SameValue(desc.getValue(), current.getValue())) && |
| 715 | (!desc.hasGetter() || |
| 716 | SameValue(desc.getGet(), current.getGet())) && |
| 717 | (!desc.hasSetter() || |
| 718 | SameValue(desc.getSet(), current.getSet()))) { |
| 719 | return true; |
| 720 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 721 | if (!current.isConfigurable()) { |
| 722 | // Step 7 |
| 723 | if (desc.isConfigurable() || |
| 724 | (desc.hasEnumerable() && |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 725 | desc.isEnumerable() != current.isEnumerable())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 726 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 727 | throw MakeTypeError("redefine_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 728 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 729 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 730 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 731 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 732 | // Step 8 |
| 733 | if (!IsGenericDescriptor(desc)) { |
| 734 | // Step 9a |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 735 | if (IsDataDescriptor(current) != IsDataDescriptor(desc)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 736 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 737 | throw MakeTypeError("redefine_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 738 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 739 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 740 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 741 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 742 | // Step 10a |
| 743 | if (IsDataDescriptor(current) && IsDataDescriptor(desc)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 744 | if (!current.isWritable() && desc.isWritable()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 745 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 746 | throw MakeTypeError("redefine_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 747 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 748 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 749 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 750 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 751 | if (!current.isWritable() && desc.hasValue() && |
| 752 | !SameValue(desc.getValue(), current.getValue())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 753 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 754 | throw MakeTypeError("redefine_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 755 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 756 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 757 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | // Step 11 |
| 761 | if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 762 | if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 763 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 764 | throw MakeTypeError("redefine_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 765 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 766 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 767 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 768 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 769 | if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 770 | if (should_throw) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 771 | throw MakeTypeError("redefine_disallowed", [p]); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 772 | } else { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 773 | return false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 774 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 775 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 776 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 781 | // Send flags - enumerable and configurable are common - writable is |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 782 | // only send to the data descriptor. |
| 783 | // Take special care if enumerable and configurable is not defined on |
| 784 | // desc (we need to preserve the existing values from current). |
| 785 | var flag = NONE; |
| 786 | if (desc.hasEnumerable()) { |
| 787 | flag |= desc.isEnumerable() ? 0 : DONT_ENUM; |
| 788 | } else if (!IS_UNDEFINED(current)) { |
| 789 | flag |= current.isEnumerable() ? 0 : DONT_ENUM; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 790 | } else { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 791 | flag |= DONT_ENUM; |
| 792 | } |
| 793 | |
| 794 | if (desc.hasConfigurable()) { |
| 795 | flag |= desc.isConfigurable() ? 0 : DONT_DELETE; |
| 796 | } else if (!IS_UNDEFINED(current)) { |
| 797 | flag |= current.isConfigurable() ? 0 : DONT_DELETE; |
| 798 | } else |
| 799 | flag |= DONT_DELETE; |
| 800 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 801 | if (IsDataDescriptor(desc) || |
| 802 | (IsGenericDescriptor(desc) && |
| 803 | (IS_UNDEFINED(current) || IsDataDescriptor(current)))) { |
| 804 | // There are 3 cases that lead here: |
| 805 | // Step 4a - defining a new data property. |
| 806 | // Steps 9b & 12 - replacing an existing accessor property with a data |
| 807 | // property. |
| 808 | // Step 12 - updating an existing data property with a data or generic |
| 809 | // descriptor. |
| 810 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 811 | if (desc.hasWritable()) { |
| 812 | flag |= desc.isWritable() ? 0 : READ_ONLY; |
| 813 | } else if (!IS_UNDEFINED(current)) { |
| 814 | flag |= current.isWritable() ? 0 : READ_ONLY; |
| 815 | } else { |
| 816 | flag |= READ_ONLY; |
| 817 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 818 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 819 | var value = void 0; // Default value is undefined. |
| 820 | if (desc.hasValue()) { |
| 821 | value = desc.getValue(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 822 | } else if (!IS_UNDEFINED(current) && IsDataDescriptor(current)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 823 | value = current.getValue(); |
| 824 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 825 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 826 | %DefineOrRedefineDataProperty(obj, p, value, flag); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 827 | } else if (IsGenericDescriptor(desc)) { |
| 828 | // Step 12 - updating an existing accessor property with generic |
| 829 | // descriptor. Changing flags only. |
| 830 | %DefineOrRedefineAccessorProperty(obj, p, GETTER, current.getGet(), flag); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 831 | } else { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 832 | // There are 3 cases that lead here: |
| 833 | // Step 4b - defining a new accessor property. |
| 834 | // Steps 9c & 12 - replacing an existing data property with an accessor |
| 835 | // property. |
| 836 | // Step 12 - updating an existing accessor property with an accessor |
| 837 | // descriptor. |
| 838 | if (desc.hasGetter()) { |
| 839 | %DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 840 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 841 | if (desc.hasSetter()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 842 | %DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag); |
| 843 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 844 | } |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | // ES5 section 15.2.3.2. |
| 850 | function ObjectGetPrototypeOf(obj) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 851 | if (!IS_SPEC_OBJECT(obj)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 852 | throw MakeTypeError("obj_ctor_property_non_object", ["getPrototypeOf"]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 853 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 854 | return %GetPrototype(obj); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 858 | // ES5 section 15.2.3.3 |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 859 | function ObjectGetOwnPropertyDescriptor(obj, p) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 860 | if (!IS_SPEC_OBJECT(obj)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 861 | throw MakeTypeError("obj_ctor_property_non_object", |
| 862 | ["getOwnPropertyDescriptor"]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 863 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 864 | var desc = GetOwnProperty(obj, p); |
| 865 | return FromPropertyDescriptor(desc); |
| 866 | } |
| 867 | |
| 868 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 869 | // For Harmony proxies |
| 870 | function ToStringArray(obj, trap) { |
| 871 | if (!IS_SPEC_OBJECT(obj)) { |
| 872 | throw MakeTypeError("proxy_non_object_prop_names", [obj, trap]); |
| 873 | } |
| 874 | var n = ToUint32(obj.length); |
| 875 | var array = new $Array(n); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 876 | var names = {}; // TODO(rossberg): use sets once they are ready. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 877 | for (var index = 0; index < n; index++) { |
| 878 | var s = ToString(obj[index]); |
| 879 | if (s in names) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 880 | throw MakeTypeError("proxy_repeated_prop_name", [obj, trap, s]); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 881 | } |
| 882 | array[index] = s; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 883 | names[s] = 0; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 884 | } |
| 885 | return array; |
| 886 | } |
| 887 | |
| 888 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 889 | // ES5 section 15.2.3.4. |
| 890 | function ObjectGetOwnPropertyNames(obj) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 891 | if (!IS_SPEC_OBJECT(obj)) { |
| 892 | throw MakeTypeError("obj_ctor_property_non_object", |
| 893 | ["getOwnPropertyNames"]); |
| 894 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 895 | // Special handling for proxies. |
| 896 | if (%IsJSProxy(obj)) { |
| 897 | var handler = %GetHandler(obj); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 898 | var names = CallTrap0(handler, "getOwnPropertyNames", void 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 899 | return ToStringArray(names, "getOwnPropertyNames"); |
| 900 | } |
| 901 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 902 | // Find all the indexed properties. |
| 903 | |
| 904 | // Get the local element names. |
| 905 | var propertyNames = %GetLocalElementNames(obj); |
| 906 | |
| 907 | // Get names for indexed interceptor properties. |
| 908 | if (%GetInterceptorInfo(obj) & 1) { |
| 909 | var indexedInterceptorNames = |
| 910 | %GetIndexedInterceptorElementNames(obj); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 911 | if (indexedInterceptorNames) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 912 | propertyNames = propertyNames.concat(indexedInterceptorNames); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 913 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Find all the named properties. |
| 917 | |
| 918 | // Get the local property names. |
| 919 | propertyNames = propertyNames.concat(%GetLocalPropertyNames(obj)); |
| 920 | |
| 921 | // Get names for named interceptor properties if any. |
| 922 | |
| 923 | if (%GetInterceptorInfo(obj) & 2) { |
| 924 | var namedInterceptorNames = |
| 925 | %GetNamedInterceptorPropertyNames(obj); |
| 926 | if (namedInterceptorNames) { |
| 927 | propertyNames = propertyNames.concat(namedInterceptorNames); |
| 928 | } |
| 929 | } |
| 930 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 931 | // Property names are expected to be unique strings. |
| 932 | var propertySet = {}; |
| 933 | var j = 0; |
| 934 | for (var i = 0; i < propertyNames.length; ++i) { |
| 935 | var name = ToString(propertyNames[i]); |
| 936 | // We need to check for the exact property value since for intrinsic |
| 937 | // properties like toString if(propertySet["toString"]) will always |
| 938 | // succeed. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 939 | if (propertySet[name] === true) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 940 | continue; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 941 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 942 | propertySet[name] = true; |
| 943 | propertyNames[j++] = name; |
| 944 | } |
| 945 | propertyNames.length = j; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 946 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 947 | return propertyNames; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | // ES5 section 15.2.3.5. |
| 952 | function ObjectCreate(proto, properties) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 953 | if (!IS_SPEC_OBJECT(proto) && proto !== null) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 954 | throw MakeTypeError("proto_object_or_null", [proto]); |
| 955 | } |
| 956 | var obj = new $Object(); |
| 957 | obj.__proto__ = proto; |
| 958 | if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties); |
| 959 | return obj; |
| 960 | } |
| 961 | |
| 962 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 963 | // ES5 section 15.2.3.6. |
| 964 | function ObjectDefineProperty(obj, p, attributes) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 965 | if (!IS_SPEC_OBJECT(obj)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 966 | throw MakeTypeError("obj_ctor_property_non_object", ["defineProperty"]); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 967 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 968 | var name = ToString(p); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 969 | if (%IsJSProxy(obj)) { |
| 970 | // Clone the attributes object for protection. |
| 971 | // TODO(rossberg): not spec'ed yet, so not sure if this should involve |
| 972 | // non-own properties as it does (or non-enumerable ones, as it doesn't?). |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 973 | var attributesClone = {}; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 974 | for (var a in attributes) { |
| 975 | attributesClone[a] = attributes[a]; |
| 976 | } |
| 977 | DefineProxyProperty(obj, name, attributesClone, true); |
| 978 | // The following would implement the spec as in the current proposal, |
| 979 | // but after recent comments on es-discuss, is most likely obsolete. |
| 980 | /* |
| 981 | var defineObj = FromGenericPropertyDescriptor(desc); |
| 982 | var names = ObjectGetOwnPropertyNames(attributes); |
| 983 | var standardNames = |
| 984 | {value: 0, writable: 0, get: 0, set: 0, enumerable: 0, configurable: 0}; |
| 985 | for (var i = 0; i < names.length; i++) { |
| 986 | var N = names[i]; |
| 987 | if (!(%HasLocalProperty(standardNames, N))) { |
| 988 | var attr = GetOwnProperty(attributes, N); |
| 989 | DefineOwnProperty(descObj, N, attr, true); |
| 990 | } |
| 991 | } |
| 992 | // This is really confusing the types, but it is what the proxies spec |
| 993 | // currently requires: |
| 994 | desc = descObj; |
| 995 | */ |
| 996 | } else { |
| 997 | var desc = ToPropertyDescriptor(attributes); |
| 998 | DefineOwnProperty(obj, name, desc, true); |
| 999 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1000 | return obj; |
| 1001 | } |
| 1002 | |
| 1003 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1004 | function GetOwnEnumerablePropertyNames(properties) { |
| 1005 | var names = new InternalArray(); |
| 1006 | for (var key in properties) { |
| 1007 | if (%HasLocalProperty(properties, key)) { |
| 1008 | names.push(key); |
| 1009 | } |
| 1010 | } |
| 1011 | return names; |
| 1012 | } |
| 1013 | |
| 1014 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1015 | // ES5 section 15.2.3.7. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1016 | function ObjectDefineProperties(obj, properties) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1017 | if (!IS_SPEC_OBJECT(obj)) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1018 | throw MakeTypeError("obj_ctor_property_non_object", ["defineProperties"]); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1019 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1020 | var props = ToObject(properties); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1021 | var names = GetOwnEnumerablePropertyNames(props); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1022 | var descriptors = new InternalArray(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1023 | for (var i = 0; i < names.length; i++) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1024 | descriptors.push(ToPropertyDescriptor(props[names[i]])); |
| 1025 | } |
| 1026 | for (var i = 0; i < names.length; i++) { |
| 1027 | DefineOwnProperty(obj, names[i], descriptors[i], true); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1028 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1029 | return obj; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1033 | // Harmony proxies. |
| 1034 | function ProxyFix(obj) { |
| 1035 | var handler = %GetHandler(obj); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1036 | var props = CallTrap0(handler, "fix", void 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1037 | if (IS_UNDEFINED(props)) { |
| 1038 | throw MakeTypeError("handler_returned_undefined", [handler, "fix"]); |
| 1039 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1040 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1041 | if (%IsJSFunctionProxy(obj)) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1042 | var callTrap = %GetCallTrap(obj); |
| 1043 | var constructTrap = %GetConstructTrap(obj); |
| 1044 | var code = DelegateCallAndConstruct(callTrap, constructTrap); |
| 1045 | %Fix(obj); // becomes a regular function |
| 1046 | %SetCode(obj, code); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1047 | // TODO(rossberg): What about length and other properties? Not specified. |
| 1048 | // We just put in some half-reasonable defaults for now. |
| 1049 | var prototype = new $Object(); |
| 1050 | $Object.defineProperty(prototype, "constructor", |
| 1051 | {value: obj, writable: true, enumerable: false, configurable: true}); |
| 1052 | // TODO(v8:1530): defineProperty does not handle prototype and length. |
| 1053 | %FunctionSetPrototype(obj, prototype); |
| 1054 | obj.length = 0; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1055 | } else { |
| 1056 | %Fix(obj); |
| 1057 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1058 | ObjectDefineProperties(obj, props); |
| 1059 | } |
| 1060 | |
| 1061 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1062 | // ES5 section 15.2.3.8. |
| 1063 | function ObjectSeal(obj) { |
| 1064 | if (!IS_SPEC_OBJECT(obj)) { |
| 1065 | throw MakeTypeError("obj_ctor_property_non_object", ["seal"]); |
| 1066 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1067 | if (%IsJSProxy(obj)) { |
| 1068 | ProxyFix(obj); |
| 1069 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1070 | var names = ObjectGetOwnPropertyNames(obj); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1071 | for (var i = 0; i < names.length; i++) { |
| 1072 | var name = names[i]; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1073 | var desc = GetOwnProperty(obj, name); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1074 | if (desc.isConfigurable()) { |
| 1075 | desc.setConfigurable(false); |
| 1076 | DefineOwnProperty(obj, name, desc, true); |
| 1077 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1078 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1079 | %PreventExtensions(obj); |
| 1080 | return obj; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | // ES5 section 15.2.3.9. |
| 1085 | function ObjectFreeze(obj) { |
| 1086 | if (!IS_SPEC_OBJECT(obj)) { |
| 1087 | throw MakeTypeError("obj_ctor_property_non_object", ["freeze"]); |
| 1088 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1089 | if (%IsJSProxy(obj)) { |
| 1090 | ProxyFix(obj); |
| 1091 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1092 | var names = ObjectGetOwnPropertyNames(obj); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1093 | for (var i = 0; i < names.length; i++) { |
| 1094 | var name = names[i]; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1095 | var desc = GetOwnProperty(obj, name); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1096 | if (desc.isWritable() || desc.isConfigurable()) { |
| 1097 | if (IsDataDescriptor(desc)) desc.setWritable(false); |
| 1098 | desc.setConfigurable(false); |
| 1099 | DefineOwnProperty(obj, name, desc, true); |
| 1100 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1101 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1102 | %PreventExtensions(obj); |
| 1103 | return obj; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1107 | // ES5 section 15.2.3.10 |
| 1108 | function ObjectPreventExtension(obj) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1109 | if (!IS_SPEC_OBJECT(obj)) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1110 | throw MakeTypeError("obj_ctor_property_non_object", ["preventExtension"]); |
| 1111 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1112 | if (%IsJSProxy(obj)) { |
| 1113 | ProxyFix(obj); |
| 1114 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1115 | %PreventExtensions(obj); |
| 1116 | return obj; |
| 1117 | } |
| 1118 | |
| 1119 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1120 | // ES5 section 15.2.3.11 |
| 1121 | function ObjectIsSealed(obj) { |
| 1122 | if (!IS_SPEC_OBJECT(obj)) { |
| 1123 | throw MakeTypeError("obj_ctor_property_non_object", ["isSealed"]); |
| 1124 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1125 | if (%IsJSProxy(obj)) { |
| 1126 | return false; |
| 1127 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1128 | var names = ObjectGetOwnPropertyNames(obj); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1129 | for (var i = 0; i < names.length; i++) { |
| 1130 | var name = names[i]; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1131 | var desc = GetOwnProperty(obj, name); |
| 1132 | if (desc.isConfigurable()) return false; |
| 1133 | } |
| 1134 | if (!ObjectIsExtensible(obj)) { |
| 1135 | return true; |
| 1136 | } |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | // ES5 section 15.2.3.12 |
| 1142 | function ObjectIsFrozen(obj) { |
| 1143 | if (!IS_SPEC_OBJECT(obj)) { |
| 1144 | throw MakeTypeError("obj_ctor_property_non_object", ["isFrozen"]); |
| 1145 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1146 | if (%IsJSProxy(obj)) { |
| 1147 | return false; |
| 1148 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1149 | var names = ObjectGetOwnPropertyNames(obj); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1150 | for (var i = 0; i < names.length; i++) { |
| 1151 | var name = names[i]; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1152 | var desc = GetOwnProperty(obj, name); |
| 1153 | if (IsDataDescriptor(desc) && desc.isWritable()) return false; |
| 1154 | if (desc.isConfigurable()) return false; |
| 1155 | } |
| 1156 | if (!ObjectIsExtensible(obj)) { |
| 1157 | return true; |
| 1158 | } |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
| 1162 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1163 | // ES5 section 15.2.3.13 |
| 1164 | function ObjectIsExtensible(obj) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1165 | if (!IS_SPEC_OBJECT(obj)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1166 | throw MakeTypeError("obj_ctor_property_non_object", ["isExtensible"]); |
| 1167 | } |
| 1168 | if (%IsJSProxy(obj)) { |
| 1169 | return true; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1170 | } |
| 1171 | return %IsExtensible(obj); |
| 1172 | } |
| 1173 | |
| 1174 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1175 | %SetCode($Object, function(x) { |
| 1176 | if (%_IsConstructCall()) { |
| 1177 | if (x == null) return this; |
| 1178 | return ToObject(x); |
| 1179 | } else { |
| 1180 | if (x == null) return { }; |
| 1181 | return ToObject(x); |
| 1182 | } |
| 1183 | }); |
| 1184 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1185 | %SetExpectedNumberOfProperties($Object, 4); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1186 | |
| 1187 | // ---------------------------------------------------------------------------- |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1188 | // Object |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1189 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1190 | function SetUpObject() { |
| 1191 | %CheckIsBootstrapping(); |
| 1192 | // Set Up non-enumerable functions on the Object.prototype object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1193 | InstallFunctions($Object.prototype, DONT_ENUM, $Array( |
| 1194 | "toString", ObjectToString, |
| 1195 | "toLocaleString", ObjectToLocaleString, |
| 1196 | "valueOf", ObjectValueOf, |
| 1197 | "hasOwnProperty", ObjectHasOwnProperty, |
| 1198 | "isPrototypeOf", ObjectIsPrototypeOf, |
| 1199 | "propertyIsEnumerable", ObjectPropertyIsEnumerable, |
| 1200 | "__defineGetter__", ObjectDefineGetter, |
| 1201 | "__lookupGetter__", ObjectLookupGetter, |
| 1202 | "__defineSetter__", ObjectDefineSetter, |
| 1203 | "__lookupSetter__", ObjectLookupSetter |
| 1204 | )); |
| 1205 | InstallFunctions($Object, DONT_ENUM, $Array( |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1206 | "keys", ObjectKeys, |
| 1207 | "create", ObjectCreate, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1208 | "defineProperty", ObjectDefineProperty, |
| 1209 | "defineProperties", ObjectDefineProperties, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1210 | "freeze", ObjectFreeze, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1211 | "getPrototypeOf", ObjectGetPrototypeOf, |
| 1212 | "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1213 | "getOwnPropertyNames", ObjectGetOwnPropertyNames, |
| 1214 | "isExtensible", ObjectIsExtensible, |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1215 | "isFrozen", ObjectIsFrozen, |
| 1216 | "isSealed", ObjectIsSealed, |
| 1217 | "preventExtensions", ObjectPreventExtension, |
| 1218 | "seal", ObjectSeal |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1219 | )); |
| 1220 | } |
| 1221 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1222 | SetUpObject(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1223 | |
| 1224 | // ---------------------------------------------------------------------------- |
| 1225 | // Boolean |
| 1226 | |
| 1227 | function BooleanToString() { |
| 1228 | // NOTE: Both Boolean objects and values can enter here as |
| 1229 | // 'this'. This is not as dictated by ECMA-262. |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1230 | var b = this; |
| 1231 | if (!IS_BOOLEAN(b)) { |
| 1232 | if (!IS_BOOLEAN_WRAPPER(b)) { |
| 1233 | throw new $TypeError('Boolean.prototype.toString is not generic'); |
| 1234 | } |
| 1235 | b = %_ValueOf(b); |
| 1236 | } |
| 1237 | return b ? 'true' : 'false'; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | function BooleanValueOf() { |
| 1242 | // NOTE: Both Boolean objects and values can enter here as |
| 1243 | // 'this'. This is not as dictated by ECMA-262. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1244 | if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1245 | throw new $TypeError('Boolean.prototype.valueOf is not generic'); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1246 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1247 | return %_ValueOf(this); |
| 1248 | } |
| 1249 | |
| 1250 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1251 | // ---------------------------------------------------------------------------- |
| 1252 | |
| 1253 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1254 | function SetUpBoolean () { |
| 1255 | %CheckIsBootstrapping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1256 | InstallFunctions($Boolean.prototype, DONT_ENUM, $Array( |
| 1257 | "toString", BooleanToString, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1258 | "valueOf", BooleanValueOf |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1259 | )); |
| 1260 | } |
| 1261 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1262 | SetUpBoolean(); |
| 1263 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1264 | |
| 1265 | // ---------------------------------------------------------------------------- |
| 1266 | // Number |
| 1267 | |
| 1268 | // Set the Number function and constructor. |
| 1269 | %SetCode($Number, function(x) { |
| 1270 | var value = %_ArgumentsLength() == 0 ? 0 : ToNumber(x); |
| 1271 | if (%_IsConstructCall()) { |
| 1272 | %_SetValueOf(this, value); |
| 1273 | } else { |
| 1274 | return value; |
| 1275 | } |
| 1276 | }); |
| 1277 | |
| 1278 | %FunctionSetPrototype($Number, new $Number(0)); |
| 1279 | |
| 1280 | // ECMA-262 section 15.7.4.2. |
| 1281 | function NumberToString(radix) { |
| 1282 | // NOTE: Both Number objects and values can enter here as |
| 1283 | // 'this'. This is not as dictated by ECMA-262. |
| 1284 | var number = this; |
| 1285 | if (!IS_NUMBER(this)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1286 | if (!IS_NUMBER_WRAPPER(this)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1287 | throw new $TypeError('Number.prototype.toString is not generic'); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1288 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1289 | // Get the value of this number in case it's an object. |
| 1290 | number = %_ValueOf(this); |
| 1291 | } |
| 1292 | // Fast case: Convert number in radix 10. |
| 1293 | if (IS_UNDEFINED(radix) || radix === 10) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1294 | return %_NumberToString(number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | // Convert the radix to an integer and check the range. |
| 1298 | radix = TO_INTEGER(radix); |
| 1299 | if (radix < 2 || radix > 36) { |
| 1300 | throw new $RangeError('toString() radix argument must be between 2 and 36'); |
| 1301 | } |
| 1302 | // Convert the number to a string in the given radix. |
| 1303 | return %NumberToRadixString(number, radix); |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | // ECMA-262 section 15.7.4.3 |
| 1308 | function NumberToLocaleString() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1309 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1310 | throw MakeTypeError("called_on_null_or_undefined", |
| 1311 | ["Number.prototype.toLocaleString"]); |
| 1312 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1313 | return this.toString(); |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | // ECMA-262 section 15.7.4.4 |
| 1318 | function NumberValueOf() { |
| 1319 | // NOTE: Both Number objects and values can enter here as |
| 1320 | // 'this'. This is not as dictated by ECMA-262. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1321 | if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1322 | throw new $TypeError('Number.prototype.valueOf is not generic'); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1323 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1324 | return %_ValueOf(this); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | // ECMA-262 section 15.7.4.5 |
| 1329 | function NumberToFixed(fractionDigits) { |
| 1330 | var f = TO_INTEGER(fractionDigits); |
| 1331 | if (f < 0 || f > 20) { |
| 1332 | throw new $RangeError("toFixed() digits argument must be between 0 and 20"); |
| 1333 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1334 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1335 | throw MakeTypeError("called_on_null_or_undefined", |
| 1336 | ["Number.prototype.toFixed"]); |
| 1337 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1338 | var x = ToNumber(this); |
| 1339 | return %NumberToFixed(x, f); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | // ECMA-262 section 15.7.4.6 |
| 1344 | function NumberToExponential(fractionDigits) { |
| 1345 | var f = -1; |
| 1346 | if (!IS_UNDEFINED(fractionDigits)) { |
| 1347 | f = TO_INTEGER(fractionDigits); |
| 1348 | if (f < 0 || f > 20) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1349 | throw new $RangeError( |
| 1350 | "toExponential() argument must be between 0 and 20"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1353 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1354 | throw MakeTypeError("called_on_null_or_undefined", |
| 1355 | ["Number.prototype.toExponential"]); |
| 1356 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1357 | var x = ToNumber(this); |
| 1358 | return %NumberToExponential(x, f); |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | // ECMA-262 section 15.7.4.7 |
| 1363 | function NumberToPrecision(precision) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1364 | if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1365 | throw MakeTypeError("called_on_null_or_undefined", |
| 1366 | ["Number.prototype.toPrecision"]); |
| 1367 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1368 | if (IS_UNDEFINED(precision)) return ToString(%_ValueOf(this)); |
| 1369 | var p = TO_INTEGER(precision); |
| 1370 | if (p < 1 || p > 21) { |
| 1371 | throw new $RangeError("toPrecision() argument must be between 1 and 21"); |
| 1372 | } |
| 1373 | var x = ToNumber(this); |
| 1374 | return %NumberToPrecision(x, p); |
| 1375 | } |
| 1376 | |
| 1377 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1378 | // ---------------------------------------------------------------------------- |
| 1379 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1380 | function SetUpNumber() { |
| 1381 | %CheckIsBootstrapping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1382 | %OptimizeObjectForAddingMultipleProperties($Number.prototype, 8); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1383 | // Set up the constructor property on the Number prototype object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1384 | %SetProperty($Number.prototype, "constructor", $Number, DONT_ENUM); |
| 1385 | |
| 1386 | %OptimizeObjectForAddingMultipleProperties($Number, 5); |
| 1387 | // ECMA-262 section 15.7.3.1. |
| 1388 | %SetProperty($Number, |
| 1389 | "MAX_VALUE", |
| 1390 | 1.7976931348623157e+308, |
| 1391 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1392 | |
| 1393 | // ECMA-262 section 15.7.3.2. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1394 | %SetProperty($Number, "MIN_VALUE", 5e-324, |
| 1395 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1396 | |
| 1397 | // ECMA-262 section 15.7.3.3. |
| 1398 | %SetProperty($Number, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1399 | |
| 1400 | // ECMA-262 section 15.7.3.4. |
| 1401 | %SetProperty($Number, |
| 1402 | "NEGATIVE_INFINITY", |
| 1403 | -1/0, |
| 1404 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 1405 | |
| 1406 | // ECMA-262 section 15.7.3.5. |
| 1407 | %SetProperty($Number, |
| 1408 | "POSITIVE_INFINITY", |
| 1409 | 1/0, |
| 1410 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1411 | %ToFastProperties($Number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1412 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1413 | // Set up non-enumerable functions on the Number prototype object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1414 | InstallFunctions($Number.prototype, DONT_ENUM, $Array( |
| 1415 | "toString", NumberToString, |
| 1416 | "toLocaleString", NumberToLocaleString, |
| 1417 | "valueOf", NumberValueOf, |
| 1418 | "toFixed", NumberToFixed, |
| 1419 | "toExponential", NumberToExponential, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1420 | "toPrecision", NumberToPrecision |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1421 | )); |
| 1422 | } |
| 1423 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1424 | SetUpNumber(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1425 | |
| 1426 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1427 | // ---------------------------------------------------------------------------- |
| 1428 | // Function |
| 1429 | |
| 1430 | $Function.prototype.constructor = $Function; |
| 1431 | |
| 1432 | function FunctionSourceString(func) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1433 | while (%IsJSFunctionProxy(func)) { |
| 1434 | func = %GetCallTrap(func); |
| 1435 | } |
| 1436 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1437 | if (!IS_FUNCTION(func)) { |
| 1438 | throw new $TypeError('Function.prototype.toString is not generic'); |
| 1439 | } |
| 1440 | |
| 1441 | var source = %FunctionGetSourceCode(func); |
| 1442 | if (!IS_STRING(source) || %FunctionIsBuiltin(func)) { |
| 1443 | var name = %FunctionGetName(func); |
| 1444 | if (name) { |
| 1445 | // Mimic what KJS does. |
| 1446 | return 'function ' + name + '() { [native code] }'; |
| 1447 | } else { |
| 1448 | return 'function () { [native code] }'; |
| 1449 | } |
| 1450 | } |
| 1451 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1452 | var name = %FunctionNameShouldPrintAsAnonymous(func) |
| 1453 | ? 'anonymous' |
| 1454 | : %FunctionGetName(func); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1455 | return 'function ' + name + source; |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | function FunctionToString() { |
| 1460 | return FunctionSourceString(this); |
| 1461 | } |
| 1462 | |
| 1463 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1464 | // ES5 15.3.4.5 |
| 1465 | function FunctionBind(this_arg) { // Length is 1. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1466 | if (!IS_SPEC_FUNCTION(this)) { |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1467 | throw new $TypeError('Bind must be called on a function'); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1468 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1469 | var boundFunction = function () { |
| 1470 | // Poison .arguments and .caller, but is otherwise not detectable. |
| 1471 | "use strict"; |
| 1472 | // This function must not use any object literals (Object, Array, RegExp), |
| 1473 | // since the literals-array is being used to store the bound data. |
| 1474 | if (%_IsConstructCall()) { |
| 1475 | return %NewObjectFromBound(boundFunction); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1476 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1477 | var bindings = %BoundFunctionGetBindings(boundFunction); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1478 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1479 | var argc = %_ArgumentsLength(); |
| 1480 | if (argc == 0) { |
| 1481 | return %Apply(bindings[0], bindings[1], bindings, 2, bindings.length - 2); |
| 1482 | } |
| 1483 | if (bindings.length === 2) { |
| 1484 | return %Apply(bindings[0], bindings[1], arguments, 0, argc); |
| 1485 | } |
| 1486 | var bound_argc = bindings.length - 2; |
| 1487 | var argv = new InternalArray(bound_argc + argc); |
| 1488 | for (var i = 0; i < bound_argc; i++) { |
| 1489 | argv[i] = bindings[i + 2]; |
| 1490 | } |
| 1491 | for (var j = 0; j < argc; j++) { |
| 1492 | argv[i++] = %_Arguments(j); |
| 1493 | } |
| 1494 | return %Apply(bindings[0], bindings[1], argv, 0, bound_argc + argc); |
| 1495 | }; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1496 | |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1497 | %FunctionRemovePrototype(boundFunction); |
| 1498 | var new_length = 0; |
| 1499 | if (%_ClassOf(this) == "Function") { |
| 1500 | // Function or FunctionProxy. |
| 1501 | var old_length = this.length; |
| 1502 | // FunctionProxies might provide a non-UInt32 value. If so, ignore it. |
| 1503 | if ((typeof old_length === "number") && |
| 1504 | ((old_length >>> 0) === old_length)) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1505 | var argc = %_ArgumentsLength(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1506 | if (argc > 0) argc--; // Don't count the thisArg as parameter. |
| 1507 | new_length = old_length - argc; |
| 1508 | if (new_length < 0) new_length = 0; |
| 1509 | } |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1510 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1511 | // This runtime function finds any remaining arguments on the stack, |
| 1512 | // so we don't pass the arguments object. |
| 1513 | var result = %FunctionBindArguments(boundFunction, this, |
| 1514 | this_arg, new_length); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1515 | |
| 1516 | // We already have caller and arguments properties on functions, |
| 1517 | // which are non-configurable. It therefore makes no sence to |
| 1518 | // try to redefine these as defined by the spec. The spec says |
| 1519 | // that bind should make these throw a TypeError if get or set |
| 1520 | // is called and make them non-enumerable and non-configurable. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1521 | // To be consistent with our normal functions we leave this as it is. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame^] | 1522 | // TODO(lrn): Do set these to be thrower. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1523 | return result; |
| 1524 | } |
| 1525 | |
| 1526 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1527 | function NewFunction(arg1) { // length == 1 |
| 1528 | var n = %_ArgumentsLength(); |
| 1529 | var p = ''; |
| 1530 | if (n > 1) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1531 | p = new InternalArray(n - 1); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 1532 | for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i); |
| 1533 | p = Join(p, n - 1, ',', NonStringToString); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1534 | // If the formal parameters string include ) - an illegal |
| 1535 | // character - it may make the combined function expression |
| 1536 | // compile. We avoid this problem by checking for this early on. |
| 1537 | if (p.indexOf(')') != -1) throw MakeSyntaxError('unable_to_parse',[]); |
| 1538 | } |
| 1539 | var body = (n > 0) ? ToString(%_Arguments(n - 1)) : ''; |
| 1540 | var source = '(function(' + p + ') {\n' + body + '\n})'; |
| 1541 | |
| 1542 | // The call to SetNewFunctionAttributes will ensure the prototype |
| 1543 | // property of the resulting function is enumerable (ECMA262, 15.3.5.2). |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 1544 | var f = %CompileString(source)(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1545 | %FunctionMarkNameShouldPrintAsAnonymous(f); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1546 | return %SetNewFunctionAttributes(f); |
| 1547 | } |
| 1548 | |
| 1549 | %SetCode($Function, NewFunction); |
| 1550 | |
| 1551 | // ---------------------------------------------------------------------------- |
| 1552 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1553 | function SetUpFunction() { |
| 1554 | %CheckIsBootstrapping(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1555 | InstallFunctions($Function.prototype, DONT_ENUM, $Array( |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1556 | "bind", FunctionBind, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1557 | "toString", FunctionToString |
| 1558 | )); |
| 1559 | } |
| 1560 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1561 | SetUpFunction(); |