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