Skip to content

extend stdlib to allow import of more packages #1099

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 8 commits into from
Jun 23, 2020
Merged

extend stdlib to allow import of more packages #1099

merged 8 commits into from
Jun 23, 2020

Conversation

cornelk
Copy link
Contributor

@cornelk cornelk commented May 11, 2020

Closes #752

these changes allow a successful import of the following go 1.14 stdlib packages:

	_ "database/sql"
	_ "debug/elf"
	_ "debug/macho"
	_ "debug/pe"
	_ "debug/plan9obj"
	_ "html"
	_ "mime"
	_ "testing"

@aykevl
Copy link
Member

aykevl commented May 12, 2020

This PR conflicts with #1012, which I think should be merged first as it also cleans up the os package a bit.

@cornelk
Copy link
Contributor Author

cornelk commented May 13, 2020

@aykevl rebased now on current dev branch after #1012 got merged

)

func IsPermission(err error) bool {
return err == ErrPermission
Copy link
Member

Choose a reason for hiding this comment

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

Not complete (it doesn't unwrap the error) but I guess it's fine for now.

Comment on lines +488 to +489
// Lookup returns the value associated with key in the tag string.
func (tag StructTag) Lookup(key string) (value string, ok bool) {
Copy link
Member

Choose a reason for hiding this comment

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

Get and Lookup will need tests. Please add them to testdata/reflect.go. You can probably just try to lookup a few tags and if they are present, print them (just like many other properties of types/values are printed).

Copy link
Member

Choose a reason for hiding this comment

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

I think it may be better to use something other than a string for StructTag eventually. The last time I looked through the standard library, no package was using StructTag as a string but instead just called the Get and Lookup functions. Therefore, I think it would be feasible to do the key/value splitting at compile time, avoiding the code size cost of doing it at runtime (and possibly speeding things up as well, although that's not the goal).

Copy link
Member

Choose a reason for hiding this comment

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

Are you sure you actually need Get and Lookup? You could perhaps simplify things by stubbing them out. At least when I tested the encoding/json package, I think I managed to do without for a proof-of-concept.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the StructTag type with it's functions is actively used in common libraries github.com/BurntSushi/toml and gopkg.in/yaml.v2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added your proposal to do it at compile time as a TODO comment. For this MR i focused on getting more libraries to compile.

Copy link
Member

Choose a reason for hiding this comment

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

I checked both libraries.

  • github.com/BurntSushi/toml passes the .Tag field as a StructTag to getOptions and only uses the .Get function. That would allow doing the key/value parsing at compile time.
  • gopkg.in/yaml.v2 unfortunately does use the raw string. It looks like they fall back to using the whole string if the struct tag is not in a key/value format. I think they shouldn't do this as it breaks the convention, but it's probably hard to persuade them to change this (there is probably code that depends on this behavior).

Not sure what to do here, maybe using raw strings is better after all (even though it will likely increase code size). Maybe there are optimizations possible here, but I'm not sure whether that's possible in a sane way.

Copy link
Member

Choose a reason for hiding this comment

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

Did you write this code yourself or did you copy it? If you copied it, it should have a copyright note (just like the strconv.go file).

@cornelk
Copy link
Contributor Author

cornelk commented May 21, 2020

@aykevl i updated the PR

@deadprogram
Copy link
Member

@aykevl any further feedback? Seems like all of the comments have been addressed.

@deadprogram
Copy link
Member

Reminder to @aykevl about this PR. LGTM.

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

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

Thanks for this PR!
For the next time, please split it up a bit more so that the changes are a bit more focused. This helps with review and with investigating regressions (if there are any).

)

// ErrSyntax indicates that a value does not have the right syntax for the target type.
var ErrSyntax = badSyntax{}
Copy link
Member

Choose a reason for hiding this comment

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

This file adds a number of exported symbols. Because this is the reflect package, these (copied) symbols should not be exported.

Comment on lines +488 to +489
// Lookup returns the value associated with key in the tag string.
func (tag StructTag) Lookup(key string) (value string, ok bool) {
Copy link
Member

Choose a reason for hiding this comment

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

Did you write this code yourself or did you copy it? If you copied it, it should have a copyright note (just like the strconv.go file).

package runtime

// Stack is a stub, not implemented
func Stack(buf []byte, all bool) int {
Copy link
Member

Choose a reason for hiding this comment

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

You could add this to stack.go, which has similar functions (to avoid yet another small file).

None of these are likely to be implemented considering what TinyGo is designed for.

do not export strconv functions in reflect
@cornelk
Copy link
Contributor Author

cornelk commented Jun 18, 2020

@aykevl comments are addressed, I will split it up next time.

@deadprogram
Copy link
Member

Thank you very much for working on this contribution, @cornelk now that all of the comments have been addressed, I am going to squash/merge. Thanks again!

@deadprogram deadprogram merged commit 720a54a into tinygo-org:dev Jun 23, 2020
@deadprogram
Copy link
Member

The tests previous passed, but now the CI fails for all Linux platforms. Perhaps rebasing again against dev would have been a good idea before I merged this. My bad.

We now need to fix this right away.

@deadprogram
Copy link
Member

Thanks to @jaddr2line root problem was discovered (of course unrelated to this PR) and I just pushed a fix to the CircleCI build.

@niaow niaow added this to the v0.14 milestone Jun 27, 2020
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.

4 participants