blob: 1d54e28e97687ca981372b41525ffbed6b150e3a [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
41const $isNaN = GlobalIsNaN;
42const $isFinite = GlobalIsFinite;
43
44// ----------------------------------------------------------------------------
45
46
47// Helper function used to install functions on objects.
48function 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 Block6ded16b2010-05-10 14:33:55 +010056 %FunctionRemovePrototype(f);
Steve Blocka7e24c12009-10-30 11:49:00 +000057 %SetProperty(object, key, f, attributes);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000058 %SetNativeFlag(f);
Steve Blocka7e24c12009-10-30 11:49:00 +000059 }
Andrei Popescu402d9372010-02-26 13:31:12 +000060 %ToFastProperties(object);
Steve Blocka7e24c12009-10-30 11:49:00 +000061}
62
Ben Murdoch589d6972011-11-30 16:04:58 +000063// 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.
67function 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 Blocka7e24c12009-10-30 11:49:00 +000092// ----------------------------------------------------------------------------
93
94
95// ECMA 262 - 15.1.4
96function GlobalIsNaN(number) {
Ben Murdoch589d6972011-11-30 16:04:58 +000097 if (!IS_NUMBER(number)) number = NonNumberToNumber(number);
98 return NUMBER_IS_NAN(number);
Steve Blocka7e24c12009-10-30 11:49:00 +000099}
100
101
102// ECMA 262 - 15.1.5
103function GlobalIsFinite(number) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100104 if (!IS_NUMBER(number)) number = NonNumberToNumber(number);
Ben Murdoch589d6972011-11-30 16:04:58 +0000105 return NUMBER_IS_FINITE(number);
Steve Blocka7e24c12009-10-30 11:49:00 +0000106}
107
108
109// ECMA-262 - 15.1.2.2
110function GlobalParseInt(string, radix) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100111 if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 // 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 Blockd0582a62009-12-15 09:54:21 +0000119 ((0.01 < string && string < 1e9) ||
120 (-1e9 < string && string < -0.01))) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 // Truncate number.
122 return string | 0;
123 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000124 string = TO_STRING_INLINE(string);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125 radix = radix | 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000127 // The spec says ToString should be evaluated before ToInt32.
128 string = TO_STRING_INLINE(string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 radix = TO_INT32(radix);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000130 if (!(radix == 0 || (2 <= radix && radix <= 36))) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 return $NaN;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000132 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000134
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100135 if (%_HasCachedArrayIndex(string) &&
136 (radix == 0 || radix == 10)) {
137 return %_GetCachedArrayIndex(string);
138 }
139 return %StringParseInt(string, radix);
Steve Blocka7e24c12009-10-30 11:49:00 +0000140}
141
142
143// ECMA-262 - 15.1.2.3
144function GlobalParseFloat(string) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100145 string = TO_STRING_INLINE(string);
146 if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string);
147 return %StringParseFloat(string);
Steve Blocka7e24c12009-10-30 11:49:00 +0000148}
149
150
151function GlobalEval(x) {
152 if (!IS_STRING(x)) return x;
153
154 var global_receiver = %GlobalReceiver(global);
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 var global_is_detached = (global === global_receiver);
156
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000157 // 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 Murdoch592a9fc2012-03-05 11:04:45 +0000160 // 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 Blocka7e24c12009-10-30 11:49:00 +0000164 'be the global object from which eval originated');
165 }
166
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800167 var f = %CompileString(x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 if (!IS_FUNCTION(f)) return f;
169
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000170 return %_CallFunction(global_receiver, f);
Steve Blocka7e24c12009-10-30 11:49:00 +0000171}
172
173
Steve Blocka7e24c12009-10-30 11:49:00 +0000174// ----------------------------------------------------------------------------
175
Ben Murdoch589d6972011-11-30 16:04:58 +0000176// Set up global object.
177function SetUpGlobal() {
178 %CheckIsBootstrapping();
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 // ECMA 262 - 15.1.1.1.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000180 %SetProperty(global, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000181
182 // ECMA-262 - 15.1.1.2.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000183 %SetProperty(global, "Infinity", 1/0, DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185 // ECMA-262 - 15.1.1.3.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000186 %SetProperty(global, "undefined", void 0,
187 DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +0000188
Ben Murdoch589d6972011-11-30 16:04:58 +0000189 // Set up non-enumerable function on the global object.
Steve Blocka7e24c12009-10-30 11:49:00 +0000190 InstallFunctions(global, DONT_ENUM, $Array(
191 "isNaN", GlobalIsNaN,
192 "isFinite", GlobalIsFinite,
193 "parseInt", GlobalParseInt,
194 "parseFloat", GlobalParseFloat,
Steve Block053d10c2011-06-13 19:13:29 +0100195 "eval", GlobalEval
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 ));
197}
198
Ben Murdoch589d6972011-11-30 16:04:58 +0000199SetUpGlobal();
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
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
223function ObjectToString() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000224 if (IS_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
225 return '[object Undefined]';
226 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000227 if (IS_NULL(this)) return '[object Null]';
Leon Clarked91b9f72010-01-27 17:25:45 +0000228 return "[object " + %_ClassOf(ToObject(this)) + "]";
Steve Blocka7e24c12009-10-30 11:49:00 +0000229}
230
231
232// ECMA-262 - 15.2.4.3
233function ObjectToLocaleString() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000234 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
235 throw MakeTypeError("called_on_null_or_undefined",
236 ["Object.prototype.toLocaleString"]);
237 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 return this.toString();
239}
240
241
242// ECMA-262 - 15.2.4.4
243function ObjectValueOf() {
Leon Clarked91b9f72010-01-27 17:25:45 +0000244 return ToObject(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000245}
246
247
248// ECMA-262 - 15.2.4.5
249function ObjectHasOwnProperty(V) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000250 if (%IsJSProxy(this)) {
251 var handler = %GetHandler(this);
252 return CallTrap1(handler, "hasOwn", DerivedHasOwnTrap, TO_STRING_INLINE(V));
253 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000254 return %HasLocalProperty(TO_OBJECT_INLINE(this), TO_STRING_INLINE(V));
Steve Blocka7e24c12009-10-30 11:49:00 +0000255}
256
257
258// ECMA-262 - 15.2.4.6
259function ObjectIsPrototypeOf(V) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000260 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
261 throw MakeTypeError("called_on_null_or_undefined",
262 ["Object.prototype.isPrototypeOf"]);
263 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100264 if (!IS_SPEC_OBJECT(V)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 return %IsInPrototypeChain(this, V);
266}
267
268
269// ECMA-262 - 15.2.4.6
270function ObjectPropertyIsEnumerable(V) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000271 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 Blocka7e24c12009-10-30 11:49:00 +0000277}
278
279
280// Extensions for providing property getters and setters.
281function ObjectDefineGetter(name, fun) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000282 var receiver = this;
283 if (receiver == null && !IS_UNDETECTABLE(receiver)) {
284 receiver = %GlobalReceiver(global);
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 }
Ben Murdoch589d6972011-11-30 16:04:58 +0000286 if (!IS_SPEC_FUNCTION(fun)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000287 throw new $TypeError(
288 'Object.prototype.__defineGetter__: Expecting function');
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 }
Steve Block44f0eee2011-05-26 01:26:41 +0100290 var desc = new PropertyDescriptor();
291 desc.setGet(fun);
292 desc.setEnumerable(true);
293 desc.setConfigurable(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000294 DefineOwnProperty(ToObject(receiver), ToString(name), desc, false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295}
296
297
298function ObjectLookupGetter(name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000299 var receiver = this;
300 if (receiver == null && !IS_UNDETECTABLE(receiver)) {
301 receiver = %GlobalReceiver(global);
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000303 return %LookupAccessor(ToObject(receiver), ToString(name), GETTER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304}
305
306
307function ObjectDefineSetter(name, fun) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000308 var receiver = this;
309 if (receiver == null && !IS_UNDETECTABLE(receiver)) {
310 receiver = %GlobalReceiver(global);
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 }
Ben Murdoch589d6972011-11-30 16:04:58 +0000312 if (!IS_SPEC_FUNCTION(fun)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 throw new $TypeError(
314 'Object.prototype.__defineSetter__: Expecting function');
315 }
Steve Block44f0eee2011-05-26 01:26:41 +0100316 var desc = new PropertyDescriptor();
317 desc.setSet(fun);
318 desc.setEnumerable(true);
319 desc.setConfigurable(true);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000320 DefineOwnProperty(ToObject(receiver), ToString(name), desc, false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321}
322
323
324function ObjectLookupSetter(name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000325 var receiver = this;
326 if (receiver == null && !IS_UNDETECTABLE(receiver)) {
327 receiver = %GlobalReceiver(global);
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000329 return %LookupAccessor(ToObject(receiver), ToString(name), SETTER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000330}
331
332
333function ObjectKeys(obj) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000334 if (!IS_SPEC_OBJECT(obj)) {
Leon Clarkee46be812010-01-19 14:06:41 +0000335 throw MakeTypeError("obj_ctor_property_non_object", ["keys"]);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000336 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000337 if (%IsJSProxy(obj)) {
338 var handler = %GetHandler(obj);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000339 var names = CallTrap0(handler, "keys", DerivedKeysTrap);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000340 return ToStringArray(names);
341 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 return %LocalKeys(obj);
343}
344
345
Leon Clarkee46be812010-01-19 14:06:41 +0000346// ES5 8.10.1.
347function IsAccessorDescriptor(desc) {
348 if (IS_UNDEFINED(desc)) return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000349 return desc.hasGetter() || desc.hasSetter();
Leon Clarkee46be812010-01-19 14:06:41 +0000350}
351
352
353// ES5 8.10.2.
354function IsDataDescriptor(desc) {
355 if (IS_UNDEFINED(desc)) return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000356 return desc.hasValue() || desc.hasWritable();
Leon Clarkee46be812010-01-19 14:06:41 +0000357}
358
359
360// ES5 8.10.3.
361function IsGenericDescriptor(desc) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000362 if (IS_UNDEFINED(desc)) return false;
Leon Clarkee46be812010-01-19 14:06:41 +0000363 return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc));
364}
365
366
367function IsInconsistentDescriptor(desc) {
368 return IsAccessorDescriptor(desc) && IsDataDescriptor(desc);
369}
370
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000371
Leon Clarkee46be812010-01-19 14:06:41 +0000372// ES5 8.10.4
373function FromPropertyDescriptor(desc) {
Andrei Popescu31002712010-02-23 13:46:05 +0000374 if (IS_UNDEFINED(desc)) return desc;
Ben Murdoch257744e2011-11-30 15:57:28 +0000375
Leon Clarkee46be812010-01-19 14:06:41 +0000376 if (IsDataDescriptor(desc)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000377 return { value: desc.getValue(),
378 writable: desc.isWritable(),
379 enumerable: desc.isEnumerable(),
380 configurable: desc.isConfigurable() };
Leon Clarkee46be812010-01-19 14:06:41 +0000381 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000382 // 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 Clarkee46be812010-01-19 14:06:41 +0000387}
388
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000389
390// Harmony Proxies
391function 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 Clarkee46be812010-01-19 14:06:41 +0000419// ES5 8.10.5.
420function ToPropertyDescriptor(obj) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100421 if (!IS_SPEC_OBJECT(obj)) {
Leon Clarkee46be812010-01-19 14:06:41 +0000422 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 Clarkee46be812010-01-19 14:06:41 +0000430 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 Murdoch589d6972011-11-30 16:04:58 +0000444 if (!IS_UNDEFINED(get) && !IS_SPEC_FUNCTION(get)) {
Leon Clarkee46be812010-01-19 14:06:41 +0000445 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 Murdoch589d6972011-11-30 16:04:58 +0000452 if (!IS_UNDEFINED(set) && !IS_SPEC_FUNCTION(set)) {
Leon Clarkee46be812010-01-19 14:06:41 +0000453 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 Murdoch3fb3ca82011-12-02 17:19:32 +0000465// For Harmony proxies.
466function ToCompletePropertyDescriptor(obj) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000467 var desc = ToPropertyDescriptor(obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000468 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 Clarkee46be812010-01-19 14:06:41 +0000482function 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 Popescu31002712010-02-23 13:46:05 +0000490 this.hasEnumerable_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000491 this.configurable_ = false;
Andrei Popescu31002712010-02-23 13:46:05 +0000492 this.hasConfigurable_ = false;
Leon Clarkee46be812010-01-19 14:06:41 +0000493 this.get_ = void 0;
494 this.hasGetter_ = false;
495 this.set_ = void 0;
496 this.hasSetter_ = false;
497}
498
Ben Murdoch589d6972011-11-30 16:04:58 +0000499SetUpLockedPrototype(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 Popescu31002712010-02-23 13:46:05 +0000576
577
Steve Block1e0659c2011-05-24 12:43:12 +0100578// 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.
581function ConvertDescriptorArrayToDescriptor(desc_array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100582 if (desc_array === false) {
Steve Block1e0659c2011-05-24 12:43:12 +0100583 throw 'Internal error: invalid desc_array';
Steve Block103cc402011-02-16 13:27:44 +0000584 }
Steve Block1e0659c2011-05-24 12:43:12 +0100585
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 Clarkee46be812010-01-19 14:06:41 +0000601
602 return desc;
603}
604
605
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000606// For Harmony proxies.
607function 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 Murdoch589d6972011-11-30 16:04:58 +0000614 } else if (!IS_SPEC_FUNCTION(trap)) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000615 throw MakeTypeError("handler_trap_must_be_callable", [handler, name]);
616 }
617 return trap;
618}
619
620
621function CallTrap0(handler, name, defaultTrap) {
622 return %_CallFunction(handler, GetTrap(handler, name, defaultTrap));
623}
624
625
626function CallTrap1(handler, name, defaultTrap, x) {
627 return %_CallFunction(handler, x, GetTrap(handler, name, defaultTrap));
628}
629
630
631function CallTrap2(handler, name, defaultTrap, x, y) {
632 return %_CallFunction(handler, x, y, GetTrap(handler, name, defaultTrap));
633}
634
635
Steve Block1e0659c2011-05-24 12:43:12 +0100636// ES5 section 8.12.1.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000637function GetOwnProperty(obj, v) {
638 var p = ToString(v);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000639 if (%IsJSProxy(obj)) {
640 var handler = %GetHandler(obj);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000641 var descriptor = CallTrap1(handler, "getOwnPropertyDescriptor", void 0, p);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000642 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 Block1e0659c2011-05-24 12:43:12 +0100651 // 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 Murdoch69a99ed2011-11-30 16:03:39 +0000654 var props = %GetOwnProperty(ToObject(obj), ToString(v));
Steve Block1e0659c2011-05-24 12:43:12 +0100655
656 // A false value here means that access checks failed.
Steve Block44f0eee2011-05-26 01:26:41 +0100657 if (props === false) return void 0;
Steve Block1e0659c2011-05-24 12:43:12 +0100658
659 return ConvertDescriptorArrayToDescriptor(props);
660}
661
662
Ben Murdochc7cc0282012-03-05 14:35:55 +0000663// ES5 section 8.12.7.
664function Delete(obj, p, should_throw) {
665 var desc = GetOwnProperty(obj, p);
666 if (IS_UNDEFINED(desc)) return true;
667 if (desc.isConfigurable()) {
668 %DeleteProperty(obj, p, 0);
669 return true;
670 } else if (should_throw) {
671 throw MakeTypeError("define_disallowed", [p]);
672 } else {
673 return;
674 }
675}
676
677
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000678// Harmony proxies.
679function DefineProxyProperty(obj, p, attributes, should_throw) {
680 var handler = %GetHandler(obj);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000681 var result = CallTrap2(handler, "defineProperty", void 0, p, attributes);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000682 if (!ToBoolean(result)) {
683 if (should_throw) {
684 throw MakeTypeError("handler_returned_false",
685 [handler, "defineProperty"]);
686 } else {
687 return false;
688 }
689 }
690 return true;
691}
692
693
Steve Block6ded16b2010-05-10 14:33:55 +0100694// ES5 8.12.9.
Ben Murdochc7cc0282012-03-05 14:35:55 +0000695function DefineObjectProperty(obj, p, desc, should_throw) {
Steve Block1e0659c2011-05-24 12:43:12 +0100696 var current_or_access = %GetOwnProperty(ToObject(obj), ToString(p));
697 // A false value here means that access checks failed.
Steve Block44f0eee2011-05-26 01:26:41 +0100698 if (current_or_access === false) return void 0;
Steve Block1e0659c2011-05-24 12:43:12 +0100699
700 var current = ConvertDescriptorArrayToDescriptor(current_or_access);
Andrei Popescu31002712010-02-23 13:46:05 +0000701 var extensible = %IsExtensible(ToObject(obj));
702
703 // Error handling according to spec.
704 // Step 3
Steve Block44f0eee2011-05-26 01:26:41 +0100705 if (IS_UNDEFINED(current) && !extensible) {
706 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000707 throw MakeTypeError("define_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100708 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000709 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100710 }
711 }
Andrei Popescu31002712010-02-23 13:46:05 +0000712
Steve Block1e0659c2011-05-24 12:43:12 +0100713 if (!IS_UNDEFINED(current)) {
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100714 // Step 5 and 6
Steve Block1e0659c2011-05-24 12:43:12 +0100715 if ((IsGenericDescriptor(desc) ||
716 IsDataDescriptor(desc) == IsDataDescriptor(current)) &&
717 (!desc.hasEnumerable() ||
718 SameValue(desc.isEnumerable(), current.isEnumerable())) &&
Ben Murdochf87a2032010-10-22 12:50:53 +0100719 (!desc.hasConfigurable() ||
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100720 SameValue(desc.isConfigurable(), current.isConfigurable())) &&
Ben Murdochf87a2032010-10-22 12:50:53 +0100721 (!desc.hasWritable() ||
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100722 SameValue(desc.isWritable(), current.isWritable())) &&
723 (!desc.hasValue() ||
724 SameValue(desc.getValue(), current.getValue())) &&
725 (!desc.hasGetter() ||
726 SameValue(desc.getGet(), current.getGet())) &&
727 (!desc.hasSetter() ||
728 SameValue(desc.getSet(), current.getSet()))) {
729 return true;
730 }
Steve Block1e0659c2011-05-24 12:43:12 +0100731 if (!current.isConfigurable()) {
732 // Step 7
733 if (desc.isConfigurable() ||
734 (desc.hasEnumerable() &&
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100735 desc.isEnumerable() != current.isEnumerable())) {
Steve Block44f0eee2011-05-26 01:26:41 +0100736 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000737 throw MakeTypeError("redefine_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100738 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000739 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100740 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100741 }
Steve Block1e0659c2011-05-24 12:43:12 +0100742 // Step 8
743 if (!IsGenericDescriptor(desc)) {
744 // Step 9a
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100745 if (IsDataDescriptor(current) != IsDataDescriptor(desc)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100746 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000747 throw MakeTypeError("redefine_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100748 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000749 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100750 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100751 }
Steve Block1e0659c2011-05-24 12:43:12 +0100752 // Step 10a
753 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100754 if (!current.isWritable() && desc.isWritable()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100755 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000756 throw MakeTypeError("redefine_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100757 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000758 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100759 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100760 }
Steve Block1e0659c2011-05-24 12:43:12 +0100761 if (!current.isWritable() && desc.hasValue() &&
762 !SameValue(desc.getValue(), current.getValue())) {
Steve Block44f0eee2011-05-26 01:26:41 +0100763 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000764 throw MakeTypeError("redefine_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100765 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000766 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100767 }
Steve Block1e0659c2011-05-24 12:43:12 +0100768 }
769 }
770 // Step 11
771 if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100772 if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())) {
Steve Block44f0eee2011-05-26 01:26:41 +0100773 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000774 throw MakeTypeError("redefine_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100775 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000776 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100777 }
Steve Block1e0659c2011-05-24 12:43:12 +0100778 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100779 if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet())) {
Steve Block44f0eee2011-05-26 01:26:41 +0100780 if (should_throw) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000781 throw MakeTypeError("redefine_disallowed", [p]);
Steve Block44f0eee2011-05-26 01:26:41 +0100782 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000783 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100784 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100785 }
Steve Block1e0659c2011-05-24 12:43:12 +0100786 }
Andrei Popescu31002712010-02-23 13:46:05 +0000787 }
788 }
Andrei Popescu31002712010-02-23 13:46:05 +0000789 }
790
Steve Block6ded16b2010-05-10 14:33:55 +0100791 // Send flags - enumerable and configurable are common - writable is
Andrei Popescu31002712010-02-23 13:46:05 +0000792 // only send to the data descriptor.
793 // Take special care if enumerable and configurable is not defined on
794 // desc (we need to preserve the existing values from current).
795 var flag = NONE;
796 if (desc.hasEnumerable()) {
797 flag |= desc.isEnumerable() ? 0 : DONT_ENUM;
798 } else if (!IS_UNDEFINED(current)) {
799 flag |= current.isEnumerable() ? 0 : DONT_ENUM;
Leon Clarkee46be812010-01-19 14:06:41 +0000800 } else {
Andrei Popescu31002712010-02-23 13:46:05 +0000801 flag |= DONT_ENUM;
802 }
803
804 if (desc.hasConfigurable()) {
805 flag |= desc.isConfigurable() ? 0 : DONT_DELETE;
806 } else if (!IS_UNDEFINED(current)) {
807 flag |= current.isConfigurable() ? 0 : DONT_DELETE;
808 } else
809 flag |= DONT_DELETE;
810
Steve Block1e0659c2011-05-24 12:43:12 +0100811 if (IsDataDescriptor(desc) ||
812 (IsGenericDescriptor(desc) &&
813 (IS_UNDEFINED(current) || IsDataDescriptor(current)))) {
814 // There are 3 cases that lead here:
815 // Step 4a - defining a new data property.
816 // Steps 9b & 12 - replacing an existing accessor property with a data
817 // property.
818 // Step 12 - updating an existing data property with a data or generic
819 // descriptor.
820
Leon Clarkef7060e22010-06-03 12:02:55 +0100821 if (desc.hasWritable()) {
822 flag |= desc.isWritable() ? 0 : READ_ONLY;
823 } else if (!IS_UNDEFINED(current)) {
824 flag |= current.isWritable() ? 0 : READ_ONLY;
825 } else {
826 flag |= READ_ONLY;
827 }
Steve Block1e0659c2011-05-24 12:43:12 +0100828
Ben Murdochb0fe1622011-05-05 13:52:32 +0100829 var value = void 0; // Default value is undefined.
830 if (desc.hasValue()) {
831 value = desc.getValue();
Steve Block1e0659c2011-05-24 12:43:12 +0100832 } else if (!IS_UNDEFINED(current) && IsDataDescriptor(current)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100833 value = current.getValue();
834 }
Steve Block1e0659c2011-05-24 12:43:12 +0100835
Ben Murdochb0fe1622011-05-05 13:52:32 +0100836 %DefineOrRedefineDataProperty(obj, p, value, flag);
Steve Block1e0659c2011-05-24 12:43:12 +0100837 } else if (IsGenericDescriptor(desc)) {
838 // Step 12 - updating an existing accessor property with generic
839 // descriptor. Changing flags only.
840 %DefineOrRedefineAccessorProperty(obj, p, GETTER, current.getGet(), flag);
Andrei Popescu31002712010-02-23 13:46:05 +0000841 } else {
Steve Block1e0659c2011-05-24 12:43:12 +0100842 // There are 3 cases that lead here:
843 // Step 4b - defining a new accessor property.
844 // Steps 9c & 12 - replacing an existing data property with an accessor
845 // property.
846 // Step 12 - updating an existing accessor property with an accessor
847 // descriptor.
848 if (desc.hasGetter()) {
849 %DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag);
Andrei Popescu31002712010-02-23 13:46:05 +0000850 }
Steve Block1e0659c2011-05-24 12:43:12 +0100851 if (desc.hasSetter()) {
Andrei Popescu31002712010-02-23 13:46:05 +0000852 %DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag);
853 }
Leon Clarkee46be812010-01-19 14:06:41 +0000854 }
855 return true;
856}
857
858
Ben Murdochc7cc0282012-03-05 14:35:55 +0000859// ES5 section 15.4.5.1.
860function DefineArrayProperty(obj, p, desc, should_throw) {
861 // Note that the length of an array is not actually stored as part of the
862 // property, hence we use generated code throughout this function instead of
863 // DefineObjectProperty() to modify its value.
864
865 // Step 3 - Special handling for length property.
866 if (p == "length") {
867 var length = obj.length;
868 if (!desc.hasValue()) {
869 return DefineObjectProperty(obj, "length", desc, should_throw);
870 }
871 var new_length = ToUint32(desc.getValue());
872 if (new_length != ToNumber(desc.getValue())) {
873 throw new $RangeError('defineProperty() array length out of range');
874 }
875 var length_desc = GetOwnProperty(obj, "length");
876 if (new_length != length && !length_desc.isWritable()) {
877 if (should_throw) {
878 throw MakeTypeError("redefine_disallowed", [p]);
879 } else {
880 return false;
881 }
882 }
883 var threw = false;
884 while (new_length < length--) {
885 if (!Delete(obj, ToString(length), false)) {
886 new_length = length + 1;
887 threw = true;
888 break;
889 }
890 }
891 // Make sure the below call to DefineObjectProperty() doesn't overwrite
892 // any magic "length" property by removing the value.
893 obj.length = new_length;
894 desc.value_ = void 0;
895 desc.hasValue_ = false;
896 if (!DefineObjectProperty(obj, "length", desc, should_throw) || threw) {
897 if (should_throw) {
898 throw MakeTypeError("redefine_disallowed", [p]);
899 } else {
900 return false;
901 }
902 }
903 return true;
904 }
905
906 // Step 4 - Special handling for array index.
907 var index = ToUint32(p);
908 if (index == ToNumber(p) && index != 4294967295) {
909 var length = obj.length;
910 var length_desc = GetOwnProperty(obj, "length");
911 if ((index >= length && !length_desc.isWritable()) ||
912 !DefineObjectProperty(obj, p, desc, true)) {
913 if (should_throw) {
914 throw MakeTypeError("define_disallowed", [p]);
915 } else {
916 return false;
917 }
918 }
919 if (index >= length) {
920 obj.length = index + 1;
921 }
922 return true;
923 }
924
925 // Step 5 - Fallback to default implementation.
926 return DefineObjectProperty(obj, p, desc, should_throw);
927}
928
929
930// ES5 section 8.12.9, ES5 section 15.4.5.1 and Harmony proxies.
931function DefineOwnProperty(obj, p, desc, should_throw) {
932 if (%IsJSProxy(obj)) {
933 var attributes = FromGenericPropertyDescriptor(desc);
934 return DefineProxyProperty(obj, p, attributes, should_throw);
935 } else if (IS_ARRAY(obj)) {
936 return DefineArrayProperty(obj, p, desc, should_throw);
937 } else {
938 return DefineObjectProperty(obj, p, desc, should_throw);
939 }
940}
941
942
Leon Clarkee46be812010-01-19 14:06:41 +0000943// ES5 section 15.2.3.2.
944function ObjectGetPrototypeOf(obj) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000945 if (!IS_SPEC_OBJECT(obj)) {
Leon Clarkee46be812010-01-19 14:06:41 +0000946 throw MakeTypeError("obj_ctor_property_non_object", ["getPrototypeOf"]);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000947 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000948 return %GetPrototype(obj);
Leon Clarkee46be812010-01-19 14:06:41 +0000949}
950
951
Steve Block6ded16b2010-05-10 14:33:55 +0100952// ES5 section 15.2.3.3
Leon Clarkee46be812010-01-19 14:06:41 +0000953function ObjectGetOwnPropertyDescriptor(obj, p) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000954 if (!IS_SPEC_OBJECT(obj)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000955 throw MakeTypeError("obj_ctor_property_non_object",
956 ["getOwnPropertyDescriptor"]);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000957 }
Leon Clarkee46be812010-01-19 14:06:41 +0000958 var desc = GetOwnProperty(obj, p);
959 return FromPropertyDescriptor(desc);
960}
961
962
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000963// For Harmony proxies
964function ToStringArray(obj, trap) {
965 if (!IS_SPEC_OBJECT(obj)) {
966 throw MakeTypeError("proxy_non_object_prop_names", [obj, trap]);
967 }
968 var n = ToUint32(obj.length);
969 var array = new $Array(n);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000970 var names = {}; // TODO(rossberg): use sets once they are ready.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000971 for (var index = 0; index < n; index++) {
972 var s = ToString(obj[index]);
973 if (s in names) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000974 throw MakeTypeError("proxy_repeated_prop_name", [obj, trap, s]);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000975 }
976 array[index] = s;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000977 names[s] = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000978 }
979 return array;
980}
981
982
Leon Clarkee46be812010-01-19 14:06:41 +0000983// ES5 section 15.2.3.4.
984function ObjectGetOwnPropertyNames(obj) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000985 if (!IS_SPEC_OBJECT(obj)) {
986 throw MakeTypeError("obj_ctor_property_non_object",
987 ["getOwnPropertyNames"]);
988 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000989 // Special handling for proxies.
990 if (%IsJSProxy(obj)) {
991 var handler = %GetHandler(obj);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000992 var names = CallTrap0(handler, "getOwnPropertyNames", void 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000993 return ToStringArray(names, "getOwnPropertyNames");
994 }
995
Leon Clarkee46be812010-01-19 14:06:41 +0000996 // Find all the indexed properties.
997
998 // Get the local element names.
999 var propertyNames = %GetLocalElementNames(obj);
1000
1001 // Get names for indexed interceptor properties.
1002 if (%GetInterceptorInfo(obj) & 1) {
1003 var indexedInterceptorNames =
1004 %GetIndexedInterceptorElementNames(obj);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001005 if (indexedInterceptorNames) {
Leon Clarkee46be812010-01-19 14:06:41 +00001006 propertyNames = propertyNames.concat(indexedInterceptorNames);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001007 }
Leon Clarkee46be812010-01-19 14:06:41 +00001008 }
1009
1010 // Find all the named properties.
1011
1012 // Get the local property names.
1013 propertyNames = propertyNames.concat(%GetLocalPropertyNames(obj));
1014
1015 // Get names for named interceptor properties if any.
1016
1017 if (%GetInterceptorInfo(obj) & 2) {
1018 var namedInterceptorNames =
1019 %GetNamedInterceptorPropertyNames(obj);
1020 if (namedInterceptorNames) {
1021 propertyNames = propertyNames.concat(namedInterceptorNames);
1022 }
1023 }
1024
Steve Block8defd9f2010-07-08 12:39:36 +01001025 // Property names are expected to be unique strings.
1026 var propertySet = {};
1027 var j = 0;
1028 for (var i = 0; i < propertyNames.length; ++i) {
1029 var name = ToString(propertyNames[i]);
1030 // We need to check for the exact property value since for intrinsic
1031 // properties like toString if(propertySet["toString"]) will always
1032 // succeed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001033 if (propertySet[name] === true) {
Steve Block8defd9f2010-07-08 12:39:36 +01001034 continue;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001035 }
Steve Block8defd9f2010-07-08 12:39:36 +01001036 propertySet[name] = true;
1037 propertyNames[j++] = name;
1038 }
1039 propertyNames.length = j;
Andrei Popescu402d9372010-02-26 13:31:12 +00001040
Leon Clarkee46be812010-01-19 14:06:41 +00001041 return propertyNames;
1042}
1043
1044
1045// ES5 section 15.2.3.5.
1046function ObjectCreate(proto, properties) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001047 if (!IS_SPEC_OBJECT(proto) && proto !== null) {
Leon Clarkee46be812010-01-19 14:06:41 +00001048 throw MakeTypeError("proto_object_or_null", [proto]);
1049 }
1050 var obj = new $Object();
1051 obj.__proto__ = proto;
1052 if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties);
1053 return obj;
1054}
1055
1056
Andrei Popescu31002712010-02-23 13:46:05 +00001057// ES5 section 15.2.3.6.
1058function ObjectDefineProperty(obj, p, attributes) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001059 if (!IS_SPEC_OBJECT(obj)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001060 throw MakeTypeError("obj_ctor_property_non_object", ["defineProperty"]);
Leon Clarkef7060e22010-06-03 12:02:55 +01001061 }
Andrei Popescu31002712010-02-23 13:46:05 +00001062 var name = ToString(p);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001063 if (%IsJSProxy(obj)) {
1064 // Clone the attributes object for protection.
1065 // TODO(rossberg): not spec'ed yet, so not sure if this should involve
1066 // non-own properties as it does (or non-enumerable ones, as it doesn't?).
Ben Murdoch589d6972011-11-30 16:04:58 +00001067 var attributesClone = {};
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001068 for (var a in attributes) {
1069 attributesClone[a] = attributes[a];
1070 }
1071 DefineProxyProperty(obj, name, attributesClone, true);
1072 // The following would implement the spec as in the current proposal,
1073 // but after recent comments on es-discuss, is most likely obsolete.
1074 /*
1075 var defineObj = FromGenericPropertyDescriptor(desc);
1076 var names = ObjectGetOwnPropertyNames(attributes);
1077 var standardNames =
1078 {value: 0, writable: 0, get: 0, set: 0, enumerable: 0, configurable: 0};
1079 for (var i = 0; i < names.length; i++) {
1080 var N = names[i];
1081 if (!(%HasLocalProperty(standardNames, N))) {
1082 var attr = GetOwnProperty(attributes, N);
1083 DefineOwnProperty(descObj, N, attr, true);
1084 }
1085 }
1086 // This is really confusing the types, but it is what the proxies spec
1087 // currently requires:
1088 desc = descObj;
1089 */
1090 } else {
1091 var desc = ToPropertyDescriptor(attributes);
1092 DefineOwnProperty(obj, name, desc, true);
1093 }
Andrei Popescu31002712010-02-23 13:46:05 +00001094 return obj;
1095}
1096
1097
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001098function GetOwnEnumerablePropertyNames(properties) {
1099 var names = new InternalArray();
1100 for (var key in properties) {
1101 if (%HasLocalProperty(properties, key)) {
1102 names.push(key);
1103 }
1104 }
1105 return names;
1106}
1107
1108
Andrei Popescu31002712010-02-23 13:46:05 +00001109// ES5 section 15.2.3.7.
Leon Clarkee46be812010-01-19 14:06:41 +00001110function ObjectDefineProperties(obj, properties) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001111 if (!IS_SPEC_OBJECT(obj)) {
Andrei Popescu31002712010-02-23 13:46:05 +00001112 throw MakeTypeError("obj_ctor_property_non_object", ["defineProperties"]);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001113 }
Leon Clarkee46be812010-01-19 14:06:41 +00001114 var props = ToObject(properties);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001115 var names = GetOwnEnumerablePropertyNames(props);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001116 var descriptors = new InternalArray();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001117 for (var i = 0; i < names.length; i++) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001118 descriptors.push(ToPropertyDescriptor(props[names[i]]));
1119 }
1120 for (var i = 0; i < names.length; i++) {
1121 DefineOwnProperty(obj, names[i], descriptors[i], true);
Leon Clarkee46be812010-01-19 14:06:41 +00001122 }
Andrei Popescu31002712010-02-23 13:46:05 +00001123 return obj;
Leon Clarkee46be812010-01-19 14:06:41 +00001124}
1125
1126
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001127// Harmony proxies.
1128function ProxyFix(obj) {
1129 var handler = %GetHandler(obj);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001130 var props = CallTrap0(handler, "fix", void 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001131 if (IS_UNDEFINED(props)) {
1132 throw MakeTypeError("handler_returned_undefined", [handler, "fix"]);
1133 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001134
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001135 if (%IsJSFunctionProxy(obj)) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001136 var callTrap = %GetCallTrap(obj);
1137 var constructTrap = %GetConstructTrap(obj);
1138 var code = DelegateCallAndConstruct(callTrap, constructTrap);
1139 %Fix(obj); // becomes a regular function
1140 %SetCode(obj, code);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001141 // TODO(rossberg): What about length and other properties? Not specified.
1142 // We just put in some half-reasonable defaults for now.
1143 var prototype = new $Object();
1144 $Object.defineProperty(prototype, "constructor",
1145 {value: obj, writable: true, enumerable: false, configurable: true});
1146 // TODO(v8:1530): defineProperty does not handle prototype and length.
1147 %FunctionSetPrototype(obj, prototype);
1148 obj.length = 0;
Ben Murdoch589d6972011-11-30 16:04:58 +00001149 } else {
1150 %Fix(obj);
1151 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001152 ObjectDefineProperties(obj, props);
1153}
1154
1155
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001156// ES5 section 15.2.3.8.
1157function ObjectSeal(obj) {
1158 if (!IS_SPEC_OBJECT(obj)) {
1159 throw MakeTypeError("obj_ctor_property_non_object", ["seal"]);
1160 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001161 if (%IsJSProxy(obj)) {
1162 ProxyFix(obj);
1163 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001164 var names = ObjectGetOwnPropertyNames(obj);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001165 for (var i = 0; i < names.length; i++) {
1166 var name = names[i];
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001167 var desc = GetOwnProperty(obj, name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001168 if (desc.isConfigurable()) {
1169 desc.setConfigurable(false);
1170 DefineOwnProperty(obj, name, desc, true);
1171 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001172 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001173 %PreventExtensions(obj);
1174 return obj;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001175}
1176
1177
1178// ES5 section 15.2.3.9.
1179function ObjectFreeze(obj) {
1180 if (!IS_SPEC_OBJECT(obj)) {
1181 throw MakeTypeError("obj_ctor_property_non_object", ["freeze"]);
1182 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001183 if (%IsJSProxy(obj)) {
1184 ProxyFix(obj);
1185 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001186 var names = ObjectGetOwnPropertyNames(obj);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001187 for (var i = 0; i < names.length; i++) {
1188 var name = names[i];
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001189 var desc = GetOwnProperty(obj, name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001190 if (desc.isWritable() || desc.isConfigurable()) {
1191 if (IsDataDescriptor(desc)) desc.setWritable(false);
1192 desc.setConfigurable(false);
1193 DefineOwnProperty(obj, name, desc, true);
1194 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001195 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001196 %PreventExtensions(obj);
1197 return obj;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001198}
1199
1200
Steve Block8defd9f2010-07-08 12:39:36 +01001201// ES5 section 15.2.3.10
1202function ObjectPreventExtension(obj) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001203 if (!IS_SPEC_OBJECT(obj)) {
Steve Block8defd9f2010-07-08 12:39:36 +01001204 throw MakeTypeError("obj_ctor_property_non_object", ["preventExtension"]);
1205 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001206 if (%IsJSProxy(obj)) {
1207 ProxyFix(obj);
1208 }
Steve Block8defd9f2010-07-08 12:39:36 +01001209 %PreventExtensions(obj);
1210 return obj;
1211}
1212
1213
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001214// ES5 section 15.2.3.11
1215function ObjectIsSealed(obj) {
1216 if (!IS_SPEC_OBJECT(obj)) {
1217 throw MakeTypeError("obj_ctor_property_non_object", ["isSealed"]);
1218 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001219 if (%IsJSProxy(obj)) {
1220 return false;
1221 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001222 var names = ObjectGetOwnPropertyNames(obj);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001223 for (var i = 0; i < names.length; i++) {
1224 var name = names[i];
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001225 var desc = GetOwnProperty(obj, name);
1226 if (desc.isConfigurable()) return false;
1227 }
1228 if (!ObjectIsExtensible(obj)) {
1229 return true;
1230 }
1231 return false;
1232}
1233
1234
1235// ES5 section 15.2.3.12
1236function ObjectIsFrozen(obj) {
1237 if (!IS_SPEC_OBJECT(obj)) {
1238 throw MakeTypeError("obj_ctor_property_non_object", ["isFrozen"]);
1239 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001240 if (%IsJSProxy(obj)) {
1241 return false;
1242 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001243 var names = ObjectGetOwnPropertyNames(obj);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001244 for (var i = 0; i < names.length; i++) {
1245 var name = names[i];
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001246 var desc = GetOwnProperty(obj, name);
1247 if (IsDataDescriptor(desc) && desc.isWritable()) return false;
1248 if (desc.isConfigurable()) return false;
1249 }
1250 if (!ObjectIsExtensible(obj)) {
1251 return true;
1252 }
1253 return false;
1254}
1255
1256
Steve Block8defd9f2010-07-08 12:39:36 +01001257// ES5 section 15.2.3.13
1258function ObjectIsExtensible(obj) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001259 if (!IS_SPEC_OBJECT(obj)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001260 throw MakeTypeError("obj_ctor_property_non_object", ["isExtensible"]);
1261 }
1262 if (%IsJSProxy(obj)) {
1263 return true;
Steve Block8defd9f2010-07-08 12:39:36 +01001264 }
1265 return %IsExtensible(obj);
1266}
1267
1268
Steve Blocka7e24c12009-10-30 11:49:00 +00001269%SetCode($Object, function(x) {
1270 if (%_IsConstructCall()) {
1271 if (x == null) return this;
1272 return ToObject(x);
1273 } else {
1274 if (x == null) return { };
1275 return ToObject(x);
1276 }
1277});
1278
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001279%SetExpectedNumberOfProperties($Object, 4);
Steve Blocka7e24c12009-10-30 11:49:00 +00001280
1281// ----------------------------------------------------------------------------
Ben Murdoch589d6972011-11-30 16:04:58 +00001282// Object
Steve Blocka7e24c12009-10-30 11:49:00 +00001283
Ben Murdoch589d6972011-11-30 16:04:58 +00001284function SetUpObject() {
1285 %CheckIsBootstrapping();
1286 // Set Up non-enumerable functions on the Object.prototype object.
Steve Blocka7e24c12009-10-30 11:49:00 +00001287 InstallFunctions($Object.prototype, DONT_ENUM, $Array(
1288 "toString", ObjectToString,
1289 "toLocaleString", ObjectToLocaleString,
1290 "valueOf", ObjectValueOf,
1291 "hasOwnProperty", ObjectHasOwnProperty,
1292 "isPrototypeOf", ObjectIsPrototypeOf,
1293 "propertyIsEnumerable", ObjectPropertyIsEnumerable,
1294 "__defineGetter__", ObjectDefineGetter,
1295 "__lookupGetter__", ObjectLookupGetter,
1296 "__defineSetter__", ObjectDefineSetter,
1297 "__lookupSetter__", ObjectLookupSetter
1298 ));
1299 InstallFunctions($Object, DONT_ENUM, $Array(
Leon Clarkee46be812010-01-19 14:06:41 +00001300 "keys", ObjectKeys,
1301 "create", ObjectCreate,
Andrei Popescu31002712010-02-23 13:46:05 +00001302 "defineProperty", ObjectDefineProperty,
1303 "defineProperties", ObjectDefineProperties,
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001304 "freeze", ObjectFreeze,
Leon Clarkee46be812010-01-19 14:06:41 +00001305 "getPrototypeOf", ObjectGetPrototypeOf,
1306 "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor,
Steve Block8defd9f2010-07-08 12:39:36 +01001307 "getOwnPropertyNames", ObjectGetOwnPropertyNames,
1308 "isExtensible", ObjectIsExtensible,
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001309 "isFrozen", ObjectIsFrozen,
1310 "isSealed", ObjectIsSealed,
1311 "preventExtensions", ObjectPreventExtension,
1312 "seal", ObjectSeal
Steve Blocka7e24c12009-10-30 11:49:00 +00001313 ));
1314}
1315
Ben Murdoch589d6972011-11-30 16:04:58 +00001316SetUpObject();
Steve Blocka7e24c12009-10-30 11:49:00 +00001317
1318// ----------------------------------------------------------------------------
1319// Boolean
1320
1321function BooleanToString() {
1322 // NOTE: Both Boolean objects and values can enter here as
1323 // 'this'. This is not as dictated by ECMA-262.
Ben Murdoch086aeea2011-05-13 15:57:08 +01001324 var b = this;
1325 if (!IS_BOOLEAN(b)) {
1326 if (!IS_BOOLEAN_WRAPPER(b)) {
1327 throw new $TypeError('Boolean.prototype.toString is not generic');
1328 }
1329 b = %_ValueOf(b);
1330 }
1331 return b ? 'true' : 'false';
Steve Blocka7e24c12009-10-30 11:49:00 +00001332}
1333
1334
1335function BooleanValueOf() {
1336 // NOTE: Both Boolean objects and values can enter here as
1337 // 'this'. This is not as dictated by ECMA-262.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001338 if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 throw new $TypeError('Boolean.prototype.valueOf is not generic');
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001340 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001341 return %_ValueOf(this);
1342}
1343
1344
Steve Blocka7e24c12009-10-30 11:49:00 +00001345// ----------------------------------------------------------------------------
1346
1347
Ben Murdoch589d6972011-11-30 16:04:58 +00001348function SetUpBoolean () {
1349 %CheckIsBootstrapping();
Steve Blocka7e24c12009-10-30 11:49:00 +00001350 InstallFunctions($Boolean.prototype, DONT_ENUM, $Array(
1351 "toString", BooleanToString,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001352 "valueOf", BooleanValueOf
Steve Blocka7e24c12009-10-30 11:49:00 +00001353 ));
1354}
1355
Ben Murdoch589d6972011-11-30 16:04:58 +00001356SetUpBoolean();
1357
Steve Blocka7e24c12009-10-30 11:49:00 +00001358
1359// ----------------------------------------------------------------------------
1360// Number
1361
1362// Set the Number function and constructor.
1363%SetCode($Number, function(x) {
1364 var value = %_ArgumentsLength() == 0 ? 0 : ToNumber(x);
1365 if (%_IsConstructCall()) {
1366 %_SetValueOf(this, value);
1367 } else {
1368 return value;
1369 }
1370});
1371
1372%FunctionSetPrototype($Number, new $Number(0));
1373
1374// ECMA-262 section 15.7.4.2.
1375function NumberToString(radix) {
1376 // NOTE: Both Number objects and values can enter here as
1377 // 'this'. This is not as dictated by ECMA-262.
1378 var number = this;
1379 if (!IS_NUMBER(this)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001380 if (!IS_NUMBER_WRAPPER(this)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001381 throw new $TypeError('Number.prototype.toString is not generic');
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001382 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001383 // Get the value of this number in case it's an object.
1384 number = %_ValueOf(this);
1385 }
1386 // Fast case: Convert number in radix 10.
1387 if (IS_UNDEFINED(radix) || radix === 10) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01001388 return %_NumberToString(number);
Steve Blocka7e24c12009-10-30 11:49:00 +00001389 }
1390
1391 // Convert the radix to an integer and check the range.
1392 radix = TO_INTEGER(radix);
1393 if (radix < 2 || radix > 36) {
1394 throw new $RangeError('toString() radix argument must be between 2 and 36');
1395 }
1396 // Convert the number to a string in the given radix.
1397 return %NumberToRadixString(number, radix);
1398}
1399
1400
1401// ECMA-262 section 15.7.4.3
1402function NumberToLocaleString() {
Ben Murdoch257744e2011-11-30 15:57:28 +00001403 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1404 throw MakeTypeError("called_on_null_or_undefined",
1405 ["Number.prototype.toLocaleString"]);
1406 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001407 return this.toString();
1408}
1409
1410
1411// ECMA-262 section 15.7.4.4
1412function NumberValueOf() {
1413 // NOTE: Both Number objects and values can enter here as
1414 // 'this'. This is not as dictated by ECMA-262.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001415 if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 throw new $TypeError('Number.prototype.valueOf is not generic');
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001417 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 return %_ValueOf(this);
1419}
1420
1421
1422// ECMA-262 section 15.7.4.5
1423function NumberToFixed(fractionDigits) {
1424 var f = TO_INTEGER(fractionDigits);
1425 if (f < 0 || f > 20) {
1426 throw new $RangeError("toFixed() digits argument must be between 0 and 20");
1427 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001428 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1429 throw MakeTypeError("called_on_null_or_undefined",
1430 ["Number.prototype.toFixed"]);
1431 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001432 var x = ToNumber(this);
1433 return %NumberToFixed(x, f);
1434}
1435
1436
1437// ECMA-262 section 15.7.4.6
1438function NumberToExponential(fractionDigits) {
1439 var f = -1;
1440 if (!IS_UNDEFINED(fractionDigits)) {
1441 f = TO_INTEGER(fractionDigits);
1442 if (f < 0 || f > 20) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001443 throw new $RangeError(
1444 "toExponential() argument must be between 0 and 20");
Steve Blocka7e24c12009-10-30 11:49:00 +00001445 }
1446 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001447 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1448 throw MakeTypeError("called_on_null_or_undefined",
1449 ["Number.prototype.toExponential"]);
1450 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001451 var x = ToNumber(this);
1452 return %NumberToExponential(x, f);
1453}
1454
1455
1456// ECMA-262 section 15.7.4.7
1457function NumberToPrecision(precision) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001458 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
1459 throw MakeTypeError("called_on_null_or_undefined",
1460 ["Number.prototype.toPrecision"]);
1461 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001462 if (IS_UNDEFINED(precision)) return ToString(%_ValueOf(this));
1463 var p = TO_INTEGER(precision);
1464 if (p < 1 || p > 21) {
1465 throw new $RangeError("toPrecision() argument must be between 1 and 21");
1466 }
1467 var x = ToNumber(this);
1468 return %NumberToPrecision(x, p);
1469}
1470
1471
Steve Blocka7e24c12009-10-30 11:49:00 +00001472// ----------------------------------------------------------------------------
1473
Ben Murdoch589d6972011-11-30 16:04:58 +00001474function SetUpNumber() {
1475 %CheckIsBootstrapping();
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 %OptimizeObjectForAddingMultipleProperties($Number.prototype, 8);
Ben Murdoch589d6972011-11-30 16:04:58 +00001477 // Set up the constructor property on the Number prototype object.
Steve Blocka7e24c12009-10-30 11:49:00 +00001478 %SetProperty($Number.prototype, "constructor", $Number, DONT_ENUM);
1479
1480 %OptimizeObjectForAddingMultipleProperties($Number, 5);
1481 // ECMA-262 section 15.7.3.1.
1482 %SetProperty($Number,
1483 "MAX_VALUE",
1484 1.7976931348623157e+308,
1485 DONT_ENUM | DONT_DELETE | READ_ONLY);
1486
1487 // ECMA-262 section 15.7.3.2.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001488 %SetProperty($Number, "MIN_VALUE", 5e-324,
1489 DONT_ENUM | DONT_DELETE | READ_ONLY);
Steve Blocka7e24c12009-10-30 11:49:00 +00001490
1491 // ECMA-262 section 15.7.3.3.
1492 %SetProperty($Number, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY);
1493
1494 // ECMA-262 section 15.7.3.4.
1495 %SetProperty($Number,
1496 "NEGATIVE_INFINITY",
1497 -1/0,
1498 DONT_ENUM | DONT_DELETE | READ_ONLY);
1499
1500 // ECMA-262 section 15.7.3.5.
1501 %SetProperty($Number,
1502 "POSITIVE_INFINITY",
1503 1/0,
1504 DONT_ENUM | DONT_DELETE | READ_ONLY);
Andrei Popescu402d9372010-02-26 13:31:12 +00001505 %ToFastProperties($Number);
Steve Blocka7e24c12009-10-30 11:49:00 +00001506
Ben Murdoch589d6972011-11-30 16:04:58 +00001507 // Set up non-enumerable functions on the Number prototype object.
Steve Blocka7e24c12009-10-30 11:49:00 +00001508 InstallFunctions($Number.prototype, DONT_ENUM, $Array(
1509 "toString", NumberToString,
1510 "toLocaleString", NumberToLocaleString,
1511 "valueOf", NumberValueOf,
1512 "toFixed", NumberToFixed,
1513 "toExponential", NumberToExponential,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001514 "toPrecision", NumberToPrecision
Steve Blocka7e24c12009-10-30 11:49:00 +00001515 ));
1516}
1517
Ben Murdoch589d6972011-11-30 16:04:58 +00001518SetUpNumber();
Steve Blocka7e24c12009-10-30 11:49:00 +00001519
1520
Steve Blocka7e24c12009-10-30 11:49:00 +00001521// ----------------------------------------------------------------------------
1522// Function
1523
1524$Function.prototype.constructor = $Function;
1525
1526function FunctionSourceString(func) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001527 while (%IsJSFunctionProxy(func)) {
1528 func = %GetCallTrap(func);
1529 }
1530
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 if (!IS_FUNCTION(func)) {
1532 throw new $TypeError('Function.prototype.toString is not generic');
1533 }
1534
1535 var source = %FunctionGetSourceCode(func);
1536 if (!IS_STRING(source) || %FunctionIsBuiltin(func)) {
1537 var name = %FunctionGetName(func);
1538 if (name) {
1539 // Mimic what KJS does.
1540 return 'function ' + name + '() { [native code] }';
1541 } else {
1542 return 'function () { [native code] }';
1543 }
1544 }
1545
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001546 var name = %FunctionNameShouldPrintAsAnonymous(func)
1547 ? 'anonymous'
1548 : %FunctionGetName(func);
Steve Blocka7e24c12009-10-30 11:49:00 +00001549 return 'function ' + name + source;
1550}
1551
1552
1553function FunctionToString() {
1554 return FunctionSourceString(this);
1555}
1556
1557
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001558// ES5 15.3.4.5
1559function FunctionBind(this_arg) { // Length is 1.
Ben Murdoch589d6972011-11-30 16:04:58 +00001560 if (!IS_SPEC_FUNCTION(this)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001561 throw new $TypeError('Bind must be called on a function');
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001562 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001563 var boundFunction = function () {
1564 // Poison .arguments and .caller, but is otherwise not detectable.
1565 "use strict";
1566 // This function must not use any object literals (Object, Array, RegExp),
1567 // since the literals-array is being used to store the bound data.
1568 if (%_IsConstructCall()) {
1569 return %NewObjectFromBound(boundFunction);
Ben Murdochf87a2032010-10-22 12:50:53 +01001570 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001571 var bindings = %BoundFunctionGetBindings(boundFunction);
Steve Block1e0659c2011-05-24 12:43:12 +01001572
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001573 var argc = %_ArgumentsLength();
1574 if (argc == 0) {
1575 return %Apply(bindings[0], bindings[1], bindings, 2, bindings.length - 2);
1576 }
1577 if (bindings.length === 2) {
1578 return %Apply(bindings[0], bindings[1], arguments, 0, argc);
1579 }
1580 var bound_argc = bindings.length - 2;
1581 var argv = new InternalArray(bound_argc + argc);
1582 for (var i = 0; i < bound_argc; i++) {
1583 argv[i] = bindings[i + 2];
1584 }
1585 for (var j = 0; j < argc; j++) {
1586 argv[i++] = %_Arguments(j);
1587 }
1588 return %Apply(bindings[0], bindings[1], argv, 0, bound_argc + argc);
1589 };
Steve Block1e0659c2011-05-24 12:43:12 +01001590
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001591 %FunctionRemovePrototype(boundFunction);
1592 var new_length = 0;
1593 if (%_ClassOf(this) == "Function") {
1594 // Function or FunctionProxy.
1595 var old_length = this.length;
1596 // FunctionProxies might provide a non-UInt32 value. If so, ignore it.
1597 if ((typeof old_length === "number") &&
1598 ((old_length >>> 0) === old_length)) {
Steve Block1e0659c2011-05-24 12:43:12 +01001599 var argc = %_ArgumentsLength();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001600 if (argc > 0) argc--; // Don't count the thisArg as parameter.
1601 new_length = old_length - argc;
1602 if (new_length < 0) new_length = 0;
1603 }
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001604 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001605 // This runtime function finds any remaining arguments on the stack,
1606 // so we don't pass the arguments object.
1607 var result = %FunctionBindArguments(boundFunction, this,
1608 this_arg, new_length);
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001609
1610 // We already have caller and arguments properties on functions,
1611 // which are non-configurable. It therefore makes no sence to
1612 // try to redefine these as defined by the spec. The spec says
1613 // that bind should make these throw a TypeError if get or set
1614 // is called and make them non-enumerable and non-configurable.
Ben Murdochf87a2032010-10-22 12:50:53 +01001615 // To be consistent with our normal functions we leave this as it is.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001616 // TODO(lrn): Do set these to be thrower.
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001617 return result;
1618}
1619
1620
Steve Blocka7e24c12009-10-30 11:49:00 +00001621function NewFunction(arg1) { // length == 1
1622 var n = %_ArgumentsLength();
1623 var p = '';
1624 if (n > 1) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001625 p = new InternalArray(n - 1);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001626 for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i);
1627 p = Join(p, n - 1, ',', NonStringToString);
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 // If the formal parameters string include ) - an illegal
1629 // character - it may make the combined function expression
1630 // compile. We avoid this problem by checking for this early on.
1631 if (p.indexOf(')') != -1) throw MakeSyntaxError('unable_to_parse',[]);
1632 }
1633 var body = (n > 0) ? ToString(%_Arguments(n - 1)) : '';
1634 var source = '(function(' + p + ') {\n' + body + '\n})';
1635
1636 // The call to SetNewFunctionAttributes will ensure the prototype
1637 // property of the resulting function is enumerable (ECMA262, 15.3.5.2).
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001638 var f = %CompileString(source)();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001639 %FunctionMarkNameShouldPrintAsAnonymous(f);
Steve Blocka7e24c12009-10-30 11:49:00 +00001640 return %SetNewFunctionAttributes(f);
1641}
1642
1643%SetCode($Function, NewFunction);
1644
1645// ----------------------------------------------------------------------------
1646
Ben Murdoch589d6972011-11-30 16:04:58 +00001647function SetUpFunction() {
1648 %CheckIsBootstrapping();
Steve Blocka7e24c12009-10-30 11:49:00 +00001649 InstallFunctions($Function.prototype, DONT_ENUM, $Array(
Kristian Monsen50ef84f2010-07-29 15:18:00 +01001650 "bind", FunctionBind,
Steve Blocka7e24c12009-10-30 11:49:00 +00001651 "toString", FunctionToString
1652 ));
1653}
1654
Ben Murdoch589d6972011-11-30 16:04:58 +00001655SetUpFunction();