blob: 6b1ce480b5b459b5a73b6ce10246351b0a237ff8 [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) \
152 F(StringSlice, 3, 1) \
153 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) \
237 \
238 /* Contexts */ \
239 F(NewContext, 1, 1) \
240 F(PushContext, 1, 1) \
241 F(PushCatchContext, 1, 1) \
242 F(LookupContext, 2, 1) \
243 F(LoadContextSlot, 2, 2) \
244 F(LoadContextSlotNoReferenceError, 2, 2) \
245 F(StoreContextSlot, 3, 1) \
246 \
247 /* Declarations and initialization */ \
248 F(DeclareGlobals, 3, 1) \
249 F(DeclareContextSlot, 4, 1) \
250 F(InitializeVarGlobal, -1 /* 1 or 2 */, 1) \
251 F(InitializeConstGlobal, 2, 1) \
252 F(InitializeConstContextSlot, 3, 1) \
253 F(OptimizeObjectForAddingMultipleProperties, 2, 1) \
254 F(TransformToFastProperties, 1, 1) \
255 \
256 /* Debugging */ \
257 F(DebugPrint, 1, 1) \
258 F(DebugTrace, 0, 1) \
259 F(TraceEnter, 0, 1) \
260 F(TraceExit, 1, 1) \
261 F(Abort, 2, 1) \
262 /* Logging */ \
263 F(Log, 2, 1) \
264 /* ES5 */ \
265 F(LocalKeys, 1, 1) \
266 \
267 /* Pseudo functions - handled as macros by parser */ \
268 F(IS_VAR, 1, 1)
269
270#ifdef ENABLE_DEBUGGER_SUPPORT
271#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
272 /* Debugger support*/ \
273 F(DebugBreak, 0, 1) \
274 F(SetDebugEventListener, 2, 1) \
275 F(Break, 0, 1) \
276 F(DebugGetPropertyDetails, 2, 1) \
277 F(DebugGetProperty, 2, 1) \
278 F(DebugLocalPropertyNames, 1, 1) \
279 F(DebugLocalElementNames, 1, 1) \
280 F(DebugPropertyTypeFromDetails, 1, 1) \
281 F(DebugPropertyAttributesFromDetails, 1, 1) \
282 F(DebugPropertyIndexFromDetails, 1, 1) \
283 F(DebugInterceptorInfo, 1, 1) \
284 F(DebugNamedInterceptorPropertyNames, 1, 1) \
285 F(DebugIndexedInterceptorElementNames, 1, 1) \
286 F(DebugNamedInterceptorPropertyValue, 2, 1) \
287 F(DebugIndexedInterceptorElementValue, 2, 1) \
288 F(CheckExecutionState, 1, 1) \
289 F(GetFrameCount, 1, 1) \
290 F(GetFrameDetails, 2, 1) \
291 F(GetScopeCount, 2, 1) \
292 F(GetScopeDetails, 3, 1) \
293 F(DebugPrintScopes, 0, 1) \
294 F(GetCFrames, 1, 1) \
295 F(GetThreadCount, 1, 1) \
296 F(GetThreadDetails, 2, 1) \
297 F(GetBreakLocations, 1, 1) \
298 F(SetFunctionBreakPoint, 3, 1) \
299 F(SetScriptBreakPoint, 3, 1) \
300 F(ClearBreakPoint, 1, 1) \
301 F(ChangeBreakOnException, 2, 1) \
302 F(PrepareStep, 3, 1) \
303 F(ClearStepping, 0, 1) \
304 F(DebugEvaluate, 4, 1) \
305 F(DebugEvaluateGlobal, 3, 1) \
306 F(DebugGetLoadedScripts, 0, 1) \
307 F(DebugReferencedBy, 3, 1) \
308 F(DebugConstructedBy, 2, 1) \
309 F(DebugGetPrototype, 1, 1) \
310 F(SystemBreak, 0, 1) \
311 F(DebugDisassembleFunction, 1, 1) \
312 F(DebugDisassembleConstructor, 1, 1) \
313 F(FunctionGetInferredName, 1, 1)
314#else
315#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
316#endif
317
318#ifdef DEBUG
319#define RUNTIME_FUNCTION_LIST_DEBUG(F) \
320 /* Testing */ \
321 F(ListNatives, 0, 1)
322#else
323#define RUNTIME_FUNCTION_LIST_DEBUG(F)
324#endif
325
326
327// ----------------------------------------------------------------------------
328// RUNTIME_FUNCTION_LIST defines all runtime functions accessed
329// either directly by id (via the code generator), or indirectly
330// via a native call by name (from within JS code).
331
332#define RUNTIME_FUNCTION_LIST(F) \
333 RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
334 RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
335 RUNTIME_FUNCTION_LIST_DEBUG(F) \
336 RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
337
338// ----------------------------------------------------------------------------
339// Runtime provides access to all C++ runtime functions.
340
341class Runtime : public AllStatic {
342 public:
343 enum FunctionId {
344#define F(name, nargs, ressize) k##name,
345 RUNTIME_FUNCTION_LIST(F)
346 kNofFunctions
347#undef F
348 };
349
350 // Runtime function descriptor.
351 struct Function {
352 // The JS name of the function.
353 const char* name;
354
355 // The name of the stub that calls the runtime function.
356 const char* stub_name;
357
358 // The C++ (native) entry point.
359 byte* entry;
360
361 // The number of arguments expected; nargs < 0 if variable no. of
362 // arguments.
363 int nargs;
364 int stub_id;
365 // Size of result, if complex (larger than a single pointer),
366 // otherwise zero.
367 int result_size;
368 };
369
370 // Get the runtime function with the given function id.
371 static Function* FunctionForId(FunctionId fid);
372
373 // Get the runtime function with the given name.
374 static Function* FunctionForName(const char* name);
375
376 static int StringMatch(Handle<String> sub, Handle<String> pat, int index);
377
378 static bool IsUpperCaseChar(uint16_t ch);
379
380 // TODO(1240886): The following three methods are *not* handle safe,
381 // but accept handle arguments. This seems fragile.
382
383 // Support getting the characters in a string using [] notation as
384 // in Firefox/SpiderMonkey, Safari and Opera.
385 static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
386
387 static Object* SetObjectProperty(Handle<Object> object,
388 Handle<Object> key,
389 Handle<Object> value,
390 PropertyAttributes attr);
391
392 static Object* ForceSetObjectProperty(Handle<JSObject> object,
393 Handle<Object> key,
394 Handle<Object> value,
395 PropertyAttributes attr);
396
397 static Object* ForceDeleteObjectProperty(Handle<JSObject> object,
398 Handle<Object> key);
399
400 static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
401
402 // This function is used in FunctionNameUsing* tests.
403 static Object* FindSharedFunctionInfoInScript(Handle<Script> script,
404 int position);
405
406 // Helper functions used stubs.
407 static void PerformGC(Object* result);
408};
409
410
411} } // namespace v8::internal
412
413#endif // V8_RUNTIME_H_