Skip to content

Commit e345775

Browse files
committed
Remove Field::FindFieldIndex() and update service protocol
This paves the way to remove functions and fields from top-level classes. They are looked up via the class dictionary when resolving names. [email protected] Review URL: https://codereview.chromium.org/1477573002 .
1 parent 5b57280 commit e345775

File tree

5 files changed

+16
-101
lines changed

5 files changed

+16
-101
lines changed

runtime/observatory/tests/service/get_object_rpc_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ var tests = [
774774
(Isolate isolate) async {
775775
// Call eval to get a class id.
776776
var evalResult = await eval(isolate, 'new _DummyClass()');
777-
var id = "${evalResult['class']['id']}/fields/0";
777+
var id = "${evalResult['class']['id']}/fields/dummyVar";
778778
var params = {
779779
'objectId': id,
780780
};
@@ -796,7 +796,7 @@ var tests = [
796796
(Isolate isolate) async {
797797
// Call eval to get a class id.
798798
var evalResult = await eval(isolate, 'new _DummyClass()');
799-
var id = "${evalResult['class']['id']}/fields/9999";
799+
var id = "${evalResult['class']['id']}/fields/mythicalField";
800800
var params = {
801801
'objectId': id,
802802
};

runtime/vm/object.cc

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,46 +3074,6 @@ void Class::AddFields(const GrowableArray<const Field*>& new_fields) const {
30743074
}
30753075

30763076

3077-
intptr_t Class::FindFieldIndex(const Field& needle) const {
3078-
Thread* thread = Thread::Current();
3079-
if (EnsureIsFinalized(thread) != Error::null()) {
3080-
return -1;
3081-
}
3082-
REUSABLE_ARRAY_HANDLESCOPE(thread);
3083-
REUSABLE_FIELD_HANDLESCOPE(thread);
3084-
REUSABLE_STRING_HANDLESCOPE(thread);
3085-
Array& fields_array = thread->ArrayHandle();
3086-
Field& field = thread->FieldHandle();
3087-
String& field_name = thread->StringHandle();
3088-
fields_array ^= fields();
3089-
ASSERT(!fields_array.IsNull());
3090-
String& needle_name = String::Handle(thread->zone());
3091-
needle_name ^= needle.name();
3092-
const intptr_t len = fields_array.Length();
3093-
for (intptr_t i = 0; i < len; i++) {
3094-
field ^= fields_array.At(i);
3095-
field_name ^= field.name();
3096-
if (field_name.Equals(needle_name)) {
3097-
return i;
3098-
}
3099-
}
3100-
// No field found.
3101-
return -1;
3102-
}
3103-
3104-
3105-
RawField* Class::FieldFromIndex(intptr_t idx) const {
3106-
const Array& flds = Array::Handle(fields());
3107-
if ((idx < 0) || (idx >= flds.Length())) {
3108-
return Field::null();
3109-
}
3110-
Field& field = Field::Handle();
3111-
field ^= flds.At(idx);
3112-
ASSERT(!field.IsNull());
3113-
return field.raw();
3114-
}
3115-
3116-
31173077
template <class FakeInstance>
31183078
RawClass* Class::New(intptr_t index) {
31193079
ASSERT(Object::class_class() != Class::null());
@@ -7561,14 +7521,17 @@ const char* Field::ToCString() const {
75617521
"Field <%s.%s>:%s%s%s", cls_name, field_name, kF0, kF1, kF2);
75627522
}
75637523

7524+
75647525
void Field::PrintJSONImpl(JSONStream* stream, bool ref) const {
75657526
JSONObject jsobj(stream);
75667527
Class& cls = Class::Handle(owner());
7567-
intptr_t id = cls.FindFieldIndex(*this);
7568-
ASSERT(id >= 0);
7569-
intptr_t cid = cls.id();
7528+
String& field_name = String::Handle(name());
7529+
ASSERT(cls.LookupField(field_name) == this->raw());
7530+
field_name = String::EncodeIRI(field_name);
75707531
AddCommonObjectProperties(&jsobj, "Field", ref);
7571-
jsobj.AddFixedServiceId("classes/%" Pd "/fields/%" Pd "", cid, id);
7532+
jsobj.AddFixedServiceId("classes/%" Pd "/fields/%s",
7533+
cls.id(), field_name.ToCString());
7534+
75727535
const String& user_name = String::Handle(PrettyName());
75737536
const String& vm_name = String::Handle(name());
75747537
AddNameProperties(&jsobj, user_name, vm_name);

runtime/vm/object.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,8 +1148,6 @@ class Class : public Object {
11481148
void SetFields(const Array& value) const;
11491149
void AddField(const Field& field) const;
11501150
void AddFields(const GrowableArray<const Field*>& fields) const;
1151-
intptr_t FindFieldIndex(const Field& field) const;
1152-
RawField* FieldFromIndex(intptr_t idx) const;
11531151

11541152
// Returns an array of all fields of this class and its superclasses indexed
11551153
// by offset in words.

runtime/vm/object_test.cc

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3769,54 +3769,6 @@ static RawClass* GetClass(const Library& lib, const char* name) {
37693769
}
37703770

37713771

3772-
TEST_CASE(FindFieldIndex) {
3773-
const char* kScriptChars =
3774-
"class A {\n"
3775-
" var a;\n"
3776-
" var b;\n"
3777-
"}\n"
3778-
"class B {\n"
3779-
" var d;\n"
3780-
"}\n"
3781-
"test() {\n"
3782-
" new A();\n"
3783-
" new B();\n"
3784-
"}";
3785-
Dart_Handle h_lib = TestCase::LoadTestScript(kScriptChars, NULL);
3786-
EXPECT_VALID(h_lib);
3787-
Dart_Handle result = Dart_Invoke(h_lib, NewString("test"), 0, NULL);
3788-
EXPECT_VALID(result);
3789-
Library& lib = Library::Handle();
3790-
lib ^= Api::UnwrapHandle(h_lib);
3791-
EXPECT(!lib.IsNull());
3792-
const Class& class_a = Class::Handle(GetClass(lib, "A"));
3793-
const Array& class_a_fields = Array::Handle(class_a.fields());
3794-
const Class& class_b = Class::Handle(GetClass(lib, "B"));
3795-
const Field& field_a = Field::Handle(GetField(class_a, "a"));
3796-
const Field& field_b = Field::Handle(GetField(class_a, "b"));
3797-
const Field& field_d = Field::Handle(GetField(class_b, "d"));
3798-
intptr_t field_a_index = class_a.FindFieldIndex(field_a);
3799-
intptr_t field_b_index = class_a.FindFieldIndex(field_b);
3800-
intptr_t field_d_index = class_a.FindFieldIndex(field_d);
3801-
// Valid index.
3802-
EXPECT_GE(field_a_index, 0);
3803-
// Valid index.
3804-
EXPECT_GE(field_b_index, 0);
3805-
// Invalid index.
3806-
EXPECT_EQ(field_d_index, -1);
3807-
Field& field_a_from_index = Field::Handle();
3808-
field_a_from_index ^= class_a_fields.At(field_a_index);
3809-
ASSERT(!field_a_from_index.IsNull());
3810-
// Same field.
3811-
EXPECT_EQ(field_a.raw(), field_a_from_index.raw());
3812-
Field& field_b_from_index = Field::Handle();
3813-
field_b_from_index ^= class_a_fields.At(field_b_index);
3814-
ASSERT(!field_b_from_index.IsNull());
3815-
// Same field.
3816-
EXPECT_EQ(field_b.raw(), field_b_from_index.raw());
3817-
}
3818-
3819-
38203772
TEST_CASE(FindClosureIndex) {
38213773
// Allocate the class first.
38223774
const String& class_name = String::Handle(Symbols::New("MyClass"));

runtime/vm/service.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,22 +1271,24 @@ static RawObject* LookupHeapObjectClasses(Thread* thread,
12711271
return func.raw();
12721272

12731273
} else if (strcmp(parts[2], "fields") == 0) {
1274-
// Field ids look like: "classes/17/fields/11"
1274+
// Field ids look like: "classes/17/fields/name"
12751275
if (num_parts != 4) {
12761276
return Object::sentinel().raw();
12771277
}
1278-
intptr_t id;
1279-
if (!GetIntegerId(parts[3], &id)) {
1278+
const char* encoded_id = parts[3];
1279+
String& id = String::Handle(zone, String::New(encoded_id));
1280+
id = String::DecodeIRI(id);
1281+
if (id.IsNull()) {
12801282
return Object::sentinel().raw();
12811283
}
1282-
Field& field = Field::Handle(zone, cls.FieldFromIndex(id));
1284+
Field& field = Field::Handle(zone, cls.LookupField(id));
12831285
if (field.IsNull()) {
12841286
return Object::sentinel().raw();
12851287
}
12861288
return field.raw();
12871289

12881290
} else if (strcmp(parts[2], "functions") == 0) {
1289-
// Function ids look like: "classes/17/functions/11"
1291+
// Function ids look like: "classes/17/functions/name"
12901292
if (num_parts != 4) {
12911293
return Object::sentinel().raw();
12921294
}

0 commit comments

Comments
 (0)