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