blob: afa278b1352b38d1e3f25d0ab03b68b639c7701f [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) \
155 \
156 /* Numbers */ \
157 F(NumberToRadixString, 2, 1) \
158 F(NumberToFixed, 2, 1) \
159 F(NumberToExponential, 2, 1) \
160 F(NumberToPrecision, 2, 1)
161
162#define RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
163 /* Reflection */ \
164 F(FunctionSetInstanceClassName, 2, 1) \
165 F(FunctionSetLength, 2, 1) \
166 F(FunctionSetPrototype, 2, 1) \
167 F(FunctionGetName, 1, 1) \
168 F(FunctionSetName, 2, 1) \
169 F(FunctionGetSourceCode, 1, 1) \
170 F(FunctionGetScript, 1, 1) \
171 F(FunctionGetScriptSourcePosition, 1, 1) \
172 F(FunctionGetPositionForOffset, 2, 1) \
173 F(FunctionIsAPIFunction, 1, 1) \
174 F(FunctionIsBuiltin, 1, 1) \
175 F(GetScript, 1, 1) \
176 F(CollectStackTrace, 2, 1) \
177 \
178 F(ClassOf, 1, 1) \
179 F(SetCode, 2, 1) \
180 \
181 F(CreateApiFunction, 1, 1) \
182 F(IsTemplate, 1, 1) \
183 F(GetTemplateField, 2, 1) \
184 F(DisableAccessChecks, 1, 1) \
185 F(EnableAccessChecks, 1, 1) \
186 \
187 /* Dates */ \
188 F(DateCurrentTime, 0, 1) \
189 F(DateParseString, 2, 1) \
190 F(DateLocalTimezone, 1, 1) \
191 F(DateLocalTimeOffset, 0, 1) \
192 F(DateDaylightSavingsOffset, 1, 1) \
193 \
194 /* Numbers */ \
195 F(NumberIsFinite, 1, 1) \
196 \
197 /* Globals */ \
198 F(CompileString, 2, 1) \
199 F(GlobalPrint, 1, 1) \
200 \
201 /* Eval */ \
202 F(GlobalReceiver, 1, 1) \
203 F(ResolvePossiblyDirectEval, 2, 1) \
204 \
205 F(SetProperty, -1 /* 3 or 4 */, 1) \
206 F(IgnoreAttributesAndSetProperty, -1 /* 3 or 4 */, 1) \
207 \
208 /* Arrays */ \
209 F(RemoveArrayHoles, 2, 1) \
210 F(GetArrayKeys, 2, 1) \
211 F(MoveArrayContents, 2, 1) \
212 F(EstimateNumberOfElements, 1, 1) \
213 \
214 /* Getters and Setters */ \
215 F(DefineAccessor, -1 /* 4 or 5 */, 1) \
216 F(LookupAccessor, 3, 1) \
217 \
218 /* Literals */ \
219 F(MaterializeRegExpLiteral, 4, 1)\
220 F(CreateArrayLiteralBoilerplate, 3, 1) \
221 F(CreateObjectLiteralBoilerplate, 3, 1) \
222 F(CloneLiteralBoilerplate, 1, 1) \
223 F(CloneShallowLiteralBoilerplate, 1, 1) \
224 \
225 /* Catch context extension objects */ \
226 F(CreateCatchExtensionObject, 2, 1) \
227 \
228 /* Statements */ \
229 F(NewClosure, 2, 1) \
230 F(NewObject, 1, 1) \
231 F(Throw, 1, 1) \
232 F(ReThrow, 1, 1) \
233 F(ThrowReferenceError, 1, 1) \
234 F(StackGuard, 1, 1) \
235 \
236 /* Contexts */ \
237 F(NewContext, 1, 1) \
238 F(PushContext, 1, 1) \
239 F(PushCatchContext, 1, 1) \
240 F(LookupContext, 2, 1) \
241 F(LoadContextSlot, 2, 2) \
242 F(LoadContextSlotNoReferenceError, 2, 2) \
243 F(StoreContextSlot, 3, 1) \
244 \
245 /* Declarations and initialization */ \
246 F(DeclareGlobals, 3, 1) \
247 F(DeclareContextSlot, 4, 1) \
248 F(InitializeVarGlobal, -1 /* 1 or 2 */, 1) \
249 F(InitializeConstGlobal, 2, 1) \
250 F(InitializeConstContextSlot, 3, 1) \
251 F(OptimizeObjectForAddingMultipleProperties, 2, 1) \
252 F(TransformToFastProperties, 1, 1) \
253 \
254 /* Debugging */ \
255 F(DebugPrint, 1, 1) \
256 F(DebugTrace, 0, 1) \
257 F(TraceEnter, 0, 1) \
258 F(TraceExit, 1, 1) \
259 F(Abort, 2, 1) \
260 /* Logging */ \
261 F(Log, 2, 1) \
262 /* ES5 */ \
263 F(LocalKeys, 1, 1) \
264 \
265 /* Pseudo functions - handled as macros by parser */ \
266 F(IS_VAR, 1, 1)
267
268#ifdef ENABLE_DEBUGGER_SUPPORT
269#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
270 /* Debugger support*/ \
271 F(DebugBreak, 0, 1) \
272 F(SetDebugEventListener, 2, 1) \
273 F(Break, 0, 1) \
274 F(DebugGetPropertyDetails, 2, 1) \
275 F(DebugGetProperty, 2, 1) \
276 F(DebugLocalPropertyNames, 1, 1) \
277 F(DebugLocalElementNames, 1, 1) \
278 F(DebugPropertyTypeFromDetails, 1, 1) \
279 F(DebugPropertyAttributesFromDetails, 1, 1) \
280 F(DebugPropertyIndexFromDetails, 1, 1) \
281 F(DebugInterceptorInfo, 1, 1) \
282 F(DebugNamedInterceptorPropertyNames, 1, 1) \
283 F(DebugIndexedInterceptorElementNames, 1, 1) \
284 F(DebugNamedInterceptorPropertyValue, 2, 1) \
285 F(DebugIndexedInterceptorElementValue, 2, 1) \
286 F(CheckExecutionState, 1, 1) \
287 F(GetFrameCount, 1, 1) \
288 F(GetFrameDetails, 2, 1) \
289 F(GetScopeCount, 2, 1) \
290 F(GetScopeDetails, 3, 1) \
291 F(DebugPrintScopes, 0, 1) \
292 F(GetCFrames, 1, 1) \
293 F(GetThreadCount, 1, 1) \
294 F(GetThreadDetails, 2, 1) \
295 F(GetBreakLocations, 1, 1) \
296 F(SetFunctionBreakPoint, 3, 1) \
297 F(SetScriptBreakPoint, 3, 1) \
298 F(ClearBreakPoint, 1, 1) \
299 F(ChangeBreakOnException, 2, 1) \
300 F(PrepareStep, 3, 1) \
301 F(ClearStepping, 0, 1) \
302 F(DebugEvaluate, 4, 1) \
303 F(DebugEvaluateGlobal, 3, 1) \
304 F(DebugGetLoadedScripts, 0, 1) \
305 F(DebugReferencedBy, 3, 1) \
306 F(DebugConstructedBy, 2, 1) \
307 F(DebugGetPrototype, 1, 1) \
308 F(SystemBreak, 0, 1) \
309 F(DebugDisassembleFunction, 1, 1) \
310 F(DebugDisassembleConstructor, 1, 1) \
311 F(FunctionGetInferredName, 1, 1)
312#else
313#define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
314#endif
315
316#ifdef DEBUG
317#define RUNTIME_FUNCTION_LIST_DEBUG(F) \
318 /* Testing */ \
319 F(ListNatives, 0, 1)
320#else
321#define RUNTIME_FUNCTION_LIST_DEBUG(F)
322#endif
323
324
325// ----------------------------------------------------------------------------
326// RUNTIME_FUNCTION_LIST defines all runtime functions accessed
327// either directly by id (via the code generator), or indirectly
328// via a native call by name (from within JS code).
329
330#define RUNTIME_FUNCTION_LIST(F) \
331 RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
332 RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
333 RUNTIME_FUNCTION_LIST_DEBUG(F) \
334 RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
335
336// ----------------------------------------------------------------------------
337// Runtime provides access to all C++ runtime functions.
338
339class Runtime : public AllStatic {
340 public:
341 enum FunctionId {
342#define F(name, nargs, ressize) k##name,
343 RUNTIME_FUNCTION_LIST(F)
344 kNofFunctions
345#undef F
346 };
347
348 // Runtime function descriptor.
349 struct Function {
350 // The JS name of the function.
351 const char* name;
352
353 // The name of the stub that calls the runtime function.
354 const char* stub_name;
355
356 // The C++ (native) entry point.
357 byte* entry;
358
359 // The number of arguments expected; nargs < 0 if variable no. of
360 // arguments.
361 int nargs;
362 int stub_id;
363 // Size of result, if complex (larger than a single pointer),
364 // otherwise zero.
365 int result_size;
366 };
367
368 // Get the runtime function with the given function id.
369 static Function* FunctionForId(FunctionId fid);
370
371 // Get the runtime function with the given name.
372 static Function* FunctionForName(const char* name);
373
374 static int StringMatch(Handle<String> sub, Handle<String> pat, int index);
375
376 static bool IsUpperCaseChar(uint16_t ch);
377
378 // TODO(1240886): The following three methods are *not* handle safe,
379 // but accept handle arguments. This seems fragile.
380
381 // Support getting the characters in a string using [] notation as
382 // in Firefox/SpiderMonkey, Safari and Opera.
383 static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
384
385 static Object* SetObjectProperty(Handle<Object> object,
386 Handle<Object> key,
387 Handle<Object> value,
388 PropertyAttributes attr);
389
390 static Object* ForceSetObjectProperty(Handle<JSObject> object,
391 Handle<Object> key,
392 Handle<Object> value,
393 PropertyAttributes attr);
394
395 static Object* ForceDeleteObjectProperty(Handle<JSObject> object,
396 Handle<Object> key);
397
398 static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
399
400 // This function is used in FunctionNameUsing* tests.
401 static Object* FindSharedFunctionInfoInScript(Handle<Script> script,
402 int position);
403
404 // Helper functions used stubs.
405 static void PerformGC(Object* result);
406};
407
408
409} } // namespace v8::internal
410
411#endif // V8_RUNTIME_H_