2

I'm tinkering with transformers + "analyzer" pub packages and I want to build a 2 stage transformer.

The stages are:

  1. Build AST for each dart source file.
  2. Generate new sources from the first stage outputs(with information from all ASTs).

I need to pass AST from one stage to another without costly serialization/deserialization, but Asset(docs) can't be build from a class instances.

Is it possible to build a class instance snapshot and pass it into the next stage?

That's what I have found so far:

From "Snapshots in Dart" article:

The Dart VM uses the following kinds of snapshots:
...
An object snapshot. Messaging from one isolate to another is implemented in the Dart VM by creating a snapshot of the Dart object that needs to be sent to the other isolate.

API docs for dart:isolate:

In the special circumstances when two isolates share the same code and are running in the same process (e.g. isolates created via spawn), it is also possible to send object instances (which would be copied in the process).

If "object snapshot" has the same restrictions, then it can't help me.

But, Interestingly enough, it should be possible to create a "deep copy"(clone) of an object by looping it through spawned isolate.

In Issue 6459: clone instance(object) @lrn pointed out:

We generally don't want ways to create an object that are not going through the class' generative constructors. You can read what Joshua Bloch says about clone in Java: http://www.artima.com/intv/bloch13.html Even with that experience in mind, we might be able to avoid the interface problems, but we can't generally decide whether a deep or a shallow copy is the right thing to do. Only the object itself knows that.

So it's not recommended even if possible.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
JAre
  • 4,666
  • 3
  • 27
  • 45
  • 1
    Did you have a look at the [code_transformers package](http://pub.dartlang.org/packages/code_transformers)? – Alexandre Ardhuin Jul 16 '14 at 06:06
  • 1
    That's what I use for [zengen](http://pub.dartlang.org/packages/zengen). See https://github.com/a14n/zengen/blob/8ee849d44131b55fef6f5205c2a036d99685c73b/lib/transformer.dart#L68-L74 – Alexandre Ardhuin Jul 16 '14 at 06:34
  • @AlexandreArdhuin That's really cool. I was thinking about writing something like memoizer(@Cached in yours package), tail recursion optimization and stuff for debugging like call logger, asserter.. – JAre Jul 16 '14 at 07:02

0 Answers0