1// 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
19var GlobalArray = global.Array;
20var GlobalBoolean = global.Boolean;
21var GlobalString = global.String;
22var speciesSymbol;
23
24utils.Import(function(from) {
25  speciesSymbol = from.species_symbol;
26});
27
28// ----------------------------------------------------------------------------
29
30
31/* ---------------------------------
32   - - -   U t i l i t i e s   - - -
33   ---------------------------------
34*/
35
36
37function ToPositiveInteger(x, rangeErrorIndex) {
38  var i = TO_INTEGER(x) + 0;
39  if (i < 0) throw %make_range_error(rangeErrorIndex);
40  return i;
41}
42
43
44function ToIndex(x, rangeErrorIndex) {
45  var i = TO_INTEGER(x) + 0;
46  if (i < 0 || i > kMaxSafeInteger) throw %make_range_error(rangeErrorIndex);
47  return i;
48}
49
50
51function MaxSimple(a, b) {
52  return a > b ? a : b;
53}
54
55
56function MinSimple(a, b) {
57  return a > b ? b : a;
58}
59
60
61%SetForceInlineFlag(MaxSimple);
62%SetForceInlineFlag(MinSimple);
63
64
65// ES2015 7.3.20
66function SpeciesConstructor(object, defaultConstructor) {
67  var constructor = object.constructor;
68  if (IS_UNDEFINED(constructor)) {
69    return defaultConstructor;
70  }
71  if (!IS_RECEIVER(constructor)) {
72    throw %make_type_error(kConstructorNotReceiver);
73  }
74  var species = constructor[speciesSymbol];
75  if (IS_NULL_OR_UNDEFINED(species)) {
76    return defaultConstructor;
77  }
78  if (%IsConstructor(species)) {
79    return species;
80  }
81  throw %make_type_error(kSpeciesNotConstructor);
82}
83
84//----------------------------------------------------------------------------
85
86// NOTE: Setting the prototype for Array must take place as early as
87// possible due to code generation for array literals.  When
88// generating code for a array literal a boilerplate array is created
89// that is cloned when running the code.  It is essential that the
90// boilerplate gets the right prototype.
91%FunctionSetPrototype(GlobalArray, new GlobalArray(0));
92
93// ----------------------------------------------------------------------------
94// Exports
95
96utils.Export(function(to) {
97  to.MaxSimple = MaxSimple;
98  to.MinSimple = MinSimple;
99  to.ToPositiveInteger = ToPositiveInteger;
100  to.ToIndex = ToIndex;
101  to.SpeciesConstructor = SpeciesConstructor;
102});
103
104})
105