blob: 7a61094da62d4bcd07b10d4a0ebb12fa7a0d809f [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 +000045function ConcatIterableToArray(target, iterable) {
46 var index = target.length;
47 for (var element of iterable) {
48 AddIndexedProperty(target, index++, element);
49 }
50 return target;
51}
52
53
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054// This function should be called rather than %AddElement in contexts where the
55// argument might not be less than 2**32-1. ES2015 ToLength semantics mean that
56// this is a concern at basically all callsites.
57function AddIndexedProperty(obj, index, value) {
58 if (index === TO_UINT32(index) && index !== kMaxUint32) {
59 %AddElement(obj, index, value);
60 } else {
61 %AddNamedProperty(obj, TO_STRING(index), value, NONE);
62 }
63}
64%SetForceInlineFlag(AddIndexedProperty);
65
66
67function ToPositiveInteger(x, rangeErrorIndex) {
68 var i = TO_INTEGER_MAP_MINUS_ZERO(x);
69 if (i < 0) throw MakeRangeError(rangeErrorIndex);
70 return i;
71}
72
73
74function MaxSimple(a, b) {
75 return a > b ? a : b;
76}
77
78
79function MinSimple(a, b) {
80 return a > b ? b : a;
81}
82
83
84%SetForceInlineFlag(MaxSimple);
85%SetForceInlineFlag(MinSimple);
86
87
88// ES2015 7.3.20
89// For the fallback with --harmony-species off, there are two possible choices:
90// - "conservative": return defaultConstructor
91// - "not conservative": return object.constructor
92// This fallback path is only needed in the transition to ES2015, and the
93// choice is made simply to preserve the previous behavior so that we don't
94// have a three-step upgrade: old behavior, unspecified intermediate behavior,
95// and ES2015.
96// In some cases, we were "conservative" (e.g., ArrayBuffer, RegExp), and in
97// other cases we were "not conservative (e.g., TypedArray, Promise).
98function SpeciesConstructor(object, defaultConstructor, conservative) {
99 if (FLAG_harmony_species) {
100 var constructor = object.constructor;
101 if (IS_UNDEFINED(constructor)) {
102 return defaultConstructor;
103 }
104 if (!IS_RECEIVER(constructor)) {
105 throw MakeTypeError(kConstructorNotReceiver);
106 }
107 var species = constructor[speciesSymbol];
108 if (IS_NULL_OR_UNDEFINED(species)) {
109 return defaultConstructor;
110 }
111 if (%IsConstructor(species)) {
112 return species;
113 }
114 throw MakeTypeError(kSpeciesNotConstructor);
115 } else {
116 return conservative ? defaultConstructor : object.constructor;
117 }
118}
119
120//----------------------------------------------------------------------------
121
122// NOTE: Setting the prototype for Array must take place as early as
123// possible due to code generation for array literals. When
124// generating code for a array literal a boilerplate array is created
125// that is cloned when running the code. It is essential that the
126// boilerplate gets the right prototype.
127%FunctionSetPrototype(GlobalArray, new GlobalArray(0));
128
129// ----------------------------------------------------------------------------
130// Exports
131
132utils.Export(function(to) {
133 to.AddIndexedProperty = AddIndexedProperty;
134 to.MaxSimple = MaxSimple;
135 to.MinSimple = MinSimple;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 to.ToPositiveInteger = ToPositiveInteger;
137 to.SpeciesConstructor = SpeciesConstructor;
138});
139
140%InstallToContext([
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 "concat_iterable_to_array", ConcatIterableToArray,
142]);
143
144})