Skip to content

Commit

Permalink
Merge pull request #9 from gopherjs/fp2
Browse files Browse the repository at this point in the history
More FP fixes
  • Loading branch information
flimzy authored Aug 4, 2023
2 parents b14cef0 + 2ae6f49 commit ddd73a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 5 additions & 2 deletions analysis/passes/jsobjectptr/jsobjectptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
if !internal.Is_jsObject(pass, node) {
return true
}
parent := stack[len(stack)-2]
parent, ok := internal.AncestorN(stack, 1)
if !ok {
return true
}
switch pt := parent.(type) {
case *ast.StarExpr:
// Fall through
return true
case *ast.CallExpr:
fun, ok := pt.Fun.(*ast.Ident)
if !ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
_ struct{ x js.Object } // want "js.Object must always be a pointer"
_ = struct{ x js.Object }{} // want "js.Object must always be a pointer"
_ = new(js.Object)
_ *js.Object
)

func _(js.Object) {} // want "js.Object must always be a pointer"
Expand All @@ -33,5 +34,9 @@ func _() js.Object { // want "js.Object must always be a pointer"
func _() {
var x any
_, _ = x.(js.Object) // want "js.Object must always be a pointer"
_ = x
}

func _() {
var x *js.Object
_ = *x // TODO: This should fail
}
10 changes: 10 additions & 0 deletions internal/jsobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ func Is_jsObject(pass *analysis.Pass, node ast.Node) bool {
}
return pkg.Path() == "github.com/gopherjs/gopherjs/js"
}

// AncestorN returns the nth ancestor from stack, and true, or nil and false if
// it does not exist.
func AncestorN(stack []ast.Node, n int) (ast.Node, bool) {
l := len(stack)
if n > l {
return nil, false
}
return stack[l-(n+1)], true
}

0 comments on commit ddd73a1

Please sign in to comment.