Questions about how to handle circular dependencies for module script

2 views
Skip to first unread message

Cheng You Bai

unread,
May 24, 2020, 8:31:53 AM5/24/20
to modul...@chromium.org
Hi kouhei, hiroshige and other folks in chromium module-dev team,

Nice to meet you all! I’m cybai, a Taiwanese Servo contributor.

Few months ago, I implemented module script, `<script type=module>`, for Servo but I thought I made too complicated.
Recently, I’m trying to refactor it to make it more readable and performant.

In my initial implementation, it was implemented by fetching a module graph with resolving a `Promise.all` promise.
Under the `Promise.all` way, each fetch for a module will be a promise and it will combine promises of its descendants with `Promise.all`; thus, when all descendants resolve their promises, Servo can know it can go ahead to notify the script to finish for calling `onload` or `onerror`.

But, I actually don’t need to combine promises with Promise.all; instead, Manish inspired me that I can do some manual checking when a module is fetched and then do some manual checking to see if we need to notify script to finish.

However, while working on the previous Promise.all way and the current manual checking way, I all need to do a recursive checking to see if the `going-to-finish` module is really ready to be finished; then, I will fail to finish for circular dependency modules.

While reading the implementation of Chromium, I saw Chromium uses a `num_incomplete_fetches_` counter to memoize if the module can be finished.
I wondered it’s used for solving the dependencies issue. However, with using a `incomplete` counter for Servo, I still can’t fix the circular dependencies issue but I can’t find how Chromium handle the circular module case.

I was guessing, is it handled by mojo? or is it handled by reference counting on the incomplete counter?

Could you elaborate how Chromium handle the circular dependencies for module script? (or is there any related public documents I can read?)

Thanks! and really thanks for letting me ask the question to the team! Thanks! 🙏

Sincerely,
Cheng-You Bai
Reply all
Reply to author
Forward
0 new messages