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

5 6
#ifndef V8_BUILTINS_ACCESSORS_H_
#define V8_BUILTINS_ACCESSORS_H_
7

8
#include "include/v8.h"
9
#include "src/base/bit-field.h"
10
#include "src/common/globals.h"
11
#include "src/objects/property-details.h"
12
#include "src/utils/allocation.h"
13

14 15
namespace v8 {
namespace internal {
16

17
// Forward declarations.
18
class AccessorInfo;
marja's avatar
marja committed
19 20
template <typename T>
class Handle;
21
class FieldIndex;
22
class JavaScriptFrame;
23

24 25
// The list of accessor descriptors. This is a second-order macro
// taking a macro to be applied to all accessor descriptor names.
26
// V(accessor_name, AccessorName, GetterSideEffectType, SetterSideEffectType)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#define ACCESSOR_INFO_LIST_GENERATOR(V, _)                                    \
  V(_, arguments_iterator, ArgumentsIterator, kHasNoSideEffect,               \
    kHasSideEffectToReceiver)                                                 \
  V(_, array_length, ArrayLength, kHasNoSideEffect, kHasSideEffectToReceiver) \
  V(_, bound_function_length, BoundFunctionLength, kHasNoSideEffect,          \
    kHasSideEffectToReceiver)                                                 \
  V(_, bound_function_name, BoundFunctionName, kHasNoSideEffect,              \
    kHasSideEffectToReceiver)                                                 \
  V(_, error_stack, ErrorStack, kHasSideEffectToReceiver,                     \
    kHasSideEffectToReceiver)                                                 \
  V(_, function_arguments, FunctionArguments, kHasNoSideEffect,               \
    kHasSideEffectToReceiver)                                                 \
  V(_, function_caller, FunctionCaller, kHasNoSideEffect,                     \
    kHasSideEffectToReceiver)                                                 \
  V(_, function_name, FunctionName, kHasNoSideEffect,                         \
    kHasSideEffectToReceiver)                                                 \
  V(_, function_length, FunctionLength, kHasNoSideEffect,                     \
    kHasSideEffectToReceiver)                                                 \
  V(_, function_prototype, FunctionPrototype, kHasNoSideEffect,               \
    kHasSideEffectToReceiver)                                                 \
47 48
  V(_, regexp_result_indices, RegExpResultIndices, kHasSideEffectToReceiver,  \
    kHasSideEffectToReceiver)                                                 \
49
  V(_, string_length, StringLength, kHasNoSideEffect, kHasSideEffectToReceiver)
50

jgruber's avatar
jgruber committed
51 52 53
#define ACCESSOR_SETTER_LIST(V) \
  V(ArrayLengthSetter)          \
  V(ErrorStackSetter)           \
54 55 56
  V(FunctionPrototypeSetter)    \
  V(ModuleNamespaceEntrySetter) \
  V(ReconfigureToDataProperty)
57

58
// Accessors contains all predefined proxy accessors.
59 60 61

class Accessors : public AllStatic {
 public:
62 63 64
#define ACCESSOR_GETTER_DECLARATION(_, accessor_name, AccessorName, ...) \
  static void AccessorName##Getter(                                      \
      v8::Local<v8::Name> name,                                          \
65
      const v8::PropertyCallbackInfo<v8::Value>& info);
66
  ACCESSOR_INFO_LIST_GENERATOR(ACCESSOR_GETTER_DECLARATION, /* not used */)
67
#undef ACCESSOR_GETTER_DECLARATION
68

69 70 71 72
#define ACCESSOR_SETTER_DECLARATION(accessor_name)          \
  static void accessor_name(                                \
      v8::Local<v8::Name> name, v8::Local<v8::Value> value, \
      const v8::PropertyCallbackInfo<v8::Boolean>& info);
73 74 75
  ACCESSOR_SETTER_LIST(ACCESSOR_SETTER_DECLARATION)
#undef ACCESSOR_SETTER_DECLARATION

76 77
  static constexpr int kAccessorInfoCount =
#define COUNT_ACCESSOR(...) +1
78
      ACCESSOR_INFO_LIST_GENERATOR(COUNT_ACCESSOR, /* not used */);
79 80 81 82 83 84 85
#undef COUNT_ACCESSOR

  static constexpr int kAccessorSetterCount =
#define COUNT_ACCESSOR(...) +1
      ACCESSOR_SETTER_LIST(COUNT_ACCESSOR);
#undef COUNT_ACCESSOR

86 87 88
  static void ModuleNamespaceEntryGetter(
      v8::Local<v8::Name> name,
      const v8::PropertyCallbackInfo<v8::Value>& info);
89 90
  static Handle<AccessorInfo> MakeModuleNamespaceEntryInfo(Isolate* isolate,
                                                           Handle<String> name);
91

92 93 94 95 96
  // Accessor function called directly from the runtime system. Returns the
  // newly materialized arguments object for the given {frame}. Note that for
  // optimized frames it is possible to specify an {inlined_jsframe_index}.
  static Handle<JSObject> FunctionGetArguments(JavaScriptFrame* frame,
                                               int inlined_jsframe_index);
97

98
  // Returns true for properties that are accessors to object fields.
99
  // If true, the matching FieldIndex is returned through |field_index|.
100 101
  static bool IsJSObjectFieldAccessor(Isolate* isolate, Handle<Map> map,
                                      Handle<Name> name,
102
                                      FieldIndex* field_index);
103

104
  static MaybeHandle<Object> ReplaceAccessorWithDataProperty(
105 106
      Handle<Object> receiver, Handle<JSObject> holder, Handle<Name> name,
      Handle<Object> value);
107

108 109 110 111 112 113 114 115
  // Create an AccessorInfo. The setter is optional (can be nullptr).
  //
  // Note that the type of setter is AccessorNameBooleanSetterCallback instead
  // of v8::AccessorNameSetterCallback.  The difference is that the former can
  // set a (boolean) return value. The setter should roughly follow the same
  // conventions as many of the internal methods in objects.cc:
  // - The return value is unset iff there was an exception.
  // - If the ShouldThrow argument is true, the return value must not be false.
116 117 118
  using AccessorNameBooleanSetterCallback =
      void (*)(Local<v8::Name> property, Local<v8::Value> value,
               const PropertyCallbackInfo<v8::Boolean>& info);
119

120
  V8_EXPORT_PRIVATE static Handle<AccessorInfo> MakeAccessor(
121
      Isolate* isolate, Handle<Name> name, AccessorNameGetterCallback getter,
122
      AccessorNameBooleanSetterCallback setter);
123 124

 private:
125
#define ACCESSOR_INFO_DECLARATION(_, accessor_name, AccessorName, ...) \
126
  static Handle<AccessorInfo> Make##AccessorName##Info(Isolate* isolate);
127
  ACCESSOR_INFO_LIST_GENERATOR(ACCESSOR_INFO_DECLARATION, /* not used */)
128 129 130
#undef ACCESSOR_INFO_DECLARATION

  friend class Heap;
131 132
};

133 134
}  // namespace internal
}  // namespace v8
135

136
#endif  // V8_BUILTINS_ACCESSORS_H_