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