blob: 44be9419766b49424e1c522af9cb82c7032c4ff7 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5(function(global, utils) {
6
7%CheckIsBootstrapping();
8
9// ----------------------------------------------------------------------------
10// Imports
11
12var GlobalArray = global.Array;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013var GlobalNumber = global.Number;
14var GlobalObject = global.Object;
15var InternalArray = utils.InternalArray;
16var iteratorSymbol = utils.ImportNow("iterator_symbol");
17var MakeRangeError;
18var MakeSyntaxError;
19var MakeTypeError;
20var MathAbs;
21var NaN = %GetRootNaN();
22var ObjectToString = utils.ImportNow("object_to_string");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
24
25utils.Import(function(from) {
26 MakeRangeError = from.MakeRangeError;
27 MakeSyntaxError = from.MakeSyntaxError;
28 MakeTypeError = from.MakeTypeError;
29 MathAbs = from.MathAbs;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030});
31
32// ----------------------------------------------------------------------------
33
34
35// ES6 18.2.3 isNaN(number)
36function GlobalIsNaN(number) {
37 number = TO_NUMBER(number);
38 return NUMBER_IS_NAN(number);
39}
40
41
42// ES6 18.2.2 isFinite(number)
43function GlobalIsFinite(number) {
44 number = TO_NUMBER(number);
45 return NUMBER_IS_FINITE(number);
46}
47
48
49// ES6 18.2.5 parseInt(string, radix)
50function GlobalParseInt(string, radix) {
51 if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) {
52 // Some people use parseInt instead of Math.floor. This
53 // optimization makes parseInt on a Smi 12 times faster (60ns
54 // vs 800ns). The following optimization makes parseInt on a
55 // non-Smi number 9 times faster (230ns vs 2070ns). Together
56 // they make parseInt on a string 1.4% slower (274ns vs 270ns).
57 if (%_IsSmi(string)) return string;
58 if (IS_NUMBER(string) &&
59 ((0.01 < string && string < 1e9) ||
60 (-1e9 < string && string < -0.01))) {
61 // Truncate number.
62 return string | 0;
63 }
64 string = TO_STRING(string);
65 radix = radix | 0;
66 } else {
67 // The spec says ToString should be evaluated before ToInt32.
68 string = TO_STRING(string);
69 radix = TO_INT32(radix);
70 if (!(radix == 0 || (2 <= radix && radix <= 36))) {
71 return NaN;
72 }
73 }
74
75 if (%_HasCachedArrayIndex(string) &&
76 (radix == 0 || radix == 10)) {
77 return %_GetCachedArrayIndex(string);
78 }
79 return %StringParseInt(string, radix);
80}
81
82
83// ES6 18.2.4 parseFloat(string)
84function GlobalParseFloat(string) {
85 // 1. Let inputString be ? ToString(string).
86 string = TO_STRING(string);
87 if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string);
88 return %StringParseFloat(string);
89}
90
91
92// ----------------------------------------------------------------------------
93
94// Set up global object.
95var attributes = DONT_ENUM | DONT_DELETE | READ_ONLY;
96
97utils.InstallConstants(global, [
98 // ES6 18.1.1
99 "Infinity", INFINITY,
100 // ES6 18.1.2
101 "NaN", NaN,
102 // ES6 18.1.3
103 "undefined", UNDEFINED,
104]);
105
106// Set up non-enumerable function on the global object.
107utils.InstallFunctions(global, DONT_ENUM, [
108 "isNaN", GlobalIsNaN,
109 "isFinite", GlobalIsFinite,
110 "parseInt", GlobalParseInt,
111 "parseFloat", GlobalParseFloat,
112]);
113
114
115// ----------------------------------------------------------------------------
116// Object
117
118// ES6 19.1.3.5 Object.prototype.toLocaleString([reserved1 [,reserved2]])
119function ObjectToLocaleString() {
120 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.toLocaleString");
121 return this.toString();
122}
123
124
125// ES6 19.1.3.7 Object.prototype.valueOf()
126function ObjectValueOf() {
127 return TO_OBJECT(this);
128}
129
130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131// ES6 19.1.3.3 Object.prototype.isPrototypeOf(V)
132function ObjectIsPrototypeOf(V) {
133 if (!IS_RECEIVER(V)) return false;
134 var O = TO_OBJECT(this);
135 return %HasInPrototypeChain(V, O);
136}
137
138
139// ES6 19.1.3.4
140function ObjectPropertyIsEnumerable(V) {
141 var P = TO_NAME(V);
142 return %PropertyIsEnumerable(TO_OBJECT(this), P);
143}
144
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145// ES6 7.3.9
146function GetMethod(obj, p) {
147 var func = obj[p];
148 if (IS_NULL_OR_UNDEFINED(func)) return UNDEFINED;
149 if (IS_CALLABLE(func)) return func;
150 throw MakeTypeError(kCalledNonCallable, typeof func);
151}
152
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000153// ES6 section 19.1.2.18.
154function ObjectSetPrototypeOf(obj, proto) {
155 CHECK_OBJECT_COERCIBLE(obj, "Object.setPrototypeOf");
156
157 if (proto !== null && !IS_RECEIVER(proto)) {
158 throw MakeTypeError(kProtoObjectOrNull, proto);
159 }
160
161 if (IS_RECEIVER(obj)) {
162 %SetPrototype(obj, proto);
163 }
164
165 return obj;
166}
167
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000168// ES6 B.2.2.1.1
169function ObjectGetProto() {
Ben Murdochc5610432016-08-08 18:44:38 +0100170 return %object_get_prototype_of(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171}
172
173
174// ES6 B.2.2.1.2
175function ObjectSetProto(proto) {
176 CHECK_OBJECT_COERCIBLE(this, "Object.prototype.__proto__");
177
178 if ((IS_RECEIVER(proto) || IS_NULL(proto)) && IS_RECEIVER(this)) {
179 %SetPrototype(this, proto);
180 }
181}
182
183
184// ES6 19.1.1.1
185function ObjectConstructor(x) {
186 if (GlobalObject != new.target && !IS_UNDEFINED(new.target)) {
187 return this;
188 }
189 if (IS_NULL(x) || IS_UNDEFINED(x)) return {};
190 return TO_OBJECT(x);
191}
192
193
194// ----------------------------------------------------------------------------
195// Object
196
197%SetNativeFlag(GlobalObject);
198%SetCode(GlobalObject, ObjectConstructor);
199
200%AddNamedProperty(GlobalObject.prototype, "constructor", GlobalObject,
201 DONT_ENUM);
202
203// Set up non-enumerable functions on the Object.prototype object.
204utils.InstallFunctions(GlobalObject.prototype, DONT_ENUM, [
205 "toString", ObjectToString,
206 "toLocaleString", ObjectToLocaleString,
207 "valueOf", ObjectValueOf,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 "isPrototypeOf", ObjectIsPrototypeOf,
209 "propertyIsEnumerable", ObjectPropertyIsEnumerable,
Ben Murdochc5610432016-08-08 18:44:38 +0100210 // __defineGetter__ is added in bootstrapper.cc.
211 // __lookupGetter__ is added in bootstrapper.cc.
212 // __defineSetter__ is added in bootstrapper.cc.
213 // __lookupSetter__ is added in bootstrapper.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000214]);
Ben Murdochc5610432016-08-08 18:44:38 +0100215utils.InstallGetterSetter(
216 GlobalObject.prototype, "__proto__", ObjectGetProto, ObjectSetProto);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217
218// Set up non-enumerable functions in the Object object.
219utils.InstallFunctions(GlobalObject, DONT_ENUM, [
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 "setPrototypeOf", ObjectSetPrototypeOf,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 // getOwnPropertySymbols is added in symbol.js.
Ben Murdochc5610432016-08-08 18:44:38 +0100222 // Others are added in bootstrapper.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223]);
224
225
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226
227// ----------------------------------------------------------------------------
228// Number
229
230// ES6 Number.prototype.toString([ radix ])
231function NumberToStringJS(radix) {
232 // NOTE: Both Number objects and values can enter here as
233 // 'this'. This is not as dictated by ECMA-262.
234 var number = this;
235 if (!IS_NUMBER(this)) {
236 if (!IS_NUMBER_WRAPPER(this)) {
237 throw MakeTypeError(kNotGeneric, 'Number.prototype.toString');
238 }
239 // Get the value of this number in case it's an object.
240 number = %_ValueOf(this);
241 }
242 // Fast case: Convert number in radix 10.
243 if (IS_UNDEFINED(radix) || radix === 10) {
244 return %_NumberToString(number);
245 }
246
247 // Convert the radix to an integer and check the range.
248 radix = TO_INTEGER(radix);
249 if (radix < 2 || radix > 36) throw MakeRangeError(kToRadixFormatRange);
250 // Convert the number to a string in the given radix.
251 return %NumberToRadixString(number, radix);
252}
253
254
255// ES6 20.1.3.4 Number.prototype.toLocaleString([reserved1 [, reserved2]])
256function NumberToLocaleString() {
257 return %_Call(NumberToStringJS, this);
258}
259
260
261// ES6 20.1.3.7 Number.prototype.valueOf()
262function NumberValueOf() {
263 // NOTE: Both Number objects and values can enter here as
264 // 'this'. This is not as dictated by ECMA-262.
265 if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) {
266 throw MakeTypeError(kNotGeneric, 'Number.prototype.valueOf');
267 }
268 return %_ValueOf(this);
269}
270
271
272// ES6 20.1.3.3 Number.prototype.toFixed(fractionDigits)
273function NumberToFixedJS(fractionDigits) {
274 var x = this;
275 if (!IS_NUMBER(this)) {
276 if (!IS_NUMBER_WRAPPER(this)) {
277 throw MakeTypeError(kIncompatibleMethodReceiver,
278 "Number.prototype.toFixed", this);
279 }
280 // Get the value of this number in case it's an object.
281 x = %_ValueOf(this);
282 }
283 var f = TO_INTEGER(fractionDigits);
284
285 if (f < 0 || f > 20) {
286 throw MakeRangeError(kNumberFormatRange, "toFixed() digits");
287 }
288
289 if (NUMBER_IS_NAN(x)) return "NaN";
290 if (x == INFINITY) return "Infinity";
291 if (x == -INFINITY) return "-Infinity";
292
293 return %NumberToFixed(x, f);
294}
295
296
297// ES6 20.1.3.2 Number.prototype.toExponential(fractionDigits)
298function NumberToExponentialJS(fractionDigits) {
299 var x = this;
300 if (!IS_NUMBER(this)) {
301 if (!IS_NUMBER_WRAPPER(this)) {
302 throw MakeTypeError(kIncompatibleMethodReceiver,
303 "Number.prototype.toExponential", this);
304 }
305 // Get the value of this number in case it's an object.
306 x = %_ValueOf(this);
307 }
308 var f = IS_UNDEFINED(fractionDigits) ? UNDEFINED : TO_INTEGER(fractionDigits);
309
310 if (NUMBER_IS_NAN(x)) return "NaN";
311 if (x == INFINITY) return "Infinity";
312 if (x == -INFINITY) return "-Infinity";
313
314 if (IS_UNDEFINED(f)) {
315 f = -1; // Signal for runtime function that f is not defined.
316 } else if (f < 0 || f > 20) {
317 throw MakeRangeError(kNumberFormatRange, "toExponential()");
318 }
319 return %NumberToExponential(x, f);
320}
321
322
323// ES6 20.1.3.5 Number.prototype.toPrecision(precision)
324function NumberToPrecisionJS(precision) {
325 var x = this;
326 if (!IS_NUMBER(this)) {
327 if (!IS_NUMBER_WRAPPER(this)) {
328 throw MakeTypeError(kIncompatibleMethodReceiver,
329 "Number.prototype.toPrecision", this);
330 }
331 // Get the value of this number in case it's an object.
332 x = %_ValueOf(this);
333 }
334 if (IS_UNDEFINED(precision)) return TO_STRING(x);
335 var p = TO_INTEGER(precision);
336
337 if (NUMBER_IS_NAN(x)) return "NaN";
338 if (x == INFINITY) return "Infinity";
339 if (x == -INFINITY) return "-Infinity";
340
341 if (p < 1 || p > 21) {
342 throw MakeRangeError(kToPrecisionFormatRange);
343 }
344 return %NumberToPrecision(x, p);
345}
346
347
348// Harmony isFinite.
349function NumberIsFinite(number) {
350 return IS_NUMBER(number) && NUMBER_IS_FINITE(number);
351}
352
353
354// Harmony isInteger
355function NumberIsInteger(number) {
356 return NumberIsFinite(number) && TO_INTEGER(number) == number;
357}
358
359
360// Harmony isNaN.
361function NumberIsNaN(number) {
362 return IS_NUMBER(number) && NUMBER_IS_NAN(number);
363}
364
365
366// Harmony isSafeInteger
367function NumberIsSafeInteger(number) {
368 if (NumberIsFinite(number)) {
369 var integral = TO_INTEGER(number);
370 if (integral == number) {
371 return MathAbs(integral) <= kMaxSafeInteger;
372 }
373 }
374 return false;
375}
376
377
378// ----------------------------------------------------------------------------
379
380%FunctionSetPrototype(GlobalNumber, new GlobalNumber(0));
381
382%OptimizeObjectForAddingMultipleProperties(GlobalNumber.prototype, 8);
383// Set up the constructor property on the Number prototype object.
384%AddNamedProperty(GlobalNumber.prototype, "constructor", GlobalNumber,
385 DONT_ENUM);
386
387utils.InstallConstants(GlobalNumber, [
388 // ECMA-262 section 15.7.3.1.
389 "MAX_VALUE", 1.7976931348623157e+308,
390 // ECMA-262 section 15.7.3.2.
391 "MIN_VALUE", 5e-324,
392 // ECMA-262 section 15.7.3.3.
393 "NaN", NaN,
394 // ECMA-262 section 15.7.3.4.
395 "NEGATIVE_INFINITY", -INFINITY,
396 // ECMA-262 section 15.7.3.5.
397 "POSITIVE_INFINITY", INFINITY,
398
399 // --- Harmony constants (no spec refs until settled.)
400
401 "MAX_SAFE_INTEGER", %_MathPow(2, 53) - 1,
402 "MIN_SAFE_INTEGER", -%_MathPow(2, 53) + 1,
403 "EPSILON", %_MathPow(2, -52)
404]);
405
406// Set up non-enumerable functions on the Number prototype object.
407utils.InstallFunctions(GlobalNumber.prototype, DONT_ENUM, [
408 "toString", NumberToStringJS,
409 "toLocaleString", NumberToLocaleString,
410 "valueOf", NumberValueOf,
411 "toFixed", NumberToFixedJS,
412 "toExponential", NumberToExponentialJS,
413 "toPrecision", NumberToPrecisionJS
414]);
415
416// Harmony Number constructor additions
417utils.InstallFunctions(GlobalNumber, DONT_ENUM, [
418 "isFinite", NumberIsFinite,
419 "isInteger", NumberIsInteger,
420 "isNaN", NumberIsNaN,
421 "isSafeInteger", NumberIsSafeInteger,
422 "parseInt", GlobalParseInt,
423 "parseFloat", GlobalParseFloat
424]);
425
426%SetForceInlineFlag(NumberIsNaN);
427
428
429// ----------------------------------------------------------------------------
430// Iterator related spec functions.
431
432// ES6 7.4.1 GetIterator(obj, method)
433function GetIterator(obj, method) {
434 if (IS_UNDEFINED(method)) {
435 method = obj[iteratorSymbol];
436 }
437 if (!IS_CALLABLE(method)) {
438 throw MakeTypeError(kNotIterable, obj);
439 }
440 var iterator = %_Call(method, obj);
441 if (!IS_RECEIVER(iterator)) {
442 throw MakeTypeError(kNotAnIterator, iterator);
443 }
444 return iterator;
445}
446
447// ----------------------------------------------------------------------------
448// Exports
449
450utils.Export(function(to) {
451 to.GetIterator = GetIterator;
452 to.GetMethod = GetMethod;
453 to.IsFinite = GlobalIsFinite;
454 to.IsNaN = GlobalIsNaN;
455 to.NumberIsNaN = NumberIsNaN;
Ben Murdochda12d292016-06-02 14:46:10 +0100456 to.NumberIsInteger = NumberIsInteger;
Ben Murdochda12d292016-06-02 14:46:10 +0100457 to.ObjectHasOwnProperty = GlobalObject.prototype.hasOwnProperty;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458});
459
460%InstallToContext([
461 "object_value_of", ObjectValueOf,
462]);
463
464})