-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid trapping exceptions by using reflection #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,12 +124,30 @@ class _MustacheContext extends MustacheToString implements MustacheContext { | |
} | ||
|
||
dynamic _getActualValue(String key) { | ||
try { | ||
return ctx[key]; | ||
} catch (NoSuchMethodError) { | ||
//Try to make dart2js understand that when we define USE_MIRRORS = false | ||
//we do not want to use any reflector | ||
return (useMirrors && USE_MIRRORS) ? ctxReflector[key] : null; | ||
//Try to make dart2js understand that when we define USE_MIRRORS = false | ||
//we do not want to use any reflector as that inflates the generated | ||
//javascript. | ||
if (useMirrors && USE_MIRRORS) { | ||
if (reflect(ctx).type.instanceMembers.containsKey(new Symbol("[]"))) { | ||
MethodMirror m = reflect(ctx).type.instanceMembers[new Symbol("[]")]; | ||
TypeMirror reflectedString = reflectType(String); | ||
if (reflectedString.isAssignableTo(m.parameters[0].type)) { | ||
try { | ||
return ctx[key]; | ||
} catch (NoSuchMethodError) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe, here a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, if (useMirrors && USE_MIRRORS) is true and the attempt to use ctx[key] resulted in an exception, we returned ctxReflector[key]. So a noop here falls through to the base case of returning ctxReflector[key]. The other two if statements just try to get us to return ctxReflector[key] if we can predict in advance that calling ctx[key] will raise an exception. |
||
//This should never happen unless we were trapping a lower level | ||
//NoSuchMethodError before. Continue to do so to be bug-for-bug | ||
//compatible. | ||
} | ||
} | ||
} | ||
return ctxReflector[key]; | ||
} else { | ||
try { | ||
return ctx[key]; | ||
} catch (NoSuchMethodError) { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like dart2js has some problems with this one:
I'm afraid I have to trap some exception after all. Do you mind sharing your usecase? Maybe I can try the exception trapping deeper in order not to hit the performance .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That exception wouldn't be trapped in any case, because UnimplementedError is not a subclass of NoSuchMethodError. So it seems unlikely that reverting this change would correct that test. Does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, never mind... isAssignableTo isn't implemented. Ouch.
Well, trapping the exception isn't the problem, it is catching it that impacts performance in the vm. As long as catching it doesn't introduce a performance problem in dart2js, it should be safe to do that from my case's perspective.