myitcv / gopherjs

A compiler from Go to JavaScript for running Go code in a browser
BSD 2-Clause "Simplified" License
21 stars 0 forks source link

MakeWrapper throws error when method with no return value called #9

Closed myitcv closed 6 years ago

myitcv commented 6 years ago

Copied from https://github.com/myitcv/gopherjs/pull/8#issuecomment-403266241 (thanks @antong)

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