api-arguments.h 7.58 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_API_API_ARGUMENTS_H_
#define V8_API_API_ARGUMENTS_H_
7

8
#include "src/api/api.h"
9
#include "src/debug/debug.h"
10
#include "src/execution/isolate.h"
11
#include "src/objects/slots.h"
12
#include "src/objects/visitors.h"
13 14 15 16 17 18 19 20 21

namespace v8 {
namespace internal {

// Custom arguments replicate a small segment of stack that can be
// accessed through an Arguments object the same way the actual stack
// can.
class CustomArgumentsBase : public Relocatable {
 protected:
22
  explicit inline CustomArgumentsBase(Isolate* isolate);
23 24 25
};

template <typename T>
26
class CustomArguments : public CustomArgumentsBase {
27 28 29
 public:
  static const int kReturnValueOffset = T::kReturnValueIndex;

30
  ~CustomArguments() override;
31

32
  inline void IterateInstance(RootVisitor* v) override {
33 34
    v->VisitRootPointers(Root::kRelocatable, nullptr, slot_at(0),
                         slot_at(T::kArgsLength));
35 36
  }

37
 protected:
38 39
  explicit inline CustomArguments(Isolate* isolate)
      : CustomArgumentsBase(isolate) {}
40 41

  template <typename V>
42
  Handle<V> GetReturnValue(Isolate* isolate);
43 44

  inline Isolate* isolate() {
45
    return reinterpret_cast<Isolate*>((*slot_at(T::kIsolateIndex)).ptr());
46
  }
47

48
  inline FullObjectSlot slot_at(int index) {
49 50
    // This allows index == T::kArgsLength so "one past the end" slots
    // can be retrieved for iterating purposes.
51 52 53
    DCHECK_LE(static_cast<unsigned>(index),
              static_cast<unsigned>(T::kArgsLength));
    return FullObjectSlot(values_ + index);
54 55
  }
  Address values_[T::kArgsLength];
56 57
};

58 59
// Note: Calling args.Call() sets the return value on args. For multiple
// Call()'s, a new args should be used every time.
60 61 62
class PropertyCallbackArguments
    : public CustomArguments<PropertyCallbackInfo<Value> > {
 public:
63 64
  using T = PropertyCallbackInfo<Value>;
  using Super = CustomArguments<T>;
65 66 67 68 69 70 71 72 73
  static const int kArgsLength = T::kArgsLength;
  static const int kThisIndex = T::kThisIndex;
  static const int kHolderIndex = T::kHolderIndex;
  static const int kDataIndex = T::kDataIndex;
  static const int kReturnValueDefaultValueIndex =
      T::kReturnValueDefaultValueIndex;
  static const int kIsolateIndex = T::kIsolateIndex;
  static const int kShouldThrowOnErrorIndex = T::kShouldThrowOnErrorIndex;

74
  PropertyCallbackArguments(Isolate* isolate, Object data, Object self,
75
                            JSObject holder, Maybe<ShouldThrow> should_throw);
76

77 78 79
  // -------------------------------------------------------------------------
  // Accessor Callbacks
  // Also used for AccessorSetterCallback.
80 81 82
  inline Handle<Object> CallAccessorSetter(Handle<AccessorInfo> info,
                                           Handle<Name> name,
                                           Handle<Object> value);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  // Also used for AccessorGetterCallback, AccessorNameGetterCallback.
  inline Handle<Object> CallAccessorGetter(Handle<AccessorInfo> info,
                                           Handle<Name> name);

  // -------------------------------------------------------------------------
  // Named Interceptor Callbacks
  inline Handle<Object> CallNamedQuery(Handle<InterceptorInfo> interceptor,
                                       Handle<Name> name);
  inline Handle<Object> CallNamedGetter(Handle<InterceptorInfo> interceptor,
                                        Handle<Name> name);
  inline Handle<Object> CallNamedSetter(Handle<InterceptorInfo> interceptor,
                                        Handle<Name> name,
                                        Handle<Object> value);
  inline Handle<Object> CallNamedDefiner(Handle<InterceptorInfo> interceptor,
                                         Handle<Name> name,
                                         const v8::PropertyDescriptor& desc);
  inline Handle<Object> CallNamedDeleter(Handle<InterceptorInfo> interceptor,
                                         Handle<Name> name);
  inline Handle<Object> CallNamedDescriptor(Handle<InterceptorInfo> interceptor,
                                            Handle<Name> name);
103 104
  inline Handle<JSObject> CallNamedEnumerator(
      Handle<InterceptorInfo> interceptor);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  // -------------------------------------------------------------------------
  // Indexed Interceptor Callbacks
  inline Handle<Object> CallIndexedQuery(Handle<InterceptorInfo> interceptor,
                                         uint32_t index);
  inline Handle<Object> CallIndexedGetter(Handle<InterceptorInfo> interceptor,
                                          uint32_t index);
  inline Handle<Object> CallIndexedSetter(Handle<InterceptorInfo> interceptor,
                                          uint32_t index, Handle<Object> value);
  inline Handle<Object> CallIndexedDefiner(Handle<InterceptorInfo> interceptor,
                                           uint32_t index,
                                           const v8::PropertyDescriptor& desc);
  inline Handle<Object> CallIndexedDeleter(Handle<InterceptorInfo> interceptor,
                                           uint32_t index);
  inline Handle<Object> CallIndexedDescriptor(
      Handle<InterceptorInfo> interceptor, uint32_t index);
121 122
  inline Handle<JSObject> CallIndexedEnumerator(
      Handle<InterceptorInfo> interceptor);
123 124

 private:
125 126 127 128 129 130 131 132
  /*
   * The following Call functions wrap the calling of all callbacks to handle
   * calling either the old or the new style callbacks depending on which one
   * has been registered.
   * For old callbacks which return an empty handle, the ReturnValue is checked
   * and used if it's been set to anything inside the callback.
   * New style callbacks always use the return value.
   */
133 134
  inline Handle<JSObject> CallPropertyEnumerator(
      Handle<InterceptorInfo> interceptor);
135

136
  inline Handle<Object> BasicCallIndexedGetterCallback(
137
      IndexedPropertyGetterCallback f, uint32_t index, Handle<Object> info);
138
  inline Handle<Object> BasicCallNamedGetterCallback(
139
      GenericNamedPropertyGetterCallback f, Handle<Name> name,
140
      Handle<Object> info, Handle<Object> receiver = Handle<Object>());
141

142
  inline JSObject holder();
143
  inline Object receiver();
144

145 146 147
  // Don't copy PropertyCallbackArguments, because they would both have the
  // same prev_ pointer.
  DISALLOW_COPY_AND_ASSIGN(PropertyCallbackArguments);
148 149 150 151 152
};

class FunctionCallbackArguments
    : public CustomArguments<FunctionCallbackInfo<Value> > {
 public:
153 154
  using T = FunctionCallbackInfo<Value>;
  using Super = CustomArguments<T>;
155 156 157 158 159 160
  static const int kArgsLength = T::kArgsLength;
  static const int kHolderIndex = T::kHolderIndex;
  static const int kDataIndex = T::kDataIndex;
  static const int kReturnValueDefaultValueIndex =
      T::kReturnValueDefaultValueIndex;
  static const int kIsolateIndex = T::kIsolateIndex;
161
  static const int kNewTargetIndex = T::kNewTargetIndex;
162

163 164 165
  FunctionCallbackArguments(Isolate* isolate, Object data, HeapObject callee,
                            Object holder, HeapObject new_target, Address* argv,
                            int argc);
166 167 168 169 170 171 172 173 174

  /*
   * The following Call function wraps the calling of all callbacks to handle
   * calling either the old or the new style callbacks depending on which one
   * has been registered.
   * For old callbacks which return an empty handle, the ReturnValue is checked
   * and used if it's been set to anything inside the callback.
   * New style callbacks always use the return value.
   */
175
  inline Handle<Object> Call(CallHandlerInfo handler);
176 177

 private:
178
  inline JSObject holder();
179

180
  internal::Address* argv_;
181 182 183 184 185 186
  int argc_;
};

}  // namespace internal
}  // namespace v8

187
#endif  // V8_API_API_ARGUMENTS_H_