93a5563c19ddaa7e0afcea891993afd722c753ba
[platform/upstream/nodejs.git] / deps / v8 / src / macros.py
1 # Copyright 2006-2009 the V8 project authors. All rights reserved.
2 # Redistribution and use in source and binary forms, with or without
3 # modification, are permitted provided that the following conditions are
4 # met:
5 #
6 #     * Redistributions of source code must retain the above copyright
7 #       notice, this list of conditions and the following disclaimer.
8 #     * Redistributions in binary form must reproduce the above
9 #       copyright notice, this list of conditions and the following
10 #       disclaimer in the documentation and/or other materials provided
11 #       with the distribution.
12 #     * Neither the name of Google Inc. nor the names of its
13 #       contributors may be used to endorse or promote products derived
14 #       from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 # Dictionary that is passed as defines for js2c.py.
29 # Used for defines that must be defined for all native JS files.
30
31 const NONE        = 0;
32 const READ_ONLY   = 1;
33 const DONT_ENUM   = 2;
34 const DONT_DELETE = 4;
35 const NEW_ONE_BYTE_STRING = true;
36 const NEW_TWO_BYTE_STRING = false;
37
38 # Constants used for getter and setter operations.
39 const GETTER = 0;
40 const SETTER = 1;
41
42 const NO_HINT     = 0;
43 const NUMBER_HINT = 1;
44 const STRING_HINT = 2;
45
46 # For date.js.
47 const HoursPerDay      = 24;
48 const MinutesPerHour   = 60;
49 const SecondsPerMinute = 60;
50 const msPerSecond      = 1000;
51 const msPerMinute      = 60000;
52 const msPerHour        = 3600000;
53 const msPerDay         = 86400000;
54 const msPerMonth       = 2592000000;
55
56 # Note: kDayZeroInJulianDay = ToJulianDay(1970, 0, 1).
57 const kInvalidDate        = 'Invalid Date';
58 const kDayZeroInJulianDay = 2440588;
59 const kMonthMask          = 0x1e0;
60 const kDayMask            = 0x01f;
61 const kYearShift          = 9;
62 const kMonthShift         = 5;
63
64 # Limits for parts of the date, so that we support all the dates that
65 # ECMA 262 - 15.9.1.1 requires us to, but at the same time be sure that
66 # the date (days since 1970) is in SMI range.
67 const kMinYear  = -1000000;
68 const kMaxYear  = 1000000;
69 const kMinMonth = -10000000;
70 const kMaxMonth = 10000000;
71
72 # Strict mode flags for passing to %SetProperty
73 const kSloppyMode = 0;
74 const kStrictMode = 1;
75
76 # Native cache ids.
77 const STRING_TO_REGEXP_CACHE_ID = 0;
78
79 # Type query macros.
80 #
81 # Note: We have special support for typeof(foo) === 'bar' in the compiler.
82 #       It will *not* generate a runtime typeof call for the most important
83 #       values of 'bar'.
84 macro IS_NULL(arg)              = (arg === null);
85 macro IS_NULL_OR_UNDEFINED(arg) = (arg == null);
86 macro IS_UNDEFINED(arg)         = (arg === (void 0));
87 macro IS_NUMBER(arg)            = (typeof(arg) === 'number');
88 macro IS_STRING(arg)            = (typeof(arg) === 'string');
89 macro IS_BOOLEAN(arg)           = (typeof(arg) === 'boolean');
90 macro IS_SYMBOL(arg)            = (typeof(arg) === 'symbol');
91 macro IS_OBJECT(arg)            = (%_IsObject(arg));
92 macro IS_ARRAY(arg)             = (%_IsArray(arg));
93 macro IS_FUNCTION(arg)          = (%_IsFunction(arg));
94 macro IS_REGEXP(arg)            = (%_IsRegExp(arg));
95 macro IS_SET(arg)               = (%_ClassOf(arg) === 'Set');
96 macro IS_MAP(arg)               = (%_ClassOf(arg) === 'Map');
97 macro IS_WEAKMAP(arg)           = (%_ClassOf(arg) === 'WeakMap');
98 macro IS_WEAKSET(arg)           = (%_ClassOf(arg) === 'WeakSet');
99 macro IS_DATE(arg)              = (%_ClassOf(arg) === 'Date');
100 macro IS_NUMBER_WRAPPER(arg)    = (%_ClassOf(arg) === 'Number');
101 macro IS_STRING_WRAPPER(arg)    = (%_ClassOf(arg) === 'String');
102 macro IS_SYMBOL_WRAPPER(arg)    = (%_ClassOf(arg) === 'Symbol');
103 macro IS_BOOLEAN_WRAPPER(arg)   = (%_ClassOf(arg) === 'Boolean');
104 macro IS_ERROR(arg)             = (%_ClassOf(arg) === 'Error');
105 macro IS_SCRIPT(arg)            = (%_ClassOf(arg) === 'Script');
106 macro IS_ARGUMENTS(arg)         = (%_ClassOf(arg) === 'Arguments');
107 macro IS_GLOBAL(arg)            = (%_ClassOf(arg) === 'global');
108 macro IS_ARRAYBUFFER(arg)       = (%_ClassOf(arg) === 'ArrayBuffer');
109 macro IS_DATAVIEW(arg)          = (%_ClassOf(arg) === 'DataView');
110 macro IS_GENERATOR(arg)         = (%_ClassOf(arg) === 'Generator');
111 macro IS_SET_ITERATOR(arg)      = (%_ClassOf(arg) === 'Set Iterator');
112 macro IS_MAP_ITERATOR(arg)      = (%_ClassOf(arg) === 'Map Iterator');
113 macro IS_UNDETECTABLE(arg)      = (%_IsUndetectableObject(arg));
114 macro FLOOR(arg)                = $floor(arg);
115
116 # Macro for ECMAScript 5 queries of the type:
117 # "Type(O) is object."
118 # This is the same as being either a function or an object in V8 terminology
119 # (including proxies).
120 # In addition, an undetectable object is also included by this.
121 macro IS_SPEC_OBJECT(arg)   = (%_IsSpecObject(arg));
122
123 # Macro for ECMAScript 5 queries of the type:
124 # "IsCallable(O)"
125 # We assume here that this is the same as being either a function or a function
126 # proxy. That ignores host objects with [[Call]] methods, but in most situations
127 # we cannot handle those anyway.
128 macro IS_SPEC_FUNCTION(arg) = (%_ClassOf(arg) === 'Function');
129
130 # Macro for ES6 CheckObjectCoercible
131 # Will throw a TypeError of the form "[functionName] called on null or undefined".
132 macro CHECK_OBJECT_COERCIBLE(arg, functionName) = if (IS_NULL_OR_UNDEFINED(arg) && !IS_UNDETECTABLE(arg)) throw MakeTypeError('called_on_null_or_undefined', [functionName]);
133
134 # Indices in bound function info retrieved by %BoundFunctionGetBindings(...).
135 const kBoundFunctionIndex = 0;
136 const kBoundThisIndex = 1;
137 const kBoundArgumentsStartIndex = 2;
138
139 # Inline macros. Use %IS_VAR to make sure arg is evaluated only once.
140 macro NUMBER_IS_NAN(arg) = (!%_IsSmi(%IS_VAR(arg)) && !(arg == arg));
141 macro NUMBER_IS_FINITE(arg) = (%_IsSmi(%IS_VAR(arg)) || ((arg == arg) && (arg != 1/0) && (arg != -1/0)));
142 macro TO_INTEGER(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : %NumberToInteger(ToNumber(arg)));
143 macro TO_INTEGER_FOR_SIDE_EFFECT(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : ToNumber(arg));
144 macro TO_INTEGER_MAP_MINUS_ZERO(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : %NumberToIntegerMapMinusZero(ToNumber(arg)));
145 macro TO_INT32(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : (arg >> 0));
146 macro TO_UINT32(arg) = (arg >>> 0);
147 macro TO_STRING_INLINE(arg) = (IS_STRING(%IS_VAR(arg)) ? arg : NonStringToString(arg));
148 macro TO_NUMBER_INLINE(arg) = (IS_NUMBER(%IS_VAR(arg)) ? arg : NonNumberToNumber(arg));
149 macro TO_OBJECT_INLINE(arg) = (IS_SPEC_OBJECT(%IS_VAR(arg)) ? arg : ToObject(arg));
150 macro JSON_NUMBER_TO_STRING(arg) = ((%_IsSmi(%IS_VAR(arg)) || arg - arg == 0) ? %_NumberToString(arg) : "null");
151 macro HAS_OWN_PROPERTY(obj, index) = (%_CallFunction(obj, index, ObjectHasOwnProperty));
152 macro SHOULD_CREATE_WRAPPER(functionName, receiver) = (!IS_SPEC_OBJECT(receiver) && %IsSloppyModeFunction(functionName));
153 macro HAS_INDEX(array, index, is_array) = ((is_array && %_HasFastPackedElements(%IS_VAR(array))) ? (index < array.length) : (index in array));
154
155 # Private names.
156 # GET_PRIVATE should only be used if the property is known to exists on obj
157 # itself (it should really use %GetOwnProperty, but that would be way slower).
158 macro GLOBAL_PRIVATE(name) = (%CreateGlobalPrivateOwnSymbol(name));
159 macro NEW_PRIVATE_OWN(name) = (%CreatePrivateOwnSymbol(name));
160 macro IS_PRIVATE(sym) = (%SymbolIsPrivate(sym));
161 macro HAS_PRIVATE(obj, sym) = (%HasOwnProperty(obj, sym));
162 macro HAS_DEFINED_PRIVATE(obj, sym) = (!IS_UNDEFINED(obj[sym]));
163 macro GET_PRIVATE(obj, sym) = (obj[sym]);
164 macro SET_PRIVATE(obj, sym, val) = (obj[sym] = val);
165 macro DELETE_PRIVATE(obj, sym) = (delete obj[sym]);
166
167 # Constants.  The compiler constant folds them.
168 const NAN = $NaN;
169 const INFINITY = (1/0);
170 const UNDEFINED = (void 0);
171
172 # Macros implemented in Python.
173 python macro CHAR_CODE(str) = ord(str[1]);
174
175 # Constants used on an array to implement the properties of the RegExp object.
176 const REGEXP_NUMBER_OF_CAPTURES = 0;
177 const REGEXP_FIRST_CAPTURE = 3;
178
179 # We can't put macros in macros so we use constants here.
180 # REGEXP_NUMBER_OF_CAPTURES
181 macro NUMBER_OF_CAPTURES(array) = ((array)[0]);
182
183 # Limit according to ECMA 262 15.9.1.1
184 const MAX_TIME_MS = 8640000000000000;
185 # Limit which is MAX_TIME_MS + msPerMonth.
186 const MAX_TIME_BEFORE_UTC = 8640002592000000;
187
188 # Gets the value of a Date object. If arg is not a Date object
189 # a type error is thrown.
190 macro CHECK_DATE(arg) = if (%_ClassOf(arg) !== 'Date') ThrowDateTypeError();
191 macro LOCAL_DATE_VALUE(arg) = (%_DateField(arg, 0) + %_DateField(arg, 21));
192 macro UTC_DATE_VALUE(arg)    = (%_DateField(arg, 0));
193
194 macro LOCAL_YEAR(arg)        = (%_DateField(arg, 1));
195 macro LOCAL_MONTH(arg)       = (%_DateField(arg, 2));
196 macro LOCAL_DAY(arg)         = (%_DateField(arg, 3));
197 macro LOCAL_WEEKDAY(arg)     = (%_DateField(arg, 4));
198 macro LOCAL_HOUR(arg)        = (%_DateField(arg, 5));
199 macro LOCAL_MIN(arg)         = (%_DateField(arg, 6));
200 macro LOCAL_SEC(arg)         = (%_DateField(arg, 7));
201 macro LOCAL_MS(arg)          = (%_DateField(arg, 8));
202 macro LOCAL_DAYS(arg)        = (%_DateField(arg, 9));
203 macro LOCAL_TIME_IN_DAY(arg) = (%_DateField(arg, 10));
204
205 macro UTC_YEAR(arg)        = (%_DateField(arg, 11));
206 macro UTC_MONTH(arg)       = (%_DateField(arg, 12));
207 macro UTC_DAY(arg)         = (%_DateField(arg, 13));
208 macro UTC_WEEKDAY(arg)     = (%_DateField(arg, 14));
209 macro UTC_HOUR(arg)        = (%_DateField(arg, 15));
210 macro UTC_MIN(arg)         = (%_DateField(arg, 16));
211 macro UTC_SEC(arg)         = (%_DateField(arg, 17));
212 macro UTC_MS(arg)          = (%_DateField(arg, 18));
213 macro UTC_DAYS(arg)        = (%_DateField(arg, 19));
214 macro UTC_TIME_IN_DAY(arg) = (%_DateField(arg, 20));
215
216 macro TIMEZONE_OFFSET(arg)   = (%_DateField(arg, 21));
217
218 macro SET_UTC_DATE_VALUE(arg, value) = (%DateSetValue(arg, value, 1));
219 macro SET_LOCAL_DATE_VALUE(arg, value) = (%DateSetValue(arg, value, 0));
220
221 # Last input and last subject of regexp matches.
222 const LAST_SUBJECT_INDEX = 1;
223 macro LAST_SUBJECT(array) = ((array)[1]);
224 macro LAST_INPUT(array) = ((array)[2]);
225
226 # REGEXP_FIRST_CAPTURE
227 macro CAPTURE(index) = (3 + (index));
228 const CAPTURE0 = 3;
229 const CAPTURE1 = 4;
230
231 # For the regexp capture override array.  This has the same
232 # format as the arguments to a function called from
233 # String.prototype.replace.
234 macro OVERRIDE_MATCH(override) = ((override)[0]);
235 macro OVERRIDE_POS(override) = ((override)[(override).length - 2]);
236 macro OVERRIDE_SUBJECT(override) = ((override)[(override).length - 1]);
237 # 1-based so index of 1 returns the first capture
238 macro OVERRIDE_CAPTURE(override, index) = ((override)[(index)]);
239
240 # PropertyDescriptor return value indices - must match
241 # PropertyDescriptorIndices in runtime-object.cc.
242 const IS_ACCESSOR_INDEX = 0;
243 const VALUE_INDEX = 1;
244 const GETTER_INDEX = 2;
245 const SETTER_INDEX = 3;
246 const WRITABLE_INDEX = 4;
247 const ENUMERABLE_INDEX = 5;
248 const CONFIGURABLE_INDEX = 6;
249
250 # For messages.js
251 # Matches Script::Type from objects.h
252 const TYPE_NATIVE = 0;
253 const TYPE_EXTENSION = 1;
254 const TYPE_NORMAL = 2;
255
256 # Matches Script::CompilationType from objects.h
257 const COMPILATION_TYPE_HOST = 0;
258 const COMPILATION_TYPE_EVAL = 1;
259 const COMPILATION_TYPE_JSON = 2;
260
261 # Matches Messages::kNoLineNumberInfo from v8.h
262 const kNoLineNumberInfo = 0;
263
264 # Matches PropertyAttributes from property-details.h
265 const PROPERTY_ATTRIBUTES_NONE = 0;
266 const PROPERTY_ATTRIBUTES_STRING = 8;
267 const PROPERTY_ATTRIBUTES_SYMBOLIC = 16;
268 const PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL = 32;
269
270 # Use for keys, values and entries iterators.
271 const ITERATOR_KIND_KEYS = 1;
272 const ITERATOR_KIND_VALUES = 2;
273 const ITERATOR_KIND_ENTRIES = 3;
274
275 # Check whether debug is active.
276 const DEBUG_IS_ACTIVE = (%_DebugIsActive() != 0);
277 macro DEBUG_IS_STEPPING(function) = (%_DebugIsActive() != 0 && %DebugCallbackSupportsStepping(function));
278 macro DEBUG_PREPARE_STEP_IN_IF_STEPPING(function) = if (DEBUG_IS_STEPPING(function)) %DebugPrepareStepInIfStepping(function);