Skip to content

Commit 5becebe

Browse files
ripsawridgeCommit bot
authored andcommitted
Wrap JSFunction bindings in a helper object.
We need to do other things with this bindings object, like store a feedback vector. Therefore, it's a good time to wrap it up in a helper class. BUG= Review URL: https://codereview.chromium.org/1369293003 Cr-Commit-Position: refs/heads/master@{#31044}
1 parent 8499fa2 commit 5becebe

9 files changed

Lines changed: 203 additions & 70 deletions

File tree

src/api.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,11 +4456,10 @@ Local<v8::Value> Function::GetBoundFunction() const {
44564456
if (!func->shared()->bound()) {
44574457
return v8::Undefined(reinterpret_cast<v8::Isolate*>(func->GetIsolate()));
44584458
}
4459-
i::Handle<i::FixedArray> bound_args = i::Handle<i::FixedArray>(
4460-
i::FixedArray::cast(func->function_bindings()));
4461-
i::Handle<i::Object> original(
4462-
bound_args->get(i::JSFunction::kBoundFunctionIndex),
4463-
func->GetIsolate());
4459+
i::Handle<i::BindingsArray> bound_args = i::Handle<i::BindingsArray>(
4460+
i::BindingsArray::cast(func->function_bindings()));
4461+
i::Handle<i::Object> original(bound_args->bound_function(),
4462+
func->GetIsolate());
44644463
return Utils::ToLocal(i::Handle<i::JSFunction>::cast(original));
44654464
}
44664465

src/debug/debug.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,8 @@ void Debug::FloodWithOneShot(Handle<JSFunction> function,
746746

747747

748748
void Debug::FloodBoundFunctionWithOneShot(Handle<JSFunction> function) {
749-
Handle<FixedArray> new_bindings(function->function_bindings());
750-
Handle<Object> bindee(new_bindings->get(JSFunction::kBoundFunctionIndex),
751-
isolate_);
749+
Handle<BindingsArray> new_bindings(function->function_bindings());
750+
Handle<Object> bindee(new_bindings->bound_function(), isolate_);
752751

753752
if (!bindee.is_null() && bindee->IsJSFunction()) {
754753
Handle<JSFunction> bindee_function(JSFunction::cast(*bindee));

src/objects-inl.h

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ bool Object::IsTypeFeedbackVector() const { return IsFixedArray(); }
731731

732732

733733
bool Object::IsLiteralsArray() const { return IsFixedArray(); }
734+
bool Object::IsBindingsArray() const { return IsFixedArray(); }
734735

735736

736737
bool Object::IsDeoptimizationInputData() const {
@@ -3506,6 +3507,75 @@ int LiteralsArray::literals_count() const {
35063507
}
35073508

35083509

3510+
Object* BindingsArray::get(int index) const { return FixedArray::get(index); }
3511+
3512+
3513+
void BindingsArray::set(int index, Object* value) {
3514+
FixedArray::set(index, value);
3515+
}
3516+
3517+
3518+
void BindingsArray::set(int index, Smi* value) {
3519+
FixedArray::set(index, value);
3520+
}
3521+
3522+
3523+
void BindingsArray::set(int index, Object* value, WriteBarrierMode mode) {
3524+
FixedArray::set(index, value, mode);
3525+
}
3526+
3527+
3528+
int BindingsArray::length() const { return FixedArray::length(); }
3529+
3530+
3531+
BindingsArray* BindingsArray::cast(Object* object) {
3532+
SLOW_DCHECK(object->IsBindingsArray());
3533+
return reinterpret_cast<BindingsArray*>(object);
3534+
}
3535+
3536+
void BindingsArray::set_feedback_vector(TypeFeedbackVector* vector) {
3537+
set(kVectorIndex, vector);
3538+
}
3539+
3540+
3541+
TypeFeedbackVector* BindingsArray::feedback_vector() const {
3542+
return TypeFeedbackVector::cast(get(kVectorIndex));
3543+
}
3544+
3545+
3546+
JSReceiver* BindingsArray::bound_function() const {
3547+
return JSReceiver::cast(get(kBoundFunctionIndex));
3548+
}
3549+
3550+
3551+
void BindingsArray::set_bound_function(JSReceiver* function) {
3552+
set(kBoundFunctionIndex, function);
3553+
}
3554+
3555+
3556+
Object* BindingsArray::bound_this() const { return get(kBoundThisIndex); }
3557+
3558+
3559+
void BindingsArray::set_bound_this(Object* bound_this) {
3560+
set(kBoundThisIndex, bound_this);
3561+
}
3562+
3563+
3564+
Object* BindingsArray::binding(int binding_index) const {
3565+
return get(kFirstBindingIndex + binding_index);
3566+
}
3567+
3568+
3569+
void BindingsArray::set_binding(int binding_index, Object* binding) {
3570+
set(kFirstBindingIndex + binding_index, binding);
3571+
}
3572+
3573+
3574+
int BindingsArray::bindings_count() const {
3575+
return length() - kFirstBindingIndex;
3576+
}
3577+
3578+
35093579
void HandlerTable::SetRangeStart(int index, int value) {
35103580
set(index * kRangeEntrySize + kRangeStartIndex, Smi::FromInt(value));
35113581
}
@@ -6322,13 +6392,13 @@ void JSFunction::set_literals(LiteralsArray* literals) {
63226392
}
63236393

63246394

6325-
FixedArray* JSFunction::function_bindings() {
6395+
BindingsArray* JSFunction::function_bindings() {
63266396
DCHECK(shared()->bound());
6327-
return literals_or_bindings();
6397+
return BindingsArray::cast(literals_or_bindings());
63286398
}
63296399

63306400

6331-
void JSFunction::set_function_bindings(FixedArray* bindings) {
6401+
void JSFunction::set_function_bindings(BindingsArray* bindings) {
63326402
DCHECK(shared()->bound());
63336403
// Bound function literal may be initialized to the empty fixed array
63346404
// before the bindings are set.

src/objects.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8815,6 +8815,47 @@ Handle<LiteralsArray> LiteralsArray::New(Isolate* isolate,
88158815
}
88168816

88178817

8818+
// static
8819+
Handle<BindingsArray> BindingsArray::New(Isolate* isolate,
8820+
Handle<TypeFeedbackVector> vector,
8821+
Handle<JSReceiver> bound_function,
8822+
Handle<Object> bound_this,
8823+
int number_of_bindings) {
8824+
Handle<FixedArray> bindings = isolate->factory()->NewFixedArray(
8825+
number_of_bindings + kFirstBindingIndex);
8826+
Handle<BindingsArray> casted_bindings = Handle<BindingsArray>::cast(bindings);
8827+
casted_bindings->set_feedback_vector(*vector);
8828+
casted_bindings->set_bound_function(*bound_function);
8829+
casted_bindings->set_bound_this(*bound_this);
8830+
return casted_bindings;
8831+
}
8832+
8833+
8834+
// static
8835+
Handle<JSArray> BindingsArray::CreateBoundArguments(
8836+
Handle<BindingsArray> bindings) {
8837+
int bound_argument_count = bindings->bindings_count();
8838+
Factory* factory = bindings->GetIsolate()->factory();
8839+
Handle<FixedArray> arguments = factory->NewFixedArray(bound_argument_count);
8840+
bindings->CopyTo(kFirstBindingIndex, *arguments, 0, bound_argument_count);
8841+
return factory->NewJSArrayWithElements(arguments);
8842+
}
8843+
8844+
8845+
// static
8846+
Handle<JSArray> BindingsArray::CreateRuntimeBindings(
8847+
Handle<BindingsArray> bindings) {
8848+
Factory* factory = bindings->GetIsolate()->factory();
8849+
// A runtime bindings array consists of
8850+
// [bound function, bound this, [arg0, arg1, ...]].
8851+
Handle<FixedArray> runtime_bindings =
8852+
factory->NewFixedArray(2 + bindings->bindings_count());
8853+
bindings->CopyTo(kBoundFunctionIndex, *runtime_bindings, 0,
8854+
2 + bindings->bindings_count());
8855+
return factory->NewJSArrayWithElements(runtime_bindings);
8856+
}
8857+
8858+
88188859
int HandlerTable::LookupRange(int pc_offset, int* stack_depth_out,
88198860
CatchPrediction* prediction_out) {
88208861
int innermost_handler = -1, innermost_start = -1;

src/objects.h

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
// - FixedArray
7878
// - DescriptorArray
7979
// - LiteralsArray
80+
// - BindingsArray
8081
// - HashTable
8182
// - Dictionary
8283
// - StringTable
@@ -941,6 +942,7 @@ template <class C> inline bool Is(Object* obj);
941942
V(LayoutDescriptor) \
942943
V(Map) \
943944
V(DescriptorArray) \
945+
V(BindingsArray) \
944946
V(TransitionArray) \
945947
V(LiteralsArray) \
946948
V(TypeFeedbackVector) \
@@ -4606,6 +4608,48 @@ class LiteralsArray : public FixedArray {
46064608
};
46074609

46084610

4611+
// A bindings array contains the bindings for a bound function. It also holds
4612+
// the type feedback vector.
4613+
class BindingsArray : public FixedArray {
4614+
public:
4615+
inline TypeFeedbackVector* feedback_vector() const;
4616+
inline void set_feedback_vector(TypeFeedbackVector* vector);
4617+
4618+
inline JSReceiver* bound_function() const;
4619+
inline void set_bound_function(JSReceiver* function);
4620+
inline Object* bound_this() const;
4621+
inline void set_bound_this(Object* bound_this);
4622+
4623+
inline Object* binding(int binding_index) const;
4624+
inline void set_binding(int binding_index, Object* binding);
4625+
inline int bindings_count() const;
4626+
4627+
static Handle<BindingsArray> New(Isolate* isolate,
4628+
Handle<TypeFeedbackVector> vector,
4629+
Handle<JSReceiver> bound_function,
4630+
Handle<Object> bound_this,
4631+
int number_of_bindings);
4632+
4633+
static Handle<JSArray> CreateBoundArguments(Handle<BindingsArray> bindings);
4634+
static Handle<JSArray> CreateRuntimeBindings(Handle<BindingsArray> bindings);
4635+
4636+
DECLARE_CAST(BindingsArray)
4637+
4638+
private:
4639+
static const int kVectorIndex = 0;
4640+
static const int kBoundFunctionIndex = 1;
4641+
static const int kBoundThisIndex = 2;
4642+
static const int kFirstBindingIndex = 3;
4643+
4644+
inline Object* get(int index) const;
4645+
inline void set(int index, Object* value);
4646+
inline void set(int index, Smi* value);
4647+
inline void set(int index, Object* value, WriteBarrierMode mode);
4648+
4649+
inline int length() const;
4650+
};
4651+
4652+
46094653
// HandlerTable is a fixed array containing entries for exception handlers in
46104654
// the code object it is associated with. The tables comes in two flavors:
46114655
// 1) Based on ranges: Used for unoptimized code. Contains one entry per
@@ -7176,8 +7220,8 @@ class JSFunction: public JSObject {
71767220
inline LiteralsArray* literals();
71777221
inline void set_literals(LiteralsArray* literals);
71787222

7179-
inline FixedArray* function_bindings();
7180-
inline void set_function_bindings(FixedArray* bindings);
7223+
inline BindingsArray* function_bindings();
7224+
inline void set_function_bindings(BindingsArray* bindings);
71817225

71827226
// The initial map for an object created by this constructor.
71837227
inline Map* initial_map();
@@ -7264,11 +7308,6 @@ class JSFunction: public JSObject {
72647308
static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
72657309
static const int kSize = kNextFunctionLinkOffset + kPointerSize;
72667310

7267-
// Layout of the bound-function binding array.
7268-
static const int kBoundFunctionIndex = 0;
7269-
static const int kBoundThisIndex = 1;
7270-
static const int kBoundArgumentsStartIndex = 2;
7271-
72727311
private:
72737312
DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
72747313
};

src/profiler/heap-snapshot-generator.cc

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,18 +1593,14 @@ void V8HeapExplorer::ExtractClosureReferences(JSObject* js_obj, int entry) {
15931593

15941594
JSFunction* func = JSFunction::cast(js_obj);
15951595
if (func->shared()->bound()) {
1596-
FixedArray* bindings = func->function_bindings();
1597-
SetNativeBindReference(js_obj, entry, "bound_this",
1598-
bindings->get(JSFunction::kBoundThisIndex));
1596+
BindingsArray* bindings = func->function_bindings();
1597+
SetNativeBindReference(js_obj, entry, "bound_this", bindings->bound_this());
15991598
SetNativeBindReference(js_obj, entry, "bound_function",
1600-
bindings->get(JSFunction::kBoundFunctionIndex));
1601-
for (int i = JSFunction::kBoundArgumentsStartIndex;
1602-
i < bindings->length(); i++) {
1603-
const char* reference_name = names_->GetFormatted(
1604-
"bound_argument_%d",
1605-
i - JSFunction::kBoundArgumentsStartIndex);
1599+
bindings->bound_function());
1600+
for (int i = 0; i < bindings->bindings_count(); i++) {
1601+
const char* reference_name = names_->GetFormatted("bound_argument_%d", i);
16061602
SetNativeBindReference(js_obj, entry, reference_name,
1607-
bindings->get(i));
1603+
bindings->binding(i));
16081604
}
16091605
}
16101606
}

src/runtime/runtime-debug.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,24 @@ MaybeHandle<JSArray> Runtime::GetInternalProperties(Isolate* isolate,
156156
RUNTIME_ASSERT_HANDLIFIED(function->function_bindings()->IsFixedArray(),
157157
JSArray);
158158

159-
Handle<FixedArray> bindings(function->function_bindings());
159+
Handle<BindingsArray> bindings(function->function_bindings());
160160

161161
Handle<FixedArray> result = factory->NewFixedArray(2 * 3);
162162
Handle<String> target =
163163
factory->NewStringFromAsciiChecked("[[TargetFunction]]");
164164
result->set(0, *target);
165-
result->set(1, bindings->get(JSFunction::kBoundFunctionIndex));
165+
result->set(1, bindings->bound_function());
166166

167167
Handle<String> bound_this =
168168
factory->NewStringFromAsciiChecked("[[BoundThis]]");
169169
result->set(2, *bound_this);
170-
result->set(3, bindings->get(JSFunction::kBoundThisIndex));
170+
result->set(3, bindings->bound_this());
171171

172-
Handle<FixedArray> arguments = factory->NewFixedArray(
173-
bindings->length() - JSFunction::kBoundArgumentsStartIndex);
174-
bindings->CopyTo(
175-
JSFunction::kBoundArgumentsStartIndex, *arguments, 0,
176-
bindings->length() - JSFunction::kBoundArgumentsStartIndex);
177172
Handle<String> bound_args =
178173
factory->NewStringFromAsciiChecked("[[BoundArgs]]");
179174
result->set(4, *bound_args);
180175
Handle<JSArray> arguments_array =
181-
factory->NewJSArrayWithElements(arguments);
176+
BindingsArray::CreateBoundArguments(bindings);
182177
result->set(5, *arguments_array);
183178
return factory->NewJSArrayWithElements(result);
184179
}

0 commit comments

Comments
 (0)