Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

fix(parser): Do not use isInterface. Works around dartbug 9434 #22

Merged
merged 1 commit into from
Jun 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,35 @@ getterChild(value, childKey) {
}
}

if (isInterface(value, Getter) && value.containsKey(childKey)) {
return [true, value[childKey]];
} else {
InstanceMirror instanceMirror = reflect(value);
Symbol curSym = new Symbol(childKey);
try {
// maybe it is a member field?
return [true, instanceMirror.getField(curSym).reflectee];
} catch (e) {
// maybe it is a member method?
if (instanceMirror.type.members.containsKey(curSym)) {
MethodMirror methodMirror = instanceMirror.type.members[curSym];
return [true, _relaxFnArgs((args) {
if (args == null) args = [];
try {
return instanceMirror.invoke(curSym, args).reflectee;
} catch (e) {
throw "$e \n\n${e.stacktrace}";
}
})];
}
return [false, null];
// TODO: replace with isInterface(value, Getter) when dart:mirrors
// can support mixins.
try {
// containsKey() might not return a boolean, so explicitly test
// against true.
if (value.containsKey(childKey) == true) {
return [true, value[childKey]];
}
} on NoSuchMethodError catch(e) {}

InstanceMirror instanceMirror = reflect(value);
Symbol curSym = new Symbol(childKey);
try {
// maybe it is a member field?
return [true, instanceMirror.getField(curSym).reflectee];
} catch (e) {
// maybe it is a member method?
if (instanceMirror.type.members.containsKey(curSym)) {
MethodMirror methodMirror = instanceMirror.type.members[curSym];
return [true, _relaxFnArgs((args) {
if (args == null) args = [];
try {
return instanceMirror.invoke(curSym, args).reflectee;
} catch (e) {
throw "$e \n\n${e.stacktrace}";
}
})];
}
return [false, null];
}
}

Expand Down Expand Up @@ -195,10 +201,13 @@ getter(scope, locals, path) {
}

setterChild(obj, childKey, value) {
if (isInterface(obj, Setter)) {
// TODO: replace with isInterface(value, Setter) when dart:mirrors
// can support mixins.
try {
obj[childKey] = value;
return value;
}
} on NoSuchMethodError catch(e) {}

InstanceMirror instanceMirror = reflect(obj);
Symbol curSym = new Symbol(childKey);
try {
Expand Down
36 changes: 36 additions & 0 deletions test/parser_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ class TestData {
method() => "testMethod";
}

class Mixin {}
class MixedTestData extends TestData with Mixin {
}

class MapData {
operator[](x) => "mapped-$x";
containsKey(x) => true;
}
class MixedMapData extends MapData with Mixin { }
class InheritedMapData extends MapData { }

class BadContainsKeys {
containsKey(x) => null;
String str = "member";
}

class LexerExpect extends Expect {
LexerExpect(actual) : super(actual);
toBeToken(int index, String text) {
Expand Down Expand Up @@ -565,6 +581,26 @@ main() {
expect(Parser.parse('str="bob"')(data)).toEqual('bob');
expect(data.str).toEqual("bob");
});

it('should support member field getters from mixins', () {
MixedTestData data = new MixedTestData();
data.str = 'dole';
expect(Parser.parse('str')(data)).toEqual('dole');
});

it('should support map getters from superclass', () {
InheritedMapData mapData = new InheritedMapData();
expect(Parser.parse('notmixed')(mapData)).toEqual('mapped-notmixed');
});

it('should support map getters from mixins', () {
MixedMapData data = new MixedMapData();
expect(Parser.parse('str')(data)).toEqual('mapped-str');
});

iit('should gracefully handle bad containsKey', () {
expect(Parser.parse('str')(new BadContainsKeys())).toEqual('member');
});
});

describe('assignable', () {
Expand Down