Thanks to visit codestin.com
Credit goes to github.com

Skip to content

js: add MakeFullWrapper to expose exported methods and struct fields. #790

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

Closed
wants to merge 2 commits into from

Conversation

myitcv
Copy link
Member

@myitcv myitcv commented Apr 13, 2018

Depends on #797

See the full actual diff here:

myitcv/gopherjs@upgrade_to_circle_ci_2...myitcv:getters

Currently the documentation for js.MakeWrapper is:

"MakeWrapper creates a JavaScript object which has wrappers for the
exported methods of i. Use explicit getter and setter methods to expose
struct fields to JavaScript."

Where the value passed to MakeWrapper is a struct value (or more
interestingly a pointer to a struct value) we can actually auto-generate
getters and setters for exported fields in the Javascript world, rather
than requiring explicit getters and setters to be defined on the Go
side. We can also usefully expose a string representing the type of the wrapped value via a $type property.

This PR:

@myitcv
Copy link
Member Author

myitcv commented Apr 13, 2018

cc @mvdan - this should make the wrapping of https://godoc.org/mvdan.cc/sh/syntax much easier.

@dmitshur
Copy link
Member

Would this be a breaking API change for js.MakeWrapper users, or is it backwards compatible?

@myitcv
Copy link
Member Author

myitcv commented Apr 14, 2018

Would this be a breaking API change for js.MakeWrapper users, or is it backwards compatible?

Yes, I think it is backwards compatible, because we are only adding properties to the returned value.

I've just pushed up a revised version with a test that demonstrate a more interesting scenario, asserting both ways for the getter and setter.

@myitcv myitcv force-pushed the getters branch 3 times, most recently from 74f2f10 to 5f0dd87 Compare April 14, 2018 14:59
@mvdan
Copy link

mvdan commented Apr 14, 2018

I can vouch for the awesomeness of this PR. It has enabled me publishing a rough first version of a Go package of mine on NPM: https://www.npmjs.com/package/mvdan-sh

I can't speak about the code itself much, but it works very well :)

@myitcv myitcv force-pushed the getters branch 6 times, most recently from 969e860 to 911dcc9 Compare April 16, 2018 08:26
@myitcv
Copy link
Member Author

myitcv commented Apr 16, 2018

@hajimehoshi @shurcooL - this is now ready for review.

@mvdan - would appreciate if you can update us here on how your testing with this has gone? Thanks very much for helping to road-test the changes.

@mvdan
Copy link

mvdan commented Apr 16, 2018

The code that I have been using to expose the Go package as a JS module is here: https://github.com/mvdan/sh/tree/master/_js

So far it works great with a minimal amount of boilerplate. I have been pinging Paul about specific runtime errors and problems that I was hitting, which he has been pushing fixes here for. At this moment, MakeWrapper is definitely useful to me, as all the basic features of the Go library work great in JS.

I hope to get at least a few considerable users of the JS module in the next couple of weeks, so I am likely to provide more edge cases, potential bugs, and suggestions. But please don't let that hold back the reviewing and merging of this PR, as this seems like a great start to have in master.

@myitcv
Copy link
Member Author

myitcv commented Apr 20, 2018

@mvdan - rebased this against recent merges into master. Just an FYI for when you're testing things out.

@myitcv myitcv force-pushed the getters branch 2 times, most recently from 4335c97 to b7d633f Compare April 22, 2018 18:05
@mvdan
Copy link

mvdan commented Apr 27, 2018

Everything still works perfectly, at least as far as I have used and tested it.

Just yesterday I was finally able to pass objects back and forth between JS and Go multiple times. So now, with the shell package, one can parse a shell program (Go -> JS), walk the syntax tree (JS -> Go and Go -> JS for the callbacks), modify nodes, and print the modified shell source code back (JS -> Go).

The glue code is still in that _js dir I pasted above, if anyone is curious: https://github.com/mvdan/sh/blob/master/_js/main.go

No further requests or bug reports from me :) Happy to continue using this and report bugs on the tracker, once this is merged.

@myitcv
Copy link
Member Author

myitcv commented Apr 27, 2018

cc @mpl - this approach could I think help in perkeep world. Avoids the need for any serialising etc. Will ping you more details offline

@mpl
Copy link

mpl commented Apr 27, 2018

@myitcv ACK, thanks. next week though. busy with preparing release/presentation this week.

@myitcv myitcv force-pushed the getters branch 4 times, most recently from 4988687 to 27f7ade Compare July 7, 2018 07:54
@myitcv myitcv changed the title js: auto-generate getters and setters for calls to MakeWrapper js: add MakeFullWrapper to expose exported methods and struct fields. Jul 7, 2018
Currently the documentation for js.MakeWrapper is:

> "MakeWrapper creates a JavaScript object which has wrappers for the
exported methods of i. Use explicit getter and setter methods to expose
struct fields to JavaScript."

Where the value a struct value (or more
interestingly a pointer to a struct value) we can actually auto-generate
getters and setters for exported fields in the JavaScript world, rather
than requiring explicit getters and setters to be defined on the Go
side.

We do this via a new MakeFullWrapper method.
@mvdan
Copy link

mvdan commented Jul 8, 2018

Perhaps I'm missing something as I'm not involved in GopherJS development - has there been any progress in getting this reviewed and merged? :)

@paralin
Copy link
Contributor

paralin commented Aug 3, 2018

GopherJS seems frozen right now! Need a merge here too :)

@mvdan
Copy link

mvdan commented Sep 1, 2018

If anyone else needs this, just use Paul's fork at https://github.com/myitcv/gopherjs - it includes this enhancement, as well as others like Go module support.

@myitcv
Copy link
Member Author

myitcv commented Sep 1, 2018

Just one note @mvdan - all this is currently sitting in the https://github.com/myitcv/gopherjs/tree/go1.11 branch of my fork.

There is a chain of PRs that, once merged, will enable full versioning of the fork with versions like 11.0.0 which will correspond to Go 1.11, 11.1.0 which will correspond to Go 1.11.1, 11.1.1 which will correspond to a point release of GopherJS between Go 1.11.1 and Go 1.11.2 etc.

@JounQin
Copy link
Contributor

JounQin commented Mar 31, 2022

Is this going to happen?

@nevkontakte
Copy link
Member

@JounQin If memory serves, @flimzy and I discussed this and it seemed like a good thing to have, but it needs to be rebased before if can be merged. One of us was going to do it once there isn't a more pressing matter (like Go 1.18 support, sigh), but so far neither of us has gotten around to it. If you are interested and willing to do the rebase, it would be a welcome contribution.

@JounQin
Copy link
Contributor

JounQin commented Apr 4, 2022

@nevkontakte

See #1112.

I'm a js/ts frontend developer, not a go expert actually :(

Hope it helps.

@nevkontakte
Copy link
Member

@JounQin and I am not really a frontend developer (despite working on GopherJS), so our areas of competences together should give a pretty good coverage :) Thanks for doing the rebase, I'll close this PR then, and continue on #1112.

@nevkontakte nevkontakte closed this Apr 4, 2022
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.

8 participants