Skip to content

Add internal/reflectlite package support. #994

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

Merged
merged 1 commit into from
Mar 12, 2021

Conversation

nevkontakte
Copy link
Member

This package is meant to be a cut-down version of the reflect package,
which is used by packages like errors to avoid a cyclic dependency
with reflect.

This code is based on goplusjs/gopherjs@8e608e5.

The natives code of the reflectlite package is a subset of its
counterpart from reflect. Unfortunately, this requires us to duplicate
a substantial chunk of code with all the maintenance problems that stem
from it. I have considered two alternative approaches, none of which
worked out:

  • Automatically rewrite imports from internal/reflectlite to
    reflect. Unfortunately, this creates cyclic dependencies and
    causes compilation to fail.
  • Redefine types in reflectlite as aliases of their reflect
    counterparts. This both causes a cyclic dependency and creates a
    divergence between GopherJS and Go standard library. TinyGo seems to
    be doing that somehow, but I suspect that they are able to handle
    cyclic dependencies.

As a bottom line, duplicating a subset of reflect code is the lesser
evil that is available to us. In the long term we could try and converge
our reflect implementation with the upstream, so that this is no longer
our problem.

Updates #989.

Co-authored-by: visualfc visualfc@gmail.com

Copy link
Member

@flimzy flimzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admitedly, a very high-level review. I didn't read every function. I expect tests to tell us about correctness, more than review.

return StructFieldType(tt, i)
}

// Field returns the i'th struct field.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, maybe?

Suggested change
// Field returns the i'th struct field.
// Field returns the type of the i'th struct field.

This package is meant to be a cut-down version of the `reflect` package,
which is used by packages like `errors` to avoid a cyclic dependency
with `reflect`.

This code is based on goplusjs/gopherjs@8e608e5.

The natives code of the reflectlite package is a subset of its
counterpart from reflect.  Unfortunately, this requires us to duplicate
a substantial chunk of code with all the maintenance problems that stem
from it. I have considered two alternative approaches, none of which
worked out:

  - Automatically rewrite imports from `internal/reflectlite` to
    `reflect`. Unfortunately, this creates cyclic dependencies and
    causes compilation to fail.
  - Redefine types in reflectlite as aliases of their reflect
    counterparts. This both causes a cyclic dependency and creates a
    divergence between GopherJS and Go standard library. TinyGo seems to
    be doing that somehow, but I suspect that they are able to handle
    cyclic dependencies.

As a bottom line, duplicating a subset of reflect code is the lesser
evil that is available to us. In the long term we could try and converge
our reflect implementation with the upstream, so that this is no longer
our problem.

Co-authored-by: visualfc <visualfc@gmail.com>
@nevkontakte
Copy link
Member Author

Unfortunately, the inner workings of the GopherJS's reflection are not really documented and it is a fairly complex piece of code. Spending some time to document it and perhaps simplify would be a valuable contribution for sure…

@nevkontakte nevkontakte merged commit c4c6314 into gopherjs:go1.16-stdlib Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants