runtime.h revision 3bec4d28b1f388dbc06a9c4276e1a03e86c52b04
1// Copyright 2006-2008 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  \
84  /* Array join support */ \
85  F(PushIfAbsent, 2, 1) \
86  F(ArrayConcat, 1, 1) \
87  \
88  /* Conversions */ \
89  F(ToBool, 1, 1) \
90  F(Typeof, 1, 1) \
91  \
92  F(StringToNumber, 1, 1) \
93  F(StringFromCharCodeArray, 1, 1) \
94  F(StringParseInt, 2, 1) \
95  F(StringParseFloat, 1, 1) \
96  F(StringToLowerCase, 1, 1) \
97  F(StringToUpperCase, 1, 1) \
98  F(StringSplit, 3, 1) \
99  F(CharFromCode, 1, 1) \
100  F(URIEscape, 1, 1) \
101  F(URIUnescape, 1, 1) \
102  \
103  F(NumberToString, 1, 1) \
104  F(NumberToStringSkipCache, 1, 1) \
105  F(NumberToInteger, 1, 1) \
106  F(NumberToIntegerMapMinusZero, 1, 1) \
107  F(NumberToJSUint32, 1, 1) \
108  F(NumberToJSInt32, 1, 1) \
109  F(NumberToSmi, 1, 1) \
110  \
111  /* Arithmetic operations */ \
112  F(NumberAdd, 2, 1) \
113  F(NumberSub, 2, 1) \
114  F(NumberMul, 2, 1) \
115  F(NumberDiv, 2, 1) \
116  F(NumberMod, 2, 1) \
117  F(NumberUnaryMinus, 1, 1) \
118  F(NumberAlloc, 0, 1) \
119  \
120  F(StringAdd, 2, 1) \
121  F(StringBuilderConcat, 3, 1) \
122  \
123  /* Bit operations */ \
124  F(NumberOr, 2, 1) \
125  F(NumberAnd, 2, 1) \
126  F(NumberXor, 2, 1) \
127  F(NumberNot, 1, 1) \
128  \
129  F(NumberShl, 2, 1) \
130  F(NumberShr, 2, 1) \
131  F(NumberSar, 2, 1) \
132  \
133  /* Comparisons */ \
134  F(NumberEquals, 2, 1) \
135  F(StringEquals, 2, 1) \
136  \
137  F(NumberCompare, 3, 1) \
138  F(SmiLexicographicCompare, 2, 1) \
139  F(StringCompare, 2, 1) \
140  \
141  /* Math */ \
142  F(Math_acos, 1, 1) \
143  F(Math_asin, 1, 1) \
144  F(Math_atan, 1, 1) \
145  F(Math_atan2, 2, 1) \
146  F(Math_ceil, 1, 1) \
147  F(Math_cos, 1, 1) \
148  F(Math_exp, 1, 1) \
149  F(Math_floor, 1, 1) \
150  F(Math_log, 1, 1) \
151  F(Math_pow, 2, 1) \
152  F(Math_pow_cfunction, 2, 1) \
153  F(RoundNumber, 1, 1) \
154  F(Math_sin, 1, 1) \
155  F(Math_sqrt, 1, 1) \
156  F(Math_tan, 1, 1) \
157  \
158  /* Regular expressions */ \
159  F(RegExpCompile, 3, 1) \
160  F(RegExpExec, 4, 1) \
161  F(RegExpExecMultiple, 4, 1) \
162  F(RegExpInitializeObject, 5, 1) \
163  F(RegExpConstructResult, 3, 1) \
164  \
165  /* Strings */ \
166  F(StringCharCodeAt, 2, 1) \
167  F(StringIndexOf, 3, 1) \
168  F(StringLastIndexOf, 3, 1) \
169  F(StringLocaleCompare, 2, 1) \
170  F(SubString, 3, 1) \
171  F(StringReplaceRegExpWithString, 4, 1) \
172  F(StringMatch, 3, 1) \
173  F(StringTrim, 3, 1) \
174  F(StringToArray, 1, 1) \
175  \
176  /* Numbers */ \
177  F(NumberToRadixString, 2, 1) \
178  F(NumberToFixed, 2, 1) \
179  F(NumberToExponential, 2, 1) \
180  F(NumberToPrecision, 2, 1)
181
182#define RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
183  /* Reflection */ \
184  F(FunctionSetInstanceClassName, 2, 1) \
185  F(FunctionSetLength, 2, 1) \
186  F(FunctionSetPrototype, 2, 1) \
187  F(FunctionGetName, 1, 1) \
188  F(FunctionSetName, 2, 1) \
189  F(FunctionRemovePrototype, 1, 1) \
190  F(FunctionGetSourceCode, 1, 1) \
191  F(FunctionGetScript, 1, 1) \
192  F(FunctionGetScriptSourcePosition, 1, 1) \
193  F(FunctionGetPositionForOffset, 2, 1) \
194  F(FunctionIsAPIFunction, 1, 1) \
195  F(FunctionIsBuiltin, 1, 1) \
196  F(GetScript, 1, 1) \
197  F(CollectStackTrace, 2, 1) \
198  F(GetV8Version, 0, 1) \
199  \
200  F(ClassOf, 1, 1) \
201  F(SetCode, 2, 1) \
202  \
203  F(CreateApiFunction, 1, 1) \
204  F(IsTemplate, 1, 1) \
205  F(GetTemplateField, 2, 1) \
206  F(DisableAccessChecks, 1, 1) \
207  F(EnableAccessChecks, 1, 1) \
208  \
209  /* Dates */ \
210  F(DateCurrentTime, 0, 1) \
211  F(DateParseString, 2, 1) \
212  F(DateLocalTimezone, 1, 1) \
213  F(DateLocalTimeOffset, 0, 1) \
214  F(DateDaylightSavingsOffset, 1, 1) \
215  F(DateMakeDay, 3, 1) \
216  F(DateYMDFromTime, 2, 1) \
217  \
218  /* Numbers */ \
219  \
220  /* Globals */ \
221  F(CompileString, 2, 1) \
222  F(GlobalPrint, 1, 1) \
223  \
224  /* Eval */ \
225  F(GlobalReceiver, 1, 1) \
226  F(ResolvePossiblyDirectEval, 3, 2) \
227  F(ResolvePossiblyDirectEvalNoLookup, 3, 2) \
228  \
229  F(SetProperty, -1 /* 3 or 4 */, 1) \
230  F(DefineOrRedefineDataProperty, 4, 1) \
231  F(DefineOrRedefineAccessorProperty, 5, 1) \
232  F(IgnoreAttributesAndSetProperty, -1 /* 3 or 4 */, 1) \
233  \
234  /* Arrays */ \
235  F(RemoveArrayHoles, 2, 1) \
236  F(GetArrayKeys, 2, 1) \
237  F(MoveArrayContents, 2, 1) \
238  F(EstimateNumberOfElements, 1, 1) \
239  F(SwapElements, 3, 1) \
240  \
241  /* Getters and Setters */ \
242  F(DefineAccessor, -1 /* 4 or 5 */, 1) \
243  F(LookupAccessor, 3, 1) \
244  \
245  /* Literals */ \
246  F(MaterializeRegExpLiteral, 4, 1)\
247  F(CreateArrayLiteralBoilerplate, 3, 1) \
248  F(CloneLiteralBoilerplate, 1, 1) \
249  F(CloneShallowLiteralBoilerplate, 1, 1) \
250  F(CreateObjectLiteral, 4, 1) \
251  F(CreateObjectLiteralShallow, 4, 1) \
252  F(CreateArrayLiteral, 3, 1) \
253  F(CreateArrayLiteralShallow, 3, 1) \
254  \
255  /* Catch context extension objects */ \
256  F(CreateCatchExtensionObject, 2, 1) \
257  \
258  /* Statements */ \
259  F(NewClosure, 2, 1) \
260  F(NewObject, 1, 1) \
261  F(Throw, 1, 1) \
262  F(ReThrow, 1, 1) \
263  F(ThrowReferenceError, 1, 1) \
264  F(StackGuard, 1, 1) \
265  F(PromoteScheduledException, 0, 1) \
266  \
267  /* Contexts */ \
268  F(NewContext, 1, 1) \
269  F(PushContext, 1, 1) \
270  F(PushCatchContext, 1, 1) \
271  F(LookupContext, 2, 1) \
272  F(LoadContextSlot, 2, 2) \
273  F(LoadContextSlotNoReferenceError, 2, 2) \
274  F(StoreContextSlot, 3, 1) \
275  \
276  /* Declarations and initialization */ \
277  F(DeclareGlobals, 3, 1) \
278  F(DeclareContextSlot, 4, 1) \
279  F(InitializeVarGlobal, -1 /* 1 or 2 */, 1) \
280  F(InitializeConstGlobal, 2, 1) \
281  F(InitializeConstContextSlot, 3, 1) \
282  F(OptimizeObjectForAddingMultipleProperties, 2, 1) \
283  \
284  /* Debugging */ \
285  F(DebugPrint, 1, 1) \
286  F(DebugTrace, 0, 1) \
287  F(TraceEnter, 0, 1) \
288  F(TraceExit, 1, 1) \
289  F(Abort, 2, 1) \
290  /* Logging */ \
291  F(Log, 2, 1) \
292  /* ES5 */ \
293  F(LocalKeys, 1, 1) \
294  /* Handle scopes */ \
295  F(DeleteHandleScopeExtensions, 0, 1) \
296  /* Cache suport */ \
297  F(GetFromCache, 2, 1) \
298  \
299  /* Pseudo functions - handled as macros by parser */ \
300  F(IS_VAR, 1, 1)
301
302#ifdef ENABLE_DEBUGGER_SUPPORT
303#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
304  /* Debugger support*/ \
305  F(DebugBreak, 0, 1) \
306  F(SetDebugEventListener, 2, 1) \
307  F(Break, 0, 1) \
308  F(DebugGetPropertyDetails, 2, 1) \
309  F(DebugGetProperty, 2, 1) \
310  F(DebugPropertyTypeFromDetails, 1, 1) \
311  F(DebugPropertyAttributesFromDetails, 1, 1) \
312  F(DebugPropertyIndexFromDetails, 1, 1) \
313  F(DebugNamedInterceptorPropertyValue, 2, 1) \
314  F(DebugIndexedInterceptorElementValue, 2, 1) \
315  F(CheckExecutionState, 1, 1) \
316  F(GetFrameCount, 1, 1) \
317  F(GetFrameDetails, 2, 1) \
318  F(GetScopeCount, 2, 1) \
319  F(GetScopeDetails, 3, 1) \
320  F(DebugPrintScopes, 0, 1) \
321  F(GetCFrames, 1, 1) \
322  F(GetThreadCount, 1, 1) \
323  F(GetThreadDetails, 2, 1) \
324  F(GetBreakLocations, 1, 1) \
325  F(SetFunctionBreakPoint, 3, 1) \
326  F(SetScriptBreakPoint, 3, 1) \
327  F(ClearBreakPoint, 1, 1) \
328  F(ChangeBreakOnException, 2, 1) \
329  F(PrepareStep, 3, 1) \
330  F(ClearStepping, 0, 1) \
331  F(DebugEvaluate, 4, 1) \
332  F(DebugEvaluateGlobal, 3, 1) \
333  F(DebugGetLoadedScripts, 0, 1) \
334  F(DebugReferencedBy, 3, 1) \
335  F(DebugConstructedBy, 2, 1) \
336  F(DebugGetPrototype, 1, 1) \
337  F(SystemBreak, 0, 1) \
338  F(DebugDisassembleFunction, 1, 1) \
339  F(DebugDisassembleConstructor, 1, 1) \
340  F(FunctionGetInferredName, 1, 1) \
341  F(LiveEditFindSharedFunctionInfosForScript, 1, 1) \
342  F(LiveEditGatherCompileInfo, 2, 1) \
343  F(LiveEditReplaceScript, 3, 1) \
344  F(LiveEditReplaceFunctionCode, 2, 1) \
345  F(LiveEditFunctionSetScript, 2, 1) \
346  F(LiveEditReplaceRefToNestedFunction, 3, 1) \
347  F(LiveEditPatchFunctionPositions, 2, 1) \
348  F(LiveEditCheckAndDropActivations, 2, 1) \
349  F(LiveEditCompareStringsLinewise, 2, 1) \
350  F(GetFunctionCodePositionFromSource, 2, 1) \
351  F(ExecuteInDebugContext, 2, 1)
352#else
353#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
354#endif
355
356#ifdef ENABLE_LOGGING_AND_PROFILING
357#define RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F) \
358  F(ProfilerResume, 2, 1) \
359  F(ProfilerPause, 2, 1)
360#else
361#define RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F)
362#endif
363
364#ifdef DEBUG
365#define RUNTIME_FUNCTION_LIST_DEBUG(F) \
366  /* Testing */ \
367  F(ListNatives, 0, 1)
368#else
369#define RUNTIME_FUNCTION_LIST_DEBUG(F)
370#endif
371
372
373// ----------------------------------------------------------------------------
374// RUNTIME_FUNCTION_LIST defines all runtime functions accessed
375// either directly by id (via the code generator), or indirectly
376// via a native call by name (from within JS code).
377
378#define RUNTIME_FUNCTION_LIST(F) \
379  RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
380  RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
381  RUNTIME_FUNCTION_LIST_DEBUG(F) \
382  RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
383  RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F)
384
385// ----------------------------------------------------------------------------
386// Runtime provides access to all C++ runtime functions.
387
388class Runtime : public AllStatic {
389 public:
390  enum FunctionId {
391#define F(name, nargs, ressize) k##name,
392    RUNTIME_FUNCTION_LIST(F)
393    kNofFunctions
394#undef F
395  };
396
397  // Runtime function descriptor.
398  struct Function {
399    // The JS name of the function.
400    const char* name;
401
402    // The C++ (native) entry point.
403    byte* entry;
404
405    // The number of arguments expected; nargs < 0 if variable no. of
406    // arguments.
407    int nargs;
408    int stub_id;
409    // Size of result, if complex (larger than a single pointer),
410    // otherwise zero.
411    int result_size;
412  };
413
414  // Get the runtime function with the given function id.
415  static Function* FunctionForId(FunctionId fid);
416
417  // Get the runtime function with the given name.
418  static Function* FunctionForName(const char* name);
419
420  static int StringMatch(Handle<String> sub, Handle<String> pat, int index);
421
422  static bool IsUpperCaseChar(uint16_t ch);
423
424  // TODO(1240886): The following three methods are *not* handle safe,
425  // but accept handle arguments. This seems fragile.
426
427  // Support getting the characters in a string using [] notation as
428  // in Firefox/SpiderMonkey, Safari and Opera.
429  static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
430  static Object* GetElement(Handle<Object> object, uint32_t index);
431
432  static Object* SetObjectProperty(Handle<Object> object,
433                                   Handle<Object> key,
434                                   Handle<Object> value,
435                                   PropertyAttributes attr);
436
437  static Object* ForceSetObjectProperty(Handle<JSObject> object,
438                                        Handle<Object> key,
439                                        Handle<Object> value,
440                                        PropertyAttributes attr);
441
442  static Object* ForceDeleteObjectProperty(Handle<JSObject> object,
443                                           Handle<Object> key);
444
445  static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
446
447  // This function is used in FunctionNameUsing* tests.
448  static Object* FindSharedFunctionInfoInScript(Handle<Script> script,
449                                                int position);
450
451  // Helper functions used stubs.
452  static void PerformGC(Object* result);
453};
454
455
456} }  // namespace v8::internal
457
458#endif  // V8_RUNTIME_H_
459