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
31const NONE        = 0;
32const READ_ONLY   = 1;
33const DONT_ENUM   = 2;
34const DONT_DELETE = 4;
35const NEW_ONE_BYTE_STRING = true;
36const NEW_TWO_BYTE_STRING = false;
37
38# Constants used for getter and setter operations.
39const GETTER = 0;
40const SETTER = 1;
41
42# These definitions must match the index of the properties in objects.h.
43const kApiTagOffset                 = 0;
44const kApiPropertyListOffset        = 1;
45const kApiSerialNumberOffset        = 3;
46const kApiConstructorOffset         = 3;
47const kApiPrototypeTemplateOffset   = 5;
48const kApiParentTemplateOffset      = 6;
49const kApiFlagOffset                = 14;
50
51const NO_HINT     = 0;
52const NUMBER_HINT = 1;
53const STRING_HINT = 2;
54
55const kFunctionTag  = 0;
56const kNewObjectTag = 1;
57
58# For date.js.
59const HoursPerDay      = 24;
60const MinutesPerHour   = 60;
61const SecondsPerMinute = 60;
62const msPerSecond      = 1000;
63const msPerMinute      = 60000;
64const msPerHour        = 3600000;
65const msPerDay         = 86400000;
66const msPerMonth       = 2592000000;
67
68# For apinatives.js
69const kUninitialized = -1;
70const kReadOnlyPrototypeBit = 3;
71const kRemovePrototypeBit = 4;  # For FunctionTemplateInfo, matches objects.h
72const kDoNotCacheBit = 5;  # For FunctionTemplateInfo, matches objects.h
73
74# Note: kDayZeroInJulianDay = ToJulianDay(1970, 0, 1).
75const kInvalidDate        = 'Invalid Date';
76const kDayZeroInJulianDay = 2440588;
77const kMonthMask          = 0x1e0;
78const kDayMask            = 0x01f;
79const kYearShift          = 9;
80const kMonthShift         = 5;
81
82# Limits for parts of the date, so that we support all the dates that
83# ECMA 262 - 15.9.1.1 requires us to, but at the same time be sure that
84# the date (days since 1970) is in SMI range.
85const kMinYear  = -1000000;
86const kMaxYear  = 1000000;
87const kMinMonth = -10000000;
88const kMaxMonth = 10000000;
89
90# Strict mode flags for passing to %SetProperty
91const kSloppyMode = 0;
92const kStrictMode = 1;
93
94# Native cache ids.
95const STRING_TO_REGEXP_CACHE_ID = 0;
96
97# Type query macros.
98#
99# Note: We have special support for typeof(foo) === 'bar' in the compiler.
100#       It will *not* generate a runtime typeof call for the most important
101#       values of 'bar'.
102macro IS_NULL(arg)              = (arg === null);
103macro IS_NULL_OR_UNDEFINED(arg) = (arg == null);
104macro IS_UNDEFINED(arg)         = (arg === (void 0));
105macro IS_NUMBER(arg)            = (typeof(arg) === 'number');
106macro IS_STRING(arg)            = (typeof(arg) === 'string');
107macro IS_BOOLEAN(arg)           = (typeof(arg) === 'boolean');
108macro IS_SYMBOL(arg)            = (typeof(arg) === 'symbol');
109macro IS_OBJECT(arg)            = (%_IsObject(arg));
110macro IS_ARRAY(arg)             = (%_IsArray(arg));
111macro IS_FUNCTION(arg)          = (%_IsFunction(arg));
112macro IS_REGEXP(arg)            = (%_IsRegExp(arg));
113macro IS_SET(arg)               = (%_ClassOf(arg) === 'Set');
114macro IS_MAP(arg)               = (%_ClassOf(arg) === 'Map');
115macro IS_WEAKMAP(arg)           = (%_ClassOf(arg) === 'WeakMap');
116macro IS_WEAKSET(arg)           = (%_ClassOf(arg) === 'WeakSet');
117macro IS_DATE(arg)              = (%_ClassOf(arg) === 'Date');
118macro IS_NUMBER_WRAPPER(arg)    = (%_ClassOf(arg) === 'Number');
119macro IS_STRING_WRAPPER(arg)    = (%_ClassOf(arg) === 'String');
120macro IS_SYMBOL_WRAPPER(arg)    = (%_ClassOf(arg) === 'Symbol');
121macro IS_BOOLEAN_WRAPPER(arg)   = (%_ClassOf(arg) === 'Boolean');
122macro IS_ERROR(arg)             = (%_ClassOf(arg) === 'Error');
123macro IS_SCRIPT(arg)            = (%_ClassOf(arg) === 'Script');
124macro IS_ARGUMENTS(arg)         = (%_ClassOf(arg) === 'Arguments');
125macro IS_GLOBAL(arg)            = (%_ClassOf(arg) === 'global');
126macro IS_ARRAYBUFFER(arg)       = (%_ClassOf(arg) === 'ArrayBuffer');
127macro IS_DATAVIEW(arg)          = (%_ClassOf(arg) === 'DataView');
128macro IS_GENERATOR(arg)         = (%_ClassOf(arg) === 'Generator');
129macro IS_SET_ITERATOR(arg)      = (%_ClassOf(arg) === 'Set Iterator');
130macro IS_MAP_ITERATOR(arg)      = (%_ClassOf(arg) === 'Map Iterator');
131macro IS_UNDETECTABLE(arg)      = (%_IsUndetectableObject(arg));
132macro FLOOR(arg)                = $floor(arg);
133
134# Macro for ECMAScript 5 queries of the type:
135# "Type(O) is object."
136# This is the same as being either a function or an object in V8 terminology
137# (including proxies).
138# In addition, an undetectable object is also included by this.
139macro IS_SPEC_OBJECT(arg)   = (%_IsSpecObject(arg));
140
141# Macro for ECMAScript 5 queries of the type:
142# "IsCallable(O)"
143# We assume here that this is the same as being either a function or a function
144# proxy. That ignores host objects with [[Call]] methods, but in most situations
145# we cannot handle those anyway.
146macro IS_SPEC_FUNCTION(arg) = (%_ClassOf(arg) === 'Function');
147
148# Macro for ES6 CheckObjectCoercible
149# Will throw a TypeError of the form "[functionName] called on null or undefined".
150macro CHECK_OBJECT_COERCIBLE(arg, functionName) = if (IS_NULL_OR_UNDEFINED(arg) && !IS_UNDETECTABLE(arg)) throw MakeTypeError('called_on_null_or_undefined', [functionName]);
151
152# Indices in bound function info retrieved by %BoundFunctionGetBindings(...).
153const kBoundFunctionIndex = 0;
154const kBoundThisIndex = 1;
155const kBoundArgumentsStartIndex = 2;
156
157# Inline macros. Use %IS_VAR to make sure arg is evaluated only once.
158macro NUMBER_IS_NAN(arg) = (!%_IsSmi(%IS_VAR(arg)) && !(arg == arg));
159macro NUMBER_IS_FINITE(arg) = (%_IsSmi(%IS_VAR(arg)) || ((arg == arg) && (arg != 1/0) && (arg != -1/0)));
160macro TO_INTEGER(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : %NumberToInteger(ToNumber(arg)));
161macro TO_INTEGER_FOR_SIDE_EFFECT(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : ToNumber(arg));
162macro TO_INTEGER_MAP_MINUS_ZERO(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : %NumberToIntegerMapMinusZero(ToNumber(arg)));
163macro TO_INT32(arg) = (%_IsSmi(%IS_VAR(arg)) ? arg : (arg >> 0));
164macro TO_UINT32(arg) = (arg >>> 0);
165macro TO_STRING_INLINE(arg) = (IS_STRING(%IS_VAR(arg)) ? arg : NonStringToString(arg));
166macro TO_NUMBER_INLINE(arg) = (IS_NUMBER(%IS_VAR(arg)) ? arg : NonNumberToNumber(arg));
167macro TO_OBJECT_INLINE(arg) = (IS_SPEC_OBJECT(%IS_VAR(arg)) ? arg : ToObject(arg));
168macro JSON_NUMBER_TO_STRING(arg) = ((%_IsSmi(%IS_VAR(arg)) || arg - arg == 0) ? %_NumberToString(arg) : "null");
169macro HAS_OWN_PROPERTY(obj, index) = (%_CallFunction(obj, index, ObjectHasOwnProperty));
170
171# Private names.
172# GET_PRIVATE should only be used if the property is known to exists on obj
173# itself (it should really use %GetOwnProperty, but that would be way slower).
174macro GLOBAL_PRIVATE(name) = (%CreateGlobalPrivateOwnSymbol(name));
175macro NEW_PRIVATE_OWN(name) = (%CreatePrivateOwnSymbol(name));
176macro IS_PRIVATE(sym) = (%SymbolIsPrivate(sym));
177macro HAS_PRIVATE(obj, sym) = (%HasOwnProperty(obj, sym));
178macro HAS_DEFINED_PRIVATE(obj, sym) = (!IS_UNDEFINED(obj[sym]));
179macro GET_PRIVATE(obj, sym) = (obj[sym]);
180macro SET_PRIVATE(obj, sym, val) = (obj[sym] = val);
181macro DELETE_PRIVATE(obj, sym) = (delete obj[sym]);
182
183# Constants.  The compiler constant folds them.
184const NAN = $NaN;
185const INFINITY = (1/0);
186const UNDEFINED = (void 0);
187
188# Macros implemented in Python.
189python macro CHAR_CODE(str) = ord(str[1]);
190
191# Constants used on an array to implement the properties of the RegExp object.
192const REGEXP_NUMBER_OF_CAPTURES = 0;
193const REGEXP_FIRST_CAPTURE = 3;
194
195# We can't put macros in macros so we use constants here.
196# REGEXP_NUMBER_OF_CAPTURES
197macro NUMBER_OF_CAPTURES(array) = ((array)[0]);
198
199# Limit according to ECMA 262 15.9.1.1
200const MAX_TIME_MS = 8640000000000000;
201# Limit which is MAX_TIME_MS + msPerMonth.
202const MAX_TIME_BEFORE_UTC = 8640002592000000;
203
204# Gets the value of a Date object. If arg is not a Date object
205# a type error is thrown.
206macro CHECK_DATE(arg) = if (%_ClassOf(arg) !== 'Date') ThrowDateTypeError();
207macro LOCAL_DATE_VALUE(arg) = (%_DateField(arg, 0) + %_DateField(arg, 21));
208macro UTC_DATE_VALUE(arg)    = (%_DateField(arg, 0));
209
210macro LOCAL_YEAR(arg)        = (%_DateField(arg, 1));
211macro LOCAL_MONTH(arg)       = (%_DateField(arg, 2));
212macro LOCAL_DAY(arg)         = (%_DateField(arg, 3));
213macro LOCAL_WEEKDAY(arg)     = (%_DateField(arg, 4));
214macro LOCAL_HOUR(arg)        = (%_DateField(arg, 5));
215macro LOCAL_MIN(arg)         = (%_DateField(arg, 6));
216macro LOCAL_SEC(arg)         = (%_DateField(arg, 7));
217macro LOCAL_MS(arg)          = (%_DateField(arg, 8));
218macro LOCAL_DAYS(arg)        = (%_DateField(arg, 9));
219macro LOCAL_TIME_IN_DAY(arg) = (%_DateField(arg, 10));
220
221macro UTC_YEAR(arg)        = (%_DateField(arg, 11));
222macro UTC_MONTH(arg)       = (%_DateField(arg, 12));
223macro UTC_DAY(arg)         = (%_DateField(arg, 13));
224macro UTC_WEEKDAY(arg)     = (%_DateField(arg, 14));
225macro UTC_HOUR(arg)        = (%_DateField(arg, 15));
226macro UTC_MIN(arg)         = (%_DateField(arg, 16));
227macro UTC_SEC(arg)         = (%_DateField(arg, 17));
228macro UTC_MS(arg)          = (%_DateField(arg, 18));
229macro UTC_DAYS(arg)        = (%_DateField(arg, 19));
230macro UTC_TIME_IN_DAY(arg) = (%_DateField(arg, 20));
231
232macro TIMEZONE_OFFSET(arg)   = (%_DateField(arg, 21));
233
234macro SET_UTC_DATE_VALUE(arg, value) = (%DateSetValue(arg, value, 1));
235macro SET_LOCAL_DATE_VALUE(arg, value) = (%DateSetValue(arg, value, 0));
236
237# Last input and last subject of regexp matches.
238const LAST_SUBJECT_INDEX = 1;
239macro LAST_SUBJECT(array) = ((array)[1]);
240macro LAST_INPUT(array) = ((array)[2]);
241
242# REGEXP_FIRST_CAPTURE
243macro CAPTURE(index) = (3 + (index));
244const CAPTURE0 = 3;
245const CAPTURE1 = 4;
246
247# For the regexp capture override array.  This has the same
248# format as the arguments to a function called from
249# String.prototype.replace.
250macro OVERRIDE_MATCH(override) = ((override)[0]);
251macro OVERRIDE_POS(override) = ((override)[(override).length - 2]);
252macro OVERRIDE_SUBJECT(override) = ((override)[(override).length - 1]);
253# 1-based so index of 1 returns the first capture
254macro OVERRIDE_CAPTURE(override, index) = ((override)[(index)]);
255
256# PropertyDescriptor return value indices - must match
257# PropertyDescriptorIndices in runtime.cc.
258const IS_ACCESSOR_INDEX = 0;
259const VALUE_INDEX = 1;
260const GETTER_INDEX = 2;
261const SETTER_INDEX = 3;
262const WRITABLE_INDEX = 4;
263const ENUMERABLE_INDEX = 5;
264const CONFIGURABLE_INDEX = 6;
265
266# For messages.js
267# Matches Script::Type from objects.h
268const TYPE_NATIVE = 0;
269const TYPE_EXTENSION = 1;
270const TYPE_NORMAL = 2;
271
272# Matches Script::CompilationType from objects.h
273const COMPILATION_TYPE_HOST = 0;
274const COMPILATION_TYPE_EVAL = 1;
275const COMPILATION_TYPE_JSON = 2;
276
277# Matches Messages::kNoLineNumberInfo from v8.h
278const kNoLineNumberInfo = 0;
279
280# Matches PropertyAttributes from property-details.h
281const PROPERTY_ATTRIBUTES_NONE = 0;
282const PROPERTY_ATTRIBUTES_STRING = 8;
283const PROPERTY_ATTRIBUTES_SYMBOLIC = 16;
284const PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL = 32;
285
286# Use for keys, values and entries iterators.
287const ITERATOR_KIND_KEYS = 1;
288const ITERATOR_KIND_VALUES = 2;
289const ITERATOR_KIND_ENTRIES = 3;
290
291# Check whether debug is active.
292const DEBUG_IS_ACTIVE = (%_DebugIsActive() != 0);
293