1 // Copyright 2012 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 #ifndef V8_RUNTIME_RUNTIME_H_
6 #define V8_RUNTIME_RUNTIME_H_
7 
8 #include <memory>
9 
10 #include "include/v8.h"
11 #include "src/base/bit-field.h"
12 #include "src/base/platform/time.h"
13 #include "src/common/globals.h"
14 #include "src/objects/elements-kind.h"
15 #include "src/strings/unicode.h"
16 #include "src/utils/allocation.h"
17 #include "src/zone/zone.h"
18 
19 namespace v8 {
20 namespace internal {
21 
22 // * Each intrinsic is exposed in JavaScript via:
23 //    * %#name, which is always a runtime call.
24 //    * (optionally) %_#name, which can be inlined or just a runtime call, the
25 //      compiler in question decides.
26 //
27 // * IntrinsicTypes are Runtime::RUNTIME and Runtime::INLINE, respectively.
28 //
29 // * IDs are Runtime::k##name and Runtime::kInline##name, respectively.
30 //
31 // * All intrinsics have a C++ implementation Runtime_##name.
32 //
33 // * Each compiler has an explicit list of intrisics it supports, falling back
34 //   to a simple runtime call if necessary.
35 
36 // Entries have the form F(name, number of arguments, number of return values):
37 // A variable number of arguments is specified by a -1, additional restrictions
38 // are specified by inline comments. To declare only the runtime version (no
39 // inline), use the F macro below. To declare the runtime version and the inline
40 // version simultaneously, use the I macro below.
41 
42 #define FOR_EACH_INTRINSIC_ARRAY(F, I) \
43   F(ArrayIncludes_Slow, 3, 1)          \
44   F(ArrayIndexOf, 3, 1)                \
45   F(ArrayIsArray, 1, 1)                \
46   F(ArraySpeciesConstructor, 1, 1)     \
47   F(GrowArrayElements, 2, 1)           \
48   I(IsArray, 1, 1)                     \
49   F(NewArray, -1 /* >= 3 */, 1)        \
50   F(NormalizeElements, 1, 1)           \
51   F(TransitionElementsKind, 2, 1)      \
52   F(TransitionElementsKindWithKind, 2, 1)
53 
54 #define FOR_EACH_INTRINSIC_ATOMICS(F, I) \
55   F(AtomicsLoad64, 2, 1)                 \
56   F(AtomicsStore64, 3, 1)                \
57   F(AtomicsAdd, 3, 1)                    \
58   F(AtomicsAnd, 3, 1)                    \
59   F(AtomicsCompareExchange, 4, 1)        \
60   F(AtomicsExchange, 3, 1)               \
61   F(AtomicsNumWaitersForTesting, 2, 1)   \
62   F(AtomicsOr, 3, 1)                     \
63   F(AtomicsSub, 3, 1)                    \
64   F(AtomicsXor, 3, 1)                    \
65   F(SetAllowAtomicsWait, 1, 1)
66 
67 #define FOR_EACH_INTRINSIC_BIGINT(F, I) \
68   F(BigIntBinaryOp, 3, 1)               \
69   F(BigIntCompareToBigInt, 3, 1)        \
70   F(BigIntCompareToNumber, 3, 1)        \
71   F(BigIntCompareToString, 3, 1)        \
72   F(BigIntEqualToBigInt, 2, 1)          \
73   F(BigIntEqualToNumber, 2, 1)          \
74   F(BigIntEqualToString, 2, 1)          \
75   F(BigIntToBoolean, 1, 1)              \
76   F(BigIntToNumber, 1, 1)               \
77   F(BigIntUnaryOp, 2, 1)                \
78   F(ToBigInt, 1, 1)
79 
80 #define FOR_EACH_INTRINSIC_CLASSES(F, I)    \
81   F(DefineClass, -1 /* >= 3 */, 1)          \
82   F(HomeObjectSymbol, 0, 1)                 \
83   F(LoadFromSuper, 3, 1)                    \
84   F(LoadKeyedFromSuper, 3, 1)               \
85   F(StoreKeyedToSuper, 4, 1)                \
86   F(StoreToSuper, 4, 1)                     \
87   F(ThrowConstructorNonCallableError, 1, 1) \
88   F(ThrowNotSuperConstructor, 2, 1)         \
89   F(ThrowStaticPrototypeError, 0, 1)        \
90   F(ThrowSuperAlreadyCalledError, 0, 1)     \
91   F(ThrowSuperNotCalled, 0, 1)              \
92   F(ThrowUnsupportedSuperError, 0, 1)
93 
94 #define FOR_EACH_INTRINSIC_COLLECTIONS(F, I) \
95   F(MapGrow, 1, 1)                           \
96   F(MapShrink, 1, 1)                         \
97   F(SetGrow, 1, 1)                           \
98   F(SetShrink, 1, 1)                         \
99   F(TheHole, 0, 1)                           \
100   F(WeakCollectionDelete, 3, 1)              \
101   F(WeakCollectionSet, 4, 1)
102 
103 #define FOR_EACH_INTRINSIC_COMPILER(F, I) \
104   F(CompileForOnStackReplacement, 0, 1)   \
105   F(CompileLazy, 1, 1)                    \
106   F(CompileOptimized_Concurrent, 1, 1)    \
107   F(CompileOptimized_NotConcurrent, 1, 1) \
108   F(EvictOptimizedCodeSlot, 1, 1)         \
109   F(FunctionFirstExecution, 1, 1)         \
110   F(InstantiateAsmJs, 4, 1)               \
111   F(NotifyDeoptimized, 0, 1)              \
112   F(ResolvePossiblyDirectEval, 6, 1)
113 
114 #define FOR_EACH_INTRINSIC_DATE(F, I) F(DateCurrentTime, 0, 1)
115 
116 #define FOR_EACH_INTRINSIC_DEBUG(F, I)          \
117   F(ClearStepping, 0, 1)                        \
118   F(CollectGarbage, 1, 1)                       \
119   F(DebugAsyncFunctionEntered, 1, 1)            \
120   F(DebugAsyncFunctionSuspended, 1, 1)          \
121   F(DebugAsyncFunctionResumed, 1, 1)            \
122   F(DebugAsyncFunctionFinished, 2, 1)           \
123   F(DebugBreakAtEntry, 1, 1)                    \
124   F(DebugCollectCoverage, 0, 1)                 \
125   F(DebugGetLoadedScriptIds, 0, 1)              \
126   F(DebugOnFunctionCall, 2, 1)                  \
127   F(DebugPopPromise, 0, 1)                      \
128   F(DebugPrepareStepInSuspendedGenerator, 0, 1) \
129   F(DebugPushPromise, 1, 1)                     \
130   F(DebugToggleBlockCoverage, 1, 1)             \
131   F(DebugTogglePreciseCoverage, 1, 1)           \
132   F(FunctionGetInferredName, 1, 1)              \
133   F(GetBreakLocations, 1, 1)                    \
134   F(GetGeneratorScopeCount, 1, 1)               \
135   F(GetGeneratorScopeDetails, 2, 1)             \
136   F(GetHeapUsage, 0, 1)                         \
137   F(HandleDebuggerStatement, 0, 1)              \
138   F(IsBreakOnException, 1, 1)                   \
139   F(LiveEditPatchScript, 2, 1)                  \
140   F(ProfileCreateSnapshotDataBlob, 0, 1)        \
141   F(ScheduleBreak, 0, 1)                        \
142   F(ScriptLocationFromLine2, 4, 1)              \
143   F(SetGeneratorScopeVariableValue, 4, 1)       \
144   I(IncBlockCounter, 2, 1)
145 
146 #define FOR_EACH_INTRINSIC_FORIN(F, I) \
147   F(ForInEnumerate, 1, 1)              \
148   F(ForInHasProperty, 2, 1)
149 
150 #ifdef V8_TRACE_IGNITION
151 #define FOR_EACH_INTRINSIC_INTERPRETER_TRACE(F, I) \
152   F(InterpreterTraceBytecodeEntry, 3, 1)           \
153   F(InterpreterTraceBytecodeExit, 3, 1)
154 #else
155 #define FOR_EACH_INTRINSIC_INTERPRETER_TRACE(F, I)
156 #endif
157 
158 #ifdef V8_TRACE_FEEDBACK_UPDATES
159 #define FOR_EACH_INTRINSIC_INTERPRETER_TRACE_FEEDBACK(F, I) \
160   F(InterpreterTraceUpdateFeedback, 3, 1)
161 #else
162 #define FOR_EACH_INTRINSIC_INTERPRETER_TRACE_FEEDBACK(F, I)
163 #endif
164 
165 #define FOR_EACH_INTRINSIC_INTERPRETER(F, I) \
166   FOR_EACH_INTRINSIC_INTERPRETER_TRACE(F, I) \
167   FOR_EACH_INTRINSIC_INTERPRETER_TRACE_FEEDBACK(F, I)
168 
169 #define FOR_EACH_INTRINSIC_FUNCTION(F, I)  \
170   I(Call, -1 /* >= 2 */, 1)                \
171   F(FunctionGetScriptSource, 1, 1)         \
172   F(FunctionGetScriptId, 1, 1)             \
173   F(FunctionGetScriptSourcePosition, 1, 1) \
174   F(FunctionGetSourceCode, 1, 1)           \
175   F(FunctionIsAPIFunction, 1, 1)           \
176   F(IsFunction, 1, 1)
177 
178 #define FOR_EACH_INTRINSIC_GENERATOR(F, I)    \
179   I(AsyncFunctionAwaitCaught, 2, 1)           \
180   I(AsyncFunctionAwaitUncaught, 2, 1)         \
181   I(AsyncFunctionEnter, 2, 1)                 \
182   I(AsyncFunctionReject, 3, 1)                \
183   I(AsyncFunctionResolve, 3, 1)               \
184   I(AsyncGeneratorAwaitCaught, 2, 1)          \
185   I(AsyncGeneratorAwaitUncaught, 2, 1)        \
186   F(AsyncGeneratorHasCatchHandlerForPC, 1, 1) \
187   I(AsyncGeneratorReject, 2, 1)               \
188   I(AsyncGeneratorResolve, 3, 1)              \
189   I(AsyncGeneratorYield, 3, 1)                \
190   I(CreateJSGeneratorObject, 2, 1)            \
191   I(GeneratorClose, 1, 1)                     \
192   F(GeneratorGetFunction, 1, 1)               \
193   I(GeneratorGetResumeMode, 1, 1)
194 
195 #ifdef V8_INTL_SUPPORT
196 #define FOR_EACH_INTRINSIC_INTL(F, I) \
197   F(FormatList, 2, 1)                 \
198   F(FormatListToParts, 2, 1)          \
199   F(StringToLowerCaseIntl, 1, 1)      \
200   F(StringToUpperCaseIntl, 1, 1)  // End of macro.
201 #else
202 #define FOR_EACH_INTRINSIC_INTL(F, I)
203 #endif  // V8_INTL_SUPPORT
204 
205 #define FOR_EACH_INTRINSIC_INTERNAL(F, I)            \
206   F(AccessCheck, 1, 1)                               \
207   F(AllocateByteArray, 1, 1)                         \
208   F(AllocateInYoungGeneration, 2, 1)                 \
209   F(AllocateInOldGeneration, 2, 1)                   \
210   F(AllocateSeqOneByteString, 1, 1)                  \
211   F(AllocateSeqTwoByteString, 1, 1)                  \
212   F(AllowDynamicFunction, 1, 1)                      \
213   I(CreateAsyncFromSyncIterator, 1, 1)               \
214   F(CreateListFromArrayLike, 1, 1)                   \
215   F(DoubleToStringWithRadix, 2, 1)                   \
216   F(FatalProcessOutOfMemoryInAllocateRaw, 0, 1)      \
217   F(FatalProcessOutOfMemoryInvalidArrayLength, 0, 1) \
218   F(GetAndResetRuntimeCallStats, -1 /* <= 2 */, 1)   \
219   F(GetTemplateObject, 3, 1)                         \
220   F(IncrementUseCounter, 1, 1)                       \
221   F(BytecodeBudgetInterrupt, 1, 1)                   \
222   F(NewError, 2, 1)                                  \
223   F(NewReferenceError, 2, 1)                         \
224   F(NewSyntaxError, 2, 1)                            \
225   F(NewTypeError, 2, 1)                              \
226   F(OrdinaryHasInstance, 2, 1)                       \
227   F(PromoteScheduledException, 0, 1)                 \
228   F(ReportMessage, 1, 1)                             \
229   F(ReThrow, 1, 1)                                   \
230   F(RunMicrotaskCallback, 2, 1)                      \
231   F(PerformMicrotaskCheckpoint, 0, 1)                \
232   F(StackGuard, 0, 1)                                \
233   F(StackGuardWithGap, 1, 1)                         \
234   F(Throw, 1, 1)                                     \
235   F(ThrowApplyNonFunction, 1, 1)                     \
236   F(ThrowCalledNonCallable, 1, 1)                    \
237   F(ThrowConstructedNonConstructable, 1, 1)          \
238   F(ThrowConstructorReturnedNonObject, 0, 1)         \
239   F(ThrowInvalidStringLength, 0, 1)                  \
240   F(ThrowInvalidTypedArrayAlignment, 2, 1)           \
241   F(ThrowIteratorError, 1, 1)                        \
242   F(ThrowSpreadArgIsNullOrUndefined, 1, 1)           \
243   F(ThrowIteratorResultNotAnObject, 1, 1)            \
244   F(ThrowNotConstructor, 1, 1)                       \
245   F(ThrowPatternAssignmentNonCoercible, 1, 1)        \
246   F(ThrowRangeError, -1 /* >= 1 */, 1)               \
247   F(ThrowReferenceError, 1, 1)                       \
248   F(ThrowAccessedUninitializedVariable, 1, 1)        \
249   F(ThrowStackOverflow, 0, 1)                        \
250   F(ThrowSymbolAsyncIteratorInvalid, 0, 1)           \
251   F(ThrowSymbolIteratorInvalid, 0, 1)                \
252   F(ThrowThrowMethodMissing, 0, 1)                   \
253   F(ThrowTypeError, -1 /* >= 1 */, 1)                \
254   F(ThrowTypeErrorIfStrict, -1 /* >= 1 */, 1)        \
255   F(Typeof, 1, 1)                                    \
256   F(UnwindAndFindExceptionHandler, 0, 1)
257 
258 #define FOR_EACH_INTRINSIC_LITERALS(F, I)           \
259   F(CreateArrayLiteral, 4, 1)                       \
260   F(CreateArrayLiteralWithoutAllocationSite, 2, 1)  \
261   F(CreateObjectLiteral, 4, 1)                      \
262   F(CreateObjectLiteralWithoutAllocationSite, 2, 1) \
263   F(CreateRegExpLiteral, 4, 1)
264 
265 #define FOR_EACH_INTRINSIC_MODULE(F, I) \
266   F(DynamicImportCall, 2, 1)            \
267   I(GetImportMetaObject, 0, 1)          \
268   F(GetModuleNamespace, 1, 1)
269 
270 #define FOR_EACH_INTRINSIC_NUMBERS(F, I) \
271   F(ArrayBufferMaxByteLength, 0, 1)      \
272   F(GetHoleNaNLower, 0, 1)               \
273   F(GetHoleNaNUpper, 0, 1)               \
274   I(IsSmi, 1, 1)                         \
275   F(IsValidSmi, 1, 1)                    \
276   F(MaxSmi, 0, 1)                        \
277   F(NumberToString, 1, 1)                \
278   F(StringParseFloat, 1, 1)              \
279   F(StringParseInt, 2, 1)                \
280   F(StringToNumber, 1, 1)                \
281   F(TypedArrayMaxLength, 0, 1)
282 
283 #define FOR_EACH_INTRINSIC_OBJECT(F, I)                         \
284   F(AddDictionaryProperty, 3, 1)                                \
285   F(AddPrivateField, 3, 1)                                      \
286   F(AddPrivateBrand, 3, 1)                                      \
287   F(AllocateHeapNumber, 0, 1)                                   \
288   F(ClassOf, 1, 1)                                              \
289   F(CollectTypeProfile, 3, 1)                                   \
290   F(CompleteInobjectSlackTrackingForMap, 1, 1)                  \
291   I(CopyDataProperties, 2, 1)                                   \
292   F(CopyDataPropertiesWithExcludedProperties, -1 /* >= 1 */, 1) \
293   I(CreateDataProperty, 3, 1)                                   \
294   I(CreateIterResultObject, 2, 1)                               \
295   F(CreatePrivateAccessors, 2, 1)                               \
296   F(DefineAccessorPropertyUnchecked, 5, 1)                      \
297   F(DefineDataPropertyInLiteral, 6, 1)                          \
298   F(DefineGetterPropertyUnchecked, 4, 1)                        \
299   F(DefineSetterPropertyUnchecked, 4, 1)                        \
300   F(DeleteProperty, 3, 1)                                       \
301   F(GetDerivedMap, 2, 1)                                        \
302   F(GetFunctionName, 1, 1)                                      \
303   F(GetOwnPropertyDescriptor, 2, 1)                             \
304   F(GetOwnPropertyKeys, 2, 1)                                   \
305   F(GetProperty, 2, 1)                                          \
306   F(HasFastPackedElements, 1, 1)                                \
307   F(HasInPrototypeChain, 2, 1)                                  \
308   I(HasProperty, 2, 1)                                          \
309   F(InternalSetPrototype, 2, 1)                                 \
310   I(IsJSReceiver, 1, 1)                                         \
311   F(JSReceiverPreventExtensionsDontThrow, 1, 1)                 \
312   F(JSReceiverPreventExtensionsThrow, 1, 1)                     \
313   F(JSReceiverGetPrototypeOf, 1, 1)                             \
314   F(JSReceiverSetPrototypeOfDontThrow, 2, 1)                    \
315   F(JSReceiverSetPrototypeOfThrow, 2, 1)                        \
316   F(LoadPrivateGetter, 1, 1)                                    \
317   F(LoadPrivateSetter, 1, 1)                                    \
318   F(NewObject, 2, 1)                                            \
319   F(ObjectCreate, 2, 1)                                         \
320   F(ObjectEntries, 1, 1)                                        \
321   F(ObjectEntriesSkipFastPath, 1, 1)                            \
322   F(ObjectGetOwnPropertyNames, 1, 1)                            \
323   F(ObjectGetOwnPropertyNamesTryFast, 1, 1)                     \
324   F(ObjectHasOwnProperty, 2, 1)                                 \
325   F(ObjectIsExtensible, 1, 1)                                   \
326   F(ObjectKeys, 1, 1)                                           \
327   F(ObjectValues, 1, 1)                                         \
328   F(ObjectValuesSkipFastPath, 1, 1)                             \
329   F(OptimizeObjectForAddingMultipleProperties, 2, 1)            \
330   F(SetDataProperties, 2, 1)                                    \
331   F(SetKeyedProperty, 3, 1)                                     \
332   F(SetNamedProperty, 3, 1)                                     \
333   F(StoreDataPropertyInLiteral, 3, 1)                           \
334   F(ShrinkPropertyDictionary, 1, 1)                             \
335   F(ToFastProperties, 1, 1)                                     \
336   I(ToLength, 1, 1)                                             \
337   F(ToName, 1, 1)                                               \
338   I(ToNumber, 1, 1)                                             \
339   F(ToNumeric, 1, 1)                                            \
340   I(ToObject, 1, 1)                                             \
341   I(ToStringRT, 1, 1)                                           \
342   F(TryMigrateInstance, 1, 1)
343 
344 #define FOR_EACH_INTRINSIC_OPERATORS(F, I) \
345   F(Add, 2, 1)                             \
346   F(Equal, 2, 1)                           \
347   F(GreaterThan, 2, 1)                     \
348   F(GreaterThanOrEqual, 2, 1)              \
349   F(LessThan, 2, 1)                        \
350   F(LessThanOrEqual, 2, 1)                 \
351   F(NotEqual, 2, 1)                        \
352   F(StrictEqual, 2, 1)                     \
353   F(StrictNotEqual, 2, 1)                  \
354   F(ReferenceEqual, 2, 1)
355 
356 #define FOR_EACH_INTRINSIC_PROMISE(F, I) \
357   F(EnqueueMicrotask, 1, 1)              \
358   F(PromiseHookAfter, 1, 1)              \
359   F(PromiseHookBefore, 1, 1)             \
360   F(PromiseHookInit, 2, 1)               \
361   F(AwaitPromisesInit, 5, 1)             \
362   F(AwaitPromisesInitOld, 5, 1)          \
363   F(PromiseMarkAsHandled, 1, 1)          \
364   F(PromiseRejectEventFromStack, 2, 1)   \
365   F(PromiseRevokeReject, 1, 1)           \
366   F(PromiseStatus, 1, 1)                 \
367   F(RejectPromise, 3, 1)                 \
368   F(ResolvePromise, 2, 1)                \
369   F(PromiseRejectAfterResolved, 2, 1)    \
370   F(PromiseResolveAfterResolved, 2, 1)
371 
372 #define FOR_EACH_INTRINSIC_PROXY(F, I) \
373   F(CheckProxyGetSetTrapResult, 2, 1)  \
374   F(CheckProxyHasTrapResult, 2, 1)     \
375   F(CheckProxyDeleteTrapResult, 2, 1)  \
376   F(GetPropertyWithReceiver, 3, 1)     \
377   F(IsJSProxy, 1, 1)                   \
378   F(JSProxyGetHandler, 1, 1)           \
379   F(JSProxyGetTarget, 1, 1)            \
380   F(SetPropertyWithReceiver, 4, 1)
381 
382 #define FOR_EACH_INTRINSIC_REGEXP(F, I)             \
383   I(IsRegExp, 1, 1)                                 \
384   F(RegExpExec, 4, 1)                               \
385   F(RegExpExecMultiple, 4, 1)                       \
386   F(RegExpInitializeAndCompile, 3, 1)               \
387   F(RegExpReplaceRT, 3, 1)                          \
388   F(RegExpSplit, 3, 1)                              \
389   F(StringReplaceNonGlobalRegExpWithFunction, 3, 1) \
390   F(StringSplit, 3, 1)
391 
392 #define FOR_EACH_INTRINSIC_SCOPES(F, I)     \
393   F(DeclareEvalFunction, 2, 1)              \
394   F(DeclareEvalVar, 1, 1)                   \
395   F(DeclareGlobals, 2, 1)                   \
396   F(DeclareModuleExports, 2, 1)             \
397   F(DeleteLookupSlot, 1, 1)                 \
398   F(LoadLookupSlot, 1, 1)                   \
399   F(LoadLookupSlotInsideTypeof, 1, 1)       \
400   F(NewArgumentsElements, 3, 1)             \
401                                             \
402   F(NewClosure, 2, 1)                       \
403   F(NewClosure_Tenured, 2, 1)               \
404   F(NewFunctionContext, 1, 1)               \
405   F(NewRestParameter, 1, 1)                 \
406   F(NewSloppyArguments, 3, 1)               \
407   F(NewSloppyArguments_Generic, 1, 1)       \
408   F(NewStrictArguments, 1, 1)               \
409   F(PushBlockContext, 1, 1)                 \
410   F(PushCatchContext, 2, 1)                 \
411   F(PushWithContext, 2, 1)                  \
412   F(StoreGlobalNoHoleCheckForReplLet, 2, 1) \
413   F(StoreLookupSlot_Sloppy, 2, 1)           \
414   F(StoreLookupSlot_SloppyHoisting, 2, 1)   \
415   F(StoreLookupSlot_Strict, 2, 1)           \
416   F(ThrowConstAssignError, 0, 1)
417 
418 #define FOR_EACH_INTRINSIC_STRINGS(F, I)  \
419   F(FlattenString, 1, 1)                  \
420   F(GetSubstitution, 5, 1)                \
421   F(InternalizeString, 1, 1)              \
422   F(StringAdd, 2, 1)                      \
423   F(StringBuilderConcat, 3, 1)            \
424   F(StringCharCodeAt, 2, 1)               \
425   F(StringEqual, 2, 1)                    \
426   F(StringEscapeQuotes, 1, 1)             \
427   F(StringGreaterThan, 2, 1)              \
428   F(StringGreaterThanOrEqual, 2, 1)       \
429   F(StringIncludes, 3, 1)                 \
430   F(StringIndexOf, 3, 1)                  \
431   F(StringIndexOfUnchecked, 3, 1)         \
432   F(StringLastIndexOf, 2, 1)              \
433   F(StringLessThan, 2, 1)                 \
434   F(StringLessThanOrEqual, 2, 1)          \
435   F(StringMaxLength, 0, 1)                \
436   F(StringReplaceOneCharWithString, 3, 1) \
437   F(StringCompareSequence, 3, 1)          \
438   F(StringSubstring, 3, 1)                \
439   F(StringToArray, 2, 1)                  \
440   F(StringTrim, 2, 1)
441 
442 #define FOR_EACH_INTRINSIC_SYMBOL(F, I)    \
443   F(CreatePrivateNameSymbol, 1, 1)         \
444   F(CreatePrivateBrandSymbol, 1, 1)        \
445   F(CreatePrivateSymbol, -1 /* <= 1 */, 1) \
446   F(SymbolDescriptiveString, 1, 1)         \
447   F(SymbolIsPrivate, 1, 1)
448 
449 #define FOR_EACH_INTRINSIC_TEST(F, I)         \
450   F(Abort, 1, 1)                              \
451   F(AbortJS, 1, 1)                            \
452   F(AbortCSAAssert, 1, 1)                     \
453   F(ArraySpeciesProtector, 0, 1)              \
454   F(ClearFunctionFeedback, 1, 1)              \
455   F(ClearMegamorphicStubCache, 0, 1)          \
456   F(CloneWasmModule, 1, 1)                    \
457   F(CompleteInobjectSlackTracking, 1, 1)      \
458   F(ConstructConsString, 2, 1)                \
459   F(ConstructDouble, 2, 1)                    \
460   F(ConstructSlicedString, 2, 1)              \
461   F(DebugPrint, 1, 1)                         \
462   F(DebugTrace, 0, 1)                         \
463   F(DebugTrackRetainingPath, -1, 1)           \
464   F(DeoptimizeFunction, 1, 1)                 \
465   F(DeserializeWasmModule, 2, 1)              \
466   F(DisallowCodegenFromStrings, 1, 1)         \
467   F(DisallowWasmCodegen, 1, 1)                \
468   F(DisassembleFunction, 1, 1)                \
469   F(EnableCodeLoggingForTesting, 0, 1)        \
470   F(EnsureFeedbackVectorForFunction, 1, 1)    \
471   F(FreezeWasmLazyCompilation, 1, 1)          \
472   F(GetCallable, 0, 1)                        \
473   F(GetInitializerFunction, 1, 1)             \
474   F(GetOptimizationStatus, -1, 1)             \
475   F(GetUndetectable, 0, 1)                    \
476   F(GetWasmExceptionId, 2, 1)                 \
477   F(GetWasmExceptionValues, 1, 1)             \
478   F(GetWasmRecoveredTrapCount, 0, 1)          \
479   F(GlobalPrint, 1, 1)                        \
480   F(HasDictionaryElements, 1, 1)              \
481   F(HasDoubleElements, 1, 1)                  \
482   F(HasElementsInALargeObjectSpace, 1, 1)     \
483   F(HasFastElements, 1, 1)                    \
484   F(HasFastProperties, 1, 1)                  \
485   F(HasFixedBigInt64Elements, 1, 1)           \
486   F(HasFixedBigUint64Elements, 1, 1)          \
487   F(HasFixedFloat32Elements, 1, 1)            \
488   F(HasFixedFloat64Elements, 1, 1)            \
489   F(HasFixedInt16Elements, 1, 1)              \
490   F(HasFixedInt32Elements, 1, 1)              \
491   F(HasFixedInt8Elements, 1, 1)               \
492   F(HasFixedUint16Elements, 1, 1)             \
493   F(HasFixedUint32Elements, 1, 1)             \
494   F(HasFixedUint8ClampedElements, 1, 1)       \
495   F(HasFixedUint8Elements, 1, 1)              \
496   F(HasHoleyElements, 1, 1)                   \
497   F(HasObjectElements, 1, 1)                  \
498   F(HasPackedElements, 1, 1)                  \
499   F(HasSloppyArgumentsElements, 1, 1)         \
500   F(HasSmiElements, 1, 1)                     \
501   F(HasSmiOrObjectElements, 1, 1)             \
502   F(HaveSameMap, 2, 1)                        \
503   F(HeapObjectVerify, 1, 1)                   \
504   F(ICsAreEnabled, 0, 1)                      \
505   F(InYoungGeneration, 1, 1)                  \
506   F(IsAsmWasmCode, 1, 1)                      \
507   F(IsBeingInterpreted, 0, 1)                 \
508   F(IsConcurrentRecompilationSupported, 0, 1) \
509   F(IsLiftoffFunction, 1, 1)                  \
510   F(IsThreadInWasm, 0, 1)                     \
511   F(IsWasmCode, 1, 1)                         \
512   F(IsWasmTrapHandlerEnabled, 0, 1)           \
513   F(RegexpHasBytecode, 2, 1)                  \
514   F(RegexpHasNativeCode, 2, 1)                \
515   F(MapIteratorProtector, 0, 1)               \
516   F(NeverOptimizeFunction, 1, 1)              \
517   F(NotifyContextDisposed, 0, 1)              \
518   F(OptimizeFunctionOnNextCall, -1, 1)        \
519   F(OptimizeOsr, -1, 1)                       \
520   F(NewRegExpWithBacktrackLimit, 3, 1)        \
521   F(PrepareFunctionForOptimization, -1, 1)    \
522   F(PrintWithNameForAssert, 2, 1)             \
523   F(RedirectToWasmInterpreter, 2, 1)          \
524   F(RunningInSimulator, 0, 1)                 \
525   F(RuntimeEvaluateREPL, 1, 1)                \
526   F(SerializeWasmModule, 1, 1)                \
527   F(SetAllocationTimeout, -1 /* 2 || 3 */, 1) \
528   F(SetForceSlowPath, 1, 1)                   \
529   F(SetIteratorProtector, 0, 1)               \
530   F(SetWasmCompileControls, 2, 1)             \
531   F(SetWasmInstantiateControls, 0, 1)         \
532   F(SetWasmThreadsEnabled, 1, 1)              \
533   F(SimulateNewspaceFull, 0, 1)               \
534   F(StringIteratorProtector, 0, 1)            \
535   F(SystemBreak, 0, 1)                        \
536   F(TraceEnter, 0, 1)                         \
537   F(TraceExit, 1, 1)                          \
538   F(TurbofanStaticAssert, 1, 1)               \
539   F(UnblockConcurrentRecompilation, 0, 1)     \
540   F(WasmGetNumberOfInstances, 1, 1)           \
541   F(WasmNumInterpretedCalls, 1, 1)            \
542   F(WasmNumCodeSpaces, 1, 1)                  \
543   F(WasmTierDownModule, 1, 1)                 \
544   F(WasmTierUpFunction, 2, 1)                 \
545   F(WasmTierUpModule, 1, 1)                   \
546   F(WasmTraceMemory, 1, 1)                    \
547   I(DeoptimizeNow, 0, 1)
548 
549 #define FOR_EACH_INTRINSIC_TYPEDARRAY(F, I) \
550   F(ArrayBufferDetach, 1, 1)                \
551   F(TypedArrayCopyElements, 3, 1)           \
552   F(TypedArrayGetBuffer, 1, 1)              \
553   F(TypedArraySet, 2, 1)                    \
554   F(TypedArraySortFast, 1, 1)
555 
556 #define FOR_EACH_INTRINSIC_WASM(F, I)   \
557   F(ThrowWasmError, 1, 1)               \
558   F(ThrowWasmStackOverflow, 0, 1)       \
559   F(WasmI32AtomicWait, 4, 1)            \
560   F(WasmI64AtomicWait, 5, 1)            \
561   F(WasmAtomicNotify, 3, 1)             \
562   F(WasmExceptionGetValues, 1, 1)       \
563   F(WasmExceptionGetTag, 1, 1)          \
564   F(WasmMemoryGrow, 2, 1)               \
565   F(WasmRunInterpreter, 2, 1)           \
566   F(WasmStackGuard, 0, 1)               \
567   F(WasmThrowCreate, 2, 1)              \
568   F(WasmThrowTypeError, 0, 1)           \
569   F(WasmRefFunc, 1, 1)                  \
570   F(WasmFunctionTableGet, 3, 1)         \
571   F(WasmFunctionTableSet, 4, 1)         \
572   F(WasmTableInit, 6, 1)                \
573   F(WasmTableCopy, 6, 1)                \
574   F(WasmTableGrow, 3, 1)                \
575   F(WasmTableFill, 4, 1)                \
576   F(WasmIsValidFuncRefValue, 1, 1)      \
577   F(WasmCompileLazy, 2, 1)              \
578   F(WasmNewMultiReturnFixedArray, 1, 1) \
579   F(WasmNewMultiReturnJSArray, 1, 1)    \
580   F(WasmDebugBreak, 0, 1)
581 
582 #define FOR_EACH_INTRINSIC_RETURN_PAIR_IMPL(F, I) \
583   F(DebugBreakOnBytecode, 1, 2)                   \
584   F(LoadLookupSlotForCall, 1, 2)
585 
586 // Most intrinsics are implemented in the runtime/ directory, but ICs are
587 // implemented in ic.cc for now.
588 #define FOR_EACH_INTRINSIC_IC(F, I)          \
589   F(ElementsTransitionAndStoreIC_Miss, 6, 1) \
590   F(KeyedLoadIC_Miss, 4, 1)                  \
591   F(KeyedStoreIC_Miss, 5, 1)                 \
592   F(StoreInArrayLiteralIC_Miss, 5, 1)        \
593   F(KeyedStoreIC_Slow, 3, 1)                 \
594   F(LoadElementWithInterceptor, 2, 1)        \
595   F(LoadGlobalIC_Miss, 4, 1)                 \
596   F(LoadGlobalIC_Slow, 3, 1)                 \
597   F(LoadIC_Miss, 4, 1)                       \
598   F(LoadNoFeedbackIC_Miss, 4, 1)             \
599   F(LoadPropertyWithInterceptor, 5, 1)       \
600   F(StoreCallbackProperty, 5, 1)             \
601   F(StoreGlobalIC_Miss, 4, 1)                \
602   F(StoreGlobalICNoFeedback_Miss, 2, 1)      \
603   F(StoreGlobalIC_Slow, 5, 1)                \
604   F(StoreIC_Miss, 5, 1)                      \
605   F(StoreInArrayLiteralIC_Slow, 5, 1)        \
606   F(StorePropertyWithInterceptor, 5, 1)      \
607   F(CloneObjectIC_Miss, 4, 1)                \
608   F(KeyedHasIC_Miss, 4, 1)                   \
609   F(HasElementWithInterceptor, 2, 1)
610 
611 #define FOR_EACH_INTRINSIC_RETURN_OBJECT_IMPL(F, I) \
612   FOR_EACH_INTRINSIC_ARRAY(F, I)                    \
613   FOR_EACH_INTRINSIC_ATOMICS(F, I)                  \
614   FOR_EACH_INTRINSIC_BIGINT(F, I)                   \
615   FOR_EACH_INTRINSIC_CLASSES(F, I)                  \
616   FOR_EACH_INTRINSIC_COLLECTIONS(F, I)              \
617   FOR_EACH_INTRINSIC_COMPILER(F, I)                 \
618   FOR_EACH_INTRINSIC_DATE(F, I)                     \
619   FOR_EACH_INTRINSIC_DEBUG(F, I)                    \
620   FOR_EACH_INTRINSIC_FORIN(F, I)                    \
621   FOR_EACH_INTRINSIC_FUNCTION(F, I)                 \
622   FOR_EACH_INTRINSIC_GENERATOR(F, I)                \
623   FOR_EACH_INTRINSIC_IC(F, I)                       \
624   FOR_EACH_INTRINSIC_INTERNAL(F, I)                 \
625   FOR_EACH_INTRINSIC_INTERPRETER(F, I)              \
626   FOR_EACH_INTRINSIC_INTL(F, I)                     \
627   FOR_EACH_INTRINSIC_LITERALS(F, I)                 \
628   FOR_EACH_INTRINSIC_MODULE(F, I)                   \
629   FOR_EACH_INTRINSIC_NUMBERS(F, I)                  \
630   FOR_EACH_INTRINSIC_OBJECT(F, I)                   \
631   FOR_EACH_INTRINSIC_OPERATORS(F, I)                \
632   FOR_EACH_INTRINSIC_PROMISE(F, I)                  \
633   FOR_EACH_INTRINSIC_PROXY(F, I)                    \
634   FOR_EACH_INTRINSIC_REGEXP(F, I)                   \
635   FOR_EACH_INTRINSIC_SCOPES(F, I)                   \
636   FOR_EACH_INTRINSIC_STRINGS(F, I)                  \
637   FOR_EACH_INTRINSIC_SYMBOL(F, I)                   \
638   FOR_EACH_INTRINSIC_TEST(F, I)                     \
639   FOR_EACH_INTRINSIC_TYPEDARRAY(F, I)               \
640   FOR_EACH_INTRINSIC_WASM(F, I)
641 
642 // Defines the list of all intrinsics, coming in 2 flavors, either returning an
643 // object or a pair.
644 #define FOR_EACH_INTRINSIC_IMPL(F, I)       \
645   FOR_EACH_INTRINSIC_RETURN_PAIR_IMPL(F, I) \
646   FOR_EACH_INTRINSIC_RETURN_OBJECT_IMPL(F, I)
647 
648 #define FOR_EACH_INTRINSIC_RETURN_OBJECT(F) \
649   FOR_EACH_INTRINSIC_RETURN_OBJECT_IMPL(F, F)
650 
651 #define FOR_EACH_INTRINSIC_RETURN_PAIR(F) \
652   FOR_EACH_INTRINSIC_RETURN_PAIR_IMPL(F, F)
653 
654 // The list of all intrinsics, including those that have inline versions, but
655 // not the inline versions themselves.
656 #define FOR_EACH_INTRINSIC(F) FOR_EACH_INTRINSIC_IMPL(F, F)
657 
658 // The list of all inline intrinsics only.
659 #define FOR_EACH_INLINE_INTRINSIC(I) FOR_EACH_INTRINSIC_IMPL(NOTHING, I)
660 
661 #define F(name, nargs, ressize)                                 \
662   Address Runtime_##name(int args_length, Address* args_object, \
663                          Isolate* isolate);
FOR_EACH_INTRINSIC_RETURN_OBJECT(F)664 FOR_EACH_INTRINSIC_RETURN_OBJECT(F)
665 #undef F
666 
667 //---------------------------------------------------------------------------
668 // Runtime provides access to all C++ runtime functions.
669 
670 class Runtime : public AllStatic {
671  public:
672   enum FunctionId : int32_t {
673 #define F(name, nargs, ressize) k##name,
674 #define I(name, nargs, ressize) kInline##name,
675     FOR_EACH_INTRINSIC(F) FOR_EACH_INLINE_INTRINSIC(I)
676 #undef I
677 #undef F
678         kNumFunctions,
679   };
680 
681   static constexpr int kNumInlineFunctions =
682 #define COUNT(...) +1
683       FOR_EACH_INLINE_INTRINSIC(COUNT);
684 #undef COUNT
685 
686   enum IntrinsicType { RUNTIME, INLINE };
687 
688   // Intrinsic function descriptor.
689   struct Function {
690     FunctionId function_id;
691     IntrinsicType intrinsic_type;
692     // The JS name of the function.
693     const char* name;
694 
695     // For RUNTIME functions, this is the C++ entry point.
696     // For INLINE functions this is the C++ entry point of the fall back.
697     Address entry;
698 
699     // The number of arguments expected. nargs is -1 if the function takes
700     // a variable number of arguments.
701     int8_t nargs;
702     // Size of result.  Most functions return a single pointer, size 1.
703     int8_t result_size;
704   };
705 
706   static const int kNotFound = -1;
707 
708   // Checks whether the runtime function with the given {id} depends on the
709   // "current context", i.e. because it does scoped lookups, or whether it's
710   // fine to just pass any context within the same "native context".
711   static bool NeedsExactContext(FunctionId id);
712 
713   // Checks whether the runtime function with the given {id} never returns
714   // to it's caller normally, i.e. whether it'll always raise an exception.
715   // More specifically: The C++ implementation returns the Heap::exception
716   // sentinel, always.
717   static bool IsNonReturning(FunctionId id);
718 
719   // Check if a runtime function with the given {id} may trigger a heap
720   // allocation.
721   static bool MayAllocate(FunctionId id);
722 
723   // Check if a runtime function with the given {id} is whitelisted for
724   // using it with fuzzers.
725   static bool IsWhitelistedForFuzzing(FunctionId id);
726 
727   // Get the intrinsic function with the given name.
728   static const Function* FunctionForName(const unsigned char* name, int length);
729 
730   // Get the intrinsic function with the given FunctionId.
731   V8_EXPORT_PRIVATE static const Function* FunctionForId(FunctionId id);
732 
733   // Get the intrinsic function with the given function entry address.
734   static const Function* FunctionForEntry(Address ref);
735 
736   // Get the runtime intrinsic function table.
737   static const Function* RuntimeFunctionTable(Isolate* isolate);
738 
739   V8_WARN_UNUSED_RESULT static Maybe<bool> DeleteObjectProperty(
740       Isolate* isolate, Handle<JSReceiver> receiver, Handle<Object> key,
741       LanguageMode language_mode);
742 
743   V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle<Object>
744   SetObjectProperty(Isolate* isolate, Handle<Object> object, Handle<Object> key,
745                     Handle<Object> value, StoreOrigin store_origin,
746                     Maybe<ShouldThrow> should_throw = Nothing<ShouldThrow>());
747 
748   V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle<Object>
749   GetObjectProperty(Isolate* isolate, Handle<Object> object, Handle<Object> key,
750                     bool* is_found_out = nullptr);
751 
752   V8_WARN_UNUSED_RESULT static MaybeHandle<Object> HasProperty(
753       Isolate* isolate, Handle<Object> object, Handle<Object> key);
754 
755   V8_WARN_UNUSED_RESULT static MaybeHandle<JSArray> GetInternalProperties(
756       Isolate* isolate, Handle<Object>);
757 
758   V8_WARN_UNUSED_RESULT static MaybeHandle<Object> ThrowIteratorError(
759       Isolate* isolate, Handle<Object> object);
760 };
761 
762 class RuntimeState {
763  public:
764 #ifndef V8_INTL_SUPPORT
to_upper_mapping()765   unibrow::Mapping<unibrow::ToUppercase, 128>* to_upper_mapping() {
766     return &to_upper_mapping_;
767   }
to_lower_mapping()768   unibrow::Mapping<unibrow::ToLowercase, 128>* to_lower_mapping() {
769     return &to_lower_mapping_;
770   }
771 #endif
772 
redirected_intrinsic_functions()773   Runtime::Function* redirected_intrinsic_functions() {
774     return redirected_intrinsic_functions_.get();
775   }
776 
set_redirected_intrinsic_functions(Runtime::Function * redirected_intrinsic_functions)777   void set_redirected_intrinsic_functions(
778       Runtime::Function* redirected_intrinsic_functions) {
779     redirected_intrinsic_functions_.reset(redirected_intrinsic_functions);
780   }
781 
782  private:
783   RuntimeState() = default;
784 #ifndef V8_INTL_SUPPORT
785   unibrow::Mapping<unibrow::ToUppercase, 128> to_upper_mapping_;
786   unibrow::Mapping<unibrow::ToLowercase, 128> to_lower_mapping_;
787 #endif
788 
789   std::unique_ptr<Runtime::Function[]> redirected_intrinsic_functions_;
790 
791   friend class Isolate;
792   friend class Runtime;
793 
794   DISALLOW_COPY_AND_ASSIGN(RuntimeState);
795 };
796 
797 V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream&, Runtime::FunctionId);
798 
799 //---------------------------------------------------------------------------
800 // Constants used by interface to runtime functions.
801 
802 using AllocateDoubleAlignFlag = base::BitField<bool, 0, 1>;
803 
804 using AllowLargeObjectAllocationFlag = base::BitField<bool, 1, 1>;
805 
806 // A set of bits returned by Runtime_GetOptimizationStatus.
807 // These bits must be in sync with bits defined in test/mjsunit/mjsunit.js
808 enum class OptimizationStatus {
809   kIsFunction = 1 << 0,
810   kNeverOptimize = 1 << 1,
811   kAlwaysOptimize = 1 << 2,
812   kMaybeDeopted = 1 << 3,
813   kOptimized = 1 << 4,
814   kTurboFanned = 1 << 5,
815   kInterpreted = 1 << 6,
816   kMarkedForOptimization = 1 << 7,
817   kMarkedForConcurrentOptimization = 1 << 8,
818   kOptimizingConcurrently = 1 << 9,
819   kIsExecuting = 1 << 10,
820   kTopmostFrameIsTurboFanned = 1 << 11,
821   kLiteMode = 1 << 12,
822   kMarkedForDeoptimization = 1 << 13,
823 };
824 
825 }  // namespace internal
826 }  // namespace v8
827 
828 #endif  // V8_RUNTIME_RUNTIME_H_
829