blob: fcded39692661f42462b6e1c4ca1de84fc881cbb [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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 { namespace internal {
32
33// The interface to C++ runtime functions.
34
35// ----------------------------------------------------------------------------
kasper.lund44510672008-07-25 07:37:58 +000036// RUNTIME_FUNCTION_LIST_ALWAYS defines runtime calls available in both
37// release and debug mode.
38// This macro should only be used by the macro RUNTIME_FUNCTION_LIST.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasper.lund44510672008-07-25 07:37:58 +000040#define RUNTIME_FUNCTION_LIST_ALWAYS(F) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041 /* Property access */ \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 F(GetProperty, 2) \
43 F(DeleteProperty, 2) \
44 F(HasLocalProperty, 2) \
45 F(HasProperty, 2) \
46 F(HasElement, 2) \
47 F(IsPropertyEnumerable, 2) \
48 F(GetPropertyNames, 1) \
49 F(GetPropertyNamesFast, 1) \
50 F(GetArgumentsProperty, 1) \
51 \
52 F(IsInPrototypeChain, 2) \
53 \
mads.s.ager31e71382008-08-13 09:32:07 +000054 F(IsConstructCall, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 \
56 /* Utilities */ \
mads.s.ager31e71382008-08-13 09:32:07 +000057 F(GetCalledFunction, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 F(GetFunctionDelegate, 1) \
59 F(NewArguments, 1) \
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000060 F(NewArgumentsFast, 3) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061 F(LazyCompile, 1) \
62 F(SetNewFunctionAttributes, 1) \
63 \
ager@chromium.org9258b6b2008-09-11 09:11:10 +000064 /* Array join support */ \
65 F(PushIfAbsent, 2) \
66 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067 /* ConsStrings */ \
68 F(ConsStringFst, 1) \
69 F(ConsStringSnd, 1) \
70 \
71 /* Conversions */ \
72 F(ToBool, 1) \
73 F(Typeof, 1) \
74 \
75 F(StringToNumber, 1) \
76 F(StringFromCharCodeArray, 1) \
77 F(StringParseInt, 2) \
78 F(StringParseFloat, 1) \
79 F(StringToLowerCase, 1) \
80 F(StringToUpperCase, 1) \
81 F(CharFromCode, 1) \
82 F(URIEscape, 1) \
83 F(URIUnescape, 1) \
84 \
85 F(NumberToString, 1) \
86 F(NumberToInteger, 1) \
87 F(NumberToJSUint32, 1) \
88 F(NumberToJSInt32, 1) \
89 \
90 /* Arithmetic operations */ \
91 F(NumberAdd, 2) \
92 F(NumberSub, 2) \
93 F(NumberMul, 2) \
94 F(NumberDiv, 2) \
95 F(NumberMod, 2) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 F(NumberUnaryMinus, 1) \
97 \
98 F(StringAdd, 2) \
99 F(StringBuilderConcat, 2) \
100 \
101 /* Bit operations */ \
102 F(NumberOr, 2) \
103 F(NumberAnd, 2) \
104 F(NumberXor, 2) \
105 F(NumberNot, 1) \
106 \
107 F(NumberShl, 2) \
108 F(NumberShr, 2) \
109 F(NumberSar, 2) \
110 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111 /* Comparisons */ \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 F(NumberEquals, 2) \
113 F(StringEquals, 2) \
114 \
115 F(NumberCompare, 3) \
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000116 F(SmiLexicographicCompare, 2) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 F(StringCompare, 2) \
118 \
119 /* Math */ \
120 F(Math_abs, 1) \
121 F(Math_acos, 1) \
122 F(Math_asin, 1) \
123 F(Math_atan, 1) \
124 F(Math_atan2, 2) \
125 F(Math_ceil, 1) \
126 F(Math_cos, 1) \
127 F(Math_exp, 1) \
128 F(Math_floor, 1) \
129 F(Math_log, 1) \
130 F(Math_pow, 2) \
mads.s.ager31e71382008-08-13 09:32:07 +0000131 F(Math_random, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132 F(Math_round, 1) \
133 F(Math_sin, 1) \
134 F(Math_sqrt, 1) \
135 F(Math_tan, 1) \
136 \
137 /* Regular expressions */ \
138 F(RegExpCompile, 3) \
139 F(RegExpExec, 3) \
140 F(RegExpExecGlobal, 2) \
141 \
142 /* Strings */ \
143 F(StringCharCodeAt, 2) \
144 F(StringIndexOf, 3) \
145 F(StringLastIndexOf, 3) \
146 F(StringLocaleCompare, 2) \
147 F(StringSlice, 3) \
148 \
149 /* Numbers */ \
150 F(NumberToRadixString, 2) \
151 F(NumberToFixed, 2) \
152 F(NumberToExponential, 2) \
153 F(NumberToPrecision, 2) \
154 \
155 /* Reflection */ \
156 F(FunctionSetInstanceClassName, 2) \
157 F(FunctionSetLength, 2) \
158 F(FunctionSetPrototype, 2) \
159 F(FunctionGetName, 1) \
ager@chromium.org236ad962008-09-25 09:45:57 +0000160 F(FunctionSetName, 2) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 F(FunctionGetSourceCode, 1) \
162 F(FunctionGetScript, 1) \
163 F(FunctionGetScriptSourcePosition, 1) \
164 F(GetScript, 1) \
165 \
166 F(ClassOf, 1) \
167 F(SetCode, 2) \
168 \
169 F(CreateApiFunction, 1) \
170 F(IsTemplate, 1) \
171 F(GetTemplateField, 2) \
172 \
173 /* Dates */ \
mads.s.ager31e71382008-08-13 09:32:07 +0000174 F(DateCurrentTime, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 F(DateParseString, 1) \
176 F(DateLocalTimezone, 1) \
mads.s.ager31e71382008-08-13 09:32:07 +0000177 F(DateLocalTimeOffset, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 F(DateDaylightSavingsOffset, 1) \
179 \
180 /* Numbers */ \
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000181 F(NumberIsFinite, 1) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 \
183 /* Globals */ \
ager@chromium.org236ad962008-09-25 09:45:57 +0000184 F(CompileString, 3) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 F(CompileScript, 4) \
186 F(GlobalPrint, 1) \
187 \
188 /* Eval */ \
189 F(EvalReceiver, 1) \
190 \
191 F(SetProperty, -1 /* 3 or 4 */) \
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000192 F(IgnoreAttributesAndSetProperty, -1 /* 3 or 4 */) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 \
194 /* Arrays */ \
195 F(RemoveArrayHoles, 1) \
196 F(GetArrayKeys, 2) \
197 F(MoveArrayContents, 2) \
198 F(EstimateNumberOfElements, 1) \
199 \
200 /* Getters and Setters */ \
201 F(DefineAccessor, -1 /* 4 or 5 */) \
202 F(LookupAccessor, 3) \
203 \
204 /* Debugging */ \
205 F(AddDebugEventListener, 2) \
206 F(RemoveDebugEventListener, 1) \
mads.s.ager31e71382008-08-13 09:32:07 +0000207 F(Break, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 F(DebugGetLocalPropertyDetails, 2) \
209 F(DebugGetProperty, 2) \
210 F(DebugLocalPropertyNames, 1) \
211 F(DebugLocalElementNames, 1) \
212 F(DebugPropertyTypeFromDetails, 1) \
213 F(DebugPropertyAttributesFromDetails, 1) \
214 F(DebugPropertyIndexFromDetails, 1) \
215 F(DebugInterceptorInfo, 1) \
216 F(DebugNamedInterceptorPropertyNames, 1) \
217 F(DebugIndexedInterceptorElementNames, 1) \
218 F(DebugNamedInterceptorPropertyValue, 2) \
219 F(DebugIndexedInterceptorElementValue, 2) \
220 F(CheckExecutionState, 1) \
221 F(GetFrameCount, 1) \
222 F(GetFrameDetails, 2) \
223 F(GetCFrames, 1) \
224 F(GetBreakLocations, 1) \
225 F(SetFunctionBreakPoint, 3) \
226 F(SetScriptBreakPoint, 3) \
227 F(ClearBreakPoint, 1) \
228 F(ChangeBreakOnException, 2) \
229 F(PrepareStep, 3) \
mads.s.ager31e71382008-08-13 09:32:07 +0000230 F(ClearStepping, 0) \
kasper.lundbd3ec4e2008-07-09 11:06:54 +0000231 F(DebugEvaluate, 4) \
232 F(DebugEvaluateGlobal, 3) \
mads.s.ager31e71382008-08-13 09:32:07 +0000233 F(DebugGetLoadedScripts, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 F(DebugReferencedBy, 3) \
235 F(DebugConstructedBy, 2) \
236 F(GetPrototype, 1) \
mads.s.ager31e71382008-08-13 09:32:07 +0000237 F(SystemBreak, 0) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 \
239 /* Literals */ \
240 F(MaterializeRegExpLiteral, 4)\
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000241 F(CreateArrayLiteral, 2) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 F(CreateObjectLiteralBoilerplate, 3) \
243 F(CloneObjectLiteralBoilerplate, 1) \
244 \
245 /* Statements */ \
246 F(NewClosure, 2) \
247 F(NewObject, 1) \
248 F(Throw, 1) \
249 F(ReThrow, 1) \
250 F(ThrowReferenceError, 1) \
251 F(StackGuard, 1) \
252 \
253 /* Contexts */ \
kasper.lund7276f142008-07-30 08:49:36 +0000254 F(NewContext, 1) \
255 F(PushContext, 1) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 F(LookupContext, 2) \
257 F(LoadContextSlot, 2) \
258 F(LoadContextSlotNoReferenceError, 2) \
259 F(StoreContextSlot, 3) \
260 \
261 /* Declarations and initialization */ \
262 F(DeclareGlobals, 3) \
263 F(DeclareContextSlot, 5) \
264 F(InitializeVarGlobal, -1 /* 1 or 2 */) \
mads.s.ager31e71382008-08-13 09:32:07 +0000265 F(InitializeConstGlobal, 2) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 F(InitializeConstContextSlot, 3) \
267 \
268 /* Debugging */ \
269 F(DebugPrint, 1) \
270 F(DebugTrace, 1) \
271 F(TraceEnter, 1) \
272 F(TraceExit, 1) \
273 F(DebugBreak, 1) \
274 F(FunctionGetAssemblerCode, 1) \
275 F(Abort, 2) \
276 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 /* Pseudo functions - handled as macros by parser */ \
278 F(IS_VAR, 1)
279
280
kasper.lund44510672008-07-25 07:37:58 +0000281#ifdef DEBUG
282#define RUNTIME_FUNCTION_LIST_DEBUG(F) \
283 /* Testing */ \
mads.s.ager31e71382008-08-13 09:32:07 +0000284 F(ListNatives, 0)
kasper.lund44510672008-07-25 07:37:58 +0000285#else
286#define RUNTIME_FUNCTION_LIST_DEBUG(F)
287#endif
288
289
290// ----------------------------------------------------------------------------
291// RUNTIME_FUNCTION_LIST defines all runtime functions accessed
292// either directly by id (via the code generator), or indirectly
293// via a native call by name (from within JS code).
294
295#define RUNTIME_FUNCTION_LIST(F) \
296 RUNTIME_FUNCTION_LIST_ALWAYS(F) \
297 RUNTIME_FUNCTION_LIST_DEBUG(F)
298
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299// ----------------------------------------------------------------------------
300// Runtime provides access to all C++ runtime functions.
301
302class Runtime : public AllStatic {
303 public:
304 enum FunctionId {
305#define F(name, nargs) k##name,
306 RUNTIME_FUNCTION_LIST(F)
307 kNofFunctions
308#undef F
309 };
310 static Object* CreateArrayLiteral(Arguments args);
311
312 // Runtime function descriptor.
313 struct Function {
314 // The JS name of the function.
315 const char* name;
316
317 // The name of the stub that calls the runtime function.
318 const char* stub_name;
319
320 // The C++ (native) entry point.
321 byte* entry;
322
323 // The number of arguments expected; nargs < 0 if variable no. of
324 // arguments.
325 int nargs;
326 int stub_id;
327 };
328
329 // Get the runtime function with the given function id.
330 static Function* FunctionForId(FunctionId fid);
331
332 // Get the runtime function with the given name.
333 static Function* FunctionForName(const char* name);
334
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000335 static int StringMatchKmp(String* sub, String* pat, int index);
336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 // TODO(1240886): The following three methods are *not* handle safe,
338 // but accept handle arguments. This seems fragile.
339
340 // Support getting the characters in a string using [] notation as
341 // in Firefox/SpiderMonkey, Safari and Opera.
342 static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
343
344 static Object* SetObjectProperty(Handle<Object> object,
345 Handle<Object> key,
346 Handle<Object> value,
347 PropertyAttributes attr);
348
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000349 static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350
351 // Helper functions used stubs.
352 static void PerformGC(Object* result);
353};
354
355
356} } // namespace v8::internal
357
358#endif // V8_RUNTIME_H_