Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Import more top level declarations #164

Merged
merged 5 commits into from
Mar 25, 2024
Merged

Import more top level declarations #164

merged 5 commits into from
Mar 25, 2024

Conversation

mateuszradomski
Copy link
Contributor

Resolves L2B-4024 L2B-4023

Copy link

changeset-bot bot commented Mar 22, 2024

⚠️ No Changeset found

Latest commit: 99ad801

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@mateuszradomski mateuszradomski marked this pull request as ready for review March 25, 2024 09:43
@mateuszradomski mateuszradomski requested a review from adamiak March 25, 2024 09:44
Consider three files:
 - file A
 - file B
 - file C

The structure is the following:
 - file A does an import that looks like this `import { Decl, IDecl }`
   from file B.
 - file B declares the `Decl` object and does an import that looks like
   `import { IDecl }` from file C.
 - file C declares the `IDecl` object.

Now the flow of what happened:
 - the code sees that there is an import in file A so it calls
   `resolveFileImport()`.
 - that function sees that it will import only two things from file B.
 - it processes the first object (`Decl`), it finds it in the
   declarations of file B. It adds it to the `alreadyImportedObjects`
   map.
 - it processes the second object (`IDecl`), it does not find it in the
   declarations of file B. It calls itself recursively to resove all the
   imports of file B. IMPORTANT: it passes the `alreadyImportedObjects` as
   an argument to that call. That call **will** modify that object, filling
   it up with all the imports that file B does. When it finds the `IDecl`
   object from the imports that are happening what we want to have happened
   is to only include that `IDecl` object in the `alreadyImportedObjects`
   map. But that map becomes tainted. The solution to that problem is to do
   a deep copy of that object so any tainting is contained to that copy
   that is later dropped.

A note on performance. Copying is almost always not the thing we want to
do if we want to achieve high performance. But in this case this
solution is fine because this is a statistically rare case.
@mateuszradomski mateuszradomski merged commit dcfccf4 into main Mar 25, 2024
4 checks passed
@mateuszradomski mateuszradomski deleted the top-level-decl branch March 25, 2024 12:35
adamiak pushed a commit to l2beat/l2beat that referenced this pull request Apr 24, 2024
* Import more top level declarations

* Compact stuff

* Tests

* Fix a very rare edge case in the PolygonZKEVM fix

Consider three files:
 - file A
 - file B
 - file C

The structure is the following:
 - file A does an import that looks like this `import { Decl, IDecl }`
   from file B.
 - file B declares the `Decl` object and does an import that looks like
   `import { IDecl }` from file C.
 - file C declares the `IDecl` object.

Now the flow of what happened:
 - the code sees that there is an import in file A so it calls
   `resolveFileImport()`.
 - that function sees that it will import only two things from file B.
 - it processes the first object (`Decl`), it finds it in the
   declarations of file B. It adds it to the `alreadyImportedObjects`
   map.
 - it processes the second object (`IDecl`), it does not find it in the
   declarations of file B. It calls itself recursively to resove all the
   imports of file B. IMPORTANT: it passes the `alreadyImportedObjects` as
   an argument to that call. That call **will** modify that object, filling
   it up with all the imports that file B does. When it finds the `IDecl`
   object from the imports that are happening what we want to have happened
   is to only include that `IDecl` object in the `alreadyImportedObjects`
   map. But that map becomes tainted. The solution to that problem is to do
   a deep copy of that object so any tainting is contained to that copy
   that is later dropped.

A note on performance. Copying is almost always not the thing we want to
do if we want to achieve high performance. But in this case this
solution is fine because this is a statistically rare case.

* Changeset
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants