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

Panic at JSON.stringify #540

Closed
nixijnat opened this issue Oct 13, 2023 · 0 comments
Closed

Panic at JSON.stringify #540

nixijnat opened this issue Oct 13, 2023 · 0 comments

Comments

@nixijnat
Copy link

go.mod:

require github.com/dop251/goja v0.0.0-20230919151941-fc55792775de

Run Code:

package main

import (
	"fmt"
	"log"

	"github.com/dop251/goja"
)

func main() {
	if err := run(); err != nil {
		log.Println("failed:", err)
	}
}

const script = `

function test(obj) {
	// return obj.MarshalJSON();
	return JSON.stringify(obj);
}

`

type PermissionType int

const (
	_ PermissionType = iota

	PermissionTypeList = 1
)

func (t PermissionType) MarshalJSON() ([]byte, error) {
	switch t {
	case PermissionTypeList:
		return []byte(`1`), nil
	}
	return nil, fmt.Errorf("invalid permission_type: %v", t)
}

func run() error {
	runtime := goja.New()
	_, err := runtime.RunString(script)
	if err != nil {
		return fmt.Errorf("run string: %w", err)
	}
	var fn func(obj interface{}) (interface{}, error)
	if err := runtime.ExportTo(runtime.Get("test"), &fn); err != nil {
		return fmt.Errorf("export: %w", err)
	}
	if fn == nil {
		return fmt.Errorf("export function is nil")
	}
	t := PermissionType(10)
	result, err := fn(t)
	if err != nil {
		return fmt.Errorf("exec: %w", err)
	}
	log.Println("result: ", result)
	return nil
}

It will panic with message:

 go run main.go
panic: invalid permission_type: 10 [recovered]
        panic: invalid permission_type: 10 [recovered]
        panic: invalid permission_type: 10 [recovered]
        panic: invalid permission_type: 10

goroutine 1 [running]:
github.com/dop251/goja.(*Runtime).runWrapped.func1()
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/runtime.go:2516 +0x174
panic({0x75cce0, 0xc00016aa50})
        /usr/local/go/src/runtime/panic.go:838 +0x207
github.com/dop251/goja.(*vm).handleThrow(0xc0000d4360, {0x75cce0, 0xc00016aa50})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:788 +0x497
github.com/dop251/goja.(*vm).try.func1()
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:807 +0x45
panic({0x75cce0, 0xc00016aa50})
        /usr/local/go/src/runtime/panic.go:838 +0x207
github.com/dop251/goja.(*vm).handleThrow(0xc0000d4360, {0x75cce0, 0xc00016aa50})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:788 +0x497
github.com/dop251/goja.(*vm).runTryInner.func1()
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:830 +0x45
panic({0x75cce0, 0xc00016aa50})
        /usr/local/go/src/runtime/panic.go:838 +0x207
github.com/dop251/goja.(*_builtinJSON_stringifyContext).str(0xc00011d0a8, {0x83bf90?, 0xbef220?}, 0xc00016f890)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/builtin_json.go:319 +0xa5a
github.com/dop251/goja.(*_builtinJSON_stringifyContext).do(0xc00011d0a8, {0x83bc00, 0xc00016f770})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/builtin_json.go:278 +0x26e
github.com/dop251/goja.(*Runtime).builtinJSON_stringify(0xc0?, {{0x83bc00, 0xc00016e570}, {0xc00014e290, 0x1, 0x7}})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/builtin_json.go:263 +0x4db
github.com/dop251/goja.(*nativeFuncObject).vmCall(0xc000168d20, 0xc0000d4360, 0x1)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/func.go:559 +0x1ae
github.com/dop251/goja.call.exec(0x9?, 0xc0000d4360)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:3366 +0x6b
github.com/dop251/goja.(*vm).run(0xc0000d4360)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:582 +0x62
github.com/dop251/goja.(*vm).runTryInner(0xc0000d4360?)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:834 +0x70
github.com/dop251/goja.(*baseJsFuncObject).__call(0xc00014e180, {0xc00016a9d0?, 0x1, 0x0?}, {0x0?, 0x0}, {0x83c3b8?, 0xc20e80?})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/func.go:426 +0x645
github.com/dop251/goja.(*baseJsFuncObject)._call(...)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/func.go:442
github.com/dop251/goja.(*baseJsFuncObject).call(0xc0000c35a8?, {{0x83c3b8, 0xc20e80}, {0xc00016a9d0, 0x1, 0x1}}, {0x0?, 0x0?})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/func.go:450 +0x7a
github.com/dop251/goja.(*baseJsFuncObject).Call(0xc0000c35b8?, {{0x83c3b8, 0xc20e80}, {0xc00016a9d0, 0x1, 0x1}})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/func.go:382 +0x45
github.com/dop251/goja.AssertFunction.func1.1()
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/runtime.go:2476 +0x74
github.com/dop251/goja.(*vm).try(0xc0000d4360, 0xc00011d800)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/vm.go:811 +0x230
github.com/dop251/goja.(*Runtime).runWrapped(0xc000080800, 0x7f9329ef75b8?)
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/runtime.go:2520 +0x7c
github.com/dop251/goja.AssertFunction.func1({0x83c3b8?, 0xc20e80?}, {0xc00016a9d0?, 0x0?, 0x10?})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/runtime.go:2475 +0x92
github.com/dop251/goja.(*Runtime).wrapJSFunc.func1({0xc0000ac5a0, 0x1, 0x2?})
        /home/good/code/go/pkg/mod/github.com/dop251/[email protected]/runtime.go:2284 +0x106
main.run()
        /home/good/code/go_repo/good/test.service.strategy/test/main.go:55 +0x147
main.main()
        /home/good/code/go_repo/good/test.service.strategy/test/main.go:11 +0x19
exit status 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants