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