Home | History | Annotate | Download | only in src
      1 // 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 
     31 namespace v8 {
     32 namespace 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(GetLocalPropertyNames, 1, 1) \
     56   F(GetLocalElementNames, 1, 1) \
     57   F(GetInterceptorInfo, 1, 1) \
     58   F(GetNamedInterceptorPropertyNames, 1, 1) \
     59   F(GetIndexedInterceptorElementNames, 1, 1) \
     60   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   \
     69   F(GetOwnProperty, 2, 1) \
     70   \
     71   F(IsExtensible, 1, 1) \
     72   \
     73   /* Utilities */ \
     74   F(GetFunctionDelegate, 1, 1) \
     75   F(GetConstructorDelegate, 1, 1) \
     76   F(NewArgumentsFast, 3, 1) \
     77   F(LazyCompile, 1, 1) \
     78   F(SetNewFunctionAttributes, 1, 1) \
     79   \
     80   /* Array join support */ \
     81   F(PushIfAbsent, 2, 1) \
     82   F(ArrayConcat, 1, 1) \
     83   \
     84   /* Conversions */ \
     85   F(ToBool, 1, 1) \
     86   F(Typeof, 1, 1) \
     87   \
     88   F(StringToNumber, 1, 1) \
     89   F(StringFromCharCodeArray, 1, 1) \
     90   F(StringParseInt, 2, 1) \
     91   F(StringParseFloat, 1, 1) \
     92   F(StringToLowerCase, 1, 1) \
     93   F(StringToUpperCase, 1, 1) \
     94   F(CharFromCode, 1, 1) \
     95   F(URIEscape, 1, 1) \
     96   F(URIUnescape, 1, 1) \
     97   \
     98   F(NumberToString, 1, 1) \
     99   F(NumberToInteger, 1, 1) \
    100   F(NumberToJSUint32, 1, 1) \
    101   F(NumberToJSInt32, 1, 1) \
    102   F(NumberToSmi, 1, 1) \
    103   \
    104   /* Arithmetic operations */ \
    105   F(NumberAdd, 2, 1) \
    106   F(NumberSub, 2, 1) \
    107   F(NumberMul, 2, 1) \
    108   F(NumberDiv, 2, 1) \
    109   F(NumberMod, 2, 1) \
    110   F(NumberUnaryMinus, 1, 1) \
    111   \
    112   F(StringAdd, 2, 1) \
    113   F(StringBuilderConcat, 3, 1) \
    114   \
    115   /* Bit operations */ \
    116   F(NumberOr, 2, 1) \
    117   F(NumberAnd, 2, 1) \
    118   F(NumberXor, 2, 1) \
    119   F(NumberNot, 1, 1) \
    120   \
    121   F(NumberShl, 2, 1) \
    122   F(NumberShr, 2, 1) \
    123   F(NumberSar, 2, 1) \
    124   \
    125   /* Comparisons */ \
    126   F(NumberEquals, 2, 1) \
    127   F(StringEquals, 2, 1) \
    128   \
    129   F(NumberCompare, 3, 1) \
    130   F(SmiLexicographicCompare, 2, 1) \
    131   F(StringCompare, 2, 1) \
    132   \
    133   /* Math */ \
    134   F(Math_abs, 1, 1) \
    135   F(Math_acos, 1, 1) \
    136   F(Math_asin, 1, 1) \
    137   F(Math_atan, 1, 1) \
    138   F(Math_atan2, 2, 1) \
    139   F(Math_ceil, 1, 1) \
    140   F(Math_cos, 1, 1) \
    141   F(Math_exp, 1, 1) \
    142   F(Math_floor, 1, 1) \
    143   F(Math_log, 1, 1) \
    144   F(Math_pow, 2, 1) \
    145   F(Math_round, 1, 1) \
    146   F(Math_sin, 1, 1) \
    147   F(Math_sqrt, 1, 1) \
    148   F(Math_tan, 1, 1) \
    149   \
    150   /* Regular expressions */ \
    151   F(RegExpCompile, 3, 1) \
    152   F(RegExpExec, 4, 1) \
    153   \
    154   /* Strings */ \
    155   F(StringCharCodeAt, 2, 1) \
    156   F(StringCharAt, 2, 1) \
    157   F(StringIndexOf, 3, 1) \
    158   F(StringLastIndexOf, 3, 1) \
    159   F(StringLocaleCompare, 2, 1) \
    160   F(SubString, 3, 1) \
    161   F(StringReplaceRegExpWithString, 4, 1) \
    162   F(StringMatch, 3, 1) \
    163   F(StringTrim, 3, 1) \
    164   \
    165   /* Numbers */ \
    166   F(NumberToRadixString, 2, 1) \
    167   F(NumberToFixed, 2, 1) \
    168   F(NumberToExponential, 2, 1) \
    169   F(NumberToPrecision, 2, 1)
    170 
    171 #define RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
    172   /* Reflection */ \
    173   F(FunctionSetInstanceClassName, 2, 1) \
    174   F(FunctionSetLength, 2, 1) \
    175   F(FunctionSetPrototype, 2, 1) \
    176   F(FunctionGetName, 1, 1) \
    177   F(FunctionSetName, 2, 1) \
    178   F(FunctionGetSourceCode, 1, 1) \
    179   F(FunctionGetScript, 1, 1) \
    180   F(FunctionGetScriptSourcePosition, 1, 1) \
    181   F(FunctionGetPositionForOffset, 2, 1) \
    182   F(FunctionIsAPIFunction, 1, 1) \
    183   F(FunctionIsBuiltin, 1, 1) \
    184   F(GetScript, 1, 1) \
    185   F(CollectStackTrace, 2, 1) \
    186   F(GetV8Version, 0, 1) \
    187   \
    188   F(ClassOf, 1, 1) \
    189   F(SetCode, 2, 1) \
    190   \
    191   F(CreateApiFunction, 1, 1) \
    192   F(IsTemplate, 1, 1) \
    193   F(GetTemplateField, 2, 1) \
    194   F(DisableAccessChecks, 1, 1) \
    195   F(EnableAccessChecks, 1, 1) \
    196   \
    197   /* Dates */ \
    198   F(DateCurrentTime, 0, 1) \
    199   F(DateParseString, 2, 1) \
    200   F(DateLocalTimezone, 1, 1) \
    201   F(DateLocalTimeOffset, 0, 1) \
    202   F(DateDaylightSavingsOffset, 1, 1) \
    203   \
    204   /* Numbers */ \
    205   F(NumberIsFinite, 1, 1) \
    206   \
    207   /* Globals */ \
    208   F(CompileString, 2, 1) \
    209   F(GlobalPrint, 1, 1) \
    210   \
    211   /* Eval */ \
    212   F(GlobalReceiver, 1, 1) \
    213   F(ResolvePossiblyDirectEval, 3, 2) \
    214   \
    215   F(SetProperty, -1 /* 3 or 4 */, 1) \
    216   F(DefineOrRedefineDataProperty, 4, 1) \
    217   F(DefineOrRedefineAccessorProperty, 5, 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) \
    236   F(CreateObjectLiteral, 3, 1) \
    237   F(CreateObjectLiteralShallow, 3, 1) \
    238   F(CreateArrayLiteral, 3, 1) \
    239   F(CreateArrayLiteralShallow, 3, 1) \
    240   \
    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) \
    251   F(PromoteScheduledException, 0, 1) \
    252   \
    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   \
    270   /* Debugging */ \
    271   F(DebugPrint, 1, 1) \
    272   F(DebugTrace, 0, 1) \
    273   F(TraceEnter, 0, 1) \
    274   F(TraceExit, 1, 1) \
    275   F(Abort, 2, 1) \
    276   /* Logging */ \
    277   F(Log, 2, 1) \
    278   /* ES5 */ \
    279   F(LocalKeys, 1, 1) \
    280   /* Handle scopes */ \
    281   F(DeleteHandleScopeExtensions, 0, 1) \
    282   \
    283   /* Pseudo functions - handled as macros by parser */ \
    284   F(IS_VAR, 1, 1)
    285 
    286 #ifdef ENABLE_DEBUGGER_SUPPORT
    287 #define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
    288   /* Debugger support*/ \
    289   F(DebugBreak, 0, 1) \
    290   F(SetDebugEventListener, 2, 1) \
    291   F(Break, 0, 1) \
    292   F(DebugGetPropertyDetails, 2, 1) \
    293   F(DebugGetProperty, 2, 1) \
    294   F(DebugPropertyTypeFromDetails, 1, 1) \
    295   F(DebugPropertyAttributesFromDetails, 1, 1) \
    296   F(DebugPropertyIndexFromDetails, 1, 1) \
    297   F(DebugNamedInterceptorPropertyValue, 2, 1) \
    298   F(DebugIndexedInterceptorElementValue, 2, 1) \
    299   F(CheckExecutionState, 1, 1) \
    300   F(GetFrameCount, 1, 1) \
    301   F(GetFrameDetails, 2, 1) \
    302   F(GetScopeCount, 2, 1) \
    303   F(GetScopeDetails, 3, 1) \
    304   F(DebugPrintScopes, 0, 1) \
    305   F(GetCFrames, 1, 1) \
    306   F(GetThreadCount, 1, 1) \
    307   F(GetThreadDetails, 2, 1) \
    308   F(GetBreakLocations, 1, 1) \
    309   F(SetFunctionBreakPoint, 3, 1) \
    310   F(SetScriptBreakPoint, 3, 1) \
    311   F(ClearBreakPoint, 1, 1) \
    312   F(ChangeBreakOnException, 2, 1) \
    313   F(PrepareStep, 3, 1) \
    314   F(ClearStepping, 0, 1) \
    315   F(DebugEvaluate, 4, 1) \
    316   F(DebugEvaluateGlobal, 3, 1) \
    317   F(DebugGetLoadedScripts, 0, 1) \
    318   F(DebugReferencedBy, 3, 1) \
    319   F(DebugConstructedBy, 2, 1) \
    320   F(DebugGetPrototype, 1, 1) \
    321   F(SystemBreak, 0, 1) \
    322   F(DebugDisassembleFunction, 1, 1) \
    323   F(DebugDisassembleConstructor, 1, 1) \
    324   F(FunctionGetInferredName, 1, 1)
    325 #else
    326 #define RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F)
    327 #endif
    328 
    329 #ifdef ENABLE_LOGGING_AND_PROFILING
    330 #define RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F) \
    331   F(ProfilerResume, 2, 1) \
    332   F(ProfilerPause, 2, 1)
    333 #else
    334 #define RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F)
    335 #endif
    336 
    337 #ifdef DEBUG
    338 #define RUNTIME_FUNCTION_LIST_DEBUG(F) \
    339   /* Testing */ \
    340   F(ListNatives, 0, 1)
    341 #else
    342 #define RUNTIME_FUNCTION_LIST_DEBUG(F)
    343 #endif
    344 
    345 
    346 // ----------------------------------------------------------------------------
    347 // RUNTIME_FUNCTION_LIST defines all runtime functions accessed
    348 // either directly by id (via the code generator), or indirectly
    349 // via a native call by name (from within JS code).
    350 
    351 #define RUNTIME_FUNCTION_LIST(F) \
    352   RUNTIME_FUNCTION_LIST_ALWAYS_1(F) \
    353   RUNTIME_FUNCTION_LIST_ALWAYS_2(F) \
    354   RUNTIME_FUNCTION_LIST_DEBUG(F) \
    355   RUNTIME_FUNCTION_LIST_DEBUGGER_SUPPORT(F) \
    356   RUNTIME_FUNCTION_LIST_PROFILER_SUPPORT(F)
    357 
    358 // ----------------------------------------------------------------------------
    359 // Runtime provides access to all C++ runtime functions.
    360 
    361 class Runtime : public AllStatic {
    362  public:
    363   enum FunctionId {
    364 #define F(name, nargs, ressize) k##name,
    365     RUNTIME_FUNCTION_LIST(F)
    366     kNofFunctions
    367 #undef F
    368   };
    369 
    370   // Runtime function descriptor.
    371   struct Function {
    372     // The JS name of the function.
    373     const char* name;
    374 
    375     // The C++ (native) entry point.
    376     byte* entry;
    377 
    378     // The number of arguments expected; nargs < 0 if variable no. of
    379     // arguments.
    380     int nargs;
    381     int stub_id;
    382     // Size of result, if complex (larger than a single pointer),
    383     // otherwise zero.
    384     int result_size;
    385   };
    386 
    387   // Get the runtime function with the given function id.
    388   static Function* FunctionForId(FunctionId fid);
    389 
    390   // Get the runtime function with the given name.
    391   static Function* FunctionForName(const char* name);
    392 
    393   static int StringMatch(Handle<String> sub, Handle<String> pat, int index);
    394 
    395   static bool IsUpperCaseChar(uint16_t ch);
    396 
    397   // TODO(1240886): The following three methods are *not* handle safe,
    398   // but accept handle arguments. This seems fragile.
    399 
    400   // Support getting the characters in a string using [] notation as
    401   // in Firefox/SpiderMonkey, Safari and Opera.
    402   static Object* GetElementOrCharAt(Handle<Object> object, uint32_t index);
    403 
    404   static Object* SetObjectProperty(Handle<Object> object,
    405                                    Handle<Object> key,
    406                                    Handle<Object> value,
    407                                    PropertyAttributes attr);
    408 
    409   static Object* ForceSetObjectProperty(Handle<JSObject> object,
    410                                         Handle<Object> key,
    411                                         Handle<Object> value,
    412                                         PropertyAttributes attr);
    413 
    414   static Object* ForceDeleteObjectProperty(Handle<JSObject> object,
    415                                            Handle<Object> key);
    416 
    417   static Object* GetObjectProperty(Handle<Object> object, Handle<Object> key);
    418 
    419   // This function is used in FunctionNameUsing* tests.
    420   static Object* FindSharedFunctionInfoInScript(Handle<Script> script,
    421                                                 int position);
    422 
    423   // Helper functions used stubs.
    424   static void PerformGC(Object* result);
    425 };
    426 
    427 
    428 } }  // namespace v8::internal
    429 
    430 #endif  // V8_RUNTIME_H_
    431