• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 namespace v8 {
32 namespace 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) \
47   F(KeyedGetProperty, 2) \
48   F(DeleteProperty, 2) \
49   F(HasLocalProperty, 2) \
50   F(HasProperty, 2) \
51   F(HasElement, 2) \
52   F(IsPropertyEnumerable, 2) \
53   F(GetPropertyNames, 1) \
54   F(GetPropertyNamesFast, 1) \
55   F(GetArgumentsProperty, 1) \
56   F(ToFastProperties, 1) \
57   F(ToSlowProperties, 1) \
58   \
59   F(IsInPrototypeChain, 2) \
60   F(SetHiddenPrototype, 2) \
61   \
62   F(IsConstructCall, 0) \
63   \
64   /* Utilities */ \
65   F(GetCalledFunction, 0) \
66   F(GetFunctionDelegate, 1) \
67   F(GetConstructorDelegate, 1) \
68   F(NewArguments, 1) \
69   F(NewArgumentsFast, 3) \
70   F(LazyCompile, 1) \
71   F(SetNewFunctionAttributes, 1) \
72   \
73   /* Array join support */ \
74   F(PushIfAbsent, 2) \
75   F(ArrayConcat, 1) \
76   \
77   /* Conversions */ \
78   F(ToBool, 1) \
79   F(Typeof, 1) \
80   \
81   F(StringToNumber, 1) \
82   F(StringFromCharCodeArray, 1) \
83   F(StringParseInt, 2) \
84   F(StringParseFloat, 1) \
85   F(StringToLowerCase, 1) \
86   F(StringToUpperCase, 1) \
87   F(CharFromCode, 1) \
88   F(URIEscape, 1) \
89   F(URIUnescape, 1) \
90   \
91   F(NumberToString, 1) \
92   F(NumberToInteger, 1) \
93   F(NumberToJSUint32, 1) \
94   F(NumberToJSInt32, 1) \
95   F(NumberToSmi, 1) \
96   \
97   /* Arithmetic operations */ \
98   F(NumberAdd, 2) \
99   F(NumberSub, 2) \
100   F(NumberMul, 2) \
101   F(NumberDiv, 2) \
102   F(NumberMod, 2) \
103   F(NumberUnaryMinus, 1) \
104   \
105   F(StringAdd, 2) \
106   F(StringBuilderConcat, 2) \
107   \
108   /* Bit operations */ \
109   F(NumberOr, 2) \
110   F(NumberAnd, 2) \
111   F(NumberXor, 2) \
112   F(NumberNot, 1) \
113   \
114   F(NumberShl, 2) \
115   F(NumberShr, 2) \
116   F(NumberSar, 2) \
117   \
118   /* Comparisons */ \
119   F(NumberEquals, 2) \
120   F(StringEquals, 2) \
121   \
122   F(NumberCompare, 3) \
123   F(SmiLexicographicCompare, 2) \
124   F(StringCompare, 2) \
125   \
126   /* Math */ \
127   F(Math_abs, 1) \
128   F(Math_acos, 1) \
129   F(Math_asin, 1) \
130   F(Math_atan, 1) \
131   F(Math_atan2, 2) \
132   F(Math_ceil, 1) \
133   F(Math_cos, 1) \
134   F(Math_exp, 1) \
135   F(Math_floor, 1) \
136   F(Math_log, 1) \
137   F(Math_pow, 2) \
138   F(Math_round, 1) \
139   F(Math_sin, 1) \
140   F(Math_sqrt, 1) \
141   F(Math_tan, 1) \
142   \
143   /* Regular expressions */ \
144   F(RegExpCompile, 3) \
145   F(RegExpExec, 4) \
146   \
147   /* Strings */ \
148   F(StringCharCodeAt, 2) \
149   F(StringIndexOf, 3) \
150   F(StringLastIndexOf, 3) \
151   F(StringLocaleCompare, 2) \
152   F(StringSlice, 3) \
153   F(StringReplaceRegExpWithString, 4) \
154   F(StringMatch, 3) \
155   \
156   /* Numbers */ \
157   F(NumberToRadixString, 2) \
158   F(NumberToFixed, 2) \
159   F(NumberToExponential, 2) \
160   F(NumberToPrecision, 2)
161 
162 #define RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
163   /* Reflection */ \
164   F(FunctionSetInstanceClassName, 2) \
165   F(FunctionSetLength, 2) \
166   F(FunctionSetPrototype, 2) \
167   F(FunctionGetName, 1) \
168   F(FunctionSetName, 2) \
169   F(FunctionGetSourceCode, 1) \
170   F(FunctionGetScript, 1) \
171   F(FunctionGetScriptSourcePosition, 1) \
172   F(FunctionGetPositionForOffset, 2) \
173   F(FunctionIsAPIFunction, 1) \
174   F(GetScript, 1) \
175   F(CollectStackTrace, 2) \
176   \
177   F(ClassOf, 1) \
178   F(SetCode, 2) \
179   \
180   F(CreateApiFunction, 1) \
181   F(IsTemplate, 1) \
182   F(GetTemplateField, 2) \
183   F(DisableAccessChecks, 1) \
184   F(EnableAccessChecks, 1) \
185   \
186   /* Dates */ \
187   F(DateCurrentTime, 0) \
188   F(DateParseString, 2) \
189   F(DateLocalTimezone, 1) \
190   F(DateLocalTimeOffset, 0) \
191   F(DateDaylightSavingsOffset, 1) \
192   \
193   /* Numbers */ \
194   F(NumberIsFinite, 1) \
195   \
196   /* Globals */ \
197   F(CompileString, 2) \
198   F(GlobalPrint, 1) \
199   \
200   /* Eval */ \
201   F(GlobalReceiver, 1) \
202   F(ResolvePossiblyDirectEval, 2) \
203   \
204   F(SetProperty, -1 /* 3 or 4 */) \
205   F(IgnoreAttributesAndSetProperty, -1 /* 3 or 4 */) \
206   \
207   /* Arrays */ \
208   F(RemoveArrayHoles, 2) \
209   F(GetArrayKeys, 2) \
210   F(MoveArrayContents, 2) \
211   F(EstimateNumberOfElements, 1) \
212   \
213   /* Getters and Setters */ \
214   F(DefineAccessor, -1 /* 4 or 5 */) \
215   F(LookupAccessor, 3) \
216   \
217   /* Literals */ \
218   F(MaterializeRegExpLiteral, 4)\
219   F(CreateArrayLiteralBoilerplate, 3) \
220   F(CreateObjectLiteralBoilerplate, 3) \
221   F(CloneLiteralBoilerplate, 1) \
222   F(CloneShallowLiteralBoilerplate, 1) \
223   \
224   /* Catch context extension objects */ \
225   F(CreateCatchExtensionObject, 2) \
226   \
227   /* Statements */ \
228   F(NewClosure, 2) \
229   F(NewObject, 1) \
230   F(Throw, 1) \
231   F(ReThrow, 1) \
232   F(ThrowReferenceError, 1) \
233   F(StackGuard, 1) \
234   \
235   /* Contexts */ \
236   F(NewContext, 1) \
237   F(PushContext, 1) \
238   F(PushCatchContext, 1) \
239   F(LookupContext, 2) \
240   F(LoadContextSlot, 2) \
241   F(LoadContextSlotNoReferenceError, 2) \
242   F(StoreContextSlot, 3) \
243   \
244   /* Declarations and initialization */ \
245   F(DeclareGlobals, 3) \
246   F(DeclareContextSlot, 4) \
247   F(InitializeVarGlobal, -1 /* 1 or 2 */) \
248   F(InitializeConstGlobal, 2) \
249   F(InitializeConstContextSlot, 3) \
250   F(OptimizeObjectForAddingMultipleProperties, 2) \
251   F(TransformToFastProperties, 1) \
252   \
253   /* Debugging */ \
254   F(DebugPrint, 1) \
255   F(DebugTrace, 0) \
256   F(TraceEnter, 0) \
257   F(TraceExit, 1) \
258   F(Abort, 2) \
259   /* Logging */ \
260   F(Log, 2) \
261   \
262   /* Pseudo functions - handled as macros by parser */ \
263   F(IS_VAR, 1)
264 
265 #ifdef ENABLE_DEBUGGER_SUPPORT
266 #define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
267   /* Debugger support*/ \
268   F(DebugBreak, 0) \
269   F(SetDebugEventListener, 2) \
270   F(Break, 0) \
271   F(DebugGetPropertyDetails, 2) \
272   F(DebugGetProperty, 2) \
273   F(DebugLocalPropertyNames, 1) \
274   F(DebugLocalElementNames, 1) \
275   F(DebugPropertyTypeFromDetails, 1) \
276   F(DebugPropertyAttributesFromDetails, 1) \
277   F(DebugPropertyIndexFromDetails, 1) \
278   F(DebugInterceptorInfo, 1) \
279   F(DebugNamedInterceptorPropertyNames, 1) \
280   F(DebugIndexedInterceptorElementNames, 1) \
281   F(DebugNamedInterceptorPropertyValue, 2) \
282   F(DebugIndexedInterceptorElementValue, 2) \
283   F(CheckExecutionState, 1) \
284   F(GetFrameCount, 1) \
285   F(GetFrameDetails, 2) \
286   F(GetScopeCount, 2) \
287   F(GetScopeDetails, 3) \
288   F(DebugPrintScopes, 0) \
289   F(GetCFrames, 1) \
290   F(GetThreadCount, 1) \
291   F(GetThreadDetails, 2) \
292   F(GetBreakLocations, 1) \
293   F(SetFunctionBreakPoint, 3) \
294   F(SetScriptBreakPoint, 3) \
295   F(ClearBreakPoint, 1) \
296   F(ChangeBreakOnException, 2) \
297   F(PrepareStep, 3) \
298   F(ClearStepping, 0) \
299   F(DebugEvaluate, 4) \
300   F(DebugEvaluateGlobal, 3) \
301   F(DebugGetLoadedScripts, 0) \
302   F(DebugReferencedBy, 3) \
303   F(DebugConstructedBy, 2) \
304   F(DebugGetPrototype, 1) \
305   F(SystemBreak, 0) \
306   F(DebugDisassembleFunction, 1) \
307   F(DebugDisassembleConstructor, 1) \
308   F(FunctionGetInferredName, 1)
309 #else
310 #define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
311 #endif
312 
313 #ifdef DEBUG
314 #define RUNTIME_FUNCTION_LIST_DEBUG(F) \
315   /* Testing */ \
316   F(ListNatives, 0)
317 #else
318 #define RUNTIME_FUNCTION_LIST_DEBUG(F)
319 #endif
320 
321 
322 // ----------------------------------------------------------------------------
323 // RUNTIME_FUNCTION_LIST defines all runtime functions accessed
324 // either directly by id (via the code generator), or indirectly
325 // via a native call by name (from within JS code).
326 
327 #define RUNTIME_FUNCTION_LIST(F) \
328   RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
329   RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
330   RUNTIME_FUNCTION_LIST_DEBUG(F) \
331   RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
332 
333 // ----------------------------------------------------------------------------
334 // Runtime provides access to all C++ runtime functions.
335 
336 class Runtime : public AllStatic {
337  public:
338   enum FunctionId {
339 #define F(name, nargs) k##name,
340     RUNTIME_FUNCTION_LIST(F)
341     kNofFunctions
342 #undef F
343   };
344 
345   // Runtime function descriptor.
346   struct Function {
347     // The JS name of the function.
348     const char* name;
349 
350     // The name of the stub that calls the runtime function.
351     const char* stub_name;
352 
353     // The C++ (native) entry point.
354     byte* entry;
355 
356     // The number of arguments expected; nargs < 0 if variable no. of
357     // arguments.
358     int nargs;
359     int stub_id;
360   };
361 
362   // Get the runtime function with the given function id.
363   static Function* FunctionForId(FunctionId fid);
364 
365   // Get the runtime function with the given name.
366   static Function* FunctionForName(const char* name);
367 
368   static int StringMatch(Handle<String> sub, Handle<String> pat, int index);
369 
370   static bool IsUpperCaseChar(uint16_t ch);
371 
372   // TODO(1240886): The following three methods are *not* handle safe,
373   // but accept handle arguments. This seems fragile.
374 
375   // Support getting the characters in a string using [] notation as
376   // in Firefox/SpiderMonkey, Safari and Opera.
377   static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
378 
379   static Object* SetObjectProperty(Handle<Object> object,
380                                    Handle<Object> key,
381                                    Handle<Object> value,
382                                    PropertyAttributes attr);
383 
384   static Object* ForceSetObjectProperty(Handle<JSObject> object,
385                                         Handle<Object> key,
386                                         Handle<Object> value,
387                                         PropertyAttributes attr);
388 
389   static Object* ForceDeleteObjectProperty(Handle<JSObject> object,
390                                            Handle<Object> key);
391 
392   static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
393 
394   // This function is used in FunctionNameUsing* tests.
395   static Object* FindSharedFunctionInfoInScript(Handle<Script> script,
396                                                 int position);
397 
398   // Helper functions used stubs.
399   static void PerformGC(Object* result);
400 };
401 
402 
403 } }  // namespace v8::internal
404 
405 #endif  // V8_RUNTIME_H_
406