Skip to content
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

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

Merged
merged 1 commit into from Jul 7, 2018

Conversation

myitcv
Copy link
Owner

@myitcv myitcv commented 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.

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.
@myitcv myitcv merged commit 75a0cb9 into master Jul 7, 2018
@myitcv myitcv deleted the getters_myitcv branch July 7, 2018 08:53
@antong
Copy link

antong commented Jul 8, 2018

This seems to introduce an issue when the wrapped method has no return value. Calling a wrapped method where the go method has no return val throws

..js:2274 Uncaught TypeError: Cannot read property 'constructor' of undefined
    at $copyIfRequired (..js:2274)
    at Object.v.$externalizeWrapper [as Inc] (..js:2051)
    at <anonymous>:1:12
$copyIfRequired @ ..js:2274
v.$externalizeWrapper @ ..js:2051
(anonymous) @ VM671:1

Note that the issue is also introduced for the previously existing js.MakeWrapper(), not only for js.MakeFullWrapper()
Example code:

package main

import (
        "github.com/gopherjs/gopherjs/js"
)

type A struct {
        A int
}

func (a *A) Inc() {
        a.A++
}

func (a *A) GetVal() int {
        return a.A
}

func newA() *js.Object {
        return js.MakeWrapper(&A{})
}

func main() {
        js.Global.Set("tmp", map[string]interface{}{
                "NewA": newA,
        })
}

This gives:

a = tmp.NewA()
a.GetVal() // works -> 0
a.Inc()      // Uncaught TypeError: Cannot read property 'constructor' of undefined
a.GetVal() // works -> 1

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.

None yet

2 participants