Skip to content

Commit

Permalink
Merge pull request #510 from olegbespalov/master
Browse files Browse the repository at this point in the history
Make EOF in JSON.parse human-oriented.
  • Loading branch information
dop251 authored May 31, 2023
2 parents f5b4a75 + b244ee5 commit d7324b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goja
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math"
Expand All @@ -20,6 +21,9 @@ func (r *Runtime) builtinJSON_parse(call FunctionCall) Value {
d := json.NewDecoder(strings.NewReader(call.Argument(0).toString().String()))

value, err := r.builtinJSON_decodeValue(d)
if errors.Is(err, io.EOF) {
panic(r.newError(r.global.SyntaxError, "Unexpected end of JSON input (%v)", err.Error()))
}
if err != nil {
panic(r.newError(r.global.SyntaxError, err.Error()))
}
Expand Down
13 changes: 13 additions & 0 deletions builtin_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ func TestQuoteMalformedSurrogatePair(t *testing.T) {
testScript(`JSON.stringify("\uD800")`, asciiString(`"\ud800"`), t)
}

func TestEOFWrapping(t *testing.T) {
vm := New()

_, err := vm.RunString("JSON.parse('{')")
if err == nil {
t.Fatal("Expected error")
}

if !strings.Contains(err.Error(), "Unexpected end of JSON input") {
t.Fatalf("Error doesn't contain human-friendly wrapper: %v", err)
}
}

func BenchmarkJSONStringify(b *testing.B) {
b.StopTimer()
vm := New()
Expand Down

0 comments on commit d7324b2

Please sign in to comment.