Home | History | Annotate | Download | only in src
      1 // Copyright 2013 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 #include "v8.h"
     29 #include "arguments.h"
     30 
     31 #include "vm-state-inl.h"
     32 
     33 namespace v8 {
     34 namespace internal {
     35 
     36 
     37 template<typename T>
     38 template<typename V>
     39 v8::Handle<V> CustomArguments<T>::GetReturnValue(Isolate* isolate) {
     40   // Check the ReturnValue.
     41   Object** handle = &this->begin()[kReturnValueOffset];
     42   // Nothing was set, return empty handle as per previous behaviour.
     43   if ((*handle)->IsTheHole()) return v8::Handle<V>();
     44   return Utils::Convert<Object, V>(Handle<Object>(handle));
     45 }
     46 
     47 
     48 v8::Handle<v8::Value> FunctionCallbackArguments::Call(FunctionCallback f) {
     49   Isolate* isolate = this->isolate();
     50   VMState<EXTERNAL> state(isolate);
     51   ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f));
     52   FunctionCallbackInfo<v8::Value> info(begin(),
     53                                        argv_,
     54                                        argc_,
     55                                        is_construct_call_);
     56   f(info);
     57   return GetReturnValue<v8::Value>(isolate);
     58 }
     59 
     60 
     61 #define WRITE_CALL_0(Function, ReturnValue)                                    \
     62 v8::Handle<ReturnValue> PropertyCallbackArguments::Call(Function f) {          \
     63   Isolate* isolate = this->isolate();                                          \
     64   VMState<EXTERNAL> state(isolate);                                            \
     65   ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f));                 \
     66   PropertyCallbackInfo<ReturnValue> info(begin());                             \
     67   f(info);                                                                     \
     68   return GetReturnValue<ReturnValue>(isolate);                                 \
     69 }
     70 
     71 
     72 #define WRITE_CALL_1(Function, ReturnValue, Arg1)                              \
     73 v8::Handle<ReturnValue> PropertyCallbackArguments::Call(Function f,            \
     74                                                         Arg1 arg1) {           \
     75   Isolate* isolate = this->isolate();                                          \
     76   VMState<EXTERNAL> state(isolate);                                            \
     77   ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f));                 \
     78   PropertyCallbackInfo<ReturnValue> info(begin());                             \
     79   f(arg1, info);                                                               \
     80   return GetReturnValue<ReturnValue>(isolate);                                 \
     81 }
     82 
     83 
     84 #define WRITE_CALL_2(Function, ReturnValue, Arg1, Arg2)                        \
     85 v8::Handle<ReturnValue> PropertyCallbackArguments::Call(Function f,            \
     86                                                         Arg1 arg1,             \
     87                                                         Arg2 arg2) {           \
     88   Isolate* isolate = this->isolate();                                          \
     89   VMState<EXTERNAL> state(isolate);                                            \
     90   ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f));                 \
     91   PropertyCallbackInfo<ReturnValue> info(begin());                             \
     92   f(arg1, arg2, info);                                                         \
     93   return GetReturnValue<ReturnValue>(isolate);                                 \
     94 }
     95 
     96 
     97 #define WRITE_CALL_2_VOID(Function, ReturnValue, Arg1, Arg2)                   \
     98 void PropertyCallbackArguments::Call(Function f,                               \
     99                                      Arg1 arg1,                                \
    100                                      Arg2 arg2) {                              \
    101   Isolate* isolate = this->isolate();                                          \
    102   VMState<EXTERNAL> state(isolate);                                            \
    103   ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f));                 \
    104   PropertyCallbackInfo<ReturnValue> info(begin());                             \
    105   f(arg1, arg2, info);                                                         \
    106 }
    107 
    108 
    109 FOR_EACH_CALLBACK_TABLE_MAPPING_0(WRITE_CALL_0)
    110 FOR_EACH_CALLBACK_TABLE_MAPPING_1(WRITE_CALL_1)
    111 FOR_EACH_CALLBACK_TABLE_MAPPING_2(WRITE_CALL_2)
    112 FOR_EACH_CALLBACK_TABLE_MAPPING_2_VOID_RETURN(WRITE_CALL_2_VOID)
    113 
    114 #undef WRITE_CALL_0
    115 #undef WRITE_CALL_1
    116 #undef WRITE_CALL_2
    117 #undef WRITE_CALL_2_VOID
    118 
    119 
    120 double ClobberDoubleRegisters(double x1, double x2, double x3, double x4) {
    121   // TODO(ulan): This clobbers only subset of registers depending on compiler,
    122   // Rewrite this in assembly to really clobber all registers.
    123   // GCC for ia32 uses the FPU and does not touch XMM registers.
    124   return x1 * 1.01 + x2 * 2.02 + x3 * 3.03 + x4 * 4.04;
    125 }
    126 
    127 
    128 } }  // namespace v8::internal
    129