blob: 24d5e7cd362b8888d7fdaa663e8592684133ccbe [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000028// This file relies on the fact that the following declarations have been made
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000029//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000030// in runtime.js:
31// const $Object = global.Object;
32// const $Boolean = global.Boolean;
33// const $Number = global.Number;
34// const $Function = global.Function;
35// const $Array = global.Array;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000036// const $NaN = 0/0;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000037//
38// in math.js:
39// const $floor = MathFloor
40
41const $isNaN = GlobalIsNaN;
42const $isFinite = GlobalIsFinite;
43
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +000044
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000045// ----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
47
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000048// Helper function used to install functions on objects.
49function InstallFunctions(object, attributes, functions) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000050 if (functions.length >= 8) {
51 %OptimizeObjectForAddingMultipleProperties(object, functions.length >> 1);
52 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000053 for (var i = 0; i < functions.length; i += 2) {
54 var key = functions[i];
55 var f = functions[i + 1];
56 %FunctionSetName(f, key);
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000057 %FunctionRemovePrototype(f);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000058 %SetProperty(object, key, f, attributes);
59 }
ager@chromium.org5c838252010-02-19 08:53:10 +000060 %ToFastProperties(object);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000061}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062
ager@chromium.org9085a012009-05-11 19:22:57 +000063// Emulates JSC by installing functions on a hidden prototype that
64// lies above the current object/prototype. This lets you override
65// functions on String.prototype etc. and then restore the old function
66// with delete. See http://code.google.com/p/chromium/issues/detail?id=1717
67function InstallFunctionsOnHiddenPrototype(object, attributes, functions) {
68 var hidden_prototype = new $Object();
69 %SetHiddenPrototype(object, hidden_prototype);
70 InstallFunctions(hidden_prototype, attributes, functions);
71}
72
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000074// ----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
76
77// ECMA 262 - 15.1.4
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000078function GlobalIsNaN(number) {
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000079 var n = ToNumber(number);
80 return NUMBER_IS_NAN(n);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000081}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000082
83
84// ECMA 262 - 15.1.5
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000085function GlobalIsFinite(number) {
lrn@chromium.org25156de2010-04-06 13:10:27 +000086 if (!IS_NUMBER(number)) number = ToNumber(number);
87
88 // NaN - NaN == NaN, Infinity - Infinity == NaN, -Infinity - -Infinity == NaN.
89 return %_IsSmi(number) || number - number == 0;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000090}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
92
93// ECMA-262 - 15.1.2.2
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000094function GlobalParseInt(string, radix) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +000095 if (IS_UNDEFINED(radix)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 // Some people use parseInt instead of Math.floor. This
97 // optimization makes parseInt on a Smi 12 times faster (60ns
98 // vs 800ns). The following optimization makes parseInt on a
99 // non-Smi number 9 times faster (230ns vs 2070ns). Together
100 // they make parseInt on a string 1.4% slower (274ns vs 270ns).
101 if (%_IsSmi(string)) return string;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000102 if (IS_NUMBER(string) &&
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000103 ((0.01 < string && string < 1e9) ||
104 (-1e9 < string && string < -0.01))) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000105 // Truncate number.
106 return string | 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000108 radix = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 } else {
110 radix = TO_INT32(radix);
111 if (!(radix == 0 || (2 <= radix && radix <= 36)))
112 return $NaN;
113 }
114 return %StringParseInt(ToString(string), radix);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000115}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
117
118// ECMA-262 - 15.1.2.3
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000119function GlobalParseFloat(string) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000120 return %StringParseFloat(ToString(string));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000121}
122
123
124function GlobalEval(x) {
125 if (!IS_STRING(x)) return x;
126
ager@chromium.orge2902be2009-06-08 12:21:35 +0000127 var global_receiver = %GlobalReceiver(global);
128 var this_is_global_receiver = (this === global_receiver);
129 var global_is_detached = (global === global_receiver);
130
131 if (!this_is_global_receiver || global_is_detached) {
132 throw new $EvalError('The "this" object passed to eval must ' +
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000133 'be the global object from which eval originated');
134 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000135
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000136 var f = %CompileString(x, false);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000137 if (!IS_FUNCTION(f)) return f;
138
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000139 return f.call(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000140}
141
142
143// execScript for IE compatibility.
144function GlobalExecScript(expr, lang) {
145 // NOTE: We don't care about the character casing.
146 if (!lang || /javascript/i.test(lang)) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000147 var f = %CompileString(ToString(expr), false);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000148 f.call(%GlobalReceiver(global));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000149 }
150 return null;
151}
152
153
154// ----------------------------------------------------------------------------
155
156
157function SetupGlobal() {
158 // ECMA 262 - 15.1.1.1.
159 %SetProperty(global, "NaN", $NaN, DONT_ENUM | DONT_DELETE);
160
161 // ECMA-262 - 15.1.1.2.
162 %SetProperty(global, "Infinity", 1/0, DONT_ENUM | DONT_DELETE);
163
164 // ECMA-262 - 15.1.1.3.
165 %SetProperty(global, "undefined", void 0, DONT_ENUM | DONT_DELETE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000166
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000167 // Setup non-enumerable function on the global object.
168 InstallFunctions(global, DONT_ENUM, $Array(
169 "isNaN", GlobalIsNaN,
170 "isFinite", GlobalIsFinite,
171 "parseInt", GlobalParseInt,
172 "parseFloat", GlobalParseFloat,
173 "eval", GlobalEval,
174 "execScript", GlobalExecScript
175 ));
176}
177
178SetupGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179
180
181// ----------------------------------------------------------------------------
182// Boolean (first part of definition)
183
184
185%SetCode($Boolean, function(x) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000186 if (%_IsConstructCall()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 %_SetValueOf(this, ToBoolean(x));
188 } else {
189 return ToBoolean(x);
190 }
191});
192
193%FunctionSetPrototype($Boolean, new $Boolean(false));
194
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000195%SetProperty($Boolean.prototype, "constructor", $Boolean, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196
197// ----------------------------------------------------------------------------
198// Object
199
200$Object.prototype.constructor = $Object;
201
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000202// ECMA-262 - 15.2.4.2
203function ObjectToString() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000204 return "[object " + %_ClassOf(ToObject(this)) + "]";
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000205}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
207
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000208// ECMA-262 - 15.2.4.3
209function ObjectToLocaleString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 return this.toString();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000211}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212
213
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000214// ECMA-262 - 15.2.4.4
215function ObjectValueOf() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000216 return ToObject(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000217}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218
219
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000220// ECMA-262 - 15.2.4.5
221function ObjectHasOwnProperty(V) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 return %HasLocalProperty(ToObject(this), ToString(V));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000223}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224
225
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000226// ECMA-262 - 15.2.4.6
227function ObjectIsPrototypeOf(V) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000228 if (!IS_SPEC_OBJECT_OR_NULL(V) && !IS_UNDETECTABLE(V)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 return %IsInPrototypeChain(this, V);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000230}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231
232
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000233// ECMA-262 - 15.2.4.6
234function ObjectPropertyIsEnumerable(V) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 if (this == null) return false;
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000236 if (!IS_SPEC_OBJECT_OR_NULL(this)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 return %IsPropertyEnumerable(this, ToString(V));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000238}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239
240
241// Extensions for providing property getters and setters.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000242function ObjectDefineGetter(name, fun) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000243 if (this == null && !IS_UNDETECTABLE(this)) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000244 throw new $TypeError('Object.prototype.__defineGetter__: this is Null');
245 }
246 if (!IS_FUNCTION(fun)) {
247 throw new $TypeError('Object.prototype.__defineGetter__: Expecting function');
248 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 return %DefineAccessor(ToObject(this), ToString(name), GETTER, fun);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
252
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000253function ObjectLookupGetter(name) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000254 if (this == null && !IS_UNDETECTABLE(this)) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000255 throw new $TypeError('Object.prototype.__lookupGetter__: this is Null');
256 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257 return %LookupAccessor(ToObject(this), ToString(name), GETTER);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000258}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259
260
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000261function ObjectDefineSetter(name, fun) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000262 if (this == null && !IS_UNDETECTABLE(this)) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000263 throw new $TypeError('Object.prototype.__defineSetter__: this is Null');
264 }
265 if (!IS_FUNCTION(fun)) {
266 throw new $TypeError(
267 'Object.prototype.__defineSetter__: Expecting function');
268 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 return %DefineAccessor(ToObject(this), ToString(name), SETTER, fun);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000270}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271
272
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000273function ObjectLookupSetter(name) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000274 if (this == null && !IS_UNDETECTABLE(this)) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000275 throw new $TypeError('Object.prototype.__lookupSetter__: this is Null');
276 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 return %LookupAccessor(ToObject(this), ToString(name), SETTER);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000278}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
280
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000281function ObjectKeys(obj) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000282 if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) &&
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000283 !IS_UNDETECTABLE(obj))
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000284 throw MakeTypeError("obj_ctor_property_non_object", ["keys"]);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000285 return %LocalKeys(obj);
286}
287
288
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000289// ES5 8.10.1.
290function IsAccessorDescriptor(desc) {
291 if (IS_UNDEFINED(desc)) return false;
292 return desc.hasGetter_ || desc.hasSetter_;
293}
294
295
296// ES5 8.10.2.
297function IsDataDescriptor(desc) {
298 if (IS_UNDEFINED(desc)) return false;
299 return desc.hasValue_ || desc.hasWritable_;
300}
301
302
303// ES5 8.10.3.
304function IsGenericDescriptor(desc) {
305 return !(IsAccessorDescriptor(desc) || IsDataDescriptor(desc));
306}
307
308
309function IsInconsistentDescriptor(desc) {
310 return IsAccessorDescriptor(desc) && IsDataDescriptor(desc);
311}
312
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000313// ES5 8.10.4
314function FromPropertyDescriptor(desc) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000315 if (IS_UNDEFINED(desc)) return desc;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000316 var obj = new $Object();
317 if (IsDataDescriptor(desc)) {
318 obj.value = desc.getValue();
319 obj.writable = desc.isWritable();
320 }
321 if (IsAccessorDescriptor(desc)) {
322 obj.get = desc.getGet();
323 obj.set = desc.getSet();
324 }
325 obj.enumerable = desc.isEnumerable();
326 obj.configurable = desc.isConfigurable();
327 return obj;
328}
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000329
330// ES5 8.10.5.
331function ToPropertyDescriptor(obj) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000332 if (!IS_SPEC_OBJECT_OR_NULL(obj)) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000333 throw MakeTypeError("property_desc_object", [obj]);
334 }
335 var desc = new PropertyDescriptor();
336
337 if ("enumerable" in obj) {
338 desc.setEnumerable(ToBoolean(obj.enumerable));
339 }
340
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000341 if ("configurable" in obj) {
342 desc.setConfigurable(ToBoolean(obj.configurable));
343 }
344
345 if ("value" in obj) {
346 desc.setValue(obj.value);
347 }
348
349 if ("writable" in obj) {
350 desc.setWritable(ToBoolean(obj.writable));
351 }
352
353 if ("get" in obj) {
354 var get = obj.get;
355 if (!IS_UNDEFINED(get) && !IS_FUNCTION(get)) {
356 throw MakeTypeError("getter_must_be_callable", [get]);
357 }
358 desc.setGet(get);
359 }
360
361 if ("set" in obj) {
362 var set = obj.set;
363 if (!IS_UNDEFINED(set) && !IS_FUNCTION(set)) {
364 throw MakeTypeError("setter_must_be_callable", [set]);
365 }
366 desc.setSet(set);
367 }
368
369 if (IsInconsistentDescriptor(desc)) {
370 throw MakeTypeError("value_and_accessor", [obj]);
371 }
372 return desc;
373}
374
375
376function PropertyDescriptor() {
377 // Initialize here so they are all in-object and have the same map.
378 // Default values from ES5 8.6.1.
379 this.value_ = void 0;
380 this.hasValue_ = false;
381 this.writable_ = false;
382 this.hasWritable_ = false;
383 this.enumerable_ = false;
ager@chromium.org5c838252010-02-19 08:53:10 +0000384 this.hasEnumerable_ = false;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000385 this.configurable_ = false;
ager@chromium.org5c838252010-02-19 08:53:10 +0000386 this.hasConfigurable_ = false;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000387 this.get_ = void 0;
388 this.hasGetter_ = false;
389 this.set_ = void 0;
390 this.hasSetter_ = false;
391}
392
393
394PropertyDescriptor.prototype.setValue = function(value) {
395 this.value_ = value;
396 this.hasValue_ = true;
397}
398
399
400PropertyDescriptor.prototype.getValue = function() {
401 return this.value_;
402}
403
404
ager@chromium.org5c838252010-02-19 08:53:10 +0000405PropertyDescriptor.prototype.hasValue = function() {
406 return this.hasValue_;
407}
408
409
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000410PropertyDescriptor.prototype.setEnumerable = function(enumerable) {
411 this.enumerable_ = enumerable;
ager@chromium.org5c838252010-02-19 08:53:10 +0000412 this.hasEnumerable_ = true;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000413}
414
415
416PropertyDescriptor.prototype.isEnumerable = function () {
417 return this.enumerable_;
418}
419
420
ager@chromium.org5c838252010-02-19 08:53:10 +0000421PropertyDescriptor.prototype.hasEnumerable = function() {
422 return this.hasEnumerable_;
423}
424
425
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000426PropertyDescriptor.prototype.setWritable = function(writable) {
427 this.writable_ = writable;
428 this.hasWritable_ = true;
429}
430
431
432PropertyDescriptor.prototype.isWritable = function() {
433 return this.writable_;
434}
435
436
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000437PropertyDescriptor.prototype.hasWritable = function() {
438 return this.hasWritable_;
439}
440
441
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000442PropertyDescriptor.prototype.setConfigurable = function(configurable) {
443 this.configurable_ = configurable;
ager@chromium.org5c838252010-02-19 08:53:10 +0000444 this.hasConfigurable_ = true;
445}
446
447
448PropertyDescriptor.prototype.hasConfigurable = function() {
449 return this.hasConfigurable_;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000450}
451
452
453PropertyDescriptor.prototype.isConfigurable = function() {
454 return this.configurable_;
455}
456
457
458PropertyDescriptor.prototype.setGet = function(get) {
459 this.get_ = get;
460 this.hasGetter_ = true;
461}
462
463
464PropertyDescriptor.prototype.getGet = function() {
465 return this.get_;
466}
467
468
ager@chromium.org5c838252010-02-19 08:53:10 +0000469PropertyDescriptor.prototype.hasGetter = function() {
470 return this.hasGetter_;
471}
472
473
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000474PropertyDescriptor.prototype.setSet = function(set) {
475 this.set_ = set;
476 this.hasSetter_ = true;
477}
478
479
480PropertyDescriptor.prototype.getSet = function() {
481 return this.set_;
482}
483
484
ager@chromium.org5c838252010-02-19 08:53:10 +0000485PropertyDescriptor.prototype.hasSetter = function() {
486 return this.hasSetter_;
487}
488
489
490
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000491// ES5 section 8.12.1.
492function GetOwnProperty(obj, p) {
493 var desc = new PropertyDescriptor();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000494
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000495 // GetOwnProperty returns an array indexed by the constants
496 // defined in macros.py.
497 // If p is not a property on obj undefined is returned.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000498 var props = %GetOwnProperty(ToObject(obj), ToString(p));
499
ager@chromium.org5c838252010-02-19 08:53:10 +0000500 if (IS_UNDEFINED(props)) return void 0;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000501
502 // This is an accessor
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000503 if (props[IS_ACCESSOR_INDEX]) {
504 desc.setGet(props[GETTER_INDEX]);
505 desc.setSet(props[SETTER_INDEX]);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000506 } else {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000507 desc.setValue(props[VALUE_INDEX]);
508 desc.setWritable(props[WRITABLE_INDEX]);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000509 }
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000510 desc.setEnumerable(props[ENUMERABLE_INDEX]);
511 desc.setConfigurable(props[CONFIGURABLE_INDEX]);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000512
513 return desc;
514}
515
516
ager@chromium.org5c838252010-02-19 08:53:10 +0000517// ES5 section 8.12.2.
518function GetProperty(obj, p) {
519 var prop = GetOwnProperty(obj);
520 if (!IS_UNDEFINED(prop)) return prop;
521 var proto = obj.__proto__;
522 if (IS_NULL(proto)) return void 0;
523 return GetProperty(proto, p);
524}
525
526
527// ES5 section 8.12.6
528function HasProperty(obj, p) {
529 var desc = GetProperty(obj, p);
530 return IS_UNDEFINED(desc) ? false : true;
531}
532
533
lrn@chromium.org25156de2010-04-06 13:10:27 +0000534// ES5 8.12.9.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000535function DefineOwnProperty(obj, p, desc, should_throw) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000536 var current = GetOwnProperty(obj, p);
537 var extensible = %IsExtensible(ToObject(obj));
538
539 // Error handling according to spec.
540 // Step 3
541 if (IS_UNDEFINED(current) && !extensible)
542 throw MakeTypeError("define_disallowed", ["defineProperty"]);
543
544 if (!IS_UNDEFINED(current) && !current.isConfigurable()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000545 // Step 5 and 6
546 if ((!desc.hasEnumerable() ||
547 SameValue(desc.isEnumerable() && current.isEnumerable())) &&
548 (!desc.hasConfigurable() ||
549 SameValue(desc.isConfigurable(), current.isConfigurable())) &&
550 (!desc.hasWritable() ||
551 SameValue(desc.isWritable(), current.isWritable())) &&
552 (!desc.hasValue() ||
553 SameValue(desc.getValue(), current.getValue())) &&
554 (!desc.hasGetter() ||
555 SameValue(desc.getGet(), current.getGet())) &&
556 (!desc.hasSetter() ||
557 SameValue(desc.getSet(), current.getSet()))) {
558 return true;
559 }
560
ager@chromium.org5c838252010-02-19 08:53:10 +0000561 // Step 7
562 if (desc.isConfigurable() || desc.isEnumerable() != current.isEnumerable())
563 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
564 // Step 9
565 if (IsDataDescriptor(current) != IsDataDescriptor(desc))
566 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
567 // Step 10
568 if (IsDataDescriptor(current) && IsDataDescriptor(desc)) {
569 if (!current.isWritable() && desc.isWritable())
570 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
571 if (!current.isWritable() && desc.hasValue() &&
572 !SameValue(desc.getValue(), current.getValue())) {
573 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
574 }
575 }
576 // Step 11
577 if (IsAccessorDescriptor(desc) && IsAccessorDescriptor(current)) {
578 if (desc.hasSetter() && !SameValue(desc.getSet(), current.getSet())){
579 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
580 }
581 if (desc.hasGetter() && !SameValue(desc.getGet(),current.getGet()))
582 throw MakeTypeError("redefine_disallowed", ["defineProperty"]);
583 }
584 }
585
lrn@chromium.org25156de2010-04-06 13:10:27 +0000586 // Send flags - enumerable and configurable are common - writable is
ager@chromium.org5c838252010-02-19 08:53:10 +0000587 // only send to the data descriptor.
588 // Take special care if enumerable and configurable is not defined on
589 // desc (we need to preserve the existing values from current).
590 var flag = NONE;
591 if (desc.hasEnumerable()) {
592 flag |= desc.isEnumerable() ? 0 : DONT_ENUM;
593 } else if (!IS_UNDEFINED(current)) {
594 flag |= current.isEnumerable() ? 0 : DONT_ENUM;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000595 } else {
ager@chromium.org5c838252010-02-19 08:53:10 +0000596 flag |= DONT_ENUM;
597 }
598
599 if (desc.hasConfigurable()) {
600 flag |= desc.isConfigurable() ? 0 : DONT_DELETE;
601 } else if (!IS_UNDEFINED(current)) {
602 flag |= current.isConfigurable() ? 0 : DONT_DELETE;
603 } else
604 flag |= DONT_DELETE;
605
606 if (IsDataDescriptor(desc) || IsGenericDescriptor(desc)) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000607 if (desc.hasWritable()) {
608 flag |= desc.isWritable() ? 0 : READ_ONLY;
609 } else if (!IS_UNDEFINED(current)) {
610 flag |= current.isWritable() ? 0 : READ_ONLY;
611 } else {
612 flag |= READ_ONLY;
613 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000614 %DefineOrRedefineDataProperty(obj, p, desc.getValue(), flag);
615 } else {
616 if (desc.hasGetter() && IS_FUNCTION(desc.getGet())) {
617 %DefineOrRedefineAccessorProperty(obj, p, GETTER, desc.getGet(), flag);
618 }
619 if (desc.hasSetter() && IS_FUNCTION(desc.getSet())) {
620 %DefineOrRedefineAccessorProperty(obj, p, SETTER, desc.getSet(), flag);
621 }
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000622 }
623 return true;
624}
625
626
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000627// ES5 section 15.2.3.2.
628function ObjectGetPrototypeOf(obj) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000629 if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) &&
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000630 !IS_UNDETECTABLE(obj))
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000631 throw MakeTypeError("obj_ctor_property_non_object", ["getPrototypeOf"]);
632 return obj.__proto__;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000633}
634
635
lrn@chromium.org25156de2010-04-06 13:10:27 +0000636// ES5 section 15.2.3.3
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000637function ObjectGetOwnPropertyDescriptor(obj, p) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000638 if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) &&
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000639 !IS_UNDETECTABLE(obj))
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000640 throw MakeTypeError("obj_ctor_property_non_object", ["getOwnPropertyDescriptor"]);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000641 var desc = GetOwnProperty(obj, p);
642 return FromPropertyDescriptor(desc);
643}
644
645
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000646// ES5 section 15.2.3.4.
647function ObjectGetOwnPropertyNames(obj) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000648 if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) &&
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000649 !IS_UNDETECTABLE(obj))
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000650 throw MakeTypeError("obj_ctor_property_non_object", ["getOwnPropertyNames"]);
651
652 // Find all the indexed properties.
653
654 // Get the local element names.
655 var propertyNames = %GetLocalElementNames(obj);
656
657 // Get names for indexed interceptor properties.
658 if (%GetInterceptorInfo(obj) & 1) {
659 var indexedInterceptorNames =
660 %GetIndexedInterceptorElementNames(obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000661 if (indexedInterceptorNames)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000662 propertyNames = propertyNames.concat(indexedInterceptorNames);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000663 }
664
665 // Find all the named properties.
666
667 // Get the local property names.
668 propertyNames = propertyNames.concat(%GetLocalPropertyNames(obj));
669
670 // Get names for named interceptor properties if any.
671
672 if (%GetInterceptorInfo(obj) & 2) {
673 var namedInterceptorNames =
674 %GetNamedInterceptorPropertyNames(obj);
675 if (namedInterceptorNames) {
676 propertyNames = propertyNames.concat(namedInterceptorNames);
677 }
678 }
679
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000680 // Property names are expected to be unique strings.
681 var propertySet = {};
682 var j = 0;
683 for (var i = 0; i < propertyNames.length; ++i) {
684 var name = ToString(propertyNames[i]);
685 // We need to check for the exact property value since for intrinsic
686 // properties like toString if(propertySet["toString"]) will always
687 // succeed.
688 if (propertySet[name] === true)
689 continue;
690 propertySet[name] = true;
691 propertyNames[j++] = name;
692 }
693 propertyNames.length = j;
ager@chromium.org5c838252010-02-19 08:53:10 +0000694
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000695 return propertyNames;
696}
697
698
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000699// ES5 section 15.2.3.5.
700function ObjectCreate(proto, properties) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000701 if (!IS_SPEC_OBJECT_OR_NULL(proto)) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000702 throw MakeTypeError("proto_object_or_null", [proto]);
703 }
704 var obj = new $Object();
705 obj.__proto__ = proto;
706 if (!IS_UNDEFINED(properties)) ObjectDefineProperties(obj, properties);
707 return obj;
708}
709
710
ager@chromium.org5c838252010-02-19 08:53:10 +0000711// ES5 section 15.2.3.6.
712function ObjectDefineProperty(obj, p, attributes) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000713 if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) &&
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000714 !IS_UNDETECTABLE(obj)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000715 throw MakeTypeError("obj_ctor_property_non_object", ["defineProperty"]);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000716 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000717 var name = ToString(p);
718 var desc = ToPropertyDescriptor(attributes);
719 DefineOwnProperty(obj, name, desc, true);
720 return obj;
721}
722
723
724// ES5 section 15.2.3.7.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000725function ObjectDefineProperties(obj, properties) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000726 if ((!IS_SPEC_OBJECT_OR_NULL(obj) || IS_NULL_OR_UNDEFINED(obj)) &&
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000727 !IS_UNDETECTABLE(obj))
ager@chromium.org5c838252010-02-19 08:53:10 +0000728 throw MakeTypeError("obj_ctor_property_non_object", ["defineProperties"]);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000729 var props = ToObject(properties);
730 var key_values = [];
731 for (var key in props) {
732 if (%HasLocalProperty(props, key)) {
733 key_values.push(key);
734 var value = props[key];
735 var desc = ToPropertyDescriptor(value);
736 key_values.push(desc);
737 }
738 }
739 for (var i = 0; i < key_values.length; i += 2) {
740 var key = key_values[i];
741 var desc = key_values[i + 1];
742 DefineOwnProperty(obj, key, desc, true);
743 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000744 return obj;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000745}
746
747
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748%SetCode($Object, function(x) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000749 if (%_IsConstructCall()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 if (x == null) return this;
751 return ToObject(x);
752 } else {
753 if (x == null) return { };
754 return ToObject(x);
755 }
756});
757
758
759// ----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760
761
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000762function SetupObject() {
763 // Setup non-enumerable functions on the Object.prototype object.
764 InstallFunctions($Object.prototype, DONT_ENUM, $Array(
765 "toString", ObjectToString,
766 "toLocaleString", ObjectToLocaleString,
767 "valueOf", ObjectValueOf,
768 "hasOwnProperty", ObjectHasOwnProperty,
769 "isPrototypeOf", ObjectIsPrototypeOf,
770 "propertyIsEnumerable", ObjectPropertyIsEnumerable,
771 "__defineGetter__", ObjectDefineGetter,
772 "__lookupGetter__", ObjectLookupGetter,
773 "__defineSetter__", ObjectDefineSetter,
774 "__lookupSetter__", ObjectLookupSetter
775 ));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000776 InstallFunctions($Object, DONT_ENUM, $Array(
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000777 "keys", ObjectKeys,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000778 "create", ObjectCreate,
ager@chromium.org5c838252010-02-19 08:53:10 +0000779 "defineProperty", ObjectDefineProperty,
780 "defineProperties", ObjectDefineProperties,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000781 "getPrototypeOf", ObjectGetPrototypeOf,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000782 "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor,
783 "getOwnPropertyNames", ObjectGetOwnPropertyNames
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000784 ));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000785}
786
787SetupObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788
789
790// ----------------------------------------------------------------------------
791// Boolean
792
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000793function BooleanToString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000794 // NOTE: Both Boolean objects and values can enter here as
795 // 'this'. This is not as dictated by ECMA-262.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000796 if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 throw new $TypeError('Boolean.prototype.toString is not generic');
798 return ToString(%_ValueOf(this));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000799}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800
801
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000802function BooleanValueOf() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 // NOTE: Both Boolean objects and values can enter here as
804 // 'this'. This is not as dictated by ECMA-262.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000805 if (!IS_BOOLEAN(this) && !IS_BOOLEAN_WRAPPER(this))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806 throw new $TypeError('Boolean.prototype.valueOf is not generic');
807 return %_ValueOf(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000808}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000809
810
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000811function BooleanToJSON(key) {
812 return CheckJSONPrimitive(this.valueOf());
813}
814
815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816// ----------------------------------------------------------------------------
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000817
818
819function SetupBoolean() {
820 InstallFunctions($Boolean.prototype, DONT_ENUM, $Array(
821 "toString", BooleanToString,
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000822 "valueOf", BooleanValueOf,
823 "toJSON", BooleanToJSON
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000824 ));
825}
826
827SetupBoolean();
828
829// ----------------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830// Number
831
832// Set the Number function and constructor.
833%SetCode($Number, function(x) {
834 var value = %_ArgumentsLength() == 0 ? 0 : ToNumber(x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000835 if (%_IsConstructCall()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000836 %_SetValueOf(this, value);
837 } else {
838 return value;
839 }
840});
841
842%FunctionSetPrototype($Number, new $Number(0));
843
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844// ECMA-262 section 15.7.4.2.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000845function NumberToString(radix) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 // NOTE: Both Number objects and values can enter here as
847 // 'this'. This is not as dictated by ECMA-262.
848 var number = this;
849 if (!IS_NUMBER(this)) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000850 if (!IS_NUMBER_WRAPPER(this))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 throw new $TypeError('Number.prototype.toString is not generic');
852 // Get the value of this number in case it's an object.
853 number = %_ValueOf(this);
854 }
855 // Fast case: Convert number in radix 10.
856 if (IS_UNDEFINED(radix) || radix === 10) {
857 return ToString(number);
858 }
859
860 // Convert the radix to an integer and check the range.
861 radix = TO_INTEGER(radix);
862 if (radix < 2 || radix > 36) {
863 throw new $RangeError('toString() radix argument must be between 2 and 36');
864 }
865 // Convert the number to a string in the given radix.
866 return %NumberToRadixString(number, radix);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000867}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868
869
870// ECMA-262 section 15.7.4.3
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000871function NumberToLocaleString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 return this.toString();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000873}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875
876// ECMA-262 section 15.7.4.4
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000877function NumberValueOf() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 // NOTE: Both Number objects and values can enter here as
879 // 'this'. This is not as dictated by ECMA-262.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000880 if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this))
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 throw new $TypeError('Number.prototype.valueOf is not generic');
882 return %_ValueOf(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000883}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884
885
886// ECMA-262 section 15.7.4.5
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000887function NumberToFixed(fractionDigits) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 var f = TO_INTEGER(fractionDigits);
889 if (f < 0 || f > 20) {
890 throw new $RangeError("toFixed() digits argument must be between 0 and 20");
891 }
892 var x = ToNumber(this);
893 return %NumberToFixed(x, f);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000894}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
896
897// ECMA-262 section 15.7.4.6
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000898function NumberToExponential(fractionDigits) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899 var f = -1;
900 if (!IS_UNDEFINED(fractionDigits)) {
901 f = TO_INTEGER(fractionDigits);
902 if (f < 0 || f > 20) {
903 throw new $RangeError("toExponential() argument must be between 0 and 20");
904 }
905 }
906 var x = ToNumber(this);
907 return %NumberToExponential(x, f);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000908}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000909
910
911// ECMA-262 section 15.7.4.7
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000912function NumberToPrecision(precision) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 if (IS_UNDEFINED(precision)) return ToString(%_ValueOf(this));
914 var p = TO_INTEGER(precision);
915 if (p < 1 || p > 21) {
916 throw new $RangeError("toPrecision() argument must be between 1 and 21");
917 }
918 var x = ToNumber(this);
919 return %NumberToPrecision(x, p);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000920}
921
922
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000923function CheckJSONPrimitive(val) {
924 if (!IsPrimitive(val))
925 throw MakeTypeError('result_not_primitive', ['toJSON', val]);
926 return val;
927}
928
929
930function NumberToJSON(key) {
931 return CheckJSONPrimitive(this.valueOf());
932}
933
934
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000935// ----------------------------------------------------------------------------
936
937function SetupNumber() {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000938 %OptimizeObjectForAddingMultipleProperties($Number.prototype, 8);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000939 // Setup the constructor property on the Number prototype object.
940 %SetProperty($Number.prototype, "constructor", $Number, DONT_ENUM);
941
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000942 %OptimizeObjectForAddingMultipleProperties($Number, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000943 // ECMA-262 section 15.7.3.1.
944 %SetProperty($Number,
945 "MAX_VALUE",
946 1.7976931348623157e+308,
947 DONT_ENUM | DONT_DELETE | READ_ONLY);
948
949 // ECMA-262 section 15.7.3.2.
950 %SetProperty($Number, "MIN_VALUE", 5e-324, DONT_ENUM | DONT_DELETE | READ_ONLY);
951
952 // ECMA-262 section 15.7.3.3.
953 %SetProperty($Number, "NaN", $NaN, DONT_ENUM | DONT_DELETE | READ_ONLY);
954
955 // ECMA-262 section 15.7.3.4.
956 %SetProperty($Number,
957 "NEGATIVE_INFINITY",
958 -1/0,
959 DONT_ENUM | DONT_DELETE | READ_ONLY);
960
961 // ECMA-262 section 15.7.3.5.
962 %SetProperty($Number,
963 "POSITIVE_INFINITY",
964 1/0,
965 DONT_ENUM | DONT_DELETE | READ_ONLY);
ager@chromium.org5c838252010-02-19 08:53:10 +0000966 %ToFastProperties($Number);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000967
968 // Setup non-enumerable functions on the Number prototype object.
969 InstallFunctions($Number.prototype, DONT_ENUM, $Array(
970 "toString", NumberToString,
971 "toLocaleString", NumberToLocaleString,
972 "valueOf", NumberValueOf,
973 "toFixed", NumberToFixed,
974 "toExponential", NumberToExponential,
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000975 "toPrecision", NumberToPrecision,
976 "toJSON", NumberToJSON
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000977 ));
978}
979
980SetupNumber();
981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982
983
984// ----------------------------------------------------------------------------
985// Function
986
987$Function.prototype.constructor = $Function;
988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989function FunctionSourceString(func) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000990 if (!IS_FUNCTION(func)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000991 throw new $TypeError('Function.prototype.toString is not generic');
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000992 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993
994 var source = %FunctionGetSourceCode(func);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000995 if (!IS_STRING(source) || %FunctionIsBuiltin(func)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 var name = %FunctionGetName(func);
997 if (name) {
998 // Mimic what KJS does.
999 return 'function ' + name + '() { [native code] }';
1000 } else {
1001 return 'function () { [native code] }';
1002 }
1003 }
1004
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 var name = %FunctionGetName(func);
1006 return 'function ' + name + source;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001007}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008
1009
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001010function FunctionToString() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 return FunctionSourceString(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001012}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013
1014
1015function NewFunction(arg1) { // length == 1
1016 var n = %_ArgumentsLength();
1017 var p = '';
1018 if (n > 1) {
1019 p = new $Array(n - 1);
1020 // Explicitly convert all parameters to strings.
1021 // Array.prototype.join replaces null with empty strings which is
1022 // not appropriate.
1023 for (var i = 0; i < n - 1; i++) p[i] = ToString(%_Arguments(i));
1024 p = p.join(',');
1025 // If the formal parameters string include ) - an illegal
1026 // character - it may make the combined function expression
1027 // compile. We avoid this problem by checking for this early on.
1028 if (p.indexOf(')') != -1) throw MakeSyntaxError('unable_to_parse',[]);
1029 }
1030 var body = (n > 0) ? ToString(%_Arguments(n - 1)) : '';
ager@chromium.org236ad962008-09-25 09:45:57 +00001031 var source = '(function(' + p + ') {\n' + body + '\n})';
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032
1033 // The call to SetNewFunctionAttributes will ensure the prototype
1034 // property of the resulting function is enumerable (ECMA262, 15.3.5.2).
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001035 var f = %CompileString(source, false)();
ager@chromium.org236ad962008-09-25 09:45:57 +00001036 %FunctionSetName(f, "anonymous");
1037 return %SetNewFunctionAttributes(f);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001038}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039
1040%SetCode($Function, NewFunction);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001041
1042// ----------------------------------------------------------------------------
1043
1044function SetupFunction() {
1045 InstallFunctions($Function.prototype, DONT_ENUM, $Array(
1046 "toString", FunctionToString
1047 ));
1048}
1049
1050SetupFunction();