api-arguments-inl.h 14.7 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 7
#ifndef V8_API_ARGUMENTS_INL_H_
#define V8_API_ARGUMENTS_INL_H_

8 9
#include "src/api-arguments.h"

10
#include "src/api-inl.h"
11
#include "src/objects/api-callbacks.h"
12 13 14 15 16 17
#include "src/tracing/trace-event.h"
#include "src/vm-state-inl.h"

namespace v8 {
namespace internal {

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
CustomArgumentsBase::CustomArgumentsBase(Isolate* isolate)
    : Relocatable(isolate) {}

template <typename T>
template <typename V>
Handle<V> CustomArguments<T>::GetReturnValue(Isolate* isolate) {
  // Check the ReturnValue.
  Object** handle = &this->begin()[kReturnValueOffset];
  // Nothing was set, return empty handle as per previous behaviour.
  if ((*handle)->IsTheHole(isolate)) return Handle<V>();
  Handle<V> result = Handle<V>::cast(Handle<Object>(handle));
  result->VerifyApiCallResultType();
  return result;
}

inline JSObject* PropertyCallbackArguments::holder() {
  return JSObject::cast(this->begin()[T::kHolderIndex]);
}

inline JSObject* FunctionCallbackArguments::holder() {
  return JSObject::cast(this->begin()[T::kHolderIndex]);
}

41 42 43
#define FOR_EACH_CALLBACK(F)                        \
  F(Query, query, Object, v8::Integer, interceptor) \
  F(Deleter, deleter, Object, v8::Boolean, Handle<Object>())
44

45 46 47 48 49
#define DCHECK_NAME_COMPATIBLE(interceptor, name) \
  DCHECK(interceptor->is_named());                \
  DCHECK(!name->IsPrivate());                     \
  DCHECK_IMPLIES(name->IsSymbol(), interceptor->can_intercept_symbols());

50 51 52 53 54 55 56 57
#define PREPARE_CALLBACK_INFO(ISOLATE, F, RETURN_VALUE, API_RETURN_TYPE,     \
                              CALLBACK_INFO)                                 \
  if (ISOLATE->debug_execution_mode() == DebugInfo::kSideEffects &&          \
      !ISOLATE->debug()->PerformSideEffectCheckForCallback(CALLBACK_INFO)) { \
    return RETURN_VALUE();                                                   \
  }                                                                          \
  VMState<EXTERNAL> state(ISOLATE);                                          \
  ExternalCallbackScope call_scope(ISOLATE, FUNCTION_ADDR(F));               \
58 59
  PropertyCallbackInfo<API_RETURN_TYPE> callback_info(begin());

60 61 62
#define CREATE_NAMED_CALLBACK(FUNCTION, TYPE, RETURN_TYPE, API_RETURN_TYPE,   \
                              INFO_FOR_SIDE_EFFECT)                           \
  Handle<RETURN_TYPE> PropertyCallbackArguments::CallNamed##FUNCTION(         \
63
      Handle<InterceptorInfo> interceptor, Handle<Name> name) {               \
64
    DCHECK_NAME_COMPATIBLE(interceptor, name);                                \
65
    Isolate* isolate = this->isolate();                                       \
66
    RuntimeCallTimerScope timer(                                              \
67 68 69 70 71 72
        isolate, RuntimeCallCounterId::kNamed##FUNCTION##Callback);           \
    GenericNamedProperty##FUNCTION##Callback f =                              \
        ToCData<GenericNamedProperty##FUNCTION##Callback>(                    \
            interceptor->TYPE());                                             \
    PREPARE_CALLBACK_INFO(isolate, f, Handle<RETURN_TYPE>, API_RETURN_TYPE,   \
                          INFO_FOR_SIDE_EFFECT);                              \
73
    LOG(isolate,                                                              \
74
        ApiNamedPropertyAccess("interceptor-named-" #TYPE, holder(), *name)); \
75
    f(v8::Utils::ToLocal(name), callback_info);                               \
76
    return GetReturnValue<RETURN_TYPE>(isolate);                              \
77 78
  }

79 80 81
FOR_EACH_CALLBACK(CREATE_NAMED_CALLBACK)
#undef CREATE_NAMED_CALLBACK

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#define CREATE_INDEXED_CALLBACK(FUNCTION, TYPE, RETURN_TYPE, API_RETURN_TYPE, \
                                INFO_FOR_SIDE_EFFECT)                         \
  Handle<RETURN_TYPE> PropertyCallbackArguments::CallIndexed##FUNCTION(       \
      Handle<InterceptorInfo> interceptor, uint32_t index) {                  \
    DCHECK(!interceptor->is_named());                                         \
    Isolate* isolate = this->isolate();                                       \
    RuntimeCallTimerScope timer(                                              \
        isolate, RuntimeCallCounterId::kIndexed##FUNCTION##Callback);         \
    IndexedProperty##FUNCTION##Callback f =                                   \
        ToCData<IndexedProperty##FUNCTION##Callback>(interceptor->TYPE());    \
    PREPARE_CALLBACK_INFO(isolate, f, Handle<RETURN_TYPE>, API_RETURN_TYPE,   \
                          INFO_FOR_SIDE_EFFECT);                              \
    LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-" #TYPE,       \
                                          holder(), index));                  \
    f(index, callback_info);                                                  \
    return GetReturnValue<RETURN_TYPE>(isolate);                              \
98 99
  }

100
FOR_EACH_CALLBACK(CREATE_INDEXED_CALLBACK)
101

102 103 104
#undef FOR_EACH_CALLBACK
#undef CREATE_INDEXED_CALLBACK

105 106 107 108 109 110
Handle<Object> FunctionCallbackArguments::Call(CallHandlerInfo* handler) {
  Isolate* isolate = this->isolate();
  LOG(isolate, ApiObjectAccess("call", holder()));
  RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kFunctionCallback);
  v8::FunctionCallback f =
      v8::ToCData<v8::FunctionCallback>(handler->callback());
111
  if (isolate->debug_execution_mode() == DebugInfo::kSideEffects &&
112 113
      !isolate->debug()->PerformSideEffectCheckForCallback(
          handle(handler, isolate))) {
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    return Handle<Object>();
  }
  VMState<EXTERNAL> state(isolate);
  ExternalCallbackScope call_scope(isolate, FUNCTION_ADDR(f));
  FunctionCallbackInfo<v8::Value> info(begin(), argv_, argc_);
  f(info);
  return GetReturnValue<Object>(isolate);
}

Handle<JSObject> PropertyCallbackArguments::CallNamedEnumerator(
    Handle<InterceptorInfo> interceptor) {
  DCHECK(interceptor->is_named());
  LOG(isolate(), ApiObjectAccess("interceptor-named-enumerator", holder()));
  RuntimeCallTimerScope timer(isolate(),
                              RuntimeCallCounterId::kNamedEnumeratorCallback);
  return CallPropertyEnumerator(interceptor);
}

Handle<JSObject> PropertyCallbackArguments::CallIndexedEnumerator(
    Handle<InterceptorInfo> interceptor) {
  DCHECK(!interceptor->is_named());
  LOG(isolate(), ApiObjectAccess("interceptor-indexed-enumerator", holder()));
  RuntimeCallTimerScope timer(isolate(),
                              RuntimeCallCounterId::kIndexedEnumeratorCallback);
  return CallPropertyEnumerator(interceptor);
}

141 142
Handle<Object> PropertyCallbackArguments::CallNamedGetter(
    Handle<InterceptorInfo> interceptor, Handle<Name> name) {
143
  DCHECK_NAME_COMPATIBLE(interceptor, name);
144 145 146 147 148 149 150
  Isolate* isolate = this->isolate();
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kNamedGetterCallback);
  LOG(isolate,
      ApiNamedPropertyAccess("interceptor-named-getter", holder(), *name));
  GenericNamedPropertyGetterCallback f =
      ToCData<GenericNamedPropertyGetterCallback>(interceptor->getter());
151
  return BasicCallNamedGetterCallback(f, name, interceptor);
152
}
153

154 155
Handle<Object> PropertyCallbackArguments::CallNamedDescriptor(
    Handle<InterceptorInfo> interceptor, Handle<Name> name) {
156
  DCHECK_NAME_COMPATIBLE(interceptor, name);
157 158 159 160 161 162 163 164
  Isolate* isolate = this->isolate();
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kNamedDescriptorCallback);
  LOG(isolate,
      ApiNamedPropertyAccess("interceptor-named-descriptor", holder(), *name));
  GenericNamedPropertyDescriptorCallback f =
      ToCData<GenericNamedPropertyDescriptorCallback>(
          interceptor->descriptor());
165
  return BasicCallNamedGetterCallback(f, name, interceptor);
166 167 168
}

Handle<Object> PropertyCallbackArguments::BasicCallNamedGetterCallback(
169 170
    GenericNamedPropertyGetterCallback f, Handle<Name> name,
    Handle<Object> info) {
171 172
  DCHECK(!name->IsPrivate());
  Isolate* isolate = this->isolate();
173
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, v8::Value, info);
174 175 176 177 178 179 180
  f(v8::Utils::ToLocal(name), callback_info);
  return GetReturnValue<Object>(isolate);
}

Handle<Object> PropertyCallbackArguments::CallNamedSetter(
    Handle<InterceptorInfo> interceptor, Handle<Name> name,
    Handle<Object> value) {
181
  DCHECK_NAME_COMPATIBLE(interceptor, name);
182 183
  GenericNamedPropertySetterCallback f =
      ToCData<GenericNamedPropertySetterCallback>(interceptor->setter());
184
  Isolate* isolate = this->isolate();
185 186
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kNamedSetterCallback);
187 188 189
  Handle<Object> side_effect_check_not_supported;
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, v8::Value,
                        side_effect_check_not_supported);
190 191
  LOG(isolate,
      ApiNamedPropertyAccess("interceptor-named-set", holder(), *name));
192
  f(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), callback_info);
193 194 195
  return GetReturnValue<Object>(isolate);
}

196 197
Handle<Object> PropertyCallbackArguments::CallNamedDefiner(
    Handle<InterceptorInfo> interceptor, Handle<Name> name,
198
    const v8::PropertyDescriptor& desc) {
199
  DCHECK_NAME_COMPATIBLE(interceptor, name);
200
  Isolate* isolate = this->isolate();
201 202 203 204
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kNamedDefinerCallback);
  GenericNamedPropertyDefinerCallback f =
      ToCData<GenericNamedPropertyDefinerCallback>(interceptor->definer());
205 206 207
  Handle<Object> side_effect_check_not_supported;
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, v8::Value,
                        side_effect_check_not_supported);
208 209
  LOG(isolate,
      ApiNamedPropertyAccess("interceptor-named-define", holder(), *name));
210
  f(v8::Utils::ToLocal(name), desc, callback_info);
211 212 213
  return GetReturnValue<Object>(isolate);
}

214 215 216
Handle<Object> PropertyCallbackArguments::CallIndexedSetter(
    Handle<InterceptorInfo> interceptor, uint32_t index, Handle<Object> value) {
  DCHECK(!interceptor->is_named());
217
  Isolate* isolate = this->isolate();
218 219 220 221
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kIndexedSetterCallback);
  IndexedPropertySetterCallback f =
      ToCData<IndexedPropertySetterCallback>(interceptor->setter());
222 223 224
  Handle<Object> side_effect_check_not_supported;
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, v8::Value,
                        side_effect_check_not_supported);
225 226
  LOG(isolate,
      ApiIndexedPropertyAccess("interceptor-indexed-set", holder(), index));
227
  f(index, v8::Utils::ToLocal(value), callback_info);
228 229 230
  return GetReturnValue<Object>(isolate);
}

231 232
Handle<Object> PropertyCallbackArguments::CallIndexedDefiner(
    Handle<InterceptorInfo> interceptor, uint32_t index,
233
    const v8::PropertyDescriptor& desc) {
234
  DCHECK(!interceptor->is_named());
235
  Isolate* isolate = this->isolate();
236 237 238 239
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kIndexedDefinerCallback);
  IndexedPropertyDefinerCallback f =
      ToCData<IndexedPropertyDefinerCallback>(interceptor->definer());
240 241 242
  Handle<Object> side_effect_check_not_supported;
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, v8::Value,
                        side_effect_check_not_supported);
243 244
  LOG(isolate,
      ApiIndexedPropertyAccess("interceptor-indexed-define", holder(), index));
245
  f(index, desc, callback_info);
246 247 248
  return GetReturnValue<Object>(isolate);
}

249 250 251
Handle<Object> PropertyCallbackArguments::CallIndexedGetter(
    Handle<InterceptorInfo> interceptor, uint32_t index) {
  DCHECK(!interceptor->is_named());
252
  Isolate* isolate = this->isolate();
253 254
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kNamedGetterCallback);
255
  LOG(isolate,
256 257 258
      ApiIndexedPropertyAccess("interceptor-indexed-getter", holder(), index));
  IndexedPropertyGetterCallback f =
      ToCData<IndexedPropertyGetterCallback>(interceptor->getter());
259
  return BasicCallIndexedGetterCallback(f, index, interceptor);
260 261 262 263 264 265 266 267 268 269 270 271
}

Handle<Object> PropertyCallbackArguments::CallIndexedDescriptor(
    Handle<InterceptorInfo> interceptor, uint32_t index) {
  DCHECK(!interceptor->is_named());
  Isolate* isolate = this->isolate();
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kIndexedDescriptorCallback);
  LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-descriptor",
                                        holder(), index));
  IndexedPropertyDescriptorCallback f =
      ToCData<IndexedPropertyDescriptorCallback>(interceptor->descriptor());
272
  return BasicCallIndexedGetterCallback(f, index, interceptor);
273 274 275
}

Handle<Object> PropertyCallbackArguments::BasicCallIndexedGetterCallback(
276
    IndexedPropertyGetterCallback f, uint32_t index, Handle<Object> info) {
277
  Isolate* isolate = this->isolate();
278
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, v8::Value, info);
279 280 281 282 283 284 285 286 287 288 289
  f(index, callback_info);
  return GetReturnValue<Object>(isolate);
}

Handle<JSObject> PropertyCallbackArguments::CallPropertyEnumerator(
    Handle<InterceptorInfo> interceptor) {
  // For now there is a single enumerator for indexed and named properties.
  IndexedPropertyEnumeratorCallback f =
      v8::ToCData<IndexedPropertyEnumeratorCallback>(interceptor->enumerator());
  // TODO(cbruni): assert same type for indexed and named callback.
  Isolate* isolate = this->isolate();
290
  PREPARE_CALLBACK_INFO(isolate, f, Handle<JSObject>, v8::Array, interceptor);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  f(callback_info);
  return GetReturnValue<JSObject>(isolate);
}

// -------------------------------------------------------------------------
// Accessors

Handle<Object> PropertyCallbackArguments::CallAccessorGetter(
    Handle<AccessorInfo> info, Handle<Name> name) {
  Isolate* isolate = this->isolate();
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kAccessorGetterCallback);
  LOG(isolate, ApiNamedPropertyAccess("accessor-getter", holder(), *name));
  AccessorNameGetterCallback f =
      ToCData<AccessorNameGetterCallback>(info->getter());
306
  return BasicCallNamedGetterCallback(f, name, info);
307 308
}

309
Handle<Object> PropertyCallbackArguments::CallAccessorSetter(
310 311 312 313 314 315 316
    Handle<AccessorInfo> accessor_info, Handle<Name> name,
    Handle<Object> value) {
  Isolate* isolate = this->isolate();
  RuntimeCallTimerScope timer(isolate,
                              RuntimeCallCounterId::kAccessorSetterCallback);
  AccessorNameSetterCallback f =
      ToCData<AccessorNameSetterCallback>(accessor_info->setter());
317 318 319
  Handle<Object> side_effect_check_not_supported;
  PREPARE_CALLBACK_INFO(isolate, f, Handle<Object>, void,
                        side_effect_check_not_supported);
320 321
  LOG(isolate, ApiNamedPropertyAccess("accessor-setter", holder(), *name));
  f(v8::Utils::ToLocal(name), v8::Utils::ToLocal(value), callback_info);
322
  return GetReturnValue<Object>(isolate);
323 324
}

325
#undef PREPARE_CALLBACK_INFO
326

327 328
}  // namespace internal
}  // namespace v8
329 330

#endif  // V8_API_ARGUMENTS_INL_H_