summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/builtins-proxy-gen.cc
blob: 30b0f08ec07295f6d09187dd14e948e8fa25e8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 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.

#include "src/builtins/builtins-utils-gen.h"
#include "src/builtins/builtins-utils.h"
#include "src/builtins/builtins.h"
#include "src/code-stub-assembler.h"

#include "src/counters.h"
#include "src/objects-inl.h"

namespace v8 {
namespace internal {
using compiler::Node;
using compiler::CodeAssembler;

// ES6 section 26.2.1.1 Proxy ( target, handler ) for the [[Call]] case.
TF_BUILTIN(ProxyConstructor, CodeStubAssembler) {
  Node* context = Parameter(Descriptor::kContext);
  ThrowTypeError(context, MessageTemplate::kConstructorNotFunction, "Proxy");
}

class ProxiesCodeStubAssembler : public CodeStubAssembler {
 public:
  explicit ProxiesCodeStubAssembler(compiler::CodeAssemblerState* state)
      : CodeStubAssembler(state) {}

  Node* IsProxyRevoked(Node* proxy) {
    CSA_ASSERT(this, IsJSProxy(proxy));

    Node* handler = LoadObjectField(proxy, JSProxy::kHandlerOffset);
    CSA_ASSERT(this, Word32Or(IsJSReceiver(handler), IsNull(handler)));

    return IsNull(handler);
  }

  void GotoIfProxyRevoked(Node* object, Label* if_proxy_revoked) {
    Label continue_checks(this);
    GotoIfNot(IsJSProxy(object), &continue_checks);
    GotoIf(IsProxyRevoked(object), if_proxy_revoked);
    Goto(&continue_checks);
    BIND(&continue_checks);
  }

  Node* AllocateProxy(Node* target, Node* handler, Node* context) {
    VARIABLE(map, MachineRepresentation::kTagged);

    Label callable_target(this), constructor_target(this), none_target(this),
        create_proxy(this);

    Node* nativeContext = LoadNativeContext(context);

    Branch(IsCallable(target), &callable_target, &none_target);

    BIND(&callable_target);
    {
      // Every object that is a constructor is implicitly callable
      // so it's okay to nest this check here
      GotoIf(IsConstructor(target), &constructor_target);
      map.Bind(
          LoadContextElement(nativeContext, Context::PROXY_CALLABLE_MAP_INDEX));
      Goto(&create_proxy);
    }
    BIND(&constructor_target);
    {
      map.Bind(LoadContextElement(nativeContext,
                                  Context::PROXY_CONSTRUCTOR_MAP_INDEX));
      Goto(&create_proxy);
    }
    BIND(&none_target);
    {
      map.Bind(LoadContextElement(nativeContext, Context::PROXY_MAP_INDEX));
      Goto(&create_proxy);
    }

    BIND(&create_proxy);
    Node* proxy = Allocate(JSProxy::kSize);
    StoreMapNoWriteBarrier(proxy, map.value());
    StoreObjectFieldRoot(proxy, JSProxy::kPropertiesOrHashOffset,
                         Heap::kEmptyPropertiesDictionaryRootIndex);
    StoreObjectFieldNoWriteBarrier(proxy, JSProxy::kTargetOffset, target);
    StoreObjectFieldNoWriteBarrier(proxy, JSProxy::kHandlerOffset, handler);
    StoreObjectFieldNoWriteBarrier(proxy, JSProxy::kHashOffset,
                                   UndefinedConstant());

    return proxy;
  }

  Node* AllocateJSArrayForCodeStubArguments(Node* context,
                                            CodeStubArguments& args, Node* argc,
                                            ParameterMode mode) {
    Node* array = nullptr;
    Node* elements = nullptr;
    Node* native_context = LoadNativeContext(context);
    Node* array_map = LoadJSArrayElementsMap(PACKED_ELEMENTS, native_context);
    Node* argc_smi = ParameterToTagged(argc, mode);
    std::tie(array, elements) = AllocateUninitializedJSArrayWithElements(
        PACKED_ELEMENTS, array_map, argc_smi, nullptr, argc, INTPTR_PARAMETERS);

    StoreMapNoWriteBarrier(elements, Heap::kFixedArrayMapRootIndex);
    StoreObjectFieldNoWriteBarrier(elements, FixedArrayBase::kLengthOffset,
                                   argc_smi);

    VARIABLE(index, MachineType::PointerRepresentation());
    index.Bind(IntPtrConstant(FixedArrayBase::kHeaderSize - kHeapObjectTag));
    VariableList list({&index}, zone());
    args.ForEach(list, [this, elements, &index](Node* arg) {
      StoreNoWriteBarrier(MachineRepresentation::kTagged, elements,
                          index.value(), arg);
      Increment(index, kPointerSize);
    });
    return array;
  }
};

// ES6 section 26.2.1.1 Proxy ( target, handler ) for the [[Construct]] case.
TF_BUILTIN(ProxyConstructor_ConstructStub, ProxiesCodeStubAssembler) {
  int const kTargetArg = 0;
  int const kHandlerArg = 1;

  Node* argc =
      ChangeInt32ToIntPtr(Parameter(BuiltinDescriptor::kArgumentsCount));
  CodeStubArguments args(this, argc);

  Node* target = args.GetOptionalArgumentValue(kTargetArg);
  Node* handler = args.GetOptionalArgumentValue(kHandlerArg);
  Node* context = Parameter(BuiltinDescriptor::kContext);

  Label throw_proxy_non_object(this, Label::kDeferred),
      throw_proxy_handler_or_target_revoked(this, Label::kDeferred),
      return_create_proxy(this);

  GotoIf(TaggedIsSmi(target), &throw_proxy_non_object);
  GotoIfNot(IsJSReceiver(target), &throw_proxy_non_object);
  GotoIfProxyRevoked(target, &throw_proxy_handler_or_target_revoked);

  GotoIf(TaggedIsSmi(handler), &throw_proxy_non_object);
  GotoIfNot(IsJSReceiver(handler), &throw_proxy_non_object);
  GotoIfProxyRevoked(handler, &throw_proxy_handler_or_target_revoked);

  args.PopAndReturn(AllocateProxy(target, handler, context));

  BIND(&throw_proxy_non_object);
  ThrowTypeError(context, MessageTemplate::kProxyNonObject);

  BIND(&throw_proxy_handler_or_target_revoked);
  ThrowTypeError(context, MessageTemplate::kProxyHandlerOrTargetRevoked);
}

TF_BUILTIN(CallProxy, ProxiesCodeStubAssembler) {
  Node* argc = Parameter(Descriptor::kActualArgumentsCount);
  Node* argc_ptr = ChangeInt32ToIntPtr(argc);
  Node* proxy = Parameter(Descriptor::kFunction);
  Node* context = Parameter(Descriptor::kContext);

  CSA_ASSERT(this, IsJSProxy(proxy));
  CSA_ASSERT(this, IsCallable(proxy));

  Label throw_proxy_handler_revoked(this, Label::kDeferred),
      trap_undefined(this), trap_defined(this, Label::kDeferred);

  // 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
  Node* handler = LoadObjectField(proxy, JSProxy::kHandlerOffset);

  // 2. If handler is null, throw a TypeError exception.
  CSA_ASSERT(this, Word32Or(IsJSReceiver(handler), IsNull(handler)));
  GotoIf(IsNull(handler), &throw_proxy_handler_revoked);

  // 3. Assert: Type(handler) is Object.
  CSA_ASSERT(this, IsJSReceiver(handler));

  // 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
  Node* target = LoadObjectField(proxy, JSProxy::kTargetOffset);

  // 5. Let trap be ? GetMethod(handler, "apply").
  Handle<Name> trap_name = factory()->apply_string();
  Node* trap = GetProperty(context, handler, trap_name);

  // 6. If trap is undefined, then
  GotoIf(IsUndefined(trap), &trap_undefined);
  Branch(IsNull(trap), &trap_undefined, &trap_defined);

  BIND(&trap_defined);
  {
    CodeStubArguments args(this, argc_ptr);
    Node* receiver = args.GetReceiver();

    // 7. Let argArray be CreateArrayFromList(argumentsList).
    Node* array = AllocateJSArrayForCodeStubArguments(context, args, argc_ptr,
                                                      INTPTR_PARAMETERS);

    // 8. Return Call(trap, handler, «target, thisArgument, argArray»).
    Node* result = CallJS(CodeFactory::Call(isolate()), context, trap, handler,
                          target, receiver, array);
    args.PopAndReturn(result);
  }

  BIND(&trap_undefined);
  {
    // 6.a. Return Call(target, thisArgument, argumentsList).
    TailCallStub(CodeFactory::Call(isolate()), context, target, argc);
  }

  BIND(&throw_proxy_handler_revoked);
  {
    CallRuntime(Runtime::kThrowTypeError, context,
                SmiConstant(MessageTemplate::kProxyRevoked),
                StringConstant("apply"));
    Unreachable();
  }
}

}  // namespace internal
}  // namespace v8