Skip to content

Commit 2916168

Browse files
kvakilNo9
authored andcommitted
ScopeInfo is no longer a FixedArray
1 parent 696b250 commit 2916168

4 files changed

Lines changed: 61 additions & 26 deletions

File tree

src/llv8-constants.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ void ScopeInfo::Load() {
279279
kEmbeddedParamAndStackLocals = kStackLocalCountOffset != -1;
280280
kContextLocalCountOffset = LoadConstant("scopeinfo_idx_ncontextlocals");
281281
kVariablePartIndex = LoadConstant("scopeinfo_idx_first_vars");
282+
// Prior to Node.js v16, ScopeInfo inherited from FixedArray. In release
283+
// lines after Node.js v16, it no longer does.
284+
kIsFixedArray = LoadConstant("parent_ScopeInfo__FixedArray") != -1;
282285
}
283286

284287

src/llv8-constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class ScopeInfo : public Module {
207207
int64_t kContextLocalCountOffset;
208208
bool kEmbeddedParamAndStackLocals;
209209
int64_t kVariablePartIndex;
210+
bool kIsFixedArray;
210211

211212
protected:
212213
void Load();

src/llv8-inl.h

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -722,21 +722,28 @@ inline CheckedType<uintptr_t> JSTypedArray::GetData() {
722722
inline ScopeInfo::PositionInfo ScopeInfo::MaybePositionInfo(Error& err) {
723723
ScopeInfo::PositionInfo position_info = {
724724
.start_position = 0, .end_position = 0, .is_valid = false};
725-
int proper_index = ContextLocalIndex(err);
725+
auto kPointerSize = v8()->common()->kPointerSize;
726+
int bytes_offset = kPointerSize * ContextLocalIndex(err);
726727
if (err.Fail()) return position_info;
727728

728729
Smi context_local_count = ContextLocalCount(err);
729730
if (err.Fail()) return position_info;
730-
proper_index += context_local_count.GetValue() * 2;
731+
bytes_offset += 2 * kPointerSize * context_local_count.GetValue();
732+
733+
int64_t data_offset =
734+
v8()->scope_info()->kIsFixedArray ? v8()->fixed_array()->kDataOffset : 0;
735+
bytes_offset += data_offset;
731736

732737
int tries = 5;
733-
while (tries > 0 && proper_index < (Length(err).GetValue() - 1)) {
738+
while (tries > 0) {
734739
err = Error();
735740

736-
Smi maybe_start_position = Get<Smi>(proper_index, err);
741+
Smi maybe_start_position =
742+
HeapObject::LoadFieldValue<Smi>(bytes_offset, err);
737743
if (err.Success() && maybe_start_position.IsSmi(err)) {
738-
proper_index++;
739-
Smi maybe_end_position = Get<Smi>(proper_index, err);
744+
bytes_offset += kPointerSize;
745+
Smi maybe_end_position =
746+
HeapObject::LoadFieldValue<Smi>(bytes_offset, err);
740747
if (err.Success() && maybe_end_position.IsSmi(err)) {
741748
position_info.start_position = maybe_start_position.GetValue();
742749
position_info.end_position = maybe_end_position.GetValue();
@@ -746,7 +753,7 @@ inline ScopeInfo::PositionInfo ScopeInfo::MaybePositionInfo(Error& err) {
746753
}
747754

748755
tries--;
749-
proper_index++;
756+
bytes_offset += kPointerSize;
750757
}
751758
return position_info;
752759
}
@@ -1091,19 +1098,34 @@ inline Value Context::ContextSlot(int index, Error& err) {
10911098
}
10921099

10931100
inline Smi ScopeInfo::ParameterCount(Error& err) {
1094-
return FixedArray::Get<Smi>(v8()->scope_info()->kParameterCountOffset, err);
1101+
int64_t data_offset =
1102+
v8()->scope_info()->kIsFixedArray ? v8()->fixed_array()->kDataOffset : 0;
1103+
return HeapObject::LoadFieldValue<Smi>(
1104+
data_offset + v8()->scope_info()->kParameterCountOffset *
1105+
v8()->common()->kPointerSize,
1106+
err);
10951107
}
10961108

10971109
inline Smi ScopeInfo::StackLocalCount(Error& err) {
10981110
if (v8()->scope_info()->kStackLocalCountOffset == -1) {
10991111
return Smi(v8(), 0);
11001112
}
1101-
return FixedArray::Get<Smi>(v8()->scope_info()->kStackLocalCountOffset, err);
1113+
int64_t data_offset =
1114+
v8()->scope_info()->kIsFixedArray ? v8()->fixed_array()->kDataOffset : 0;
1115+
return HeapObject::LoadFieldValue<Smi>(
1116+
data_offset + v8()->scope_info()->kStackLocalCountOffset *
1117+
v8()->common()->kPointerSize,
1118+
err);
11021119
}
11031120

11041121
inline Smi ScopeInfo::ContextLocalCount(Error& err) {
1105-
return FixedArray::Get<Smi>(v8()->scope_info()->kContextLocalCountOffset,
1106-
err);
1122+
int64_t data_offset = v8()->scope_info()->kIsFixedArray
1123+
? v8()->fixed_array()->kDataOffset
1124+
: v8()->common()->kPointerSize;
1125+
return HeapObject::LoadFieldValue<Smi>(
1126+
data_offset + v8()->scope_info()->kContextLocalCountOffset *
1127+
v8()->common()->kPointerSize,
1128+
err);
11071129
}
11081130

11091131
inline int ScopeInfo::ContextLocalIndex(Error& err) {
@@ -1122,30 +1144,39 @@ inline int ScopeInfo::ContextLocalIndex(Error& err) {
11221144
}
11231145

11241146
inline String ScopeInfo::ContextLocalName(int index, Error& err) {
1125-
int proper_index = ContextLocalIndex(err) + index;
1147+
int64_t data_offset = v8()->scope_info()->kIsFixedArray
1148+
? v8()->fixed_array()->kDataOffset
1149+
: v8()->common()->kPointerSize;
1150+
int proper_index = data_offset + (ContextLocalIndex(err) + index) *
1151+
v8()->common()->kPointerSize;
11261152
if (err.Fail()) return String();
1127-
return FixedArray::Get<String>(proper_index, err);
1153+
return HeapObject::LoadFieldValue<String>(proper_index, err);
11281154
}
11291155

11301156
inline HeapObject ScopeInfo::MaybeFunctionName(Error& err) {
1131-
int proper_index = ContextLocalIndex(err);
1132-
if (err.Fail()) return HeapObject();
1133-
1134-
Smi context_local_count = ContextLocalCount(err);
1135-
if (err.Fail()) return HeapObject();
1136-
proper_index += context_local_count.GetValue() * 2;
1137-
11381157
// NOTE(mmarchini): FunctionName can be stored either in the first, second or
11391158
// third slot after ContextLocalCount. Since there are missing postmortem
11401159
// metadata to determine in which slot its being stored for the present
11411160
// ScopeInfo, we try to find it heuristically.
1142-
int tries = 3;
1161+
auto kPointerSize = v8()->common()->kPointerSize;
11431162
HeapObject likely_function_name;
1144-
while (tries > 0 && proper_index < Length(err).GetValue()) {
1163+
int bytes_offset = kPointerSize * ContextLocalIndex(err);
1164+
if (err.Fail()) return likely_function_name;
1165+
1166+
Smi context_local_count = ContextLocalCount(err);
1167+
if (err.Fail()) return likely_function_name;
1168+
bytes_offset += 2 * kPointerSize * context_local_count.GetValue();
1169+
1170+
int64_t data_offset =
1171+
v8()->scope_info()->kIsFixedArray ? v8()->fixed_array()->kDataOffset : 0;
1172+
bytes_offset += data_offset;
1173+
1174+
int tries = 5;
1175+
while (tries > 0) {
11451176
err = Error();
11461177

11471178
HeapObject maybe_function_name =
1148-
FixedArray::Get<HeapObject>(proper_index, err);
1179+
HeapObject::LoadFieldValue<HeapObject>(bytes_offset, err);
11491180
if (err.Success() && String::IsString(v8(), maybe_function_name, err)) {
11501181
likely_function_name = maybe_function_name;
11511182
if (*String(likely_function_name).Length(err) > 0) {
@@ -1154,7 +1185,7 @@ inline HeapObject ScopeInfo::MaybeFunctionName(Error& err) {
11541185
}
11551186

11561187
tries--;
1157-
proper_index++;
1188+
bytes_offset += kPointerSize;
11581189
}
11591190

11601191
if (likely_function_name.Check()) {

src/llv8.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ class NameDictionary : public FixedArray {
509509
inline int64_t Length(Error& err);
510510
};
511511

512-
class ScopeInfo : public FixedArray {
512+
class ScopeInfo : public HeapObject {
513513
public:
514-
V8_VALUE_DEFAULT_METHODS(ScopeInfo, FixedArray)
514+
V8_VALUE_DEFAULT_METHODS(ScopeInfo, HeapObject)
515515

516516
struct PositionInfo {
517517
int64_t start_position;

0 commit comments

Comments
 (0)