runtime.h revision 0d5e116f6aee03185f237311a943491bb079a768
1// Copyright 2010 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#ifndef V8_RUNTIME_H_
29#define V8_RUNTIME_H_
30
31namespace v8 {
32namespace internal {
33
34// The interface to C++ runtime functions.
35
36// ----------------------------------------------------------------------------
37// RUNTIME_FUNCTION_LIST_ALWAYS defines runtime calls available in both
38// release and debug mode.
39// This macro should only be used by the macro RUNTIME_FUNCTION_LIST.
40
41// WARNING: RUNTIME_FUNCTION_LIST_ALWAYS_* is a very large macro that caused
42// MSVC Intellisense to crash.  It was broken into two macros to work around
43// this problem. Please avoid large recursive macros whenever possible.
44#define RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
45  /* Property access */ \
46  F(GetProperty, 2, 1) \
47  F(KeyedGetProperty, 2, 1) \
48  F(DeleteProperty, 2, 1) \
49  F(HasLocalProperty, 2, 1) \
50  F(HasProperty, 2, 1) \
51  F(HasElement, 2, 1) \
52  F(IsPropertyEnumerable, 2, 1) \
53  F(GetPropertyNames, 1, 1) \
54  F(GetPropertyNamesFast, 1, 1) \
55  F(GetLocalPropertyNames, 1, 1) \
56  F(GetLocalElementNames, 1, 1) \
57  F(GetInterceptorInfo, 1, 1) \
58  F(GetNamedInterceptorPropertyNames, 1, 1) \
59  F(GetIndexedInterceptorElementNames, 1, 1) \
60  F(GetArgumentsProperty, 1, 1) \
61  F(ToFastProperties, 1, 1) \
62  F(ToSlowProperties, 1, 1) \
63  F(FinishArrayPrototypeSetup, 1, 1) \
64  F(SpecialArrayFunctions, 1, 1) \
65  F(GetGlobalReceiver, 0, 1) \
66  \
67  F(IsInPrototypeChain, 2, 1) \
68  F(SetHiddenPrototype, 2, 1) \
69  \
70  F(IsConstructCall, 0, 1) \
71  \
72  F(GetOwnProperty, 2, 1) \
73  \
74  F(IsExtensible, 1, 1) \
75  F(PreventExtensions, 1, 1)\
76  \
77  /* Utilities */ \
78  F(GetFunctionDelegate, 1, 1) \
79  F(GetConstructorDelegate, 1, 1) \
80  F(NewArgumentsFast, 3, 1) \
81  F(LazyCompile, 1, 1) \
82  F(SetNewFunctionAttributes, 1, 1) \
83  F(AllocateInNewSpace, 1, 1) \
84  \
85  /* Array join support */ \
86  F(PushIfAbsent, 2, 1) \
87  F(ArrayConcat, 1, 1) \
88  \
89  /* Conversions */ \
90  F(ToBool, 1, 1) \
91  F(Typeof, 1, 1) \
92  \
93  F(StringToNumber, 1, 1) \
94  F(StringFromCharCodeArray, 1, 1) \
95  F(StringParseInt, 2, 1) \
96  F(StringParseFloat, 1, 1) \
97  F(StringToLowerCase, 1, 1) \
98  F(StringToUpperCase, 1, 1) \
99  F(StringSplit, 3, 1) \
100  F(CharFromCode, 1, 1) \
101  F(URIEscape, 1, 1) \
102  F(URIUnescape, 1, 1) \
103  \
104  F(NumberToString, 1, 1) \
105  F(NumberToStringSkipCache, 1, 1) \
106  F(NumberToInteger, 1, 1) \
107  F(NumberToIntegerMapMinusZero, 1, 1) \
108  F(NumberToJSUint32, 1, 1) \
109  F(NumberToJSInt32, 1, 1) \
110  F(NumberToSmi, 1, 1) \
111  \
112  /* Arithmetic operations */ \
113  F(NumberAdd, 2, 1) \
114  F(NumberSub, 2, 1) \
115  F(NumberMul, 2, 1) \
116  F(NumberDiv, 2, 1) \
117  F(NumberMod, 2, 1) \
118  F(NumberUnaryMinus, 1, 1) \
119  F(NumberAlloc, 0, 1) \
120  \
121  F(StringAdd, 2, 1) \
122  F(StringBuilderConcat, 3, 1) \
123  \
124  /* Bit operations */ \
125  F(NumberOr, 2, 1) \
126  F(NumberAnd, 2, 1) \
127  F(NumberXor, 2, 1) \
128  F(NumberNot, 1, 1) \
129  \
130  F(NumberShl, 2, 1) \
131  F(NumberShr, 2, 1) \
132  F(NumberSar, 2, 1) \
133  \
134  /* Comparisons */ \
135  F(NumberEquals, 2, 1) \
136  F(StringEquals, 2, 1) \
137  \
138  F(NumberCompare, 3, 1) \
139  F(SmiLexicographicCompare, 2, 1) \
140  F(StringCompare, 2, 1) \
141  \
142  /* Math */ \
143  F(Math_acos, 1, 1) \
144  F(Math_asin, 1, 1) \
145  F(Math_atan, 1, 1) \
146  F(Math_atan2, 2, 1) \
147  F(Math_ceil, 1, 1) \
148  F(Math_cos, 1, 1) \
149  F(Math_exp, 1, 1) \
150  F(Math_floor, 1, 1) \
151  F(Math_log, 1, 1) \
152  F(Math_pow, 2, 1) \
153  F(Math_pow_cfunction, 2, 1) \
154  F(RoundNumber, 1, 1) \
155  F(Math_sin, 1, 1) \
156  F(Math_sqrt, 1, 1) \
157  F(Math_tan, 1, 1) \
158  \
159  /* Regular expressions */ \
160  F(RegExpCompile, 3, 1) \
161  F(RegExpExec, 4, 1) \
162  F(RegExpExecMultiple, 4, 1) \
163  F(RegExpInitializeObject, 5, 1) \
164  F(RegExpConstructResult, 3, 1) \
165  F(RegExpCloneResult, 1, 1) \
166  \
167  /* Strings */ \
168  F(StringCharCodeAt, 2, 1) \
169  F(StringIndexOf, 3, 1) \
170  F(StringLastIndexOf, 3, 1) \
171  F(StringLocaleCompare, 2, 1) \
172  F(SubString, 3, 1) \
173  F(StringReplaceRegExpWithString, 4, 1) \
174  F(StringMatch, 3, 1) \
175  F(StringTrim, 3, 1) \
176  F(StringToArray, 1, 1) \
177  F(NewStringWrapper, 1, 1) \
178  \
179  /* Numbers */ \
180  F(NumberToRadixString, 2, 1) \
181  F(NumberToFixed, 2, 1) \
182  F(NumberToExponential, 2, 1) \
183  F(NumberToPrecision, 2, 1)
184
185#define RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
186  /* Reflection */ \
187  F(FunctionSetInstanceClassName, 2, 1) \
188  F(FunctionSetLength, 2, 1) \
189  F(FunctionSetPrototype, 2, 1) \
190  F(FunctionGetName, 1, 1) \
191  F(FunctionSetName, 2, 1) \
192  F(FunctionRemovePrototype, 1, 1) \
193  F(FunctionGetSourceCode, 1, 1) \
194  F(FunctionGetScript, 1, 1) \
195  F(FunctionGetScriptSourcePosition, 1, 1) \
196  F(FunctionGetPositionForOffset, 2, 1) \
197  F(FunctionIsAPIFunction, 1, 1) \
198  F(FunctionIsBuiltin, 1, 1) \
199  F(GetScript, 1, 1) \
200  F(CollectStackTrace, 2, 1) \
201  F(GetV8Version, 0, 1) \
202  \
203  F(ClassOf, 1, 1) \
204  F(SetCode, 2, 1) \
205  F(SetExpectedNumberOfProperties, 2, 1) \
206  \
207  F(CreateApiFunction, 1, 1) \
208  F(IsTemplate, 1, 1) \
209  F(GetTemplateField, 2, 1) \
210  F(DisableAccessChecks, 1, 1) \
211  F(EnableAccessChecks, 1, 1) \
212  \
213  /* Dates */ \
214  F(DateCurrentTime, 0, 1) \
215  F(DateParseString, 2, 1) \
216  F(DateLocalTimezone, 1, 1) \
217  F(DateLocalTimeOffset, 0, 1) \
218  F(DateDaylightSavingsOffset, 1, 1) \
219  F(DateMakeDay, 3, 1) \
220  F(DateYMDFromTime, 2, 1) \
221  \
222  /* Numbers */ \
223  \
224  /* Globals */ \
225  F(CompileString, 2, 1) \
226  F(GlobalPrint, 1, 1) \
227  \
228  /* Eval */ \
229  F(GlobalReceiver, 1, 1) \
230  F(ResolvePossiblyDirectEval, 3, 2) \
231  F(ResolvePossiblyDirectEvalNoLookup, 3, 2) \
232  \
233  F(SetProperty, -1 /* 3 or 4 */, 1) \
234  F(DefineOrRedefineDataProperty, 4, 1) \
235  F(DefineOrRedefineAccessorProperty, 5, 1) \
236  F(IgnoreAttributesAndSetProperty, -1 /* 3 or 4 */, 1) \
237  \
238  /* Arrays */ \
239  F(RemoveArrayHoles, 2, 1) \
240  F(GetArrayKeys, 2, 1) \
241  F(MoveArrayContents, 2, 1) \
242  F(EstimateNumberOfElements, 1, 1) \
243  F(SwapElements, 3, 1) \
244  \
245  /* Getters and Setters */ \
246  F(DefineAccessor, -1 /* 4 or 5 */, 1) \
247  F(LookupAccessor, 3, 1) \
248  \
249  /* Literals */ \
250  F(MaterializeRegExpLiteral, 4, 1)\
251  F(CreateArrayLiteralBoilerplate, 3, 1) \
252  F(CloneLiteralBoilerplate, 1, 1) \
253  F(CloneShallowLiteralBoilerplate, 1, 1) \
254  F(CreateObjectLiteral, 4, 1) \
255  F(CreateObjectLiteralShallow, 4, 1) \
256  F(CreateArrayLiteral, 3, 1) \
257  F(CreateArrayLiteralShallow, 3, 1) \
258  \
259  /* Catch context extension objects */ \
260  F(CreateCatchExtensionObject, 2, 1) \
261  \
262  /* Statements */ \
263  F(NewClosure, 2, 1) \
264  F(NewObject, 1, 1) \
265  F(NewObjectFromBound, 2, 1) \
266  F(FinalizeInstanceSize, 1, 1) \
267  F(Throw, 1, 1) \
268  F(ReThrow, 1, 1) \
269  F(ThrowReferenceError, 1, 1) \
270  F(StackGuard, 1, 1) \
271  F(PromoteScheduledException, 0, 1) \
272  \
273  /* Contexts */ \
274  F(NewContext, 1, 1) \
275  F(PushContext, 1, 1) \
276  F(PushCatchContext, 1, 1) \
277  F(LookupContext, 2, 1) \
278  F(LoadContextSlot, 2, 2) \
279  F(LoadContextSlotNoReferenceError, 2, 2) \
280  F(StoreContextSlot, 3, 1) \
281  \
282  /* Declarations and initialization */ \
283  F(DeclareGlobals, 3, 1) \
284  F(DeclareContextSlot, 4, 1) \
285  F(InitializeVarGlobal, -1 /* 1 or 2 */, 1) \
286  F(InitializeConstGlobal, 2, 1) \
287  F(InitializeConstContextSlot, 3, 1) \
288  F(OptimizeObjectForAddingMultipleProperties, 2, 1) \
289  \
290  /* Debugging */ \
291  F(DebugPrint, 1, 1) \
292  F(DebugTrace, 0, 1) \
293  F(TraceEnter, 0, 1) \
294  F(TraceExit, 1, 1) \
295  F(Abort, 2, 1) \
296  /* Logging */ \
297  F(Log, 2, 1) \
298  /* ES5 */ \
299  F(LocalKeys, 1, 1) \
300  /* Handle scopes */ \
301  F(DeleteHandleScopeExtensions, 0, 1) \
302  /* Cache suport */ \
303  F(GetFromCache, 2, 1) \
304  \
305  /* Pseudo functions - handled as macros by parser */ \
306  F(IS_VAR, 1, 1)
307
308#ifdef ENABLE_DEBUGGER_SUPPORT
309#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
310  /* Debugger support*/ \
311  F(DebugBreak, 0, 1) \
312  F(SetDebugEventListener, 2, 1) \
313  F(Break, 0, 1) \
314  F(DebugGetPropertyDetails, 2, 1) \
315  F(DebugGetProperty, 2, 1) \
316  F(DebugPropertyTypeFromDetails, 1, 1) \
317  F(DebugPropertyAttributesFromDetails, 1, 1) \
318  F(DebugPropertyIndexFromDetails, 1, 1) \
319  F(DebugNamedInterceptorPropertyValue, 2, 1) \
320  F(DebugIndexedInterceptorElementValue, 2, 1) \
321  F(CheckExecutionState, 1, 1) \
322  F(GetFrameCount, 1, 1) \
323  F(GetFrameDetails, 2, 1) \
324  F(GetScopeCount, 2, 1) \
325  F(GetScopeDetails, 3, 1) \
326  F(DebugPrintScopes, 0, 1) \
327  F(GetCFrames, 1, 1) \
328  F(GetThreadCount, 1, 1) \
329  F(GetThreadDetails, 2, 1) \
330  F(SetDisableBreak, 1, 1) \
331  F(GetBreakLocations, 1, 1) \
332  F(SetFunctionBreakPoint, 3, 1) \
333  F(SetScriptBreakPoint, 3, 1) \
334  F(ClearBreakPoint, 1, 1) \
335  F(ChangeBreakOnException, 2, 1) \
336  F(IsBreakOnException, 1, 1) \
337  F(PrepareStep, 3, 1) \
338  F(ClearStepping, 0, 1) \
339  F(DebugEvaluate, 4, 1) \
340  F(DebugEvaluateGlobal, 3, 1) \
341  F(DebugGetLoadedScripts, 0, 1) \
342  F(DebugReferencedBy, 3, 1) \
343  F(DebugConstructedBy, 2, 1) \
344  F(DebugGetPrototype, 1, 1) \
345  F(SystemBreak, 0, 1) \
346  F(DebugDisassembleFunction, 1, 1) \
347  F(DebugDisassembleConstructor, 1, 1) \
348  F(FunctionGetInferredName, 1, 1) \
349  F(LiveEditFindSharedFunctionInfosForScript, 1, 1) \
350  F(LiveEditGatherCompileInfo, 2, 1) \
351  F(LiveEditReplaceScript, 3, 1) \
352  F(LiveEditReplaceFunctionCode, 2, 1) \
353  F(LiveEditFunctionSetScript, 2, 1) \
354  F(LiveEditReplaceRefToNestedFunction, 3, 1) \
355  F(LiveEditPatchFunctionPositions, 2, 1) \
356  F(LiveEditCheckAndDropActivations, 2, 1) \
357  F(LiveEditCompareStringsLinewise, 2, 1) \
358  F(GetFunctionCodePositionFromSource, 2, 1) \
359  F(ExecuteInDebugContext, 2, 1)
360#else
361#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
362#endif
363
364#ifdef ENABLE_LOGGING_AND_PROFILING
365#define RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F) \
366  F(ProfilerResume, 2, 1) \
367  F(ProfilerPause, 2, 1)
368#else
369#define RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F)
370#endif
371
372#ifdef DEBUG
373#define RUNTIME_FUNCTION_LIST_DEBUG(F) \
374  /* Testing */ \
375  F(ListNatives, 0, 1)
376#else
377#define RUNTIME_FUNCTION_LIST_DEBUG(F)
378#endif
379
380
381// ----------------------------------------------------------------------------
382// RUNTIME_FUNCTION_LIST defines all runtime functions accessed
383// either directly by id (via the code generator), or indirectly
384// via a native call by name (from within JS code).
385
386#define RUNTIME_FUNCTION_LIST(F) \
387  RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
388  RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
389  RUNTIME_FUNCTION_LIST_DEBUG(F) \
390  RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
391  RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F)
392
393// ----------------------------------------------------------------------------
394// INLINE_FUNCTION_LIST defines all inlined functions accessed
395// with a native call of the form %_name from within JS code.
396// Entries have the form F(name, number of arguments, number of return values).
397#define INLINE_FUNCTION_LIST(F) \
398  F(IsSmi, 1, 1)                                                             \
399  F(IsNonNegativeSmi, 1, 1)                                                  \
400  F(IsArray, 1, 1)                                                           \
401  F(IsRegExp, 1, 1)                                                          \
402  F(CallFunction, -1 /* receiver + n args + function */, 1)                  \
403  F(ArgumentsLength, 0, 1)                                                   \
404  F(Arguments, 1, 1)                                                         \
405  F(ValueOf, 1, 1)                                                           \
406  F(SetValueOf, 2, 1)                                                        \
407  F(StringCharFromCode, 1, 1)                                                \
408  F(StringCharAt, 2, 1)                                                      \
409  F(ObjectEquals, 2, 1)                                                      \
410  F(RandomHeapNumber, 0, 1)                                                  \
411  F(IsObject, 1, 1)                                                          \
412  F(IsFunction, 1, 1)                                                        \
413  F(IsUndetectableObject, 1, 1)                                              \
414  F(IsSpecObject, 1, 1)                                                      \
415  F(IsStringWrapperSafeForDefaultValueOf, 1, 1)                              \
416  F(MathPow, 2, 1)                                                           \
417  F(MathSin, 1, 1)                                                           \
418  F(MathCos, 1, 1)                                                           \
419  F(MathSqrt, 1, 1)                                                          \
420  F(IsRegExpEquivalent, 2, 1)                                                \
421  F(HasCachedArrayIndex, 1, 1)                                               \
422  F(GetCachedArrayIndex, 1, 1)
423
424
425// ----------------------------------------------------------------------------
426// INLINE_AND_RUNTIME_FUNCTION_LIST defines all inlined functions accessed
427// with a native call of the form %_name from within JS code that also have
428  // a corresponding runtime function, that is called for slow cases.
429// Entries have the form F(name, number of arguments, number of return values).
430#define INLINE_RUNTIME_FUNCTION_LIST(F) \
431  F(IsConstructCall, 0, 1)                                                   \
432  F(ClassOf, 1, 1)                                                           \
433  F(StringCharCodeAt, 2, 1)                                                  \
434  F(Log, 3, 1)                                                               \
435  F(StringAdd, 2, 1)                                                         \
436  F(SubString, 3, 1)                                                         \
437  F(StringCompare, 2, 1)                                                     \
438  F(RegExpExec, 4, 1)                                                        \
439  F(RegExpConstructResult, 3, 1)                                             \
440  F(RegExpCloneResult, 1, 1)                                                 \
441  F(GetFromCache, 2, 1)                                                      \
442  F(NumberToString, 1, 1)                                                    \
443  F(SwapElements, 3, 1)
444
445
446//---------------------------------------------------------------------------
447// Runtime provides access to all C++ runtime functions.
448
449class Runtime : public AllStatic {
450 public:
451  enum FunctionId {
452#define F(name, nargs, ressize) k##name,
453    RUNTIME_FUNCTION_LIST(F)
454#undef F
455#define F(name, nargs, ressize) kInline##name,
456    INLINE_FUNCTION_LIST(F)
457    INLINE_RUNTIME_FUNCTION_LIST(F)
458#undef F
459    kNumFunctions,
460    kFirstInlineFunction = kInlineIsSmi
461  };
462
463  enum IntrinsicType {
464    RUNTIME,
465    INLINE
466  };
467
468  // Intrinsic function descriptor.
469  struct Function {
470    FunctionId function_id;
471    IntrinsicType intrinsic_type;
472    // The JS name of the function.
473    const char* name;
474
475    // The C++ (native) entry point.  NULL if the function is inlined.
476    byte* entry;
477
478    // The number of arguments expected. nargs is -1 if the function takes
479    // a variable number of arguments.
480    int nargs;
481    // Size of result.  Most functions return a single pointer, size 1.
482    int result_size;
483  };
484
485  static const int kNotFound = -1;
486
487  // Add symbols for all the intrinsic function names to a StringDictionary.
488  // Returns failure if an allocation fails.  In this case, it must be
489  // retried with a new, empty StringDictionary, not with the same one.
490  // Alternatively, heap initialization can be completely restarted.
491  static Object* InitializeIntrinsicFunctionNames(Object* dictionary);
492
493  // Get the intrinsic function with the given name, which must be a symbol.
494  static Function* FunctionForSymbol(Handle<String> name);
495
496  // Get the intrinsic function with the given FunctionId.
497  static Function* FunctionForId(FunctionId id);
498
499  // General-purpose helper functions for runtime system.
500  static int StringMatch(Handle<String> sub, Handle<String> pat, int index);
501
502  static bool IsUpperCaseChar(uint16_t ch);
503
504  // TODO(1240886): The following three methods are *not* handle safe,
505  // but accept handle arguments. This seems fragile.
506
507  // Support getting the characters in a string using [] notation as
508  // in Firefox/SpiderMonkey, Safari and Opera.
509  static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
510  static Object* GetElement(Handle<Object> object, uint32_t index);
511
512  static Object* SetObjectProperty(Handle<Object> object,
513                                   Handle<Object> key,
514                                   Handle<Object> value,
515                                   PropertyAttributes attr);
516
517  static Object* ForceSetObjectProperty(Handle<JSObject> object,
518                                        Handle<Object> key,
519                                        Handle<Object> value,
520                                        PropertyAttributes attr);
521
522  static Object* ForceDeleteObjectProperty(Handle<JSObject> object,
523                                           Handle<Object> key);
524
525  static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
526
527  // This function is used in FunctionNameUsing* tests.
528  static Object* FindSharedFunctionInfoInScript(Handle<Script> script,
529                                                int position);
530
531  // Helper functions used stubs.
532  static void PerformGC(Object* result);
533};
534
535
536} }  // namespace v8::internal
537
538#endif  // V8_RUNTIME_H_
539