Skip to content

Commit

Permalink
Correctly detect circular references in JSON.stringify for wrapped Go…
Browse files Browse the repository at this point in the history
… objects. Fixes #543.
  • Loading branch information
dop251 committed Oct 24, 2023
1 parent 873a149 commit 5944104
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (ctx *_builtinJSON_stringifyContext) str(key Value, holder *Object) bool {
ctx.buf.WriteString("null")
case *Object:
for _, object := range ctx.stack {
if value1 == object {
if value1.SameAs(object) {
ctx.r.typeErrorResult(true, "Converting circular structure to JSON")
}
}
Expand Down
17 changes: 17 additions & 0 deletions builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ func TestJSONMarshalObjectCircular(t *testing.T) {
}
}

func TestJSONStringifyCircularWrappedGo(t *testing.T) {
type CircularType struct {
Self *CircularType
}
vm := New()
v := CircularType{}
v.Self = &v
vm.Set("v", &v)
_, err := vm.RunString("JSON.stringify(v)")
if err == nil {
t.Fatal("Expected error")
}
if !strings.HasPrefix(err.Error(), "TypeError: Converting circular structure to JSON") {
t.Fatalf("Unexpected error: %v", err)
}
}

func TestJSONParseReviver(t *testing.T) {
// example from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Expand Down

0 comments on commit 5944104

Please sign in to comment.