How to use the @MirrorsUsed annotation?

289 views
Skip to first unread message

Georgios Valotasios

unread,
Mar 24, 2014, 5:56:07 PM3/24/14
to mi...@dartlang.org
Hi,
I'm maintaining a library in dart (mustache4dart) and lately I had a issue raised by a user regarding the file size of the produced js file. The problem is that I'm using the dart:mirrors package. I had a look at the documentation and this blog post but I could not really understand what I should do and how.

For example, I do have a library which is the only place where the mirrors package is being used. Should the annotation at that file be enough? In anyway, objects in javascript are just maps, so keeping the whole mirrors stuff looks like an overkill when all I want to do is to just get the value of a property of a javascript object. For dartlang itself the use of a helper class (if you have a look at the link there is a helper class for that reason called _ObjectReflector) looks ok but in js a property lookup is all that is needed.

So my question is, is there a way to understand on the runtime that my host environment is js and provide a more simple implementation of the _ObjectReflector? I could do that anyway and let the used configure what he wants. So in the client he could use the mirrorless implementation (causing hopefully dartjs to tree shake the mirrors stuff) but,doing it automatically would be much better.

In general the use of mirrors on the client side (where js is the only solution at the moment) looks a little bit problematic if a user has to go that far for something that simple.

Kind regards,
Yoryos

Joao Pedrosa

unread,
Mar 24, 2014, 6:32:06 PM3/24/14
to mi...@dartlang.org
Hi,

The Dart developers seem to have tried a lot of Mirrors themselves and I recall a recent discussion that they said they were still working on improving how Mirrors get used. Although they also seem concerned that Mirrors can continue to be an issue, so if you're concerned about saving bytes perhaps dropping Mirrors would be a better idea.

I recently tried creating a templating method mostly to be used on the server-side so users can create the HTML file with some embedded code to get data out of Dart and so on. It's considerably harder to setup than a simple runtime eval of Ruby/JavaScript. But it's still an OK approach once the obstacles are overcome.

In your library I think most obstacles can be overcome by the user supplying a map of name/value pairs to the templating engine, which your library already supports. So perhaps the issue is more one of being fully compliant with the more thorough JavaScript implementation of it. Maybe you should encourage the users to provide the map to bind to the variables. That may be the easiest solution.

After doing that, maybe create a second version of the library that included Mirrors for the users who wouldn't mind the extra bytes. Maybe to be used on the server-side.

The third option of creating a slimmer reflection implementation may be too much of a nuisance.

Cheers,
Joao






--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Günter Zöchbauer

unread,
Mar 25, 2014, 2:16:40 AM3/25/14
to mi...@dartlang.org
This might be interesting (haven't used it myself yet) http://pub.dartlang.org/packages/smoke
Just discovered it recently.

Georgios Valotasios

unread,
Mar 30, 2014, 9:04:16 AM3/30/14
to mi...@dartlang.org
Well as I understand in order to use this, you must be in control of the objects to be reflected which is not the case with mustache. What I need is to check if a given object (of any class) has some properties and then use them.

Georgios Valotasios

unread,
Mar 30, 2014, 9:11:21 AM3/30/14
to mi...@dartlang.org
It looks like this is the most reasonable solution. Or in other way, do not use mirrors when compiling down to js which is what I had to do finally. Still testing though.

I hope that there will be some improvement there as the "do not use mirrors" on the client side does not look so developer friendly to me. Of course it is not something that you regularly use but there should be at least a well documented walk around. What we get instead is an undocumented (or at least partially documented) experimental solution :( I hope that this could get the attention of the dart2js developers

Alex Tatumizer

unread,
Mar 30, 2014, 10:53:01 AM3/30/14
to mi...@dartlang.org
Please note that mirrors in dart are much more powerful than, say, reflection in java. There can be a million of good uses cases...
Losing this feature when you just gained it is sad.
And code generation based on smoke or something equivalent is not the same as runtime mirrors which give you access to runtime context, with all live  variables etc.
Plus, code generation is quite error-prone, with infinite number of corner cases, especially if you need several "layers" of code generation.

Dart was designed to be a "better javascript", but now Javascript is hurting dart brutally. 

Cassio Tavares

unread,
Apr 16, 2014, 1:33:16 AM4/16/14
to mi...@dartlang.org
I have a similar problem here. My js output is almost 6 MB and I don't know how to solve this problem.

I'm trying to use the MirrorsUsed annotation but it doesn't work. I keep receiving the message "Try adding '@MirrorsUsed...". I tried it in many different formats and none has worked.

I think that I'm gonna quit using it for now too.

Cassio Tavares

unread,
Apr 16, 2014, 5:23:34 PM4/16/14
to mi...@dartlang.org
Good and bad news.

I found that the problem gone away creating a separate library to hold the class that is using mirrors.

The problem is that i was using mirrors to convert objects to JSON, but after dart2js it returns an empty JSON object.
Reply all
Reply to author
Forward
0 new messages