blob: 216685fe4f2692eba560e0be9aedd03a919b6c01 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2006-2008 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// This files contains runtime support implemented in JavaScript.
6
7// CAUTION: Some of the functions specified in this file are called
8// directly from compiled code. These are the functions with names in
9// ALL CAPS. The compiled code passes the first argument in 'this'.
10
11
12// The following declarations are shared with other native JS files.
13// They are all declared at this one spot to avoid redeclaration errors.
14
15(function(global, utils) {
16
17%CheckIsBootstrapping();
18
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019var GlobalArray = global.Array;
20var GlobalBoolean = global.Boolean;
21var GlobalString = global.String;
22var MakeRangeError;
23var MakeTypeError;
24var speciesSymbol;
25
26utils.Import(function(from) {
27 MakeRangeError = from.MakeRangeError;
28 MakeTypeError = from.MakeTypeError;
29 speciesSymbol = from.species_symbol;
30});
31
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032// ----------------------------------------------------------------------------
33
Ben Murdoch097c5b22016-05-18 11:27:45 +010034
35/* ---------------------------------
36 - - - U t i l i t i e s - - -
37 ---------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038*/
39
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041function ToPositiveInteger(x, rangeErrorIndex) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010042 var i = TO_INTEGER(x) + 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 if (i < 0) throw MakeRangeError(rangeErrorIndex);
44 return i;
45}
46
47
48function MaxSimple(a, b) {
49 return a > b ? a : b;
50}
51
52
53function MinSimple(a, b) {
54 return a > b ? b : a;
55}
56
57
58%SetForceInlineFlag(MaxSimple);
59%SetForceInlineFlag(MinSimple);
60
61
62// ES2015 7.3.20
Ben Murdoch61f157c2016-09-16 13:49:30 +010063function SpeciesConstructor(object, defaultConstructor) {
64 var constructor = object.constructor;
65 if (IS_UNDEFINED(constructor)) {
66 return defaultConstructor;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 }
Ben Murdoch61f157c2016-09-16 13:49:30 +010068 if (!IS_RECEIVER(constructor)) {
69 throw MakeTypeError(kConstructorNotReceiver);
70 }
71 var species = constructor[speciesSymbol];
72 if (IS_NULL_OR_UNDEFINED(species)) {
73 return defaultConstructor;
74 }
75 if (%IsConstructor(species)) {
76 return species;
77 }
78 throw MakeTypeError(kSpeciesNotConstructor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079}
80
81//----------------------------------------------------------------------------
82
83// NOTE: Setting the prototype for Array must take place as early as
84// possible due to code generation for array literals. When
85// generating code for a array literal a boilerplate array is created
86// that is cloned when running the code. It is essential that the
87// boilerplate gets the right prototype.
88%FunctionSetPrototype(GlobalArray, new GlobalArray(0));
89
90// ----------------------------------------------------------------------------
91// Exports
92
93utils.Export(function(to) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 to.MaxSimple = MaxSimple;
95 to.MinSimple = MinSimple;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 to.ToPositiveInteger = ToPositiveInteger;
97 to.SpeciesConstructor = SpeciesConstructor;
98});
99
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100})