blob: a6a0b4df91409106c74769998c7e65398f2a18d9 [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
19var FLAG_harmony_species;
20var GlobalArray = global.Array;
21var GlobalBoolean = global.Boolean;
22var GlobalString = global.String;
23var MakeRangeError;
24var MakeTypeError;
25var speciesSymbol;
26
27utils.Import(function(from) {
28 MakeRangeError = from.MakeRangeError;
29 MakeTypeError = from.MakeTypeError;
30 speciesSymbol = from.species_symbol;
31});
32
33utils.ImportFromExperimental(function(from) {
34 FLAG_harmony_species = from.FLAG_harmony_species;
35});
36
37// ----------------------------------------------------------------------------
38
Ben Murdoch097c5b22016-05-18 11:27:45 +010039
40/* ---------------------------------
41 - - - U t i l i t i e s - - -
42 ---------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043*/
44
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046function ToPositiveInteger(x, rangeErrorIndex) {
47 var i = TO_INTEGER_MAP_MINUS_ZERO(x);
48 if (i < 0) throw MakeRangeError(rangeErrorIndex);
49 return i;
50}
51
52
53function MaxSimple(a, b) {
54 return a > b ? a : b;
55}
56
57
58function MinSimple(a, b) {
59 return a > b ? b : a;
60}
61
62
63%SetForceInlineFlag(MaxSimple);
64%SetForceInlineFlag(MinSimple);
65
66
67// ES2015 7.3.20
68// For the fallback with --harmony-species off, there are two possible choices:
69// - "conservative": return defaultConstructor
70// - "not conservative": return object.constructor
71// This fallback path is only needed in the transition to ES2015, and the
72// choice is made simply to preserve the previous behavior so that we don't
73// have a three-step upgrade: old behavior, unspecified intermediate behavior,
74// and ES2015.
75// In some cases, we were "conservative" (e.g., ArrayBuffer, RegExp), and in
76// other cases we were "not conservative (e.g., TypedArray, Promise).
77function SpeciesConstructor(object, defaultConstructor, conservative) {
78 if (FLAG_harmony_species) {
79 var constructor = object.constructor;
80 if (IS_UNDEFINED(constructor)) {
81 return defaultConstructor;
82 }
83 if (!IS_RECEIVER(constructor)) {
84 throw MakeTypeError(kConstructorNotReceiver);
85 }
86 var species = constructor[speciesSymbol];
87 if (IS_NULL_OR_UNDEFINED(species)) {
88 return defaultConstructor;
89 }
90 if (%IsConstructor(species)) {
91 return species;
92 }
93 throw MakeTypeError(kSpeciesNotConstructor);
94 } else {
95 return conservative ? defaultConstructor : object.constructor;
96 }
97}
98
99//----------------------------------------------------------------------------
100
101// NOTE: Setting the prototype for Array must take place as early as
102// possible due to code generation for array literals. When
103// generating code for a array literal a boilerplate array is created
104// that is cloned when running the code. It is essential that the
105// boilerplate gets the right prototype.
106%FunctionSetPrototype(GlobalArray, new GlobalArray(0));
107
108// ----------------------------------------------------------------------------
109// Exports
110
111utils.Export(function(to) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 to.MaxSimple = MaxSimple;
113 to.MinSimple = MinSimple;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 to.ToPositiveInteger = ToPositiveInteger;
115 to.SpeciesConstructor = SpeciesConstructor;
116});
117
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118})