forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSCallableProxy.cpp
More file actions
214 lines (197 loc) · 7.84 KB
/
JSCallableProxy.cpp
File metadata and controls
214 lines (197 loc) · 7.84 KB
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/JSCallableProxy.h"
#include "hermes/VM/JSArray.h"
namespace hermes {
namespace vm {
//===----------------------------------------------------------------------===//
// class JSCallableProxy
const CallableVTable JSCallableProxy::vt{
{
VTable(CellKind::CallableProxyKind, cellSize<JSCallableProxy>()),
JSCallableProxy::_getOwnIndexedRangeImpl,
JSCallableProxy::_haveOwnIndexedImpl,
JSCallableProxy::_getOwnIndexedPropertyFlagsImpl,
JSCallableProxy::_getOwnIndexedImpl,
JSCallableProxy::_setOwnIndexedImpl,
JSCallableProxy::_deleteOwnIndexedImpl,
JSCallableProxy::_checkAllOwnIndexedImpl,
},
JSCallableProxy::_newObjectImpl,
JSCallableProxy::_callImpl,
};
void CallableProxyBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
mb.addJSObjectOverlapSlots(JSObject::numOverlapSlots<JSCallableProxy>());
NativeFunctionBuildMeta(cell, mb);
const auto *self = static_cast<const JSCallableProxy *>(cell);
mb.addField("@target", &self->slots_.target);
mb.addField("@handler", &self->slots_.handler);
}
#ifdef HERMESVM_SERIALIZE
JSCallableProxy::JSCallableProxy(Deserializer &d)
: NativeFunction(
d,
&vt.base.base,
nullptr,
&JSCallableProxy::_proxyNativeCall) {
d.readRelocation(&slots_.target, RelocationKind::GCPointer);
d.readRelocation(&slots_.handler, RelocationKind::GCPointer);
}
void CallableProxySerialize(Serializer &s, const GCCell *cell) {
NativeFunction::serializeNativeFunctionImpl(
s, cell, JSObject::numOverlapSlots<JSCallableProxy>());
auto *self = vmcast<const JSCallableProxy>(cell);
s.writeRelocation(self->slots_.target.get(s.getRuntime()));
s.writeRelocation(self->slots_.handler.get(s.getRuntime()));
s.endObject(cell);
}
void CallableProxyDeserialize(Deserializer &d, CellKind kind) {
assert(kind == CellKind::CallableProxyKind && "Expected CallableProxy");
void *mem = d.getRuntime()->alloc(cellSize<JSCallableProxy>());
auto *cell = new (mem) JSCallableProxy(d);
d.endObject(cell);
}
#endif
PseudoHandle<JSCallableProxy> JSCallableProxy::create(Runtime *runtime) {
JSObjectAlloc<JSCallableProxy> mem{runtime};
JSCallableProxy *cproxy = new (mem) JSCallableProxy(
runtime,
runtime->objectPrototypeRawPtr,
runtime->getHiddenClassForPrototypeRaw(
runtime->objectPrototypeRawPtr,
JSObject::numOverlapSlots<JSCallableProxy>() +
ANONYMOUS_PROPERTY_SLOTS));
cproxy->flags_.proxyObject = true;
return mem.initToPseudoHandle(cproxy);
}
CallResult<HermesValue> JSCallableProxy::create(
Runtime *runtime,
Handle<JSObject> prototype) {
assert(
prototype.get() == runtime->objectPrototypeRawPtr &&
"JSCallableProxy::create() can only be used with object prototype");
return create(runtime).getHermesValue();
}
void JSCallableProxy::setTargetAndHandler(
Runtime *runtime,
Handle<JSObject> target,
Handle<JSObject> handler) {
slots_.target.set(runtime, target.get(), &runtime->getHeap());
slots_.handler.set(runtime, handler.get(), &runtime->getHeap());
}
CallResult<HermesValue>
JSCallableProxy::_proxyNativeCall(void *, Runtime *runtime, NativeArgs) {
// We don't use NativeArgs; the implementations just read the current
// stack frame directly.
StackFramePtr callerFrame = runtime->getCurrentFrame();
auto selfHandle =
Handle<JSCallableProxy>::vmcast(&callerFrame.getCalleeClosureOrCBRef());
Predefined::Str trapName = callerFrame->isConstructorCall()
? Predefined::construct
: Predefined::apply;
CallResult<Handle<Callable>> trapRes =
detail::findTrap(selfHandle, runtime, trapName);
if (trapRes == ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// detail::slots() will always return the slots_ field here, but
// this isn't a member, so it can't see it. It doesn't seem to be
// worth tweaking the abstractions to avoid the small overhead in
// this case.
Handle<JSObject> target =
runtime->makeHandle(detail::slots(*selfHandle).target);
// 6. If trap is undefined, then
if (!*trapRes) {
// a. Return ? Call(target, thisArgument, argumentsList).
// OR
// a. Assert: IsConstructor(target) is true.
// b. Return ? Construct(target, argumentsList, newTarget).
HermesValue newTarget = callerFrame->isConstructorCall()
? callerFrame.getNewTargetRef()
: HermesValue::encodeUndefinedValue();
ScopedNativeCallFrame newFrame{runtime,
callerFrame.getArgCount(),
target.getHermesValue(),
newTarget,
callerFrame.getThisArgRef()};
if (LLVM_UNLIKELY(newFrame.overflowed()))
return runtime->raiseStackOverflow(
Runtime::StackOverflowKind::NativeStack);
std::uninitialized_copy_n(
&(callerFrame->getArgRefUnsafe(0)),
callerFrame.getArgCount(),
&(newFrame->getArgRefUnsafe(0)));
// I know statically that target is a Callable, but storing it as
// a Callable makes it much harder to share all the JSProxy code,
// so we cast here.
auto res = Callable::call(Handle<Callable>::vmcast(target), runtime);
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return res->get();
}
// 7. Let argArray be CreateArrayFromList(argumentsList).
CallResult<PseudoHandle<JSArray>> argArrayRes = JSArray::create(
runtime, callerFrame->getArgCount(), callerFrame->getArgCount());
if (LLVM_UNLIKELY(argArrayRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
Handle<JSArray> argArray = runtime->makeHandle(std::move(*argArrayRes));
JSArray::setStorageEndIndex(argArray, runtime, callerFrame->getArgCount());
for (uint32_t i = 0; i < callerFrame->getArgCount(); ++i) {
JSArray::unsafeSetExistingElementAt(
*argArray, runtime, i, callerFrame.getArgRef(i));
}
// 8. Let newObj be ? Call(trap, handler, « target, argArray, newTarget »).
if (callerFrame->isConstructorCall()) {
CallResult<PseudoHandle<>> newObjRes = Callable::executeCall3(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue(),
argArray.getHermesValue(),
callerFrame.getNewTargetRef());
if (LLVM_UNLIKELY(newObjRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
// 9. If Type(newObj) is not Object, throw a TypeError exception.
if (!vmisa<JSObject>(newObjRes->get())) {
return runtime->raiseTypeError(
"Proxy construct trap returned non-Object");
}
// 10. return newObj.
return newObjRes->get();
} else {
// 8. Return ? Call(trap, handler, « target, thisArgument, argArray »).
auto res = Callable::executeCall3(
*trapRes,
runtime,
runtime->makeHandle(detail::slots(*selfHandle).handler),
target.getHermesValue(),
callerFrame.getThisArgRef(),
argArray.getHermesValue());
if (LLVM_UNLIKELY(res == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return res->get();
}
}
CallResult<PseudoHandle<JSObject>> JSCallableProxy::_newObjectImpl(
Handle<Callable> callable,
Runtime *runtime,
Handle<JSObject> protoHandle) {
if (!vmcast<JSCallableProxy>(*callable)->isConstructor(runtime)) {
return runtime->raiseTypeError("Function is not a constructor");
}
return vm::Callable::newObject(
Handle<Callable>::vmcast(
runtime->makeHandle(detail::slots(*callable).target)),
runtime,
protoHandle);
}
} // namespace vm
} // namespace hermes